Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 :

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. Is there a problem with ResNet in Keras?

like image 401
Hooli Avatar asked Oct 18 '22 08:10

Hooli


1 Answers

Your fine_model is a Model with another Model (i.e. ResNet50) inside it. It seems that the problem is save_weight() and load_weight() cannot handle this type of nested Models correctly.

Maybe you can try to build model in a way that does not result in a "nested 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)
...
like image 147
Yu-Yang Avatar answered Oct 21 '22 03:10

Yu-Yang