RICHERGIRL commited on
Commit
087066d
·
verified ·
1 Parent(s): cbf5ccd

Update train_model.py

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