forked from Sant60/ASL-Project
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain_model.py
More file actions
69 lines (58 loc) · 1.71 KB
/
train_model.py
File metadata and controls
69 lines (58 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# =============================
# File: train_model.py
# =============================
import os
import numpy as np
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Conv2D, MaxPooling2D, Flatten, Dropout
from tensorflow.keras.optimizers import Adam
# CONFIGURATION
data_path = "HandSignsDataset" # Folder with subfolders: M, N, R, T, U, V, etc.
img_size = 224
batch_size = 32
epochs = 15
# DATA GENERATOR
datagen = ImageDataGenerator(rescale=1. / 255, validation_split=0.2)
train_data = datagen.flow_from_directory(
data_path,
target_size=(img_size, img_size),
batch_size=batch_size,
class_mode='categorical',
subset='training',
shuffle=True
)
val_data = datagen.flow_from_directory(
data_path,
target_size=(img_size, img_size),
batch_size=batch_size,
class_mode='categorical',
subset='validation'
)
# MODEL DEFINITION
model = Sequential([
Conv2D(32, (3, 3), activation='relu', input_shape=(img_size, img_size, 3)),
MaxPooling2D(2, 2),
Dropout(0.25),
Conv2D(64, (3, 3), activation='relu'),
MaxPooling2D(2, 2),
Dropout(0.25),
Flatten(),
Dense(128, activation='relu'),
Dropout(0.5),
Dense(train_data.num_classes, activation='softmax')
])
model.compile(optimizer=Adam(), loss='categorical_crossentropy', metrics=['accuracy'])
# TRAINING
model.fit(
train_data,
validation_data=val_data,
epochs=epochs
)
# SAVE MODEL & LABELS
model.save("keras_model.h5")
labels = list(train_data.class_indices.keys())
with open("labels.txt", "w") as f:
for label in labels:
f.write(label + "\n")
print("Model and labels saved successfully.")