使用 tf.data 在 Tensorflow-2.0 中读取图像和掩模(用于分割问题)

Sil*_*fer 5 tensorflow tensorflow-datasets

我正在尝试通过以下链接读取分割问题(​​1 类)的图像数据集。我的主文件夹包含两个文件夹,即 (a) img(b) maskimg包含图像样本并mask包含相应的掩模。我的方法是,生成图像的路径,然后更改字符串路径(即 img->mask)。我修改了此处提供的代码,现在看起来如下:

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
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试使用以下命令查看样本的大小时:

for image, mask in labeled_ds.take(1):
  print("Image shape: ", image.numpy().shape)
  print("Mask shape: ", mask.numpy().shape)
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

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]

问题:关于如何从给定文件夹中读取图像和蒙版而不出现上述错误,有什么建议吗?

Sil*_*fer 2

我们可以使用它tf.regex.replace来重命名字符串。因此,使用:代替 python 字符串替换file_path_mask = tf.regex_replace(file_path, "img", "mask")。对于 TF 2.0,请使用tf.strings.regex_replace.