Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 36

Summary of Classification

using Tensorflow

Anam Sadaf
FA23-REE-002
Introduction

This tutorial shows how to classify images of flowers using a tf.keras.Sequential model and
load data using tf.keras.utils.image_dataset_from_directory. It demonstrates the following concepts:
•Efficiently loading a dataset off disk.
•Identifying overfitting and applying techniques to mitigate it, including data augmentation
and dropout.
Outline

This tutorial follows a basic machine learning workflow:

1.Examine and understand data


2.Build an input pipeline
3.Build the model
4.Train the model
5.Test the model
6.Improve the model and repeat the process
Main Code

 First step is setting up a work environment in


which we import necessary libraries and files
required for the project
Exploring the Dataset

 Load data and visualize it


 Check if there is any preprocessing
required to make data more useful for the
architecture.
 dataset is photos of flowers.
 Image_count returns number of available
images in dataset = 3,670
Class Labels

 Classes in dataset
flower_photo/
daisy/
dandelion/
roses/
sunflowers/
tulips/
Load data using a Keras utility

 Load images using keras API


 Using
tf.keras.utils.image_dataset_from_directo
ry utility.
Create a dataset
 Next,split dataset into training
and validation data
 Use 80% of the images for
training and 20% for validation.
Visualize the training Dataset

 Plot different images from


training folder to visualize the
data and to verify respective class
labels
Visualize the training Dataset
 Manually iterate over the dataset
and retrieve batches of images
Configure the dataset for performance
 Dataset.cache
 keeps the images in memory after
they're loaded off disk during the
first epoch. This will ensure the
dataset does not become a
bottleneck while training your
model. If your dataset is too large to
fit into memory, you can also use
this method to create a performant
on-disk cache.
 The RGB channel values are in
the [0 255] This is not ideal for a
neural network; in general you
should seek to make your input
values small.
 standardize values to be in the [0 1]
Model Architecture
 Convolutional Neural Network
CNN Architecture
 Convolutional layer:
 Convolutional layers perform convolutions, which are operations where a filter is moved over an
input image, calculating the values in a resulting feature map.
 A convolutional layer is usually built up of multiple filters, which will produce multiple feature
maps. During training of the CNN, the model will learn what weights to apply to the different
feature maps and, hence, be able to recognize which features to extract from the input images.
Convolutional layer
CNN Architecture
 By increasing the number of convolutional layers in the CNN, the model will be able to detect
more complex features in an image.
 Pooling Layer
A pooling layer is a layer that reduces the computational cost of the model
and helps fight overfitting by reducing the dimensionality of its input. There are
different types of pooling layers:
Max pooling: Select the largest value in the matrix
Min pooling: Select the smallest value in the matrix
Average pooling: Select the average of the values in the matrix
Pooling Layer
 In a pooling layer, a filter is applied to the different areas of an image. The window size
and stride will decide the size of the output and how the filter is moved over the input
matrix.
CNN Architecture

 Fully Connected Layer


A fully connected layer transforms its input to the desired output format.
In a classification task, this typically includes converting a matrix of image
features into a 1xC vector where C is the number of classes.
 Softmax Layer
A softmax layer is most commonly applied after the fully connected
layers. This layer takes a vector of size 1xC as an input, where C is the
number of classes and all of the numbers add up to 1.
 The softmax layer then uses this vector and creates a new vector where each of the inputs
represents a probability for the image to be of that particular class. A softmax is therefore
mostly used in classification tasks.
CNN Architecture
 Dropout Layer
 By using dropout layer, in every iteration, you will work on a smaller neural network than the
previous one and therefore, it approaches regularization.
 Dropout helps in shrinking the squared norm of the weights and this tends to a reduction in
overfitting.
Create & Compile the model
Create & Compile the model
 The goal of the optimizer is to
find the values of the model
parameters (weights and biases)
that minimize the cost function.
 Model is compiled using ‘adam’
optimizer.
 Adam stands for Adaptive
Moment Estimation, which
means that it adapts the learning
rate of each parameter based on
its historical gradients and
momentum
Train the model
Summary of Model
Visualize training results
 Create plots of the loss and accuracy on
the training and validation sets.
Visualize training results
 The plots show that training accuracy and
validation accuracy are off by large margins,
and the model has achieved only around 60%
accuracy on the validation set.
Overfitting
Data Augmentation
 Overfitting generally occurs when
there are a small number of
training examples.
 Data augmentation takes the
approach of generating additional
training data from your existing
examples by augmenting them
using random transformations that
yield believable-looking images.
This helps expose the model to
more aspects of the data and
generalize better.
Data Augmentation
Dropout
 Another technique to reduce overfitting
is to introduce dropout regularization to
the network.
 When you apply dropout to a layer, it
randomly drops out (by setting the
activation to zero) a number of output
units from the layer during the training
process. Dropout takes a fractional
number as its input value, in the form
such as 0.1, 0.2, 0.4, etc. This means
dropping out 10%, 20% or 40% of the
output units randomly from the applied
layer.
Model Summary after Data augmentation and
by add dropout layer
Model Summary after Data augmentation and
by add dropout layer
Visualizing Training Results
Visualizing Training Results
Predict on new data
Thank You!

You might also like