I have some trouble loading pre-trained weights with Keras.
Let's say I have a keras model model
and that my weights are stored at my_weights.h5
.
I try to load my weights as follow :
model.load_weights("my_weights.h5", by_name=True)
But this give me the following error :
Layer #1 (named "conv2d_1"), weight <tf.Variable 'conv2d_1/kernel:0' shape=(3, 3, 32, 64) dtype=float32> has shape (3, 3, 32, 64), but the saved weight has shape (32, 3, 3, 3).
So I tried to see what was the shape of my weights and my model structure :
for layer in model_body.layers :
print(layer.name+" : input ("+str(layer.input_shape)+") output ("+str(layer.output_shape)+")")
print("__")
with h5py.File(weights_filepath, 'r') as f:
for k in f.keys():
for l in f[k].keys():
for m in f[k][l].keys():
print(k+ " : " + m + " : " + str(f[k][l][m].shape))
conv2d_1 : input ((None, None, None, 32)) output ((None, None, None, 64))
__
conv2d_1 : kernel:0 : (3, 3, 3, 32)
(I kept only the layer that appear in the error)
By seeing this, I don't understand why the shapes mismatch, and where the shape (3, 3, 32, 64)
in the error come from). Am I missing something ?
model
and the original model which was used to generatemy_weights.h5
. Comparesummary()
of both the models, with special attention to the layer names (sinceby_name=True
is being used here), and see if there is a discrepancy. $\endgroup$keras.models.load_model
instead of defining your own architecture. If you already haven't use this as reference - github.com/qqwweee/keras-yolo3. $\endgroup$load_weights()
function throws the error when I am using Keras from Tensorflow (from tensorflow.python.keras import backend as K
), but the same code work well when using Keras that is not included in Tensorflow (from keras import backend as K
). $\endgroup$