我正在尝试从类似以下格式的文件路径中提取标签:
'/content/UTKFace/26_0_3_20170119181310597.jpg.chip.jpg'
文件名中的标签是26、0和3
首先,我创建了一个列表数据集:
data_dir = '/content/UTKFace'data_dir = pathlib.Path(data_dir)list_ds = tf.data.Dataset.list_files(str(data_dir/'*'))
然后,我定义了一个函数来读取图像并获取标签,并在list_ds上使用.map()方法
def decode(filename): bits = tf.io.read_file(filename) image = tf.io.decode_jpeg(bits, channels=3) image = tf.image.resize(image, [80, 80]) image = (image - 127.5) / 127.5 parts1 = tf.strings.split(filename, sep='/')[-1] parts2 = tf.strings.split(parts1, sep='_')[0:3] labels = tf.strings.to_number(parts2, tf.int64) return image, labelsds = list_ds.map(decode)
当我打印某个标签作为健全性检查时,我得到了这样的结果(1):
for i, labels in ds.take(1): print(labels)tf.Tensor([1 0 2], shape=(3,), dtype=int64)
但是,当我在ds上应用.batch()方法后,然后尝试打印数据集中的所有标签时,大多数标签都被打印出来了,但随后出现了这个错误:
---------------------------------------------------------------------------InvalidArgumentError Traceback (most recent call last)<ipython-input-254-3b19dc87cdce> in <module>()----> 1 ss = tf.strings.to_number(spl, tf.int64)4 frames/usr/local/lib/python3.6/dist-packages/tensorflow/python/util/dispatch.py in wrapper(*args, **kwargs) 199 """Call target, and fall back on dispatchers if there is a TypeError.""" 200 try:--> 201 return target(*args, **kwargs) 202 except (TypeError, ValueError): 203 # Note: convert_to_eager_tensor currently raises a ValueError, not a/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/string_ops.py in string_to_number(input, out_type, name) 477 A `Tensor` of type `out_type`. 478 """--> 479 return gen_parsing_ops.string_to_number(input, out_type, name) 480 481 /usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/gen_parsing_ops.py in string_to_number(string_tensor, out_type, name) 2311 return _result 2312 except _core._NotOkStatusException as e:-> 2313 _ops.raise_from_not_ok_status(e, name) 2314 except _core._FallbackException: 2315 pass/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py in raise_from_not_ok_status(e, name) 6860 message = e.message + (" name: " + name if name is not None else "") 6861 # pylint: disable=protected-access-> 6862 six.raise_from(core._status_to_exception(e.code, message), None) 6863 # pylint: enable=protected-access 6864 /usr/local/lib/python3.6/dist-packages/six.py in raise_from(value, from_value)InvalidArgumentError: StringToNumberOp could not correctly convert string: [Op:StringToNumber]
有什么想法可能导致这个错误吗?
关于(1),我期待输出的张量是tf.Tensor([1, 0, 2], shape=(3,), dtype=int64)
而不是tf.Tensor([1 0 2], shape=(3,), dtype=int64)
。这是什么类型的张量?
回答:
原来,错误是由一些文件引起的,这些文件的文件名中缺少第三个标签,而tf.strings.to_number()方法试图将类似’20170119181310597.jpg.chip.jpg’的子字符串转换为数字。