TensorFlow图像分割的文件夹结构?

huangapple go评论76阅读模式
英文:

Folder structure for TensorFlow image segmentation?

问题

我正在使用这个教程来使用TensorFlow进行图像分割。然而,我想要为此创建自己的数据。如果我想要与教程保持一致,我的文件夹结构应该是什么样的?目前,我的结构如下:

path/to/image
  train/
    image/
      1.png
      ...
    segmentation_mask/
      1.jpg
  test/
    image/
      6.png
      ...
    segmentation_mask/
      6.jpg
      ...

我正在尝试使用tf.data_folder.ImageFolder("path/to/image")来读取数据集,然后使用.as_dataset()将其转换为数据集。然而,这在程序的其余部分引发了错误。所以,我应该如何组织我的文件夹结构,以及如何正确加载数据集,以避免出现更多错误?

英文:

I am using this tutorial to do image segmentation with tensorflow. However, I want to make my own data for this. What should my folder structure be if I'm trying to stay consistent with the tutorial? Currently, I have the following:

path/to/image
  train/
    image/
      1.png
      ...
    segmentation_mask/
      1.jpg
  test/
    image/
      6.png
      ...
    segmentation_mask/
      6.jpg
      ...

I'm trying to read the dataset using tf.data_folder.ImageFolder("path/to/image") and then .as_dataset() to convert it to be a dataset. However, this raises errors throughout the rest of the program. So, how to I structure my folders and how do I load the dataset in properly so there would not be any more errors?

答案1

得分: 1

为了正确加载数据集并避免错误,您需要按照以下结构组织文件夹:

path/to/image/
  train/
    images/
      1.png
      2.png
      3.png
      ...
    masks/
      1.png
      2.png
      3.png
  test/
    images/
      6.png
      7.png
      8.png
    masks/
      6.png
      7.png
      8.png

确保图像文件和相应的分割遮罩文件具有相同的名称并放置在各自的文件夹中。例如,train/images 文件夹中的 1.png 应该有其相应的遮罩 1.png 放在 train/masks 文件夹中。

要使用 tf.data.Dataset.from_folder() 加载数据集,您可以按照以下方式操作:

import tensorflow as tf

# 定义训练和测试目录的路径
train_path = "path/to/image/train"
test_path = "path/to/image/test"

# 定义图像和遮罩的子目录
image_dir = "images"
mask_dir = "masks"

# 加载训练数据集
train_dataset = tf.data.Dataset.from_folder(
    train_path,
    image_size=(height, width),  # 设置所需的图像大小
    seed=123,  # 设置种子以便复现性(可选)
    labels="inferred",
    label_mode=None,  # 因为加载的是遮罩,所以没有标签
    image_folder=image_dir,
    mask_folder=mask_dir,
    interpolation="nearest"  # 设置调整大小的插值方法(可选)
)

# 加载测试数据集
test_dataset = tf.data.Dataset.from_folder(
    test_path,
    image_size=(height, width),  # 设置所需的图像大小
    seed=123,  # 设置种子以便复现性(可选)
    labels="inferred",
    label_mode=None,  # 因为加载的是遮罩,所以没有标签
    image_folder=image_dir,
    mask_folder=mask_dir,
    interpolation="nearest"  # 设置调整大小的插值方法(可选)
)

如果这对您有用,请告诉我!

英文:

To load your dataset properly and avoid errors, you need to structure your folders as follows:

path/to/image/
  train/
    images/
      1.png
      2.png
      3.png
      ...
    masks/
      1.png
      2.png
      3.png
  test/
    images/
      6.png
      7.png
      8.png
    masks/
      6.png
      7.png
      8.png

Make sure that your image files and corresponding segmentation mask files have the same names and are placed in their respective folders. For example, 1.png in the train/images folder should have its corresponding mask 1.png in the train/masks folder.

To load the dataset using tf.data.Dataset.from_folder(), you can do the following:

import tensorflow as tf

# Define the paths to the train and test directories
train_path = "path/to/image/train"
test_path = "path/to/image/test"

# Define the image and mask subdirectories
image_dir = "images"
mask_dir = "masks"

# Load the train dataset
train_dataset = tf.data.Dataset.from_folder(
    train_path,
    image_size=(height, width),  # Set the desired image size
    seed=123,  # Set a seed for reproducibility (optional)
    labels="inferred",
    label_mode=None,  # No labels as we are loading masks
    image_folder=image_dir,
    mask_folder=mask_dir,
    interpolation="nearest"  # Set the interpolation method for resizing (optional)
)

# Load the test dataset
test_dataset = tf.data.Dataset.from_folder(
    test_path,
    image_size=(height, width),  # Set the desired image size
    seed=123,  # Set a seed for reproducibility (optional)
    labels="inferred",
    label_mode=None,  # No labels as we are loading masks
    image_folder=image_dir,
    mask_folder=mask_dir,
    interpolation="nearest"  # Set the interpolation method for resizing (optional)
)

Let me know if this works for you!

huangapple
  • 本文由 发表于 2023年7月13日 21:26:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76679892.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定