InvalidArgumentError: StringToNumberOp could not correctly convert string
InvalidArgumentError: StringToNumberOp could not correctly convert string
我正在尝试从以下格式的文件路径中提取标签:
'/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, labels
ds = 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 的子字符串' 到一个数字。
我正在尝试从以下格式的文件路径中提取标签:
'/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, labels
ds = 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 的子字符串' 到一个数字。