0% found this document useful (0 votes)
9 views26 pages

DLL 4

This document loads and preprocesses image data for a facial recognition model. It defines functions to load the data, preprocess it by resizing and normalizing images, and encode the labels. It then builds a convolutional neural network model with Conv2D, MaxPooling2D, Flatten, and Dense layers. The model is compiled and trained on the data in batches over 5 epochs. Test loss and accuracy are calculated, and predictions are made on test data and visualized alongside true labels.

Uploaded by

Darshil Raj
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)
9 views26 pages

DLL 4

This document loads and preprocesses image data for a facial recognition model. It defines functions to load the data, preprocess it by resizing and normalizing images, and encode the labels. It then builds a convolutional neural network model with Conv2D, MaxPooling2D, Flatten, and Dense layers. The model is compiled and trained on the data in batches over 5 epochs. Test loss and accuracy are calculated, and predictions are made on test data and visualized alongside true labels.

Uploaded by

Darshil Raj
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/ 26

[46]:

import numpy as np

def load_and_preprocess_data(data_path):

img_path = os.path.join(person_dir_path, img_name)


img = cv2.imread(img_path)

img = cv2.resize(img, (224, 224))

1
img = img / 255.0

X.append(img)
y.append(idx)

X = np.array(X)
y = np.array(y)
y = to_categorical(y, num_classes=len(labels_dict) + 1)
return X, y, labels_dict

[47]:
X, y, labels_dict = load_and_preprocess_data(data_path)

[48]:

[ ]: y_adjusted
y_adjusted

2 Split the adjusted data into training and testing sets

[ ]:

[49]: def visualize_images(images, labels, num=3):


for i in range(num):
plt.imshow(images[i])
label_index = np.argmax(labels[i])
plt.title(f"Label: {label_index}")
plt.axis('off')
plt.show()
visualize_images(X, y)

2
3
[50]:
from keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout

def build_model(input_shape):
model = Sequential()

↪ input_shape=input_shape))

4
# Output layer
model.add(Dense(24, activation='softmax'))

return model
# Build the model
input_shape = (224, 224, 3) # Width, Height, Channels
model = build_model(input_shape)

# Print model summary


model.summary()

Model: "sequential_3"
Layer (type) Output Shape Param #
=================================================================
conv2d_6 (Conv2D) (None, 222, 222, 32) 896

max_pooling2d_6 (MaxPoolin (None, 111, 111, 32) 0


g2D)

conv2d_7 (Conv2D) (None, 109, 109, 64) 18496

max_pooling2d_7 (MaxPoolin (None, 54, 54, 64) 0


g2D)

flatten_3 (Flatten) (None, 186624) 0

dense_6 (Dense) (None, 128) 23888000

dropout_3 (Dropout) (None, 128) 0

dense_7 (Dense) (None, 24) 3096

=================================================================
Total params: 23910488 (91.21 MB)
Trainable params: 23910488 (91.21 MB)
Non-trainable params: 0 (0.00 Byte)

[51]: compile(optimizer
metrics

[52]:
batch_size

Epoch 1/5

5
54/54 [==============================] - 32s 571ms/step - loss: 1.9974 -
accuracy: 0.5596 - val_loss: 0.1179 - val_accuracy: 0.9836
Epoch 2/5
54/54 [==============================] - 28s 516ms/step - loss: 0.2164 -
accuracy: 0.9363 - val_loss: 0.0153 - val_accuracy: 0.9977
Epoch 3/5
54/54 [==============================] - 25s 464ms/step - loss: 0.1014 -
accuracy: 0.9713 - val_loss: 0.0019 - val_accuracy: 1.0000
Epoch 4/5
54/54 [==============================] - 28s 522ms/step - loss: 0.0545 -
accuracy: 0.9854 - val_loss: 0.0024 - val_accuracy: 0.9977
Epoch 5/5
54/54 [==============================] - 29s 530ms/step - loss: 0.0607 -
accuracy: 0.9854 - val_loss: 4.2501e-04 - val_accuracy: 1.0000

[53]:
print("Test Loss:", test_loss)

17/17 [==============================] - 2s 110ms/step - loss: 3.4294e-04 -


accuracy: 1.0000
Test Loss: 0.00034293910721316934
Test Accuracy: 1.0

3 Make predictions on test data

[54]: y_pred = model.predict(X_test)


argmax(y_test,

17/17 [==============================] - 2s 103ms/step

4 Visualize some test samples, true labels, and predictions

[56]:

6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

You might also like