DOsinga / deep_learning_cookbook

Deep Learning Cookbox
Apache License 2.0
689 stars 337 forks source link

15.2 Index Local MP3s --ValueError: Unknown initializer: GlorotUniform #79

Open mikechen66 opened 4 years ago

mikechen66 commented 4 years ago

Hi Douwe:

While running the script, it shows ValueError: Unknown initializer: GlorotUniform. It eventually showa ValueError: Unknown layer: BatchNormalizationV2 after I tried to fix it with the following method.

1. Original Code

cnn_model = load_model('/home/mike/Documents/dl-cookbook/zoo/15/song_classify.h5')
vectorize_model = Model(inputs=cnn_model.input, outputs=cnn_model.layers[-4].output)
vectors = vectorize_model.predict(inputs)
vectors.shape

2. ValueError: Unknown initializer: GlorotUniform


ValueError Traceback (most recent call last)

in ----> 1 cnn_model = load_model('/home/mike/Documents/dl-cookbook/zoo/15/song_classify.h5') 2 vectorize_model = Model(inputs=cnn_model.input, outputs=cnn_model.layers[-4].output) 3 vectors = vectorize_model.predict(inputs) 4 vectors.shape ~/miniconda3/lib/python3.7/site-packages/keras/engine/saving.py in load_wrapper(*args, **kwargs) 490 os.remove(tmp_filepath) 491 return res --> 492 return load_function(*args, **kwargs) 493 494 return load_wrapper ~/miniconda3/lib/python3.7/site-packages/keras/engine/saving.py in load_model(filepath, custom_objects, compile) 582 if H5Dict.is_supported_type(filepath): 583 with H5Dict(filepath, mode='r') as h5dict: --> 584 model = _deserialize_model(h5dict, custom_objects, compile) 585 elif hasattr(filepath, 'write') and callable(filepath.write): 586 def load_function(h5file): ~/miniconda3/lib/python3.7/site-packages/keras/engine/saving.py in _deserialize_model(h5dict, custom_objects, compile) 272 raise ValueError('No model found in config.') 273 model_config = json.loads(model_config.decode('utf-8')) --> 274 model = model_from_config(model_config, custom_objects=custom_objects) 275 model_weights_group = h5dict['model_weights'] 276 ~/miniconda3/lib/python3.7/site-packages/keras/engine/saving.py in model_from_config(config, custom_objects) 625 '`Sequential.from_config(config)`?') 626 from ..layers import deserialize --> 627 return deserialize(config, custom_objects=custom_objects) 628 629 ~/miniconda3/lib/python3.7/site-packages/keras/layers/__init__.py in deserialize(config, custom_objects) 166 module_objects=globs, 167 custom_objects=custom_objects, --> 168 printable_module_name='layer') ~/miniconda3/lib/python3.7/site-packages/keras/utils/generic_utils.py in deserialize_keras_object(identifier, module_objects, custom_objects, printable_module_name) 145 config['config'], 146 custom_objects=dict(list(_GLOBAL_CUSTOM_OBJECTS.items()) + --> 147 list(custom_objects.items()))) 148 with CustomObjectScope(custom_objects): 149 return cls.from_config(config['config']) ~/miniconda3/lib/python3.7/site-packages/keras/engine/network.py in from_config(cls, config, custom_objects) 1054 # First, we create all layers and enqueue nodes to be processed 1055 for layer_data in config['layers']: -> 1056 process_layer(layer_data) 1057 1058 # Then we process nodes in order of layer depth. ~/miniconda3/lib/python3.7/site-packages/keras/engine/network.py in process_layer(layer_data) 1040 1041 layer = deserialize_layer(layer_data, -> 1042 custom_objects=custom_objects) 1043 created_layers[layer_name] = layer 1044 ~/miniconda3/lib/python3.7/site-packages/keras/layers/__init__.py in deserialize(config, custom_objects) 166 module_objects=globs, 167 custom_objects=custom_objects, --> 168 printable_module_name='layer') ~/miniconda3/lib/python3.7/site-packages/keras/utils/generic_utils.py in deserialize_keras_object(identifier, module_objects, custom_objects, printable_module_name) 147 list(custom_objects.items()))) 148 with CustomObjectScope(custom_objects): --> 149 return cls.from_config(config['config']) 150 else: 151 # Then `cls` may be a function returning a class. ~/miniconda3/lib/python3.7/site-packages/keras/engine/base_layer.py in from_config(cls, config) 1177 A layer instance. 1178 """ -> 1179 return cls(**config) 1180 1181 def count_params(self): ~/miniconda3/lib/python3.7/site-packages/keras/legacy/interfaces.py in wrapper(*args, **kwargs) 89 warnings.warn('Update your `' + object_name + '` call to the ' + 90 'Keras 2 API: ' + signature, stacklevel=2) ---> 91 return func(*args, **kwargs) 92 wrapper._original_function = func 93 return wrapper ~/miniconda3/lib/python3.7/site-packages/keras/layers/convolutional.py in __init__(self, filters, kernel_size, strides, padding, data_format, dilation_rate, activation, use_bias, kernel_initializer, bias_initializer, kernel_regularizer, bias_regularizer, activity_regularizer, kernel_constraint, bias_constraint, **kwargs) 351 kernel_constraint=kernel_constraint, 352 bias_constraint=bias_constraint, --> 353 **kwargs) 354 355 def get_config(self): ~/miniconda3/lib/python3.7/site-packages/keras/layers/convolutional.py in __init__(self, rank, filters, kernel_size, strides, padding, data_format, dilation_rate, activation, use_bias, kernel_initializer, bias_initializer, kernel_regularizer, bias_regularizer, activity_regularizer, kernel_constraint, bias_constraint, **kwargs) 115 self.activation = activations.get(activation) 116 self.use_bias = use_bias --> 117 self.kernel_initializer = initializers.get(kernel_initializer) 118 self.bias_initializer = initializers.get(bias_initializer) 119 self.kernel_regularizer = regularizers.get(kernel_regularizer) ~/miniconda3/lib/python3.7/site-packages/keras/initializers.py in get(identifier) 513 def get(identifier): 514 if isinstance(identifier, dict): --> 515 return deserialize(identifier) 516 elif isinstance(identifier, six.string_types): 517 config = {'class_name': str(identifier), 'config': {}} ~/miniconda3/lib/python3.7/site-packages/keras/initializers.py in deserialize(config, custom_objects) 508 module_objects=globals(), 509 custom_objects=custom_objects, --> 510 printable_module_name='initializer') 511 512 ~/miniconda3/lib/python3.7/site-packages/keras/utils/generic_utils.py in deserialize_keras_object(identifier, module_objects, custom_objects, printable_module_name) 138 if cls is None: 139 raise ValueError('Unknown ' + printable_module_name + --> 140 ': ' + class_name) 141 if hasattr(cls, 'from_config'): 142 custom_objects = custom_objects or {} ValueError: Unknown initializer: GlorotUniform ### 3. An attempt to correct the error I tried to correct the ValueError with the following lines of code. ``` import keras from keras.models import load_model from keras.utils import CustomObjectScope from keras.initializers import glorot_uniform with CustomObjectScope({'GlorotUniform': glorot_uniform()}): cnn_model = load_model('/home/mike/Documents/dl-cookbook/zoo/15/song_classify.h5') ``` However, it has another error as follows: ValueError: Unknown layer: BatchNormalizationV2. I think that it is the inconsistency between TensorFlow 2.0/Keras 2.3.1 and the old .h5 model. Do you have any method to solve the issue? Best regards, Mike
DOsinga commented 4 years ago

This sounds like we are trying to load the pre-trained network for TF 1.4 into a setup that uses 2.0. The causes always this sort of error I am afraid. The real solution would be to get the training to work.

mikechen66 commented 4 years ago

Thanks. Since the major libraries have been changed based on TensorFlow 2.0 and Keras 2.3.1, it has been difficult not to comply with the scenarios/situations. As a matter of fact, I sometimes test scripts based on TensorFlow 1.5 that is quite similar with TensorFlow 2.0.