Zen-4011 commited on
Commit
b83b539
·
verified ·
1 Parent(s): 456d2f1

Upload 7 files

Browse files
Files changed (7) hide show
  1. Dockerfile +6 -0
  2. app.py +38 -0
  3. label_encoder.pkl +3 -0
  4. model.py +272 -0
  5. music_genre_classifier.pkl +3 -0
  6. requirements.txt +5 -0
  7. scaler.pkl +3 -0
Dockerfile ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ FROM python:3.9
2
+ WORKDIR /code
3
+ COPY requirements.txt .
4
+ RUN pip install --no-cache-dir -r requirements.txt
5
+ COPY . .
6
+ CMD ["python", "app.py"]
app.py ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, render_template, request, jsonify
2
+ import joblib
3
+ import pandas as pd
4
+ import numpy as np
5
+
6
+ app = Flask(__name__)
7
+
8
+ model = joblib.load("music_genre_classifier.pkl")
9
+ scaler = joblib.load("scaler.pkl")
10
+ le = joblib.load("label_encoder.pkl")
11
+
12
+ @app.route('/')
13
+ def home():
14
+ return render_template('index.html')
15
+
16
+ @app.route('/predict', methods = ['POST'])
17
+ def predict():
18
+ data = request.get_json()
19
+
20
+ input_df = pd.DataFrame([data])
21
+
22
+ # Scale and Predict
23
+ scaled_data = scaler.transform(input_df)
24
+ prediction_idx = model.predict(scaled_data)[0]
25
+
26
+ # Get probability / confidence
27
+ probs = model.predict_proba(scaled_data)[0]
28
+ confidence = np.max(probs) * 100
29
+
30
+ genre = le.inverse_transform([prediction_idx])[0]
31
+
32
+ return jsonify({
33
+ 'prediction': genre,
34
+ 'confidence': confidence
35
+ })
36
+
37
+ if __name__ == "__main__":
38
+ app.run(host = "0.0.0.0", port = 7860)
label_encoder.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:16794a76fdfb082780a6ff856dde9776d9a1b3e213395c3f4c446bd77f19fce9
3
+ size 557
model.py ADDED
@@ -0,0 +1,272 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ import numpy as np
3
+ import matplotlib.pyplot as plt
4
+ import seaborn as sns
5
+ import time
6
+ import tracemalloc
7
+ import warnings
8
+ import os
9
+ warnings.filterwarnings('ignore')
10
+
11
+ from sklearn.preprocessing import LabelEncoder, StandardScaler
12
+ from sklearn.model_selection import (train_test_split, KFold,
13
+ cross_val_score, learning_curve)
14
+ from sklearn.linear_model import LogisticRegression
15
+ from sklearn.ensemble import RandomForestClassifier
16
+ from sklearn.svm import SVC
17
+ from sklearn.metrics import (classification_report, confusion_matrix,
18
+ accuracy_score, roc_auc_score)
19
+ import joblib
20
+
21
+ # Load Dataset
22
+ df = pd.read_csv("data/features_30_sec.csv", index_col=0)
23
+ print(f"Dataset shape: {df.shape}")
24
+ print(f"Class distribution:\n{df['label'].value_counts()}\n")
25
+
26
+ # Preprocess Data
27
+ le = LabelEncoder()
28
+ y = le.fit_transform(df["label"])
29
+ X = df.drop("label", axis=1)
30
+
31
+ X_train, X_test, y_train, y_test = train_test_split(
32
+ X, y, test_size=0.2, random_state=42, stratify=y
33
+ )
34
+
35
+ scaler = StandardScaler()
36
+ X_train_scaled = scaler.fit_transform(X_train)
37
+ X_test_scaled = scaler.transform(X_test)
38
+
39
+ X_full = np.vstack([X_train_scaled, X_test_scaled])
40
+ y_full = np.hstack([y_train, y_test])
41
+
42
+ # Comutational Cost Measurement Function
43
+ def measure_cost(model, X_tr, y_tr, X_te):
44
+ tracemalloc.start()
45
+ t0 = time.time()
46
+ model.fit(X_tr, y_tr)
47
+ train_time = time.time() - t0
48
+ _, peak = tracemalloc.get_traced_memory()
49
+ tracemalloc.stop()
50
+
51
+ t0 = time.time()
52
+ model.predict(X_te)
53
+ infer_time = (time.time() - t0) / len(X_te)
54
+
55
+ return round(train_time, 3), round(peak / 1024**2, 3), round(infer_time * 1000, 4)
56
+
57
+ # Models
58
+ models = {
59
+ "Logistic Regression": LogisticRegression(max_iter=1000, random_state=42),
60
+ "Random Forest": RandomForestClassifier(n_estimators=300, max_depth=20, random_state=42),
61
+ "SVM (RBF)": SVC(kernel="rbf", C=10, gamma="scale", probability=True, random_state=42),
62
+ }
63
+
64
+ results = {}
65
+ cost_rows = []
66
+
67
+ # Train, evaluate, and measure cost for each model
68
+ for name, model in models.items():
69
+ train_time, mem_mb, infer_ms = measure_cost(model, X_train_scaled, y_train, X_test_scaled)
70
+ y_pred = model.predict(X_test_scaled)
71
+ y_prob = model.predict_proba(X_test_scaled)
72
+
73
+ acc = accuracy_score(y_test, y_pred)
74
+ roc_auc = roc_auc_score(y_test, y_prob, multi_class='ovr', average='macro')
75
+
76
+ results[name] = {
77
+ "model": model,
78
+ "y_pred": y_pred,
79
+ "y_prob": y_prob,
80
+ "acc": acc,
81
+ "roc_auc": roc_auc,
82
+ }
83
+ cost_rows.append({
84
+ "Model": name,
85
+ "Train Time (s)": train_time,
86
+ "Peak Memory (MB)": mem_mb,
87
+ "Inference/sample (ms)": infer_ms,
88
+ })
89
+
90
+ print(f"\n{'='*55}")
91
+ print(f" {name}")
92
+ print(f" Accuracy : {acc*100:.2f}% ROC-AUC : {roc_auc:.4f}")
93
+ print(classification_report(y_test, y_pred, target_names=le.classes_))
94
+
95
+ # K-Fold Cross Validation
96
+ print("\n" + "="*55)
97
+ print(" K-FOLD CROSS VALIDATION (K = 10)")
98
+ print("="*55)
99
+
100
+ kf = KFold(n_splits=10, shuffle=True, random_state=42)
101
+
102
+ for name, info in results.items():
103
+ cv = cross_val_score(info["model"], X_full, y_full, cv=kf, scoring="accuracy", n_jobs=-1)
104
+ results[name]["cv"] = cv
105
+ print(f"\n{name}")
106
+ print(f" Fold scores : {cv.round(3)}")
107
+ print(f" Mean ± Std : {cv.mean():.4f} ± {cv.std():.4f}")
108
+ print(f" Test acc : {info['acc']:.4f} "
109
+ f"({'overfit' if info['acc'] > cv.mean() + 0.05 else 'generalises well'})")
110
+
111
+ # Visualizations
112
+ os.makedirs('plots', exist_ok=True)
113
+
114
+ # Heatmaps
115
+ fig, axes = plt.subplots(1, 3, figsize=(22, 7))
116
+ for ax, (name, info) in zip(axes, results.items()):
117
+ cm = confusion_matrix(y_test, info["y_pred"])
118
+ sns.heatmap(cm, annot=True, fmt="d", cmap="Blues",
119
+ xticklabels=le.classes_, yticklabels=le.classes_, ax=ax,
120
+ linewidths=0.5, cbar=False)
121
+ ax.set_title(f"{name}\nAcc: {info['acc']*100:.1f}% ROC-AUC: {info['roc_auc']:.3f}",
122
+ fontsize=11, fontweight="bold")
123
+ ax.set_xlabel("Predicted Label")
124
+ ax.set_ylabel("True Label")
125
+ ax.tick_params(axis="x", rotation=45)
126
+ plt.suptitle("Confusion Matrices — All Models", fontsize=14, fontweight="bold", y=1.02)
127
+ plt.tight_layout()
128
+ plt.savefig("plots/confusion_matrices.png", dpi=150, bbox_inches="tight")
129
+ plt.show()
130
+
131
+ # CV Score Boxplot
132
+ fig, ax = plt.subplots(figsize=(10, 5))
133
+ cv_vals = [info["cv"] for info in results.values()]
134
+ bp = ax.boxplot(cv_vals, labels=results.keys(), patch_artist=True, widths=0.4)
135
+ colors = ["#4e9e94", "#2a7d74", "#1a5c55"]
136
+ for patch, color in zip(bp["boxes"], colors):
137
+ patch.set_facecolor(color)
138
+ patch.set_alpha(0.7)
139
+ ax.set_title("K-Fold CV Score Distribution (K = 10)", fontweight="bold")
140
+ ax.set_ylabel("Accuracy")
141
+ ax.set_ylim(0.5, 1.0)
142
+ ax.axhline(y=0.8, color="red", linestyle="--", alpha=0.4, label="80% threshold")
143
+ ax.legend()
144
+ ax.grid(axis="y", linestyle="--", alpha=0.4)
145
+ plt.tight_layout()
146
+ plt.savefig("plots/cv_boxplot.png", dpi=150, bbox_inches="tight")
147
+ plt.show()
148
+
149
+ # Learning Curves — all 3 models
150
+ def plot_learning_curve(model, X, y, title):
151
+ train_sz, train_sc, val_sc = learning_curve(
152
+ model, X, y, cv=5, n_jobs=-1,
153
+ train_sizes=np.linspace(0.1, 1.0, 10),
154
+ scoring="accuracy"
155
+ )
156
+ t_mean, t_std = train_sc.mean(axis=1), train_sc.std(axis=1)
157
+ v_mean, v_std = val_sc.mean(axis=1), val_sc.std(axis=1)
158
+
159
+ gap = t_mean[-1] - v_mean[-1]
160
+ diagnosis = ("Overfitting" if gap > 0.08 else
161
+ "Underfitting" if v_mean[-1] < 0.65 else
162
+ "Generalises well")
163
+
164
+ plt.figure(figsize=(9, 5))
165
+ plt.plot(train_sz, t_mean, "o-", color="teal", label="Training Score")
166
+ plt.fill_between(train_sz, t_mean - t_std, t_mean + t_std, alpha=0.15, color="teal")
167
+ plt.plot(train_sz, v_mean, "o-", color="coral", label="Validation Score")
168
+ plt.fill_between(train_sz, v_mean - v_std, v_mean + v_std, alpha=0.15, color="coral")
169
+ plt.title(f"Learning Curve — {title}\nDiagnosis: {diagnosis}", fontweight="bold")
170
+ plt.xlabel("Training Set Size")
171
+ plt.ylabel("Accuracy")
172
+ plt.legend(loc="lower right")
173
+ plt.grid(linestyle="--", alpha=0.4)
174
+ plt.ylim(0.3, 1.05)
175
+ plt.tight_layout()
176
+ plt.savefig(f"plots/learning_curve_{title.replace(' ','_')}.png", dpi=150, bbox_inches="tight")
177
+ plt.show()
178
+
179
+ for name, info in results.items():
180
+ plot_learning_curve(info["model"], X_full, y_full, name)
181
+
182
+ # Feature Importance (Random Forest only)
183
+ rf = results["Random Forest"]["model"]
184
+ importances = pd.Series(rf.feature_importances_, index=X.columns).sort_values(ascending=True).tail(10)
185
+ fig, ax = plt.subplots(figsize=(11, 6))
186
+ importances.plot(kind="barh", color="teal", ax=ax, edgecolor="white")
187
+ ax.set_title("Top 10 Most Important Audio Features (Random Forest)", fontweight="bold")
188
+ ax.set_xlabel("Feature Importance Score")
189
+ ax.grid(axis="x", linestyle="--", alpha=0.4)
190
+ plt.tight_layout()
191
+ plt.savefig("plots/feature_importance.png", dpi=150, bbox_inches="tight")
192
+ plt.show()
193
+
194
+ # Model Accuracy Comparison Bar Chart
195
+ fig, ax = plt.subplots(figsize=(8, 5))
196
+ names = list(results.keys())
197
+ accs = [info["acc"] * 100 for info in results.values()]
198
+ aucs = [info["roc_auc"] for info in results.values()]
199
+ x = np.arange(len(names))
200
+ bars = ax.bar(x - 0.2, accs, 0.35, label="Accuracy (%)", color="teal", alpha=0.8)
201
+ bars2 = ax.bar(x + 0.2, [a*100 for a in aucs], 0.35, label="ROC-AUC × 100", color="coral", alpha=0.8)
202
+ ax.set_xticks(x)
203
+ ax.set_xticklabels(names)
204
+ ax.set_ylim(60, 100)
205
+ ax.set_title("Model Comparison — Accuracy vs ROC-AUC", fontweight="bold")
206
+ ax.legend()
207
+ ax.grid(axis="y", linestyle="--", alpha=0.4)
208
+ for bar in bars:
209
+ ax.text(bar.get_x() + bar.get_width()/2, bar.get_height() + 0.3,
210
+ f"{bar.get_height():.1f}", ha="center", fontsize=9)
211
+ plt.tight_layout()
212
+ plt.savefig("plots/model_comparison.png", dpi=150, bbox_inches="tight")
213
+ plt.show()
214
+
215
+ # Computational Cost Analysis
216
+ cost_df = pd.DataFrame(cost_rows)
217
+ print("\n" + "="*55)
218
+ print(" COMPUTATIONAL COST ANALYSIS")
219
+ print("="*55)
220
+ print(cost_df.to_string(index=False))
221
+
222
+ # Save the best model + scaler + label encoder
223
+ best_name = max(results, key=lambda k: results[k]["acc"])
224
+ joblib.dump(results[best_name]["model"], "music_genre_classifier.pkl")
225
+ joblib.dump(scaler, "scaler.pkl")
226
+ joblib.dump(le, "label_encoder.pkl")
227
+ print(f"\nBest model ({best_name}) + scaler + label encoder saved.")
228
+
229
+ # Class Distribution Bar Chart
230
+ plt.figure(figsize=(10, 5))
231
+ df['label'].value_counts().sort_index().plot(kind='bar', color='teal', edgecolor='white')
232
+ plt.title("Class Distribution Across Music Genres", fontweight='bold')
233
+ plt.xlabel("Genre")
234
+ plt.ylabel("Number of Samples")
235
+ plt.xticks(rotation=45)
236
+ plt.tight_layout()
237
+ plt.savefig("plots/class_distribution.png", dpi=150)
238
+ plt.show()
239
+
240
+ # Corelation Heatmap
241
+ plt.figure(figsize=(12, 8))
242
+ top_features = X.columns[:15]
243
+ sns.heatmap(X[top_features].corr(), cmap='coolwarm', annot=False, linewidths=0.5)
244
+ plt.title("Feature Correlation Heatmap", fontweight='bold')
245
+ plt.tight_layout()
246
+ plt.savefig("plots/correlation_heatmap.png", dpi=150)
247
+ plt.show()
248
+
249
+ # Scalability Analysis — Training Time vs. Dataset Size
250
+ fractions = np.linspace(0.1, 1.0, 10)
251
+ scalability = {name: [] for name in models}
252
+
253
+ for frac in fractions:
254
+ n = int(len(X_train_scaled) * frac)
255
+ X_sub = X_train_scaled[:n]
256
+ y_sub = y_train[:n]
257
+ for name, model in models.items():
258
+ t0 = time.time()
259
+ model.fit(X_sub, y_sub)
260
+ scalability[name].append(time.time() - t0)
261
+
262
+ plt.figure(figsize=(10, 5))
263
+ for name, times in scalability.items():
264
+ plt.plot([int(len(X_train_scaled) * f) for f in fractions], times, marker='o', label=name)
265
+ plt.title("Scalability — Training Time vs. Dataset Size", fontweight='bold')
266
+ plt.xlabel("Training Samples")
267
+ plt.ylabel("Training Time (seconds)")
268
+ plt.legend()
269
+ plt.grid(linestyle='--', alpha=0.4)
270
+ plt.tight_layout()
271
+ plt.savefig("plots/scalability_curve.png", dpi=150)
272
+ plt.show()
music_genre_classifier.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:24b63fddcc290ea151f63f2834802aec7dcee0c1c2c8b967922979180ae14004
3
+ size 14408809
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ flask
2
+ pandas
3
+ numpy
4
+ scikit-learn
5
+ joblib
scaler.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:ad16667404a60a3a56edbfe1a851b7f985bc71614782472240d3dfec45cb6c97
3
+ size 3095