Learn Python

Learn Data Structure & Algorithm

Learn Numpy

Learn Pandas

Learn Matplotlib

Learn Seaborn

Learn Statistics

Learn Math

Learn MATLAB

Learn Machine learning

Learn Github

Learn OpenCV

Introduction

Setup

ANN

Working process ANN

Propagation

Bias parameter

Activation function

Loss function

Overfitting and Underfitting

Optimization function

Chain rule

Minima

Gradient problem

Weight initialization

Dropout

ANN Regression Exercise

ANN Classification Exercise

Hyper parameter tuning

CNN

CNN basics

Convolution

Padding

Pooling

Data argumentation

Flattening

Create Custom Dataset

Binary Classification Exercise

Multiclass Classification Exercise

Transfer learning

Transfer model Basic template

RNN

How RNN works

LSTM

Bidirectional RNN

Sequence to sequence

Attention model

Transformer model

Bag of words

Tokenization & Stop words

Stemming & Lemmatization

TF-IDF

N-Gram

Word embedding

Normalization

Pos tagging

Parser

semantic analysis

Regular expression

Learn MySQL

Learn MongoDB

Learn Web scraping

Learn Excel

Learn Power BI

Learn Tableau

Learn Docker

Learn Hadoop

Convolution Neural Network Multiclass Classification Exercise

Look in this problem, I will use four different types of disease of strawberry and will try to classify those disease. Here the classes or disease will be angular leaf spot, gray, leaf spot, powdery mildew leaf.
So to prepare the dataset:
At first create folder. The inside that folder create four different folders for four different classes of disease. You can give any name of these folders but I will use the disease name. Now search the on the internet for these disease images. Now download and put the disease images in the folder. Four disease put in four folders. Be Careful about mixing of the images.

'''
Because my those four different images folder are present in main folder name data2. So I need the path of this data folder to access those images folder. So in directory variable we have the path of the data folders.
'''

base_dir=r"D:\data2"

Data preprocessing

Suppose we give cat and dog images to our CNN model and then it predicts that is it a cat or dog. So our model learns from a normal cat image but when we give a new cat image then that image can be flipped, rotated, horizontally shifted, vertically shifted, etc. So in data preprocessing, we create different images from one image.
Suppose we will take a cat image and will create a flipped image, rotated image, zoom image, etc images so that when our CNN model gets a flipped image of a cat it can recognize that cat image.

#Data Preprocessing for training dataset
train_datagen=tf.keras.preprocessing.image.ImageDataGenerator(
      rescale=1./255,
      shear_range=0.2,
      zoom_range=0.2,
      horizontal_flip=True,
      fill_mode='nearest',
      rotation_range=50,
      validation_split=0.1
)
'''
So here we will take each image and will shear, zoom, horizontal_flip, fill_mode , rotate the image. So after the work we will get 5 different images from one image. So if we have to images then after preprocessing we will have 50 images. Here I used validation split is 0.1. It means we will take 10% data for testing. Here we rescale each image between 0 to 1, by doing this we are performing normalization.
'''



#Data Preprocessing for test dataset
test_datagen=tf.keras.preprocessing.image.ImageDataGenerator(
      rescale=1./255,
      validation_split=0.1
)
'''
For test data we will try to keep the images raw as much as possible. Because using this raw images will we see that is our model is working or not.
'''

Let's define the directory

#At first defined the directory for train data
train_datagen=train_datagen.flow_from_directory(
      base_dir,
      target_size=(224,224),
      #target size means image size
      batch_size=64,
      subset="training"
)


#let's defined the directory for test data
test_datagen=test_datagen.flow_from_directory(
     base_dir,
     target_size=(224,224),
     #target size means image size
     batch_size=64,
     subset="validation"
)

In the output after defining the directory we will see:
Found 144 images belonging to 4 classes means,
it found total 4 classes and took 144 images for training
Found 12 images belonging to 4 classes,
it found total 4 classes and took 12 images for testing.

Let's create cnn model

cnn =tf.keras.Sequential()
#adding First convolution layer
cnn.add(tf.keras.layers.Conv2D( filters=64, padding="same", strides=2, kernel_size=3, activation="relu", input_shape=(224,224,3) ))
cnn.add(tf.keras.layers.MaxPool2D(pool_size=2,strides=2))

#adding Second convolution layer
cnn.add(tf.keras.layers.Conv2D( filters=32, padding="same", strides=2, kernel_size=3, activation="relu", ))
cnn.add(tf.keras.layers.MaxPool2D(pool_size=2,strides=2))

#adding Second convolution layer
cnn.add(tf.keras.layers.Conv2D( filters=32, padding="same", strides=2, kernel_size=3, activation="relu", ))
cnn.add(tf.keras.layers.MaxPool2D(pool_size=2))


cnn.add(tf.keras.layers.Flatten())
cnn.add(tf.keras.layers.Dense(4,activation="softmax"))
'''
Here we write 4, because we have total 4 classes and in the output layer we want 4 neurons. Here I used softmax because it is multiclass classification problem.
'''

Let's compile the model

cnn.compile(optimizer=tf.keras.optimizers.Adam(), loss="categorical_crossentropy", metrics=["accuracy"]) Here I used adam optimizer, loss as categorical_crossentropy, and metrics as accuracy

Let's fit the model

cnn.fit(train_datagen,epochs=10,validation_data=test_datagen)

CodersAim is created for learning and training a self learner to become a professional from beginner. While using CodersAim, you agree to have read and accepted our terms of use, privacy policy, Contact Us

© Copyright All rights reserved www.CodersAim.com. Developed by CodersAim.