1

I have a folder that is called ‘train’, which is divided into 8 sub folders with each sub folder contains the data ( images )of one class ( and hence my dataset is a multi class one with 8 classes) . Now I have the following questions :

  1. I need to do standardization feature-wise and therefore I need to use datagen.fit( xtrain ) and I don’t know how to create x _train with all of my data, which are separated in different folders as I haven’t work with images before with python . Note I have enough memory to store all the data at once.

  2. Since I have 8 classes do the stats be calculated on each class separately , if so how could I do that using the .fit( x_train) ?

AAAA
  • 11
  • 1

1 Answers1

0

Please follow below sample code snippets to resolve your query.(I have used 20 images dataset of two classes)

import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator

PATH ="/train/"   #path to your train directory which contains 8 subfolders

train_datagen = ImageDataGenerator(rescale=1./255, shear_range=0.2, zoom_range=0.2, horizontal_flip=True)
train_generator = train_datagen.flow_from_directory(PATH, target_size=(50, 50), batch_size=5, class_mode='binary')  #class_mode='categorical'

Output:

Found 20 images belonging to 2 classes.

After defining and compiling the model, fit this dataset to the model.

num_classes = 2   # you can define num_classes = 8

model = tf.keras.Sequential([
  tf.keras.layers.Rescaling(1./255),
  tf.keras.layers.Conv2D(32, 3, activation='relu'),
  tf.keras.layers.MaxPooling2D(),
  tf.keras.layers.Conv2D(32, 3, activation='relu'),
  tf.keras.layers.MaxPooling2D(),
  tf.keras.layers.Conv2D(32, 3, activation='relu'),
  tf.keras.layers.MaxPooling2D(),
  tf.keras.layers.Flatten(),
  tf.keras.layers.Dense(128, activation='relu'),
  tf.keras.layers.Dense(num_classes)
])

model.compile(optimizer='adam',loss=tf.losses.SparseCategoricalCrossentropy(from_logits=True),metrics=['accuracy'])

model.fit(train_generator,steps_per_epoch=2,epochs=5)  # you can define steps_per_epoch=(number of images/batch_size),epochs

Please refer this link for more detials.