python - 使用 Dataset API 创建图像数据集时出现“TypeError: expected bytes, str found”
问题描述
我想使用 Dataset API 从我的图像中创建一个 TensorFlow 的数据集。这些图像以复杂的层次结构组织,但最后总是有两个目录“False”和“Genuine”。我写了这段代码
import tensorflow as tf
from tensorflow.data import Dataset
import os
def enumerate_all_files(rootdir):
for subdir, dir, files in os.walk(rootdir):
for file in files:
# return path to the file and its label
# label is simply a 1 or 0 depending on whether an image is in the "Genuine" folder or not
yield os.path.join(subdir, file), int(subdir.split(os.path.sep)[-1] == "Genuine")
def input_parser(img_path, label):
# convert the label to one-hot encoding
one_hot = tf.one_hot(label, 2)
# read the img from file
img_file = tf.read_file(img_path)
img_decoded = tf.image.decode_png(img_file, channels=3)
return img_decoded, one_hot
def get_dataset():
generator = lambda: enumerate_all_files("/tmp/images/training/")
dataset = Dataset.from_generator(generator, (tf.string, tf.int32)).shuffle(1000).batch(100)
dataset = dataset.map(input_parser)
return dataset
但是,当我在终端中运行它时
tf.enable_eager_execution()
# all the code above
d = get_dataset()
for f in d.make_one_shot_iterator():
print(f)
它因错误而崩溃
W tensorflow/core/framework/op_kernel.cc:1306] Unknown: SystemError: <weakref at 0x7ff8232f0620; to 'function' at 0x7ff8233c9048 (generator_py_func)> returned a result with an error set
TypeError: expected bytes, str found
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "lcnn.py", line 29, in <module>
for f in d.make_one_shot_iterator():
File "/opt/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow/python/data/ops/iterator_ops.py", line 487, in __next__
return self.next()
File "/opt/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow/python/data/ops/iterator_ops.py", line 518, in next
return self._next_internal()
File "/opt/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow/python/data/ops/iterator_ops.py", line 508, in _next_internal
output_shapes=self._flat_output_shapes)
File "/opt/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow/python/ops/gen_dataset_ops.py", line 1848, in iterator_get_next_sync
"output_types", output_types, "output_shapes", output_shapes)
SystemError: <built-in function TFE_Py_FastPathExecute> returned a result with an error set
我在这里做错了什么?
编辑
我尝试在不调用的情况下运行代码map
,shuffle
并且batch
注释掉方法,input_parser
但错误仍然出现。
编辑 2
我改为Dataset.from_generator
查看Dataset.from_tensor_slices
打开图片的代码是否有效。所以更改后的代码看起来像
def input_parser(img_path):
# convert the label to one-hot encoding
# one_hot = tf.one_hot(label, 2)
# read the img from file
img_file = tf.read_file(img_path)
img_decoded = tf.image.decode_png(img_file, channels=3)
return img_decoded
def get_dataset():
dataset = Dataset.from_tensor_slices(["/tmp/images/training/1000010.png"]).map(input_parser).shuffle(1000).batch(100)
return dataset
虽然这很好用
解决方案
我不确定错误消息,但我尝试了您的代码,如果我在批处理和随机操作之前进行输入解析器映射,它对我有用:
def get_dataset():
generator = lambda: enumerate_all_files("/tmp/images/training/")
dataset = Dataset.from_generator(generator, (tf.string,tf.int32)).map(input_parser)
dataset = dataset.shuffle(1000).batch(100)
return dataset
推荐阅读
- ios - 区分 iOS 上的 Lightning 连接器和 3.5 毫米耳机插孔
- javascript - 我的全日历调度程序视图资源忘记了一个资源
- git - 恢复已删除的分支 Git
- android - 在 Android 移动设备上查找 adb 加速度计事件
- angular - 用 *ngIf else 设置标题
- java - 使用 JNI 从 Java 调用 C# 方法
- c# - 我想使用服务阻止机器进入睡眠/休眠模式
- java - Mockito UnfinishedStubbingException,但存在间谍
- c - Matlab 最终因 mex 回调而崩溃
- java - Android 字体 createFromFile