我有一个使用python 3.6训练的keras模型,并且使用带有python 3.5的raspbian。
当您将使用python 3.6训练的模型(或至少我的模型)加载到python 3.6中时,您会遇到异常:
IndexError: tuple index out of range
问题是由于不同的原因我不能将训练平台改为3.5或RPi改为3.6,所以我必须转换model.h5。
有没有办法将h5转换为中间值,然后在其他平台中从中间转换为h5?
调用load_module时错误上升
问题是由于不同的原因我不能将训练平台改为3.5或RPi改为3.6,所以我必须转换de model.h5。
有没有办法将h5转换为中间值,然后在其他平台中从中间转换为h5?
load_model("model1527371035.h5")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.5/dist-packages/keras/models.py", line 270, in load_model
model = model_from_config(model_config, custom_objects=custom_objects)
File "/usr/local/lib/python3.5/dist-packages/keras/models.py", line 347, in model_from_config
return layer_module.deserialize(config, custom_objects=custom_objects)
File "/usr/local/lib/python3.5/dist-packages/keras/layers/__init__.py", line 55, in deserialize
printable_module_name='layer')
File "/usr/local/lib/python3.5/dist-packages/keras/utils/generic_utils.py", line 144, in deserialize_keras_object
list(custom_objects.items())))
File "/usr/local/lib/python3.5/dist-packages/keras/models.py", line 1412, in from_config
model.add(layer)
File "/usr/local/lib/python3.5/dist-packages/keras/models.py", line 497, in add
layer(x)
File "/usr/local/lib/python3.5/dist-packages/keras/engine/topology.py", line 619, in __call__
output = self.call(inputs, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/keras/layers/core.py", line 685, in call
return self.function(inputs, **arguments)
File "<ipython-input-11-b85ceb3c6761>", line 64, in <lambda>
IndexError: tuple index out of range
模型如下:
model = Sequential()
model.add(Lambda(lambda x: x / 255.0 - 0.5, input_shape=(84, 84, 3)))
model.add(BatchNormalization())
model.add(Conv2D(36,(5,5), strides=(2,2), activation='relu'))
model.add(Dropout(dropout))
model.add(Conv2D(64,(3,3), activation='relu'))
model.add(Dropout(dropout))
model.add(Flatten())
model.add(Dropout(dropout))
model.add(Dense(40))
model.add(Dropout(dropout))
model.add(Dense(10))
model.add(Dense(6, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer="adam",metrics=['mae', 'acc'])
答案 0 :(得分:2)
是的,通过正在进行的issue查看评论,当前的解决方法看起来就像保存和加载权重一样:
model.save_weights(filename)
# you have to rebuild model again
model.load_weights(filename)
这种情况下保存的文件不包含架构,您每次都必须重建它。这不是昂贵的,所以它应该不是问题。
编辑:这可能只影响Lambda
图层,可能是一个简单的custom layer可以避免此问题:
class MyLayer(Layer):
def call(self, x):
return x / 255.0 - 0.5