0% found this document useful (0 votes)
4 views3 pages

DL 6

The document outlines the implementation of a transfer learning concept for image classification using a convolutional neural network (CNN) with the MNIST dataset. It includes data preprocessing steps, model architecture, training, and evaluation, achieving a test accuracy of approximately 98.78%. Key libraries used are Keras and TensorFlow for building and training the model.

Uploaded by

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

DL 6

The document outlines the implementation of a transfer learning concept for image classification using a convolutional neural network (CNN) with the MNIST dataset. It includes data preprocessing steps, model architecture, training, and evaluation, achieving a test accuracy of approximately 98.78%. Key libraries used are Keras and TensorFlow for building and training the model.

Uploaded by

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

6.

IMPLEMENTATION OF A TRANSFER LEARNING CONCEPT


IN
IMAGE CLASSIFICATION.

import numpy as np # linear algebra


import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
import keras
from keras.models import Sequential
from keras.layers import Conv2D, Dense, MaxPool2D, Dropout, Flatten
from keras.optimizers import Adam
from keras.preprocessing.image import ImageDataGenerator
from keras.callbacks import ReduceLROnPlateau
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
import seaborn as sns

df_train = pd.read_csv('train.csv')
X_train = df_train.iloc[:, 1:]
Y_train = df_train.iloc[:, 0]

X_train.head()

pixel0 pixel1 pixel2 pixel3 pixel4 pixel5 pixel6 pixel7 pixel8 pixel9 ... pixel774 pi

0 0 0 0 0 0 0 0 0 0 0 ... 0.0

1 0 0 0 0 0 0 0 0 0 0 ... 0.0

2 0 0 0 0 0 0 0 0 0 0 ... 0.0

3 0 0 0 0 0 0 0 0 0 0 ... 0.0

4 0 0 0 0 0 0 0 0 0 0 ... 0.0

5 rows × 784 columns

Y_train.head()

0 1
1 0
2 1
3 4
4 0
Name: label, dtype: int64
import tensorflow as tf
from tensorflow.keras import layers, models
from tensorflow.keras.datasets import mnist
from tensorflow.keras.utils import to_categorical
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()

train_images = train_images.reshape((60000, 28, 28, 1)).astype('float32') / 255


test_images = test_images.reshape((10000, 28, 28, 1)).astype('float32') / 255

train_labels = to_categorical(train_labels)
test_labels = to_categorical(test_labels)

train_labels

array([[0., 0., 0., ..., 0., 0., 0.],


[1., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.],
...,
[0., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 1., 0.]], dtype=float32)

model = models.Sequential([
layers.Conv2D(6, (5, 5), activation='relu', input_shape=(28, 28, 1)),
layers.MaxPooling2D((2, 2)),
layers.Conv2D(16, (5, 5), activation='relu'),
layers.MaxPooling2D((2, 2)),
layers.Flatten(),
layers.Dense(120, activation='relu'),
layers.Dense(84, activation='relu'),
layers.Dense(10, activation='softmax')
])

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

# Train the model


model.fit(train_images, train_labels, epochs=5, batch_size=64, validation_split=0.2)

# Evaluate the model


test_loss, test_acc = model.evaluate(test_images, test_labels)
print('Test accuracy:', test_acc)

Epoch 1/5
750/750 [==============================] - 22s 28ms/step - loss: 0.2750 - accuracy: 0.9172 - val_lo
Epoch 2/5
750/750 [==============================] - 21s 28ms/step - loss: 0.0771 - accuracy: 0.9769 - val_lo
Epoch 3/5
750/750 [==============================] - 22s 29ms/step - loss: 0.0561 - accuracy: 0.9820 - val_lo
Epoch 4/5
750/750 [==============================] - 21s 28ms/step - loss: 0.0440 - accuracy: 0.9860 - val_lo
Epoch 5/5
750/750 [==============================] - 22s 29ms/step - loss: 0.0358 - accuracy: 0.9885 - val_lo
313/313 [==============================] - 2s 6ms/step - loss: 0.0364 - accuracy: 0.9878
Test accuracy: 0.9878000020980835

You might also like