I’ve just run my first CNN project based on The Ultimate Beginner’s Guide to Deep Learning in Python. It’s a simple classifier of MNIST dataset and it works perfectly. I’m amazed and really pleased to understand almost the whole conception. Next step would be a development of a classifier on a more complex dataset.
#####Ref: https://elitedatascience.com/keras-tutorial-deep-learning-in-python## ############################################################################### ########################Import libraries and modules########################### from keras.datasets import fashion_mnist import numpy as np from keras.models import Sequential ## Common NN layers from keras.layers import Dense, Dropout, Activation, Flatten ## CNN layers from keras.layers.convolutional import Conv2D from keras.layers import MaxPooling2D ## Utilities to transform data from keras.utils import np_utils ############################################################################### ######################## Load image data from MNIST############################ from keras.datasets import mnist (X_train, Y_train),(X_test, Y_test) = mnist.load_data() ## Look at some data print (X_train.shape) # (60000, 28, 28) from matplotlib import pyplot as plt plt.imshow(X_train[0]) ############################################################################### ########################Preprocess input data for Keras######################## ## Transform the dataset from (n, width, height) to (n, depth, width, height) depth = 1 # for RGB photos depth = 3 X_train = X_train.reshape(X_train.shape[0], X_train.shape[1],X_train.shape[2], depth) X_test = X_test.reshape(X_test.shape[0], X_test.shape[1],X_test.shape[2], depth) ## Look again at the data print (X_train.shape) # (60000, 1, 28, 28) ## Convert the datatype to float32 and normalize the data X_train = X_train.astype('float32') / 255 X_test = X_test.astype('float32') / 255 ############################################################################### ########################Preprocess class labels for Keras###################### ## Look at the shape of the class label data print (Y_train.shape) #(60000,) print (Y_train[:10]) # [5 0 4 1 9 2 1 3 1 4] ## Convert 1-dimensional class arrays to 10-dimensional class matrices num_classes = 10 Y_train = np_utils.to_categorical(Y_train, num_classes) Y_test = np_utils.to_categorical(Y_test, num_classes) ## Look again at the shape of the class label data print (Y_train.shape) # (60000, 10) print (Y_train[:10][0]) #[ 0. 0. 0. 0. 0. 1. 0. 0. 0. 0.] ############################################################################### ########################Define model architecture############################## model = Sequential() ## Input layer ## Input: 28x28 images with 1 channel ## This applies 32 convolution filters of size 3x3 each model.add(Conv2D(32,kernel_size=(3,3),activation='relu',input_shape=(28,28,1))) ## Look at the output shape print (model.output_shape) # (None, 32, 26, 26) ## Other layers model.add(Conv2D(32, (3, 3), activation='relu')) ## Reduce the number of parameters in the model model.add(MaxPooling2D(pool_size=(2,2))) ## Regularize the model in order to prevent overfitting model.add(Dropout(0.25)) ## Add a fully connected layer and then the output layer ## Flatten the weights from the Convolution layers(made 1-dimensional)... ## ...before passing them to the fully connected Dense layer model.add(Flatten()) # 128 = output size of this Dense layer model.add(Dense(128, activation='relu')) model.add(Dropout(0.5)) # 10 = output size of this Dense layer model.add(Dense(10, activation='softmax')) ############################################################################### ########################Compile model########################################## ## Compile the model & ... model.compile(loss ='categorical_crossentropy', optimizer = 'adam' , # optimizer: SGD, Adam, etc. metrics = ['accuracy'] ) ## Fit model on training data model.fit(X_train, Y_train, batch_size = 32, epochs = 10, verbose =1) ############################################################################### ########################Evaluate model on test data############################ score = model.evaluate(X_test, Y_test, verbose = 0) print(score)
Categories: Technical
Tags: AI, CNN, Deep Learning, Project