使用TensorFlow-Datasets tfds从PyTorch文件夹结构中加载ImageNet。

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

Load ImageNet with TensorFlow-Datasets tfds from a PyTorch Folder Structure

问题

亲爱的ImageNet和TensorFlow专家,

最初,我已经下载了ImageNet数据集的tar文件ILSVRC2012_img_train.tarILSVRC2012_img_val.tar,并使用这个PyTorch脚本来提取文件。这导致了以下文件夹结构:

#  imagenet/train/
#  ├── n01440764
#  │   ├── n01440764_10026.JPEG
#  │   ├── n01440764_10027.JPEG
#  │   ├── ......
#  ├── ......
#  imagenet/val/
#  ├── n01440764
#  │   ├── ILSVRC2012_val_00000293.JPEG
#  │   ├── ILSVRC2012_val_00002138.JPEG
#  │   ├── ......
#  ├── ......

我如何将这个生成的文件夹结构加载到TensorFlow数据集 (tfds) 中? 这里有一个示例,但它假定从tar文件中提取(我现在不再有这些文件)。请帮忙:)

英文:

Dear ImageNet and TensorFlow specialists

Originally, I have downloaded the ImageNet dataset tar files ILSVRC2012_img_train.tar, ILSVRC2012_img_val.tar and used this PyTorch script to extract the files. This results in the folder structure

#  imagenet/train/
#  ├── n01440764
#  │   ├── n01440764_10026.JPEG
#  │   ├── n01440764_10027.JPEG
#  │   ├── ......
#  ├── ......
#  imagenet/val/
#  ├── n01440764
#  │   ├── ILSVRC2012_val_00000293.JPEG
#  │   ├── ILSVRC2012_val_00002138.JPEG
#  │   ├── ......
#  ├── ......

How can I load this resulting folder structure into a TensorFlow Dataset (tfds)? An example is shown here, but it assumes extraction from the tar files (which I no longer have available). Please help : )

答案1

得分: 2

不需要使用TensorFlow数据集(tfds)来从现有的文件夹结构加载ImageNet数据集,而可以利用 TensorFlow库中的 tf.data.Dataset

使用Keras API

import tensorflow as tf
import os

# 定义训练和验证数据集的路径
train_dir = 'path_to/imagenet/train'
val_dir = 'path_to/imagenet/val'

# 加载训练数据集
train_dataset = tf.keras.preprocessing.image_dataset_from_directory(
    train_dir,
    image_size=(224, 224),  # 定义所需的图像尺寸
    batch_size=32,  # 定义所需的批量大小
    label_mode='categorical',  # 用于多类分类问题
    shuffle=True,
    seed=42,
)

# 加载验证数据集
val_dataset = tf.keras.preprocessing.image_dataset_from_directory(
    val_dir,
    image_size=(224, 224),  # 定义所需的图像尺寸
    batch_size=32,  # 定义所需的批量大小
    label_mode='categorical',  # 用于多类分类问题
    shuffle=False,
    seed=42,
)

这将从您的文件夹结构中创建 TensorFlow 数据集。请将 'path_to/imagenet/train''path_to/imagenet/val' 替换为训练和验证数据集的实际路径。

函数 tf.keras.utils.image_dataset_from_directory 会自动从提供的目录路径中的子目录名称推断标签。标签将进行一热编码,适用于多类分类任务。有关详细信息,请参阅 George Novack 的文章 "使用 TensorFlow 构建一热编码层"。

tf.keras.preprocessing.image_dataset_from_directory 函数中的 label_mode='categorical' 参数指示 TensorFlow 对标签进行一热编码。这意味着标签会转换为二进制向量,其中 1 表示正确的类别,0 表示其他类别。这种一热编码格式适用于多类分类任务,因为在这些任务中,一个实例可以属于多个可能的类别之一(与“二进制”不同,只有两个可能的类别)。

ImageNet数据集的上下文中,有 1,000 个可能的类别,因此一热编码是表示类标签的合适选择。

该函数返回一个 tf.data.Dataset 对象,可以产生图像和标签的批次。图像以 float32 格式呈现,值在 0 到 1 之间,标签也以 float32 格式呈现。您可以使用这些数据集来训练您的模型。

英文:

You don't need to use TensorFlow Datasets (tfds) to load the ImageNet dataset from the existing folder structure, but you can utilize tf.data.Dataset from the TensorFlow library.

Using the Keras API:

import tensorflow as tf
import os

# Define paths to the train and validation datasets
train_dir = 'path_to/imagenet/train'
val_dir = 'path_to/imagenet/val'

# Load training dataset
train_dataset = tf.keras.preprocessing.image_dataset_from_directory(
    train_dir,
    image_size=(224, 224),  # Define the image size you want
    batch_size=32,  # Define the batch size you want
    label_mode='categorical',  # For multi-class classification problem
    shuffle=True,
    seed=42,
)

# Load validation dataset
val_dataset = tf.keras.preprocessing.image_dataset_from_directory(
    val_dir,
    image_size=(224, 224),  # Define the image size you want
    batch_size=32,  # Define the batch size you want
    label_mode='categorical',  # For multi-class classification problem
    shuffle=False,
    seed=42,
)

That will create TensorFlow datasets from your folder structure.
Do replace 'path_to/imagenet/train' and 'path_to/imagenet/val' with the actual paths to the training and validation datasets.

The function tf.keras.utils.image_dataset_from_directory automatically infers the labels from the subdirectory names in the provided directory path. The labels are one-hot encoded which suits multi-class classification tasks.
See "Building a One Hot Encoding Layer with TensorFlow" from George Novack for illustrating that concept.
The label_mode='categorical' argument in the tf.keras.preprocessing.image_dataset_from_directory function instructs TensorFlow to one-hot encode the labels. That means that the labels are converted into a binary vector where 1 stands for the correct class, and 0 stands for the rest.
That one-hot encoding format suits multi-class classification tasks because in these tasks, an instance can belong to one class out of multiple possible classes. (as opposed to "binary", when there are only two possible classes).
In the context of the ImageNet dataset, there are 1,000 possible classes, so one-hot encoding is a suitable choice for representing the class labels.

The function returns a tf.data.Dataset object that yields batches of images and labels. Images are in the format of float32 with values between 0 and 1, and the labels are also in float32 format.

You can then use these datasets to train your model.

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

发表评论

匿名网友

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

确定