/
test_codet.py
584 lines (523 loc) · 20.2 KB
/
test_codet.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
import argparse
import os
from copy import deepcopy
import seaborn as sns
import torch.optim as optim
from torch.utils.data import DataLoader
from coperception.datasets import V2XSimDet
from coperception.configs import Config, ConfigGlobal
from coperception.utils.CoDetModule import *
from coperception.utils.loss import *
from coperception.utils.mean_ap import eval_map
from coperception.models.det import *
from coperception.utils.detection_util import late_fusion
from coperception.utils.data_util import apply_pose_noise
def check_folder(folder_path):
if not os.path.exists(folder_path):
os.mkdir(folder_path)
return folder_path
@torch.no_grad()
def main(args):
config = Config("train", binary=True, only_det=True)
config_global = ConfigGlobal("train", binary=True, only_det=True)
need_log = args.log
num_workers = args.nworker
apply_late_fusion = args.apply_late_fusion
pose_noise = args.pose_noise
compress_level = args.compress_level
only_v2i = args.only_v2i
# Specify gpu device
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
device_num = torch.cuda.device_count()
print("device number", device_num)
config.inference = args.inference
if args.com == "upperbound":
flag = "upperbound"
elif args.com == "when2com":
flag = "when2com"
if args.inference == "argmax_test":
flag = "who2com"
if args.warp_flag:
flag = flag + "_warp"
elif args.com in {"v2v", "disco", "sum", "mean", "max", "cat", "agent"}:
flag = args.com
elif args.com == "lowerbound":
flag = "lowerbound"
if args.box_com:
flag += "_box_com"
else:
raise ValueError(f"com: {args.com} is not supported")
print("flag", flag)
config.flag = flag
config.split = "test"
num_agent = args.num_agent
# agent0 is the RSU
agent_idx_range = range(num_agent) if args.rsu else range(1, num_agent)
validation_dataset = V2XSimDet(
dataset_roots=[f"{args.data}/agent{i}" for i in agent_idx_range],
config=config,
config_global=config_global,
split="val",
val=True,
bound="upperbound" if args.com == "upperbound" else "lowerbound",
kd_flag=args.kd_flag,
rsu=args.rsu,
)
validation_data_loader = DataLoader(
validation_dataset, batch_size=1, shuffle=False, num_workers=num_workers
)
print("Validation dataset size:", len(validation_dataset))
if not args.rsu:
num_agent -= 1
if flag == "upperbound" or flag.startswith("lowerbound"):
model = FaFNet(
config, layer=args.layer, kd_flag=args.kd_flag, num_agent=num_agent
)
elif flag.startswith("when2com") or flag.startswith("who2com"):
# model = PixelwiseWeightedFusionSoftmax(config, layer=args.layer)
model = When2com(
config,
layer=args.layer,
warp_flag=args.warp_flag,
num_agent=num_agent,
compress_level=compress_level,
only_v2i=only_v2i,
)
elif args.com == "disco":
model = DiscoNet(
config,
layer=args.layer,
kd_flag=args.kd_flag,
num_agent=num_agent,
compress_level=compress_level,
only_v2i=only_v2i,
)
elif args.com == "sum":
model = SumFusion(
config,
layer=args.layer,
kd_flag=args.kd_flag,
num_agent=num_agent,
compress_level=compress_level,
only_v2i=only_v2i,
)
elif args.com == "mean":
model = MeanFusion(
config,
layer=args.layer,
kd_flag=args.kd_flag,
num_agent=num_agent,
compress_level=compress_level,
only_v2i=only_v2i,
)
elif args.com == "max":
model = MaxFusion(
config,
layer=args.layer,
kd_flag=args.kd_flag,
num_agent=num_agent,
compress_level=compress_level,
only_v2i=only_v2i,
)
elif args.com == "cat":
model = CatFusion(
config,
layer=args.layer,
kd_flag=args.kd_flag,
num_agent=num_agent,
compress_level=compress_level,
only_v2i=only_v2i,
)
elif args.com == "agent":
model = AgentWiseWeightedFusion(
config,
layer=args.layer,
kd_flag=args.kd_flag,
num_agent=num_agent,
compress_level=compress_level,
only_v2i=only_v2i,
)
elif args.com == "v2v":
model = V2VNet(
config,
gnn_iter_times=args.gnn_iter_times,
layer=args.layer,
layer_channel=256,
num_agent=num_agent,
compress_level=compress_level,
only_v2i=only_v2i,
)
model = nn.DataParallel(model)
model = model.to(device)
optimizer = optim.Adam(model.parameters(), lr=0.001)
criterion = {
"cls": SoftmaxFocalClassificationLoss(),
"loc": WeightedSmoothL1LocalizationLoss(),
}
fafmodule = FaFModule(model, model, config, optimizer, criterion, args.kd_flag)
model_save_path = args.resume[: args.resume.rfind("/")]
if args.inference == "argmax_test":
model_save_path = model_save_path.replace("when2com", "who2com")
os.makedirs(model_save_path, exist_ok=True)
log_file_name = os.path.join(model_save_path, "log.txt")
saver = open(log_file_name, "a")
saver.write("GPU number: {}\n".format(torch.cuda.device_count()))
saver.flush()
# Logging the details for this experiment
saver.write("command line: {}\n".format(" ".join(sys.argv[1:])))
saver.write(args.__repr__() + "\n\n")
saver.flush()
checkpoint = torch.load(
args.resume, map_location="cpu"
) # We have low GPU utilization for testing
start_epoch = checkpoint["epoch"] + 1
fafmodule.model.load_state_dict(checkpoint["model_state_dict"])
fafmodule.optimizer.load_state_dict(checkpoint["optimizer_state_dict"])
fafmodule.scheduler.load_state_dict(checkpoint["scheduler_state_dict"])
print("Load model from {}, at epoch {}".format(args.resume, start_epoch - 1))
# ===== eval =====
fafmodule.model.eval()
save_fig_path = [
check_folder(os.path.join(model_save_path, f"vis{i}")) for i in agent_idx_range
]
tracking_path = [
check_folder(os.path.join(model_save_path, f"tracking{i}"))
for i in agent_idx_range
]
# for local and global mAP evaluation
det_results_local = [[] for i in agent_idx_range]
annotations_local = [[] for i in agent_idx_range]
tracking_file = [set()] * num_agent
for cnt, sample in enumerate(validation_data_loader):
t = time.time()
(
padded_voxel_point_list,
padded_voxel_points_teacher_list,
label_one_hot_list,
reg_target_list,
reg_loss_mask_list,
anchors_map_list,
vis_maps_list,
gt_max_iou,
filenames,
target_agent_id_list,
num_agent_list,
trans_matrices_list,
) = zip(*sample)
print(filenames)
filename0 = filenames[0]
trans_matrices = torch.stack(tuple(trans_matrices_list), 1)
target_agent_ids = torch.stack(tuple(target_agent_id_list), 1)
num_all_agents = torch.stack(tuple(num_agent_list), 1)
# add pose noise
if pose_noise > 0:
apply_pose_noise(pose_noise, trans_matrices)
if not args.rsu:
num_all_agents -= 1
if flag == "upperbound":
padded_voxel_points = torch.cat(tuple(padded_voxel_points_teacher_list), 0)
else:
padded_voxel_points = torch.cat(tuple(padded_voxel_point_list), 0)
label_one_hot = torch.cat(tuple(label_one_hot_list), 0)
reg_target = torch.cat(tuple(reg_target_list), 0)
reg_loss_mask = torch.cat(tuple(reg_loss_mask_list), 0)
anchors_map = torch.cat(tuple(anchors_map_list), 0)
vis_maps = torch.cat(tuple(vis_maps_list), 0)
data = {
"bev_seq": padded_voxel_points.to(device),
"labels": label_one_hot.to(device),
"reg_targets": reg_target.to(device),
"anchors": anchors_map.to(device),
"vis_maps": vis_maps.to(device),
"reg_loss_mask": reg_loss_mask.to(device).type(dtype=torch.bool),
"target_agent_ids": target_agent_ids.to(device),
"num_agent": num_all_agents.to(device),
"trans_matrices": trans_matrices.to(device),
}
if flag == "lowerbound_box_com":
loss, cls_loss, loc_loss, result = fafmodule.predict_all_with_box_com(
data, data["trans_matrices"]
)
elif flag == "disco":
(
loss,
cls_loss,
loc_loss,
result,
save_agent_weight_list,
) = fafmodule.predict_all(data, 1, num_agent=num_agent)
else:
loss, cls_loss, loc_loss, result = fafmodule.predict_all(
data, 1, num_agent=num_agent
)
box_color_map = ["red", "yellow", "blue", "purple", "black", "orange"]
# If has RSU, do not count RSU's output into evaluation
eval_start_idx = 1 if args.rsu else 0
# local qualitative evaluation
for k in range(eval_start_idx, num_agent):
box_colors = None
if apply_late_fusion == 1 and len(result[k]) != 0:
pred_restore = result[k][0][0][0]["pred"]
score_restore = result[k][0][0][0]["score"]
selected_idx_restore = result[k][0][0][0]["selected_idx"]
data_agents = {
"bev_seq": torch.unsqueeze(padded_voxel_points[k, :, :, :, :], 1),
"reg_targets": torch.unsqueeze(reg_target[k, :, :, :, :, :], 0),
"anchors": torch.unsqueeze(anchors_map[k, :, :, :, :], 0),
}
temp = gt_max_iou[k]
if len(temp[0]["gt_box"]) == 0:
data_agents["gt_max_iou"] = []
else:
data_agents["gt_max_iou"] = temp[0]["gt_box"][0, :, :]
# late fusion
if apply_late_fusion == 1 and len(result[k]) != 0:
box_colors = late_fusion(
k, num_agent, result, trans_matrices, box_color_map
)
result_temp = result[k]
temp = {
"bev_seq": data_agents["bev_seq"][0, -1].cpu().numpy(),
"result": [] if len(result_temp) == 0 else result_temp[0][0],
"reg_targets": data_agents["reg_targets"].cpu().numpy()[0],
"anchors_map": data_agents["anchors"].cpu().numpy()[0],
"gt_max_iou": data_agents["gt_max_iou"],
}
det_results_local[k], annotations_local[k] = cal_local_mAP(
config, temp, det_results_local[k], annotations_local[k]
)
filename = str(filename0[0][0])
cut = filename[filename.rfind("agent") + 7 :]
seq_name = cut[: cut.rfind("_")]
idx = cut[cut.rfind("_") + 1 : cut.rfind("/")]
seq_save = os.path.join(save_fig_path[k], seq_name)
check_folder(seq_save)
idx_save = str(idx) + ".png"
temp_ = deepcopy(temp)
if args.visualization:
visualization(
config,
temp,
box_colors,
box_color_map,
apply_late_fusion,
os.path.join(seq_save, idx_save),
)
# # plot the cell-wise edge
# if flag == "disco" and k < len(save_agent_weight_list):
# one_agent_edge = save_agent_weight_list[k]
# for kk in range(len(one_agent_edge)):
# idx_edge_save = (
# str(idx) + "_edge_" + str(kk) + "_to_" + str(k) + ".png"
# )
# savename_edge = os.path.join(seq_save, idx_edge_save)
# sns.set()
# plt.savefig(savename_edge, dpi=500)
# plt.close(0)
# == tracking ==
if args.tracking:
scene, frame = filename.split("/")[-2].split("_")
det_file = os.path.join(tracking_path[k], f"det_{scene}.txt")
if scene not in tracking_file[k]:
det_file = open(det_file, "w")
tracking_file[k].add(scene)
else:
det_file = open(det_file, "a")
det_corners = get_det_corners(config, temp_)
for ic, c in enumerate(det_corners):
det_file.write(
",".join(
[
str(
int(frame) + 1
), # frame idx is 1-based for tracking
"-1",
"{:.2f}".format(c[0]),
"{:.2f}".format(c[1]),
"{:.2f}".format(c[2]),
"{:.2f}".format(c[3]),
str(result_temp[0][0][0]["score"][ic]),
"-1",
"-1",
"-1",
]
)
+ "\n"
)
det_file.flush()
det_file.close()
# restore data before late-fusion
if apply_late_fusion == 1 and len(result[k]) != 0:
result[k][0][0][0]["pred"] = pred_restore
result[k][0][0][0]["score"] = score_restore
result[k][0][0][0]["selected_idx"] = selected_idx_restore
print("Validation scene {}, at frame {}".format(seq_name, idx))
print("Takes {} s\n".format(str(time.time() - t)))
logger_root = args.logpath if args.logpath != "" else "logs"
logger_root = os.path.join(
logger_root, f"{flag}_eval", "with_rsu" if args.rsu else "no_rsu"
)
os.makedirs(logger_root, exist_ok=True)
log_file_path = os.path.join(logger_root, "log_test.txt")
log_file = open(log_file_path, "w")
def print_and_write_log(log_str):
print(log_str)
log_file.write(log_str + "\n")
mean_ap_local = []
# local mAP evaluation
det_results_all_local = []
annotations_all_local = []
for k in range(eval_start_idx, num_agent):
if type(det_results_local[k]) != list or len(det_results_local[k]) == 0:
continue
print_and_write_log("Local mAP@0.5 from agent {}".format(k))
mean_ap, _ = eval_map(
det_results_local[k],
annotations_local[k],
scale_ranges=None,
iou_thr=0.5,
dataset=None,
logger=None,
)
mean_ap_local.append(mean_ap)
print_and_write_log("Local mAP@0.7 from agent {}".format(k))
ean_ap, _ = eval_map(
det_results_local[k],
annotations_local[k],
scale_ranges=None,
iou_thr=0.7,
dataset=None,
logger=None,
)
mean_ap_local.append(mean_ap)
det_results_all_local += det_results_local[k]
annotations_all_local += annotations_local[k]
mean_ap_local_average, _ = eval_map(
det_results_all_local,
annotations_all_local,
scale_ranges=None,
iou_thr=0.5,
dataset=None,
logger=None,
)
mean_ap_local.append(mean_ap_local_average)
mean_ap_local_average, _ = eval_map(
det_results_all_local,
annotations_all_local,
scale_ranges=None,
iou_thr=0.7,
dataset=None,
logger=None,
)
mean_ap_local.append(mean_ap_local_average)
print_and_write_log(
"Quantitative evaluation results of model from {}, at epoch {}".format(
args.resume, start_epoch - 1
)
)
for k in range(num_agent - 1 if args.rsu else num_agent):
print_and_write_log(
"agent{} mAP@0.5 is {} and mAP@0.7 is {}".format(
k + 1, mean_ap_local[k * 2], mean_ap_local[(k * 2) + 1]
)
)
print_and_write_log(
"average local mAP@0.5 is {} and average local mAP@0.7 is {}".format(
mean_ap_local[-2], mean_ap_local[-1]
)
)
if need_log:
saver.close()
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"-d",
"--data",
default=None,
type=str,
help="The path to the preprocessed sparse BEV training data",
)
parser.add_argument("--nepoch", default=100, type=int, help="Number of epochs")
parser.add_argument("--nworker", default=1, type=int, help="Number of workers")
parser.add_argument("--lr", default=0.001, type=float, help="Initial learning rate")
parser.add_argument("--log", action="store_true", help="Whether to log")
parser.add_argument("--logpath", default="", help="The path to the output log file")
parser.add_argument(
"--resume",
default="",
type=str,
help="The path to the saved model that is loaded to resume training",
)
parser.add_argument(
"--resume_teacher",
default="",
type=str,
help="The path to the saved teacher model that is loaded to resume training",
)
parser.add_argument(
"--layer",
default=3,
type=int,
help="Communicate which layer in the single layer com mode",
)
parser.add_argument(
"--warp_flag", default=0, type=int, help="Whether to use pose info for When2com"
)
parser.add_argument(
"--kd_flag",
default=0,
type=int,
help="Whether to enable distillation (only DiscNet is 1 )",
)
parser.add_argument("--kd_weight", default=100000, type=int, help="KD loss weight")
parser.add_argument(
"--gnn_iter_times",
default=3,
type=int,
help="Number of message passing for V2VNet",
)
parser.add_argument(
"--visualization", type=int, default=0, help="Visualize validation result"
)
parser.add_argument(
"--com",
default="",
type=str,
help="lowerbound/upperbound/disco/when2com/v2v/sum/mean/max/cat/agent",
)
parser.add_argument("--inference", type=str)
parser.add_argument("--tracking", action="store_true")
parser.add_argument("--box_com", action="store_true")
parser.add_argument("--rsu", default=0, type=int, help="0: no RSU, 1: RSU")
# scene_batch => batch size in each scene
parser.add_argument(
"--num_agent", default=6, type=int, help="The total number of agents"
)
parser.add_argument(
"--apply_late_fusion",
default=0,
type=int,
help="1: apply late fusion. 0: no late fusion",
)
parser.add_argument(
"--compress_level",
default=0,
type=int,
help="Compress the communication layer channels by 2**x times in encoder",
)
parser.add_argument(
"--pose_noise",
default=0,
type=float,
help="draw noise from normal distribution with given mean (in meters), apply to transformation matrix.",
)
parser.add_argument(
"--only_v2i",
default=0,
type=int,
help="1: only v2i, 0: v2v and v2i",
)
torch.multiprocessing.set_sharing_strategy("file_system")
args = parser.parse_args()
print(args)
main(args)