...

How to classify monkeys images using convolutional neural network , Keras tuner hyper parameters , and transfer learning ? (part3)

Optimize CNN for Accuracy using Keras Tuner Hyper Parameters

Welcome to our comprehensive image classification tutorial series! In this tutorial playlist, consisting of five informative videos, we will guide you through the entire process of classifying monkey species in images.

This is part of our TensorFlow image classification tutorial series!

Each video focuses on a specific aspect of the process, providing you with a well-rounded understanding of the subject matter. Join us as we cover data preparation, classic CNN classification, hyperparameter tuning with Keras Tuner, fine-tuning a pretrained model (VGG16), and exploring the outcome of deep neural network layers in this TensorFlow image classification tutorial. Get ready to dive into the world of monkey species classification and enhance your knowledge with practical examples and expert tips.

Video 1: Data Preparation Tutorial:

In the first video of our series, we will walk you through the essential steps of data preparation for monkey species classification. Discover how to download the necessary image data and explore its characteristics. Learn how to preprocess and format the data to ensure it is ready for the subsequent phases of the classification process. By the end of this tutorial, you will have a well-prepared dataset, laying the foundation for accurate and efficient classification.

Link for more info : https://eranfeit.net/how-to-classify-monkeys-images-using-convolutional-neural-network-keras-tuner-hyper-parameters-and-transfer-learning-part1/


Video 2: CNN Classification Tutorial:

Our second video focuses on the fundamental method of classifying monkey species using a Convolutional Neural Network (CNN). Join us as we guide you through the process of writing a CNN model, training it using the prepared dataset, and evaluating its performance. Gain insights into the inner workings of CNNs and witness their effectiveness in accurately classifying monkey species based on image data.

Link for more info : https://eranfeit.net/how-to-classify-monkeys-images-using-convolutional-neural-network-keras-tuner-hyper-parameters-and-transfer-learning-part2/


Video 3: Enhancing Classification with Keras Tuner:

In the third video, we take the CNN classification tutorial to the next level by incorporating Keras Tuner. Discover how to optimize the performance of your CNN model by automatically searching for the best hyperparameters. Learn how to leverage the power of Keras Tuner to fine-tune your model and achieve even more accurate results in classifying monkey species.

Link for more info : https://eranfeit.net/how-to-classify-monkeys-images-using-convolutional-neural-network-keras-tuner-hyper-parameters-and-transfer-learning-part3/

Video 4: Fine-tuning with Pretrained VGG16:

In the fourth video, we explore an alternative approach to image classification by utilizing a pretrained model, specifically VGG16. Join us as we guide you through the process of fine-tuning the VGG16 model for the task of monkey species classification. Learn how to adapt a powerful pretrained model to accurately classify monkey species and leverage its advanced features for improved results

Link for more info : https://eranfeit.net/how-to-classify-monkeys-images-using-convolutional-neural-network-keras-tuner-hyper-parameters-and-transfer-learning-part4/

Video 5: Visualizing Deep Neural Network Layers:

In our fifth and final video, we delve into the fascinating world of model interpretability by exploring the outcome of deep neural network layers. Witness what a trained network “sees” as we dissect the layers and visualize their outputs for a specific class. Gain a deeper understanding of how the network processes and interprets images, providing valuable insights into the classification process.

Link for more info : https://eranfeit.net/what-actually-sees-a-cnn-deep-neural-network-model/


Part 3: Optimize for Accuracy using Keras Tuner Hyper Parameters :

🎯 Take your monkey species classification to the next level by leveraging the power of Keras Tuner.

So , how can we decide how many layers should we define ? how many filters in each convolutional layer ?

Should we use Dropout layer ? and what should be its value ?

Which learning rate value is better ? and more similar questions.

Optimize your CNN model’s hyperparameters, fine-tune its performance, and achieve even higher accuracy.

Learn the potential of hyperparameter tuning and enhance the precision of your classification results.

This is the link for part 3: https://youtu.be/RHMLCK5UWyk&list=UULFTiWJJhaH6BviSWKLJUM9sg

Link for the full code : https://ko-fi.com/s/8fbd4679f6

Link for my post blog : https://eranfeit.net/blog/

You can find the code in this link : https://ko-fi.com/s/8fbd4679f6


TensorFlow Image Classification Tutorial: Hyperparameter Tuning with Keras Tuner

Introduction

In this tutorial, you will build a TensorFlow CNN for multi-class image classification and automatically tune its hyperparameters with Keras Tuner Random Search.
You will define a search space for convolutional filters, dense layer width, dropout, and learning rate, then let the tuner pick the best configuration based on validation accuracy.
You will also apply robust data augmentation with ImageDataGenerator, add EarlyStopping to prevent overfitting, and finally export the best model to disk for production use.
The example uses a 10-class dataset and demonstrates a clean, reproducible training pipeline from data loading to best-model selection.

### Install core libraries if needed. # pip install tensorflow  ### Install Keras Tuner if needed. # pip install keras_tuner  ### Import TensorFlow for deep learning operations. import tensorflow as tf  ### Import Keras utilities for single-image processing if required later. from tensorflow.keras.preprocessing import image  ### Import ImageDataGenerator for efficient data loading and augmentation. from tensorflow.keras.preprocessing.image import ImageDataGenerator  ### Import training callbacks such as EarlyStopping and ModelCheckpoint. from tensorflow.keras.callbacks import EarlyStopping , ModelCheckpoint  ### Import NumPy for numeric operations and array handling. import numpy as np  ### Import time for optional timing measurements. import time  ### Import matplotlib for optional plotting of metrics. import matplotlib.pyplot as plt  ### Define a base image edge length. IMG=200  ### Create the model input size tuple. IMG_SIZE = [IMG, IMG]  ### Set the number of classes for the softmax output. numOfClasses = 10  ### Choose the batch size for training and validation. batchSize = 32  ### Set an upper bound on epochs for tuning. EPOCHS = 50  ### Configure EarlyStopping patience on validation accuracy. PATIENCE=5   ### Define a function that builds a CNN given a hyperparameter object. def build_model(hp):     ### Sample the number of filters for the first conv layer.     filters_layer1=hp.Int('filters_layer1',min_value=32 , max_value=256, step=32)     ### Sample the number of filters for the second conv layer.     filters_layer2=hp.Int('filters_layer2',min_value=32 , max_value=256, step=32)     ### Sample the number of filters for the third conv layer.     filters_layer3=hp.Int('filters_layer3',min_value=32 , max_value=256, step=32)     ### Sample the number of filters for the fourth conv layer.     filters_layer4=hp.Int('filters_layer4',min_value=32 , max_value=256, step=32)      ### Choose a learning rate for the Adam optimizer.     hp_learning_rate=hp.Choice('learning_rate',values=[1e-2, 1e-3, 1e-4 ])     ### Build the Adam optimizer using the sampled learning rate.     hp_optimizer = tf.keras.optimizers.Adam(learning_rate = hp_learning_rate )     ### Sample dropout rate to regularize the dense head.     hp_dropout = hp.Choice('drop_out', values=[0.3 , 0.5])      ### Sample the width of the last dense hidden layer.     hp_last_dense_layer = hp.Int('last_dense_layer',min_value=128 , max_value=768, step=64)      ### Construct a sequential CNN with four conv blocks and a dense head.     model1 = tf.keras.models.Sequential ([         ### First convolution followed by 2x2 max pooling.         tf.keras.layers.Conv2D(filters_layer1,kernel_size=(3,3), activation='relu', input_shape=(IMG,IMG,3)),         ### Downsample spatially to reduce computation and capture invariances.         tf.keras.layers.MaxPooling2D(2,2),          ### Second convolution to learn richer local features.         tf.keras.layers.Conv2D(filters_layer2,kernel_size=(3,3), activation='relu'),         ### Pool to control feature map size and overfitting.         tf.keras.layers.MaxPooling2D(2,2),          ### Third convolution to deepen the representation.         tf.keras.layers.Conv2D(filters_layer3,kernel_size=(3,3), activation='relu'),         ### Pool again to progressively reduce resolution.         tf.keras.layers.MaxPooling2D(2,2),          ### Fourth convolution to capture higher-level patterns.         tf.keras.layers.Conv2D(filters_layer4,kernel_size=(3,3), activation='relu'),         ### Pool for additional spatial reduction before the classifier head.         tf.keras.layers.MaxPooling2D(2,2),          ### Flatten 3D feature maps into a 1D vector for the dense layers.         tf.keras.layers.Flatten(),         ### Apply sampled dropout to mitigate overfitting.         tf.keras.layers.Dropout(hp_dropout),          ### Dense hidden layer with sampled width and ReLU activation.         tf.keras.layers.Dense(hp_last_dense_layer, activation='relu'),          ### Output layer with softmax for multi-class probabilities.         tf.keras.layers.Dense(numOfClasses, activation='softmax')     ])      ### Compile the model with categorical crossentropy and accuracy metric.     model1.compile(loss='categorical_crossentropy', optimizer=hp_optimizer , metrics=['accuracy'])      ### Return the compiled model to the tuner.     return model1 

You can find the code in this link : https://ko-fi.com/s/8fbd4679f6

You now have a tuner-aware CNN builder that exposes key architectural and optimization knobs, making it simple to explore high-impact configurations automatically.

Data pipeline, augmentation, steps, and callbacks

What you’ll do: Point to train and validation folders, configure augmentation for generalization, create generators, compute steps per epoch, and define callbacks such as EarlyStopping.
Why it matters: Clean input pipelines and regularization are essential for stable tuning runs and high validation accuracy.

### Set the training directory containing subfolders per class. trainMyImagesFolder = "C:/Python-cannot-upload-to-GitHub/10-Monkey-Species/training/training"  ### Set the validation directory containing subfolders per class. testMyImagesFolder = "C:/Python-cannot-upload-to-GitHub/10-Monkey-Species/validation/validation"  ### Create an ImageDataGenerator with aggressive but reasonable augmentation. train_datagen = ImageDataGenerator(rescale = 1. / 255,                                      rotation_range = 20 ,                                     width_shift_range = 0.2 ,                                     height_shift_range = 0.2 ,                                     shear_range = 0.2 ,                                     zoom_range = 0.2 ,                                     horizontal_flip = True)  ### Build the training iterator from the directory with shuffling and target size. training_set = train_datagen.flow_from_directory(trainMyImagesFolder,                                                 shuffle=True,                                                 target_size=IMG_SIZE,                                                 batch_size=batchSize,                                                 class_mode = 'categorical')  ### Create a validation generator with only rescaling. test_datagen = ImageDataGenerator(rescale = 1. / 255)  ### Build the validation iterator without shuffling to keep evaluation deterministic. test_set = test_datagen.flow_from_directory(testMyImagesFolder,                                                 shuffle=False, #### important                                                 target_size=IMG_SIZE,                                                 batch_size=batchSize,                                                 class_mode = 'categorical')  ### Compute the number of steps per epoch for training. stepsPerEpochs = np.ceil (training_set.samples / batchSize) # round the result up  ### Compute the number of validation steps per epoch. validationSteps =np.ceil (test_set.samples / batchSize)   ### Choose a filepath to save the best model found by tuning. best_model_file = "C:/Python-cannot-upload-to-GitHub/10-Monkey-Species/myCnnMonkeyModelHyperParam.h5"  ### Define EarlyStopping on validation accuracy to curb overfitting and save time. stop_early = tf.keras.callbacks.EarlyStopping(monitor='val_accuracy', patience=PATIENCE) 

You can find the code in this link : https://ko-fi.com/s/8fbd4679f6

Your training and validation streams are ready with balanced augmentation, deterministic evaluation, and computed step counts, plus a callback to terminate unpromising runs early.

Keras Tuner Random Search, experiment run, model selection, and export

What you’ll do: Initialize Keras Tuner’s Random Search, launch the search with EarlyStopping, inspect the best hyperparameters and model, then save the top checkpoint to disk.
Why it matters: Automated search finds strong configurations faster than manual guesswork and produces a reproducible, best-model artifact you can deploy or fine-tune.

### Import Keras Tuner core and RandomSearch engine. import keras_tuner from keras_tuner import RandomSearch  ### Instantiate a Random Search tuner with objective on validation accuracy. tuner = RandomSearch(     build_model ,      objective='val_accuracy',     max_trials=5 , # how many random options     executions_per_trial=12, # in each version , how many times to execute it     directory="c:/temp",     project_name='MoneyCnnRandomSearch',     overwrite=True    )  ### Launch the tuner search over your generators with EarlyStopping. tuner.search(     training_set,     validation_data = test_set,     epochs = EPOCHS,     batch_size = batchSize,     callbacks=[stop_early],     steps_per_epoch=stepsPerEpochs,     validation_steps=validationSteps   )  ### Retrieve the best hyperparameters sampled by the tuner. best_hp = tuner.get_best_hyperparameters()[0].values   ### Print a separator for readability. print("==================================")  ### Print a title for the hyperparameter summary. print("Best model parameters :")  ### Inspect the dictionary of best hyperparameter values. print(best_hp)   ### Print a separator for readability. print("==================================")  ### Print a blank line for spacing. print("  ")  ### Retrieve the top ranked model configured with the best hyperparameters. model = tuner.get_best_models(num_models=1)[0]  ### Print a separator for readability. print("==================================")  ### Describe the best model architecture and parameter counts. print("Best model is  :")  ### Show the model summary to the console. print(model.summary())  ### Print a separator for readability. print("==================================")  ### Print a blank line for spacing. print("  ")  ### Persist the best model to the chosen H5 file for reuse and deployment. model.save(best_model_file) 

You can find the code in this link : https://ko-fi.com/s/8fbd4679f6

The tuner executed randomized trials, chose the strongest configuration, reported the hyperparameters and model summary, and saved the winning network to disk for inference or future fine-tuning.


Keras Tuner Hyperband vs Random Search: Faster TensorFlow CNN Tuning for Image Classification

This script builds a TensorFlow CNN for a 10-class image classification task and tunes its architecture and training hyperparameters with Keras Tuner’s Hyperband.
It defines a search space over convolutional filters, dense layer width, dropout, and Adam learning rate, sets up an augmented data pipeline with ImageDataGenerator, runs bracketed Hyperband rounds with EarlyStopping, then retrieves and saves the best model.

Compared with a plain RandomSearch tuner, Hyperband is typically faster and more compute-efficient because it allocates small budgets to many trials first and promotes only the most promising ones to larger budgets, pruning weak trials early.
This balances exploration and exploitation, reduces wasted epochs on poor configurations, and often reaches a stronger validation accuracy within the same or fewer GPU hours.

### Install TensorFlow if it is not already installed. # pip install tensorflow  ### Install Keras Tuner if it is not already installed. # pip install keras_tuner   ### Import TensorFlow and Keras APIs for model building and training. import tensorflow as tf  ### Import a utility for individual image preprocessing if needed later. from tensorflow.keras.preprocessing import image  ### Import ImageDataGenerator for augmentation and efficient directory-based loading. from tensorflow.keras.preprocessing.image import ImageDataGenerator  ### Import callbacks for training control such as EarlyStopping and ModelCheckpoint. from tensorflow.keras.callbacks import EarlyStopping , ModelCheckpoint  ### Import NumPy for numerical operations and array handling. import numpy as np  ### Import time for optional timing measurements and logging. import time  ### Import matplotlib for optional visualization of learning curves. import matplotlib.pyplot as plt   ### Define the square input resolution for images. IMG=200  ### Create the width × height tuple expected by Keras generators and layers. IMG_SIZE = [IMG, IMG]  ### Set the total number of target classes for softmax output. numOfClasses = 10  ### Choose the mini-batch size for training and validation. batchSize = 32  ### Set an upper bound on epochs used during the tuner’s .search loop. EPOCHS = 50  ### Configure patience for EarlyStopping on validation accuracy. PATIENCE=5     ### Define a tuner-aware factory that builds a compiled model from hyperparameters. def build_model(hp):      ### Sample the number of filters for the first convolutional layer from 32 to 256 in steps of 32.     filters_layer1=hp.Int('filters_layer1',min_value=32 , max_value=256, step=32)      ### Sample the number of filters for the second convolutional layer from 32 to 256 in steps of 32.     filters_layer2=hp.Int('filters_layer2',min_value=32 , max_value=256, step=32)      ### Sample the number of filters for the third convolutional layer from 32 to 256 in steps of 32.     filters_layer3=hp.Int('filters_layer3',min_value=32 , max_value=256, step=32)      ### Sample the number of filters for the fourth convolutional layer from 32 to 256 in steps of 32.     filters_layer4=hp.Int('filters_layer4',min_value=32 , max_value=256, step=32)      ### Choose a learning rate for the Adam optimizer from a small categorical set.     hp_learning_rate=hp.Choice('learning_rate',values=[1e-2, 1e-3, 1e-4 ])      ### Construct the Adam optimizer using the sampled learning rate.     hp_optimizer = tf.keras.optimizers.Adam(learning_rate = hp_learning_rate )      ### Sample a dropout rate to regularize the dense head.     hp_dropout = hp.Choice('drop_out', values=[0.3 , 0.5])      ### Sample the width of the penultimate dense layer from 128 to 768 in steps of 64.     hp_last_dense_layer = hp.Int('last_dense_layer',min_value=128 , max_value=768, step=64)        ### Create a sequential CNN with four conv–pool blocks followed by a dense classifier.     model1 = tf.keras.models.Sequential ([          ### First convolution extracts low-level features and defines input shape.         tf.keras.layers.Conv2D(filters_layer1,kernel_size=(3,3), activation='relu', input_shape=(IMG,IMG,3)),          ### Max pooling reduces spatial dimensions and adds translation invariance.         tf.keras.layers.MaxPooling2D(2,2),          ### Second convolution deepens the feature representation for mid-level patterns.         tf.keras.layers.Conv2D(filters_layer2,kernel_size=(3,3), activation='relu'),          ### Pooling compacts features and limits overfitting by reducing parameters.         tf.keras.layers.MaxPooling2D(2,2),          ### Third convolution captures higher-level structures in the image.         tf.keras.layers.Conv2D(filters_layer3,kernel_size=(3,3), activation='relu'),          ### Additional pooling further reduces the feature map size.         tf.keras.layers.MaxPooling2D(2,2),          ### Fourth convolution increases representational capacity for complex patterns.         tf.keras.layers.Conv2D(filters_layer4,kernel_size=(3,3), activation='relu'),          ### Pool again to prepare for the fully connected head.         tf.keras.layers.MaxPooling2D(2,2),          ### Flatten converts 3D feature maps into a 1D vector for dense layers.         tf.keras.layers.Flatten(),          ### Dropout randomly zeros activations to mitigate overfitting.         tf.keras.layers.Dropout(hp_dropout),          ### Dense layer provides a learned non-linear combination before classification.         tf.keras.layers.Dense(hp_last_dense_layer, activation='relu'),          ### Output layer produces class probabilities via softmax over numOfClasses.         tf.keras.layers.Dense(numOfClasses, activation='softmax')     ])      ### Compile the model with categorical crossentropy and accuracy for multi-class classification.     model1.compile(loss='categorical_crossentropy', optimizer=hp_optimizer , metrics=['accuracy'])      ### Return the compiled model instance to the tuner.     return model1   ### Set the training images directory organized in subfolders per class. trainMyImagesFolder = "C:/Python-cannot-upload-to-GitHub/10-Monkey-Species/training/training"  ### Set the validation images directory organized in subfolders per class. testMyImagesFolder = "C:/Python-cannot-upload-to-GitHub/10-Monkey-Species/validation/validation"  ### Create an augmentation pipeline with rescaling and common geometric transforms. train_datagen = ImageDataGenerator(rescale = 1. / 255,                                      rotation_range = 20 ,                                     width_shift_range = 0.2 ,                                     height_shift_range = 0.2 ,                                     shear_range = 0.2 ,                                     zoom_range = 0.2 ,                                     horizontal_flip = True)  ### Build the training generator that shuffles data and outputs batches at IMG_SIZE. training_set = train_datagen.flow_from_directory(trainMyImagesFolder,                                                 shuffle=True,                                                 target_size=IMG_SIZE,                                                 batch_size=batchSize,                                                 class_mode = 'categorical')  ### Create a validation generator that only rescales pixels to preserve evaluation consistency. test_datagen = ImageDataGenerator(rescale = 1. / 255)  ### Build the validation generator with deterministic ordering by disabling shuffling. test_set = test_datagen.flow_from_directory(testMyImagesFolder,                                                 shuffle=False, #### important                                                 target_size=IMG_SIZE,                                                 batch_size=batchSize,                                                 class_mode = 'categorical')  ### Compute training steps per epoch by rounding up samples divided by batch size. stepsPerEpochs = np.ceil (training_set.samples / batchSize) # round the result up  ### Compute validation steps per epoch similarly using the validation set size. validationSteps =np.ceil (test_set.samples / batchSize)   ### Define the file path where the best tuned model will be saved in H5 format. best_model_file = "C:/Python-cannot-upload-to-GitHub/10-Monkey-Species/myCnnMonkeyModelHyperBandParam.h5"  ### Create an EarlyStopping callback that monitors validation accuracy with configured patience. stop_early = tf.keras.callbacks.EarlyStopping(monitor='val_accuracy', patience=PATIENCE)    ### Import Keras Tuner and the Hyperband algorithm for efficient resource allocation. import keras_tuner from keras_tuner import RandomSearch, Hyperband  ### Instantiate a Hyperband tuner that promotes promising trials to higher epoch budgets. tuner = Hyperband (     build_model, # our function name     objective='val_accuracy',     max_epochs=100, # ceiling epochs per trial across brackets     factor=3, # downsampling factor that controls pruning between rounds     directory ='c:/temp',     project_name = 'MoneyCnnHyperSearch',     overwrite=True     )   ### Launch the tuner search over the generators using EarlyStopping for each trial. tuner.search(     training_set,     validation_data = test_set,     epochs = EPOCHS,     batch_size = batchSize,     callbacks=[stop_early],     steps_per_epoch=stepsPerEpochs,     validation_steps=validationSteps   )   ### Retrieve the top hyperparameter set selected by the tuner. best_hp = tuner.get_best_hyperparameters()[0].values   ### Print a visual separator for readability in the console. print("==================================")  ### Print a title preceding the hyperparameter dictionary. print("Best model parameters :")  ### Output the dictionary of best hyperparameters. print(best_hp)   ### Print a visual separator for readability in the console. print("==================================")  ### Print a blank line to add spacing in the log. print("  ")   ### Retrieve the best-performing trained Keras model from the tuner. model = tuner.get_best_models(num_models=1)[0]  ### Print a visual separator for readability in the console. print("==================================")  ### Print a label preceding the model summary. print("Best model is  :")  ### Print the model architecture summary including layers and parameter counts. print(model.summary())  ### Print a visual separator for readability in the console. print("==================================")  ### Print a blank line to add spacing in the log. print("  ")  ### Save the best model to disk for later inference or fine-tuning. model.save(best_model_file) 

You can find the code in this link : https://ko-fi.com/s/8fbd4679f6

error: Content is protected !!
Eran Feit