# How to load a dataset with a specific structure in tfds library?

I have a dataset that it's classes arranged in the following way:

/dataset/train/images/class1/

/dataset/train/images/class2/
.
.
.
/dataset/train/images/classN/


Does anyone have any idea how to store data in a train_ds variable with the help of TFDS library?

This is a common folder structure for Image Classification, so common many libraries have a dataset class for it (torchvision, fastai, tfds), usually called ImageFolder.

In the case of TFDS this is implemented in the tfds.folder_dataset.ImageFolder:

tfds.folder_dataset.ImageFolder(
root_dir: str,
*,
shape: Optional[type_utils.Shape] = None,
dtype: Optional[tf.DType] = None
)


  split_name/  # Ex: 'train'
label1/  # Ex: 'airplane' or '0015'
xxx.png
xxy.png
xxz.png
label2/
xxx.png
xxy.png
xxz.png
split_name/  # Ex: 'test'


You could just instantiate it like this:

train_ds = tfds.folder_dataset.ImageFolder(
root_dir = "/dataset/", # Note that this is a absolute path, you should use "./dataset/" or "dataset/" or "<current_working_dir_full_path>/dataset/" if that is the case.
)

# If you want a tensorflow.data.Dataset
train_ds = train_ds.as_dataset(**args)


Suported args for as_dataset method are:

(