1

I have created a model from mobilenetv2 model on TensorFlow hub, I use model.save() to save the model and use:

my_reloaded_model = tf.keras.models.load_model(
       ("./model/model_flower.h5"),
       custom_objects={'KerasLayer':hub.KerasLayer}
)

to load model.

My program works fine but after about a week when I run the program again it generates an error like this:

TypeError: Error when deserializing class 'KerasLayer' using config={'name': 'keras_layer_1', 'trainable': False, 'dtype': 'float32', 'batch_input_shape': [None, 224, 224, 3], 'handle': 'https://tfhub.dev/google/tf2-preview/mobilenet_v2/feature_vector/4'}.

Exception encountered: Trying to load a model of incompatible/unknown type. 'C:\Users\Lenovo\AppData\Local\Temp\tfhub_modules\145bb06ec3b59b08fb564ab752bd5aa222bfb50a' contains neither 'saved_model.pb' nor 'saved_model.pbtxt'.

I checked on stack overflow and got one result:

Essentially what I understood from the article above, is that TensorFlow will create a temp directory to keep loaded models; however, after a few days or so, the contents of the folders (the loaded model) will be deleted. Then when you look to load a model again, TensorFlow will route to the temp dir, but the model will be deleted from the temp dir.

This makes sense and explains why if your code is running totally fine the past few days, and then all of the sudden gets this error, it probably has to do with deleting the old temp directory."

I deleted the temp folder and ran it again and it worked fine. But that's only a temporary solution, the program still stops working every 1 week or so. I don't want to have to delete that temporary folder every once in a while.

Do you have any solution?
I want my program to work for a long time, not having to fix it every time.

6
  • This sounds like a bug in tensorflow, you should open an issue with them. Commented Jul 25, 2023 at 11:34
  • After about a few days of sitting and reading the document, I now have a solution (although not very good, but basically enough to solve my problem, anyone with a better answer is welcome, Anyone who doesn't have a solution can refer to my way) Commented Aug 9, 2023 at 7:26
  • The problem here is that my program when it trains I use the transfer learning method and use the keras layer as the original part (no change) and only edit the last layer (output layer). so when you save the model, the program will only save the part you edited (the output layer). So when you want to load the model, you also need a keras layer to make the original combined with the output layers (the layer you edit yourself) into a complete model. Commented Aug 9, 2023 at 7:29
  • It was the Keras layer that loaded a temporary model containing the model's weights and structure (this folder won't last long - leading to an error after a period of testing, needing to be deleted to run). again after an interval of maybe 3 or 4 days ) . My approach is to solve the problem before it happens. Instead of using the keras layer as a background layer (the layer does not change), I downloaded the keras layer as a raw material for training, ie the training process will change a bit of the keras layer. Commented Aug 9, 2023 at 7:30
  • Use :from tensorflow.keras.applications import "the library you want to use" and train as usual to have a model that can be saved and loaded as a self-built model. However, this way may require more computational resources and the size of the model will be larger (eg model using keras layer remotely it will give the model only about 10mb but if using keras layer by how to download, the model will be about 45mb) Commented Aug 9, 2023 at 7:31

2 Answers 2

2

This kind of error occurs when you use a keras layers from the tf.hub, while you using the hub layers in your model the pre-weights of the layers is stored in a folder which will decompose over time and only work if you are frequently using the model so the weights will always updated. otherwise it will raise an error ...===>To solve the error you need to just delete the folder from the directory "C:\Users\Lenovo\AppData\Local\Temp\tfhub_modules\145bb06ec3b59b08fb564ab752bd5aa222bfb50a" delete the folder which raising the exception in your case this one "145bb06ec3b59b08fb564ab752bd5aa222bfb50a" ..or delete all the folders inside tfhub_modules.

Sign up to request clarification or add additional context in comments.

1 Comment

This needs more upvotes, solved it instantly.
0

After about a few days of sitting and reading the document, I now have a solution (although not very good, but basically enough to solve my problem, anyone with a better answer is welcome, Anyone who doesn't have a solution can refer to my way)

The problem here is that my program when it trains I use the transfer learning method and use the keras layer as the original part (no change) and only edit the last layer (output layer). so when you save the model, the program will only save the part you edited (the output layer). So when you want to load the model, you also need a keras layer to make the original combined with the output layers (the layer you edit yourself) into a complete model. It was the Keras layer that loaded a temporary model containing the model's weights and structure (this folder won't last long - leading to an error after a period of testing, needing to be deleted to run). again after an interval of maybe 3 or 4 days ) . My approach is to solve the problem before it happens. Instead of using the keras layer as a background layer (the layer does not change), I downloaded the keras layer as a raw material for training, ie the training process will change a bit of the keras layer. Use :from tensorflow.keras.applications import "the library you want to use" and train as usual to have a model that can be saved and loaded as a self-built model. However, this way may require more computational resources and the size of the model will be larger (eg model using keras layer remotely it will give the model only about 10mb but if using keras layer by how to download, the model will be about 45mb) . I refer to this article to do it the new way: https://saturncloud.io/blog/proper-way-to-save-transfer-learning-models-in-keras/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.