-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_model.py
110 lines (89 loc) · 3.68 KB
/
train_model.py
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
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout, BatchNormalization, \
GlobalAveragePooling2D
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.callbacks import EarlyStopping, ReduceLROnPlateau
import pandas as pd
import numpy as np
# Load the dataset
train_data = pd.read_csv('dataset/archive/sign_mnist_train/sign_mnist_train.csv')
test_data = pd.read_csv('dataset/archive/sign_mnist_test/sign_mnist_test.csv')
# Separate features and labels
X_train = train_data.iloc[:, 1:].values
y_train = train_data.iloc[:, 0].values
X_test = test_data.iloc[:, 1:].values
y_test = test_data.iloc[:, 0].values
# Reshape and normalize the features
X_train = X_train.reshape(X_train.shape[0], 28, 28, 1).astype('float32') / 255
X_test = X_test.reshape(X_test.shape[0], 28, 28, 1).astype('float32') / 255
# Check the unique labels in your data
unique_labels = np.unique(y_train)
print(f"Unique labels in the training set: {unique_labels}")
# Determine the number of classes based on unique labels
num_classes = np.max(unique_labels) + 1 # This will give us 25 classes since max label is 24
# Convert labels to categorical (adjusting for the number of classes)
y_train = tf.keras.utils.to_categorical(y_train, num_classes)
y_test = tf.keras.utils.to_categorical(y_test, num_classes)
# Data augmentation with slightly less aggressive parameters
datagen = ImageDataGenerator(
rotation_range=10, # Reduced rotation range
width_shift_range=0.1, # Reduced width shift
height_shift_range=0.1, # Reduced height shift
zoom_range=0.1, # Reduced zoom range
shear_range=0.1, # Reduced shear range
horizontal_flip=True,
fill_mode='nearest' # Filling missing pixels after transformations
)
datagen.fit(X_train)
# Build a slightly simplified model
model = Sequential([
Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)),
BatchNormalization(),
MaxPooling2D(2, 2),
Conv2D(64, (3, 3), activation='relu'),
BatchNormalization(),
MaxPooling2D(2, 2),
Conv2D(128, (3, 3), activation='relu'),
BatchNormalization(),
MaxPooling2D(2, 2),
Flatten(),
Dense(128, activation='relu'),
Dropout(0.5), # Dropout to prevent overfitting
Dense(num_classes, activation='softmax')
])
# Compile the model with a lower initial learning rate
model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.0003), loss='categorical_crossentropy',
metrics=['accuracy'])
# Use EarlyStopping and ReduceLROnPlateau
callbacks = [
EarlyStopping(monitor='val_loss', patience=5, restore_best_weights=True),
ReduceLROnPlateau(monitor='val_loss', factor=0.5, patience=3, min_lr=1e-6)
]
# Train the model with data augmentation
history = model.fit(datagen.flow(X_train, y_train, batch_size=32),
epochs=50,
validation_data=(X_test, y_test),
callbacks=callbacks)
# Save the model in the new format
model.save('hand_gesture_model.keras')
# Optional: Plot the training history for better visualization
import matplotlib.pyplot as plt
# Plot training & validation accuracy values
plt.figure(figsize=(12, 4))
plt.subplot(1, 2, 1)
plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])
plt.title('Model accuracy')
plt.ylabel('Accuracy')
plt.xlabel('Epoch')
plt.legend(['Train', 'Validation'], loc='upper left')
# Plot training & validation loss values
plt.subplot(1, 2, 2)
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('Model loss')
plt.ylabel('Loss')
plt.xlabel('Epoch')
plt.legend(['Train', 'Validation'], loc='upper left')
plt.show()