问题
I am very new on neural network, and I am trying to use Keras to build a fish classifier. I got a python code that uses Keras with tensorflow-backend to create a RESNET-50 model, the code use model.save() to output a h5 file.
I write another python file to reload the RESNET-50 model from the h5 file. I use keras.models.load_model() to load h5 file. But python give me the error message like below, and I don't knwo why. Please give me a hand, thanks.
Traceback (most recent call last):
File "keras-cnn-predict.py", line 75, in <module>
model=keras.models.load_model(model_config_filename)
File "C:\Python\Python36\lib\site-packages\keras\engine\saving.py", line 419, in load_model
model = _deserialize_model(f, custom_objects, compile)
File "C:\Python\Python36\lib\site-packages\keras\engine\saving.py", line 225, in _deserialize_model
model = model_from_config(model_config, custom_objects=custom_objects)
File "C:\Python\Python36\lib\site-packages\keras\engine\saving.py", line 458, in model_from_config
return deserialize(config, custom_objects=custom_objects)
File "C:\Python\Python36\lib\site-packages\keras\layers\__init__.py", line 55, in deserialize
printable_module_name='layer')
File "C:\Python\Python36\lib\site-packages\keras\utils\generic_utils.py", line 145, in deserialize_keras_object
list(custom_objects.items())))
File "C:\Python\Python36\lib\site-packages\keras\engine\network.py", line 1022, in from_config
process_layer(layer_data)
File "C:\Python\Python36\lib\site-packages\keras\engine\network.py", line 1008, in process_layer
custom_objects=custom_objects)
File "C:\Python\Python36\lib\site-packages\keras\layers\__init__.py", line 55, in deserialize
printable_module_name='layer')
File "C:\Python\Python36\lib\site-packages\keras\utils\generic_utils.py", line 147, in deserialize_keras_object
return cls.from_config(config['config'])
File "C:\Python\Python36\lib\site-packages\keras\engine\base_layer.py", line 1109, in from_config
return cls(**config)
File "C:\Python\Python36\lib\site-packages\keras\legacy\interfaces.py", line 91, in wrapper
return func(*args, **kwargs)
File "C:\Python\Python36\lib\site-packages\keras\layers\convolutional.py", line 490, in __init__
**kwargs)
File "C:\Python\Python36\lib\site-packages\keras\layers\convolutional.py", line 117, in __init__
self.kernel_initializer = initializers.get(kernel_initializer)
File "C:\Python\Python36\lib\site-packages\keras\initializers.py", line 508, in get
return deserialize(identifier)
File "C:\Python\Python36\lib\site-packages\keras\initializers.py", line 503, in deserialize
printable_module_name='initializer')
File "C:\Python\Python36\lib\site-packages\keras\utils\generic_utils.py", line 147, in deserialize_keras_object
return cls.from_config(config['config'])
File "C:\Python\Python36\lib\site-packages\keras\initializers.py", line 30, in from_config
return cls(**config)
File "C:\Python\Python36\lib\site-packages\keras\initializers.py", line 195, in __init__
'but got', distribution)
ValueError: ('Invalid `distribution` argument: expected one of {"normal", "uniform"} but got', 'truncated_normal')
回答1:
D:\Anaconda3\lib\site-packages\keras\initializers.py in __init__(self, scale, mode, distribution, seed)
193 raise ValueError('Invalid `distribution` argument: '
194 'expected one of {"normal", "uniform"} '
--> 195 'but got', distribution)
196 self.scale = scale
197 self.mode = mode
ValueError: ('Invalid `distribution` argument: expected one of {"normal", "uniform"} but got', 'truncated_normal')
In import tensorflow.python.keras
replace keras
. Then it's successful.
来源:https://stackoverflow.com/questions/53064834/cant-reload-a-trained-resnet50-model-from-a-h5-file