-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_set_eval(suri).py
130 lines (115 loc) · 3.62 KB
/
test_set_eval(suri).py
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
import numpy as np
from keras.optimizers import SGD, rmsprop
from keras.models import model_from_json, model_from_yaml
from keras.preprocessing.image import ImageDataGenerator
np.set_printoptions(suppress=True,linewidth=300)
img_width = 160
img_height = 150
def compare_song_class(prediction, genre):
if genre.startswith("Classical") and prediction == 0:
return True
elif genre.startswith("Electronic") and prediction == 1:
return True
elif genre.startswith("Experimental") and prediction == 2:
return True
elif genre.startswith("Folk") and prediction == 3:
return True
elif genre.startswith("Hip-Hop") and prediction == 4:
return True
elif genre.startswith("Instrumental") and prediction == 5:
return True
elif genre.startswith("International") and prediction == 6:
return True
elif genre.startswith("Old-Time_Historic") and prediction == 7:
return True
elif genre.startswith("Pop") and prediction == 8:
return True
elif genre.startswith("Rock") and prediction == 9:
return True
else:
return False
def def_genre_from_str(genre):
if genre.startswith("Classical"):
return 0
elif genre.startswith("Electronic"):
return 1
elif genre.startswith("Experimental"):
return 2
elif genre.startswith("Folk"):
return 3
elif genre.startswith("Hip-Hop"):
return 4
elif genre.startswith("Instrumental"):
return 5
elif genre.startswith("International"):
return 6
elif genre.startswith("Old-Time_Historic"):
return 7
elif genre.startswith("Pop"):
return 8
elif genre.startswith("Rock"):
return 9
f = open('arch_weights/MuGeRe Architecture_F.yaml', 'r')
model = model_from_yaml(f.read())
f.close()
model.load_weights('arch_weights/MuGeRe Weights_F.h5')
model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])
model.summary()
datagen = ImageDataGenerator(
rescale=1. / 255
)
generator = datagen.flow_from_directory(
'fma_medium_test/',
target_size=(img_width, img_height),
batch_size=50,
class_mode=None,
shuffle=False)
# Initialization
confusion_matrix = np.zeros((10, 10))
right = np.zeros(10)
wrong = np.zeros(10)
accuracy = 0
song_genres = np.zeros(10)
spectrogram = -1
predictions = model.predict_generator(generator, steps=482)
for i, n in enumerate(sorted(generator.filenames)):
my_pred = np.argmax(predictions[i])
spectrogram += 1
if spectrogram == 4:
# If prediction is valid, update accuracy
if compare_song_class(np.argmax(song_genres), n):
accuracy += 1
right[my_pred] += 1
else:
wrong[my_pred] += 1
# Reset all variables
spectrogram = -1
song_genres = np.zeros(10)
confusion_matrix[my_pred][def_genre_from_str(n)] += 1
song_genres += predictions[i]
for i in range(10):
if i == 0:
print("Classica")
elif i == 1:
print("Electronic")
elif i == 2:
print("Experimental")
elif i == 3:
print("Folk")
elif i == 4:
print("Hip-Hop")
elif i == 5:
print("Instrumental")
elif i == 6:
print("International")
elif i == 7:
print("Old Time Historic")
elif i == 8:
print("Pop")
elif i == 9:
print("Rock")
print('{} su {} - Percentuale {}'.format(right[i], right[i] + wrong[i], right[i] / (right[i] + wrong[i])))
print("Total accuracy on test set: ")
print(accuracy/((np.shape(predictions)[0])//5))
print("\nConfusion matrix:")
print(confusion_matrix)