I'm trying to use Tensorflow's Object Detection API with a pre-trained model. I'm loading the model with this:
model_name='fish_inception_v2_graph2'
PATH_TO_CKPT='models/research/object_detection/'+model_name+'/frozen_inference_graph.pb'
### Load a (frozen) Tensorflow model into memory.
detection_model = tf.Graph()
with detection_graph.as_default():
od_graph_def = tf.compat.v1.GraphDef()
with tf.compat.v2.io.gfile.GFile(PATH_TO_CKPT, 'rb') as fid:
serialized_graph = fid.read()
od_graph_def.ParseFromString(serialized_graph)
tf.import_graph_def(od_graph_def, name='')
That seems to work fine but then the API has a section to "Check the model's input signature, it expects a batch of 3-color images of type uint8":
print(detection_model.signatures['serving_default'].inputs)
When I run that is when I get the error "AttributeError: 'Graph' object has no attribute 'signatures'".
Anyone know how to fix this? Thank you!
In the path to the checkpoint just mention the model.ckpt-10000 or something of that sort. You are providing a .pb file and hence the error.
Related
Here is a sample code of my training function(unnecessary parts are deleted):
I am trying to save my model data_gen in the torch.save(), and after running the train_dmc function, I can find the checkpoint file in the directory.
def train_dmc(loader,loss):
data_gen = DataGenerator().to(device)
data_gen_optimizer = optim.Rprop(para_list, lr=lrate)
savepath='/content/drive/MyDrive/'+loss+'checkpoint.t7'
state = {
'epoch': epoch,
'model_state_dict': data_gen.state_dict(),
'optimizer_state_dict': data_gen_optimizer.state_dict(),
'data loss': data_loss,
'latent_loss':latent_loss
}
torch.save(state,savepath)
My question is that how to load the checkpoint file to continue training if Google Colab disconnects.
Should I load data_gen or train_dmc(), it is my first time using this and I am really confused because the data_gen is defined inside another function. Hope someone can help me with explanation
data_gen.load_state_dict(torch.load(PATH))
data_gen.eval()
#or
train_dmc.load_state_dict(torch.load(PATH))
train_dmc.eval()
As the state variable is a dictionary, So try saving it as:
with open('/content/checkpoint.t7', 'wb') as handle:
pickle.dump(state, handle, protocol=pickle.HIGHEST_PROTOCOL)
Initiate your model class as data_gen = DataGenerator().to(device).
And load the checkpoint file as:
import pickle
file = open('/content/checkpoint.t7', 'rb')
loaded_state = pickle.load(file)
Then you can load the state_dict using data_gen = loaded_state['model_state_dict']. This will load the state_dict to the model class!
I tried to solve the problem by writing the following code but no luck.
with detection_graph.as_default():
od_graph_def = tf.GraphDef()
with tf.gfile.GFile(path, 'r') as fid:
serialized_graph = fid.read()
Then I saw an error like this
NameError: name 'path' is not defined
How can I fix it?
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
I just guess because the infomation you give is not enough. Just try it if your tensorflow has version issue.
Try:
detection_graph = tf.Graph()
with detection_graph.as_default():
od_graph_def = tf.compat.v1.GraphDef()
Go to environment variables and add it to PATH.
I am trying to convert my saved model to a tflite model, the saved model is saved on my desktop, however when I try and run this code:
I gen an error -
OSError: SavedModel file does not exist at: C:/Users/Omar/Desktop/model00000014.h5/{saved_model.pbtxt|saved_model.pb}.
Not sure what the problem is.
import tensorflow as tf
saved_model_dir = "r"C:/Users/Omar/Desktop/model00000014.h5""
converter = tf.lite.TFLiteConverter.from_saved_model(saved_model_dir)
tflite_model = converter.convert()
open("converted_model.tflite", "wb").write(tflite_model)
If you're trying to convert a .h5 Keras model to a TFLite model, make sure you use TFLiteConverter.from_keras_model() method, as described in the docs,
model = tf.keras.models.load( "C:/Users/Omar/Desktop/model00000014.h5" )
converter = tf.lite.TFLiteConverter.from_keras_model( model )
open( 'model.tflite' , 'wb' ).write( converter.convert() )
In case of a SavedModel, use TFLiteConverter.from_saved_model() and mention the file path of the directory of the SavedModel,
saved_model_dir = 'path/to/savedModelDir'
converter = tf.lite.TFLiteConverter.from_saved_model(saved_model_dir)
You're providing a Keras Model to the TFLiteConverter.from_saved_model() method, which might be causing an error.
i have done tensorflow object detection to counting object. but i want to cange it to tensorflow Lite model. for example i have TF model loader bellow, how to change this code to support tflite models? i'm new on tflite, i'm so confused.
detection_graph = tf.Graph()
with detection_graph.as_default():
od_graph_def = tf.compat.v1.GraphDef()
with tf.io.gfile.GFile(PATH_TO_CKPT, 'rb') as fid:
serialized_graph = fid.read()
od_graph_def.ParseFromString(serialized_graph)
tf.import_graph_def(od_graph_def, name='')
any helps will be appriciate
thankyou
https://github.com/tensorflow/models/tree/master/slim
This gives download link for checkpoints for Inception v1-4 pretrained models. However, the tar.gz contains only the .ckpt file.
In the tutorial on using Inception v3 2012 [This link], the tar.gz contains .pb and .pbtxt files which are used for classification.
How can i use just the .ckpt file to generate respective .pb and .pbtxt files?
OR
Is there any alternate way of using the .ckpt file for classification?
Even i am also trying inception_v4 model. During my search i could able to find the the checkpoint files contains the weights. So inorder to use this, inception_v4 graph needed to be loaded from inception_v4.py and the session needed to be restored from the checkpoint file. Following code will read the checkpoint file and create the protobuf file.
import tensorflow as tf
slim = tf.contrib.slim
import tf_slim.models.slim.nets as net
# inception_v3_arg_scope
import tf_slim
import inception_v4 as net
import cv2
# checkpoint file
checkpoint_file = '/home/.../inception_v4.ckpt'
# Load Session
sess = tf.Session()
arg_scope = net.inception_v4_arg_scope()
input_tensor = tf.placeholder(tf.float32, [None, 299, 299, 3])
with slim.arg_scope(arg_scope):
logits, end_points = net.inception_v4(inputs=input_tensor)
saver = tf.train.Saver()
saver.restore(sess, checkpoint_file)
f = tf.gfile.FastGFile('./mynet.pb', "w")
f.write(sess.graph_def.SerializeToString())
f.close()
# reading the graph
#
with tf.gfile.FastGFile('./mynet.pb', 'rb') as fp:
graph_def = tf.GraphDef()
graph_def.ParseFromString(fp.read())
with tf.Session(graph=tf.import_graph_def(graph_def, name='')) as sess:
# op = sess.graph.get_operations()
# with open('./tensors.txt', mode='w') as fp:
# for m in op:
# # print m.values()
# fp.write('%s \n' % str(m.values()))
cell_patch = cv2.imread('./car.jpg')
softmax_tensor = sess.graph.get_tensor_by_name('InceptionV4/Logits/Predictions:0')
predictions = sess.run(softmax_tensor, {'Placeholder:0': cell_patch})
But the above code wont give you the predictions. Because I am facing problem in giving the input to the graph. But It can be of good starting point to work with checkpoint files.
Checkpoint is downloaded from following link checkpoints