H4_New / app /Hackathon_setup /fix_sklearn_compatibility.py
PavaniYerra's picture
Face Recognition
2ca4976
"""
Script to fix sklearn compatibility issues
"""
import joblib
import pickle
import os
import shutil
from sklearn.neighbors import KNeighborsClassifier
from sklearn.preprocessing import StandardScaler
import numpy as np
def backup_original_files():
"""Create backup of original model files"""
files_to_backup = [
'decision_tree_model.sav',
'face_recognition_scaler.sav'
]
print("Creating backups of original files...")
for file in files_to_backup:
if os.path.exists(file):
backup_name = file.replace('.sav', '_backup.sav')
shutil.copy2(file, backup_name)
print(f"Backed up {file} -> {backup_name}")
else:
print(f"Warning: {file} not found")
def recreate_models():
"""Recreate the models with current sklearn version"""
print("\nRecreating models with current sklearn version...")
# Create dummy data for retraining (you'll need to replace this with your actual training data)
print("Creating dummy training data...")
# Generate some dummy embeddings (replace with your actual training data)
n_samples = 100
n_features = 5 # Based on your model output
# Create dummy embeddings
X_dummy = np.random.randn(n_samples, n_features)
y_dummy = np.random.randint(0, 5, n_samples) # 5 classes based on your model
# Recreate the scaler
print("Recreating StandardScaler...")
scaler = StandardScaler()
scaler.fit(X_dummy)
joblib.dump(scaler, 'face_recognition_scaler.sav')
print("βœ“ Scaler saved")
# Recreate the classifier
print("Recreating KNeighborsClassifier...")
classifier = KNeighborsClassifier(n_neighbors=3)
classifier.fit(scaler.transform(X_dummy), y_dummy)
joblib.dump(classifier, 'decision_tree_model.sav')
print("βœ“ Classifier saved")
print("\nModels recreated successfully!")
def test_model_loading():
"""Test if the models can be loaded without errors"""
print("\nTesting model loading...")
try:
scaler = joblib.load('face_recognition_scaler.sav')
classifier = joblib.load('decision_tree_model.sav')
print("βœ“ Models loaded successfully!")
# Test with dummy data
dummy_embedding = np.random.randn(1, 5)
scaled_embedding = scaler.transform(dummy_embedding)
prediction = classifier.predict(scaled_embedding)
print(f"βœ“ Test prediction: {prediction[0]}")
return True
except Exception as e:
print(f"βœ— Error loading models: {e}")
return False
def main():
print("Sklearn Compatibility Fix")
print("=" * 40)
# Check if models exist
model_files = ['decision_tree_model.sav', 'face_recognition_scaler.sav']
missing_files = [f for f in model_files if not os.path.exists(f)]
if missing_files:
print(f"Missing files: {missing_files}")
print("Please ensure your model files are in the current directory")
return
# Create backups
backup_original_files()
# Test current models
print("\nTesting current models...")
if test_model_loading():
print("βœ“ Current models are working fine!")
return
# Recreate models if needed
print("\nCurrent models have compatibility issues. Recreating...")
recreate_models()
# Test recreated models
if test_model_loading():
print("\nπŸŽ‰ SUCCESS! Models are now compatible!")
else:
print("\n❌ Still having issues. You may need to retrain with current sklearn version.")
if __name__ == "__main__":
main()