File size: 5,313 Bytes
2ca4976
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Test script to diagnose and fix sklearn model loading issues
"""

import joblib
import pickle
import os
import sys

def test_model_loading():
    """Test loading the sklearn models"""
    print("Testing sklearn model loading...")
    print("=" * 40)
    
    # Test scaler
    print("Testing StandardScaler...")
    try:
        scaler = joblib.load('face_recognition_scaler.sav')
        print("OK: Scaler loaded successfully")
        
        # Test with dummy data
        import numpy as np
        dummy_data = np.random.randn(1, 5)  # Assuming 5 features
        scaled_data = scaler.transform(dummy_data)
        print(f"OK: Scaler transform works: {scaled_data.shape}")
        
    except Exception as e:
        print(f"ERROR: Scaler error: {e}")
        return False
    
    # Test classifier
    print("\nTesting KNeighborsClassifier...")
    try:
        classifier = joblib.load('decision_tree_model.sav')
        print("OK: Classifier loaded successfully")
        
        # Test prediction
        prediction = classifier.predict(scaled_data)
        print(f"OK: Classifier prediction works: {prediction[0]}")
        
    except Exception as e:
        print(f"ERROR: Classifier error: {e}")
        return False
    
    return True

def try_compatibility_fixes():
    """Try different compatibility approaches"""
    print("\nTrying compatibility fixes...")
    print("=" * 40)
    
    # Method 1: Try with different joblib versions
    print("Method 1: Trying with different joblib parameters...")
    try:
        scaler = joblib.load('face_recognition_scaler.sav', mmap_mode=None)
        classifier = joblib.load('decision_tree_model.sav', mmap_mode=None)
        print("OK: Loaded with mmap_mode=None")
        return True
    except Exception as e:
        print(f"ERROR: Method 1 failed: {e}")
    
    # Method 2: Try with pickle directly
    print("\nMethod 2: Trying with pickle...")
    try:
        with open('face_recognition_scaler.sav', 'rb') as f:
            scaler = pickle.load(f)
        with open('decision_tree_model.sav', 'rb') as f:
            classifier = pickle.load(f)
        print("OK: Loaded with pickle")
        return True
    except Exception as e:
        print(f"ERROR: Method 2 failed: {e}")
    
    # Method 3: Try with different sklearn version
    print("\nMethod 3: Checking sklearn version compatibility...")
    import sklearn
    print(f"Current sklearn version: {sklearn.__version__}")
    
    # Try to downgrade sklearn temporarily
    print("You may need to downgrade sklearn to match the training version")
    print("Try: pip install scikit-learn==1.6.1")
    
    return False

def create_dummy_models():
    """Create dummy models for testing"""
    print("\nCreating dummy models for testing...")
    print("=" * 40)
    
    try:
        from sklearn.neighbors import KNeighborsClassifier
        from sklearn.preprocessing import StandardScaler
        import numpy as np
        
        # Create dummy data
        n_samples = 50
        n_features = 5
        X = np.random.randn(n_samples, n_features)
        y = np.random.randint(0, 5, n_samples)
        
        # Create and fit scaler
        scaler = StandardScaler()
        scaler.fit(X)
        joblib.dump(scaler, 'face_recognition_scaler_dummy.sav')
        print("OK: Created dummy scaler")
        
        # Create and fit classifier
        classifier = KNeighborsClassifier(n_neighbors=3)
        classifier.fit(scaler.transform(X), y)
        joblib.dump(classifier, 'decision_tree_model_dummy.sav')
        print("OK: Created dummy classifier")
        
        # Test the dummy models
        test_data = np.random.randn(1, n_features)
        scaled_data = scaler.transform(test_data)
        prediction = classifier.predict(scaled_data)
        print(f"OK: Dummy model test: {prediction[0]}")
        
        return True
        
    except Exception as e:
        print(f"ERROR: Error creating dummy models: {e}")
        return False

def main():
    print("Sklearn Model Loading Diagnostic")
    print("=" * 50)
    
    # Check if model files exist
    model_files = ['decision_tree_model.sav', 'face_recognition_scaler.sav']
    for file in model_files:
        if os.path.exists(file):
            print(f"OK: Found {file}")
        else:
            print(f"ERROR: Missing {file}")
            return
    
    # Test current models
    if test_model_loading():
        print("\nSUCCESS! Your models are working fine!")
        return
    
    # Try compatibility fixes
    if try_compatibility_fixes():
        print("\nSUCCESS! Fixed with compatibility approach!")
        return
    
    # Create dummy models as fallback
    if create_dummy_models():
        print("\nWARNING: Created dummy models. You should retrain with current sklearn version.")
        print("To use dummy models, rename them:")
        print("  mv face_recognition_scaler_dummy.sav face_recognition_scaler.sav")
        print("  mv decision_tree_model_dummy.sav decision_tree_model.sav")
    
    print("\n" + "=" * 50)
    print("RECOMMENDATIONS:")
    print("1. Downgrade sklearn: pip install scikit-learn==1.6.1")
    print("2. Retrain your models with current sklearn version")
    print("3. Use the dummy models for testing")

if __name__ == "__main__":
    main()