1

I want to use a pre-trained model and add a segmentation head at the end of that, but the problem is that I just have the 'frozen_inference_graph.pb'. These are the files that I have from the model:

enter image description here

I have tried several ways:

1. loading the pre-trained model into a Keras model: It seems to be impossible with the files that I have. It just gives me an AutoTrackable object instead of a model.

2. Accessing the Tensor Objects of the frozen model and make the model with tensors: I found out how to access the tensors but couldn't make a Keras model with Tensor objects.

with self.graph.as_default():
    graph = tf.import_graph_def(graph_def, name='')

graph = tf.compat.v1.import_graph_def(graph_def)
tf.compat.v1.Graph.as_default(graph)
self.sess = tf.Session(graph=self.graph)
self.tensors = [tensor for op in tf.compat.v1.get_default_graph().get_operations() for tensor in op.values()]

Here I can get the tensors but I can't use the tensors in the model:

model = tf.keras.models.Model(inputs=self.tensors[0], outputs=self.tensors[-1])

Is there any way to convert this frozen graph to a Keras model? Or If there is another approach which I can train the model, I would be glad to know.

P.S. The pre-trained model is 'ssd_mobilenet_v3_small_coco_2020_01_14' which can be found Here.

malekiamir
  • 119
  • 2
  • 11
  • I've added more details about the problem and it's more clear now. If you please reopen the question. Thanks – malekiamir Aug 23 '20 at 09:04
  • Isn't that a [saved model](https://www.tensorflow.org/guide/saved_model) directory? About using a frozen graph, see if [this answer](https://stackoverflow.com/a/63619517/1782792) helps. – jdehesa Sep 01 '20 at 12:40

1 Answers1

2

You can use two methods:

  1. The file 'frozen_inference_graph.pb' contains all necessary information about the weights and the model architecture. Use the following snippet to read the model and add a new layer:a
 customModel = tf.keras.models.load_model('savedModel')  
 # savedModel is the folder with .pb data

 pretrainedOutput = customModel.layers[-1].output 
 newOutput = tf.keras.layers.Dense(2)(pretrainedOutput) # change layer as needed

 new_model = tf.keras.Model(inputs=customModel.inputs, outputs=[newOutput]) 
 # create a new model with input of old model and new output tensors

where 'savedModel' is the name of the folder with 'frozen_inference_graph.pb' and other meta data. See details about using .pb files and finetuning the custom models in the TFguide.

  1. Try using .meta file with model architecture and .ckpt to restore the weights in TF 1.x:
with tf.Session() as sess:
  new_saver = tf.train.import_meta_graph('my_test_model-1000.meta')
  new_saver.restore(sess, tf.train.latest_checkpoint('./'))

Refer to the tutorial on how to load and customize restored models in TF 1.x.