-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
105 lines (78 loc) · 3.67 KB
/
Copy pathmodel.py
File metadata and controls
105 lines (78 loc) · 3.67 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
# -*- coding: utf-8 -*-
"""Model.ipynb
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/1QBK60SxPNUE7AvxcXU7Essvmz0qBAcuP
"""
import pickle
# Load the pickle file
with open('hidden_states_by_genre.pkl', 'rb') as f:
hidden_states_by_genre = pickle.load(f)
import pandas as pd
import numpy as np
from ast import literal_eval
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report, accuracy_score
# Added for clustering
from sklearn.cluster import KMeans
from sklearn.preprocessing import StandardScaler
embeddings = []
labels = []
for s in hidden_states_by_genre:
for e in hidden_states_by_genre[s]:
embeddings.append(np.array(e).flatten())
labels.append(s)
# split data into train and test
X_train, X_test, y_train, y_test = train_test_split(
embeddings, labels, test_size=0.2, random_state=42
)
# Standardize embeddings for both train and test
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
# Perform clustering using KMeans on the training data
n_clusters = len(set(labels)) # Number of clusters equal to the number of genres
kmeans = KMeans(n_clusters=n_clusters, random_state=42)
train_clusters = kmeans.fit_predict(X_train_scaled)
# Apply the same KMeans transformation to the test data
test_clusters = kmeans.predict(X_test_scaled)
# Add clusters as additional features to both training and test sets
X_train_with_clusters = np.hstack([X_train_scaled, train_clusters.reshape(-1, 1)])
X_test_with_clusters = np.hstack([X_test_scaled, test_clusters.reshape(-1, 1)])
# Train random forest classifier
clf = RandomForestClassifier(n_estimators=100)
clf.fit(X_train_with_clusters, y_train)
preds = clf.predict(X_test_with_clusters)
probas = clf.predict_proba(X_test_with_clusters)
# Generate and print classification report
report = classification_report(y_test, preds)
print(report)
import librosa
import numpy as np
from sklearn.preprocessing import StandardScaler
# Load audio file and extract features
def extract_features_from_audio(file_path, target_dim=9984):
y, sr = librosa.load("/content/The Police - Roxanne (Official Music Video).wav", duration=120) # Load the audio (limit to 30 seconds)
# Extract MFCCs (Mel-frequency cepstral coefficients)
mfccs = librosa.feature.mfcc(y=y, sr=sr, n_mfcc=13) # You can adjust n_mfcc to control feature size
# Flatten MFCCs
mfccs_flattened = mfccs.flatten()
# If the flattened features don't match the target dimension, resize or pad
if len(mfccs_flattened) > target_dim:
mfccs_flattened = mfccs_flattened[:target_dim] # Truncate if larger
elif len(mfccs_flattened) < target_dim:
mfccs_flattened = np.pad(mfccs_flattened, (0, target_dim - len(mfccs_flattened)), 'constant') # Pad if smaller
return mfccs_flattened
# Example: Extract features from a music file and match target dimension
file_path = "/content/The Police - Roxanne (Official Music Video).wav" # Replace with your audio file
new_embedding = extract_features_from_audio(file_path, target_dim=9984)
# Standardize the new embedding (using the same scaler used on the training data)
new_embedding_scaled = scaler.transform([new_embedding])
# Predict the cluster for the new embedding using the KMeans model
new_cluster = kmeans.predict(new_embedding_scaled)
# Add cluster as a feature
new_embedding_with_cluster = np.hstack([new_embedding_scaled, new_cluster.reshape(-1, 1)])
# Predict the genre using the trained classifier
new_pred = clf.predict(new_embedding_with_cluster)
print(f"Predicted genre: {new_pred[0]}")