创建自定义初始时的tensorflow.python.framework.errors_impl.NotFoundError

Har*_*aki 10 python machine-learning tensorflow

我使用以下代码使用tensorflow创建自定义初始化.

import tensorflow as tf
import sys

interesting_class = sys.argv[1:]
print("Interesting class: ", interesting_class)

# Read in the image_data

from os import listdir
from shutil import copyfile
from os.path import isfile, join
varPath = 'toScan/'
destDir = "scanned/"
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("/tf_files/retrained_labels.txt")]

# Unpersists graph from file
with tf.gfile.FastGFile("/tf_files/retrained_graph.pb", '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') 
    file_count = len(imgFiles)
    i = 0

    for imageFile in imgFiles:
        print("File ", i, " of ",  file_count)
        i = i+1
        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(interesting_class, label_lines[firstElt])
        if interesting_class == label_lines[firstElt]:
            print(newFileName)
            copyfile(varPath+"/"+imageFile, destDir+"/"+newFileName)

        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))
Run Code Online (Sandbox Code Playgroud)

我在执行此操作时收到以下错误

('有趣的类:',[])Traceback(最近一次调用最后一次):文件"/Users/Downloads/imagenet_train-master/label_dir.py",第22行,在tf.gfile.GFile中("/ tf_files/retrained_labels .txt")]文件"/Users/tensorflow/lib/python2.7/site-packages/tensorflow/python/lib/io/file_io.py",第156行,在下一个retval = self.readline()文件中"/ Users/tensorflow/lib/python2.7/site-packages/tensorflow/python/lib/io/file_io.py",第123行,在readline self._preread_check()文件"/Users/tensorflow/lib/python2.7/ site-packages/tensorflow/python/lib/io/file_io.py",第73行,在_preread_check compat.as_bytes(self.name ),1024*512,status)文件"/System/Library/Frameworks/Python.framework/版本/ 2.7/lib/python2.7/contextlib.py",第24行,在__exit self.gen.next()文件"/Users/tensorflow/lib/python2.7/site-packages/tensorflow/python/framework/ errors_impl.py",第466行,在raise_exception_on_not_ok_status pywrap_tensorflow.TF_GetCode(status)中)tensorflow.python.framework.errors_impl.NotFoundError:/ tf_files/retra ined_labels.txt

为什么我收到此错误?

以下是我的文件夹结构:

tensorflow_try
|- new_pics
|  |- class1
|  |- class2
|  |- ...
|- toScan
|- scanned
Run Code Online (Sandbox Code Playgroud)

小智 8

问题来自这条线:

label_lines = [line.rstrip() for line 
                   in tf.gfile.GFile("/tf_files/retrained_labels.txt")]
Run Code Online (Sandbox Code Playgroud)

请检查:

  1. tf_files文件系统根目录中是否存在文件夹- 您可以运行ls /tf_files以检查此项;
  2. (如果第1项没问题),如果您有读/写权限/tf_files/retrained_labels.txt.