Cant properly load saved model and call predict method - python

I am having trouble loading large model after saving.
have tried all below saveing methods:
tf.saved_model.save(model, model_save_path)
model.save(model_save_path+"_new_save")
tf.keras.models.save_model(model, model_save_path+"_v3")
error when loading :
method 1
m2=tf.keras.models.load_model(model_save_path+"_v3")
error:
__init__() got an unexpected keyword argument 'reduction'
method 2
m3=tf.keras.models.load_model(model_save_path
error:
ARNING: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 xxxx . 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`.
method 3
m4=tf.saved_model.load(model_save_path)
this works but m4 object has no predict method
and not able to use
model.signatures["serving_default"](**input_data)
or
model.__call__(input_data,training=False)
to predict on data
any help would be appreciated

Adding compile=False to the load function will solve the issue:
m2=tf.keras.models.load_model(model_save_path+"_v3", compile=False)

Related

Azure machine learning unable to load pytorch Model from the outputs folder

I am unable to load saved pytorch model from the outputs folder in my other scripts.
I am using following lines of code to save the model:
os.makedirs("./outputs/model", exist_ok=True)
torch.save({
'model_state_dict': copy.deepcopy(model.state_dict()),
'optimizer_state_dict': optimizer.state_dict()
}, './outputs/model/best-model.pth')
new_run.upload_file("outputs/model/best-model.pth", "outputs/model/best-model.pth")
saved_model = new_run.register_model(model_name='pytorch-model', model_path='outputs/model/best-model.pth')
and using the following code to access it:
global model
best_model_path = 'outputs/model/best-model.pth'
model_checkpoint = torch.load(best_model_path)
model.load_state_dict(model_checkpoint['model_state_dict'], strict = False)
but when I run the above mentioned code, I get this error: No such file or directory: './outputs/model/best-model.pth'
Also I want to know is there a way to get the saved model from Azure Models? I have tried to get it by using following lines of code:
from azureml.core.model import Model
model = Model(ws, "Pytorch-model")
but it returns Model type object which returns error on model.eval() (error: Model has no such attribute eval()).
There is no global output folder. If you want to use a Model in a new script you need to give the script the model as an input or register the model and download the model from the new script.
The Model object form from azureml.core.model import Model is not your pytorch Model. 1
You can use model.register(...) to register your model. And model.download(...) to download you model. Than you can use pytorch to load you model. 2

AttributeError: 'Functional' object has no attribute 'predict_segmentation' When importing TensorFlow model Keras

I have successfully trained a Keras model like:
import tensorflow as tf
from keras_segmentation.models.unet import vgg_unet
# initaite the model
model = vgg_unet(n_classes=50, input_height=512, input_width=608)
# Train
model.train(
train_images=train_images,
train_annotations=train_annotations,
checkpoints_path="/tmp/vgg_unet_1", epochs=5
)
And saved it in hdf5 format with:
tf.keras.models.save_model(model,'my_model.hdf5')
Then I load my model with
model=tf.keras.models.load_model('my_model.hdf5')
Finally I want to make a segmentation prediction on a new image with
out = model.predict_segmentation(
inp=image_to_test,
out_fname="/tmp/out.png"
)
I am getting the following error:
AttributeError: 'Functional' object has no attribute 'predict_segmentation'
What am I doing wrong ?
Is it when I am saving my model or when I am loading it ?
Thanks !
predict_segmentation isn't a function available in normal Keras models. It looks like it was added after the model was created in the keras_segmentation library, which might be why Keras couldn't load it again.
I think you have 2 options for this.
You could use the line from the code I linked to manually add the function back to the model.
model.predict_segmentation = MethodType(keras_segmentation.predict.predict, model)
You could create a new vgg_unet with the same arguments when you reload the model, and transfer the weights from your hdf5 file to that model as suggested in the Keras documentation.
model = vgg_unet(n_classes=50, input_height=512, input_width=608)
model.load_weights('my_model.hdf5')

weights = 'noisy-student' ValueError: The `weights` argument should be either `None`, `imagenet`, or the path to the weights file to be loaded

I am using Google Colab and I want to use the weights of EfficientNet Noisy Student. https://www.kaggle.com/c/bengaliai-cv19/discussion/132894
First, I installed the package via:
!pip install git+https://github.com/qubvel/efficientnet
Then I tried the code found on the site mentioned above:
import efficientnet.keras as eff
model = eff.EfficientNetB0(weights='noisy-student')
And got this Value error:
ValueError: The `weights` argument should be either `None` (random initialization), `imagenet` (pre-training on ImageNet), or the path to the weights file to be loaded.
Does someone know how to fix this?
You could download the weights from here.
And load it manually like this:
path_to_weights = "/..your..path../efficientnet-b5_noisy-student_notop.h5"
model = EfficientNetB5(include_top=False)
model.load_weights(path_to_weights, by_name=True)

How to load Chainer training checkpoint from npz?

I am using Chainer to train (fine-tune) a Resnet model and then use the checkpoint for evaluation. The checkpoint is a npz file with the following structure:
When I am loading the model for evaluation with chainer.serializers.load_npz(args.load, model) (where model is the standard resnet) I get the following error: KeyError: 'rpn/loc/b is not a file in the archive'.
I think the problem is that the files in the model do not have the 'updater/optimizer/faster/extractor' prefix.
How can I change the name of the files in the resulting npz to remove the prefix or what else should I do to fix the problem?
Thank you!
When you load a snapshot generated by the Snapshot Extension, you need to do it from the trainer.
chainer.serializers.load_npz(args.load, trainer) The trainer will automatically load the state of the updater, optimizer and the model.
You can also load only the model manually by accessing the corresponding field in the snapshot and passing it as an argument to the model.serialize function
npz_data = numpy.load(args.load)
snap = chainer.serializers.NpzDeserializer(npz_data)
model.serialize(snap['updater']['model:main'])
This should load only the weights of the model

TensorFlow Hub error when Saving model as H5 or SavedModel

I want to use this TF Hub asset:
https://tfhub.dev/google/imagenet/resnet_v1_50/feature_vector/3
Versions:
Version: 1.15.0-dev20190726
Eager mode: False
Hub version: 0.5.0
GPU is available
Code
feature_extractor_url = "https://tfhub.dev/google/imagenet/resnet_v1_50/feature_vector/3"
feature_extractor_layer = hub.KerasLayer(module,
input_shape=(HEIGHT, WIDTH, CHANNELS))
I get:
ValueError: Importing a SavedModel with tf.saved_model.load requires a 'tags=' argument if there is more than one MetaGraph. Got 'tags=None', but there are 2 MetaGraphs in the SavedModel with tag sets [[], ['train']]. Pass a 'tags=' argument to load this SavedModel.
I tried:
module = hub.Module("https://tfhub.dev/google/imagenet/resnet_v1_50/feature_vector/3",
tags={"train"})
feature_extractor_layer = hub.KerasLayer(module,
input_shape=(HEIGHT, WIDTH, CHANNELS))
But when I try to save the model I get:
tf.keras.experimental.export_saved_model(model, tf_model_path)
# model.save(h5_model_path) # Same error
NotImplementedError: Can only generate a valid config for `hub.KerasLayer(handle, ...)`that uses a string `handle`.
Got `type(handle)`: <class 'tensorflow_hub.module.Module'>
Tutorial here
It's been a while, but assuming you have migrated to the TF2, this can easily be accomplished with the most recent model version as follows:
import tensorflow as tf
import tensorflow_hub as hub
num_classes=10 # For example
m = tf.keras.Sequential([
hub.KerasLayer("https://tfhub.dev/google/imagenet/resnet_v1_50/feature_vector/5", trainable=True)
tf.keras.layers.Dense(num_classes, activation='softmax')
])
m.build([None, 224, 224, 3]) # Batch input shape.
# train as needed
m.save("/some/output/path")
Please update this question if that doesn't work for you. I believe your issue arose from mixing hub.Module with hub.KerasLayer. The model version you were using was in TF1 Hub format, so within TF1 it is meant to be used exclusively with hub.Module, and not mixed with hub.KerasLayer. Within TF2, hub.KerasLayer can load TF1 Hub format models directly from their URL for composition in larger models, but they cannot be fine-tuned.
Please refer to this compatibility guide for more information
You should use tf.keras.models.save_model(model,'NeuralNetworkModel')
You will get saved model in a folder that can be used later in your sequential nework

Categories

Resources