11

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!!

Eliza
  • 584
  • 4
  • 14

3 Answers3

23

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

train_set.samples 
Biranchi
  • 16,120
  • 23
  • 124
  • 161
4

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)
Dharman
  • 30,962
  • 25
  • 85
  • 135
Joe Dawson
  • 95
  • 7
0

__iter__ will be your solution.

first you try to predict type of train_set .if you do not know.

print(type(train_set))
#then you find **keras.preprocessing.image.DirectoryIterator**

Now you want to apply some function or modification on this type data follow this linkhere

Welcome_back
  • 1,245
  • 12
  • 18