tensorflow TypeError: ParseFromString() missing 1 required positional argument: 'serialized' - tensorflow

this is my first time using tensor flow i want to try captcha solver i found in the internet but i got an error
the link the tutorial https://pylessons.com/TensorFlow-CAPTCHA-final-detection/
TypeError: ParseFromString() missing 1 required positional argument: 'serialized'
here is my code
# Load a (frozen) Tensorflow model into memory.
label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=NUM_CLASSES, use_display_name=True)
category_index = label_map_util.create_category_index(categories)
detection_graph = tf.Graph()
with detection_graph.as_default():
od_graph_def = tf.GraphDef
with tf.gfile.GFile(PATH_TO_FROZEN_GRAPH, 'rb') as fid:
serialized_graph = fid.read()
od_graph_def.ParseFromString(serialized_graph)
tf.import_graph_def(od_graph_def, name='')

If you are using tensorflow version >= 2.0 then above code won't work. Below is the updated code which will work on tensorflow version > 2.0
with detection_graph.as_default():
od_graph_def = tf.compat.v1.GraphDef()
with tf.compat.v1.gfile.GFile(PATH_TO_FROZEN_GRAPH, 'rb') as fid:
serialized_graph = fid.read()
od_graph_def.ParseFromString(serialized_graph)
tf.compat.v1.import_graph_def(od_graph_def, name='')

Related

how import a model from .pb file

'''
with tf.Session() as sess:
model_filename="./model/skipGram-word2Vec/saved_model.pb"
with gfile.FastGFile(model_filename,'rb') as f:
graph_def=tf.GraphDef()
graph_def.ParseFromString(f.read())
sess.graph.as_default()
result=tf.import_graph_def(graph_def,name='')
print(sess.run(result))
'''
then ,the error occured:
DecodeError: Wrong wire type in tag.
Here is how you can load the model from .pb file in tensorflow 2.0
import tensorflow as tf
GRAPH_PB_PATH = './frozen_model.pb'
with tf.compat.v1.Session() as sess:
print("load graph")
with tf.io.gfile.GFile(GRAPH_PB_PATH,'rb') as f:
graph_def = tf.compat.v1.GraphDef()
graph_def.ParseFromString(f.read())
sess.graph.as_default()
tf.import_graph_def(graph_def, name='')
graph_nodes=[n for n in graph_def.node]
names = []
for t in graph_nodes:
names.append(t.name)
print(names)

How to load Tensorflow frozen graph model from Google bucket?

Locally when we want to load model using TensorFlow we do this:
path_to _frozen = model_path + '/frozen_inference_graph.pb'
detection_graph = tf.Graph()
with detection_graph.as_default():
od_graph_def = tf.GraphDef()
with tf.io.gfile.GFile(path_to _frozen, 'rb') as fid:
serialized_graph = fid.read()
od_graph_def.ParseFromString(serialized_graph)
tf.import_graph_def(od_graph_def, name='')
How we can load a stored model on google bucket using google cloud function?
def download_blob(bucket_name, source_blob_name, destination_file_name):
storage_client = storage.Client()
bucket = storage_client.get_bucket(bucket_name)
blob = bucket.blob(source_blob_name)
blob.download_to_filename(destination_file_name)
def handler(request):
download_blob(BUCKET_NAME,'redbull/output_inference_graph.pb/frozen_inference_graph.pb','/tmp/frozen_inference_graph.pb')
print("okay")
detection_graph = tf.Graph()
with detection_graph.as_default():
od_graph_def = tf.GraphDef()
with tf.io.gfile.GFile('/tmp/frozen_inference_graph.pb', 'rb') as fid:
serialized_graph = fid.read()
od_graph_def.ParseFromString(serialized_graph)
tf.import_graph_def(od_graph_def, name='')
You can store your pb file in storage.
Then, in your function, download it in the local writable directory /tmp. Remember, this directory is "in memory". This mean that the memory allocated to your function has to be well defined to handle your app memory footprint AND your model downloaded file
Replace your first line by something like this.
# Be sure that your function service account as access to the storage bucket
storage_client = storage.Client()
bucket = storage_client.get_bucket('<bucket_name>')
blob = bucket.blob('<path/to>/frozen_inference_graph.pb')
# Download locally your pb file
path_to_frozen = '/tmp/frozen_inference_graph.pb'
blob.download_to_filename(path_to_frozen)

What is 'tensorflow' has no attribute 'GraphDef' with Tensorflow version 2.0.0?

I am using the following function in part of object detection application I am working on
detection_graph = tf.Graph()
with detection_graph.as_default():
od_graph_def = tf.GraphDef()
with tf.gfile.GFile(PATH_TO_FROZEN_GRAPH, 'rb') as fid:
serialized_graph = fid.read()
od_graph_def.ParseFromString(serialized_graph)
tf.import_graph_def(od_graph_def, name='')
When I run this code I get the following error :
AttributeError Traceback (most recent call last)
<ipython-input-6-d55b98fd5a78> in <module>
1 detection_graph = tf.Graph()
2 with detection_graph.as_default():
----> 3 od_graph_def = tf.GraphDef()
4 with tf.gfile.GFile(PATH_TO_FROZEN_GRAPH, 'rb') as fid:
5 serialized_graph = fid.read()
AttributeError: module 'tensorflow' has no attribute 'GraphDef'
I am using TensorFlow 2.0.0. Is this a version mismatch related?
This code is from the following link.
Yes, it is a version mismatch issue, you should not blindly assume that every library supports TensorFlow 2.0, which was only recently released.
If a library does not advertise explicit support for TensorFlow 2.0, then you should just use it with TensorFlow 1.x
Make sure you have properly installed the libraries as given in link,
https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/installation.md

Tensorflow: error trying to restore a model in a pb file

I'm trying to load an already trained model taken from https://github.com/tensorflow/models/tree/master/official/resnet, but when I try to load the .pb I get an error on ParseFromString method:
import tensorflow as tf
from tensorflow.python.platform import gfile
GRAPH_PB_PATH = '../resnet_v2_fp32_savedmodel_NHWC/1538687283/saved_model.pb'
with tf.gfile.FastGFile(GRAPH_PB_PATH, "rb") as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
g_in = tf.import_graph_def(graph_def, name="")
sess = tf.Session(graph=g_in)
DecodeError: Error parsing message
What I wrong?
I was having a similar problem, instead of using gfile I use the tf.saved_model.loader.load function like in this post https://stackoverflow.com/a/46547595/4637693:
sess = tf.Session(graph=tf.Graph())
model = tf.saved_model.loader.load(sess, [tf.saved_model.tag_constants.SERVING], './model')
graph_def = model.graph_def

Tensorflow, use import_graph_def() to load model error

I attempt to read an RNN network through import_graph_def() and do inference.
But I cannot use tf.trainable_variables() to get any variables.
In the following code, tf.trainable_variables() returns [] (a list with nothing)
Also, when I use saver = tf.train.Saver(), tensorflow reports "no variables to save"
def eval_on_test(graph_path):
batch_size = 80
train_begin = 0
train_end = 3000
with tf.Graph().as_default() as graph:
with open(graph_path, 'rb') as f:
tf_graph = tf.GraphDef()
print("Loading graph_def from {}".format(graph_path))
tf_graph.ParseFromString(f.read())
return_elements = tf.import_graph_def(tf_graph, name="", return_elements=['input_x:0', 'output_y:0', 'pred:0', 'loss:0'])
X = return_elements[0]
Y = return_elements[1]
pred = return_elements[2]
loss = return_elements[3]
tf_config = tf.ConfigProto()
tf_config.gpu_options.allow_growth = True
print("graph loaded, start testing")
with tf.Session(config=tf_config) as sess:
init_op = sess.graph.get_operation_by_name('init')
sess.run(init_op)
print(tf.trainable_variables())
batch_index,train_x,train_y=get_train_data(batch_size,time_step,train_begin,train_end)
for batch in range(len(batch_index)-1):
loss_ = sess.run(loss, feed_dict={X:train_x[batch_index[batch]:batch_index[batch+1]],Y:train_y[batch_index[batch]:batch_index[batch+1]]})
print(batch, loss_)
Any help would be appreciated.
import_graph_def will only restore graph but will not restore collections such as GLOBAL_VARIABLES, that's why Saver can't find any variables in graph, to solve this, you can try tf.train.import_meta_graph which will also restore all collections.