Tag Archives: Tensorflow/Keras Error reading weights

[Solved] Tensorflow/Keras Error reading weights: ValueError: axes don‘t match array

Error information:

Traceback (most recent call last):
  File "bs.py", line 149, in <module>
    tcpserver1=MYTCPServer(('192.168.0.109',54321)) 
  File "wserver_bs.py", line 65, in __init__
    self.model.load_weights(weight_filepath)
  File "/home/ps/anaconda3/envs/anomaly/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 162, in load_weights
    return super(Model, self).load_weights(filepath, by_name)
  File "/home/ps/anaconda3/envs/anomaly/lib/python3.6/site-packages/tensorflow/python/keras/engine/network.py", line 1424, in load_weights
    saving.load_weights_from_hdf5_group(f, self.layers)
  File "/home/ps/anaconda3/envs/anomaly/lib/python3.6/site-packages/tensorflow/python/keras/saving/hdf5_format.py", line 749, in load_weights_from_hdf5_group
    layer, weight_values, original_keras_version, original_backend)
  File "/home/ps/anaconda3/envs/anomaly/lib/python3.6/site-packages/tensorflow/python/keras/saving/hdf5_format.py", line 456, in preprocess_weights_for_loading
    weights[0] = np.transpose(weights[0], (3, 2, 0, 1))
  File "<__array_function__ internals>", line 6, in transpose
  File "/home/ps/anaconda3/envs/anomaly/lib/python3.6/site-packages/numpy/core/fromnumeric.py", line 653, in transpose
    return _wrapfunc(a, 'transpose', axes)
  File "/home/ps/anaconda3/envs/anomaly/lib/python3.6/site-packages/numpy/core/fromnumeric.py", line 58, in _wrapfunc
    return bound(*args, **kwds)
ValueError: axes don't match array

Tossed half a night, the Internet to check a variety of methods are useless
the fault lies in load_ On the weights function, it’s a matter of setting the model!

When loading the model, the default input size is used

model = cnn.CNNLikeModel() 

The actual size of the input tensor is different from the default size, which leads to this error.

solve

1. Modify the default value of the function
2. Fill in the correct value when using this function.