Facing some model loading issue when I want to run face recognition lab on my local computer

This is the error message please help me to solve it out

ValueError Traceback (most recent call last)
Cell In[20], line 6
4 loaded_model_json = json_file.read()
5 json_file.close()
----> 6 model = model_from_json(loaded_model_json)
7 model.load_weights(‘keras-facenet-h5/model.h5’)

File ~\anaconda3\Lib\site-packages\keras\src\saving\legacy\model_config.py:125, in model_from_json(json_string, custom_objects)
102 “”“Parses a JSON model configuration string and returns a model instance.
103
104 Usage:
(…)
119 A Keras model instance (uncompiled).
120 “””
121 from keras.src.layers import (
122 deserialize_from_json,
123 )
→ 125 return deserialize_from_json(json_string, custom_objects=custom_objects)

File ~\anaconda3\Lib\site-packages\keras\src\layers\serialization.py:299, in deserialize_from_json(json_string, custom_objects)
293 populate_deserializable_objects()
294 config = json_utils.decode_and_deserialize(
295 json_string,
296 module_objects=LOCAL.ALL_OBJECTS,
297 custom_objects=custom_objects,
298 )
→ 299 return deserialize(config, custom_objects)

File ~\anaconda3\Lib\site-packages\keras\src\layers\serialization.py:276, in deserialize(config, custom_objects, use_legacy_format)
268 if use_legacy_format:
269 return legacy_serialization.deserialize_keras_object(
270 config,
271 module_objects=LOCAL.ALL_OBJECTS,
272 custom_objects=custom_objects,
273 printable_module_name=“layer”,
274 )
→ 276 return serialization_lib.deserialize_keras_object(
277 config,
278 module_objects=LOCAL.ALL_OBJECTS,
279 custom_objects=custom_objects,
280 printable_module_name=“layer”,
281 )

File ~\anaconda3\Lib\site-packages\keras\src\saving\serialization_lib.py:609, in deserialize_keras_object(config, custom_objects, safe_mode, **kwargs)
602 if isinstance(module_objects[config], types.FunctionType):
603 return deserialize_keras_object(
604 serialize_with_public_fn(
605 module_objects[config], config, fn_module_name
606 ),
607 custom_objects=custom_objects,
608 )
→ 609 return deserialize_keras_object(
610 serialize_with_public_class(
611 module_objects[config], inner_config=inner_config
612 ),
613 custom_objects=custom_objects,
614 )
616 if isinstance(config, PLAIN_TYPES):
617 return config

File ~\anaconda3\Lib\site-packages\keras\src\saving\serialization_lib.py:728, in deserialize_keras_object(config, custom_objects, safe_mode, **kwargs)
726 safe_mode_scope = SafeModeScope(safe_mode)
727 with custom_obj_scope, safe_mode_scope:
→ 728 instance = cls.from_config(inner_config)
729 build_config = config.get(“build_config”, None)
730 if build_config:

File ~\anaconda3\Lib\site-packages\keras\src\engine\training.py:3306, in Model.from_config(cls, config, custom_objects)
3298 revivable_as_functional = (
3299 cls in {functional.Functional, Model}
3300 or argspec.args[1:] == functional_init_args
3301 or (argspec.varargs == “args” and argspec.varkw == “kwargs”)
3302 )
3303 if is_functional_config and revivable_as_functional:
3304 # Revive Functional model
3305 # (but not Functional subclasses with a custom init)
→ 3306 inputs, outputs, layers = functional.reconstruct_from_config(
3307 config, custom_objects
3308 )
3309 model = cls(
3310 inputs=inputs, outputs=outputs, name=config.get(“name”)
3311 )
3312 functional.connect_ancillary_layers(model, layers)

File ~\anaconda3\Lib\site-packages\keras\src\engine\functional.py:1493, in reconstruct_from_config(config, custom_objects, created_layers)
1491 # First, we create all layers and enqueue nodes to be processed
1492 for layer_data in config[“layers”]:
→ 1493 process_layer(layer_data)
1494 # Then we process nodes in order of layer depth.
1495 # Nodes that cannot yet be processed (if the inbound node
1496 # does not yet exist) are re-enqueued, and the process
1497 # is repeated until all nodes are processed.
1498 while unprocessed_nodes:

File ~\anaconda3\Lib\site-packages\keras\src\engine\functional.py:1474, in reconstruct_from_config..process_layer(layer_data)
1470 else:
1471 # Instantiate layer.
1472 from keras.src.layers import deserialize as deserialize_layer
→ 1474 layer = deserialize_layer(layer_data, custom_objects=custom_objects)
1475 created_layers[layer_name] = layer
1477 node_count_by_layer[layer] = int(_should_skip_first_node(layer))

File ~\anaconda3\Lib\site-packages\keras\src\layers\serialization.py:276, in deserialize(config, custom_objects, use_legacy_format)
268 if use_legacy_format:
269 return legacy_serialization.deserialize_keras_object(
270 config,
271 module_objects=LOCAL.ALL_OBJECTS,
272 custom_objects=custom_objects,
273 printable_module_name=“layer”,
274 )
→ 276 return serialization_lib.deserialize_keras_object(
277 config,
278 module_objects=LOCAL.ALL_OBJECTS,
279 custom_objects=custom_objects,
280 printable_module_name=“layer”,
281 )

File ~\anaconda3\Lib\site-packages\keras\src\saving\serialization_lib.py:609, in deserialize_keras_object(config, custom_objects, safe_mode, **kwargs)
602 if isinstance(module_objects[config], types.FunctionType):
603 return deserialize_keras_object(
604 serialize_with_public_fn(
605 module_objects[config], config, fn_module_name
606 ),
607 custom_objects=custom_objects,
608 )
→ 609 return deserialize_keras_object(
610 serialize_with_public_class(
611 module_objects[config], inner_config=inner_config
612 ),
613 custom_objects=custom_objects,
614 )
616 if isinstance(config, PLAIN_TYPES):
617 return config

File ~\anaconda3\Lib\site-packages\keras\src\saving\serialization_lib.py:728, in deserialize_keras_object(config, custom_objects, safe_mode, **kwargs)
726 safe_mode_scope = SafeModeScope(safe_mode)
727 with custom_obj_scope, safe_mode_scope:
→ 728 instance = cls.from_config(inner_config)
729 build_config = config.get(“build_config”, None)
730 if build_config:

File ~\anaconda3\Lib\site-packages\keras\src\layers\core\lambda_layer.py:327, in Lambda.from_config(cls, config, custom_objects)
324 @classmethod
325 def from_config(cls, config, custom_objects=None):
326 config = config.copy()
→ 327 function = cls._parse_function_from_config(
328 config, custom_objects, “function”, “module”, “function_type”
329 )
331 output_shape = cls._parse_function_from_config(
332 config,
333 custom_objects,
(…)
336 “output_shape_type”,
337 )
338 if “mask” in config:

File ~\anaconda3\Lib\site-packages\keras\src\layers\core\lambda_layer.py:394, in Lambda._parse_function_from_config(cls, config, custom_objects, func_attr_name, module_attr_name, func_type_attr_name)
392 elif function_type == “lambda”:
393 if serialization_lib.in_safe_mode():
→ 394 raise ValueError(
395 "Requested the deserialization of a Lambda layer with a "
396 "Python lambda inside it. "
397 "This carries a potential risk of arbitrary code execution "
398 "and thus it is disallowed by default. If you trust the "
399 "source of the saved model, you can pass safe_mode=False "
400 "to the loading function in order to allow "
401 “Lambda layer loading.”
402 )
403 # /!\ Unsafe deserialization from bytecode! Danger! /!
404 function = generic_utils.func_load(
405 config[func_attr_name], globs=globs
406 )

ValueError: Requested the deserialization of a Lambda layer with a Python lambda inside it. This carries a potential risk of arbitrary code execution and thus it is disallowed by default. If you trust the source of the saved model, you can pass safe_mode=False to the loading function in order to allow Lambda layer loading.

1 Like

If you want to use the course notebooks on your local environment, you must install exactly the same version that Coursera Labs uses, for all of the software packages.

I believe this is discussed in the DLS FAQ. You can find it via the forum Search tool.

1 Like

Please share the link of the DLS FAQ

1 Like

Are you not able to use the Search tool? It should be at the top of every page of the forum.

1 Like

I know it but I does not find any related topic there !

1 Like

Sorry, I was incorrect. There isn’t any information in the DLS FAQ about installing the notebook locally.

1 Like

please share the solution

1 Like

I do not have one, sorry.

1 Like

There is a topic on the FAQ that tells how to do the download of all the files. But that is only the first step. There is no official documentation about how to do what Tom mentioned about duplicating the environment. It is not a simple problem, but here’s a thread with some links to get you started. Here’s another one.

Till I can not solve my issue … Please help me

As Paul already mentioned duplicating the environment is not simple and there is no official documentation about how to do that. So, we cannot help you with this. You may use the link Paul shared earlier or ChatGPT/Bard.

Please share some alternative solution to solve this issue … where I can run the model with current version of python and tensorflow. I am facing issue to load the model.

I also try to download a pretrain facenet model from internet where I am facing EOF error …

Please give me some alternative way to solve this problem

There are two overall paths to solve this type of “versionitis” problem:

  1. You can do the work to duplicate the versions of all the packages that were used for that assignment on the course website. I gave you links to a couple of threads that will show you how to do that, but there is no complete or “official” set of instructions for that.
  2. You can search the internet or look at the Keras website to see if you can find a different version of the FaceNet model that is compatible with the current version of Keras. Keras does have a lot of pretrained models on their website, but I’m not sure if they provide a newer FaceNet.

Option 2) is probably easier although knowing how to do option 1) is also a valuable thing to have in your toolbox. It can come in handy in lots of situations.

Whichever method you choose, the higher level point is that this is beyond the scope of the course. It’s perfectly fine to ask this kind of question and maybe you will get lucky and someone will have a “canned” answer for you, but there is no guarantee that will be true. Your best strategy is to strengthen your own problem solving skills. The issue is that once you start buildling things on your own, the problems just never end. You can’t just throw up your hands and expect some Deus ex Machina to swoop in and solve your problems for you. E.g. in this case for option 2), the next step is to google “facenet pretrained model”. StackExchange is also a valuable resource for solving problems like this.

If you find a FaceNet model that is compatible with newer Keras, then the next problem will be that it may not be packaged as an H5 file, as the assignment code is written to expect. So how are you going to solve that problem? You either need to figure out how to import it in the actual format in which it is available or you need to figure out how to convert it to an H5 file. And on and on. As I said above, it just never ends, so the right way to look at this is that it is this type of problem solving skills that you need to have to succeed at this.

A hacked-together solution that one could use:
Notice that when you download the assignment, there are also a fr_utils and inception_blocks_v2 files. These files actually come from a Facenet repository, and you can download the weights by cloning this repository: GitHub - Skuldur/facenet-face-recognition: A face recognition demo performed by feeding images of faces recorded by a webcam into a trained FaceNet network to determine the identity of the face . You can import the model by:

from inception_blocks_v2 import faceRecoModel
model = faceRecoModel(input_shape=(3, 96, 96))

and see further usage in the facenet.py file.
Also, a few things to note:

Hope this helps!

@BISHWANATH_JANA consider the following, for me it worked and it was easy.

Like @TMosh and @paulinpaloalto the problem is a compatibility issue BUT JUST WITH PYTHON VERSION. The problem is at its base for the serialization/de-serialization of python bytecode.

So, juts installing a coda environment with python 3.7.6 and the packages for that version as follows (extracted from the python -V in the TERMINAL or in the Jupyter Notebook as !python -V):

Note: this steps are done in the TERMINAL not in the code cells.


conda create -n myenv python=3.7.6
conda activate myenv
python --version
conda install ipykernel
python -m ipykernel install --user --name=myenv
pip install --upgrade jupyter_client
pip install tensorflow
pip install pandas
conda install -c anaconda pillow
conda install -c anaconda matplotlib

Then us the myenv in the kernel of the Jupyter Notebook


Also in the check code CELL after the # GRADED FUNCTION: triplet_loss … def triplet_loss(y_true, y_pred, alpha = 0.2): …

Copy the UNIT TEST CODE in another new CELL and change the following line as follows:

#assert type(loss) == tf.python.framework.ops.EagerTensor, "Use tensorflow functions"
assert isinstance(loss, tf.Tensor) and hasattr(loss, 'numpy'), "Use tensorflow functions"


Last, as per keras/tensorflow docs. It is suggested to use subclass tf.keras.layers.Layer instead of using Lambda layers:

https://www.tensorflow.org/api_docs/python/tf/keras/layers/Lambda

2 Likes

Hi, Alba.

Thanks very much for sharing the knowledge that you discovered here. This is how we hope the community works: sharing what you learned will help others who hit this issue in the future.

Regards,
Paul