"No one is harder on a talented person than the person themselves" - Linda Wilkinson ; "Trust your guts and don't follow the herd" ; "Validate direction not destination" ;

February 25, 2020

Day #327 - Keras basic experiments

Revising basics again. Example code of MNIST in Sequential vs Functional vs Multi-Inputs based approach. Already we have posted a few best practices/guidelines notes. These are few more handly experiments.

#https://github.com/keras-team/keras/blob/master/examples/mnist_cnn.py
from __future__ import print_function
import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten, Input, concatenate
from keras.layers import Conv2D, MaxPooling2D
from keras.models import Model
from keras import backend as K
batch_size = 128
num_classes = 10
epochs = 3
# input image dimensions
img_rows, img_cols = 28, 28
def load_data():
# the data, split between train and test sets
(x_train, y_train), (x_test, y_test) = mnist.load_data()
if K.image_data_format() == 'channels_first':
x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols)
x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols)
input_shape = (1, img_rows, img_cols)
else:
x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
input_shape = (img_rows, img_cols, 1)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
print('x_train shape:', x_train.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')
# convert class vectors to binary class matrices
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)
return input_shape, x_train, x_test, y_train, y_test
input_shape, x_train, x_test, y_train, y_test = load_data()
def model_definition_sequence_model(input_shape,num_classes):
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3),
activation='relu',
input_shape=input_shape))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))
model.compile(loss=keras.losses.categorical_crossentropy,
optimizer=keras.optimizers.Adadelta(),
metrics=['accuracy'])
return model
#base code
#https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras/blob/master/chapter2-deep-networks/cnn-functional-2.1.1.py
def model_definition_functional_model(input_shape,num_classes):
kernel_size = 3
filters = 64
dropout = 0.3
inputs = Input(shape=input_shape)
y = Conv2D(filters=filters,
kernel_size=kernel_size,
activation='relu')(inputs)
y = MaxPooling2D()(y)
y = Conv2D(filters=filters,
kernel_size=kernel_size,
activation='relu')(y)
y = MaxPooling2D()(y)
y = Conv2D(filters=filters,
kernel_size=kernel_size,
activation='relu')(y)
# image to vector before connecting to dense layer
y = Flatten()(y)
# dropout regularization
y = Dropout(dropout)(y)
outputs = Dense(num_classes, activation='softmax')(y)
# build the model by supplying inputs/outputs
model = Model(inputs=inputs, outputs=outputs)
# network model in text
model.summary()
model.compile(loss=keras.losses.categorical_crossentropy,
optimizer=keras.optimizers.Adadelta(),
metrics=['accuracy'])
return model
#base code
#https://github.com/PacktPublishing/Advanced-Deep-Learning-with-Keras/blob/master/chapter2-deep-networks/cnn-y-network-2.1.2.py
def branch_definition_functional_model(input_shape,num_classes):
kernel_size = 3
filters = 64
dropout = 0.3
left_inputs = Input(shape=input_shape)
y1 = Conv2D(filters=filters,
kernel_size=kernel_size,
activation='relu')(left_inputs)
y1 = MaxPooling2D()(y1)
right_inputs = Input(shape=input_shape)
y2 = Conv2D(filters=filters,
kernel_size=kernel_size,
activation='relu')(right_inputs)
y2 = MaxPooling2D()(y2)
y = concatenate([y1,y2])
y = Conv2D(filters=filters,
kernel_size=kernel_size,
activation='relu')(y)
y = MaxPooling2D()(y)
y = Conv2D(filters=filters,
kernel_size=kernel_size,
activation='relu')(y)
# image to vector before connecting to dense layer
y = Flatten()(y)
# dropout regularization
y = Dropout(dropout)(y)
outputs = Dense(num_classes, activation='softmax')(y)
# build the model by supplying inputs/outputs
model = Model(inputs=[left_inputs,right_inputs], outputs=outputs)
# network model in text
model.summary()
model.compile(loss=keras.losses.categorical_crossentropy,
optimizer=keras.optimizers.Adadelta(),
metrics=['accuracy'])
return model
def seq_approach(input_shape,num_classes):
model = model_definition_sequence_model(input_shape,num_classes)
model.fit(x_train, y_train,
batch_size=batch_size,
epochs=epochs,
verbose=1,
validation_data=(x_test, y_test))
score = model.evaluate(x_test, y_test, verbose=0)
print('seq_approach Test loss:', score[0])
print('seq_approach Test accuracy:', score[1])
def fun_approach(input_shape,num_classes):
model = model_definition_functional_model(input_shape,num_classes)
model.fit(x_train, y_train,
batch_size=batch_size,
epochs=epochs,
verbose=1,
validation_data=(x_test, y_test))
score = model.evaluate(x_test, y_test, verbose=0)
print('fun_approach Test loss:', score[0])
print('fun_approach Test accuracy:', score[1])
def branch_approach(input_shape,num_classes):
model = branch_definition_functional_model(input_shape,num_classes)
#Two feature vectors
#In future generate two feature vectors vgg/ resnet and build classification models
model.fit([x_train,x_train], y_train,
batch_size=batch_size,
epochs=epochs,
verbose=1,
validation_data=([x_test,x_test], y_test))
score = model.evaluate([x_test,x_test], y_test, verbose=0)
print('branch_approach Test loss:', score[0])
print('branch_approach Test accuracy:', score[1])
seq_approach(input_shape,num_classes)
fun_approach(input_shape,num_classes)
branch_approach(input_shape,num_classes)
#seq_approach Test loss: 0.03464194674291939
#seq_approach Test accuracy: 0.9878
#fun_approach Test loss: 0.030942173414956777
#fun_approach Test accuracy: 0.9895
#branch_approach Test loss: 0.031166232500807384
#branch_approach Test accuracy: 0.9899
#https://keras.io/layers/merge/
#Layer that adds a list of inputs.
#import keras
#input1 = keras.layers.Input(shape=(16,))
#x1 = keras.layers.Dense(8, activation='relu')(input1)
#input2 = keras.layers.Input(shape=(32,))
#x2 = keras.layers.Dense(8, activation='relu')(input2)
# equivalent to added = keras.layers.add([x1, x2])
#added = keras.layers.Add()([x1, x2])
#out = keras.layers.Dense(4)(added)
#model = keras.models.Model(inputs=[input1, input2], outputs=out)
Happy Learning!!!

No comments: