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

docs

The document outlines a Python script for creating a dataset of images related to various identification documents and training a convolutional neural network (CNN) to classify them. It includes steps for data preprocessing, model architecture, training, and evaluation, achieving an accuracy of approximately 96.55% on the test set. The model is saved for future use and can predict labels for new images.

Uploaded by

ananyagoyal2504
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)
2 views

docs

The document outlines a Python script for creating a dataset of images related to various identification documents and training a convolutional neural network (CNN) to classify them. It includes steps for data preprocessing, model architecture, training, and evaluation, achieving an accuracy of approximately 96.55% on the test set. The model is saved for future use and can predict labels for new images.

Uploaded by

ananyagoyal2504
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/ 7

docs

September 30, 2024

[2]: import os
import cv2
import numpy as np

CLASSES_LIST = [
("aadhar", "C:/Users/Ananya Gupta/Downloads/aadhar_card"),
("pan", "C:/Users/Ananya Gupta/Downloads/pancard"),
("voter", "C:/Users/Ananya Gupta/Downloads/voterid"),
("dl", "C:/Users/Ananya Gupta/Downloads/dl")
]

IMG_SIZE = 224

# Creating dataset
def create_dataset():
s = []
labels = []
for class_index, (class_name, folder_path) in enumerate(CLASSES_LIST):
files_list = os.listdir(folder_path)
for file_name in files_list:
path = os.path.join(folder_path, file_name)
try:
image = cv2.resize(cv2.imread(path), (IMG_SIZE, IMG_SIZE))
s.append(np.array(image))
labels.append(class_index)
except Exception as e:
print(f"Error processing file {file_name} in {class_name}: {e}")
continue
features = np.asarray(s)
labels = np.array(labels)
return features, labels

# Example usage
features, labels = create_dataset()

Import tensorflow as tf from tensorflow.keras import datasets,layers,models import mat-


plotlib.pyplot as plt import numpy as np import os import cv2 from tensorflow.keras.models import
Sequential from tensorflow.keras.callbacks import EarlyStopping from tensorflow.keras.utils import

1
to_categorical
[4]: import tensorflow as tf
from tensorflow.keras import datasets,layers,models
import matplotlib.pyplot as plt
import numpy as np
import os
import cv2
from tensorflow.keras.models import Sequential
from tensorflow.keras.callbacks import EarlyStopping
from tensorflow.keras.utils import to_categorical
import random
seed_constant=27
np.random.seed(seed_constant)
random.seed(seed_constant)
tf.random.set_seed(seed_constant)

[5]: print(features.shape)
print(labels.shape)

(1545, 224, 224, 3)


(1545,)

[6]: one_hot_encoded_labels = to_categorical(labels)

[7]: labels

[7]: array([0, 0, 0, …, 3, 3, 3])

[8]: from sklearn.model_selection import train_test_split


features_train, features_test, labels_train, labels_test =␣
↪train_test_split(features, one_hot_encoded_labels,


↪test_size = 0.056, shuffle = True,

↪random_state=seed_constant
)

[9]: labels_train
y_classes = [np.argmax(element) for element in labels_test]
y_classes[:5]

[9]: [2, 2, 1, 2, 0]

[10]: print(len(labels_train))
print(len(features_test))

2
1458
87

[11]: features_train=features_train/255
features_test=features_test/255

[12]: # Data Augmentation


data_augmentation = tf.keras.Sequential([
layers.experimental.preprocessing.
↪RandomFlip("horizontal_and_vertical"),

])
data_augmentation

[12]: <keras.engine.sequential.Sequential at 0x194a5e39420>

[21]: cnn = models.Sequential([


layers.Conv2D(filters=32, kernel_size=(3, 3), activation='relu', ␣
↪strides=(2, 2), padding="same", input_shape=(224, 224, 3)),

layers.MaxPooling2D((2,2)),
layers.Dropout(0.10),
layers.Conv2D(64 ,(3, 3), activation='relu'),
layers.MaxPooling2D((2,2)),
layers.Dropout(0.10),
layers.Conv2D(128, (3, 3), activation='relu'),
layers.MaxPooling2D((2,2)),
layers.Dropout(0.10),
layers.Conv2D(256, (3, 3), activation='relu'),
layers.MaxPooling2D((2,2)),
layers.Conv2D(512, (3, 3), activation='relu'),

layers.Flatten(),
layers.Dense(512,activation='relu'),
layers.Dropout(0.07),
layers.Dense(256,activation='relu'),
layers.Dropout(0.07),
layers.Dense(128,activation='relu'),
layers.Dropout(0.07),
layers.Dense(64,activation='relu'),
layers.Dropout(0.07),
layers.Dense(4, activation='softmax')
])

[22]: cnn.summary()

Model: "sequential_2"
_________________________________________________________________

3
Layer (type) Output Shape Param #
=================================================================
conv2d_5 (Conv2D) (None, 112, 112, 32) 896

max_pooling2d_4 (MaxPooling (None, 56, 56, 32) 0


2D)

dropout_7 (Dropout) (None, 56, 56, 32) 0

conv2d_6 (Conv2D) (None, 54, 54, 64) 18496

max_pooling2d_5 (MaxPooling (None, 27, 27, 64) 0


2D)

dropout_8 (Dropout) (None, 27, 27, 64) 0

conv2d_7 (Conv2D) (None, 25, 25, 128) 73856

max_pooling2d_6 (MaxPooling (None, 12, 12, 128) 0


2D)

dropout_9 (Dropout) (None, 12, 12, 128) 0

conv2d_8 (Conv2D) (None, 10, 10, 256) 295168

max_pooling2d_7 (MaxPooling (None, 5, 5, 256) 0


2D)

conv2d_9 (Conv2D) (None, 3, 3, 512) 1180160

flatten_1 (Flatten) (None, 4608) 0

dense_5 (Dense) (None, 512) 2359808

dropout_10 (Dropout) (None, 512) 0

dense_6 (Dense) (None, 256) 131328

dropout_11 (Dropout) (None, 256) 0

dense_7 (Dense) (None, 128) 32896

dropout_12 (Dropout) (None, 128) 0

dense_8 (Dense) (None, 64) 8256

dropout_13 (Dropout) (None, 64) 0

4
dense_9 (Dense) (None, 4) 260

=================================================================
Total params: 4,101,124
Trainable params: 4,101,124
Non-trainable params: 0
_________________________________________________________________

[23]: from tensorflow.keras.callbacks import EarlyStopping

[24]: early_stopping_callback = EarlyStopping(monitor = 'val_loss', patience = 4,␣


↪mode = 'min', restore_best_weights = True)

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

[26]: history = cnn.fit(features_train,labels_train,epochs=40, batch_size = 32␣


↪,shuffle = True,validation_split = 0.25,callbacks=[early_stopping_callback])

Epoch 1/40
35/35 [==============================] - 12s 350ms/step - loss: 0.0439 -
accuracy: 0.9854 - val_loss: 0.0655 - val_accuracy: 0.9753
Epoch 2/40
35/35 [==============================] - 16s 468ms/step - loss: 0.0486 -
accuracy: 0.9826 - val_loss: 0.1359 - val_accuracy: 0.9644
Epoch 3/40
35/35 [==============================] - 16s 445ms/step - loss: 0.0491 -
accuracy: 0.9817 - val_loss: 0.0749 - val_accuracy: 0.9781
Epoch 4/40
35/35 [==============================] - 15s 423ms/step - loss: 0.0754 -
accuracy: 0.9771 - val_loss: 0.1258 - val_accuracy: 0.9616
Epoch 5/40
35/35 [==============================] - 15s 420ms/step - loss: 0.0677 -
accuracy: 0.9790 - val_loss: 0.0557 - val_accuracy: 0.9808
Epoch 6/40
35/35 [==============================] - 14s 392ms/step - loss: 0.0508 -
accuracy: 0.9872 - val_loss: 0.2489 - val_accuracy: 0.9534
Epoch 7/40
35/35 [==============================] - 14s 393ms/step - loss: 0.0634 -
accuracy: 0.9844 - val_loss: 0.1506 - val_accuracy: 0.9671
Epoch 8/40
35/35 [==============================] - 14s 405ms/step - loss: 0.1827 -
accuracy: 0.9405 - val_loss: 0.2319 - val_accuracy: 0.9370
Epoch 9/40
35/35 [==============================] - 14s 399ms/step - loss: 0.1581 -
accuracy: 0.9515 - val_loss: 0.1408 - val_accuracy: 0.9726

5
[27]: cnn.evaluate(features_test,labels_test)#bs=4

3/3 [==============================] - 0s 55ms/step - loss: 0.1270 - accuracy:


0.9655

[27]: [0.12695112824440002, 0.9655172228813171]

[28]: from keras.models import load_model

cnn.save('model61.h5') # creates a HDF5 file 'my_model.h5'


#del model # deletes the existing model

# returns a compiled model


# identical to the previous one
model = load_model('model61.h5')

[29]: model.evaluate(features_test,labels_test)

3/3 [==============================] - 0s 70ms/step - loss: 0.1270 - accuracy:


0.9655

[29]: [0.12695112824440002, 0.9655172228813171]

[30]: y_pred = cnn.predict(features_test)

3/3 [==============================] - 0s 80ms/step

[31]: y_classes = [np.argmax(element) for element in y_pred]


y_classes[:5]

[31]: [2, 2, 1, 2, 2]

[35]: import cv2


import numpy as np

filename = r"C:\Users\Ananya Gupta\Downloads\12.jpg"


IMG_SIZE = 224

# Load and preprocess the image


image = cv2.imread(filename)
image = cv2.resize(image, (IMG_SIZE, IMG_SIZE)) # Resize to 224x224
image = np.expand_dims(image, axis=0) # Add batch dimension

# Normalize the image (if needed, depending on how your model was trained)
# Assuming your model was trained on normalized data

# Predict the label


label = cnn.predict(image)

6
print(label)

1/1 [==============================] - 0s 21ms/step


[[0. 1. 0. 0.]]

You might also like