How to Load and Use Model from Older Tensorflow? - python

I'm having a problem loading a Tensorflow model I downloaded. Based on the error, it sounds like incompatibility issue with model from older Tensorflow? I tried to load it on Tensorflow 2.4, 2.3 with no success. It throws a completely different error.
Inside model folder, I see checkpoint, saved_model, pipeline.config.
Inside model/saved_model folder, I see variables and saved_model.pb.
If I try to load using tf.keras.models.load_model("model/saved_model"), I get this.
WARNING:tensorflow:SavedModel saved prior to TF 2.5 detected when loading Keras model. Please ensure that you are saving the model with model.save() or tf.keras.models.save_model(), *NOT* tf.saved_model.save(). To confirm, there should be a file named "keras_metadata.pb" in the SavedModel directory.
ValueError: Unable to create a Keras model from SavedModel at design-system-detector/rico/models/mobilenetv2-50k/saved-model/saved_model. This SavedModel was exported with `tf.saved_model.save`, and lacks the Keras metadata file. Please save your Keras model by calling `model.save` or `tf.keras.models.save_model`.
Note that you can still load this SavedModel with `tf.saved_model.load`.
If I load with tf.saved_model.load, it just returns _UserObject, and I can't run model.predict.
AttributeError: '_UserObject' object has no attribute 'predict'
What's the best way to use this model to predict?

Related

Save tensorflow model to pickle file

For deployment purposes, I need to save a deep learning model as a pickle file, but when doing so, even with the joblib function, I always get these errors.
I used to save my model using tf.keras (as a hdf5 file) , but with this method,I found that the test file needs tensorflow and keras packages and this is not really efficient when it comes to deployment..

Tensorflow: Convert Save_mode.pb to a freeze_graph.pb with Tensorflow 2.5.0

I recently trained my own custom object detector model using mobilenetssd as my pretrained model. I have been following this tutorial to train my model, and used exporter_main_v2.py program in TensorFlow/models/research/object_detection/ to generate my save_model.pb file. I would like to use OpenCV DNN module where it requires a frozen model and the pbtext. In order to generate the pbtext file, I would need to convert it from the frozen_graph.pb. I would like to convert my save_model.pb to a frozen_graph.pb file, but it looks like freezing graphs has been deprecated in tensorflow 2.X. I tried this solution but I get this error:
AttributeError: '_UserObject' object has no attribute 'inputs'
Does anyone have recommendations or solutions on how I can covert my save_model.pb to a freeze_graph.pb? I would really appreciate the help!

How to open and analyze Google Tables model using python/tf?

I read several discussions about this and still cannot make it work for my case
Have a classification model trained using Google Tables.
Exported the model and download the directory with cli.
My goal is to get a better understanding of the model trained by google, study it, understand its decisions. And later try to prune it to improve performance.
I'm using this code, just to start:
import tensorflow as tf
from tensorflow import keras
import struct2tensor
location = "model_dir"
model = tf.saved_model.load(location)
model.summary()
I get this error:
AttributeError: 'AutoTrackable' object has no attribute 'summary'
the variable model is of type:
<tensorflow.python.training.tracking.tracking.AutoTrackable at 0x7fa8eaa7ed30>
And I stuck there, don't know how to continue. Using Python 3.8 and the last version of those libraries. Any idea of how can I proceed?
Thanks!
The proper method to load your model depends on your file formatting.
You can see in the Tensorflow documentation that "The object returned by tf.saved_model.load is not a Keras object (i.e. doesn't have .fit, .predict, etc. methods)" and "Use tf.keras.models.load_model to restore the Keras model".
I'm not sure if you want to use the keras module or not, but since you have imported it I assume you do. In that case I would recommend checking this other Stackoverflow thread where it is explained how to use the tf.keras.models.load_model method depending if your model is saved as .pb or .h5.
If the model is saved as .pb you should use it with the string pointing to the directory where the model is saved, as you did in your code snippet but in this case using the keras method:
model = tf.keras.models.load_model('model_dir')
If instead it's saved as .h5 you should use it specifying it:
model = tf.keras.models.load_model('my_model_in_h5.h5')

Keras Predictions and Sagemaker Predictions are different

I am Trying to deploy a tensorflow keras model using amazon sagemaker. Process finishes successfully, yet i get different prediction results when predicted directly using keras and when calling sagemaker endpoint to make predictions.
I used these steps in deploying the model to sagemaker.
Check the following example.
data = np.random.randn(1, 150, 150, 3)
# predict using amazon sagemaker
sagemaker_predict = uncompiled_predictor.predict(data)
print(sagemaker_predict)
#predict same using keras
val = model.predict(data)
print(val)
>>{'predictions': [[0.491645753]]}
[[0.]]
Is this something supposed to happen? For my knowledge it should be the same. For some reason data gets corrupted or sagemaker weights get reinitialized. Any ideas?
Not suppose to happen.
See what you get if you deloy the model directly to TensorFlow serving (which is what the SageMaker inference container wraps).
To experiment faster you can work with SageMaker inference container in local mode, so you can start/stop an endopint in seconds.
Finally found a solution. It seems to be a problem with .h5 (HDF5) weights file, for some reason sagemaker seems not to extract weight from .h5. Therefore changed the weights file to TensorFlow SavedModel format
As for tensorflow keras save and serialize documentation
There are two formats you can use to save an entire model to disk: the TensorFlow SavedModel format, and the older Keras H5 format. The recommended format is SavedModel. It is the default when you use model.save().
You can switch to the H5 format by:
Passing save_format='h5' to save().
Passing a filename that ends in .h5 or .keras to save()
So instead of saving weights as
model.save("my_model.h5")
save as
model.save("my_model")
And load the same weights as
keras.models.load_model("my_model")
This will save your file in TensorFlow SavedModel format which you can follow in the above documentation to load and deploy to sagemaker.

How to load Keras model with version mismatch error when keras version used while training is not known

I am working on a github code and there is a pre-trained Keras model saved as h5 file. On loading the file I get an error which is due to version mismatch i.e. the version of Keras while training is different from the version I am using to load it.
from keras.models import load_model
try:
model = load_model('data/cnn_model_preTrained.h5')
except IOError:
print("model file doesn't exit")
It results into this error:
Traceback (most recent call last):
File "C:\Python37\lib\site-packages\keras\models.py", line 1211, in from_config
if 'class_name' not in config[0] or config[0]['class_name'] == 'Merge':
KeyError: 0
The problem is that the version of Keras used while training is not known to me, thus I am unable to solve the version mismatch issue.
Is there a way to solve this?
I got to know about the h5py package of python used to handle HDF5 binary data format or the h5 file from one of my mentors and this can be used to view the metadata of the saved model. So I tried this:
import h5py
## loading the saved model I was getting an error for
hf = h5py.File('cnn_model_preTrained.h5')
for i in hf.attrs:
print(i)
It resulted into:
keras_version
backend
model_config
training_config
Fortunately, there was the Keras version saved as metadata, so I tried printing it.
print(hf.attrs['keras_version'])
which resulted to
b'2.2.4'
And that was the Keras version required to load the pre-trained model. I installed the 2.2.4 version of Keras and my code worked like a piece of cake.
Besides, to avoid such error, always save the model graph in model.json. Then you can load the graph from JSON and then can use the load_weight function.

Categories

Resources