Last active
April 15, 2020 00:58
-
-
Save tanishqsandhu/7cb08e8771f0cf482b8ab0d571e510cc to your computer and use it in GitHub Desktop.
Keras CNN Implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
from matplotlib import pyplot | |
import tensorflow as tf | |
from tf.keras.datasets import cifar10 | |
from keras.utils import to_categorical | |
from keras.models import Sequential | |
from keras.layers import Conv2D | |
from keras.layers import MaxPooling2D | |
from keras.layers import Dense | |
from keras.layers import Flatten | |
from keras.layers import Dropout | |
from keras.optimizers import SGD | |
# load train/test dataset | |
def load_dataset(): | |
# load dataset | |
(trainX, trainY), (testX, testY) = cifar10.load_data() | |
# one hot encode target values | |
trainY = to_categorical(trainY) | |
testY = to_categorical(testY) | |
return trainX, trainY, testX, testY | |
# scale pixels | |
def prep_pixels(train, test): | |
# convert from integers to floats | |
train_norm = train.astype('float32') | |
test_norm = test.astype('float32') | |
# normalize to range 0-1 | |
train_norm = train_norm / 255.0 | |
test_norm = test_norm / 255.0 | |
# return normalized images | |
return train_norm, test_norm | |
# define cnn model | |
def define_model(): | |
model = Sequential() | |
model.add(Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_uniform', padding='same', input_shape=(32, 32, 3))) | |
model.add(Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_uniform', padding='same')) | |
model.add(MaxPooling2D((2, 2))) | |
model.add(Dropout(0.2)) | |
model.add(Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_uniform', padding='same')) | |
model.add(Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_uniform', padding='same')) | |
model.add(MaxPooling2D((2, 2))) | |
model.add(Dropout(0.2)) | |
model.add(Conv2D(128, (3, 3), activation='relu', kernel_initializer='he_uniform', padding='same')) | |
model.add(Conv2D(128, (3, 3), activation='relu', kernel_initializer='he_uniform', padding='same')) | |
model.add(MaxPooling2D((2, 2))) | |
model.add(Dropout(0.2)) | |
model.add(Flatten()) | |
model.add(Dense(128, activation='relu', kernel_initializer='he_uniform')) | |
model.add(Dropout(0.2)) | |
model.add(Dense(10, activation='softmax')) | |
# compile model | |
opt = SGD(lr=0.001, momentum=0.9) | |
model.compile(optimizer=opt, loss='categorical_crossentropy', metrics=['accuracy']) | |
return model | |
# plot diagnostic learning curves | |
def summarize_diagnostics(history): | |
# plot loss | |
pyplot.subplot(211) | |
pyplot.title('Cross Entropy Loss') | |
pyplot.plot(history.history['loss'], color='blue', label='train') | |
pyplot.plot(history.history['val_loss'], color='orange', label='test') | |
# plot accuracy | |
pyplot.subplot(212) | |
pyplot.title('Classification Accuracy') | |
pyplot.plot(history.history['accuracy'], color='blue', label='train') | |
pyplot.plot(history.history['val_accuracy'], color='orange', label='test') | |
# save plot to file | |
filename = sys.argv[0].split('/')[-1] | |
pyplot.savefig(filename + '_plot.png') | |
pyplot.close() | |
# run the test harness for evaluating a model | |
def run_test_harness(): | |
# load dataset | |
trainX, trainY, testX, testY = load_dataset() | |
# prepare pixel data | |
trainX, testX = prep_pixels(trainX, testX) | |
# define model | |
model = define_model() | |
# fit model | |
history = model.fit(trainX, trainY, epochs=100, batch_size=64, validation_data=(testX, testY), verbose=0) | |
# evaluate model | |
_, acc = model.evaluate(testX, testY, verbose=0) | |
print('> %.3f' % (acc * 100.0)) | |
# learning curves | |
summarize_diagnostics(history) | |
# entry point, run the test harness | |
run_test_harness() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment