#! /usr/bin/env python3 # def mnist_convnet ( ): #*****************************************************************************80 # ## mnist_convnet classifies the MNIST digit data with a convolutional network. # # Discussion: # # The data consists of gray scale images, which can be thought of as # 28 x 28 matrices of integers between 0 and 255. # # These images are crude pictures of handwritten digits, "0" through "9". # # The task is to find a rule for classifying a given image by stating # the digit it represents. # # In a typical neural network procedure, each 28x28 array is reshaped # into a vector. In a convolutional neural network, each image retains # its original shape, and, in fact, there is a third dimension available # so that color images can be easily handled. Because our images are # gray-scaled, this potential third dimension is set to 1. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 01 May 2019 # # Author: # # The original text is by Francois Chollet; # some modifications by John Burkardt. # # Reference: # # Francois Chollet, # Deep Learning with Python, # Manning, 2018, # ISBN: 9781617294433. # import keras import platform print ( '' ) print ( 'mnist_convnet:' ) print ( ' python version: %s' % ( platform.python_version ( ) ) ) print ( ' keras version: %s' % ( keras.__version__ ) ) print ( ' Use a convolutional neural network to classify the MNIST digit image data.' ) # Import and load the dataset. # from keras.datasets import mnist ( train_images, train_labels ), ( test_images, test_labels ) = mnist.load_data() # # Display the shape, size, and type of the training and testing items. # print ( '' ) print ( ' Number of training items = %d' % ( len ( train_labels ) ) ) print ( ' Training data shape = (%d,%d,%d)' % ( train_images.shape ) ) print ( ' Sample training labels:' ) print ( train_labels ) print ( '' ) print ( ' Number of testing items = %d' % ( len ( test_labels ) ) ) print ( ' Training data shape = (%d,%d,%d)' % ( train_images.shape ) ) print ( ' Sample testing labels:' ) print ( test_labels ) print ( '' ) # # Convert the training data from uint8 arrays of shape (60000,28,28) to # float32 arrays of shape (60000,28,28,1), and similarly for the test data. # train_images = train_images.reshape ( ( 60000, 28, 28, 1 ) ) train_images = train_images.astype ( 'float32' ) / 255 test_images = test_images.reshape ( ( 10000, 28, 28, 1 ) ) test_images = test_images.astype ( 'float32' ) / 255 # # Convert the labels from numeric values to categorical values. # from keras.utils import to_categorical train_labels = to_categorical ( train_labels ) test_labels = to_categorical ( test_labels ) # # Define the network architecture. # from keras import models from keras import layers network = models.Sequential() network.add ( layers.Conv2D ( 32, ( 3, 3 ), activation = 'relu', input_shape = ( 28, 28, 1 ) ) ) network.add ( layers.MaxPooling2D ( ( 2, 2 ) ) ) network.add ( layers.Conv2D ( 32, ( 3, 3 ), activation = 'relu' ) ) network.add ( layers.MaxPooling2D ( ( 2, 2 ) ) ) network.add ( layers.Conv2D ( 64, ( 3, 3 ), activation = 'relu' ) ) network.summary ( ) network.add ( layers.Flatten ( ) ) network.add ( layers.Dense ( 64, activation = 'relu' ) ) network.add ( layers.Dense ( 10, activation = 'softmax' ) ) network.summary ( ) # # Compile the network. # network.compile ( \ optimizer = 'rmsprop', \ loss = 'categorical_crossentropy', \ metrics = [ 'accuracy' ] ) # # Train the network. # network.fit ( train_images, train_labels, epochs = 5, batch_size = 64 ) # # Test the network. # test_loss, test_acc = network.evaluate ( test_images, test_labels ) # # Print the test accuracy. # print ( '' ) print ( 'test_loss: %g' % ( test_loss ) ) print ( 'test_acc: %g' % ( test_acc ) ) # # Terminate. # print ( '' ) print ( 'mnist_convnet:' ) print ( ' Normal end of execution.' ) return def timestamp ( ): #*****************************************************************************80 # ## TIMESTAMP prints the date as a timestamp. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 06 April 2013 # # Author: # # John Burkardt # # Parameters: # # None # import time t = time.time ( ) print ( time.ctime ( t ) ) return None if ( __name__ == '__main__' ): timestamp ( ) mnist_convnet ( ) timestamp ( )