question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error loading trained models

See original GitHub issue

Hello author(s), Amazing work and thank you for sharing your code! I am trying to extend your work by training models using Spinal CT images and I am running into a problem when loading the models trained with your code. I am using .nii files scaled [0, 1] and of size (128, 128, 96) to train and have seemingly successfully run 1500 epochs, however, only the model trained on the first epoch will load when running the register.py file. All others fail with the following error:

  File "/home/phoenixyuwilkie/miniconda3/envs/obl_tf/lib/python3.6/site-packages/tensorflow/python/ops/array_ops.py", line 1694, in ones
    tensor_shape.TensorShape(shape))
  File "/home/phoenixyuwilkie/miniconda3/envs/obl_tf/lib/python3.6/site-packages/tensorflow/python/framework/constant_op.py", line 250, in _tensor_shape_tensor_conversion_function
    "Cannot convert a partially known TensorShape to a Tensor: %s" % s)
ValueError: Cannot convert a partially known TensorShape to a Tensor: (1, ?, ?, ?, 3)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "register.py", line 103, in <module>
    register(**vars(args))
  File "register.py", line 66, in register
    net = keras.models.load_model(model_file, custom_objects=custom_objects)
  File "/home/phoenixyuwilkie/.local/lib/python3.6/site-packages/keras/engine/saving.py", line 419, in load_model
    model = _deserialize_model(f, custom_objects, compile)
  File "/home/phoenixyuwilkie/.local/lib/python3.6/site-packages/keras/engine/saving.py", line 312, in _deserialize_model
    sample_weight_mode=sample_weight_mode)
  File "/home/phoenixyuwilkie/.local/lib/python3.6/site-packages/keras/engine/training.py", line 342, in compile
    sample_weight, mask)
  File "/home/phoenixyuwilkie/.local/lib/python3.6/site-packages/keras/engine/training_utils.py", line 404, in weighted
    score_array = fn(y_true, y_pred)
  File "/home/phoenixyuwilkie/Documents/Sam/obl_voxelmorph/src/losses.py", line 221, in kl_loss
    self.D = self._degree_matrix(self.flow_vol_shape)
  File "/home/phoenixyuwilkie/Documents/Sam/obl_voxelmorph/src/losses.py", line 168, in _degree_matrix
    z = K.ones([1] + sz)
  File "/home/phoenixyuwilkie/.local/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py", line 734, in ones
    v = tf.ones(shape=shape, dtype=tf_dtype, name=name)
  File "/home/phoenixyuwilkie/miniconda3/envs/obl_tf/lib/python3.6/site-packages/tensorflow/python/ops/array_ops.py", line 1697, in ones
    shape = ops.convert_to_tensor(shape, dtype=dtypes.int32)
  File "/home/phoenixyuwilkie/miniconda3/envs/obl_tf/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 1050, in convert_to_tensor
    as_ref=False)
  File "/home/phoenixyuwilkie/miniconda3/envs/obl_tf/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 1146, in internal_convert_to_tensor
    ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
  File "/home/phoenixyuwilkie/miniconda3/envs/obl_tf/lib/python3.6/site-packages/tensorflow/python/framework/constant_op.py", line 229, in _constant_tensor_conversion_function
    return constant(v, dtype=dtype, name=name)
  File "/home/phoenixyuwilkie/miniconda3/envs/obl_tf/lib/python3.6/site-packages/tensorflow/python/framework/constant_op.py", line 208, in constant
    value, dtype=dtype, shape=shape, verify_shape=verify_shape))
  File "/home/phoenixyuwilkie/miniconda3/envs/obl_tf/lib/python3.6/site-packages/tensorflow/python/framework/tensor_util.py", line 442, in make_tensor_proto
    _AssertCompatible(values, dtype)
  File "/home/phoenixyuwilkie/miniconda3/envs/obl_tf/lib/python3.6/site-packages/tensorflow/python/framework/tensor_util.py", line 353, in _AssertCompatible
    (dtype.name, repr(mismatch), type(mismatch).__name__))
TypeError: Expected int32, got None of type '_Message' instead.

The training seems to have completed, I have attached the output from running the train_miccai2018.py file. nohup.txt

Thank you, Samuel Newhook

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:9

github_iconTop GitHub Comments

2reactions
JesperDramschcommented, Aug 29, 2019

I ran into a similar problem when using register.py, after some investigation, I found that the MICCAI version is trained with:

flow_vol_shape = model.outputs[-1].shape[1:-1]
loss_class = losses.Miccai2018(image_sigma, prior_lambda, flow_vol_shape=flow_vol_shape)

and the custom_layer defintion contains:

custom_objects = { [......]
             'recon_loss': losses.Miccai2018(0.02, 10).recon_loss, # values shouldn't matter
             'kl_loss': losses.Miccai2018(0.02, 10).kl_loss        # values shouldn't matter
             }

The values don’t matter, but the type of the values matters, it seems with the most recent version on Github. The following code fixed the error “TypeError: Expected int32, got None of type ‘_Message’ instead.”.

custom_objects = { [......]
             'recon_loss': losses.Miccai2018(0.02, 10, flow_vol_shape=[32]).recon_loss, # values shouldn't matter
             'kl_loss': losses.Miccai2018(0.02, 10, flow_vol_shape=[32]).kl_loss        # values shouldn't matter
             }
0reactions
John1231983commented, Aug 29, 2019

I just use original code without modify anything in train.py

train.txt

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error when loading trained model with tf.keras ... - GitHub
Error when loading trained model with tf.keras.models.load_model #48235 ... This error only occurs if the model has been trained while ...
Read more >
Train, save model and load: error while loading model
I'm training a model using TensorFlow and Keras. I would like to save the model and then load it. but I'm getting some...
Read more >
error during loading a trained model - Kaggle
when i try to load a keras trained model it showing an error ValueError: You are trying to load a weight file containing...
Read more >
Inference error when loading a previously trained saved model
I'm trying to find a solution to an unusual error that occurs during inference on my trained model. My process begins by creating...
Read more >
Save and load Keras models | TensorFlow Core
Saving the weights values only. This is generally used when training the model. Let's take a look at each of these options. When...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found