-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrl_test.py
305 lines (269 loc) · 11.9 KB
/
rl_test.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
from supervised_model.sup_model import Frontend
from utils import config as cfg
import torch
import argparse
import os
from tianshou_rl_train import get_args, omss_train_val_test_split, state_to, omss_train_val_split
from rl import tianshou_rl_model, tianshou_env
from tianshou.policy import DQNPolicy
import matplotlib.pyplot as plt
import numpy as np
from tqdm import trange
from links_cluster import LinksCluster
from utils.msaf_validation import eval_seg
import warnings
warnings.filterwarnings("ignore", category=DeprecationWarning)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
def test(args):
# seed
np.random.seed(args.seed)
torch.manual_seed(args.seed)
# prepare dataset (file paths)
test_files = None
if cfg.test_csv:
# load test set indexs TODO
import pandas as pd
val_files = np.array(pd.read_csv(cfg.test_csv, header=None)[0])
train_dataset, val_dataset = omss_train_val_split(cfg.val_pct, val_files, args)
else:
train_dataset, val_dataset, test_dataset = omss_train_val_test_split(cfg.val_pct, cfg.test_pct, test_files, args)
# define model
backend_input_size = cfg.EMBEDDING_DIM + args.num_clusters if args.cluster_encode else cfg.EMBEDDING_DIM
if not args.freeze_frontend:
net = tianshou_rl_model.QNet(
input_shape=(cfg.BIN, cfg.CHUNK_LEN),
embedding_size=backend_input_size,
hidden_size=args.hidden_size,
num_layers=args.num_layers,
num_heads=args.num_heads,
num_clusters=args.num_clusters,
cluster_encode=args.cluster_encode,
use_rnn=args.use_rnn,
device=device,
freeze_frontend=args.freeze_frontend
)
if args.pretrained:
net.load_frontend(args.pretrained)
else:
net = tianshou_rl_model.TianshouBackend(input_size=backend_input_size,
hidden_size=args.hidden_size,
num_layers=args.num_layers,
num_clusters=args.num_clusters,
num_heads=args.num_heads,
mode='train',
use_rnn=args.use_rnn,
device=device,
cluster_encode=args.cluster_encode)
checkpoint = torch.load(args.pretrained)
frontend = Frontend((cfg.BIN, cfg.CHUNK_LEN), embedding_dim=cfg.EMBEDDING_DIM)
frontend.load_state_dict(checkpoint['state_dict'])
# policy
# define policy
policy = DQNPolicy(
model=net,
optim=None,
discount_factor=args.gamma,
target_update_freq=args.target_update_freq,
is_double=True
).to(device)
# load pretrained model
checkpoint = torch.load(args.resume_path, map_location=device)
policy.load_state_dict(checkpoint['state_dict'])
best_score = checkpoint['best_score']
print('best score: ', best_score)
print("Loaded agent from: ", args.resume_path)
# result dir
exp_dir = args.resume_path.split('/best')[0]
res_dir = os.path.join(exp_dir, args.resume_path.split('/best_')[1].split('_policy')[0])
if not os.path.exists(res_dir):
os.makedirs(res_dir)
print(res_dir)
# test loop
q_net = policy.model
q_net.eval()
if not args.freeze_frontend:
frontend = q_net.get_frontend()
metrics = {
'HitRate_3F': 0,
'HitRate_3P': 0,
'HitRate_3R': 0,
'HitRate_0.5F': 0,
'HitRate_0.5P': 0,
'HitRate_0.5R': 0,
'PWF': 0,
'PWP': 0,
'PWR': 0,
'score': 0
}
with torch.no_grad():
with trange(len(val_dataset)) as t:
for k in t:
fp = val_dataset[k]
# if k < 26:
# continue
# if not fp.split('/')[-1].startswith('0603'):
# continue
print(fp)
env = tianshou_env.OMSSEnv(#q_net.module.get_frontend(),
frontend,
args.num_clusters,
fp,
args.seq_max_len, # TODO don't need this in val
cluster_encode=args.cluster_encode,
freeze_frontend=args.freeze_frontend,
mode='test')
song_action_list = [0]
state = env.reset()
label_list = [env._ref_labels[0]]
done = False
song_score = 0
song_reward_list = [0]
est_idxs = [0]
pre_action = 0
idx = 1
while not done:
format_state = state_to(state, device, args=args)
logits = policy.model(format_state)[0].detach().cpu().numpy()
action = np.argmax(logits)
song_action_list.append(int(action))
label_list.append(env._ref_labels[-1])
next_state, reward, done, info = env.step(action)
state = next_state
metrics['score'] += reward
song_score += reward
song_reward_list.append(int(reward))
# boundary evaluation
if int(action) != pre_action:
pre_action = action
est_idxs.append(idx)
idx += 1
# plot result for current song
times = (np.arange(len(song_action_list)) * cfg.eval_hop_size * cfg.BIN_TIME_LEN \
+ (cfg.CHUNK_LEN - cfg.time_lag_len) * cfg.BIN_TIME_LEN)
plt.rcParams['figure.figsize'] = (20, 12)
plt.subplot(3, 1, 1)
plt.plot(times, song_action_list, 'o', markersize=2)
plt.xlabel('time / s')
plt.yticks(range(0, args.num_clusters))
plt.ylabel('action')
song_num = fp.split('specs')[-1].split('.')[0][1:-4]
plt.title('Song No. {}, f1: {}, score: {}'.format(song_num, info['f1'], song_score))
plt.subplot(3, 1, 2)
plt.plot(times, song_reward_list, 'o', markersize=2)
plt.xlabel('time / s')
plt.ylabel('reward')
plt.subplot(3, 1, 3)
plt.plot(times, label_list, 'o', markersize=2)
plt.xlabel('time / s')
plt.ylabel('label')
#print(res_dir)
plt.savefig(os.path.join(res_dir, song_num + '.jpg'))
plt.close()
# boundary evaluation
end_idx = len(song_action_list) - 1
if end_idx in est_idxs:
est_seg_labels = np.array(song_action_list)[est_idxs[:-1]]
else:
est_seg_labels = np.array(song_action_list)[est_idxs]
est_idxs.append(end_idx)
est_times = times[est_idxs]
res = eval_seg(est_times,
est_seg_labels,
env._times,
env._labels[:-1])
for k in metrics:
if k != 'score':
metrics[k] += res[k]
t.set_description('f1: {}, '.format(info['f1']))
with open(os.path.join(res_dir, song_num + '.json'), 'w') as f:
f.write(str(res).replace("'", '"'))
f.close()
# save overall result
for k in metrics:
metrics[k] /= len(val_dataset)
with open(os.path.join(res_dir, 'res.json'), 'w') as f:
f.write(str(metrics).replace("'", '"'))
f.close()
def test_links(args):
# prepare dataset (file paths)
test_files = None
if cfg.test_csv:
# load test set indexs TODO
pass
else:
train_dataset, val_dataset, test_dataset = omss_train_val_test_split(cfg.val_pct, cfg.test_pct, test_files, args)
checkpoint = torch.load(args.pretrained)
frontend = Frontend((cfg.BIN, cfg.CHUNK_LEN), embedding_dim=cfg.EMBEDDING_DIM)
frontend.load_state_dict(checkpoint['state_dict'])
# result dir
exp_dir = 'links'
res_dir = os.path.join(exp_dir, cfg.dataset)
if not os.path.exists(res_dir):
os.makedirs(res_dir)
print(res_dir)
agent = LinksCluster(cluster_similarity_threshold=0.9,
subcluster_similarity_threshold=0.8,
pair_similarity_maximum=1.0,
store_vectors=True)
score = 0
f1 = 0
with torch.no_grad():
with trange(len(val_dataset)) as t:
for k in t:
fp = val_dataset[k]
print(fp)
env = tianshou_env.OMSSEnv(#q_net.module.get_frontend(),
frontend,
args.num_clusters,
fp,
args.seq_max_len, # TODO don't need this in val
cluster_encode=args.cluster_encode,
freeze_frontend=args.freeze_frontend,
mode='test')
song_action_list = [0]
state = env.reset()
label_list = [env._ref_labels[0]]
done = False
song_score = 0
song_reward_list = [0]
while not done:
action = agent.predict((state['cur_embedding']))
# print(action)
song_action_list.append(int(action))
label_list.append(env._ref_labels[-1])
# action = policy.take_action(state, env, args.test_eps, args.num_clusters)
next_state, reward, done, info = env.step(action)
state = next_state
score += reward
song_score += reward
song_reward_list.append(int(reward))
f1 += info['f1']
t.set_description('f1: {}'.format(info['f1']))
# plot result for current song
x = (np.arange(len(song_action_list)) * cfg.eval_hop_size * cfg.BIN_TIME_LEN \
+ (cfg.CHUNK_LEN - cfg.time_lag_len) * cfg.BIN_TIME_LEN)
plt.rcParams['figure.figsize'] = (20, 12)
plt.subplot(3, 1, 1)
plt.plot(x, song_action_list, 'o', markersize=2)
plt.xlabel('time / s')
plt.yticks(range(0, args.num_clusters))
plt.ylabel('action')
song_num = fp.split('specs')[-1].split('.')[0][1:-4]
plt.title('Song No. {}, f1: {}, score: {}'.format(song_num, info['f1'], song_score))
plt.subplot(3, 1, 2)
plt.plot(x, song_reward_list, 'o', markersize=2)
plt.xlabel('time / s')
plt.ylabel('reward')
plt.subplot(3, 1, 3)
plt.plot(x, label_list, 'o', markersize=2)
plt.xlabel('time / s')
plt.ylabel('label')
#print(res_dir)
plt.savefig(os.path.join(res_dir, song_num + '.jpg'))
plt.close()
score /= len(val_dataset)
f1 /= len(val_dataset)
if __name__ == '__main__':
args = get_args()
test(args)
# test_links(args)