Ma-Dan / keras-yolo4

A Keras implementation of YOLOv4 (Tensorflow backend)
MIT License
382 stars 176 forks source link

ValueError: Shapes (1, 1, 1024, 75) and (255, 1024, 1, 1) are incompatible #36

Open mayurmahurkar opened 4 years ago

mayurmahurkar commented 4 years ago
Traceback (most recent call last):
  File "test.py", line 58, in <module>
    yolo4_model.load_weights(model_path)
  File "/usr/local/lib/python3.6/dist-packages/keras/engine/saving.py", line 492, in load_wrapper
    return load_function(*args, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/keras/engine/network.py", line 1230, in load_weights
    f, self.layers, reshape=reshape)
  File "/usr/local/lib/python3.6/dist-packages/keras/engine/saving.py", line 1237, in load_weights_from_hdf5_group
    K.batch_set_value(weight_value_tuples)
  File "/usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py", line 2960, in batch_set_value
    tf_keras_backend.batch_set_value(tuples)
  File "/home/mayur/.local/lib/python3.6/site-packages/tensorflow_core/python/keras/backend.py", line 3323, in batch_set_value
    x.assign(np.asarray(value, dtype=dtype(x)))
  File "/home/mayur/.local/lib/python3.6/site-packages/tensorflow_core/python/ops/resource_variable_ops.py", line 819, in assign
    self._shape.assert_is_compatible_with(value_tensor.shape)
  File "/home/mayur/.local/lib/python3.6/site-packages/tensorflow_core/python/framework/tensor_shape.py", line 1110, in assert_is_compatible_with
    raise ValueError("Shapes %s and %s are incompatible" % (self, other))
ValueError: Shapes (1, 1, 1024, 75) and (255, 1024, 1, 1) are incompatible
mayurmahurkar commented 4 years ago

Facing this issue in test.py

FrancescoManigrass commented 3 years ago

how did you solve?

Ivan-basis commented 3 years ago

that means that some model's layers have different shape then the weights you load. This is surely because you created the model with another number of classes (not 80), so the conv_110 layer has (1, 1, 1024, 75) weights' shape instead of (1, 1, 1024, 255) that the model with weights you load was. So, you need to use yolo4_model.load_weights(model_path, by_name=True, skip_mismatch=True) to load only coinciding layer's weights without errors. In particular, in this case all except the last 3 layers will get the weights from the checkpoint and the last 3 layers will be randomly initialized and must be trained for your task.

carloscamposalcocer commented 3 years ago

@Ivan-basis is right, use coco_classes.txt instead of voc_classes.txt

urbansound8K commented 2 years ago

@Ivan-basis is there any way to fix it before we convert the weight?

I am using the converted model for another app and getting the same prob? could you help, please?