I have successfully retrained the last layer of TensorFlow inception using my own images and three categories. Now I want to be able to export out the probabilities for each image so I can look at a few performance metrics.
I figured this should be pretty easy and I tried to implement the code here: label_dir.py but I got an error that says there is an invalid wire type.
Traceback (most recent call last):
File "tb/inception/tf_files/Label_AllImages_v3.py", line 28, in <module>
graph_def.ParseFromString(f.read())
File "/share/apps/opt/rh/python27/root/usr/lib/python2.7/site-packages/google/protobuf/message.py", line 185, in ParseFromString
self.MergeFromString(serialized)
File "/share/apps/opt/rh/python27/root/usr/lib/python2.7/site-packages/google/protobuf/internal/python_message.py", line 1091, in MergeFromString
if self._InternalParse(serialized, 0, length) != length:
File "/share/apps/opt/rh/python27/root/usr/lib/python2.7/site-packages/google/protobuf/internal/python_message.py", line 1117, in InternalParse
new_pos = local_SkipField(buffer, new_pos, end, tag_bytes)
File "/share/apps/opt/rh/python27/root/usr/lib/python2.7/site-packages/google/protobuf/internal/decoder.py", line 850, in SkipField
return WIRETYPE_TO_SKIPPER[wire_type](buffer, pos, end)
File "/share/apps/opt/rh/python27/root/usr/lib/python2.7/site-packages/google/protobuf/internal/decoder.py", line 820, in _RaiseInvalidWireType
raise _DecodeError('Tag had invalid wire type.')
google.protobuf.message.DecodeError: Tag had invalid wire type.
When I tried to implement this SO Post suggestion save/restore graph I still get an error: google.protobuf.text_format.ParseError: 1:1 : Message type "tensorflow.GraphDef" has no field named
I am obviously missing something fairly simple because I imagine that many people are doing what I am trying to do...My Label_AllImages_v3.py
code simply altered the paths and then wrote the results to a file:
import tensorflow as tf
import sys
# change this as you see fit
#image_path = sys.argv[1]
# Read in the image_data
#image_data = tf.gfile.FastGFile(image_path, 'rb').read()
import os
import shutil
from os import listdir
from os import mkdir
from shutil import copyfile
from os.path import isfile, join
varPath = 'tb/inception/tf_files/test_labels'
#destDir = "tb/inception/tf_files/data_dir/abnormal/"
target = open("tb/inception/test_classifier.txt","w")
imgFiles = [f for f in listdir(varPath) if isfile(join(varPath, f))]
# Loads label file, strips off carriage return
label_lines = [line.rstrip() for line
in tf.gfile.GFile("tb/inception/tf_files/retrained_labels.txt")]
# Unpersists graph from file
with tf.gfile.FastGFile("tb/inception/tf_files/retrained_labels.txt", 'rb') as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
_ = tf.import_graph_def(graph_def, name='')
with tf.Session() as sess:
# Feed the image_data as input to the graph and get first prediction
softmax_tensor = sess.graph.get_tensor_by_name('final_result:0')
#try:
# shutil.rmtree(destDir)
#except:
# None
#mkdir ('scanned')
for imageFile in imgFiles:
image_data = tf.gfile.FastGFile(varPath+"/"+imageFile, 'rb').read()
print (varPath+"/"+imageFile)
predictions = sess.run(softmax_tensor, \
{'DecodeJpeg/contents:0': image_data})
# Sort to show labels of first prediction in order of confidence
#top_k = predictions[0].argsort()[-len(predictions[0]):][::-1]
firstElt = top_k[0];
newFileName = label_lines[firstElt] +"--"+ str(predictions[0][firstElt])[2:7]+".jpg"
print(newFileName)
#copyfile(varPath+"/"+imageFile, destDir+"/"+newFileName)
this_score=imageFile
for node_id in top_k:
human_string = label_lines[node_id]
score = predictions[0][node_id]
#print (node_id)
#print('%s (score = %.5f)' % (human_string, score))
this_score += "," + human_string + "," + str(score)
target.write(this_score + "\n")
target.close