NameError: name 'path' is not defined - python

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.

Related

AttributeError: module 'tensorflow' has no attribute 'WholeFileReader'

I have been trying to implement this piece of code and realized a lot has been deprecated and updated in TensorFlow V2.
CODE
filenames=['images/000001.jpg','images/000002.jpg','images/000003.jpg','images/000004.jpg']
labels=[1,0,1,0]
filename_queue=tf.train.string_input_producer(filenames)
reader=tf.WholeFileReader()
filename, content = reader.read(filename_queue)
images=tf.image.decode_jpeg(content, channels=3)
images=tf.cast(images, tf.float32)
resized_images=tf.image.resize_images(images, (224, 224))
I have altered the tf.train.string_input_producer(filenames) part. But I am not able to find any alternative to tf.WholeFileReader(). Also I am using google colab and thus cannot rely on V1 as it says colab don't support TensorFlow V2.
Here's my piece of code.
My code
import os
import numpy as np
image_path = '/content/drive/MyDrive'
categ = ["IMG"]
for c in categ:
path = os.path.join(image_path, c)
for img in os.listdir(path):
image = [os.path.join(path, img)]
queue=tf.data.Dataset.from_tensor_slices(image)
reader=tf.WholeFileReader() #AttributeError
Can anyone help?
You probably should be able to find it here:
tf.io.WholeFileReader()
instead of
tf.WholeFileReader()

AttributeError: 'Graph' object has no attribute 'signatures'

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.

implement tflite on tensorflow library

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

python UnicodeEncodeError:"utf-8" codec can't decode character "\udea8"

I got this error when I ran the notebook on jupyter.
log_dir = "inception_log"
if not os.path.exists(log_dir):
os.makedirs(log_dir)
#classify_image_graph_def.pd是google訓練好的模型
inception_graph_def_file = os.path.join("inception_model","classify_image_graph_def.pd")
with tf.Session() as sess:
#建一張圖存放google訓練好的模型
with tf.gfile.FastGFile(inception_graph_def_file,'rb') as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
tf.import_graph_def(graph_def,name = "")
#保存圖的結構
print("start")
writer = tf.summary.FileWriter("inception_log",sess.graph)
writer.close()
print("end")
I'm not sure is this error cause I couldn't write a file in my folder.
I tried to google this error but didn't find the way to solve this error.
Thanks.

Using pre-trained Inception_v4 model

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

Categories

Resources