我正在尝试使用tensorflow队列直接读取图像,我使用tf.train.slice_input_producer
创建队列,使用tf.train.shuffle_batch
来获取批处理数据。我没有将图像转换为TFRecordfiles,这是我的代码:
import tensorflow as tf
import os
import tensorflow as tf
import os
VGG_MEAN = tf.constant([123.68, 116.779, 103.939], dtype=tf.float32)
batch_size = 3
training_fire_image_path = 'train/fire/'
training_non_fire_image_path = 'train/non_fire/'
training_labels = []
training_filenames = os.listdir(training_fire_image_path)
training_filenames[len(training_filenames):len(training_filenames)] = os.listdir(training_non_fire_image_path)
for i in range(len(training_filenames)):
if i < 60:
training_labels.append(1)
else:
training_labels.append(0)
images_tensor = tf.convert_to_tensor(training_filenames, dtype=tf.string)
labels_tensor = tf.convert_to_tensor(training_labels, dtype=tf.uint8)
file_queue = tf.train.slice_input_producer([images_tensor, labels_tensor], shuffle=True)
image_content = tf.read_file(file_queue[0])
image_data = tf.image.decode_jpeg(image_content, channels=3)
image = tf.image.resize_images(image_data, [224, 224])
img_centered = tf.subtract(image, VGG_MEAN)
img_bgr = img_centered[:, :, ::-1]
labels = tf.cast(file_queue[1], tf.uint8)
files, labels = tf.train.shuffle_batch([img_bgr, labels],
batch_size=3,
capacity=2000,
min_after_dequeue=1000,
shapes=[(224, 224, 3), ()])
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
tf.train.start_queue_runners(sess=sess)
for _ in range(3):
image_batch, label_batch = sess.run([files, labels])
print(image_batch, label_batch)
但是当我运行该文件时,出现了一个错误:
ERROR:tensorflow:Exception in QueueRunner: NewRandomAccessFile failed to Create/Open: Narita_International_Airport_Terminal_2_counters_(1).JPG : ϵͳ\udcd5Ҳ\udcbb\udcb5\udcbdָ\udcb6\udca8\udcb5\udcc4\udcceļ\udcfe\udca1\udca3
[[Node: ReadFile = ReadFile[_device="/job:localhost/replica:0/task:0/cpu:0"](input_producer/Gather)]]
Exception in thread Thread-2:
Traceback (most recent call last):
File "C:\Users\vipyo\Python36\Lib\threading.py", line 916, in _bootstrap_inner
self.run()
File "C:\Users\vipyo\Python36\Lib\threading.py", line 864, in run
self._target(*self._args, **self._kwargs)
File "C:\python36\lib\site-packages\tensorflow\python\training\queue_runner_impl.py", line 238, in _run
enqueue_callable()
File "C:\python36\lib\site-packages\tensorflow\python\client\session.py", line 1235, in _single_operation_run
target_list_as_strings, status, None)
File "C:\Users\vipyo\Python36\Lib\contextlib.py", line 88, in __exit__
next(self.gen)
File "C:\python36\lib\site-packages\tensorflow\python\framework\errors_impl.py", line 466, in raise_exception_on_not_ok_status
pywrap_tensorflow.TF_GetCode(status))
tensorflow.python.framework.errors_impl.NotFoundError: NewRandomAccessFile failed to Create/Open: Narita_International_Airport_Terminal_2_counters_(1).JPG : ϵͳ\udcd5Ҳ\udcbb\udcb5\udcbdָ\udcb6\udca8\udcb5\udcc4\udcceļ\udcfe\udca1\udca3
[[Node: ReadFile = ReadFile[_device="/job:localhost/replica:0/task:0/cpu:0"](input_producer/Gather)]]
我不确定读者是否还没有阅读数据,请帮助我,谢谢!
答案 0 :(得分:0)
我最近遇到了类似的错误,结果发现它是模型目录中的损坏文件。
我的模型目录为..\Temp\tfhub_modules\
,最终我删除了该目录中的所有内容。这迫使TensorFlow从头开始重新创建所有模型文件,从而解决了问题。
答案 1 :(得分:0)
为此,您必须创建一个单独的文件夹 Image,然后将所有图像及其 将 xml 文件放入其中并运行以下命令:
python tfrecord_genrat.py --csv_input=D:\Deep_learning_Ann\Face-pimples\test\data\test_pim.csv --output_path=D:/Deep_learning_Ann/Face-pimples/test/data/test.record --image_dir=D:\Deep_learning_Ann\Face-pimples\test\image_test