-
Notifications
You must be signed in to change notification settings - Fork 1
/
convert_gt_map_json.py
1090 lines (953 loc) · 47.3 KB
/
convert_gt_map_json.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
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os
import json
import copy
import tempfile
import pickle
from typing import Dict, List
from mmcv.fileio.io import dump,load
import numpy as np
from mmcv.datasets import NuScenesDataset
import pyquaternion
import mmcv
from os import path as osp
import torch
import numpy as np
from mmcv.nuscenes.eval.common.utils import quaternion_yaw, Quaternion
from mmcv.nuscenes.eval.common.utils import center_distance
from mmcv.utils.visual import save_tensor
from mmcv.parallel import DataContainer as DC
import random
from mmcv.core.bbox.structures.lidar_box3d import LiDARInstance3DBoxes
from mmcv.nuscenes.utils.data_classes import Box as NuScenesBox
from mmcv.core.bbox.structures.nuscenes_box import CustomNuscenesBox
from shapely import affinity, ops
from shapely.geometry import LineString, box, MultiPolygon, MultiLineString
from mmcv.datasets.pipelines import to_tensor
from mmcv.nuscenes.map_expansion.map_api import NuScenesMap, NuScenesMapExplorer
from mmcv.nuscenes.eval.detection.constants import DETECTION_NAMES
from tqdm import tqdm
import argparse
def parse_args():
parser = argparse.ArgumentParser(description='')
parser.add_argument('--data_root', type=str, help='Data root path', default='/data/nuplan/dataset')
parser.add_argument('--pkl_path', type=str, help='Pickle file path', default='/data/nuplan/ann_files/test/sampled_test_300.pkl')
parser.add_argument('--save_path', type=str, help='Save path', default='/data/nuplan/ann_files/test/eval_map.json')
args = parser.parse_args()
return args
class LiDARInstanceLines(object):
"""Line instance in LIDAR coordinates
"""
def __init__(self,
instance_line_list,
sample_dist=1,
num_samples=250,
padding=False,
fixed_num=-1,
padding_value=-10000,
patch_size=None):
assert isinstance(instance_line_list, list)
assert patch_size is not None
if len(instance_line_list) != 0:
assert isinstance(instance_line_list[0], LineString)
self.patch_size = patch_size
self.max_x = self.patch_size[1] / 2
self.max_y = self.patch_size[0] / 2
self.sample_dist = sample_dist
self.num_samples = num_samples
self.padding = padding
self.fixed_num = fixed_num
self.padding_value = padding_value
self.instance_list = instance_line_list
@property
def start_end_points(self):
"""
return torch.Tensor([N,4]), in xstart, ystart, xend, yend form
"""
assert len(self.instance_list) != 0
instance_se_points_list = []
for instance in self.instance_list:
se_points = []
se_points.extend(instance.coords[0])
se_points.extend(instance.coords[-1])
instance_se_points_list.append(se_points)
instance_se_points_array = np.array(instance_se_points_list)
instance_se_points_tensor = to_tensor(instance_se_points_array)
instance_se_points_tensor = instance_se_points_tensor.to(
dtype=torch.float32)
instance_se_points_tensor[:,0] = torch.clamp(instance_se_points_tensor[:,0], min=-self.max_x,max=self.max_x)
instance_se_points_tensor[:,1] = torch.clamp(instance_se_points_tensor[:,1], min=-self.max_y,max=self.max_y)
instance_se_points_tensor[:,2] = torch.clamp(instance_se_points_tensor[:,2], min=-self.max_x,max=self.max_x)
instance_se_points_tensor[:,3] = torch.clamp(instance_se_points_tensor[:,3], min=-self.max_y,max=self.max_y)
return instance_se_points_tensor
@property
def bbox(self):
"""
return torch.Tensor([N,4]), in xmin, ymin, xmax, ymax form
"""
assert len(self.instance_list) != 0
instance_bbox_list = []
for instance in self.instance_list:
# bounds is bbox: [xmin, ymin, xmax, ymax]
instance_bbox_list.append(instance.bounds)
instance_bbox_array = np.array(instance_bbox_list)
instance_bbox_tensor = to_tensor(instance_bbox_array)
instance_bbox_tensor = instance_bbox_tensor.to(
dtype=torch.float32)
instance_bbox_tensor[:,0] = torch.clamp(instance_bbox_tensor[:,0], min=-self.max_x,max=self.max_x)
instance_bbox_tensor[:,1] = torch.clamp(instance_bbox_tensor[:,1], min=-self.max_y,max=self.max_y)
instance_bbox_tensor[:,2] = torch.clamp(instance_bbox_tensor[:,2], min=-self.max_x,max=self.max_x)
instance_bbox_tensor[:,3] = torch.clamp(instance_bbox_tensor[:,3], min=-self.max_y,max=self.max_y)
return instance_bbox_tensor
@property
def fixed_num_sampled_points(self):
"""
return torch.Tensor([N,fixed_num,2]), in xmin, ymin, xmax, ymax form
N means the num of instances
"""
assert len(self.instance_list) != 0
instance_points_list = []
for instance in self.instance_list:
distances = np.linspace(0, instance.length, self.fixed_num)
sampled_points = np.array([list(instance.interpolate(distance).coords) for distance in distances]).reshape(-1, 2)
instance_points_list.append(sampled_points)
instance_points_array = np.array(instance_points_list)
instance_points_tensor = to_tensor(instance_points_array)
instance_points_tensor = instance_points_tensor.to(
dtype=torch.float32)
instance_points_tensor[:,:,0] = torch.clamp(instance_points_tensor[:,:,0], min=-self.max_x,max=self.max_x)
instance_points_tensor[:,:,1] = torch.clamp(instance_points_tensor[:,:,1], min=-self.max_y,max=self.max_y)
return instance_points_tensor
@property
def fixed_num_sampled_points_ambiguity(self):
"""
return torch.Tensor([N,fixed_num,2]), in xmin, ymin, xmax, ymax form
N means the num of instances
"""
assert len(self.instance_list) != 0
instance_points_list = []
for instance in self.instance_list:
distances = np.linspace(0, instance.length, self.fixed_num)
sampled_points = np.array([list(instance.interpolate(distance).coords) for distance in distances]).reshape(-1, 2)
instance_points_list.append(sampled_points)
instance_points_array = np.array(instance_points_list)
instance_points_tensor = to_tensor(instance_points_array)
instance_points_tensor = instance_points_tensor.to(
dtype=torch.float32)
instance_points_tensor[:,:,0] = torch.clamp(instance_points_tensor[:,:,0], min=-self.max_x,max=self.max_x)
instance_points_tensor[:,:,1] = torch.clamp(instance_points_tensor[:,:,1], min=-self.max_y,max=self.max_y)
instance_points_tensor = instance_points_tensor.unsqueeze(1)
return instance_points_tensor
@property
def fixed_num_sampled_points_torch(self):
"""
return torch.Tensor([N,fixed_num,2]), in xmin, ymin, xmax, ymax form
N means the num of instances
"""
assert len(self.instance_list) != 0
instance_points_list = []
for instance in self.instance_list:
# distances = np.linspace(0, instance.length, self.fixed_num)
# sampled_points = np.array([list(instance.interpolate(distance).coords) for distance in distances]).reshape(-1, 2)
poly_pts = to_tensor(np.array(list(instance.coords)))
poly_pts = poly_pts.unsqueeze(0).permute(0,2,1)
sampled_pts = torch.nn.functional.interpolate(poly_pts,size=(self.fixed_num),mode='linear',align_corners=True)
sampled_pts = sampled_pts.permute(0,2,1).squeeze(0)
instance_points_list.append(sampled_pts)
# instance_points_array = np.array(instance_points_list)
# instance_points_tensor = to_tensor(instance_points_array)
instance_points_tensor = torch.stack(instance_points_list,dim=0)
instance_points_tensor = instance_points_tensor.to(
dtype=torch.float32)
instance_points_tensor[:,:,0] = torch.clamp(instance_points_tensor[:,:,0], min=-self.max_x,max=self.max_x)
instance_points_tensor[:,:,1] = torch.clamp(instance_points_tensor[:,:,1], min=-self.max_y,max=self.max_y)
return instance_points_tensor
@property
def shift_fixed_num_sampled_points(self):
"""
return [instances_num, num_shifts, fixed_num, 2]
"""
fixed_num_sampled_points = self.fixed_num_sampled_points
instances_list = []
is_poly = False
# is_line = False
# import pdb;pdb.set_trace()
for fixed_num_pts in fixed_num_sampled_points:
# [fixed_num, 2]
is_poly = fixed_num_pts[0].equal(fixed_num_pts[-1])
fixed_num = fixed_num_pts.shape[0]
shift_pts_list = []
if is_poly:
# import pdb;pdb.set_trace()
for shift_right_i in range(fixed_num):
shift_pts_list.append(fixed_num_pts.roll(shift_right_i,0))
else:
shift_pts_list.append(fixed_num_pts)
shift_pts_list.append(fixed_num_pts.flip(0))
shift_pts = torch.stack(shift_pts_list,dim=0)
shift_pts[:,:,0] = torch.clamp(shift_pts[:,:,0], min=-self.max_x,max=self.max_x)
shift_pts[:,:,1] = torch.clamp(shift_pts[:,:,1], min=-self.max_y,max=self.max_y)
if not is_poly:
padding = torch.full([fixed_num-shift_pts.shape[0],fixed_num,2], self.padding_value)
shift_pts = torch.cat([shift_pts,padding],dim=0)
# padding = np.zeros((self.num_samples - len(sampled_points), 2))
# sampled_points = np.concatenate([sampled_points, padding], axis=0)
instances_list.append(shift_pts)
instances_tensor = torch.stack(instances_list, dim=0)
instances_tensor = instances_tensor.to(
dtype=torch.float32)
return instances_tensor
@property
def shift_fixed_num_sampled_points_v1(self):
"""
return [instances_num, num_shifts, fixed_num, 2]
"""
fixed_num_sampled_points = self.fixed_num_sampled_points
instances_list = []
is_poly = False
# is_line = False
# import pdb;pdb.set_trace()
for fixed_num_pts in fixed_num_sampled_points:
# [fixed_num, 2]
is_poly = fixed_num_pts[0].equal(fixed_num_pts[-1])
pts_num = fixed_num_pts.shape[0]
shift_num = pts_num - 1
if is_poly:
pts_to_shift = fixed_num_pts[:-1,:]
shift_pts_list = []
if is_poly:
for shift_right_i in range(shift_num):
shift_pts_list.append(pts_to_shift.roll(shift_right_i,0))
else:
shift_pts_list.append(fixed_num_pts)
shift_pts_list.append(fixed_num_pts.flip(0))
shift_pts = torch.stack(shift_pts_list,dim=0)
if is_poly:
_, _, num_coords = shift_pts.shape
tmp_shift_pts = shift_pts.new_zeros((shift_num, pts_num, num_coords))
tmp_shift_pts[:,:-1,:] = shift_pts
tmp_shift_pts[:,-1,:] = shift_pts[:,0,:]
shift_pts = tmp_shift_pts
shift_pts[:,:,0] = torch.clamp(shift_pts[:,:,0], min=-self.max_x,max=self.max_x)
shift_pts[:,:,1] = torch.clamp(shift_pts[:,:,1], min=-self.max_y,max=self.max_y)
if not is_poly:
padding = torch.full([shift_num-shift_pts.shape[0],pts_num,2], self.padding_value)
shift_pts = torch.cat([shift_pts,padding],dim=0)
# padding = np.zeros((self.num_samples - len(sampled_points), 2))
# sampled_points = np.concatenate([sampled_points, padding], axis=0)
instances_list.append(shift_pts)
instances_tensor = torch.stack(instances_list, dim=0)
instances_tensor = instances_tensor.to(
dtype=torch.float32)
return instances_tensor
@property
def shift_fixed_num_sampled_points_v2(self):
"""
return [instances_num, num_shifts, fixed_num, 2]
"""
assert len(self.instance_list) != 0
instances_list = []
for instance in self.instance_list:
distances = np.linspace(0, instance.length, self.fixed_num)
poly_pts = np.array(list(instance.coords))
start_pts = poly_pts[0]
end_pts = poly_pts[-1]
is_poly = np.equal(start_pts, end_pts)
is_poly = is_poly.all()
shift_pts_list = []
pts_num, coords_num = poly_pts.shape
shift_num = pts_num - 1
final_shift_num = self.fixed_num - 1
if is_poly:
pts_to_shift = poly_pts[:-1,:]
for shift_right_i in range(shift_num):
shift_pts = np.roll(pts_to_shift,shift_right_i,axis=0)
pts_to_concat = shift_pts[0]
pts_to_concat = np.expand_dims(pts_to_concat,axis=0)
shift_pts = np.concatenate((shift_pts,pts_to_concat),axis=0)
shift_instance = LineString(shift_pts)
shift_sampled_points = np.array([list(shift_instance.interpolate(distance).coords) for distance in distances]).reshape(-1, 2)
shift_pts_list.append(shift_sampled_points)
# import pdb;pdb.set_trace()
else:
sampled_points = np.array([list(instance.interpolate(distance).coords) for distance in distances]).reshape(-1, 2)
flip_sampled_points = np.flip(sampled_points, axis=0)
shift_pts_list.append(sampled_points)
shift_pts_list.append(flip_sampled_points)
multi_shifts_pts = np.stack(shift_pts_list,axis=0)
shifts_num,_,_ = multi_shifts_pts.shape
if shifts_num > final_shift_num:
index = np.random.choice(multi_shifts_pts.shape[0], final_shift_num, replace=False)
multi_shifts_pts = multi_shifts_pts[index]
multi_shifts_pts_tensor = to_tensor(multi_shifts_pts)
multi_shifts_pts_tensor = multi_shifts_pts_tensor.to(
dtype=torch.float32)
multi_shifts_pts_tensor[:,:,0] = torch.clamp(multi_shifts_pts_tensor[:,:,0], min=-self.max_x,max=self.max_x)
multi_shifts_pts_tensor[:,:,1] = torch.clamp(multi_shifts_pts_tensor[:,:,1], min=-self.max_y,max=self.max_y)
# if not is_poly:
if multi_shifts_pts_tensor.shape[0] < final_shift_num:
padding = torch.full([final_shift_num-multi_shifts_pts_tensor.shape[0],self.fixed_num,2], self.padding_value)
multi_shifts_pts_tensor = torch.cat([multi_shifts_pts_tensor,padding],dim=0)
instances_list.append(multi_shifts_pts_tensor)
instances_tensor = torch.stack(instances_list, dim=0)
instances_tensor = instances_tensor.to(
dtype=torch.float32)
return instances_tensor
@property
def shift_fixed_num_sampled_points_v3(self):
"""
return [instances_num, num_shifts, fixed_num, 2]
"""
assert len(self.instance_list) != 0
instances_list = []
for instance in self.instance_list:
distances = np.linspace(0, instance.length, self.fixed_num)
poly_pts = np.array(list(instance.coords))
start_pts = poly_pts[0]
end_pts = poly_pts[-1]
is_poly = np.equal(start_pts, end_pts)
is_poly = is_poly.all()
shift_pts_list = []
pts_num, coords_num = poly_pts.shape
shift_num = pts_num - 1
final_shift_num = self.fixed_num - 1
if is_poly:
pts_to_shift = poly_pts[:-1,:]
for shift_right_i in range(shift_num):
shift_pts = np.roll(pts_to_shift,shift_right_i,axis=0)
pts_to_concat = shift_pts[0]
pts_to_concat = np.expand_dims(pts_to_concat,axis=0)
shift_pts = np.concatenate((shift_pts,pts_to_concat),axis=0)
shift_instance = LineString(shift_pts)
shift_sampled_points = np.array([list(shift_instance.interpolate(distance).coords) for distance in distances]).reshape(-1, 2)
shift_pts_list.append(shift_sampled_points)
flip_pts_to_shift = np.flip(pts_to_shift, axis=0)
for shift_right_i in range(shift_num):
shift_pts = np.roll(flip_pts_to_shift,shift_right_i,axis=0)
pts_to_concat = shift_pts[0]
pts_to_concat = np.expand_dims(pts_to_concat,axis=0)
shift_pts = np.concatenate((shift_pts,pts_to_concat),axis=0)
shift_instance = LineString(shift_pts)
shift_sampled_points = np.array([list(shift_instance.interpolate(distance).coords) for distance in distances]).reshape(-1, 2)
shift_pts_list.append(shift_sampled_points)
# import pdb;pdb.set_trace()
else:
sampled_points = np.array([list(instance.interpolate(distance).coords) for distance in distances]).reshape(-1, 2)
flip_sampled_points = np.flip(sampled_points, axis=0)
shift_pts_list.append(sampled_points)
shift_pts_list.append(flip_sampled_points)
multi_shifts_pts = np.stack(shift_pts_list,axis=0)
shifts_num,_,_ = multi_shifts_pts.shape
# import pdb;pdb.set_trace()
if shifts_num > 2*final_shift_num:
index = np.random.choice(shift_num, final_shift_num, replace=False)
flip0_shifts_pts = multi_shifts_pts[index]
flip1_shifts_pts = multi_shifts_pts[index+shift_num]
multi_shifts_pts = np.concatenate((flip0_shifts_pts,flip1_shifts_pts),axis=0)
multi_shifts_pts_tensor = to_tensor(multi_shifts_pts)
multi_shifts_pts_tensor = multi_shifts_pts_tensor.to(
dtype=torch.float32)
multi_shifts_pts_tensor[:,:,0] = torch.clamp(multi_shifts_pts_tensor[:,:,0], min=-self.max_x,max=self.max_x)
multi_shifts_pts_tensor[:,:,1] = torch.clamp(multi_shifts_pts_tensor[:,:,1], min=-self.max_y,max=self.max_y)
# if not is_poly:
if multi_shifts_pts_tensor.shape[0] < 2*final_shift_num:
padding = torch.full([final_shift_num*2-multi_shifts_pts_tensor.shape[0],self.fixed_num,2], self.padding_value)
multi_shifts_pts_tensor = torch.cat([multi_shifts_pts_tensor,padding],dim=0)
instances_list.append(multi_shifts_pts_tensor)
instances_tensor = torch.stack(instances_list, dim=0)
instances_tensor = instances_tensor.to(
dtype=torch.float32)
return instances_tensor
@property
def shift_fixed_num_sampled_points_v4(self):
"""
return [instances_num, num_shifts, fixed_num, 2]
"""
fixed_num_sampled_points = self.fixed_num_sampled_points
instances_list = []
is_poly = False
# is_line = False
# import pdb;pdb.set_trace()
for fixed_num_pts in fixed_num_sampled_points:
# [fixed_num, 2]
is_poly = fixed_num_pts[0].equal(fixed_num_pts[-1])
pts_num = fixed_num_pts.shape[0]
shift_num = pts_num - 1
shift_pts_list = []
if is_poly:
pts_to_shift = fixed_num_pts[:-1,:]
for shift_right_i in range(shift_num):
shift_pts_list.append(pts_to_shift.roll(shift_right_i,0))
flip_pts_to_shift = pts_to_shift.flip(0)
for shift_right_i in range(shift_num):
shift_pts_list.append(flip_pts_to_shift.roll(shift_right_i,0))
else:
shift_pts_list.append(fixed_num_pts)
shift_pts_list.append(fixed_num_pts.flip(0))
shift_pts = torch.stack(shift_pts_list,dim=0)
if is_poly:
_, _, num_coords = shift_pts.shape
tmp_shift_pts = shift_pts.new_zeros((shift_num*2, pts_num, num_coords))
tmp_shift_pts[:,:-1,:] = shift_pts
tmp_shift_pts[:,-1,:] = shift_pts[:,0,:]
shift_pts = tmp_shift_pts
shift_pts[:,:,0] = torch.clamp(shift_pts[:,:,0], min=-self.max_x,max=self.max_x)
shift_pts[:,:,1] = torch.clamp(shift_pts[:,:,1], min=-self.max_y,max=self.max_y)
if not is_poly:
padding = torch.full([shift_num*2-shift_pts.shape[0],pts_num,2], self.padding_value)
shift_pts = torch.cat([shift_pts,padding],dim=0)
# padding = np.zeros((self.num_samples - len(sampled_points), 2))
# sampled_points = np.concatenate([sampled_points, padding], axis=0)
instances_list.append(shift_pts)
instances_tensor = torch.stack(instances_list, dim=0)
instances_tensor = instances_tensor.to(
dtype=torch.float32)
return instances_tensor
@property
def shift_fixed_num_sampled_points_torch(self):
"""
return [instances_num, num_shifts, fixed_num, 2]
"""
fixed_num_sampled_points = self.fixed_num_sampled_points_torch
instances_list = []
is_poly = False
# is_line = False
# import pdb;pdb.set_trace()
for fixed_num_pts in fixed_num_sampled_points:
# [fixed_num, 2]
is_poly = fixed_num_pts[0].equal(fixed_num_pts[-1])
fixed_num = fixed_num_pts.shape[0]
shift_pts_list = []
if is_poly:
# import pdb;pdb.set_trace()
for shift_right_i in range(fixed_num):
shift_pts_list.append(fixed_num_pts.roll(shift_right_i,0))
else:
shift_pts_list.append(fixed_num_pts)
shift_pts_list.append(fixed_num_pts.flip(0))
shift_pts = torch.stack(shift_pts_list,dim=0)
shift_pts[:,:,0] = torch.clamp(shift_pts[:,:,0], min=-self.max_x,max=self.max_x)
shift_pts[:,:,1] = torch.clamp(shift_pts[:,:,1], min=-self.max_y,max=self.max_y)
if not is_poly:
padding = torch.full([fixed_num-shift_pts.shape[0],fixed_num,2], self.padding_value)
shift_pts = torch.cat([shift_pts,padding],dim=0)
# padding = np.zeros((self.num_samples - len(sampled_points), 2))
# sampled_points = np.concatenate([sampled_points, padding], axis=0)
instances_list.append(shift_pts)
instances_tensor = torch.stack(instances_list, dim=0)
instances_tensor = instances_tensor.to(
dtype=torch.float32)
return instances_tensor
# @property
# def polyline_points(self):
# """
# return [[x0,y0],[x1,y1],...]
# """
# assert len(self.instance_list) != 0
# for instance in self.instance_list:
class VectorizedLocalMap(object):
CLASS2LABEL = {
'road_divider': 0,
'lane_divider': 0,
'ped_crossing': 1,
'contours': 2,
'others': -1
}
def __init__(self,
dataroot,
patch_size,
map_classes=['divider','ped_crossing','boundary'],
line_classes=['road_divider', 'lane_divider'],
ped_crossing_classes=['ped_crossing'],
contour_classes=['road_segment', 'lane'],
sample_dist=1,
num_samples=250,
padding=False,
fixed_ptsnum_per_line=-1,
padding_value=-10000,
MAPS = ['singapore-onenorth', 'singapore-hollandvillage', 'singapore-queenstown', 'boston-seaport',
'us-ma-boston', 'sg-one-north', 'us-nv-las-vegas-strip', 'us-pa-pittsburgh-hazelwood']
):
'''
Args:
fixed_ptsnum_per_line = -1 : no fixed num
'''
super().__init__()
self.data_root = dataroot
self.MAPS = MAPS
self.vec_classes = map_classes
self.line_classes = line_classes
self.ped_crossing_classes = ped_crossing_classes
self.polygon_classes = contour_classes
self.nusc_maps = {}
self.map_explorer = {}
try:
for loc in self.MAPS:
self.nusc_maps[loc] = NuScenesMap(dataroot=self.data_root, map_name=loc)
self.map_explorer[loc] = NuScenesMapExplorer(self.nusc_maps[loc])
except:
map_path = os.path.join(self.data_root, 'maps','expansion')
self.MAPS = [i.replace(".json","") for i in os.listdir(map_path)]
for loc in self.MAPS:
self.nusc_maps[loc] = NuScenesMap(dataroot=self.data_root, map_name=loc)
self.map_explorer[loc] = NuScenesMapExplorer(self.nusc_maps[loc])
self.patch_size = patch_size
self.sample_dist = sample_dist
self.num_samples = num_samples
self.padding = padding
self.fixed_num = fixed_ptsnum_per_line
self.padding_value = padding_value
def gen_vectorized_samples(self, location, lidar2global_translation, lidar2global_rotation):
'''
use lidar2global to get gt map layers
'''
map_pose = lidar2global_translation[:2]
rotation = Quaternion(lidar2global_rotation)
patch_box = (map_pose[0], map_pose[1], self.patch_size[0], self.patch_size[1])
patch_angle = quaternion_yaw(rotation) / np.pi * 180
# import pdb;pdb.set_trace()
vectors = []
for vec_class in self.vec_classes:
if vec_class == 'divider':
line_geom = self.get_map_geom(patch_box, patch_angle, self.line_classes, location)
line_instances_dict = self.line_geoms_to_instances(line_geom)
line_vector_list = self.line_geoms_to_vectors(line_geom)
for line_type, instances in line_vector_list.items():
for instance in instances:
vectors.append(
{
"pts":instance[0].tolist(),
"pts_num": len(instance[0].tolist()),
"cls_name": vec_class,
"type": 0
}
)
elif vec_class == 'ped_crossing':
ped_geom = self.get_map_geom(patch_box, patch_angle, self.ped_crossing_classes, location)
# ped_vector_list = self.ped_poly_geoms_to_vectors_1(ped_geom)
# ped_vector_list3 = self.ped_geoms_to_vectors(ped_geom)
ped_instance_list = self.ped_poly_geoms_to_instances(ped_geom)
# import pdb;pdb.set_trace()
for instance in ped_instance_list:
distances = np.linspace(0, instance.length, self.fixed_num)
sampled_points = np.array([list(instance.interpolate(distance).coords) for distance in distances]).reshape(-1, 2)
vectors.append(
{
"pts":sampled_points.tolist(),
"pts_num": len(sampled_points.tolist()),
"cls_name": vec_class,
"type": 1
}
)
elif vec_class == 'boundary':
polygon_geom = self.get_map_geom(patch_box, patch_angle, self.polygon_classes, location)
# import pdb;pdb.set_trace()
poly_vector_list = self.poly_geoms_to_vectors(polygon_geom)
# import pdb;pdb.set_trace()
for contour in poly_vector_list:
vectors.append(
{
"pts":contour[0].tolist(),
"pts_num": len(contour[0].tolist()),
"cls_name": vec_class,
"type": 2
}
)
else:
raise ValueError(f'WRONG vec_class: {vec_class}')
# filter out -1
filtered_vectors = []
gt_pts_loc_3d = []
gt_pts_num_3d = []
gt_labels = []
gt_instance = []
# for instance, type in vectors:
# if type != -1:
# gt_instance.append(instance)
# gt_labels.append(type)
# gt_instance = LiDARInstanceLines(gt_instance,self.sample_dist,
# self.num_samples, self.padding, self.fixed_num, self.padding_value, patch_size=self.patch_size)
# anns_results = dict(
# gt_vecs_pts_loc=gt_instance,
# gt_vecs_label=gt_labels,
# )
# import pdb;pdb.set_trace()
return vectors
def get_map_geom(self, patch_box, patch_angle, layer_names, location):
map_geom = []
for layer_name in layer_names:
if layer_name in self.line_classes:
# import pdb;pdb.set_trace()
geoms = self.get_divider_line(patch_box, patch_angle, layer_name, location)
# import pdb;pdb.set_trace()
# geoms = self.map_explorer[location]._get_layer_line(patch_box, patch_angle, layer_name)
map_geom.append((layer_name, geoms))
elif layer_name in self.polygon_classes:
geoms = self.get_contour_line(patch_box, patch_angle, layer_name, location)
# geoms = self.map_explorer[location]._get_layer_polygon(patch_box, patch_angle, layer_name)
map_geom.append((layer_name, geoms))
elif layer_name in self.ped_crossing_classes:
geoms = self.get_ped_crossing_line(patch_box, patch_angle, location)
# geoms = self.map_explorer[location]._get_layer_polygon(patch_box, patch_angle, layer_name)
map_geom.append((layer_name, geoms))
return map_geom
def _one_type_line_geom_to_vectors(self, line_geom):
line_vectors = []
for line in line_geom:
if not line.is_empty:
if line.geom_type == 'MultiLineString':
for single_line in line.geoms:
line_vectors.append(self.sample_pts_from_line(single_line))
elif line.geom_type == 'LineString':
line_vectors.append(self.sample_pts_from_line(line))
else:
raise NotImplementedError
return line_vectors
def _one_type_line_geom_to_instances(self, line_geom):
line_instances = []
for line in line_geom:
if not line.is_empty:
if line.geom_type == 'MultiLineString':
for single_line in line.geoms:
line_instances.append(single_line)
elif line.geom_type == 'LineString':
line_instances.append(line)
else:
raise NotImplementedError
return line_instances
def poly_geoms_to_vectors(self, polygon_geom):
roads = polygon_geom[0][1]
lanes = polygon_geom[1][1]
union_roads = ops.unary_union(roads)
union_lanes = ops.unary_union(lanes)
union_segments = ops.unary_union([union_roads, union_lanes])
max_x = self.patch_size[1] / 2
max_y = self.patch_size[0] / 2
local_patch = box(-max_x + 0.2, -max_y + 0.2, max_x - 0.2, max_y - 0.2)
exteriors = []
interiors = []
if union_segments.geom_type != 'MultiPolygon':
union_segments = MultiPolygon([union_segments])
for poly in union_segments.geoms:
exteriors.append(poly.exterior)
for inter in poly.interiors:
interiors.append(inter)
results = []
for ext in exteriors:
if ext.is_ccw:
ext.coords = list(ext.coords)[::-1]
lines = ext.intersection(local_patch)
if isinstance(lines, MultiLineString):
lines = ops.linemerge(lines)
results.append(lines)
for inter in interiors:
if not inter.is_ccw:
inter.coords = list(inter.coords)[::-1]
lines = inter.intersection(local_patch)
if isinstance(lines, MultiLineString):
lines = ops.linemerge(lines)
results.append(lines)
return self._one_type_line_geom_to_vectors(results)
def ped_poly_geoms_to_vectors_1(self, polygon_geom):
ped = polygon_geom[0][1]
union_segments = ops.unary_union(ped)
max_x = self.patch_size[1] / 2
max_y = self.patch_size[0] / 2
# local_patch = box(-max_x + 0.2, -max_y + 0.2, max_x - 0.2, max_y - 0.2)
local_patch = box(-max_x - 0.2, -max_y - 0.2, max_x + 0.2, max_y + 0.2)
exteriors = []
interiors = []
if union_segments.geom_type != 'MultiPolygon':
union_segments = MultiPolygon([union_segments])
for poly in union_segments.geoms:
exteriors.append(poly.exterior)
for inter in poly.interiors:
interiors.append(inter)
results = []
for ext in exteriors:
if ext.is_ccw:
ext.coords = list(ext.coords)[::-1]
lines = ext.intersection(local_patch)
if isinstance(lines, MultiLineString):
lines = ops.linemerge(lines)
results.append(lines)
for inter in interiors:
if not inter.is_ccw:
inter.coords = list(inter.coords)[::-1]
lines = inter.intersection(local_patch)
if isinstance(lines, MultiLineString):
lines = ops.linemerge(lines)
results.append(lines)
return self._one_type_line_geom_to_vectors(results)
def ped_poly_geoms_to_instances(self, ped_geom):
# import pdb;pdb.set_trace()
ped = ped_geom[0][1]
union_segments = ops.unary_union(ped)
max_x = self.patch_size[1] / 2
max_y = self.patch_size[0] / 2
# local_patch = box(-max_x + 0.2, -max_y + 0.2, max_x - 0.2, max_y - 0.2)
local_patch = box(-max_x - 0.2, -max_y - 0.2, max_x + 0.2, max_y + 0.2)
exteriors = []
interiors = []
if union_segments.geom_type != 'MultiPolygon':
union_segments = MultiPolygon([union_segments])
for poly in union_segments.geoms:
exteriors.append(poly.exterior)
for inter in poly.interiors:
interiors.append(inter)
results = []
for ext in exteriors:
if ext.is_ccw:
ext.coords = list(ext.coords)[::-1]
lines = ext.intersection(local_patch)
if isinstance(lines, MultiLineString):
lines = ops.linemerge(lines)
results.append(lines)
for inter in interiors:
if not inter.is_ccw:
inter.coords = list(inter.coords)[::-1]
lines = inter.intersection(local_patch)
if isinstance(lines, MultiLineString):
lines = ops.linemerge(lines)
results.append(lines)
return self._one_type_line_geom_to_instances(results)
def poly_geoms_to_instances(self, polygon_geom):
roads = polygon_geom[0][1]
lanes = polygon_geom[1][1]
union_roads = ops.unary_union(roads)
union_lanes = ops.unary_union(lanes)
union_segments = ops.unary_union([union_roads, union_lanes])
max_x = self.patch_size[1] / 2
max_y = self.patch_size[0] / 2
local_patch = box(-max_x + 0.2, -max_y + 0.2, max_x - 0.2, max_y - 0.2)
exteriors = []
interiors = []
if union_segments.geom_type != 'MultiPolygon':
union_segments = MultiPolygon([union_segments])
for poly in union_segments.geoms:
exteriors.append(poly.exterior)
for inter in poly.interiors:
interiors.append(inter)
results = []
for ext in exteriors:
if ext.is_ccw:
ext.coords = list(ext.coords)[::-1]
lines = ext.intersection(local_patch)
if isinstance(lines, MultiLineString):
lines = ops.linemerge(lines)
results.append(lines)
for inter in interiors:
if not inter.is_ccw:
inter.coords = list(inter.coords)[::-1]
lines = inter.intersection(local_patch)
if isinstance(lines, MultiLineString):
lines = ops.linemerge(lines)
results.append(lines)
return self._one_type_line_geom_to_instances(results)
def line_geoms_to_vectors(self, line_geom):
line_vectors_dict = dict()
for line_type, a_type_of_lines in line_geom:
one_type_vectors = self._one_type_line_geom_to_vectors(a_type_of_lines)
line_vectors_dict[line_type] = one_type_vectors
return line_vectors_dict
def line_geoms_to_instances(self, line_geom):
line_instances_dict = dict()
for line_type, a_type_of_lines in line_geom:
one_type_instances = self._one_type_line_geom_to_instances(a_type_of_lines)
line_instances_dict[line_type] = one_type_instances
return line_instances_dict
def ped_geoms_to_vectors(self, ped_geom):
ped_geom = ped_geom[0][1]
union_ped = ops.unary_union(ped_geom)
if union_ped.geom_type != 'MultiPolygon':
union_ped = MultiPolygon([union_ped])
max_x = self.patch_size[1] / 2
max_y = self.patch_size[0] / 2
# local_patch = box(-max_x + 0.2, -max_y + 0.2, max_x - 0.2, max_y - 0.2)
local_patch = box(-max_x - 0.2, -max_y - 0.2, max_x + 0.2, max_y + 0.2)
results = []
for ped_poly in union_ped:
# rect = ped_poly.minimum_rotated_rectangle
ext = ped_poly.exterior
if not ext.is_ccw:
ext.coords = list(ext.coords)[::-1]
lines = ext.intersection(local_patch)
results.append(lines)
return self._one_type_line_geom_to_vectors(results)
def get_contour_line(self,patch_box,patch_angle,layer_name,location):
# if layer_name not in self.map_explorer[location].map_api.non_geometric_polygon_layers:
# raise ValueError('{} is not a polygonal layer'.format(layer_name))
patch_x = patch_box[0]
patch_y = patch_box[1]
patch = self.map_explorer[location].get_patch_coord(patch_box, patch_angle)
records = getattr(self.map_explorer[location].map_api, layer_name)
polygon_list = []
if layer_name == 'drivable_area':
for record in records:
polygons = [self.map_explorer[location].map_api.extract_polygon(polygon_token) for polygon_token in record['polygon_tokens']]
for polygon in polygons:
new_polygon = polygon.intersection(patch)
if not new_polygon.is_empty:
new_polygon = affinity.rotate(new_polygon, -patch_angle,
origin=(patch_x, patch_y), use_radians=False)
new_polygon = affinity.affine_transform(new_polygon,
[1.0, 0.0, 0.0, 1.0, -patch_x, -patch_y])
if new_polygon.geom_type == 'Polygon':
new_polygon = MultiPolygon([new_polygon])
polygon_list.append(new_polygon)
else:
for record in records:
polygon = self.map_explorer[location].map_api.extract_polygon(record['polygon_token'])
if polygon.is_valid:
new_polygon = polygon.intersection(patch)
if not new_polygon.is_empty:
new_polygon = affinity.rotate(new_polygon, -patch_angle,
origin=(patch_x, patch_y), use_radians=False)
new_polygon = affinity.affine_transform(new_polygon,
[1.0, 0.0, 0.0, 1.0, -patch_x, -patch_y])
if new_polygon.geom_type == 'Polygon':
new_polygon = MultiPolygon([new_polygon])
polygon_list.append(new_polygon)
return polygon_list
def get_divider_line(self,patch_box,patch_angle,layer_name,location):
# if layer_name not in self.map_explorer[location].map_api.non_geometric_line_layers:
# raise ValueError("{} is not a line layer".format(layer_name))
if layer_name == 'traffic_light':
return None
patch_x = patch_box[0]
patch_y = patch_box[1]
patch = self.map_explorer[location].get_patch_coord(patch_box, patch_angle)
line_list = []
records = getattr(self.map_explorer[location].map_api, layer_name)
for record in records:
line = self.map_explorer[location].map_api.extract_line(record['line_token'])
if line.is_empty: # Skip lines without nodes.
continue
new_line = line.intersection(patch)
if not new_line.is_empty:
new_line = affinity.rotate(new_line, -patch_angle, origin=(patch_x, patch_y), use_radians=False)
new_line = affinity.affine_transform(new_line,
[1.0, 0.0, 0.0, 1.0, -patch_x, -patch_y])
line_list.append(new_line)
return line_list
def get_ped_crossing_line(self, patch_box, patch_angle, location):
patch_x = patch_box[0]
patch_y = patch_box[1]
patch = self.map_explorer[location].get_patch_coord(patch_box, patch_angle)
polygon_list = []
records = getattr(self.map_explorer[location].map_api, 'ped_crossing')
# records = getattr(self.nusc_maps[location], 'ped_crossing')
for record in records:
polygon = self.map_explorer[location].map_api.extract_polygon(record['polygon_token'])
if polygon.is_valid:
new_polygon = polygon.intersection(patch)
if not new_polygon.is_empty:
new_polygon = affinity.rotate(new_polygon, -patch_angle,
origin=(patch_x, patch_y), use_radians=False)
new_polygon = affinity.affine_transform(new_polygon,
[1.0, 0.0, 0.0, 1.0, -patch_x, -patch_y])
if new_polygon.geom_type == 'Polygon':
new_polygon = MultiPolygon([new_polygon])
polygon_list.append(new_polygon)
return polygon_list
def sample_pts_from_line(self, line):
if self.fixed_num < 0:
distances = np.arange(0, line.length, self.sample_dist)
sampled_points = np.array([list(line.interpolate(distance).coords) for distance in distances]).reshape(-1, 2)
else:
# fixed number of points, so distance is line.length / self.fixed_num
distances = np.linspace(0, line.length, self.fixed_num)
sampled_points = np.array([list(line.interpolate(distance).coords) for distance in distances]).reshape(-1, 2)
# tmpdistances = np.linspace(0, line.length, 2)
# tmpsampled_points = np.array([list(line.interpolate(tmpdistance).coords) for tmpdistance in tmpdistances]).reshape(-1, 2)
# import pdb;pdb.set_trace()
# if self.normalize:
# sampled_points = sampled_points / np.array([self.patch_size[1], self.patch_size[0]])
num_valid = len(sampled_points)
if not self.padding or self.fixed_num > 0:
# fixed num sample can return now!
return sampled_points, num_valid
# fixed distance sampling need padding!
num_valid = len(sampled_points)
if self.fixed_num < 0:
if num_valid < self.num_samples:
padding = np.zeros((self.num_samples - len(sampled_points), 2))
sampled_points = np.concatenate([sampled_points, padding], axis=0)
else:
sampled_points = sampled_points[:self.num_samples, :]
num_valid = self.num_samples
# if self.normalize:
# sampled_points = sampled_points / np.array([self.patch_size[1], self.patch_size[0]])
# num_valid = len(sampled_points)
return sampled_points, num_valid
def output_to_vecs(detection):
box3d = detection['map_boxes_3d'].numpy()
scores = detection['map_scores_3d'].numpy()