当使用tensorflow开始时,如何标记整个目录(在python中)?

时间:2017-03-31 15:50:46

标签: python tensorflow

我使用自己的图像和三个类别成功地重新训练了最后一层TensorFlow。现在我希望能够导出每个图像的概率,以便我可以查看一些性能指标。

我认为这个应该非常简单,我试图在这里实现代码:label_dir.py但是我收到一个错误,指出存在无效的线路类型。

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.

当我尝试实施此SO帖子建议save/restore graph时,我仍然收到错误:google.protobuf.text_format.ParseError: 1:1 : Message type "tensorflow.GraphDef" has no field named

我显然错过了一些相当简单的东西,因为我想很多人正在做我想做的事情......我的Label_AllImages_v3.py代码只是改变了路径,然后将结果写入文件:

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

0 个答案:

没有答案