# Import basic libraries and keras import os import keras import numpy as np from keras.utils import np_utils from keras.models import Sequential, load_model from keras.layers import Dense, Dropout, Activation, Flatten, Convolution2D, MaxPooling2D # Read the MNIST data and split to train and test f = np.load('mnist.npz') x_train, y_train = f['x_train'], f['y_train'] x_test, y_test = f['x_test'], f['y_test'] f.close() # Change depth of image to 1 x_train = x_train.reshape(x_train.shape[0], 28, 28, 1) x_test = x_test.reshape(x_test.shape[0], 28, 28, 1) # Change type from int to float and normalize to [0, 1] x_train = x_train.astype('float32') x_test = x_test.astype('float32') x_train /= 255 x_test /= 255 # Optionally check the number of samples #print(x_train.shape[0], 'train samples') #print(x_test.shape[0], 'test samples') # Convert class vectors to binary class matrices (transform the problem to multi-class classification) num_classes = 10 y_train = keras.utils.to_categorical(y_train, num_classes) y_test = keras.utils.to_categorical(y_test, num_classes) # Check if there is a pre-trained model if not os.path.exists('cnn_model.h5'): # Create a neural network with 2 convolutional layers and 2 dense layers model = Sequential() model.add(Convolution2D(32, 3, 3, activation='relu', input_shape=(28,28,1))) model.add(Convolution2D(32, 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(10, activation='softmax')) model.summary() model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) # Train the model model.fit(x_train, y_train, batch_size=32, epochs=10, verbose=1, validation_data=(x_test, y_test)) # Save the model model.save('cnn_model.h5') else: # Load the model from disk model = load_model('cnn_model.h5') # Get loss and accuracy on validation set score = model.evaluate(x_test, y_test, verbose=0) print('Test loss:', score[0]) print('Test accuracy:', score[1])