voxelmorph icon indicating copy to clipboard operation
voxelmorph copied to clipboard

Error while loading vxm_dense_brain_T1_3D_mse.h5

Open srivathsapv opened this issue 3 years ago • 2 comments

I am using Keras 2.3.0 and tensorflow 1.14

I initialize the dense model like this

enc_nf = [16, 32, 32, 32]
dec_nf = [32, 32, 32, 32, 32, 16, 16]

model = vxm.networks.VxmDense(
    inshape=(160, 192, 224),
    nb_unet_features=[enc_nf, dec_nf],
    bidir=False,
    use_probs=False,
    int_steps=7,
    int_downsize=2,
    src_feats=1,
    trg_feats=1
)

and then I do

model.load_weights('vxm_dense_brain_T1_3D_mse.h5')

I get an error as follows

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-49-90e78dfcb397> in <module>
----> 1 model.load_weights('/home/srivathsa/projects/studies/gad/vmorph/tutorial_data/vxm_dense_brain_T1_3D_mse.h5')

~/miniconda3/envs/voxelmorph/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py in load_weights(self, filepath, by_name)
    160         raise ValueError('Load weights is not yet supported with TPUStrategy '
    161                          'with steps_per_run greater than 1.')
--> 162     return super(Model, self).load_weights(filepath, by_name)
    163 
    164   @trackable.no_automatic_dependency_tracking

~/miniconda3/envs/voxelmorph/lib/python3.7/site-packages/tensorflow/python/keras/engine/network.py in load_weights(self, filepath, by_name)
   1422         saving.load_weights_from_hdf5_group_by_name(f, self.layers)
   1423       else:
-> 1424         saving.load_weights_from_hdf5_group(f, self.layers)
   1425 
   1426   def _updated_config(self):

~/miniconda3/envs/voxelmorph/lib/python3.7/site-packages/tensorflow/python/keras/saving/hdf5_format.py in load_weights_from_hdf5_group(f, layers)
    750     symbolic_weights = _legacy_weights(layer)
    751     weight_values = preprocess_weights_for_loading(
--> 752         layer, weight_values, original_keras_version, original_backend)
    753     if len(weight_values) != len(symbolic_weights):
    754       raise ValueError('Layer #' + str(k) + ' (named "' + layer.name +

~/miniconda3/envs/voxelmorph/lib/python3.7/site-packages/tensorflow/python/keras/saving/hdf5_format.py in preprocess_weights_for_loading(layer, weights, original_keras_version, original_backend)
    457       print(weights[0].shape)
    458       print(layer.weights[0].shape)
--> 459       weights[0] = np.transpose(weights[0], (3, 2, 0, 1))
    460       if layer.__class__.__name__ == 'ConvLSTM2D':
    461         weights[1] = np.transpose(weights[1], (3, 2, 0, 1))

<__array_function__ internals> in transpose(*args, **kwargs)

~/miniconda3/envs/voxelmorph/lib/python3.7/site-packages/numpy/core/fromnumeric.py in transpose(a, axes)
    658 
    659     """
--> 660     return _wrapfunc(a, 'transpose', axes)
    661 
    662 

~/miniconda3/envs/voxelmorph/lib/python3.7/site-packages/numpy/core/fromnumeric.py in _wrapfunc(obj, method, *args, **kwds)
     55 
     56     try:
---> 57         return bound(*args, **kwds)
     58     except TypeError:
     59         # A TypeError occurs if the object does have such a method in its

ValueError: axes don't match array

I further debug and find that the shape of weights of the layer vxm_dense_unet_dec_final_conv_0 is (3, 3, 3, 32, 32) in the h5 file but in the model definition above it is (3, 3, 3, 48, 32).

How can I fix this inconsistency? Thanks

srivathsapv avatar Jun 30 '21 21:06 srivathsapv

I have a follow up question:

The pretrained weights vxm_dense_brain_T1_3D_mse.h5 - is this the model weights trained using 3000+ 3D T1W brain MRI images as described in this paper?

srivathsapv avatar Jun 30 '21 21:06 srivathsapv

I get the same question. And I have found a way to solve this in voxelmorph/scripts/tf/test.py. You can try to use model = vxm.networks.VxmDense.load('../mods/vxm_dense_brain_T1_3D_mse.h5') to load this model.

MrPolong avatar Mar 08 '22 12:03 MrPolong