#! /usr/bin/env python3 # def dogs_vs_cats ( ): #*****************************************************************************80 # ## dogs_vs_cats() classifies images of dogs and cats. # # Discussion: # # The data consists of jpeg images of dogs and cats. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 08 January 2025 # # Author: # # Original keras version by Francois Chollet; # This version by John Burkardt. # # Reference: # # Francois Chollet, # Deep Learning with Python, # Second Edition, # Manning, 2021, # ISBN: 9781617296864. # import matplotlib.pyplot as plt import numpy as np import platform import tensorflow from tensorflow import keras from tensorflow.keras import layers print ( '' ) print ( 'dogs_vs_cats():' ) print ( ' python version: ' + platform.python_version ( ) ) print ( ' numpy version: ' + np.version.version ) print ( ' tensorflow version: ' + tensorflow.__version__ ) print ( '' ) print ( ' Use a convolutional neural model' ) print ( ' to classify jpeg images of dogs and cats.' ) # # Build the model. # inputs = keras.Input ( shape = ( 180, 180, 3 ) ) x = layers.Rescaling ( 1.0 / 255 ) ( inputs ) x = layers.Conv2D ( filters = 32, kernel_size = 3, activation = 'relu' )(x) x = layers.MaxPooling2D ( pool_size = 2 ) ( x ) x = layers.Conv2D ( filters = 64, kernel_size = 3, activation = 'relu' )(x) x = layers.MaxPooling2D ( pool_size = 2 ) ( x ) x = layers.Conv2D ( filters = 128, kernel_size = 3, activation = 'relu' )(x) x = layers.MaxPooling2D ( pool_size = 2 ) ( x ) x = layers.Conv2D ( filters = 256, kernel_size = 3, activation = 'relu' )(x) x = layers.MaxPooling2D ( pool_size = 2 ) ( x ) x = layers.Conv2D ( filters = 256, kernel_size = 3, activation = 'relu' )(x) x = layers.Flatten() ( x ) outputs = layers.Dense ( 1, activation = 'sigmoid' ) ( x ) model = keras.Model ( inputs = inputs, outputs = outputs ) # # Look at how the dimensions of the feature maps change with each # successive level. # model.summary ( ) # # Configure the model for training. # model.compile ( \ loss = 'binary_crossentropy', \ optimizer = 'rmsprop', \ metrics = [ 'accuracy' ] ) # # Read the images. # from tensorflow.keras.utils import image_dataset_from_directory new_base_dir ='/home/john/public_html/keras_src/dogs_vs_cats_data' train_dataset = image_dataset_from_directory ( '/home/john/public_html/keras_src/dogs_vs_cats_data/train', image_size = ( 180, 180 ), batch_size = 32 ) validation_dataset = image_dataset_from_directory ( '/home/john/public_html/keras_src/dogs_vs_cats_data/validation', image_size = ( 180, 180 ), batch_size = 32 ) test_dataset = image_dataset_from_directory ( '/home/john/public_html/keras_src/dogs_vs_cats_data/test', image_size = ( 180, 180 ), batch_size = 32 ) # # Look at the shape of data (32,180,180,3) and labels (32,) in a single batch. # for data_batch, labels_batch in train_dataset: print ( 'data_batch.shape: ', data_batch.shape ) print ( 'labels_batch.shape:', labels_batch.shape ) break # # Fit the model using a dataset. # callbacks = [ keras.callbacks.ModelCheckpoint ( filepath = 'convnet_from_scratch.keras', save_best_only = True, monitor = 'val_loss' ) ] history = model.fit ( \ train_dataset, \ steps_per_epoch = 100, \ epochs = 10, \ validation_data = validation_dataset, \ callbacks = callbacks ) # # Display loss and accuracy during training. # accuracy = history.history[ 'accuracy' ] val_accuracy = history.history[ 'val_accuracy' ] loss = history.history[ 'loss' ] val_loss = history.history[ 'val_loss' ] epochs = range ( 1, len ( accuracy ) + 1 ) plt.plot ( epochs, accuracy, 'bo', label = 'Training accuracy' ) plt.plot ( epochs, val_accuracy, 'b', label = 'Validation accuracy' ) plt.title ( 'Training and validation accuracy' ) plt.grid ( True ) plt.legend ( ) filename = 'dogs_vs_cats1_tvacc.png' plt.savefig ( filename ) print ( '' ) print ( ' Graphics saved as "' + 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.grid ( True ) plt.legend ( ) filename = 'dogs_vs_cats1_tvloss.png' plt.savefig ( filename ) print ( '' ) print ( ' Graphics saved as "' + filename + '"' ) plt.show ( ) # # Evaluate the model on the test set. # test_model = keras.models.load_model ( 'convnet_from_scratch.keras' ) test_loss, test_acc = test_model.evaluate ( test_dataset ) print ( ' Test accuracy: ', test_acc ) # # Terminate. # print ( '' ) print ( 'dogs_vs_cats():' ) print ( ' Normal end of execution.' ) return def timestamp ( ): #*****************************************************************************80 # ## timestamp() prints the date as a timestamp. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 06 April 2013 # # Author: # # John Burkardt # import time t = time.time ( ) print ( time.ctime ( t ) ) return None if ( __name__ == '__main__' ): timestamp ( ) dogs_vs_cats ( ) timestamp ( )