Save trained gensim word2vec model as a tensorflow SavedModel - tensorflow

Do we have an option to save a trained Gensim Word2Vec model as a saved model using tf 2.0 tf.saved_model.save? In other words, how can I save a trained embedding vector as a saved model signature to work with tensorflow 2.0. The following steps are not correct normally:
model = gensim.models.Word2Vec(...)
model.init_sims(..)
model.train(..)
model.save(..)
module = gensim.models.KeyedVectors.load_word2vec(...)
tf.saved_model.save(
module,
export_dir
)
EDIT:
This example helped me about how to do it : https://keras.io/examples/nlp/pretrained_word_embeddings/

Gensim does not use TensorFlow and it has its own methods for loading and saving models.
You would need to convert Gensim embeddings into a TensorFlow a model which only makes sense if you further plan to use your embeddings within TensorFlow and possibly fine-tune them for your task.
Gensim Word2Vec are two steps in TensorFlow:
Vocabulary lookup: a table that assigns indices to tokens.
Embedding lookup layer that picks up the actual embeddings for the indices.
Then, you can save it as any other TensorFlow model.

Related

Frozen tensorflow graph inputs

I created several custom CNN models in TensorFlow 1.14 and output them as frozen graphs. Then I import the frozen.pb files into 'netron.app' to check their structures. Oddly, there are two elements at the input: x and Identity. Here is a screenshot.custom model input
But when I froze a pre-trained model, such as the mobileNetV2, the input only has the x. pre-trained mobileNetV2 input
Does anyone have a clue why?

What are the image datasets that pre trained weights are available in keras?

I want to know,
what are the datasets that the pretrained weights are available in keras. Let's say keras inception v3 model refering the weights of imagenet dataset.
keras.applications.InceptionV3(weights='imagenet')
But I want to know other datasets like imagenet?
It says in the docs: One of None (random initialization), imagenet (pre-training on ImageNet), or the path to the weights file to be loaded. Default to imagenet. Link to the docs: https://www.tensorflow.org/api_docs/python/tf/keras/applications/InceptionV3

How to convert a Tensorflow session to a keras model object?

Suppose I have a pre-trained model stored in a Tensorflow checkpoint. I'd like to convert it into a Keras model. I can load the checkpoint into a TF session alright but that's where I get stuck.
I think it's impossible to create a Keras model using TF checkpoint, but you can copy it's weights to the already created Keras model.
Checkout this. https://github.com/yuyang-huang/keras-inception-resnet-v2
The extract_weights.py is to save the TF weights to numpy array, while load_weights.py is for load the npy file to the Keras model.
For more reference, this is how I implement it https://github.com/DableUTeeF/keras-efficientnet/tree/master/keras_efficientnet.

Fine-Tuning Keras model

I'm working on facial expression recognition using CNN. I'm using Keras and Tensorflow as backend. My model is saved to h5 format.
I want to retrain my network, and fine-tune my model with the VGG model.
How can I do that with keras ?
Save your models architecture and weights:
json_string = model.to_json()
model.save_weights('model_weights.h5')
Load model architecture and weights:
from keras.models import model_from_json
model = model_from_json(json_string)
model.load_weights('model_weights.h5')
Start training again from here for finetuning. I hope this helps.
You can use the Keras model.save(filepath) function.
Details for the various Keras saving and loading techniques are discussed with examples in this YouTube video: Save and load a Keras model
model.save(filepath)saves:
The architecture of the model, allowing to re-create the model.
The weights of the model.
The training configuration (loss, optimizer).
The state of the optimizer, allowing to resume training exactly where you left off.
To load this saved model, you would use the following:
from keras.models import load_model
new_model = load_model(filepath)
If you used model.to_json(), you would only be saving the architecture of the model. Additionally, if you used model.save_weights(), you would only be saving the weights of the model. With both of these alternative saving techniques, you would not be saving the training configuration (loss, optimizer), nor would you be saving the state of the optimizer.

Saving Word2Vec for CNN Text Classification

I want to train my own Word2Vec model for my text corpus. I can get the code from TensorFlow's tutorial. What I don't know is how to save this model to use for CNN text classification later? Should I use pickle to save it and then read it later?
No pickling is not the way of saving the model in case of tensorflow.
Tensorflow provides with tensorflow serving for saving the models as proto bufs(for exporting the model). The way to save model would be to save the tensorflow session as:
saver.save(sess, 'my_test_model',global_step=1000)
Heres the link for complete answer:
Tensorflow: how to save/restore a model?
You can use pickle to save it to disk. Then when you are creating the CNN model, load the saved word embedding table and use it to initialize the TensorFlow variable that holds the word embeddings for your CNN classifier.