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

标签 tensorflow tensorflow-datasets

我正在尝试通过关注 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/

相关文章:

tensorflow-dataset-如何用tfds格式制作我们自己的数据集?

python - 将 tf.dataset 作为字典的键传递

python - 在 tensorflow 中使用迭代器生成特征和标签

tensorflow - 如何查找卡住模型的输入和输出节点

python - TensorFlow 连接/堆叠 N 个张量交错最后一个维度

python - 如果第一维等于 1,则使用 tf.cond 压缩第一维

python - 窗口多维 Tensorflow 数据集

python - Deep Dream : CPU works but not GPU, 加载原生TensorFlow运行时失败

python - ValueError : Dimensions must be equal, 但对于 'Conv2D' 是 1 和 3 (op : 'Conv2D' ) with input shapes: [1, 400,400,1], [1,3,3,1]

tensorflow - TensorFlow 数据集的函数 cache() 和 prefetch() 有什么作用?