DEV Community

Cover image for Basic step in image classification model - Loading Image Dataset (Part-1)
Modgil
Modgil

Posted on

Basic step in image classification model - Loading Image Dataset (Part-1)

It's expected that you know the problem and must have the dataset on which you want to work on.

In image classification/detection tasks the basic step is to load the data in your notebook.

It is very important to arrange the data in proper order before using it.
The image dataset must follow a particular hierarchy depending upon your problem.
image-data/
image-data/class-1
image-data/class-2
.
.
.
image-data/class-n

The dataset folder contains file having sorted images of different classes.

Now we will be loading the dataset
Using Keras

1. Using Keras

We'll use image_dataset_from_directory function to load dataset.

 image_size = (n, n)  #depending 
 #upon the model requirement
 batch_size = 32  

 train_ds = 
 tf.keras.preprocessing.
 image_dataset_from_directory(  
 Directory,
 validation_split=0.2,
 labels="inferred",
 subset="training",
 class_names=["Class1","Class2"],
 seed=123,
 image_size=image_size,
 batch_size=batch_size)   
Enter fullscreen mode Exit fullscreen mode

The function will automatically split the dataset into 2 parts, train and validation set depending upon the value provided in validation_split parameter.

val_ds = 
tf.keras.preprocessing.
image_dataset_from_directory(
Directory,
validation_split=0.2,
labels="inferred",
subset="validation",
class_names=['Class1','Class2'],
seed=123,
image_size=image_size,
batch_size=batch_size)
Enter fullscreen mode Exit fullscreen mode

Hence, this way we can load our image data and can use for further training process.
For more info about parameters used in this function refer to keras documentation .

In next article we will learn how to load image data using other techniques.

Top comments (0)