Spaces:
Configuration error
Configuration error
Update model/random_forest.pkl
Browse files- model/random_forest.pkl +75 -17
model/random_forest.pkl
CHANGED
|
@@ -1,40 +1,98 @@
|
|
| 1 |
-
# train_model.py
|
| 2 |
from sklearn.ensemble import RandomForestClassifier
|
| 3 |
from sklearn.preprocessing import LabelEncoder
|
|
|
|
|
|
|
| 4 |
import joblib
|
| 5 |
import pandas as pd
|
| 6 |
import os
|
| 7 |
|
| 8 |
-
# 1.
|
|
|
|
| 9 |
data = {
|
| 10 |
'face_shape': ['Oval', 'Round', 'Square'] * 100,
|
| 11 |
'skin_tone': ['Fair', 'Medium', 'Dark'] * 100,
|
| 12 |
'face_size': ['Small', 'Medium', 'Large'] * 100,
|
| 13 |
-
'mask_style': ['
|
|
|
|
| 14 |
}
|
| 15 |
df = pd.DataFrame(data)
|
|
|
|
| 16 |
|
| 17 |
-
# 2.
|
|
|
|
| 18 |
encoders = {
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
}
|
| 22 |
|
| 23 |
-
# 3.
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
)
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
os.makedirs('model', exist_ok=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
joblib.dump(model, 'model/random_forest.pkl', protocol=4)
|
| 38 |
joblib.dump(encoders, 'model/label_encoders.pkl', protocol=4)
|
| 39 |
|
| 40 |
-
print("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# train_model.py
|
| 2 |
from sklearn.ensemble import RandomForestClassifier
|
| 3 |
from sklearn.preprocessing import LabelEncoder
|
| 4 |
+
from sklearn.model_selection import train_test_split
|
| 5 |
+
from sklearn.metrics import classification_report
|
| 6 |
import joblib
|
| 7 |
import pandas as pd
|
| 8 |
import os
|
| 9 |
|
| 10 |
+
# 1. Dataset Preparation
|
| 11 |
+
print("=== Preparing Dataset ===")
|
| 12 |
data = {
|
| 13 |
'face_shape': ['Oval', 'Round', 'Square'] * 100,
|
| 14 |
'skin_tone': ['Fair', 'Medium', 'Dark'] * 100,
|
| 15 |
'face_size': ['Small', 'Medium', 'Large'] * 100,
|
| 16 |
+
'mask_style': ['Glitter', 'Animal', 'Floral'] * 100,
|
| 17 |
+
'mask_image': ['masks/glitter.png', 'masks/animal.png', 'masks/floral.png'] * 100
|
| 18 |
}
|
| 19 |
df = pd.DataFrame(data)
|
| 20 |
+
print(f"Dataset created with {len(df)} samples")
|
| 21 |
|
| 22 |
+
# 2. Initialize Encoders with Image Mappings
|
| 23 |
+
print("\n=== Initializing Encoders ===")
|
| 24 |
encoders = {
|
| 25 |
+
'face_shape': LabelEncoder().fit(df['face_shape']),
|
| 26 |
+
'skin_tone': LabelEncoder().fit(df['skin_tone']),
|
| 27 |
+
'face_size': LabelEncoder().fit(df['face_size']),
|
| 28 |
+
'mask_style': LabelEncoder().fit(df['mask_style']),
|
| 29 |
+
'mask_images': {
|
| 30 |
+
0: 'masks/glitter.png',
|
| 31 |
+
1: 'masks/animal.png',
|
| 32 |
+
2: 'masks/floral.png'
|
| 33 |
+
}
|
| 34 |
}
|
| 35 |
|
| 36 |
+
# 3. Feature Engineering
|
| 37 |
+
print("\n=== Encoding Features ===")
|
| 38 |
+
X = pd.DataFrame({
|
| 39 |
+
col: encoders[col].transform(df[col])
|
| 40 |
+
for col in ['face_shape', 'skin_tone', 'face_size']
|
| 41 |
+
})
|
| 42 |
+
y = encoders['mask_style'].transform(df['mask_style'])
|
| 43 |
+
|
| 44 |
+
# 4. Train-Test Split
|
| 45 |
+
X_train, X_test, y_train, y_test = train_test_split(
|
| 46 |
+
X, y, test_size=0.2, random_state=42
|
| 47 |
)
|
| 48 |
+
print(f"Train samples: {len(X_train)}, Test samples: {len(X_test)}")
|
| 49 |
+
|
| 50 |
+
# 5. Model Training (Enhanced Parameters)
|
| 51 |
+
print("\n=== Training Model ===")
|
| 52 |
+
model = RandomForestClassifier(
|
| 53 |
+
n_estimators=150, # Increased from 50
|
| 54 |
+
max_depth=7, # Increased from 5
|
| 55 |
+
min_samples_split=5, # New parameter
|
| 56 |
+
class_weight='balanced', # Handle imbalanced data
|
| 57 |
+
random_state=42
|
| 58 |
)
|
| 59 |
+
model.fit(X_train, y_train)
|
| 60 |
+
|
| 61 |
+
# 6. Enhanced Evaluation
|
| 62 |
+
print("\n=== Model Evaluation ===")
|
| 63 |
+
print(f"Training Accuracy: {model.score(X_train, y_train):.2f}")
|
| 64 |
+
print(f"Test Accuracy: {model.score(X_test, y_test):.2f}")
|
| 65 |
|
| 66 |
+
# Feature Importance
|
| 67 |
+
print("\nFeature Importances:")
|
| 68 |
+
for col, imp in zip(X.columns, model.feature_importances_):
|
| 69 |
+
print(f"- {col}: {imp:.3f}")
|
| 70 |
+
|
| 71 |
+
# Classification Report
|
| 72 |
+
print("\nClassification Report:")
|
| 73 |
+
print(classification_report(y_test, model.predict(X_test)))
|
| 74 |
+
|
| 75 |
+
# 7. Save Model with Verification
|
| 76 |
+
print("\n=== Saving Assets ===")
|
| 77 |
os.makedirs('model', exist_ok=True)
|
| 78 |
+
os.makedirs('masks', exist_ok=True)
|
| 79 |
+
|
| 80 |
+
# Verify mask images exist
|
| 81 |
+
print("\nMask Image Verification:")
|
| 82 |
+
for class_idx, path in encoders['mask_images'].items():
|
| 83 |
+
if os.path.exists(path):
|
| 84 |
+
print(f"✓ {encoders['mask_style'].classes_[class_idx]}: {path}")
|
| 85 |
+
else:
|
| 86 |
+
print(f"✗ Missing: {path}")
|
| 87 |
+
|
| 88 |
+
# Save files
|
| 89 |
joblib.dump(model, 'model/random_forest.pkl', protocol=4)
|
| 90 |
joblib.dump(encoders, 'model/label_encoders.pkl', protocol=4)
|
| 91 |
|
| 92 |
+
print("\n=== Saved Files ===")
|
| 93 |
+
print("Model: model/random_forest.pkl")
|
| 94 |
+
print("Encoders: model/label_encoders.pkl")
|
| 95 |
+
print("\nClass Mappings:")
|
| 96 |
+
print("- Face Shapes:", list(encoders['face_shape'].classes_))
|
| 97 |
+
print("- Mask Styles:", list(encoders['mask_style'].classes_))
|
| 98 |
+
print("- Mask Images:", encoders['mask_images'])
|