我正在尝试通过关注 this 来读取分割问题(1 类)的图像数据集关联。我的主文件夹包含两个文件夹,即 (a) img
(b) mask
. img
包含图像样本和 mask
包含相应的掩码。我的方法是,生成图像的路径,然后更改字符串路径(即 img->mask)。我修改了提供的代码 here现在看起来像:
def process_path(file_path):
file_path_str = str(file_path)
file_path_mask = file_path_str.replace('img', 'mask')
# load the raw data from the file as a string
img = tf.io.read_file(file_path)
img = decode_img(img)
mask = tf.io.read_file(str(file_path_mask))
mask = decode_mask(mask)
return img, mask
但是,当我尝试使用以下方法查看样本大小时:
for image, mask in labeled_ds.take(1):
print("Image shape: ", image.numpy().shape)
print("Mask shape: ", mask.numpy().shape)
我收到以下错误:
InvalidArgumentError: NewRandomAccessFile failed to Create/Open: Tensor("arg0:0", shape=(), dtype=string) : The filename, directory name, or volume label syntax is incorrect.
; Unknown error
[[{{node ReadFile_1}}]] [Op:IteratorGetNextSync]
问题:关于如何从给定文件夹中读取图像和蒙版而没有上述错误的任何建议?
最佳答案
我们可以使用 tf.regex.replace
重命名字符串。因此,代替 python 字符串替换,使用:file_path_mask = tf.regex_replace(file_path, "img", "mask")
.对于 TF 2.0,使用 tf.strings.regex_replace
.
关于tensorflow - 使用 tf.data 在 Tensorflow-2.0 中读取图像和掩码(用于分割问题),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58185222/