This notebook uses a pretrained model to build a classifier (CNN)
# import required libs
import os
import keras
import numpy as np
from keras import backend as K
from keras import applications
from keras.datasets import cifar10
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
import matplotlib.pyplot as plt
params = {'legend.fontsize': 'x-large',
'figure.figsize': (15, 5),
'axes.labelsize': 'x-large',
'axes.titlesize':'x-large',
'xtick.labelsize':'x-large',
'ytick.labelsize':'x-large'}
plt.rcParams.update(params)
%matplotlib inline
vgg_model = applications.VGG19(include_top=False, weights='imagenet')
vgg_model.summary()
Set Parameters
batch_size = 128
num_classes = 10
epochs = 50
bottleneck_path = r'F:\work\kaggle\cifar10_cnn\bottleneck_features_train_vgg19.npy'
# the data, shuffled and split between train and test sets
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
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)
y_train.shape
if not os.path.exists(bottleneck_path):
bottleneck_features_train = vgg_model.predict(x_train,verbose=1)
np.save(open(bottleneck_path, 'wb'),
bottleneck_features_train)
else:
bottleneck_features_train = np.load(open(bottleneck_path,'rb'))
bottleneck_features_train[0].shape
bottleneck_features_test = vgg_model.predict(x_test,verbose=1)
clf_model = Sequential()
clf_model.add(Flatten(input_shape=bottleneck_features_train.shape[1:]))
clf_model.add(Dense(512, activation='relu'))
clf_model.add(Dropout(0.5))
clf_model.add(Dense(256, activation='relu'))
clf_model.add(Dropout(0.5))
clf_model.add(Dense(num_classes, activation='softmax'))
from IPython.display import SVG
from keras.utils.vis_utils import model_to_dot
SVG(model_to_dot(clf_model, show_shapes=True,
show_layer_names=True, rankdir='TB').create(prog='dot', format='svg'))
clf_model.compile(loss=keras.losses.categorical_crossentropy,
optimizer=keras.optimizers.Adadelta(),
metrics=['accuracy'])
clf_model.fit(bottleneck_features_train, y_train,
batch_size=batch_size,
epochs=epochs,
verbose=1)
score = clf_model.evaluate(bottleneck_features_test, y_test, verbose=1)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
def predict_label(img_idx,show_proba=True):
plt.imshow(x_test[img_idx],aspect='auto')
plt.title("Image to be Labeled")
plt.show()
print("Actual Class:{}".format(np.nonzero(y_test[img_idx])[0][0]))
test_image =np.expand_dims(x_test[img_idx], axis=0)
bf = vgg_model.predict(test_image,verbose=0)
pred_label = clf_model.predict_classes(bf,batch_size=1,verbose=0)
print("Predicted Class:{}".format(pred_label[0]))
if show_proba:
print("Predicted Probabilities")
print(clf_model.predict_proba(bf))
img_idx = 3999 # sample indices : 999,1999 and 3999
for img_idx in [999,1999,3999]:
predict_label(img_idx)