-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathtrain.py
More file actions
70 lines (56 loc) · 2.23 KB
/
train.py
File metadata and controls
70 lines (56 loc) · 2.23 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
"""
train.py
Description : This script trains a Sign Language Recognition model using hand keypoint data.
It loads data from a CSV file, trains a neural network using TensorFlow/Keras,
and saves both the HDF5 and TFLite versions of the trained model.
Author : Sam <sam@codingsamrat.com>
Created : May 26, 2023
"""
import numpy as np
import tensorflow as tf
from sklearn.model_selection import train_test_split
import os
# Set random seed for reproducibility
RANDOM_SEED = 42
np.random.seed(RANDOM_SEED)
tf.random.set_seed(RANDOM_SEED)
# Paths
dataset_path = 'slr/model/keypoint.csv' # Make sure to place your dataset here
model_h5_path = 'slr/model/slr_model.hdf5'
model_tflite_path = 'slr/model/slr_model.tflite'
# Number of classes (e.g., 24 alphabets excluding J & Z)
NUM_CLASSES = 24
# Load dataset
print("[INFO] Loading dataset...")
X = np.loadtxt(dataset_path, delimiter=',', dtype='float32', usecols=list(range(1, 43)))
y = np.loadtxt(dataset_path, delimiter=',', dtype='int32', usecols=(0,))
# Split into training and test sets
print("[INFO] Splitting dataset...")
X_train, X_test, y_train, y_test = train_test_split(X, y, train_size=0.75, random_state=RANDOM_SEED)
# Define the model
print("[INFO] Building model...")
model = tf.keras.models.Sequential([
tf.keras.layers.Input((42,)), # 21 keypoints * 2 (x, y)
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(20, activation='relu'),
tf.keras.layers.Dropout(0.4),
tf.keras.layers.Dense(10, activation='relu'),
tf.keras.layers.Dense(NUM_CLASSES, activation='softmax')
])
# Compile model
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
# Train the model
print("[INFO] Training model...")
model.fit(X_train, y_train, epochs=50, validation_data=(X_test, y_test))
# Save the model in HDF5 format
print(f"[INFO] Saving model to {model_h5_path}...")
model.save(model_h5_path)
# Convert to TFLite
print(f"[INFO] Converting model to TFLite and saving to {model_tflite_path}...")
converter = tf.lite.TFLiteConverter.from_keras_model(model)
tflite_model = converter.convert()
with open(model_tflite_path, 'wb') as f:
f.write(tflite_model)
print("[INFO] Training complete.")