Sunteți pe pagina 1din 15

“IMAGE CLASSIFICATION USING NEURAL

NETWORKS”

Project Report

Submitted For The Course: Neural Networks And Fuzzy Controls(ECE3009)

17BEC0181 K. Naga Kaushik

17BEC0479 V.B.Srivathsava

17BEC0905 P.Tushar Sai Surya

Slot:G1+TG1

Name Of Faculty: Sankar Ganesh

(School of Electronics Engineering)


Table of contents

1. Abstract ................................................................................ 1

2. Introduction .......................................................................... 2

3. Methodology ....................................................................... 3

4. Algorithms............................................................................. 4

5. Results ................................................................................. 10

6. Inference.............................................................................. 13
Abstract:
The intent of the classification process is to categorize all pixels in a digital
image into one of several land cover classes, or "themes". This categorized
data may then be used to produce thematic maps of the land cover present
in an image. Normally, multispectral data are used to perform the
classification and, indeed, the spectral pattern present within the data for
each pixel is used as the numerical basis for categorization (Lillesand and
Kiefer, 1994). The objective of image classification is to identify and
portray, as a unique gray level (or color), the features occurring in an image
in terms of the object or type of land cover these features actually represent
on the ground.

Image classification is perhaps the most important part of digital image


analysis. It is very nice to have a "pretty picture" or an image, showing a
magnitude of colors illustrating various features of the underlying terrain,
but it is quite useless unless to know what the colors mean. (PCI, 1997).
Two main classification methods are Supervised
Classification and Unsupervised Classification.

Deep learning is a vast field so we’ll narrow our focus a bit and take up the
challenge of solving an Image Classification project. Additionally, we’ll be
using a Convolutional Neural Network(CNN) to achieve a pretty impressive
accuracy score.

You can consider the Python code we’ll see in this project as a benchmark
for building Image Classification models
Introduction:
This part gives us the introduction about some terms which we will be using
in our CNN creation and training.

Conv2D the layer to convolve the image


into multiple images
Activation the activation function.
MaxPooling2D used to max pool the value from
the given size matrix and same
is used for the next 2 layers.
then, Flatten is used to flatten
the dimensions of the image
obtained after convolving it.
Dense used to make this a fully
connected model and is the
hidden layer.
Dropout used to avoid overfitting on the
dataset.
Dense the output layer contains only
one neuron which decide to
which category image belongs.
_train the function that is used to
prepare data from the
train_dataset directory
Target_size specifies the target
size of the image.
_test used to prepare test data for the
model and all is similar as
above.
epochs tells us the number of times
model will be trained in forward
and backward pass.
.

Methodology:
Stage 1 & 2: Loading and pre-processing the data
Data is gold as far as deep learning models are concerned. Your image
classification model has a far better chance of performing well if you have a
good amount of images in the training set. Also, the shape of the data
varies according to the architecture/framework that we use.

But we are not quite there yet. In order to see how our model performs on
unseen data (and before exposing it to the test set), we need to create a
validation set. This is done by partitioning the training set data.

In short, we train the model on the training data and validate it on the
validation data. Once we are satisfied with the model’s performance on the
validation set, we can use it for making predictions on the test data.

Stage 3 & 4: Defining the model’s architecture


This is another crucial step in our deep learning model building process.
We have to define how our model will look and that requires answering
questions like:

How many convolutional layers do we want?

What should be the activation function for each layer?

How many hidden units should each layer have?

And many more. These are essentially the hyperparameters of the model
which play a MASSIVE part in deciding how good the predictions will be.

Stage 5: Training the model


For training the model, we require:

Training images and their corresponding true labels


Validation images and their corresponding true labels (we use these labels
only to validate the model and not during the training phase)

We also define the number of epochs in this step. For starters, we will run
the model for 10 epochs (you can change the number of epochs later).

Stage 6: Estimating the model’s performance


Finally, we load the test data (images) and go through the pre-processing
step here as well. We then predict the classes for these images using the
trained model.

Algorithms:
Task 1: Import Libraries
In [ ]:

import tensorflow as tf
import os
import numpy as np

from matplotlib import pyplot as plt


%matplotlib inline

if not os.path.isdir('models'):
os.mkdir('models')

print('TensorFlow version:', tf. version )


print('Is using GPU?', tf.test.is_gpu_available())
Task 2: Preprocess Data

In [ ]:

def get_three_classes(x, y):


def indices_of(class_id):
indices, _ = np.where(y == float(class_id))
return indices

indices = np.concatenate([indices_of(0), indices_of(1), indices_of(2)],


axis=0)

x = x[indices]
y = y[indices]

count = x.shape[0]
indices = np.random.choice(range(count), count, replace=False)

x = x[indices]
y = y[indices]

y = tf.keras.utils.to_categorical(y)

return x, y

In [ ]:

(x_train, y_train), (x_test, y_test) = tf.keras.datasets.cifar10.load_data()

x_train, y_train = get_three_classes(x_train, y_train)


x_test, y_test = get_three_classes(x_test, y_test)

print(x_train.shape, y_train.shape)
print(x_test.shape, y_test.shape)

Task 3: Visualize Examples

In [ ]:
class_names = ['aeroplane', 'car', 'bird']

def show_random_examples(x, y, p):


indices = np.random.choice(range(x.shape[0]), 10, replace=False)

x = x[indices]
y = y[indices]
p = p[indices]

plt.figure(figsize=(10, 5))
for i in range(10):
plt.subplot(2, 5, i + 1)
plt.imshow(x[i])
plt.xticks([])
plt.yticks([])
col = 'green' if np.argmax(y[i]) == np.argmax(p[i]) else 'red'
plt.xlabel(class_names[np.argmax(p[i])], color=col)

plt.show()

show_random_examples(x_train, y_train, y_train)

In [ ]:
show_random_examples(x_test, y_test, y_test)
Task 4: Create Model

In [ ]:
from tensorflow.keras.layers import Conv2D, MaxPooling2D,
BatchNormalization
from tensorflow.keras.layers import Dropout, Flatten, Input, Dense

def create_model():

def add_conv_block(model, num_filters):

model.add(Conv2D(num_filters,3, activation='relu', padding='same'))


model.add(BatchNormalization())
model.add(Conv2D(num_filters, 3, activation='relu', padding='valid'))
model.add(MaxPooling2D(pool_size=2))
model.add(Dropout(0.2))

return model

model = tf.keras.models.Sequential()
model.add(Input(shape=(32, 32, 3)))

model = add_conv_block(model, 32)


model = add_conv_block(model, 64)
model = add_conv_block(model, 128)

model.add(Flatten())
model.add(Dense(3, activation='softmax'))

model.compile(loss='categorical_crossentropy',optimizer='adam',
metrics=['accuracy'])
return model

model = create_model()
model.summary()

Task 5: Train the Model

In [ ]:

%%time

h = model.fit(
x_train/255., y_train,
validation_data=(x_test/255., y_test),
epochs=20, batch_size=256,
callbacks=[

tf.keras.callbacks.EarlyStopping(monitor='val_accuracy',
patience=2),

tf.keras.callbacks.ModelCheckpoint('models/model_{val_acc
uracy:.3f}.h5', save_best_only=True,

save_weights_only=False, monitor='val_accuracy’)]
)
Task 6: Final Predictions
In [ ]:
losses = h.history['loss']
accs = h.history['accuracy']
val_losses = h.history['val_loss']
val_accs = h.history['val_accuracy']
epochs = len(losses)

plt.figure(figsize=(12, 4))
for i, metrics in enumerate(zip([losses, accs], [val_losses,
val_accs], ['Loss', 'Accuracy'])):
plt.subplot(1, 2, i + 1)
plt.plot(range(epochs), metrics[0], label='Training
{}'.format(metrics[2])) plt.plot(range(epochs), metrics[1],
label='Validation {}'.format(metrics[2]))
plt.legend()
plt.show()

In [ ]:
model = tf.keras.models.load_model('models/model_0.913.h5')
preds = model.predict(x_test/255.)

In [ ]:
show_random_examples(x_test, y_test, preds)
Results:

Task 1: Import Libraries


First of all, we need to import all the libraries from TensorFlow

Task 2: Preprocess Data


The Data Set contains 60K images for training and another 10K images for
testing. Due to the time constraint we will select a subset from the data set
for our project.

Task 3: Visualize Examples


Here, we are visualizing the training and testing sets to check if they correct
or not.

Train Set Examples:


Testing Set Examples:

Task 4: Create Model


This is the main part where we design our CNN.
Task 5: Train the Model
In this task, we train our CNN model.
Task 6: Final Predictions
This the Final result for our Project. Here, the maximum accuracy our CNN
able to get is 88%. Hence, our results are mixed.

Inference:
In This way we can be able to build a CNN network and train it to do Image
Classification.

S-ar putea să vă placă și