RobotEdh / Yolov-4

Yolo v4 using TensorFlow 2.x
110 stars 30 forks source link

load_model error #9

Closed Dragos-Stan closed 3 years ago

Dragos-Stan commented 3 years ago

Dear Sir,

Thank you very much for your beautifull work ! I'm not an expert, I just started 9 months ago as a hobby, most of my learnings coming from Jason Brownlee's - machine_learning_mastery - books including YoloV3 tutorials. Everything works perfectly except the fact that I cannot load the model !

yolo_model = load_model(...) gives the following error : "ValueError: Unknown activation function: Mish" !

Many thanks and kind regards

Dragos Stan

""

ValueError Traceback (most recent call last)

in 2 3 from keras.models import load_model, Model ----> 4 yolo_model = load_model('YOLO/YOLO_V4/YoloV4_model.h5') /opt/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/saving/save.py in load_model(filepath, custom_objects, compile, options) 180 if (h5py is not None and ( 181 isinstance(filepath, h5py.File) or h5py.is_hdf5(filepath))): --> 182 return hdf5_format.load_model_from_hdf5(filepath, custom_objects, compile) 183 184 filepath = path_to_string(filepath) /opt/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/saving/hdf5_format.py in load_model_from_hdf5(filepath, custom_objects, compile) 176 model_config = json.loads(model_config.decode('utf-8')) 177 model = model_config_lib.model_from_config(model_config, --> 178 custom_objects=custom_objects) 179 180 # set weights /opt/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/saving/model_config.py in model_from_config(config, custom_objects) 53 '`Sequential.from_config(config)`?') 54 from tensorflow.python.keras.layers import deserialize # pylint: disable=g-import-not-at-top ---> 55 return deserialize(config, custom_objects=custom_objects) 56 57 /opt/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/layers/serialization.py in deserialize(config, custom_objects) 173 module_objects=LOCAL.ALL_OBJECTS, 174 custom_objects=custom_objects, --> 175 printable_module_name='layer') /opt/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/utils/generic_utils.py in deserialize_keras_object(identifier, module_objects, custom_objects, printable_module_name) 356 custom_objects=dict( 357 list(_GLOBAL_CUSTOM_OBJECTS.items()) + --> 358 list(custom_objects.items()))) 359 with CustomObjectScope(custom_objects): 360 return cls.from_config(cls_config) /opt/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/engine/functional.py in from_config(cls, config, custom_objects) 615 """ 616 input_tensors, output_tensors, created_layers = reconstruct_from_config( --> 617 config, custom_objects) 618 model = cls(inputs=input_tensors, outputs=output_tensors, 619 name=config.get('name')) /opt/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/engine/functional.py in reconstruct_from_config(config, custom_objects, created_layers) 1202 # First, we create all layers and enqueue nodes to be processed 1203 for layer_data in config['layers']: -> 1204 process_layer(layer_data) 1205 # Then we process nodes in order of layer depth. 1206 # Nodes that cannot yet be processed (if the inbound node /opt/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/engine/functional.py in process_layer(layer_data) 1184 from tensorflow.python.keras.layers import deserialize as deserialize_layer # pylint: disable=g-import-not-at-top 1185 -> 1186 layer = deserialize_layer(layer_data, custom_objects=custom_objects) 1187 created_layers[layer_name] = layer 1188 /opt/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/layers/serialization.py in deserialize(config, custom_objects) 173 module_objects=LOCAL.ALL_OBJECTS, 174 custom_objects=custom_objects, --> 175 printable_module_name='layer') /opt/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/utils/generic_utils.py in deserialize_keras_object(identifier, module_objects, custom_objects, printable_module_name) 358 list(custom_objects.items()))) 359 with CustomObjectScope(custom_objects): --> 360 return cls.from_config(cls_config) 361 else: 362 # Then `cls` may be a function returning a class. /opt/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/engine/base_layer.py in from_config(cls, config) 695 A layer instance. 696 """ --> 697 return cls(**config) 698 699 def compute_output_shape(self, input_shape): /opt/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/layers/core.py in __init__(self, activation, **kwargs) 422 super(Activation, self).__init__(**kwargs) 423 self.supports_masking = True --> 424 self.activation = activations.get(activation) 425 426 def call(self, inputs): /opt/anaconda3/lib/python3.7/site-packages/tensorflow/python/util/dispatch.py in wrapper(*args, **kwargs) 199 """Call target, and fall back on dispatchers if there is a TypeError.""" 200 try: --> 201 return target(*args, **kwargs) 202 except (TypeError, ValueError): 203 # Note: convert_to_eager_tensor currently raises a ValueError, not a /opt/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/activations.py in get(identifier) 529 return deserialize(identifier) 530 elif isinstance(identifier, dict): --> 531 return deserialize(identifier) 532 elif callable(identifier): 533 return identifier /opt/anaconda3/lib/python3.7/site-packages/tensorflow/python/util/dispatch.py in wrapper(*args, **kwargs) 199 """Call target, and fall back on dispatchers if there is a TypeError.""" 200 try: --> 201 return target(*args, **kwargs) 202 except (TypeError, ValueError): 203 # Note: convert_to_eager_tensor currently raises a ValueError, not a /opt/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/activations.py in deserialize(name, custom_objects) 490 module_objects=globals(), 491 custom_objects=custom_objects, --> 492 printable_module_name='activation function') 493 494 /opt/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/utils/generic_utils.py in deserialize_keras_object(identifier, module_objects, custom_objects, printable_module_name) 345 config = identifier 346 (cls, cls_config) = class_and_config_for_serialized_keras_object( --> 347 config, module_objects, custom_objects, printable_module_name) 348 349 if hasattr(cls, 'from_config'): /opt/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/utils/generic_utils.py in class_and_config_for_serialized_keras_object(config, module_objects, custom_objects, printable_module_name) 294 cls = get_registered_object(class_name, custom_objects, module_objects) 295 if cls is None: --> 296 raise ValueError('Unknown ' + printable_module_name + ': ' + class_name) 297 298 cls_config = config['config'] ValueError: Unknown activation function: Mish ""
RobotEdh commented 3 years ago

Hello, sorry for my late answer.

Did you run all the steps of the Jupyter notebook ?

The Mish function is defined in the first step: def mish(x): return (x* tf.math.tanh(mysoftplus(x)))

Can you check that you have run the first step of the notebook without any error

Dragos-Stan commented 3 years ago

Dear Sir, thank you for your reply !

Yes I did, actually it works perfectly well, no other errors from the beginning to the end for both YoloV4_tf with image detection and YoloV4_Train_tf on VOC dataset with decent loss after training 1000 images. No error when saving the model but not possible to load_model so I had to use yolo_model = model in order to perform the detection. My hardware is a MacBookPro 2019 laptop, TensorFlow 2.3, Keras 2.4.0, Eager True, GPU = 0, but no Cuda and this is the only difference... Kind regards, Dragos

RobotEdh commented 3 years ago

hello,

very strange, you have an issue to deserialize the custom object mish defined for activation.

I was in TF 2.1 so i have upgraded to TF 2.3.1 but I still not have the same issue. Can you upgrade in TF 2.3.1 (If you are in 2.3.0) Are you in python 3.7.3 ?

Dragos-Stan commented 3 years ago

Hello

Python 3.7.6, upgraded to tf 2.3.1, the same error...

kind regards

RobotEdh commented 3 years ago

hello,

I run the same version of Python, TF and Keras and I have no issue. The only difference is that I run under windows10. So maybe an issue in the distrib TF on MacPro.

lidiador commented 3 years ago

I am running under Win10 and had the same issue. I think the problem resides in the fact that a layer is passed as an activation of another layer. As a workaround, I applied get_custom_objects().update({'mish': mish}), which seems to resolve the problem.

Dragos-Stan commented 3 years ago

Right ! Thank you very much Sir !

RobotEdh commented 3 years ago

Thanks guys, strange behavior because I have no issue on Win 10.

so if I understand correctly, you have replaced get_custom_objects().update({'mish': Mish(mish)}) by get_custom_objects().update({'mish': mish})

thanks

Dragos-Stan commented 3 years ago

Yes, that's all!

lidiador commented 3 years ago

Yes, I did just that. What is more, when trying to load this model from the h5 file outside of Google Colab, toying with it in VS Code, I encountered the same error, even with this line changed. Here the workaround is:

from keras.utils.generic_utils import CustomObjectScope with CustomObjectScope({'mish': mish}): yolo_model = load_model(r'path\to\model_dir\yolov4.h5')

Of course, the mish and mysoftplus functions must be imported beforehand.