-
Notifications
You must be signed in to change notification settings - Fork 1
/
evaluate.py
162 lines (133 loc) · 4.96 KB
/
evaluate.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
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
import Audio.model
from Visual.nets import get_vis_model
from val_model import get_val_model
from va_model import get_va_model
from Gen.va_gen import *
from os.path import isfile, join
import numpy as np
from sklearn.metrics import accuracy_score,classification_report
high_path = 'clips/highlights'
non_path = 'clips/non-highlights'
def evaluate(label, pred):
acc = accuracy_score(label, pred)
target_names = ['Non-highlight', 'Highlight']
print(classification_report(label, pred))
return acc
def load_samples():
# read file list
with open('clips/all_n.csv'.format(type)) as f:
nons = f.read().splitlines()
with open('clips/train_n.csv'.format(type)) as f:
tn = f.read().splitlines()
with open('clips/val_n.csv'.format(type)) as f:
vn = f.read().splitlines()
nons = list(set(nons)-set(tn)-set(vn))
with open('clips/test_h.csv'.format(type)) as f:
highs = f.read().splitlines()
return nons,highs
def predict_audio():
count = 0
nons, highs = load_samples()
y_true = [0 for i in range(len(nons))] + [1 for i in range(len(highs))]
model = Audio.model.get_model()
model.load_weights("{}-weights.h5".format('Audio'), by_name=True)
y_pred = []
for i in range(len(nons)):
count += 1
print(count)
filename = join(non_path,nons[i])
xbatch_buf = [read_audio(filename)]
np_x = np.asarray(xbatch_buf)
result = model.predict(np_x)
y_pred.append(int(result[0][0]>0.5))
for i in range(len(highs)):
count += 1
print(count)
filename = join(high_path,highs[i])
xbatch_buf = [read_audio(filename)]
np_x = np.asarray(xbatch_buf)
result = model.predict(np_x)
y_pred.append(int(result[0][0]>0.5))
print(evaluate(y_true,y_pred))
def predict_visual():
count = 0
nons, highs = load_samples()
y_true = [0 for i in range(len(nons))] + [1 for i in range(len(highs))]
model = get_vis_model()
model.load_weights("{}-weights-{}.h5".format('Visual','v1'), by_name=True)
y_pred = []
for i in range(len(nons)):
count += 1
print(count)
filename = join(non_path,nons[i])
xbatch_buf = [read_frames(filename)]
np_x = preprocess_input(np.asarray(xbatch_buf))
result = model.predict(np_x)
y_pred.append(int(result[0][0]>0.5))
for i in range(len(highs)):
count += 1
print(count)
filename = join(high_path,highs[i])
xbatch_buf = [read_frames(filename)]
np_x = preprocess_input(np.asarray(xbatch_buf))
result = model.predict(np_x)
y_pred.append(int(result[0][0]>0.5))
print(evaluate(y_true,y_pred))
def predict_va():
count = 0
nons, highs = load_samples()
y_true = [0 for i in range(len(nons))] + [1 for i in range(len(highs))]
model = get_va_model('v1')
model.load_weights("{}-weights-{}.h5".format('VAModel','v1'), by_name=True)
y_pred = []
for i in range(len(nons)):
count += 1
print(count)
filename = join(non_path,nons[i])
x1batch_buf = [read_frames(filename)]
np_x1 = preprocess_input(np.asarray(x1batch_buf))
x2batch_buf = [read_audio(filename)]
np_x2 = np.asarray(x2batch_buf)
result = model.predict([np_x1,np_x2])
y_pred.append(int(result[0][0]>0.5))
for i in range(len(highs)):
count += 1
print(count)
filename = join(high_path,highs[i])
x1batch_buf = [read_frames(filename)]
np_x1 = preprocess_input(np.asarray(x1batch_buf))
x2batch_buf = [read_audio(filename)]
np_x2 = np.asarray(x2batch_buf)
result = model.predict([np_x1,np_x2])
y_pred.append(int(result[0][0]>0.5))
print(evaluate(y_true,y_pred))
def predict_val():
count = 0
nons, highs = load_samples()
y_true = [0 for i in range(len(nons))] + [1 for i in range(len(highs))]
model = get_val_model('v1')
model.load_weights("{}-weights-{}.h5".format('VALModel','v1'), by_name=True)
y_pred = []
for i in range(len(nons)):
count += 1
print(count)
filename = join(non_path,nons[i])
x1batch_buf = [read_frames(filename)]
np_x1 = preprocess_input(np.asarray(x1batch_buf))
x2batch_buf = [read_audio(filename)]
np_x2 = np.asarray(x2batch_buf)
result = model.predict([np_x1,np_x2])
y_pred.append(int(result[0][0]>0.5))
for i in range(len(highs)):
count += 1
print(count)
filename = join(high_path,highs[i])
x1batch_buf = [read_frames(filename)]
np_x1 = preprocess_input(np.asarray(x1batch_buf))
x2batch_buf = [read_audio(filename)]
np_x2 = np.asarray(x2batch_buf)
result = model.predict([np_x1,np_x2])
y_pred.append(int(result[0][0]>0.5))
print(evaluate(y_true,y_pred))
if __name__ == '__main__':
predict_audio()