0

这是我的网络。我加载了权重,然后微调了网络。建筑始终保持不变。但是当我在微调后加载权重(block5 和 fc 层可训练)时,权重值中的权重顺序发生了变化,因此加载权重失败。

input_layer = Input(shape=(img_width,img_height,3),name = 'image_input')

model_vgg16_conv = VGG16(weights='imagenet',
        include_top=False,input_shape=(200,200,3))
output_vgg16_conv = model_vgg16_conv(input_layer)
model_vgg16_conv.summary()
fl = Flatten(name='flatten')(output_vgg16_conv)
dense = Dense(512, 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)

微调前:

<HDF5 group "/image_input" (0 members)> []
<HDF5 group "/vgg16" (13 members)> [<HDF5 dataset "kernel:0": shape (3, 3, 3, 64), type "<f4">, <HDF5 dataset "bias:0": shape (64,), type "<f4">, <HDF5 dataset "kernel:0": shape (3, 3, 64, 64), type "<f4">, <HDF5 dataset "bias:0": shape (64,), type "<f4">, <HDF5 dataset "kernel:0": shape (3, 3, 64, 128), type "<f4">, <HDF5 dataset "bias:0": shape (128,), type "<f4">, <HDF5 dataset "kernel:0": shape (3, 3, 128, 128), type "<f4">, <HDF5 dataset "bias:0": shape (128,), type "<f4">, <HDF5 dataset "kernel:0": shape (3, 3, 128, 256), type "<f4">, <HDF5 dataset "bias:0": shape (256,), type "<f4">, <HDF5 dataset "kernel:0": shape (3, 3, 256, 256), type "<f4">, <HDF5 dataset "bias:0": shape (256,), type "<f4">, <HDF5 dataset "kernel:0": shape (3, 3, 256, 256), type "<f4">, <HDF5 dataset "bias:0": shape (256,), type "<f4">, <HDF5 dataset "kernel:0": shape (3, 3, 256, 512), type "<f4">, <HDF5 dataset "bias:0": shape (512,), type "<f4">, <HDF5 dataset "kernel:0": shape (3, 3, 512, 512), type "<f4">, <HDF5 dataset "bias:0": shape (512,), type "<f4">, <HDF5 dataset "kernel:0": shape (3, 3, 512, 512), type "<f4">, <HDF5 dataset "bias:0": shape (512,), type "<f4">, <HDF5 dataset "kernel:0": shape (3, 3, 512, 512), type "<f4">, <HDF5 dataset "bias:0": shape (512,), type "<f4">, <HDF5 dataset "kernel:0": shape (3, 3, 512, 512), type "<f4">, <HDF5 dataset "bias:0": shape (512,), type "<f4">, <HDF5 dataset "kernel:0": shape (3, 3, 512, 512), type "<f4">, <HDF5 dataset "bias:0": shape (512,), type "<f4">]
<HDF5 group "/flatten" (0 members)> []
<HDF5 group "/fc1" (1 members)> [<HDF5 dataset "kernel:0": shape (18432, 512), type "<f4">, <HDF5 dataset "bias:0": shape (512,), type "<f4">]
<HDF5 group "/drop" (0 members)> []
<HDF5 group "/predictions" (1 members)> [<HDF5 dataset "kernel:0": shape (512, 40), type "<f4">, <HDF5 dataset "bias:0": shape (40,), type "<f4">]

微调后权重不会加载,因此会出现错误:

<HDF5 group "/image_input" (0 members)> []
<HDF5 group "/vgg16" (13 members)> [<HDF5 dataset "kernel:0": shape (3, 3, 512, 512), type "<f4">, <HDF5 dataset "bias:0": shape (512,), type "<f4">, <HDF5 dataset "kernel:0": shape (3, 3, 512, 512), type "<f4">, <HDF5 dataset "bias:0": shape (512,), type "<f4">, <HDF5 dataset "kernel:0": shape (3, 3, 512, 512), type "<f4">, <HDF5 dataset "bias:0": shape (512,), type "<f4">, <HDF5 dataset "kernel:0": shape (3, 3, 3, 64), type "<f4">, <HDF5 dataset "bias:0": shape (64,), type "<f4">, <HDF5 dataset "kernel:0": shape (3, 3, 64, 64), type "<f4">, <HDF5 dataset "bias:0": shape (64,), type "<f4">, <HDF5 dataset "kernel:0": shape (3, 3, 64, 128), type "<f4">, <HDF5 dataset "bias:0": shape (128,), type "<f4">, <HDF5 dataset "kernel:0": shape (3, 3, 128, 128), type "<f4">, <HDF5 dataset "bias:0": shape (128,), type "<f4">, <HDF5 dataset "kernel:0": shape (3, 3, 128, 256), type "<f4">, <HDF5 dataset "bias:0": shape (256,), type "<f4">, <HDF5 dataset "kernel:0": shape (3, 3, 256, 256), type "<f4">, <HDF5 dataset "bias:0": shape (256,), type "<f4">, <HDF5 dataset "kernel:0": shape (3, 3, 256, 256), type "<f4">, <HDF5 dataset "bias:0": shape (256,), type "<f4">, <HDF5 dataset "kernel:0": shape (3, 3, 256, 512), type "<f4">, <HDF5 dataset "bias:0": shape (512,), type "<f4">, <HDF5 dataset "kernel:0": shape (3, 3, 512, 512), type "<f4">, <HDF5 dataset "bias:0": shape (512,), type "<f4">, <HDF5 dataset "kernel:0": shape (3, 3, 512, 512), type "<f4">, <HDF5 dataset "bias:0": shape (512,), type "<f4">]
<HDF5 group "/flatten" (0 members)> []
<HDF5 group "/fc1" (1 members)> [<HDF5 dataset "kernel:0": shape (18432, 512), type "<f4">, <HDF5 dataset "bias:0": shape (512,), type "<f4">]
<HDF5 group "/drop" (0 members)> []
<HDF5 group "/predictions" (1 members)> [<HDF5 dataset "kernel:0": shape (512, 40), type "<f4">, <HDF5 dataset "bias:0": shape (40,), type "<f4">]
Traceback (most recent call last):
  File "construct_index.py", line 87, in <module>
    fine_model.load_weights(filepath)
  File "/usr/local/lib/python2.7/site-packages/keras/engine/topology.py", line 2538, in load_weights
    load_weights_from_hdf5_group(f, self.layers)
  File "/usr/local/lib/python2.7/site-packages/keras/engine/topology.py", line 2970, in load_weights_from_hdf5_group
    K.batch_set_value(weight_value_tuples)
  File "/usr/local/lib/python2.7/site-packages/keras/backend/tensorflow_backend.py", line 2153, in batch_set_value
    get_session().run(assign_ops, feed_dict=feed_dict)
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 778, in run
    run_metadata_ptr)
  File "/usr/local/lib/python2.7/site-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 (3, 3, 512, 512) for Tensor u'Placeholder:0', which has shape '(3, 3, 3, 64)'

由于某种原因,顺序发生了变化!

请帮忙,训练这个网络花了这么多天,我不能减掉这些重量。谢谢

4

0 回答 0