File size: 6,043 Bytes
1f4e4ae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import numpy as np
import tensorflow as tf
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
import os
import glob
from utils.model_utils import create_model, get_training_callbacks, evaluate_model
from utils.data_utils import DataProcessor

class SignLanguageTrainer:
    def __init__(self):
        self.data_processor = DataProcessor()
        self.label_encoder = LabelEncoder()
        
    def load_dataset(self, dataset_path="dataset"):
        """Load dataset from collected data."""
        X, y = [], []
        labels = []
        
        # Get all sign directories
        sign_dirs = [d for d in os.listdir(dataset_path) if os.path.isdir(os.path.join(dataset_path, d))]
        
        for sign_name in sign_dirs:
            sign_path = os.path.join(dataset_path, sign_name)
            sequence_file = os.path.join(sign_path, "sequences.npy")
            
            if os.path.exists(sequence_file):
                sequences = np.load(sequence_file)
                
                for sequence in sequences:
                    X.append(sequence)
                    y.append(sign_name)
                    
                if sign_name not in labels:
                    labels.append(sign_name)
                    
                print(f"Loaded {len(sequences)} sequences for {sign_name}")
        
        # Save labels
        with open('models/labels.txt', 'w') as f:
            f.write('\n'.join(labels))
        
        # Encode labels
        y_encoded = self.label_encoder.fit_transform(y)
        
        return np.array(X), y_encoded, labels
    
    def prepare_data(self, X, y, test_size=0.2, val_size=0.2):
        """Prepare data for training."""
        # Split into train and test
        X_train, X_test, y_train, y_test = train_test_split(
            X, y, test_size=test_size, random_state=42, stratify=y
        )
        
        # Split train into train and validation
        X_train, X_val, y_train, y_val = train_test_split(
            X_train, y_train, test_size=val_size, random_state=42, stratify=y_train
        )
        
        return X_train, X_val, X_test, y_train, y_val, y_test
    
    def train_model(self, X_train, y_train, X_val, y_val, 
                   num_classes, epochs=50, batch_size=32):
        """Train the sign language model."""
        
        # Get input shape
        sequence_length, num_features = X_train.shape[1], X_train.shape[2]
        
        # Create model
        model = create_model(num_classes, sequence_length, num_features)
        
        print("Model Summary:")
        model.summary()
        
        # Get callbacks
        callbacks = get_training_callbacks()
        
        # Train model
        history = model.fit(
            X_train, y_train,
            validation_data=(X_val, y_val),
            epochs=epochs,
            batch_size=batch_size,
            callbacks=callbacks,
            verbose=1
        )
        
        return model, history
    
    def save_model(self, model, model_name="sign_language_model"):
        """Save the trained model."""
        # Save Keras model
        model.save(f'models/{model_name}.h5')
        
        # Convert to TFLite
        converter = tf.lite.TFLiteConverter.from_keras_model(model)
        converter.optimizations = [tf.lite.Optimize.DEFAULT]
        
        # Quantization for smaller model size
        def representative_dataset():
            for _ in range(100):
                yield [np.random.random((1, 30, 126)).astype(np.float32)]
        
        converter.representative_dataset = representative_dataset
        converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]
        converter.inference_input_type = tf.uint8
        converter.inference_output_type = tf.uint8
        
        try:
            tflite_model = converter.convert()
            with open('models/model.tflite', 'wb') as f:
                f.write(tflite_model)
            print("Model converted to TFLite successfully!")
        except Exception as e:
            print(f"TFLite conversion failed: {e}")
            # Fallback to float32 model
            converter = tf.lite.TFLiteConverter.from_keras_model(model)
            converter.optimizations = [tf.lite.Optimize.DEFAULT]
            tflite_model = converter.convert()
            with open('models/model.tflite', 'wb') as f:
                f.write(tflite_model)
            print("Model converted to TFLite (float32) successfully!")

def main():
    trainer = SignLanguageTrainer()
    
    print("Sign Language Model Training")
    print("=" * 40)
    
    # Check if dataset exists
    if not os.path.exists("dataset"):
        print("Dataset not found! Please run data_collection.py first.")
        return
    
    # Load dataset
    print("Loading dataset...")
    X, y, labels = trainer.load_dataset()
    
    if len(X) == 0:
        print("No data found! Please collect data first using data_collection.py")
        return
    
    print(f"Loaded {len(X)} sequences for {len(labels)} signs")
    print(f"Signs: {labels}")
    
    # Prepare data
    print("Preparing data...")
    X_train, X_val, X_test, y_train, y_val, y_test = trainer.prepare_data(X, y)
    
    print(f"Training set: {len(X_train)} sequences")
    print(f"Validation set: {len(X_val)} sequences")
    print(f"Test set: {len(X_test)} sequences")
    
    # Train model
    print("Training model...")
    model, history = trainer.train_model(
        X_train, y_train, X_val, y_val,
        num_classes=len(labels),
        epochs=50,
        batch_size=32
    )
    
    # Evaluate model
    print("Evaluating model...")
    results = evaluate_model(model, X_test, y_test)
    print(f"Test Accuracy: {results['accuracy']:.4f}")
    
    # Save model
    print("Saving model...")
    trainer.save_model(model)
    
    print("Training completed successfully!")
    print("You can now run the main application: streamlit run app.py")

if __name__ == "__main__":
    main()