0% found this document useful (0 votes)
4 views

Experiment 4

Uploaded by

HunTer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Experiment 4

Uploaded by

HunTer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

EXP 4.

IMAGE classification on MNIST dataset (CNN model with


fully connected layers)

AIM: IMAGE classification on MNIST dataset

Requirements:

Python (3.6 version or higher)/Anaconda

PROGRAM :

import numpy as np
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras.utils import to_categorical
from keras import backend as K

# Load data
(x_train, y_train), (x_test, y_test) = mnist.load_data()

# Process the data


img_rows, img_cols = 28, 28
if K.image_data_format() == 'channels_first':
x_train = x_train.reshape(x_train.shape[0], 1, img_rows,
img_cols)
x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols)
input_shape = (1, img_rows, img_cols)
else:
x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols,
1)
x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
input_shape = (img_rows, img_cols, 1)

x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255

y_train = to_categorical(y_train, 10)


y_test = to_categorical(y_test, 10)

# Create the model


model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3),
activation='relu',
input_shape=input_shape))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))

# Compile the model


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

# Train the model


batch_size = 128
epochs = 20
model.fit(x_train, y_train,
batch_size=batch_size,
epochs=epochs,
verbose=1,
validation_data=(x_test, y_test))

# Evaluate the model


score = model.evaluate(x_test, y_test, verbose=0)
print(f'Test loss: {score[0]}')
print(f'Test accuracy: {score[1]}')

# Predict

pred = model.predict(x_test[:5])
pred_classes = np.argmax(pred, axis=1)
true_classes = np.argmax(y_test[:5], axis=1)
print("Predicted:", pred_classes)
print("True Labels:", true_classes)

OUTPUT:
Epoch 1/10
469/469 ━━━━━━━━━━━━━━━━━━━━ 187s 395ms/step - accuracy: 0.1348 - loss:
2.2897 - val_accuracy: 0.3236 - val_loss: 2.2485
Epoch 2/10
469/469 ━━━━━━━━━━━━━━━━━━━━ 197s 384ms/step - accuracy: 0.2472 - loss:
2.2431 - val_accuracy: 0.4553 - val_loss: 2.1850
Epoch 3/10
469/469 ━━━━━━━━━━━━━━━━━━━━ 195s 369ms/step - accuracy: 0.3376 - loss:
2.1811 - val_accuracy: 0.5587 - val_loss: 2.0999
Epoch 4/10
469/469 ━━━━━━━━━━━━━━━━━━━━ 212s 391ms/step - accuracy: 0.4139 - loss:
2.0994 - val_accuracy: 0.6180 - val_loss: 1.9870
Epoch 5/10
469/469 ━━━━━━━━━━━━━━━━━━━━ 176s 376ms/step - accuracy: 0.4719 - loss:
1.9854 - val_accuracy: 0.6499 - val_loss: 1.8434
Epoch 6/10
469/469 ━━━━━━━━━━━━━━━━━━━━ 194s 360ms/step - accuracy: 0.5046 - loss:
1.8538 - val_accuracy: 0.6859 - val_loss: 1.6725
Epoch 7/10
469/469 ━━━━━━━━━━━━━━━━━━━━ 205s 368ms/step - accuracy: 0.5393 - loss:
1.7078 - val_accuracy: 0.7241 - val_loss: 1.4894
Epoch 8/10
469/469 ━━━━━━━━━━━━━━━━━━━━ 199s 362ms/step - accuracy: 0.5758 - loss:
1.5501 - val_accuracy: 0.7591 - val_loss: 1.3109
Epoch 9/10
469/469 ━━━━━━━━━━━━━━━━━━━━ 199s 355ms/step - accuracy: 0.6063 - loss:
1.4029 - val_accuracy: 0.7854 - val_loss: 1.1511
Epoch 10/10
469/469 ━━━━━━━━━━━━━━━━━━━━ 172s 368ms/step - accuracy: 0.6332 - loss:
1.2797 - val_accuracy: 0.8036 - val_loss: 1.0184
Test loss:
Test accuracy:

Predicted:
True Labels:

RESULT: Hence IMAGE classification on MNIST Dataset is observed and


obtained the output.

You might also like