Using a ConvNet to Compute Encodings

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’)

This is the code given by the notebook. When i save it into my own computer and runs it. The error " bad marshal data (unknown type code)" appears. What can i do to remedy it?

Hey, are you sure you’re running on the same version of tensorflow as the lab?

I am using version 2.5.0 and the lab is using 2.3.0. After downgrading to 2.3.0, the error “Expecting value: line 1 column 1 (char 0)” appears.

This is seen below:

JSONDecodeError Traceback (most recent call last)
in
6 json_file.close()
7
----> 8 model = model_from_json(loaded_model_json)
9 model.load_weights(‘keras-facenet-h5/model.h5’)

~/opt/anaconda3/lib/python3.8/site-packages/tensorflow/python/keras/saving/model_config.py in model_from_json(json_string, custom_objects)
118 A Keras model instance (uncompiled).
119 “”"
→ 120 config = json.loads(json_string)
121 from tensorflow.python.keras.layers import deserialize # pylint: disable=g-import-not-at-top
122 return deserialize(config, custom_objects=custom_objects)

~/opt/anaconda3/lib/python3.8/json/init.py in loads(s, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
355 parse_int is None and parse_float is None and
356 parse_constant is None and object_pairs_hook is None and not kw):
→ 357 return _default_decoder.decode(s)
358 if cls is None:
359 cls = JSONDecoder

~/opt/anaconda3/lib/python3.8/json/decoder.py in decode(self, s, _w)
335
336 “”"
→ 337 obj, end = self.raw_decode(s, idx=_w(s, 0).end())
338 end = _w(s, end).end()
339 if end != len(s):

~/opt/anaconda3/lib/python3.8/json/decoder.py in raw_decode(self, s, idx)
353 obj, end = self.scan_once(s, idx)
354 except StopIteration as err:
→ 355 raise JSONDecodeError(“Expecting value”, s, err.value) from None
356 return obj, end

JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Please help with this

Are you sure you’ve downloaded the weights properly? Remember the weights are actually symlinked in the exercise. You’ll have to archive the workspace with tar -czhf ~/workspace.tar.gz ~/work rather than what’s given in coursera’s guide. then I think it should be able to read.

I downloaded the files as it is using the download button found in the server. Will there be anything different that will result in any errors?

Yeah those are symlinks. Follow these instructions, just use the command I sent instead of the one mentioned in the article. You’ll get the proper files.

I did follow your instructions but the file i obtained is in alias format. I cant open this format file as a a result. I am so sorry but will it be possible if you can list the step accordingly? I might be doing something wrong somewhere.

I clicked on the Jupyter logo in coursera and typed these into the terminal

rm -f ~/workspace.tar.gz && rm -f ~/work/workspace.tar.gz

tar -czhf ~/workspace.tar.gz ~/work

Have you moved the file following the next instruction on that page mv ~/workspace.tar.gz ~/work/workspace.tar.gz

Once you do that, the file should be there once you click “Jupyter” on top again. You’ll have to extract it on your system, but it should give you all the files needed.

I did the steps mentioned above and i downloaded the ‘workspace.tar.gz’. When i extracted it in my local computer, the model.h5 has a size of 92.2mb and model.json has a size of 237kb. I ran the code again and it is still stuck.

What could be the problem?

Are you having the same error? I have no idea why that could be if that’s the case tbh.

Yes i am still having the same error of not being able to load the h5 and json file.