#! /usr/bin/env python3 # def dogs_vs_cats1 ( ): #*****************************************************************************80 # ## dogs_vs_cats1 classifies images of dogs and cats. # # Discussion: # # The data consists of jpeg images of dogs and cats. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 14 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 from keras import layers from keras import models print ( '' ) print ( 'dogs_vs_cats1:' ) print ( ' python version: %s' % ( platform.python_version ( ) ) ) print ( ' keras version: %s' % ( keras.__version__ ) ) print ( ' Use a convolutional neural model' ) print ( ' to classify jpeg images of dogs and cats.' ) # # Build the model. # from keras import models from keras import layers model = models.Sequential() model.add ( layers.Conv2D ( 32, ( 3, 3 ), activation = 'relu', input_shape = ( 150, 150, 3 ) ) ) model.add ( layers.MaxPooling2D ( ( 2, 2 ) ) ) model.add ( layers.Conv2D ( 64, ( 3, 3 ), activation = 'relu' ) ) model.add ( layers.MaxPooling2D ( ( 2, 2 ) ) ) model.add ( layers.Conv2D ( 128, ( 3, 3 ), activation = 'relu' ) ) model.add ( layers.MaxPooling2D ( ( 2, 2 ) ) ) model.add ( layers.Conv2D ( 128, ( 3, 3 ), activation = 'relu' ) ) model.add ( layers.MaxPooling2D ( ( 2, 2 ) ) ) model.add ( layers.Flatten ( ) ) model.add ( layers.Dense ( 512, activation = 'relu' ) ) model.add ( layers.Dense ( 1, activation = 'sigmoid' ) ) model.summary ( ) # # Compile the model. # from keras import optimizers model.compile ( \ loss = 'binary_crossentropy', \ optimizer = optimizers.RMSprop ( lr = 1.0E-04 ), \ metrics = [ 'acc' ] ) # # Read the images. # from keras.preprocessing.image import ImageDataGenerator train_datagen = ImageDataGenerator ( rescale = 1.0 / 255.0 ) test_datagen = ImageDataGenerator ( rescale = 1.0 / 255.0 ) train_dir = '/Users/burkardt/public_html/keras_src/dogs_vs_cats_data/train' train_generator = train_datagen.flow_from_directory ( \ train_dir, \ target_size = ( 150, 150 ), \ batch_size = 20, \ class_mode = 'binary' ) validation_dir = '/Users/burkardt/public_html/keras_src/dogs_vs_cats_data/valid' validation_generator = train_datagen.flow_from_directory ( \ validation_dir, \ target_size = ( 150, 150 ), \ batch_size = 20, \ class_mode = 'binary' ) # # Look at output of generator, but only do one pass. # for data_batch, labels_batch in train_generator: print ( 'data_batch.shape:', data_batch.shape ) print ( 'labels_batch.shape:', labels_batch.shape ) break # # Fit the model using a batch generator. # history = model.fit_generator ( \ train_generator, \ steps_per_epoch = 100, \ epochs = 30, \ validation_data = validation_generator, \ validation_steps = 50 ) # # Save a copy of the model as an HDF5 file. # model.save ( 'dogs_vs_cats1.h5' ) # # Plot the loss and accuracy. # import matplotlib.pyplot as plt acc = history.history[ 'acc' ] val_acc = history.history[ 'val_acc' ] loss = history.history[ 'loss' ] val_loss = history.history[ 'val_loss' ] epochs = range ( 1, len ( acc ) + 1 ) plt.plot ( epochs, acc, 'bo', label = 'Training acc' ) plt.plot ( epochs, val_acc, 'b', label = 'Validation acc' ) plt.title ( 'Training and validation accuracy' ) plt.legend ( ) filename = 'dogs_vs_cats1_tvacc.png' plt.savefig ( filename ) print ( '' ) print ( ' Graphics saved as "%s"' % ( filename ) ) plt.show ( ) plt.plot ( epochs, loss, 'ro', label = 'Training loss' ) plt.plot ( epochs, val_loss, 'r', label = 'Validation loss' ) plt.title ( 'Training and validation loss' ) plt.legend ( ) filename = 'dogs_vs_cats1_tvloss.png' plt.savefig ( filename ) print ( '' ) print ( ' Graphics saved as "%s"' % ( filename ) ) plt.show ( ) # # Terminate. # print ( '' ) print ( 'dogs_vs_cats1:' ) 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 ( ) dogs_vs_cats1 ( ) timestamp ( )