5

我首先使用以下方法在我的数据集上冻结了 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 有问题吗?

4

2 回答 2

3

fine_modelModel其中的另一个Model(即ResNet50)。似乎问题是save_weight()并且load_weight()无法Model正确处理这种类型的嵌套 s。

也许您可以尝试以不会导致“嵌套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)
...
于 2017-07-25T03:16:21.503 回答
0

以下程序通常对我有用:

  1. 将您的重量加载到冷冻模型中。

  2. 将图层更改为可训练的。

  3. 编译模型。

即在这种情况下:

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()
于 2018-05-06T17:20:17.787 回答