Tensorflow无法找到文件

时间:2018-03-27 19:12:58

标签: tensorflow

因此,当我尝试运行我的代码时,它一直说它无法找到图像,但它实际上就在那里。我尝试了很多东西,但没有任何作用。有人可以帮帮我吗?这是代码试图从nist数据集读取图像,然后我试图训练模型。这是我的代码和错误代码:

import os
import cv2
import tensorflow as tf

upper_level_dirs = open("/Users/cam/reader/top_level_dirs")
upper_level_dirs = upper_level_dirs.read().split()

print(upper_level_dirs)

file_names = []
labels = []

for folder in upper_level_dirs:
    for filename in os.listdir("./train/" + folder + "/"):
        file_names.append("./train/" + folder + "/" + filename)
        labels.append(folder)


# Use a custom OpenCV function to read the image, instead of the standard
# TensorFlow `tf.read_file()` operation.
def _read_py_function(file_name, label):
  image_string = tf.read_file(filename)
  image_decoded = tf.image.decode_image(image_string, channels=3)
  image_resized = tf.image.resize_image_with_crop_or_pad(image_decoded, 28, 28)
  return image_resized, label
  # image_decoded = cv2.imread(file_name.decode(), cv2.IMREAD_GRAYSCALE)
  # return image_decoded, label




dataset = tf.data.Dataset.from_tensor_slices((file_names, labels))

dataset = dataset.map(_read_py_function)

sess = tf.InteractiveSession()

dataset = dataset.batch(32)
iterator = dataset.make_initializable_iterator()
next_element = iterator.get_next()

for _ in range(100):
  sess.run(iterator.initializer)
  while True:
    try:
      sess.run(next_element)
    except tf.errors.OutOfRangeError:
      break

错误:

2018-03-27 12:59:58.001455: I tensorflow/core/platform/cpu_feature_guard.cc:140] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
2018-03-27 13:00:01.574038: W tensorflow/core/framework/op_kernel.cc:1278] OP_REQUIRES failed at whole_file_read_ops.cc:114 : Not found: train_7a_02017.png; No such file or directory
2018-03-27 13:00:01.576585: W tensorflow/core/framework/op_kernel.cc:1278] OP_REQUIRES failed at whole_file_read_ops.cc:114 : Not found: train_7a_02017.png; No such file or directory
2018-03-27 13:00:01.578373: W tensorflow/core/framework/op_kernel.cc:1278] OP_REQUIRES failed at whole_file_read_ops.cc:114 : Not found: train_7a_02017.png; No such file or directory
Traceback (most recent call last):
  File "/anaconda3/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1330, in _do_call
    return fn(*args)
  File "/anaconda3/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1315, in _run_fn
    options, feed_dict, fetch_list, target_list, run_metadata)
  File "/anaconda3/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1423, in _call_tf_sessionrun
    status, run_metadata)
  File "/anaconda3/lib/python3.5/site-packages/tensorflow/python/framework/errors_impl.py", line 516, in __exit__
    c_api.TF_GetCode(self.status.status))
tensorflow.python.framework.errors_impl.NotFoundError: train_7a_02017.png; No such file or directory
     [[Node: ReadFile = ReadFile[](ReadFile/filename)]]
     [[Node: IteratorGetNext = IteratorGetNext[output_shapes=[[?,28,28,?], [?]], output_types=[DT_UINT8, DT_STRING], _device="/job:localhost/replica:0/task:0/device:CPU:0"](Iterator)]]

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "split2.py", line 46, in <module>
    sess.run(next_element)
  File "/anaconda3/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 908, in run
    run_metadata_ptr)
  File "/anaconda3/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1143, in _run
    feed_dict_tensor, options, run_metadata)
  File "/anaconda3/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1324, in _do_run
    run_metadata)
  File "/anaconda3/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1343, in _do_call
    raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.NotFoundError: train_7a_02017.png; No such file or directory
     [[Node: ReadFile = ReadFile[](ReadFile/filename)]]
     [[Node: IteratorGetNext = IteratorGetNext[output_shapes=[[?,28,28,?], [?]], output_types=[DT_UINT8, DT_STRING], _device="/job:localhost/replica:0/task:0/device:CPU:0"](Iterator)]]

1 个答案:

答案 0 :(得分:0)

我也发现该错误,我的解决方法是:   也许您是通过Windows创建路径,并且当使用notepad ++查看txt时,路径以'CR'结尾,而linux无法读取。  您可以用记事本++将“ CR”替换为“”。然后就可以了。