[英]Not able to load weights for fine tuning in Keras with ResNet50
I first trained with ResNet-50 layers frozen on my dataset using the following : 我首先使用以下方法在我的数据集上冻结了ResNet-50图层:
model_r50 = ResNet50(weights='imagenet', include_top=False)
model_r50.summary()
input_layer = Input(shape=(img_width,img_height,3),name = 'image_input')
output_r50 = model_r50(input_layer)
fl = Flatten(name='flatten')(output_r50)
dense = Dense(1024, activation='relu', name='fc1')(fl)
drop = Dropout(0.5, name='drop')(dense)
pred = Dense(nb_classes, activation='softmax', name='predictions')(drop)
fine_model = Model(outputs=pred,inputs=input_layer)
for layer in model_r50.layers:
layer.trainable = False
print layer
fine_model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
fine_model.summary()
I then try to fine tune it with layers unfrozen using the following: 然后,我尝试使用以下方法对图层解冻进行微调:
model_r50 = ResNet50(weights='imagenet', include_top=False)
model_r50.summary()
input_layer = Input(shape=(img_width,img_height,3),name = 'image_input')
output_r50 = model_r50(input_layer)
fl = Flatten(name='flatten')(output_r50)
dense = Dense(1024, activation='relu', name='fc1')(fl)
drop = Dropout(0.5, name='drop')(dense)
pred = Dense(nb_classes, activation='softmax', name='predictions')(drop)
fine_model = Model(outputs=pred,inputs=input_layer)
weights = 'val54_r50.01-0.86.hdf5'
fine_model.load_weights('models/'+weights)
fine_model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
fine_model.summary()
But I get this error out of nowhere. 但是我无处可去。 I just unfroze the network and didnt change anything!
我刚刚解冻网络并没有改变任何东西!
load_weights_from_hdf5_group(f, self.layers)
File "/usr/local/lib/python2.7/dist-packages/keras/engine/topology.py", line 3008, in load_weights_from_hdf5_group
K.batch_set_value(weight_value_tuples)
File "/usr/local/lib/python2.7/dist-packages/keras/backend/tensorflow_backend.py", line 2189, in batch_set_value
get_session().run(assign_ops, feed_dict=feed_dict)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 778, in run
run_metadata_ptr)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 961, in _run
% (np_val.shape, subfeed_t.name, str(subfeed_t.get_shape())))
ValueError: Cannot feed value of shape (128,) for Tensor u'Placeholder_140:0', which has shape '(512,)'
And it's not consistent. 而且它并不一致。 I get a different shape most of the time.
我大部分时间都有不同的形状。 Why is this happening?
为什么会这样? If I just change ResNet to VGG19 this won't happen.
如果我只是将ResNet更改为VGG19,则不会发生这种情况。 Is there a problem with ResNet in Keras?
Keras的ResNet有问题吗?
Your fine_model
is a Model
with another Model
(ie ResNet50
) inside it. 你的
fine_model
是一个Model
,里面有另一个Model
(即ResNet50
)。 It seems that the problem is save_weight()
and load_weight()
cannot handle this type of nested Model
s correctly. 似乎问题是
save_weight()
和load_weight()
无法正确处理这种类型的嵌套Model
。
Maybe you can try to build model in a way that does not result in a "nested Model
". 也许您可以尝试以不会导致“嵌套
Model
”的方式构建模型。 For example, 例如,
input_layer = Input(shape=(img_width, img_height, 3), name='image_input')
model_r50 = ResNet50(weights='imagenet', include_top=False, input_tensor=input_layer)
output_r50 = model_r50.output
fl = Flatten(name='flatten')(output_r50)
...
The following procedure usually worked for me: 以下程序通常对我有用:
Load your weights into the frozen model. 将权重加载到冻结模型中。
Change the layers to be trainable. 将图层更改为可训练。
Compile the model. 编译模型。
Ie in this case: 即在这种情况下:
model_r50 = ResNet50(weights='imagenet', include_top=False)
model_r50.summary()
input_layer = Input(shape=(img_width,img_height,3),name = 'image_input')
output_r50 = model_r50(input_layer)
fl = Flatten(name='flatten')(output_r50)
dense = Dense(1024, activation='relu', name='fc1')(fl)
drop = Dropout(0.5, name='drop')(dense)
pred = Dense(nb_classes, activation='softmax', name='predictions')(drop)
fine_model = Model(outputs=pred,inputs=input_layer)
for layer in model_r50.layers:
layer.trainable = False
print layer
weights = 'val54_r50.01-0.86.hdf5'
fine_model.load_weights('models/'+weights)
for layer in model_r50.layers:
layer.trainable = True
fine_model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
fine_model.summary()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.