Face Recognition assignment `model_from_json()` `TypeError: Could not locate class 'Functional'.`

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) # XXX
model.load_weights('keras-facenet-h5/model.h5')

Error:

TypeError: Could not locate class 'Functional'. Make sure custom classes are decorated with `@keras.saving.register_keras_serializable()`. Full object config: ...

The latest version of TF/keras fails loading the model as it was serialized without the required annotation.

In addition, the syntax used is not pythonic. This is the recommended way of file handling:

with open('./keras-facenet-h5/model.json', 'r', newline='') as f:
    loaded_model_json = f.read()
    self._model = model_from_json(loaded_model_json)

Please ensure that packages on your setup match with coursera jupyter environment. For this assignment, tensorflow version is 2.3.0.

Don’t worry too much about the pythonic way of doing things. For learning purposes, the snippet of code provided as part of starter code is sufficient.

Happy learning.

tensorflow==2.20.0. How to regenerate or where can I get the model’s .json file which works with the latest versions of the ecosystem?

Please wait for the staff to provide more details on your ask.

This is not a simple task. You may find this thread helpful, as well as this one.

Not available. The courses are intended to be run on the Coursera platform.

None of this is helpful at all. Where / how did the coursework get the serialized model json file?

The course is five years old at least. The folks who created that assignment are (likely) long gone - or more certainly, aren’t reading the forums.

You are telling the community here “don’t take the course!” as it is outdated. The current staff spend no effort in maintaining and upkeeping the content of the course! Spend your money wisely elsewhere!

Not at all. You may misunderstand the message here. Going beyond the provided platform is left for the student to undertake.

The course is entirely valid and useful as presented.

That the ML industry doesn’t see any value in backward compatibility as their tools are updated - that is a second (and important) issue.

1 Like

Hi @khteh,

I understand getting the TypeError is a real headache. Let me try to clear things up a bit, reiterate what others have mentioned already.

The thing is, these courses are designed to work perfectly inside the Coursera platform, which uses an older but very stable version of TensorFlow. We keep it that way so everyone can focus on the core deep learning ideas instead of chasing software updates. And there is no plan of an update of these courses any time soon.

It’s awesome that you’re trying to run it on your own machine, but we don’t officially support local setups, but figuring out how to make old code work with new libraries is a super useful skill for any developer.

As for where that JSON file came from, honestly, I don’t know. The course is quite old, and the original authors aren’t around, so I can’t provide an updated version either.

The main goal here is for you to learn the concepts taught in these courses, and those are just as relevant today as ever.

Hope this helps explain things.

Best,
Mubsi

2 Likes

That part of the message is clear to me but what’s the purpose of this community.deeplearning.ai? Is it limited to only the textbook content of the course? How useful is that if this is the case? Or is it open to discuss and share issues/problems like this so that we could apply what we learn into practical use? If this is the case, I don’t expect the staff / instructors to spoon-feed me to come up with solutions but some pointers will be helpful, if you know it. However, what I see here is a more serious problem. The course content you don’t even know how to reproduce it and no internal documentation? Doesn’t that tell the quality of the staff / instructors and of deeplearning.ai itself?

1 Like

I hear your frustration, and I want to be clear that the purpose of this community is absolutely to help you apply what you learn to practical problems. Your questions are welcome here.

You’ve brought up a really important point that gets to the heart of a common challenge in software: the difference between reproducibility and updating.

I think there may be a slight misunderstanding of terms here. The course content is 100% reproducible in the environment it was designed for, with the original library versions. We ensure that so the learning experience is stable.

What you’re trying to do, which is a great learning exercise, is to update the old code to run with the very latest libraries. As you know, that’s a much more complex task. Libraries evolve, syntax changes, functions are moved, and even subtle things like how random numbers are generated can differ between versions. It’s a significant engineering challenge, not just a simple rerun of the code.

So I’m just being transparent with you, as much as I’d like to help you with a quick solution. The challenge you’re facing is a classic migration issue that developers often encounter.


Here’s a quick reply from an LLM on how you can approach the issue you are facing:

1. The Root of the TypeError

The error TypeError: Could not locate class 'Functional' is a strong clue. The way Keras saves and loads model architecture has changed significantly since TensorFlow 2.3. The older model_from_json method is now deprecated and often fails with newer versions because the internal structure of the model object (like the Functional class) isn’t recognized from the old JSON format.

2. The Modern Approach: Unified Model Files

Instead of a separate .json for the architecture and .h5 for the weights, the modern standard is to use a single, unified format. The recommended way to save and load a complete model is:

  • To save: model.save('my_model.keras')
  • To load: model = tf.keras.models.load_model('my_model.keras')

This single file contains everything: the architecture, weights, and optimizer state.

3. A Practical First Step

Since you don’t have the original code that created the model, you can’t re-save it in the new .keras format. However, you can try loading the weights into a newly defined model structure. If you can find the Python code that defines the FaceNet model’s architecture in the assignment, you could:

  1. Re-create the model architecture in your code.
  2. Load only the weights onto this new model using new_model.load_weights('keras-facenet-h5/model.h5').

This approach bypasses the problematic .json file entirely and is much more likely to work in TF 2.20. It directly answers your question: you don’t need a new .json file if you rebuild the model in code and load the weights.

I hope these pointers are helpful and give you a good direction to explore!


Best,
Mubsi

Don’t the staff/instructor have that piece of information?

If it is not already part of the assignemnt, try looking for it here:

it is useful in the knowledge you gained.
This sets you up to expand your knowledge further.

1 Like