-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterfuser_agent.py
741 lines (666 loc) · 27.8 KB
/
interfuser_agent.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
import os
import json
import datetime
import pathlib
import time
import imp
import cv2
import carla
from collections import deque
import torch
import carla
import numpy as np
import torchvision.transforms.functional
from PIL import Image
from easydict import EasyDict
from torchvision import transforms
from leaderboard.autoagents import autonomous_agent
from timm.models import create_model
from team_code.utils import lidar_to_histogram_features, transform_2d_points
from team_code.planner import RoutePlanner
from team_code.interfuser_controller import InterfuserController
from team_code.render import render, render_self_car, render_waypoints
from team_code.tracker import Tracker
import math
import yaml
from team_code.cyclegan import CycleGAN
from wandb.kubeflow import storage
try:
import pygame
except ImportError:
raise RuntimeError("cannot import pygame, make sure pygame package is installed")
SAVE_PATH = os.environ.get("SAVE_PATH", 'eval')
IMAGENET_DEFAULT_MEAN = (0.485, 0.456, 0.406)
IMAGENET_DEFAULT_STD = (0.229, 0.224, 0.225)
class DisplayInterface(object):
def __init__(self):
self._width = 2000
self._height = 600
self._surface = None
pygame.init()
pygame.font.init()
self._clock = pygame.time.Clock()
self._display = pygame.display.set_mode(
(self._width, self._height), pygame.HWSURFACE | pygame.DOUBLEBUF
)
# ckpt = torch.load("/media/banana/data/models/online-testing/temp/cyclegan_version=0_epoch=1_step=44598_night.ckpt", map_location=torch.device('cuda'))
# self.translate_model = CycleGAN(num_residual_blocks=4, gen_channels=128)
# print(ckpt['state_dict'].keys())
# self.translate_model.generator_AB = ckpt['state_dict']['generator_AB']
# print(ckpt.keys())
pygame.display.set_caption("Human Agent")
def run_interface(self, input_data):
rgb = input_data['rgb']
rgb_left = input_data['rgb_left']
rgb_right = input_data['rgb_right']
rgb_focus = input_data['rgb_focus']
map = input_data['map']
surface = np.zeros((600, 2000, 3), np.uint8)
surface[:, :800] = rgb
surface[:400, 800:1200] = map
surface[400:600, 800:1000] = input_data['map_t1']
surface[400:600, 1000:1200] = input_data['map_t2']
surface[:150, :200] = input_data['rgb_left']
surface[:150, 600:800] = input_data['rgb_right']
surface[:150, 325:475] = input_data['rgb_focus']
surface = cv2.putText(surface, input_data['control'], (20, 580), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 1)
surface = cv2.putText(surface, input_data['meta_infos'][0], (20, 560), cv2.FONT_HERSHEY_SIMPLEX, 0.5,
(0, 0, 255), 1)
surface = cv2.putText(surface, input_data['meta_infos'][1], (20, 540), cv2.FONT_HERSHEY_SIMPLEX, 0.5,
(0, 0, 255), 1)
surface = cv2.putText(surface, input_data['time'], (20, 520), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 1)
surface = cv2.putText(surface, 'Left View', (40, 135), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 0), 2)
surface = cv2.putText(surface, 'Focus View', (335, 135), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 0), 2)
surface = cv2.putText(surface, 'Right View', (640, 135), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 0), 2)
surface = cv2.putText(surface, 'Future Prediction', (940, 420), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 2)
surface = cv2.putText(surface, 't', (1160, 385), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 0, 0), 2)
surface = cv2.putText(surface, '0', (1170, 385), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 2)
surface = cv2.putText(surface, 't', (960, 585), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 0, 0), 2)
surface = cv2.putText(surface, '1', (970, 585), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 2)
surface = cv2.putText(surface, 't', (1160, 585), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 0, 0), 2)
surface = cv2.putText(surface, '2', (1170, 585), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 2)
x = 1200
surface[:, x:x+800] = input_data['rgb_orig']
surface[:150, x:x+200] = input_data['rgb_left_orig']
surface[:150, x+600:x+800] = input_data['rgb_right_orig']
surface[:150, x+325:x+475] = input_data['rgb_focus_orig']
surface = cv2.putText(surface, 'Left View', (x+40, 135), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 0), 2)
surface = cv2.putText(surface, 'Focus View', (x+335, 135), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 0), 2)
surface = cv2.putText(surface, 'Right View', (x+640, 135), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 0), 2)
surface[:150, 198:202] = 0
surface[:150, 323:327] = 0
surface[:150, 473:477] = 0
surface[:150, 598:602] = 0
surface[148:152, :200] = 0
surface[148:152, 325:475] = 0
surface[148:152, 600:800] = 0
surface[430:600, 998:1000] = 255
surface[0:600, 798:800] = 255
surface[0:600, 1198:1200] = 255
surface[0:2, 800:1200] = 255
surface[598:600, 800:1200] = 255
surface[398:400, 800:1200] = 255
# display image
self._surface = pygame.surfarray.make_surface(surface.swapaxes(0, 1))
if self._surface is not None:
self._display.blit(self._surface, (0, 0))
pygame.display.flip()
pygame.event.get()
return surface
def _quit(self):
pygame.quit()
def get_entry_point():
return "InterfuserAgent"
class Resize2FixedSize:
def __init__(self, size):
self.size = size
def __call__(self, pil_img):
pil_img = pil_img.resize(self.size)
return pil_img
def create_carla_rgb_transform(
input_size, need_scale=True, mean=IMAGENET_DEFAULT_MEAN, std=IMAGENET_DEFAULT_STD
):
if isinstance(input_size, (tuple, list)):
img_size = input_size[-2:]
else:
img_size = input_size
tfl = []
if isinstance(input_size, (tuple, list)):
input_size_num = input_size[-1]
else:
input_size_num = input_size
if need_scale:
if input_size_num == 112:
tfl.append(Resize2FixedSize((170, 128)))
elif input_size_num == 128:
tfl.append(Resize2FixedSize((195, 146)))
elif input_size_num == 224:
tfl.append(Resize2FixedSize((341, 256)))
elif input_size_num == 256:
tfl.append(Resize2FixedSize((288, 288)))
else:
raise ValueError("Can't find proper crop size")
tfl.append(transforms.CenterCrop(img_size))
tfl.append(transforms.ToTensor())
tfl.append(transforms.Normalize(mean=torch.tensor(mean), std=torch.tensor(std)))
return transforms.Compose(tfl)
class InterfuserAgent(autonomous_agent.AutonomousAgent):
def setup(self, path_to_conf_file):
self._hic = DisplayInterface()
self.lidar_processed = list()
self.track = autonomous_agent.Track.SENSORS
self.step = -1
self.wall_start = time.time()
self.initialized = False
self.rgb_front_transform = create_carla_rgb_transform(224)
self.rgb_left_transform = create_carla_rgb_transform(128)
self.rgb_right_transform = create_carla_rgb_transform(128)
self.rgb_center_transform = create_carla_rgb_transform(128, need_scale=False)
self.tracker = Tracker()
self.input_buffer = {
"rgb": deque(),
"rgb_left": deque(),
"rgb_right": deque(),
"rgb_rear": deque(),
"lidar": deque(),
"gps": deque(),
"thetas": deque(),
}
self.config = imp.load_source("MainModel", path_to_conf_file).GlobalConfig()
self.skip_frames = self.config.skip_frames
self.controller = InterfuserController(self.config)
if isinstance(self.config.model, list):
self.ensemble = True
else:
self.ensemble = False
if self.ensemble:
for i in range(len(self.config.model)):
self.nets = []
net = create_model(self.config.model[i])
path_to_model_file = self.config.model_path[i]
print('load model: %s' % path_to_model_file)
net.load_state_dict(torch.load(path_to_model_file)["state_dict"])
net.cuda()
net.eval()
self.nets.append(net)
else:
self.net = create_model(self.config.model)
path_to_model_file = self.config.model_path
print('load model: %s' % path_to_model_file)
self.net.load_state_dict(torch.load(path_to_model_file)["state_dict"])
self.net.cuda()
self.net.eval()
self.softmax = torch.nn.Softmax(dim=1)
self.traffic_meta_moving_avg = np.zeros((400, 7))
self.momentum = self.config.momentum
self.prev_lidar = None
self.prev_control = None
self.prev_surround_map = None
self.translate_model = CycleGAN.load_from_checkpoint(
"/media/banana/data/models/online-testing/carla/A road during night/cyclegan_version=0_epoch=0_step=20535_.ckpt",
# "/media/banana/data/models/online-testing/carla/A road in a forest/cyclegan_version=0_epoch=0_step=23094_.ckpt",
# "/media/banana/data/models/online-testing/carla/A road during dust storm weather/cyclegan_version=0_epoch=0_step=17853_.ckpt",
# "/media/banana/data/models/online-testing/carla/A road during summer/cyclegan_version=0_epoch=0_step=23160_.ckpt",
# "/media/banana/data/models/online-testing/carla/A road during autumn/cyclegan_version=0_epoch=1_step=40200_.ckpt",
# "/media/banana/data/models/online-testing/carla/A road during desert/cyclegan_version=0_epoch=1_step=29388_.ckpt",
# "/media/banana/data/models/online-testing/carla/A road during winter/cyclegan_version=0_epoch=1_step=44478_.ckpt",
# "/media/banana/data/models/online-testing/carla/A road during summer/cyclegan_version=0_epoch=1_step=43653_.ckpt",
strict=False).to("cuda")
self.save_path = None
if SAVE_PATH is not None:
now = datetime.datetime.now()
string = pathlib.Path(os.environ["ROUTES"]).stem + "_"
string += "_".join(
map(
lambda x: "%02d" % x,
(now.month, now.day, now.hour, now.minute, now.second),
)
)
print(string)
self.save_path = pathlib.Path(SAVE_PATH) / string
self.save_path.mkdir(parents=True, exist_ok=False)
(self.save_path / "meta").mkdir(parents=True, exist_ok=False)
def _init(self):
self._route_planner = RoutePlanner(4.0, 50.0)
self._route_planner.set_route(self._global_plan, True)
self.initialized = True
def _get_position(self, tick_data):
gps = tick_data["gps"]
gps = (gps - self._route_planner.mean) * self._route_planner.scale
return gps
def sensors(self):
return [
{
"type": "sensor.camera.rgb",
"x": 1.3,
"y": 0.0,
"z": 2.3,
"roll": 0.0,
"pitch": 0.0,
"yaw": 0.0,
"width": 800,
"height": 600,
"fov": 100,
"id": "rgb",
},
{
"type": "sensor.camera.rgb",
"x": 1.3,
"y": 0.0,
"z": 2.3,
"roll": 0.0,
"pitch": 0.0,
"yaw": -60.0,
"width": 400,
"height": 300,
"fov": 100,
"id": "rgb_left",
},
{
"type": "sensor.camera.rgb",
"x": 1.3,
"y": 0.0,
"z": 2.3,
"roll": 0.0,
"pitch": 0.0,
"yaw": 60.0,
"width": 400,
"height": 300,
"fov": 100,
"id": "rgb_right",
},
{
"type": "sensor.lidar.ray_cast",
"x": 1.3,
"y": 0.0,
"z": 2.5,
"roll": 0.0,
"pitch": 0.0,
"yaw": -90.0,
"id": "lidar",
},
{
"type": "sensor.other.imu",
"x": 0.0,
"y": 0.0,
"z": 0.0,
"roll": 0.0,
"pitch": 0.0,
"yaw": 0.0,
"sensor_tick": 0.05,
"id": "imu",
},
{
"type": "sensor.other.gnss",
"x": 0.0,
"y": 0.0,
"z": 0.0,
"roll": 0.0,
"pitch": 0.0,
"yaw": 0.0,
"sensor_tick": 0.01,
"id": "gps",
},
{
"type": "sensor.camera.semantic_segmentation",
"x": 1.3,
"y": 0.0,
"z": 2.3,
"roll": 0.0,
"pitch": 0.0,
"yaw": 0.0,
"width": 800,
"height": 600,
"fov": 100,
"id": "seg",
},
{
"type": "sensor.camera.semantic_segmentation",
"x": 1.3,
"y": 0.0,
"z": 2.3,
"roll": 0.0,
"pitch": 0.0,
"yaw": -60.0,
"width": 400,
"height": 300,
"fov": 100,
"id": "seg_left",
},
{
"type": "sensor.camera.semantic_segmentation",
"x": 1.3,
"y": 0.0,
"z": 2.3,
"roll": 0.0,
"pitch": 0.0,
"yaw": 60.0,
"width": 400,
"height": 300,
"fov": 100,
"id": "seg_right",
},
{"type": "sensor.speedometer", "reading_frequency": 20, "id": "speed"},
]
def tick(self, input_data):
rgb = cv2.cvtColor(input_data["rgb"][1][:, :, :3], cv2.COLOR_BGR2RGB)
rgb_left = cv2.cvtColor(input_data["rgb_left"][1][:, :, :3], cv2.COLOR_BGR2RGB)
rgb_right = cv2.cvtColor(input_data["rgb_right"][1][:, :, :3], cv2.COLOR_BGR2RGB)
seg = cv2.cvtColor(input_data["seg"][1][:, :, :3], cv2.COLOR_BGR2RGB)
seg_left = cv2.cvtColor(input_data["seg_left"][1][:, :, :3], cv2.COLOR_BGR2RGB)
seg_right = cv2.cvtColor(input_data["seg_right"][1][:, :, :3], cv2.COLOR_BGR2RGB)
gps = input_data["gps"][1][:2]
speed = input_data["speed"][1]["speed"]
compass = input_data["imu"][1][-1]
if (
math.isnan(compass) == True
): # It can happen that the compass sends nan for a few frames
compass = 0.0
result = {
"rgb": rgb,
"rgb_left": rgb_left,
"rgb_right": rgb_right,
"seg": seg,
"seg_left": seg_left,
"seg_right": seg_right,
"gps": gps,
"speed": speed,
"compass": compass,
}
pos = self._get_position(result)
lidar_data = input_data['lidar'][1]
result['raw_lidar'] = lidar_data
lidar_unprocessed = lidar_data[:, :3]
lidar_unprocessed[:, 1] *= -1
full_lidar = transform_2d_points(
lidar_unprocessed,
np.pi / 2 - compass,
-pos[0],
-pos[1],
np.pi / 2 - compass,
-pos[0],
-pos[1],
)
lidar_processed = lidar_to_histogram_features(full_lidar, crop=224)
if self.step % 2 == 0 or self.step < 4:
self.prev_lidar = lidar_processed
result["lidar"] = self.prev_lidar
result["gps"] = pos
next_wp, next_cmd = self._route_planner.run_step(pos)
result["next_command"] = next_cmd.value
result['measurements'] = [pos[0], pos[1], compass, speed]
theta = compass + np.pi / 2
R = np.array([[np.cos(theta), -np.sin(theta)], [np.sin(theta), np.cos(theta)]])
local_command_point = np.array([next_wp[0] - pos[0], next_wp[1] - pos[1]])
local_command_point = R.T.dot(local_command_point)
result["target_point"] = local_command_point
return result
@torch.no_grad()
def run_step(self, input_data, timestamp):
if not self.initialized:
self._init()
self.step += 1
if self.step % self.skip_frames != 0 and self.step > 4:
return self.prev_control
tick_data = self.tick(input_data)
velocity = tick_data["speed"]
command = tick_data["next_command"]
with torch.no_grad():
tick_data["rgb_orig"] = tick_data["rgb"]
tick_data["rgb_left_orig"] = tick_data["rgb_left"]
tick_data["rgb_right_orig"] = tick_data["rgb_right"]
tick_data["rgb"] = np.array(torchvision.transforms.functional.to_pil_image(
self.translate_model(torchvision.transforms.functional.to_tensor(tick_data["rgb"]).to("cuda"))
))
tick_data["rgb_left"] = np.array(torchvision.transforms.functional.to_pil_image(
self.translate_model(torchvision.transforms.functional.to_tensor(tick_data["rgb_left"]).to("cuda"))
))
tick_data["rgb_right"] = np.array(torchvision.transforms.functional.to_pil_image(
self.translate_model(torchvision.transforms.functional.to_tensor(tick_data["rgb_right"]).to("cuda"))
))
rgb = (
self.rgb_front_transform(Image.fromarray(tick_data["rgb"]))
.unsqueeze(0)
.cuda()
.float()
)
rgb_left = (
self.rgb_left_transform(Image.fromarray(tick_data["rgb_left"]))
.unsqueeze(0)
.cuda()
.float()
)
rgb_right = (
self.rgb_right_transform(Image.fromarray(tick_data["rgb_right"]))
.unsqueeze(0)
)
rgb_center = (
self.rgb_center_transform(Image.fromarray(tick_data["rgb"]))
.unsqueeze(0)
.cuda()
.float()
)
rgb_orig = (
self.rgb_front_transform(Image.fromarray(tick_data["rgb_orig"]))
.unsqueeze(0)
.cuda()
.float()
)
rgb_left_orig = (
self.rgb_left_transform(Image.fromarray(tick_data["rgb_left_orig"]))
.unsqueeze(0)
.cuda()
.float()
)
rgb_right_orig = (
self.rgb_right_transform(Image.fromarray(tick_data["rgb_right_orig"]))
.unsqueeze(0)
)
rgb_center_orig = (
self.rgb_center_transform(Image.fromarray(tick_data["rgb"]))
.unsqueeze(0)
.cuda()
.float()
)
seg = (
self.rgb_front_transform(Image.fromarray(tick_data["seg"]))
.unsqueeze(0)
)
seg_left = (
self.rgb_left_transform(Image.fromarray(tick_data["seg_left"]))
.unsqueeze(0)
)
seg_right = (
self.rgb_right_transform(Image.fromarray(tick_data["seg_right"]))
.unsqueeze(0)
.cuda()
.float()
)
cmd_one_hot = [0, 0, 0, 0, 0, 0]
cmd = command - 1
cmd_one_hot[cmd] = 1
cmd_one_hot.append(velocity)
mes = np.array(cmd_one_hot)
mes = torch.from_numpy(mes).float().unsqueeze(0).cuda()
input_data = {}
input_data["rgb"] = rgb.cuda()
input_data["rgb_left"] = rgb_left.cuda()
input_data["rgb_right"] = rgb_right.cuda()
input_data["rgb_center"] = rgb_center.cuda()
input_data["rgb_orig"] = rgb
input_data["rgb_left_orig"] = rgb_left
input_data["rgb_right_orig"] = rgb_right
input_data["rgb_center_orig"] = rgb_center
input_data["seg"] = seg
input_data["seg_left"] = seg_left
input_data["seg_right"] = seg_right
input_data["measurements"] = mes
input_data["target_point"] = (
torch.from_numpy(tick_data["target_point"]).float().cuda().view(1, -1)
)
input_data["lidar"] = (
torch.from_numpy(tick_data["lidar"]).float().cuda().unsqueeze(0)
)
if self.ensemble:
outputs = []
with torch.no_grad():
for net in self.nets:
output = net(input_data)
outputs.append(output)
traffic_meta = torch.mean(torch.stack([x[0] for x in outputs]), 0)
pred_waypoints = torch.mean(torch.stack([x[1] for x in outputs]), 0)
is_junction = torch.mean(torch.stack([x[2] for x in outputs]), 0)
traffic_light_state = torch.mean(torch.stack([x[3] for x in outputs]), 0)
stop_sign = torch.mean(torch.stack([x[4] for x in outputs]), 0)
bev_feature = torch.mean(torch.stack([x[5] for x in outputs]), 0)
else:
with torch.no_grad():
(
traffic_meta,
pred_waypoints,
is_junction,
traffic_light_state,
stop_sign,
bev_feature,
) = self.net(input_data)
traffic_meta = traffic_meta.detach().cpu().numpy()[0]
bev_feature = bev_feature.detach().cpu().numpy()[0]
pred_waypoints = pred_waypoints.detach().cpu().numpy()[0]
is_junction = self.softmax(is_junction).detach().cpu().numpy().reshape(-1)[0]
traffic_light_state = (
self.softmax(traffic_light_state).detach().cpu().numpy().reshape(-1)[0]
)
stop_sign = self.softmax(stop_sign).detach().cpu().numpy().reshape(-1)[0]
if self.step % 2 == 0 or self.step < 4:
traffic_meta = self.tracker.update_and_predict(traffic_meta.reshape(20, 20, -1), tick_data['gps'],
tick_data['compass'], self.step // 2)
traffic_meta = traffic_meta.reshape(400, -1)
self.traffic_meta_moving_avg = (
self.momentum * self.traffic_meta_moving_avg
+ (1 - self.momentum) * traffic_meta
)
traffic_meta = self.traffic_meta_moving_avg
tick_data["raw"] = traffic_meta
tick_data["bev_feature"] = bev_feature
steer, throttle, brake, meta_infos = self.controller.run_step(
velocity,
pred_waypoints,
is_junction,
traffic_light_state,
stop_sign,
self.traffic_meta_moving_avg,
)
if brake < 0.05:
brake = 0.0
if brake > 0.1:
throttle = 0.0
control = carla.VehicleControl()
control.steer = float(steer)
control.throttle = float(throttle)
control.brake = float(brake)
surround_map, box_info = render(traffic_meta.reshape(20, 20, 7), pixels_per_meter=20)
surround_map = surround_map[:400, 160:560]
surround_map = np.stack([surround_map, surround_map, surround_map], 2)
self_car_map = render_self_car(
loc=np.array([0, 0]),
ori=np.array([0, -1]),
box=np.array([2.45, 1.0]),
color=[1, 1, 0], pixels_per_meter=20
)[:400, 160:560]
pred_waypoints = pred_waypoints.reshape(-1, 2)
safe_index = 10
for i in range(10):
if pred_waypoints[i, 0] ** 2 + pred_waypoints[i, 1] ** 2 > (meta_infos[3] + 0.5) ** 2:
safe_index = i
break
wp1 = render_waypoints(pred_waypoints[:safe_index], pixels_per_meter=20, color=(0, 255, 0))[:400, 160:560]
wp2 = render_waypoints(pred_waypoints[safe_index:], pixels_per_meter=20, color=(255, 0, 0))[:400, 160:560]
wp = wp1 + wp2
surround_map = np.clip(
(
surround_map.astype(np.float32)
+ self_car_map.astype(np.float32)
+ wp.astype(np.float32)
),
0,
255,
).astype(np.uint8)
map_t1, box_info = render(traffic_meta.reshape(20, 20, 7), pixels_per_meter=20, t=1)
map_t1 = map_t1[:400, 160:560]
map_t1 = np.stack([map_t1, map_t1, map_t1], 2)
map_t1 = np.clip(map_t1.astype(np.float32) + self_car_map.astype(np.float32), 0, 255).astype(np.uint8)
map_t1 = cv2.resize(map_t1, (200, 200))
map_t2, box_info = render(traffic_meta.reshape(20, 20, 7), pixels_per_meter=20, t=2)
map_t2 = map_t2[:400, 160:560]
map_t2 = np.stack([map_t2, map_t2, map_t2], 2)
map_t2 = np.clip(map_t2.astype(np.float32) + self_car_map.astype(np.float32), 0, 255).astype(np.uint8)
map_t2 = cv2.resize(map_t2, (200, 200))
if self.step % 2 != 0 and self.step > 4:
control = self.prev_control
else:
self.prev_control = control
self.prev_surround_map = surround_map
tick_data["map"] = self.prev_surround_map
tick_data["map_t1"] = map_t1
tick_data["map_t2"] = map_t2
tick_data["rgb_raw"] = tick_data["rgb"]
tick_data["rgb_left_raw"] = tick_data["rgb_left"]
tick_data["rgb_right_raw"] = tick_data["rgb_right"]
tick_data["rgb"] = cv2.resize(tick_data["rgb"], (800, 600))
tick_data["rgb_left"] = cv2.resize(tick_data["rgb_left"], (200, 150))
tick_data["rgb_right"] = cv2.resize(tick_data["rgb_right"], (200, 150))
tick_data["rgb_focus"] = cv2.resize(tick_data["rgb_raw"][244:356, 344:456], (150, 150))
tick_data["rgb_focus_orig"] = cv2.resize(tick_data["rgb_orig"][244:356, 344:456], (150, 150))
tick_data["rgb_orig"] = cv2.resize(tick_data["rgb_orig"], (800, 600))
tick_data["rgb_left_orig"] = cv2.resize(tick_data["rgb_left_orig"], (200, 150))
tick_data["rgb_right_orig"] = cv2.resize(tick_data["rgb_right_orig"], (200, 150))
tick_data["control"] = "throttle: %.2f, steer: %.2f, brake: %.2f" % (
control.throttle,
control.steer,
control.brake,
)
tick_data["meta_infos"] = meta_infos
tick_data["box_info"] = "car: %d, bike: %d, pedestrian: %d" % (
box_info["car"],
box_info["bike"],
box_info["pedestrian"],
)
tick_data["mes"] = "speed: %.2f" % velocity
tick_data["time"] = "time: %.3f" % timestamp
surface = self._hic.run_interface(tick_data)
tick_data["surface"] = surface
if SAVE_PATH is not None:
self.save(tick_data)
return control
def save(self, tick_data):
frame = self.step // self.skip_frames
Image.fromarray(tick_data["surface"]).save(
self.save_path / "meta" / ("surface_%08d.jpg" % frame)
)
Image.fromarray(tick_data["rgb_raw"]).save(
self.save_path / "meta" / ("rgb_%08d.jpg" % frame)
)
Image.fromarray(tick_data["rgb_left_raw"]).save(
self.save_path / "meta" / ("rgb_left_%08d.jpg" % frame)
)
Image.fromarray(tick_data["rgb_right_raw"]).save(
self.save_path / "meta" / ("rgb_right_%08d.jpg" % frame)
)
Image.fromarray(tick_data["seg"]).save(
self.save_path / "meta" / ("seg_%08d.png" % frame)
)
Image.fromarray(tick_data["seg_left"]).save(
self.save_path / "meta" / ("seg_left_%08d.png" % frame)
)
Image.fromarray(tick_data["seg_right"]).save(
self.save_path / "meta" / ("seg_right_%08d.png" % frame)
)
# Image.fromarray(tick_data["rgb_focus"]).save(
# self.save_path / "meta" / ("rgb_focus_%08d.jpg" % frame)
# )
return
def destroy(self):
if self.ensemble:
del self.nets
else:
del self.net