Working with Complex Image data for CNNs

Source: Cognitiveclass.ai

Reading the Data

The directory structure
from tensorflow.keras.preprocessing.image import ImageDataGenerator
train_image_generator = ImageDataGenerator(rescale=1./255)train_data_gen =  train_iamge_generator.flow_from_directory(
batch_size=batch_size,
directory=train_dir,
shuffle=True,
target_size=(IMG_HEIGHT,IMG_WIDTH)
class_mode='binary')
validation_image_generator = ImageDataGenerator(rescale=1./255)
val_data_gen = validation_imadata_generator.flow_from_directory(
batch_size=batch_size,
directory= validation_dir,
shuffle=True,
target_size=(IMG_HEIGHT,IMG_WIDTH)
class_mode='binary')

Training the model

model = Sequential([
Conv2D(16, (3,3), padding='same', activation='relu',
input_shape=(IMG_HEIGHT, IMG_WIDTH ,3)),
MaxPooling2D(2,2),
Conv2D(32, (3,3), padding='same', activation='relu'),
MaxPooling2D(2,2),
Conv2D(64, (3,3), padding='same', activation='relu'),
MaxPooling2D(2,2),
Flatten(),
Dense(512, activation='relu'),
Dense(1, activation='sigmoid')])
model.compile(optimizer='adam',
loss='binary_crossentropy',
metrics=['accuracy'])
history = model.fit_generator(
train_data_gen,
steps_per_epoch=total_train//batch_size,
epochs=epochs,
validation_data=val_data_gen,
validation_steps=total_val//batch_size
)

Trying out for yourself

About Me

High School,Ted-X,Ted-Ed Speaker|Mentor,@tfugmumbai|@Microsoft Student Ambassador|International Speaker

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store