ellisdg/3DUnetCNN

"ValueError: Unknown layer: InstanceNormalization" while predicting (isensee2017)

Closed this issue · 5 comments

b-z commented

Hello,
I have followed the readme to train the brats data using the method of isensee2017.
The training phrase worked well, but when performing python predict.py, an error occurred

ValueError: Unknown layer: InstanceNormalization

at https://github.com/ellisdg/3DUnetCNN/blob/master/unet3d/prediction.py#L120

I've searched in Google but found no results,
could you please give me some ideas?

b-z commented
Traceback (most recent call last):
  File "predict.py", line 19, in <module>
    main()
  File "predict.py", line 15, in main
    labels=config["labels"], hdf5_file=config["data_file"])
  File "/home/vis/zbw/3DUnetCNN/unet3d/prediction.py", line 120, in run_validation_case
    model = load_old_model(model_file)
  File "/home/vis/zbw/3DUnetCNN/unet3d/training.py", line 44, in load_old_model
    return load_model(model_file, custom_objects=custom_objects)
  File "/home/vis/anaconda3/lib/python3.6/site-packages/keras/models.py", line 240, in load_model
    model = model_from_config(model_config, custom_objects=custom_objects)
  File "/home/vis/anaconda3/lib/python3.6/site-packages/keras/models.py", line 314, in model_from_config
    return layer_module.deserialize(config, custom_objects=custom_objects)
  File "/home/vis/anaconda3/lib/python3.6/site-packages/keras/layers/__init__.py", line 55, in deserialize
    printable_module_name='layer')
  File "/home/vis/anaconda3/lib/python3.6/site-packages/keras/utils/generic_utils.py", line 140, in deserialize_keras_object
    list(custom_objects.items())))
  File "/home/vis/anaconda3/lib/python3.6/site-packages/keras/engine/topology.py", line 2490, in from_config
    process_layer(layer_data)
  File "/home/vis/anaconda3/lib/python3.6/site-packages/keras/engine/topology.py", line 2476, in process_layer
    custom_objects=custom_objects)
  File "/home/vis/anaconda3/lib/python3.6/site-packages/keras/layers/__init__.py", line 55, in deserialize
    printable_module_name='layer')
  File "/home/vis/anaconda3/lib/python3.6/site-packages/keras/utils/generic_utils.py", line 134, in deserialize_keras_object
    ': ' + class_name)
ValueError: Unknown layer: InstanceNormalization

Could be related with this.

from keras_contrib.layers.normalization import InstanceNormalization

Please check your path environment.
For test, add above into predict.py file.

This has to do with the load_model function not understanding the Instance Normalization layer. I'll try to fix this soon, but to fix it now this you can add from keras-contrib import InstanceNormalization to the training.py file and then in the load_model function add 'InstanceNormalization': InstanceNormalization to the custom_objects dictionary. When loading the model this will tell Keras what exactly 'InstanceNormalization' is referring to.

The issue should be fixed now. You will have to install keras-contrib:
pip install git+https://www.github.com/keras-team/keras-contrib.git

b-z commented

Thanks a lot!