-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain.py
More file actions
123 lines (104 loc) · 2.86 KB
/
Copy pathtrain.py
File metadata and controls
123 lines (104 loc) · 2.86 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout
from tensorflow.keras.preprocessing.image import ImageDataGenerator
import matplotlib.pyplot as plt
import os
# Dataset path
DATASET_PATH = "dataset/asl_alphabet_train"
IMG_SIZE = 64
BATCH_SIZE = 32
EPOCHS = 10
# Data augmentation and preprocessing
datagen = ImageDataGenerator(
rescale=1./255,
validation_split=0.2,
rotation_range=10,
width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=True
)
# Load training data
print("Loading training data...")
train_data = datagen.flow_from_directory(
DATASET_PATH,
target_size=(IMG_SIZE, IMG_SIZE),
batch_size=BATCH_SIZE,
subset='training',
class_mode='categorical'
)
# Load validation data
print("Loading validation data...")
val_data = datagen.flow_from_directory(
DATASET_PATH,
target_size=(IMG_SIZE, IMG_SIZE),
batch_size=BATCH_SIZE,
subset='validation',
class_mode='categorical'
)
# Print class labels
print("Classes found:", train_data.class_indices)
NUM_CLASSES = len(train_data.class_indices)
print(f"Total classes: {NUM_CLASSES}")
# Build CNN Model
print("Building CNN model...")
model = Sequential([
Conv2D(32, (3,3), activation='relu', input_shape=(IMG_SIZE, IMG_SIZE, 3)),
MaxPooling2D(2,2),
Conv2D(64, (3,3), activation='relu'),
MaxPooling2D(2,2),
Conv2D(128, (3,3), activation='relu'),
MaxPooling2D(2,2),
Conv2D(256, (3,3), activation='relu'),
MaxPooling2D(2,2),
Flatten(),
Dense(512, activation='relu'),
Dropout(0.5),
Dense(256, activation='relu'),
Dropout(0.3),
Dense(NUM_CLASSES, activation='softmax')
])
# Compile model
model.compile(
optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy']
)
# Print model summary
model.summary()
# Train the model
print("Starting training...")
history = model.fit(
train_data,
validation_data=val_data,
epochs=EPOCHS,
verbose=1
)
# Save the model
os.makedirs('model', exist_ok=True)
model.save('model/sign_language_model.h5')
print("Model saved successfully!")
# Save class labels
import json
with open('model/class_labels.json', 'w') as f:
json.dump(train_data.class_indices, f)
print("Class labels saved!")
# Plot accuracy graph
plt.figure(figsize=(12, 4))
plt.subplot(1, 2, 1)
plt.plot(history.history['accuracy'], label='Train Accuracy')
plt.plot(history.history['val_accuracy'], label='Val Accuracy')
plt.title('Model Accuracy')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.legend()
plt.subplot(1, 2, 2)
plt.plot(history.history['loss'], label='Train Loss')
plt.plot(history.history['val_loss'], label='Val Loss')
plt.title('Model Loss')
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.legend()
plt.tight_layout()
plt.savefig('model/training_graph.png')
print("Training graph saved!")