Let's start at the beginning. So far I have created and trained small networks in Tensorflow myself. During the training I save my model and get the following files in my directory:
model.ckpt.meta
model.ckpt.index
model.ckpt.data-00000-of-00001
Later, I load the model saved in network_dir to do some classifications and extract the trainable variables of my model.
saver = tf.train.import_meta_graph(network_dir + ".meta")
variables = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope="NETWORK")
Now I want to work with larger pretrained models like the VGG16 or ResNet and want to use my code to do that. I want to load pretrained models like my own networks as shown above.
On this site, I found many pretrained models:
https://github.com/tensorflow/models/tree/master/research/slim#pre-trained-models
I downloaded the VGG16 checkpoint and realized that these are only the trained parameters.
I would like to know how or where I can get the saved model or graph structure of these pretrained network? How do I use, for example, the VGG16 checkpoint without model.ckpt.meta, model.ckpt.index and the model.ckpt.data-00000-of-00001 files?
Next to the weights link, there is link to the code that defines the model. For instance, for VGG16: Code. Create the model using the code and restore variables from the checkpoint:
import tensorflow as tf
slim = tf.contrib.slim
image = ... # Define your input somehow, e.g with placeholder
logits, _ = vgg.vgg_16(image)
predictions = tf.argmax(logits, 1)
variables_to_restore = slim.get_variables_to_restore()
saver = tf.train.Saver(variables_to_restore)
with tf.Session() as sess:
saver.restore(sess, "/path/to/model.ckpt")
So, the code contained in vgg.py will create all the variables for you. Using the tf-slim helper, you can get the list. Then, just follow the usual procedure. There was a similar question on this.
Related
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.
I have trained a seq2seq language translation model on tensorflow and save in the form of checkpoints with the following files in my train folder.
translate.ckpt-157450.data-00000-of-00001
translate.ckpt-157450.index
translate.ckpt-157450.meta and
checkpoint file
Now, I want to convert it to a protobuf file (.pb) for deployment purposes. Here is some code that I am using:
import tensorflow as tf
meta_path = "/home/i9/L-T_Model_Training/01_Apr_model/train/translate.ckpt-157450.meta"
with tf.Session() as sess:
saver = tf.train.import_meta_graph(meta_path)
saver.restore(sess, tf.train.latest_checkpoint('.'))
output_node_names =[n.name for n in tf.get_default_graph().as_graph_def().node]
frozen_graph = tf.graph_util.convert_variables_to_constants(sess, sess_graph_def, output_node_names)
with open("output_graph.pb", "wb") as f:
f.write(frozen_graph.SerializeToString())
I am running this code inside my train folder.
It shows me an error: ValueError: Can't load save_path when it is None.
I also tried freeze_graph.py script but could not get the model.
I did it for a NVIDIA/OpenSeq2Seq trained model, don't know if it is your case.
I created a gist file with the relevant code.
Basically, the sequence I did was:
Load the model
Call build_trt_forward_pass_graph (it's the only way I could get it working)
Get the right output node
Fix batch norm nodes
Freeze the graph
Save it
Please let me know if you have other ideas and if you try it, share the results with us.
Regards
I am newbie of Tensorflow and trying to run one tutorial code located in https://github.com/Hvass-Labs/TensorFlow-Tutorials/blob/master/02_Convolutional_Neural_Network.ipynb
Based on this code, I would like to try to save the model in .pb format using simple_save and restore it for testing but I have no idea how to modify this piece of code. I have browsed some web pages but still didn't get the idea. Can anyone help me change this piece of code so that I can save the trained model and then load it for inference? Thank you!
For saving model, you need two things - input and output tensor names. In your case, input tensor is called x and output tensor is y_pred and y_pred_cls (mentioned in In [29] in notebook). Here's simple example to save your model:
simple_save(session,
export_dir,
inputs={"x": x,},
outputs={"y_pred": y_pred,
"y_pred_cls": y_pred_class})
EDIT:
Restoring-
restoring_graph = tf.Graph()
with restoring_graph.as_default():
with tf.Session(graph=restoring_graph) as sess:
# Restore saved values
tf.saved_model.loader.load(
sess,
[tag_constants.TRAINING],
export_dir # Path to SavedModel
)
# Pass inputs to model and do predictions below
I have been unable to figure out how to use transfer learning/last layer retraining with the new TF Estimator API.
The Estimator requires a model_fn which contains the architecture of the network, and training and eval ops, as defined in the documentation. An example of a model_fn using a CNN architecture is here.
If I want to retrain the last layer of, for example, the inception architecture, I'm not sure whether I will need to specify the whole model in this model_fn, then load the pre-trained weights, or whether there is a way to use the saved graph as is done in the 'traditional' approach (example here).
This has been brought up as an issue, but is still open and the answers are unclear to me.
It is possible to load the metagraph during model definition and use SessionRunHook to load the weights from a ckpt file.
def model(features, labels, mode, params):
# Create the graph here
return tf.estimator.EstimatorSpec(mode,
predictions,
loss,
train_op,
training_hooks=[RestoreHook()])
The SessionRunHook can be:
class RestoreHook(tf.train.SessionRunHook):
def after_create_session(self, session, coord=None):
if session.run(tf.train.get_or_create_global_step()) == 0:
# load weights here
This way, the weights are loaded in first step and saved during training in model checkpoints.
I trained a FCN model in Tensorflow following implementation in link and saved the complete model as checkpoint, Now I want to use the saved model(pre-trained) for different problem.
I tried to restore the model from checkpoint by specifying the weights in Saver as:
saver = tf.train.Saver({"weights" : [w1_1,w1_2,w2_1,w2_2,w3_1,w3_2,w3_3,w3_4, w4_1, w4_2, w4_3, w4_4,w5_1,w5_2,w5_3,w6,w7]})
I am getting weights as:
w1_1=tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES,scope='inference/conv1_1_w')
and so on....
I am not able to restore it successfully (up to specific layer).
Tensorflow version:0.12r
Either you can call init = tf.initialize_variables([list_of_vars]) followed by sess.run(init) and that would reinitialize those variables for you, or you can recreate the graph with same structure from the point where you want to freeze the weights but keep different names for variables. Further in case you only want to train certain variables only, you can pass those variables only to optimizer. tf.train.AdamOptimizer(learning_rate).minimize(loss,var_list = [wi, wj, ....])