CV Lab 7

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 4

DEPARTMENT OF INFORMATION TECHNOLOGY, QUEST NAWABSHAH

Computer Vision, 21 BS(AI) BATCH

Name: _____________________________ Roll No: __________________________________

Signature of Lab Tutor: ____________________ Date: ________________________________

LAB 7: Image Classification using Neural Networks

OBJECTIVES

After completion of this lab, students will be familiar with:


 Neural Network
 Design of Basic Neural Network
 Training a Neural Network

Introduction to Neural Networks

The idea of machine learning is to build systems that given an input are able to predict the
correct output with respect to a certain predefined context. For example, we want to build an
image classification system, which is capable of telling the users what the given input image is
of.

Figure 1: A neural network takes in an image and returns what that image is of

Design of a basic neural network

Neural networks are made up of several neurons connected to each other. Neural networks are
made up of programmatically designed neurons that are connected to each other. We call the
programmatically designed neurons as perceptrons.
Perceptrons form the atomic unit of neural networks. They take as input a set of numbers (one or
many), multiply the input with some weights, and return an output.

Figure 2: A perceptron

1
DEPARTMENT OF INFORMATION TECHNOLOGY, QUEST NAWABSHAH
Computer Vision, 21 BS(AI) BATCH
In Figure 2, a perceptron takes input as three values and returns an output. The most usual
operation in each input is multiplied by a weight and the normalized sum of all products is
returned. Depending on the application and the data, the perceptron can be programmed to
perform other operations. For example, after the sum of the products is calculated, the perceptron
returns a 1 or a 0 if the sum is over a certain threshold.

Figure 3: An example of a neural network

Here, each of the individual circles represents a perceptron. Each perceptron takes in a set of
inputs and returns an output to the next layer of perceptron. What is in Figure 3 is just one way
of creating a neural network. Each perceptron in a particular layer is connected to all the
perceptrons in the previous layer. This is called a fully connected neural network.
The first layer of a neural network is usually called the input layer and similarly the last layer of
the neural network is called the output layer. All other layers between them are known as the
hidden layers. An input layer and an output layer is a must in a network but the number of hidden
layers can vary from zero to as many as you want. The size of the input layer is decided by the
size of the images. If an image size is 28 x 28, then the size of the input layer will be 784 (28 *
28). The size of hidden layer is decided by the user. Finally, the size of the output layer depends
on the number of labels we have.

Training a Network

After network design, we can start training the network. The training phase of any neural
network comprises two parts: first, feed forward the input and second, backpropagate the error.
Feed forward means to take the input and pass it through the perceptrons in our network and
calculate the output using the perceptrons. The input values are multiplied with the weights
values of the perceptrons and an output is generated.
While backpropagating, we take the feed forward output (from the last step) and find its
difference from the actual output (ground truth). Using this error, we modify the weights of the
perceptron.

Example 1:

from sklearn.datasets import fetch_openml


from sklearn.neural_network import MLPClassifier
from sklearn.preprocessing import normalize
from sklearn.model_selection import train_test_split

#Get MNIST Dataset

2
DEPARTMENT OF INFORMATION TECHNOLOGY, QUEST NAWABSHAH
Computer Vision, 21 BS(AI) BATCH
print('Getting MNIST Data...')
#mnist = fetch_mldata('MNIST original')
images, labels = fetch_openml('mnist_784', version=1, return_X_y=True, as_frame=False)
print(images.shape, labels.shape)
print('MNIST Data downloaded!')

#Preprocess the images


images = normalize(images, norm='l2') #You can use l1 norm too
#Split the data into training set and test set
images_train, images_test, labels_train, labels_test = train_test_split(images, labels,
test_size=0.25, random_state=21)

#Setup the neural network that we want to train on


nn = MLPClassifier(hidden_layer_sizes=(200), max_iter=30, solver='sgd',
learning_rate_init=0.001, verbose=True)

#Start training the network


print('NN Training started...')
nn.fit(images_train, labels_train)
print('NN Training completed!')

#Evaluate the performance of the neural network on test data


print('Network Performance: %f' % nn.score(images_test, labels_test))

Exercise:

1. Increase the size of the hidden layer from 200 to 300.


2. Decrease the size of the hidden layer from 200 to 100.
3. Increase the number of hidden layers from 1 to 3 or more.
4. Read the following images from the dataset, apply the neural network and classify the
images into Dogs and Cats.

import cv2
import numpy as np
from sklearn.neural_network import MLPClassifier
from sklearn.preprocessing import normalize
from sklearn.model_selection import train_test_split

#read the data from the folders


X,y = [],[]
for i in range(51):
img = cv2.imread('F:\\QUEST\\PetImages\\Cat\\' + str(i) + '.jpg')
img = cv2.resize(img,(64,64))
X.append(img)
y.append(0)
img2 = cv2.imread('F:\\QUEST\\PetImages\\Dog\\' + str(i) + '.jpg')
img2 = cv2.resize(img,(64,64))
X.append(img2)

3
DEPARTMENT OF INFORMATION TECHNOLOGY, QUEST NAWABSHAH
Computer Vision, 21 BS(AI) BATCH
y.append(1)

#Split the data into training set and test set


images_train, images_test, labels_train, labels_test = train_test_split(X, y, test_size=0.1,
random_state=17)

#Reshape and normalize the data


x_train = np.array(images_train)
y_train = np.array(labels_train)
y_train = y_train.reshape(-1, 1)

x_test = np.array(images_test)
y_test = np.array(labels_test)
y_test = y_test.reshape(-1, 1)

print(x_train.shape)
nsamples, nx, ny,c = x_train.shape
x_train = x_train.reshape((nsamples,nx*ny*c))
# x_train_flatten = x_train.reshape(x_train.shape[0], -1).T
x_train = x_train / 255

testsamples, nxt, nyt,ct = x_test.shape


x_test = x_test.reshape((testsamples,nxt*nyt*ct))
x_test = x_test / 255

#setup the neural network


nn3 = MLPClassifier(hidden_layer_sizes=(200), max_iter=30, activation='tanh',
solver='sgd', learning_rate_init=0.001, verbose=True)

#Start training the network


print('NN Training started...')
nn3.fit(x_train, y_train)
print('NN Training completed!')

#Evaluate the performance of the neural network on test data


print('Network Performance: %f' % nn3.score(x_test, y_test))

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy