How to save full tensorflow model from google colab - python

I trained a computer vision model in google colab.
Now I want to get it to my computer to work with it.
If I do model.save("name.h5") and then download the .h5 file, does this save the trained model?
Or is it just the untrained structure of the model?

Yes, model.save("name.h5") saves the trained model. Of course, you should execute this line after you have trained/fit the model.
However, model.save() only saves the model structure and the updated weights. And it does not store any loss function weights and information of the loss function. Therefore, you should avoid re-training your model after loading it from the saved file.
If you are willing to further re-train your model, you should use tf.keras.models.save_model() to save a model, and tf.keras.models.load_model() to load a model. Further information can be found in the Keras documentation.

Related

How to train pre trained model (MNIST) tensorflow

I have a project with Fashion MNIST, which predicts clothes from uploaded images, and I want to make some improvements with it. Is it possible to modify my project that it will train automatically after each uploaded and prediction?
You can train your model manually by using the transfer learning technique(Transfer learning is a method of reusing an already trained model for another task).
Instantiate a base model and load pre-trained weights into it.
Freeze all layers in the base model by setting trainable = False.
Create a new model on top of the output of one (or several) layers
from the base model. Train your new model on your new dataset.
Please refer to this gist for working code example. Thank You.

Is there a way to use a pre-trained transformers model without the configuration file?

I would like to fine-tune a pre-trained transformers model on Question Answering. The model was pre-trained on large engineering & science related corpora.
I have been provided a "checkpoint.pt" file containing the weights of the model. They have also provided me with a "bert_config.json" file but I am not sure if this is the correct configuration file.
from transformers import AutoModel, AutoTokenizer, AutoConfig
MODEL_PATH = "./checkpoint.pt"
config = AutoConfig.from_pretrained("./bert_config.json")
model = AutoModel.from_pretrained(MODEL_PATH, config=config)
The reason I believe that bert_config.json doesn't match "./checkpoint.pt" file is that, when I load the model with the code above, I get the error that goes as below.
Some weights of the model checkpoint at ./aerobert/phase2_ckpt_4302592.pt were not used when initializing BertModel: ['files', 'optimizer', 'model', 'master params']
This IS expected if you are initializing BertModel from the checkpoint of a model trained on another task or with another architecture (e.g. initializing a BertForSequenceClassification model from a BertForPreTraining model).
This IS NOT expected if you are initializing BertModel from the checkpoint of a model that you expect to be exactly identical (initializing a BertForSequenceClassification model from a BertForSequenceClassification model).
Some weights of BertModel were not initialized from the model checkpoint at ./aerobert/phase2_ckpt_4302592.pt and are newly initialized: ['encoder.layer.2.attention.output.LayerNorm.weight', 'encoder.layer.6.output.LayerNorm.bias', 'encoder.layer.7.intermediate.dense.bias', 'encoder.layer.2.output.LayerNorm.bias', 'encoder.layer.21.attention.self.value.bias', 'encoder.layer.11.attention.self.value.bias', ............
If I am correct in assuming that "bert_config.json" is not the correct one, is there a way to load this model correctly without the config.json file?
Is there a way to see the model architecture from the saved weights of checkpoint.pt file?
This is a warning message instead of a error.
It means that the pretrained model is pretrained in some task (such as Question Answering, MLM etc), if your own fine tune task is the same as those pretrained task, then this IS NOT expected; unless this IS expected because some pooler of pretrained model will not be used in fine tune.
But this message doesn't mean that the bert_config.json isn't the right one. You can test it on huggingface's official colab notebook
You can find more information in this issue.

Adding new images data to previously trained and saved tensorflow h5 model

I have a pre-trained tensorflow h5 saved model to classify images.
here is the block of code :
import tensorflow as tf
model_version = "1"
model_name = "fresh-rotten-model"
model_path = os.path.join(model_name, model_version)
tf.saved_model.save(model, model_path)
model.save("fresh-rotten-model.h5")
I built a back-end that will upload new images every week using a schedule to a node server
Is there any way to add these images as a new data to train the model and build a new model without having to train the whole data set again ?
You can't just add data to a model file, a model file contains only weights, not the data used in it, so you'll have to have something (supposedly your backend server) do the training every time you want to update the model. You can kind of update the model by loading it and then doing more training epochs with it with the extended dataset, but other than that, there's not much you can do.

Use tensorflow's .ckpt model

I used this code for training a model.
I now have 3 files:
model.ckpt-1.meta
model.ckpt-1.index
model.ckpt-1.data-00000-of-00001
How (with what methods) can I use these models now?
I'm not exactly sure what you mean with
How (with what methods) can I use these models now?
The model is not saved in those files but i can be restored with them.
Those*.ckpt get saved during training but do not contain your model. If you want to "use" your model you need to restore those files to it. Take a look at Tensorflow's Checkpoint and CheckpointManager. This tutorial shows a simple snipped of how to restore .ckpt files to your model.

TensorFlow restore/deploy network without the model?

I've built and trained some networks with TensorFlow and successfully managed to save and restore the model's parameters.
However, for some scenarios - e.g. like deploying a trained network in a customer's infrastructure - it is not the best solution to ship the full code/model. Thus, I am wondering if there is any way to restore/run a trained network without the original code/model used for training?
I guess this leads to the question if TensorFlow is able to save a (compressed?) version of the network architecture into the checkpoint files in addition to the weights of the variables.
Is this somehow possible?
If you really need to restore just from the graphdef file (*.pb), to load it from another application for instance, you will need to use the freeze_graph.py script from here: https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/tools/freeze_graph.py
This script takes a graphdef (.pb) and a checkpoint (.ckpt) file as input and outputs a graphdef file which contains the weights in the form of constants (you can read the docs on the script for more details).

Categories