Call Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts Service
Learning keras by building dogs-vs-cats image classifier
1. Learning _______
by building Dogs-vs-Cats image classifier
Jian Wu
Reading Group
AI & Machine Learning Lab
Samsung SDSRA
2. Keras Overview
● Keras is a high-level neural network API, written in Python
● Built on top of either Theano or TensorFlow
● Most powerful & easy to use for developing and evaluating deep neural network
models
Ref: https://keras.io
3. Keras is a core package in TensorFlow 1.4
Ref: https://developers.googleblog.com/2017/11/announcing-tensorflow-r14.html
4. Deep Learning and Convolutional Neural Network
for Image Classification
Ref: https://deeplearning.ai
5. Convolutional Neural Network in a Nutshell
1. Convolution Filter/Operator
2. Max Pooling or Downsampling
The purpose of the convolution filter is to
extract features from the input image, it
preserves the spatial relationship between
pixels by learning image features using small
squares of input data
Max pooling or downsampling is to reduces
the dimensionality of each feature map but
retains the most important information
Ref: https://www.coursera.org/learn/convolutional-neural-networks/home/welcome
6. Convolutional Neural Network to classify dogs vs cats
Kaggle Dogs vs. Cats competition with labeled data set
Using CNN to classify dogs vs cats
This training data set contains 12500
labeled dog images and 12500 labeled cat
images, total 25000 images
Ref: https://www.kaggle.com/c/dogs-vs-cats
7. Get Started with Keras (tf.keras)
Keras is a model-level deep learning framework, which provides high-level API to construct neural
network model through layers, there are two ways to build a nn model in Keras:
The simplest type of model is the Sequential model, a linear stack of layers:
model = Sequential()
model.add(Dense(32, input_dim=784))
model.add(Activation('relu'))
model.add(Dense(10))
model.add(Activation('softmax'))
The Keras functional API is used to define complex models, such as model(s) with shared layers and
multi-output models:
inputs = Input(shape=(784,))
x = Dense(64, activation='relu')(inputs)
x = Dense(64, activation='relu')(x)
predictions = Dense(10, activation='softmax')(x)
model = Model(inputs=inputs, outputs=predictions)
8. Processing image data with ImageDataGenerator
For image classification task, it is best practice to augment your training image data, producing more
image data and preventing overfitting. Keras ImageDataGenerator is a simple and powerful tool to
process and augment image data, supporting :
-- batch and iterative processing image data just-in-time
-- zoom image, rescale/normalize image, shear image, flip image horizontally and vertically, … ...
Processing/Augmenting image data for training and validation:
train_datagen = ImageDataGenerator(rescale=(1.0/255), shear_range=0.2, zoom_range=0.2, horizontal_flip=True)
train_generator = train_datagen.flow_from_directory(train_data_dir,
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode='binary')
validation_datagen = ImageDataGenerator(rescale=(1.0/255))
validation_generator = validation_datagen.flow_from_directory(validation_data_dir,
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode='binary')
9. Building CNN model with Functional model API
With TensorFlow 1.4, it is recommended to use Keras Functional model API to build neural network
model, for dogs-vs-cats image classification, we are building a CNN model with three convolution
layers with max pooling plus two dense layers with dropout:
def build_cnn_model(input_shape):
input_image = Input(shape=input_shape)
conv_1 = Conv2D(32, (3, 3), activation='relu')(input_image)
pool_1 = MaxPooling2D((2, 2))(conv_1)
conv_2 = Conv2D(32, (3, 3), activation='relu')(pool_1)
pool_2 = MaxPooling2D((2, 2))(conv_2)
conv_3 = Conv2D(64, (3, 3), activation='relu')(pool_2)
pool_3 = MaxPooling2D((2, 2))(conv_3)
flatten = Flatten()(pool_3)
dense = Dense(64, activation='relu')(flatten)
dropout = Dropout(0.5)(dense)
prediction = Dense(1, activation='sigmoid')(dropout)
cnn_model = Model(inputs=input_image, outputs=prediction)
cnn_model.compile(loss='binary_crossentropy',
optimizer='rmsprop',
metrics=['accuracy'])
return cnn_model
MaxPooling2D
Dense
Convolution2D
Convolution2D
MaxPooling2D
Convolution2D
MaxPooling2D
Flatten
Dense
10. Training CNN model with labeled dogs-vs-cats image set
With configured ImageDataGenerator and designed CNN Model, we can start training using Kaggle
labeled dogs-vs-cats image set and monitor the training with TensorBoard:
train_steps = nb_train_samples//batch_size
validation_steps =
nb_validation_samples//batch_size
tensorboard = TensorBoard(log_dir="summary_logs",
write_graph=True)
cnn_model = build_cnn_model(input_shape)
cnn_model.fit_generator(
train_generator,
steps_per_epoch=train_steps,
epochs=epochs,
validation_data=validation_generator,
validation_steps=validation_steps,
callbacks=[tensorboard])
11. Save and reload trained CNN model to make predictions
Keras provides API to make prediction with trained model, it also provides API to save trained
weights of neural network model to HD5 file, the HD5 weight file can be used in production to reload
pre-trained model by reading trained weights from HD5 file and make predictions :
# save trained model weights
cnn_model.save(model_file)
# reload trained model at production site
cnn_model.load_weights(model_file)
# make predictions with pre-trained model
image_data_generator = ImageDataGenerator(rescale=(1.0 / 255))
test_generator = image_data_generator.flow_from_directory(
test_data_dir,
target_size=(img_width, img_height),
batch_size=5,
class_mode=None, # only data, no labels
shuffle=False
)
predictions = cnn_model.predict_generator(test_generator, steps=2)