I am getting error while running the code in my own computer

Hi,

I can run the code perfectly in coursera jupyter notebook, but while running into my computer, I have faced this problem.

For the following code:

from tensorflow.keras.models import model_from_json

json_file = open('keras-facenet-h5/model.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
model = model_from_json(loaded_model_json)
model.load_weights('keras-facenet-h5/model.h5')

I am getting this error:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
C:\Users\NAFISA~1.SAM\AppData\Local\Temp/ipykernel_7476/324912946.py in <module>
      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')

~\anaconda3\lib\site-packages\keras\saving\model_config.py in model_from_json(json_string, custom_objects)
    102   config = json_utils.decode(json_string)
    103   from keras.layers import deserialize  # pylint: disable=g-import-not-at-top
--> 104   return deserialize(config, custom_objects=custom_objects)

~\anaconda3\lib\site-packages\keras\layers\serialization.py in deserialize(config, custom_objects)
    205   """
    206   populate_deserializable_objects()
--> 207   return generic_utils.deserialize_keras_object(
    208       config,
    209       module_objects=LOCAL.ALL_OBJECTS,

~\anaconda3\lib\site-packages\keras\utils\generic_utils.py in deserialize_keras_object(identifier, module_objects, custom_objects, printable_module_name)
    677 
    678       if 'custom_objects' in arg_spec.args:
--> 679         deserialized_obj = cls.from_config(
    680             cls_config,
    681             custom_objects=dict(

~\anaconda3\lib\site-packages\keras\engine\functional.py in from_config(cls, config, custom_objects)
    706       if all(key in config for key in [
    707           'name', 'layers', 'input_layers', 'output_layers']):
--> 708         input_tensors, output_tensors, created_layers = reconstruct_from_config(
    709             config, custom_objects)
    710         model = cls(

~\anaconda3\lib\site-packages\keras\engine\functional.py in reconstruct_from_config(config, custom_objects, created_layers)
   1324   # First, we create all layers and enqueue nodes to be processed
   1325   for layer_data in config['layers']:
-> 1326     process_layer(layer_data)
   1327   # Then we process nodes in order of layer depth.
   1328   # Nodes that cannot yet be processed (if the inbound node

~\anaconda3\lib\site-packages\keras\engine\functional.py in process_layer(layer_data)
   1306       from keras.layers import deserialize as deserialize_layer  # pylint: disable=g-import-not-at-top
   1307 
-> 1308       layer = deserialize_layer(layer_data, custom_objects=custom_objects)
   1309       created_layers[layer_name] = layer
   1310 

~\anaconda3\lib\site-packages\keras\layers\serialization.py in deserialize(config, custom_objects)
    205   """
    206   populate_deserializable_objects()
--> 207   return generic_utils.deserialize_keras_object(
    208       config,
    209       module_objects=LOCAL.ALL_OBJECTS,

~\anaconda3\lib\site-packages\keras\utils\generic_utils.py in deserialize_keras_object(identifier, module_objects, custom_objects, printable_module_name)
    677 
    678       if 'custom_objects' in arg_spec.args:
--> 679         deserialized_obj = cls.from_config(
    680             cls_config,
    681             custom_objects=dict(

~\anaconda3\lib\site-packages\keras\layers\core\lambda_layer.py in from_config(cls, config, custom_objects)
    301   def from_config(cls, config, custom_objects=None):
    302     config = config.copy()
--> 303     function = cls._parse_function_from_config(config, custom_objects,
    304                                                'function', 'module',
    305                                                'function_type')

~\anaconda3\lib\site-packages\keras\layers\core\lambda_layer.py in _parse_function_from_config(cls, config, custom_objects, func_attr_name, module_attr_name, func_type_attr_name)
    356     elif function_type == 'lambda':
    357       # Unsafe deserialization from bytecode
--> 358       function = generic_utils.func_load(config[func_attr_name], globs=globs)
    359     elif function_type == 'raw':
    360       function = config[func_attr_name]

~\anaconda3\lib\site-packages\keras\utils\generic_utils.py in func_load(code, defaults, closure, globs)
    791   except (UnicodeEncodeError, binascii.Error):
    792     raw_code = code.encode('raw_unicode_escape')
--> 793   code = marshal.loads(raw_code)
    794   if globs is None:
    795     globs = globals()

ValueError: bad marshal data (unknown type code)

How can I solve this?

There are no official instructions for running the assignments locally, but there are some threads with helpful instructions and pointers.

Note that the notebooks are not “standalone” entities: they include a whole tree of files with each assignment. It doesn’t work just to bring down the notebook “ipynb” file itself: you need all the other associated files as well. You also need to install the same versions of all the supporting packages that the notebooks use, since there can be “versionitis” problems because everything mutates really quickly in this space. The notebooks are from April 2021 and you can’t just install the very latest version of TF, python and all the zillion packages and expect things to “just work”.

Here’s a thread about how to download all the files associated with a given assignment.

Here’s a thread with some useful instructions about the “versionitis” issues, but, as you’ll quickly see from reading it, this is not a trivial exercise.

2 Likes

Thank you so much for your reply. I didnt download only ipynb file, but I downloaded the whole folder, and try to explore more the code with different images, that was my target. I installed all the necessary libraries, and successfully import them, but I have no idea about this error and I couldnt solve it even I searched a lot.

I will follow your instructions from the thread that you shared. Thanks again.

Yes, those error traces are always difficult to decipher. My best guess is that it’s saying it doesn’t like the format of the h5 file. Maybe it’s a “versionitis” problem, but my suggestion would be to try opening and reading the h5 file just as a separate operation and see if you can get that to work.

1 Like

Maybe write the code to dump information about the contents of the h5 file and then run it both locally and in the notebook on the course website and see if the results differ.

Here’s a thread which shows how to create an h5 file containing your own images. That may not be directly relevant to what you are doing here (since the h5 file contains the weights of the model, not data samples), but at least it shows you some of the h5 related python APIs.