Deep learning project end to end | Potato Disease Classification - 3 : Model Building

Deep learning project end to end | Potato Disease Classification - 3 : Model Building

Building a Convolutional Neural Network for Image Classification

In this video, we will build a convolutional neural network to classify images. We will use pre-processed data and train the model on it. We will measure the accuracy using test data and export the model to a file on disk.

Architecture of Convolutional Neural Network

  • The architecture consists of convolutional layers followed by pooling layers.
  • There could be multiple layers of each type.
  • The final layer is a dense layer where the output is flattened.

Convolutional Layers

  • The first layer is resize and rescale.
  • Then, data augmentation is done to generate new samples.
  • Next, convolutional layers are added using Conv2d function with arguments such as number of filters and kernel size.

Pooling Layers

  • After each convolutional layer, there should be a pooling layer.
  • Max pooling is commonly used where you take the maximum value in an area.

Model Training

  • After adding all the required layers, compile the model using compile() function with appropriate loss function and optimizer.
  • Train the model using fit() function with training data set and validation data set.
  • Evaluate the accuracy of trained model using test dataset.

Exporting Model

  • If accuracy looks good, export the trained model to a file on disk so that it can be used later for making predictions in FastAPI based server.

Building a Neural Network for Image Classification

In this section, the speaker explains how to build a neural network for image classification using Keras.

Defining the Neural Network Architecture

  • The number of layers and neurons in each layer is determined through trial and error.
  • After stacking max pooling layers, convert to 2D layer max pooling layer.
  • Flatten the data so that it's an array of neurons and add a dense layer with 64 neurons.
  • Add a final output classification layer with three neurons using softmax activation function to normalize probability of classes.

Compiling and Training the Model

  • Use model.build() API to compile the model. Input shape should have channels and batch size.
  • Compile using optimizers such as Adam optimizer, define loss function, matrix in each epoch, and metric to track gradient descent (accuracy).
  • Train your network using model.fit(). Set epochs (50), batch size, verbose level (1), and validation data set. Record all history of every epoch in history parameter for plotting later on.

Evaluating Model Performance

  • Run test on test data set before deploying any model to evaluate its performance without bias. Accuracy comes out to be 98%.

Understanding the History Parameter

In this section, the speaker explains how to use the history parameter in TensorFlow Keras callback history.

Storing and Accessing History

  • The history parameter stores information about a model's performance during training.
  • The history object has an element called "history" that contains four parameters: loss, accuracy, validation loss, and validation accuracy.
  • For each of these parameters, there are 50 values recorded for each epoch.

Plotting Performance Metrics

  • The speaker demonstrates how to plot training and validation accuracy using Matplotlib.
  • Loss is another important metric that can be plotted alongside accuracy to track error reduction during backpropagation.

Making Predictions with Trained Models

In this section, the speaker explains how to make predictions using a trained model in TensorFlow Keras.

Preparing Test Data

  • To make predictions on new data, test data must first be prepared by setting up a for loop to iterate through batches of images.
  • Images can be accessed by indexing into the batch tensor.

Displaying Images and Labels

  • Images can be displayed using Matplotlib or converted from tensors to numpy arrays for display.
  • Actual labels can also be accessed and displayed alongside images.

Generating Predictions

  • Predictions can be generated using the predict function on a trained model.
  • Softmax activation produces probabilities for each class label.

Finding the Index

In this section, the speaker explains how to find the index of an array using numpy.

Using np.dot.argmax()

  • To find the index of an array, use np.dot.argmax().
  • The answer will be zero if the item at the zeroth location is maximum.

Performing Predictions

In this section, the speaker discusses how to perform predictions using a convolutional neural network.

Creating a Function for Prediction

  • Create a function that takes in a model and image as input.
  • Use predict() function to get predicted class and confidence.
  • A confidence score of 100% means that accuracy of prediction is 100%.

Plotting Predicted Class and Confidence

  • Use plt.subplots() to show all images.
  • Increase dimension so each image looks bigger.
  • Add title with actual label, predicted label, and confidence score.

Saving Model

In this section, the speaker explains how to save a model in TensorFlow.

Saving Model in New Directory

  • Create new directory called "models".
  • Save model in this directory using model.save().
  • Use python format string to specify model version.

Auto-incrementing Model Versions

In this section, the speaker explains how to auto-increment model versions using the OS library in Python.

Incrementing Model Versions

  • To keep a running count of model versions, use the OS library and list directories.
  • Convert string to integer using list comprehension.
  • Find max of all integers to get next model version.

Saving Models

  • Save models with incremented version numbers.
  • Running the code again will automatically create new versions.

Exercise

  • As an exercise, build a model for tomato classification with multiple classes.

Turn any video into a summary like this

YouTube links, meetings, lectures — with transcripts, search, and chat.

Video description

We will train a convolutional neural network in tensorflow using potato plant images. The goal of this model would be to classify these images as either healthy or early blight or late blight. We will cover following topics, ⭐️ Timestamps ⭐️ 00:00 Introduction 00:28 Build and train a CNN model 11:31 Plot training history on graph 15:26 Make predictions/inference on sample images 24:06 Export model to a file on disk 27:42 Exercise for you (very important!) :) Do you want to learn technology from me? Check https://codebasics.io/ for my affordable video courses. Code: https://github.com/codebasics/potato-disease-classification/blob/main/training/potato-disease-classification-model.ipynb Dataset is taken from Kaggle: https://www.kaggle.com/arjuntejaswi/plant-village Project Playlist : https://www.youtube.com/playlist?list=PLeo1K3hjS3ut49PskOfLnE6WUoOp_2lsD Project Hindi Playlist : https://www.youtube.com/playlist?list=PLPbgcxheSpE1gl5WkrwtmRiCwiGMM8NdH Deep learning tutorials: https://www.youtube.com/playlist?list=PLeo1K3hjS3uu7CxAacxVndI4bE_o3BDtO Python tutorials: https://www.youtube.com/playlist?list=PLeo1K3hjS3uv5U-Lmlnucd7gqF-3ehIh0 All machine learning projects: https://www.youtube.com/playlist?list=PLeo1K3hjS3ut2o1ay5Dqh-r1kq6ZU8W0M 🌎 Website: https://codebasics.io/ 🎥 Codebasics Hindi channel: https://www.youtube.com/channel/UCTmFBhuhMibVoSfYom1uXEg #️⃣ Social Media #️⃣ 🔗 Discord: https://discord.gg/r42Kbuk 📸 Instagram: https://www.instagram.com/codebasicshub/ 🔊 Facebook: https://www.facebook.com/codebasicshub 📱 Twitter: https://twitter.com/codebasicshub 📝 Linkedin (Personal): https://www.linkedin.com/in/dhavalsays/ 📝 Linkedin (Codebasics): https://www.linkedin.com/company/codebasics/ ❗❗ DISCLAIMER: All opinions expressed in this video are of my own and not that of my employers'.