[英]Not able to load weights for fine tuning in Keras with ResNet50
我首先使用以下方法在我的數據集上凍結了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()
然后,我嘗試使用以下方法對圖層解凍進行微調:
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()
但是我無處可去。 我剛剛解凍網絡並沒有改變任何東西!
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,)'
而且它並不一致。 我大部分時間都有不同的形狀。 為什么會這樣? 如果我只是將ResNet更改為VGG19,則不會發生這種情況。 Keras的ResNet有問題嗎?
你的fine_model
是一個Model
,里面有另一個Model
(即ResNet50
)。 似乎問題是save_weight()
和load_weight()
無法正確處理這種類型的嵌套Model
。
也許您可以嘗試以不會導致“嵌套Model
”的方式構建模型。 例如,
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)
...
以下程序通常對我有用:
將權重加載到凍結模型中。
將圖層更改為可訓練。
編譯模型。
即在這種情況下:
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.