Saving model to .h5 format using PyTorch - python

I'm exporting model to .h5 format using PyTorch like this.
torch.save(model.state_dict(), 'model.h5')
But when I load this model.h5 in TensorFlow, I got this error.
File "h5py/h5f.pyx", line 106, in h5py.h5f.open
OSError: Unable to open file (file signature not found)
I try both ways of saving models.
# 1.
torch.save(model.state_dict(), 'model.h5')
# 2.
torch.save(model, 'model.h5')
In both ways, I'm getting the same error.

You cant do that!
according to the official TF documentation, TensorFlow uses the HDF5 file format. PyTorch doesnt. Pytorch uses Python’s pickle to store the weights.
Just passing a random file extension doesn't magically convert it to that file format! you have to explicitly save your weights as hdf5.

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..

Load joblib file with custom metrics

I am training multiple models (on Google Colab) and saving them using joblib. I am then downloading these files and loading them using joblib, but I am getting the following error;
Unable to restore custom object of type _tf_keras_metric
I am compiling the models using metrics=['accuracy', specificity, tf.keras.metrics.Precision()], where specificity is a custom metric function from https://www.sabinasz.net/unbalanced-classes-machine-learning/.
mobilenet_model = MobileNetV3Small(include_top=True, weights=None, classes=2, input_shape=(100, 100, 3))
mobilenet_model.compile(loss='binary_crossentropy', metrics=['accuracy', specificity, tf.keras.metrics.Precision()])
trained_model = mobilenet_model.fit(X_train, y_train)
joblib.dump(trained_model, 'mobilenet_model.joblib')
I tried to load them using keras.models.load_model but I get a Error opening file (File signature not found) which according to Error opening file in H5PY (File signature not found) means the file is corrupt (I imagine that the way joblib saves models is not compatible with keras's save_model function).
I tried using keras.models.save_model to save a file and passing in custom_objects={'specificity': specificity, 'precision': tf.keras.metrics.Precision()}, but the model seems to be saved without the pretrained weights because when I evaluate it, it only has 0.5 accuracy, when it was getting 0.9 when I trained it.
So what can I do?

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.

how to convert .h5 file to .pb file?

How do I convert a .h5 weight file to .pb file ? I trained keras pre-trained model and saved the file as something.h5 . How do i convert it to .pb file for using it for tensorflow serving ?
P.S: Don't degrade the question, I couldn't find any solution online. Do mention the reason why you degraded the question. Otherwise, help with solving the question.
import tensorflow as tf
model = tf.keras.models.load_model(keras_model_path)
tf.saved_model.save(model, saved_model_path)
Bingo.

TypeError: can't pickle _thread.lock objects while dumping nn4_small2_pretrained model from Keras-Openface Project

I was trying to implement Facial Recognition using the pretrained models from Keras-Openface Project and amazingly explained and implimented by Martin Krasser here
The OpenFace project provides pre-trained models that were trained with the public face recognition datasets FaceScrub and CASIA-WebFace. The Keras-OpenFace project converted the weights of the pre-trained nn4.small2.v1 model to CSV files which were then converted here to a binary format that can be loaded by Keras with load_weights
The Code is simply :
nn4_small2_pretrained = create_model()
nn4_small2_pretrained.load_weights('weights/nn4.small2.v1.h5')
Before going to the error to give more insight on what's happening in create_model(), you can go through the code here
Now I need nn4_small2_pretrained in my predict.py file (It is initially a part of train.py to train my custom images), but if I do
from train import nn4_small2_pretrained
Or write the code
nn4_small2_pretrained = create_model()
nn4_small2_pretrained.load_weights('weights/nn4.small2.v1.h5')
all over again, then the prediction file takes a lot of time to compile as it goes through the whole process again. So to resolve this, I tried to dump the model in a pickle file like so
# Save the nn4 pretrained model to pickle file
f = open('pretrained_model.pickle', 'wb')
pickle.dump(nn4_small2_pretrained, f)
When I run the code it gives me this error
File "train.py", line 24, in <module>
pickle.dump(nn4_small2_pretrained, f)
TypeError: can't pickle _thread.lock objects
I have started with Deel Learning Models and Pickle recently and I cannot figure out what's wrong.
Any help would be much appreciated.
Thanks.
I see that the create_model() creates an instance of Keras Model. If it is a Keras Model then you can save the model using model.save(filepath).
Refer this link for other options.

Categories

Resources