How to import a CNN model in keras - conv-neural-network

I am new to machine learning technology and I have a simple question. I have trained a network on the fashion MNIST and saved it, and then I would like to import the saved model for further processing instead of training the network each time I use the code. Can anyone help?
I have used the import function, but it always gives me an error
model.save("model_1.h5py") # save the model
model.import("model_2.h5py") # when I try to import the #model alwys gives me a valid synthax

Related

Loading a GPU trained BERTopic model on CPU?

I trained a BERTopic model on a GPU, and now for visualization purposes I want to load it on a CPU.
But when I tried to do that I got:
RuntimeError: Attempting to deserialize object on a CUDA device but torch.cuda.is_available() is False. If you are running on a CPU-only machine, please use torch.load with map_location=torch.device('cpu') to map your storages to the CPU.
When I tried to use the suggested fix I got the same problem?
Saw some fix that suggests to save the model without its embeddings model, but don't want to retrain an resave unless its the last option, and would also love if someone could explain what's this embedding model and what's going on under the hood.
topic_model = torch.load(args.model, map_location=torch.device('cpu'))
When you want to save the BERTopic model without the embedding model, you can run the following:
from bertopic import BERTopic
from sklearn.datasets import fetch_20newsgroups
from sentence_transformers import SentenceTransformer
docs = fetch_20newsgroups(subset='all', remove=('headers', 'footers', 'quotes'))['data']
# Train the model
embedding_model = SentenceTransformer("all-MiniLM-L6-v2")
topic_model = BERTopic(embedding_model=embedding_model)
topics, probs = topic_model.fit_transform(docs)
# Save the model without the embedding model
topic_model.save("my_model", save_embedding_model=False)
This should prevent any issues with GPU/CPU if you are not using any of the cuML sub-models in BERTopic.
Saw some fix that suggests to save the model without its embeddings model, but don't want to retrain an resave unless its the last option, and would also love if someone could explain what's this embedding model and what's going on under the hood.
The embedding model is typically a pre-trained model that actually is not learning from the input data. There are options to make it learn during training but that requires a custom component in BERTopic. In other words, when you use a pre-trained model, it is no problem removing that pre-trained model when saving the topic model as there would be no need to re-train the model.
In other words, we would first save our topic model in our GPU environment without the embedding model:
topic_model.save("my_model", save_embedding_model=False)
Then, we load in our saved BERTopic model in our CPU environment and then pass the pre-trained embedding model:
from sentence_transformers import SentenceTransformer
embedding_model = SentenceTransformer("all-MiniLM-L6-v2")
topic_model = BERTopic.load("my_model", embedding_model=embedding_model )
You can learn more about the role of the embedding model here.

Query regarding saving keras model. How does saving a model work?

After saving a Keras model, where is it saved? Is it saved locally? The below code is saving a model.
from keras.models import load_model
model.save('my_model.h5') # creates a HDF5 file 'my_model.h5
model = load_model('my_model.h5')
After saving the model can I use the load model again in another jupyter ipynb file?
When we do model.save, it will save model architecture and weight at the same time. It will save as .h5 locally and we can load it in another environment (local notebook, kaggle notebook, colab, GCP, etc). Using load_model will load fully model (arch + weight).
Please, read the official blog, you will find plenty of examples. https://www.tensorflow.org/guide/keras/save_and_serialize

How can I fix "Unknown layer: DenseFeatures" while importing pre-trained keras model?

I was following the tutorial for classifying structured data from here. I trained the model as described with my data which only comprised of numeric data.
I have trained and optimised my model on google colab and downloaded it locally to test it on some new data.
However, when I load the model using this snippet.
from keras.models import load_model
model = load_model("my_model.h5")
I get the following error
Unknown layer: DenseFeatures
...along with the trace.
I have tried setting up custom_objects while loading the model like this
model = load_model("my_model.h5", custom_objects={'DenseFeatures': tf.keras.layers.DenseFeatures})
But I still get the following error:
__init__() takes at least 2 arguments (3 given)
What could I be doing wrong? I tried going through documentation but couldn't find anything of help on github.

Error: Currently `save` requires model to be a graph network. Unable to save model

I am using google colaboratory to implement deep learning in python3. I create a model, train it, test it. Everything is fine. Finally I try to save the model on my google drive. But is says
Error: Currently 'save' requires model to be a graph network.
Upto training and testing there is no problem.
Then I mount the drive
from google.colab import drive
drive.mount('/content/gdrive')
And then try to save the model for later use as:
model.save('my_model_name.model')
But it is not saving the model. What am I missing?
the preferred way to save a model with tensorflow is to use the tf.train.Saver() module. so lets say your model is simply called Model and you want to save it in a particular directory. This is the preferred way to do it.
import tensorflow as tf
directory_to_save = '/content/drive'
with tf.Session() as sess
saver = tf.train.Saver()
#train model
saver.save(sess, directory_to_save)

Train, Save and Load a Tensorflow Model

Refer to this to train a GAN model for MNIST dataset, I want to save a model and restore it for further prediction. After having some understanding of Saving and Importing a Tensorflow Model I am able to save and restore some variables of inputs and outputs but for this network I am able to save the model only after some specific iterations and not able to predict some output.
Did you refer to this guide? It explains very clearly how to load and save tensorflow models in all possible formats.
If you are new to ML, I'd recommend you give Keras a try first, which is much easier to use. See https://keras.io/getting-started/faq/#how-can-i-save-a-keras-model, pretty much you can use:
model.save('my_model.h5')
to save your model to disk.
model = load_model('my_model.h5')
to load your model and make prediction

Resources