Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How knowing number of images in flow_from_directory

We can generate image dataset using ImageDataGenerator with flow_from_directory method.

train_datagen = ImageDataGenerator(
    rescale=1./255, #scale images from integers 0-255 to floats 0-1.
    shear_range=0.2,
    zoom_range=0.2, # zoom in or out in images
    horizontal_flip=True) #horizontal flip of images
train_set = train_datagen.flow_from_directory(..)

and that displays:

Found 200 images belonging to 2 classes

I would like to write a loop for to count the number of images on train_set

For image in train_set:
    count = count+1
print(count)

but this does'nt display anything!!

like image 608
Eliza Avatar asked Apr 30 '20 10:04

Eliza


People also ask

How many images are created in ImageDataGenerator generate?

There are a total of 1646 unique images in the dataset. Since these are not a lot of images to create a robust neural network, it will act as a great dataset to test the potential of the ImageDataGenerator class!

How does flow_from_directory work?

flow_from_directory Method This method is useful when the images are sorted and placed in there respective class/label folders. This method will identify classes automatically from the folder name.

What is Class_mode =' categorical?

Class modes: "categorical" : 2D output (aka. list of numbers of length N), [0, 0, 1, 0], which is a one-hot encoding (only one number is 1/ "hot") representing the donkey. This is for mutually exclusive labels.

What is flow_from_directory in keras?

The flow_from_directory() method takes a path of a directory and generates batches of augmented data. The directory structure is very important when you are using flow_from_directory() method. The flow_from_directory() assumes: The root directory contains at least two folders one for train and one for the test.


Video Answer


2 Answers

To access the number of samples of your dataset you first must know which type it is:

If you are using the ImageDataGenerator then:

type(train_ds)

will return tensorflow.python.keras.preprocessing.image.DirectoryIterator. In this case you can access the number of samples with:

train_ds.samples

However, if you are creating your creating your dataset using:

train_ds = tf.keras.preprocessing.image_dataset_from_directory( rescale = 1/255. , etc...)

then

type(train_ds)

will return tensorflow.python.data.ops.dataset_ops.BatchDataset which means that you can access the number of samples indirectly with

len(train_ds.file_paths)
like image 53
Anel Music Avatar answered Oct 24 '22 08:10

Anel Music


To get the no. of images, try with the below code.

train_set.samples 
like image 40
Biranchi Avatar answered Oct 24 '22 08:10

Biranchi