-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain_fish_model.py
More file actions
178 lines (139 loc) · 6.31 KB
/
Copy pathtrain_fish_model.py
File metadata and controls
178 lines (139 loc) · 6.31 KB
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
178
#!/usr/bin/env python3
"""
Fish Freshness Model Training Pipeline
======================================
Trains a multi-stage freshness classification model specifically for fish
using extracted color features from fish_features.csv.
Outputs:
- model/fish_classifier.pkl
- model/fish_scaler.pkl
- model/fish_feature_names.pkl
Usage:
python train_fish_model.py
"""
import os
import sys
import csv
import numpy as np
import joblib
from sklearn.ensemble import RandomForestClassifier, ExtraTreesClassifier, GradientBoostingClassifier
from sklearn.svm import SVC
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import StratifiedKFold, cross_val_score, LeaveOneGroupOut
from sklearn.metrics import classification_report, accuracy_score, confusion_matrix
sys.stdout.reconfigure(encoding='utf-8')
sys.stderr.reconfigure(encoding='utf-8')
import config
def load_fish_features(csv_path: str):
"""Load fish features from CSV, returning X, y, sources, and feature_names."""
features = []
labels = []
sources = []
feature_names = []
with open(csv_path, "r") as f:
reader = csv.DictReader(f)
headers = reader.fieldnames
feature_names = sorted([h for h in headers if h not in ("label", "source")])
for row in reader:
labels.append(int(row["label"]))
sources.append(row.get("source", ""))
feat = [float(row[k]) if row[k] != "" else 0.0 for k in feature_names]
features.append(feat)
return np.array(features), np.array(labels), sources, feature_names
def train_fish_model():
csv_path = os.path.join(config.BASE_DIR, "fish_features_3stage.csv")
if not os.path.exists(csv_path):
print("❌ fish_features_3stage.csv not found! Running prepare_fish_data.py first...")
from prepare_fish_data import prepare_fish_dataset
csv_path = prepare_fish_dataset()
if not csv_path or not os.path.exists(csv_path):
print("❌ Data preparation failed.")
return
from prepare_fish_data import LABEL_NAMES_3CLASS
print("=" * 60)
print("🐟 Fish Freshness Classification — Model Training (3-Stage)")
print("=" * 60)
X, y, sources, feature_names = load_fish_features(csv_path)
print(f"\n📊 Dataset Loaded: {len(X)} samples across {X.shape[1]} features")
for stage_id, stage_name in LABEL_NAMES_3CLASS.items():
count = np.sum(y == stage_id)
print(f" {stage_name}: {count} samples ({count / len(y):.1%})")
# Feature Selection: Select top 15% most discriminative features to remove noise
from sklearn.feature_selection import SelectPercentile, f_classif
from sklearn.ensemble import VotingClassifier
selector = SelectPercentile(f_classif, percentile=15)
X_selected = selector.fit_transform(X, y)
print(f" Reduced features from {X.shape[1]} to {X_selected.shape[1]} using ANOVA F-score selection.")
# Standard Scaling on selected features
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X_selected)
rf = RandomForestClassifier(n_estimators=200, max_depth=15, class_weight="balanced", random_state=42)
et = ExtraTreesClassifier(n_estimators=200, max_depth=15, class_weight="balanced", random_state=42)
gb = GradientBoostingClassifier(n_estimators=150, learning_rate=0.08, max_depth=5, random_state=42)
svm = SVC(kernel="rbf", C=15.0, gamma="scale", class_weight="balanced", probability=True, random_state=42)
voting_ensemble = VotingClassifier(
estimators=[("rf", rf), ("et", et), ("gb", gb), ("svm", svm)],
voting="soft"
)
# Candidate Classifiers
models = {
"RandomForest": rf,
"ExtraTrees": et,
"GradientBoosting": gb,
"SVM (RBF)": svm,
"Voting Ensemble 🏆": voting_ensemble
}
print("\n🔍 Evaluating Classifiers with 5-Fold Stratified Cross-Validation:")
best_name = None
best_score = -1.0
best_clf = None
skf = StratifiedKFold(n_splits=5, shuffle=True, random_state=42)
for name, clf in models.items():
scores = cross_val_score(clf, X_scaled, y, cv=skf, scoring="accuracy")
mean_acc = np.mean(scores)
std_acc = np.std(scores)
print(f" {name:20s}: {mean_acc:.2%} (±{std_acc:.2%})")
if mean_acc > best_score:
best_score = mean_acc
best_name = name
best_clf = clf
print(f"\n🏆 Best Classifier Selected: {best_name} (3-Stage Cross-Validation Accuracy: {best_score:.2%})")
# Fit final model on full fish dataset
best_clf.fit(X_scaled, y)
y_pred = best_clf.predict(X_scaled)
print("\n📈 Final Model Training Performance Summary (3-Stage System):")
target_names = [LABEL_NAMES_3CLASS[i] for i in sorted(np.unique(y))]
report = classification_report(y, y_pred, target_names=target_names)
print(report)
cm = confusion_matrix(y, y_pred)
print("🧩 Confusion Matrix:")
print(cm)
# Feature Importance (if tree model)
if hasattr(best_clf, "feature_importances_"):
importances = best_clf.feature_importances_
top_idx = np.argsort(importances)[::-1][:10]
print("\n⭐ Top 10 Most Important Features:")
for idx in top_idx:
print(f" - {feature_names[idx]:20s}: {importances[idx]:.4f}")
# Save Fish Model artifacts
model_dir = config.MODEL_DIR
os.makedirs(model_dir, exist_ok=True)
fish_model_path = os.path.join(model_dir, "fish_classifier.pkl")
fish_scaler_path = os.path.join(model_dir, "fish_scaler.pkl")
fish_feats_path = os.path.join(model_dir, "fish_feature_names.pkl")
joblib.dump(best_clf, fish_model_path)
joblib.dump(scaler, fish_scaler_path)
joblib.dump(feature_names, fish_feats_path)
# Also update main classifier.pkl & scaler.pkl if desired
main_model_path = os.path.join(model_dir, "classifier.pkl")
main_scaler_path = os.path.join(model_dir, "scaler.pkl")
joblib.dump(best_clf, main_model_path)
joblib.dump(scaler, main_scaler_path)
print("\n💾 Model Artifacts Successfully Saved:")
print(f" - Fish Model : {fish_model_path}")
print(f" - Fish Scaler : {fish_scaler_path}")
print(f" - Main Model : {main_model_path}")
print(f" - Main Scaler : {main_scaler_path}")
print("\n🎉 Fish model training complete!")
if __name__ == "__main__":
train_fish_model()