DLL 4
DLL 4
import numpy as np
def load_and_preprocess_data(data_path):
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
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)
Model: "sequential_3"
Layer (type) Output Shape Param #
=================================================================
conv2d_6 (Conv2D) (None, 222, 222, 32) 896
=================================================================
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)
[56]:
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26