-
Notifications
You must be signed in to change notification settings - Fork 1
/
evaluator.py
1131 lines (979 loc) · 47.1 KB
/
evaluator.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
#!/usr/bin/env python
# Copyright (c) 2019 Computer Vision Center (CVC) at the Universitat Autonoma de
# Barcelona (UAB).
#
# This work is licensed under the terms of the MIT license.
# For a copy, see <https://opensource.org/licenses/MIT>.
import argparse
import json
import random
import shutil
from collections import defaultdict
from functools import partial
from math import sqrt
from operator import attrgetter, itemgetter
from pathlib import Path
from time import sleep
from typing import Tuple, List, Dict, Any
import cv2
from custom_carla.agents.navigation.local_planner import RoadOption
from data.types import DriveDataFrame, LengthComputer
from environment import set_world_asynchronous, set_world_synchronous, FrameCounter, should_quit, GameEnvironment
from util.common import add_carla_module, get_logger
from util.directory import EvaluationDirectory, mkdir_if_not_exists
add_carla_module()
logger = get_logger(__name__)
import carla
import numpy as np
import pygame
import torch
from config import IMAGE_WIDTH, IMAGE_HEIGHT, EVAL_FRAMERATE_SCALE, DATASET_FRAMERATE
from data.dataset import load_index_from_word, generate_templated_sentence_dict, HighLevelDataset
from util.road_option import fetch_road_option_from_str, fetch_onehot_vector_dim, fetch_onehot_vector_from_index, \
fetch_num_sentence_commands, fetch_onehot_vector_from_sentence_command, fetch_onehot_index_from_high_level_str, \
fetch_high_level_command_from_index
from util.image import tensor_from_numpy_image, video_from_files
from model import init_hidden_states
from parameter import Parameter
from trainer import CheckpointBase
def canonicalize(src: str):
return str(src).replace('\\', '/').replace('//', '/')
def onehot_from_index(cmd: int, use_low_level_segment: bool) -> torch.Tensor:
onehot_dim = fetch_onehot_vector_dim(use_low_level_segment)
return fetch_onehot_vector_from_index(cmd, use_low_level_segment).view(1, onehot_dim)
def _tensor_from_numpy_image(image: np.ndarray) -> torch.Tensor:
c = image.shape[2]
return tensor_from_numpy_image(image, False).view(1, 1, c, IMAGE_HEIGHT, IMAGE_WIDTH)
class LowLevelEvaluator(CheckpointBase):
def __init__(self, param: Parameter, cmd: int):
CheckpointBase.__init__(self, param)
self.cmd = cmd
self.param = param
self.step_elapsed = 0
self.use_low_level_segment = param.use_low_level_segment
self.onehot_dim = fetch_onehot_vector_dim(param.use_low_level_segment)
self.onehot_func = partial(onehot_from_index, use_low_level_segment=param.use_low_level_segment)
self.encoder_hidden, self.decoder_hidden, self.images = None, None, []
self.initialize()
def initialize(self):
self.encoder_hidden, self.decoder_hidden = init_hidden_states(self.param)
self.images = []
def run_step(self, image: np.ndarray) -> Any:
batch = self._prepare_batch(image)
output = self._run_step(batch)
self.step_elapsed += 1
return output
@property
def onehot_vector(self):
return fetch_onehot_vector_from_index(self.cmd, self.use_low_level_segment).view(1, self.onehot_dim)
def _prepare_batch(self, image: np.ndarray, custom_action_index: int = -1):
# self.initialize()
self.images.append(_tensor_from_numpy_image(image))
self.images = self.images[-10:]
data_dict = {
'onehot': self.onehot_vector,
'action_index': [self.cmd if custom_action_index < 0 else custom_action_index],
'images': torch.cat(self.images, dim=1)
}
for key, value in data_dict.items():
if isinstance(value, torch.Tensor):
data_dict[key] = value.to(device=self.param.device_type)
return data_dict
def _run_step(self, data):
self.model.eval()
model_output = self.model.forward(data, self.encoder_hidden, self.decoder_hidden)
output = model_output['output'][0][-1]
if output.size(-1) == 2:
control = carla.VehicleControl()
control.throttle = output[0].item()
control.steer = output[1].item()
control.brake = 0.0
control.hand_brake = False
control.manual_gear_shift = False
return control
else:
return output.item()
class HighLevelEvaluator(CheckpointBase):
def __init__(self, param: Parameter, cmd: str):
CheckpointBase.__init__(self, param)
self.cmd = cmd
self.param = param
self.step_elapsed = 0
self.onehot_dim = fetch_num_sentence_commands()
self.onehot_func = fetch_onehot_vector_from_sentence_command
self.index_func = fetch_onehot_index_from_high_level_str
self.sentence = param.eval_keyword.lower()
self.index_from_word = load_index_from_word()
self.encoder_hidden, self.decoder_hidden, self.images = None, None, []
self.initialize()
def fetch_word_index(self, word: str):
if word in self.index_from_word:
return self.index_from_word[word]
else:
return 1 # assigned for 'unknown'
def initialize(self):
self.encoder_hidden, self.decoder_hidden = init_hidden_states(self.param)
self.images = []
def run_step(self, image: np.ndarray, sentence: str) -> torch.Tensor:
batch = self._prepare_batch(image, sentence)
action = self._run_step(batch)
return action
@property
def onehot_vector(self):
return self.onehot_func(self.cmd).view(1, self.onehot_dim)
def _prepare_batch(self, image: np.ndarray, sentence: str):
word_indices = [self.fetch_word_index(w) for w in sentence.lower().split(' ')]
length = torch.tensor([len(word_indices)], dtype=torch.long)
logger.info((length.shape, sentence))
word_indices = torch.tensor(word_indices, dtype=torch.long)
self.images.append(_tensor_from_numpy_image(image))
self.images = self.images[-10:]
data_dict = {
'sentence': sentence,
'word_indices': word_indices,
'length': length,
'images': torch.cat(self.images, dim=1)
}
for key, value in data_dict.items():
if isinstance(value, torch.Tensor):
data_dict[key] = value.to(device=self.param.device_type)
return data_dict
def _run_step(self, data):
self.model.eval()
model_output = self.model(data, self.encoder_hidden, self.decoder_hidden)
agent_action = model_output['output'][0]
self.step_elapsed += 1
return agent_action
class SingleEvaluator(CheckpointBase):
def __init__(self, param: Parameter, cmd: int):
CheckpointBase.__init__(self, param)
self.cmd = cmd
self.param = param
self.step_elapsed = 0
self.use_low_level_segment = param.use_low_level_segment
self.index_from_word = load_index_from_word()
self.onehot_dim = fetch_onehot_vector_dim(param.use_low_level_segment)
self.onehot_func = partial(onehot_from_index, use_low_level_segment=param.use_low_level_segment)
self.encoder_hidden, self.decoder_hidden, self.images = None, None, []
self.initialize()
def fetch_word_index(self, word: str):
if word in self.index_from_word:
return self.index_from_word[word]
else:
return 1 # assigned for 'unknown'
def initialize(self):
self.encoder_hidden, self.decoder_hidden = init_hidden_states(self.param)
self.images = []
def run_step(self, image: np.ndarray, sentence: str) -> Any:
batch = self._prepare_batch(image, sentence)
output = self._run_step(batch)
self.step_elapsed += 1
return output
@property
def onehot_vector(self):
return fetch_onehot_vector_from_index(self.cmd, self.use_low_level_segment).view(1, self.onehot_dim)
def _prepare_batch(self, image: np.ndarray, sentence: str, custom_action_index: int = -1):
# self.initialize()
word_indices = [self.fetch_word_index(w) for w in sentence.lower().split(' ')]
length = torch.tensor([len(word_indices)], dtype=torch.long)
word_indices = torch.tensor(word_indices, dtype=torch.long)
self.images.append(_tensor_from_numpy_image(image))
self.images = self.images[-10:]
data_dict = {
'sentence': sentence,
'word_indices': word_indices,
'length': length,
'images': torch.cat(self.images, dim=1)
}
for key, value in data_dict.items():
if isinstance(value, torch.Tensor):
data_dict[key] = value.to(device=self.param.device_type)
return data_dict
def _run_step(self, data):
self.model.eval()
model_output = self.model.forward(data, self.encoder_hidden, self.decoder_hidden)
output = model_output['output'][0][-1]
if output.size(-1) == 2:
control = carla.VehicleControl()
control.throttle = output[0].item()
control.steer = output[1].item()
control.brake = 0.0
control.hand_brake = False
control.manual_gear_shift = False
return control
else:
return output.item()
class EvaluationMetaInfo:
def __init__(self, timestamp: int, road_option: RoadOption, frame_range: Tuple[int, int]):
self.timestamp = timestamp
self.road_option = road_option
self.frame_range = frame_range
def str_from_road_option(road_option: RoadOption) -> str:
return road_option.name.lower()
def road_option_from_str(road_option_str: str) -> RoadOption:
return fetch_road_option_from_str(road_option_str)
def _prepare_evaluation_param(param: Parameter) -> Parameter:
assert param.eval_data_name
assert param.eval_info_name
assert param.eval_keyword
if param.model_level == 'low':
param.eval_keyword = fetch_road_option_from_str(param.eval_keyword.upper())
elif param.model_level == 'high':
param.eval_keyword = param.eval_keyword.lower()
else:
logger.info(param.model_level)
raise TypeError('invalid eval_keyword was given {}'.format(param.eval_keyword))
param.max_data_length = -1
param.shuffle = False
param.batch_size = 1
param.dataset_data_names = [param.eval_data_name]
param.dataset_info_names = [param.eval_info_name]
if param.model_level == 'low':
param.use_multi_cam = False
param.use_sequence = False
param.has_clusters = False
return param
def fetch_unique_data_from_high_level_dataset(dataset: HighLevelDataset, eval_keyword: str) -> \
List[Tuple[List[DriveDataFrame], str]]:
keywords = dataset.get_extended_keywords()
indices = list(map(itemgetter(0), filter(lambda x: x[1].lower() == eval_keyword, enumerate(keywords))))
def position_from_drive_data_frame(drive_frame: DriveDataFrame):
location = drive_frame.state.transform.location
return location.x, location.y
positions = []
for i in indices:
drive_list = dataset.get_mid_drive_data(i)
xs, ys = zip(*list(map(position_from_drive_data_frame, drive_list)))
positions.append((list(xs), list(ys)))
def compute_list_dist(l1, l2) -> float:
return sqrt(sum([(v2[0] - v1[0]) ** 2 + (v2[1] - v1[1]) ** 2 for v1, v2 in zip(l1, l2)]))
dist_threshold = 10.0
edge_dict = defaultdict(list)
for i, v1 in enumerate(positions):
ivl = sorted([(j, compute_list_dist(v1, positions[j])) for j in range(i + 1, len(positions))],
key=itemgetter(1))
edge_dict[i] = list(map(itemgetter(0), filter(lambda x: x[1] < dist_threshold, ivl)))
visited = [False for _ in range(len(positions))]
unique_indices = []
for i in range(len(positions)):
if visited[i]:
continue
visited[i] = True
for n in edge_dict[i]:
visited[n] = True
unique_indices.append(i)
indices = [indices[i] for i in unique_indices]
data_list = []
for i in indices:
keyword, sentence, data_frame = dataset.get_trajectory_data_from_sequence_index(i)
data_list.append((data_frame.drives, sentence))
return data_list
def load_evaluation_dataset(param: Parameter) -> Tuple[List[List[DriveDataFrame]], List[str]]:
param = _prepare_evaluation_param(param)
data_root = Path.cwd() / '.carla/dataset/evaluation'
if int(param.dataset_data_names[0][-1]) == 1:
data_root = data_root / 'town1'
else:
data_root = data_root / 'town2'
if not data_root.exists():
raise FileNotFoundError('could not find {}'.format(data_root))
data_path = data_root / '{}.json'.format(param.eval_keyword)
with open(str(data_path), 'r') as file:
eval_dict = json.load(file)
drives = [[DriveDataFrame.load_from_str(d) for d in dl] for dl in eval_dict['drives']]
sentences = eval_dict['sentences']
return list(drives), list(sentences)
def load_param_and_evaluator(eval_keyword: str, args, model_type: str):
param = Parameter()
low_level = model_type in ['control', 'stop']
if model_type == 'control':
exp_index = args.control_model_index
exp_name = args.control_model_name
exp_step = args.control_model_step
elif model_type == 'stop':
exp_index = args.stop_model_index
exp_name = args.stop_model_name
exp_step = args.stop_model_step
elif model_type == 'high':
exp_index = args.high_level_index
exp_name = args.high_level_name
exp_step = args.high_level_step
elif model_type == 'single':
exp_index = args.single_model_index
exp_name = args.single_model_name
exp_step = args.single_model_step
else:
raise TypeError('invalid model type {}'.format(model_type))
param.exp_name = exp_name
param.exp_index = exp_index
param.load()
param.batch_size = 1
param.eval_keyword = eval_keyword
param.eval_data_name = args.eval_data_name
param.eval_info_name = args.eval_info_name
logger.info('model type: {}'.format(param.model_type))
cls = LowLevelEvaluator if low_level else (SingleEvaluator if model_type == 'single' else HighLevelEvaluator)
logger.info((model_type, cls, param.model_level, param.encoder_type))
eval_arg = args.exp_cmd if low_level else eval_keyword
evaluator = cls(param, eval_arg)
evaluator.load(step=exp_step)
return param, evaluator
class EvaluationEnvironmentBase(GameEnvironment, EvaluationDirectory):
def __init__(self, args, model_type: str):
GameEnvironment.__init__(self, args=args, agent_type='evaluation')
self.eval_param, self.evaluator = load_param_and_evaluator(args=args, model_type=model_type)
self.eval_transforms = self.world.get_map().get_spawn_points()
EvaluationDirectory.__init__(self, *self.eval_info)
@property
def eval_info(self):
return self.eval_param.exp_index, self.eval_param.exp_name, self.evaluator.step, 'online'
class AllOfflineEvaluationEnvironment(GameEnvironment, EvaluationDirectory):
def __init__(self, eval_keyword: str, args):
self.eval_keyword = eval_keyword
GameEnvironment.__init__(self, args=args, agent_type='evaluation')
self.eval_name = args.eval_name
# load params and evaluators
self.control_param, self.control_evaluator = \
load_param_and_evaluator(eval_keyword=eval_keyword, args=args, model_type='control')
self.stop_param, self.stop_evaluator = \
load_param_and_evaluator(eval_keyword=eval_keyword, args=args, model_type='stop')
self.high_level_param, self.high_level_evaluator = \
load_param_and_evaluator(eval_keyword=eval_keyword, args=args, model_type='high')
# set image type
self.image_type = self.high_level_param.image_type
if 'd' in self.image_type:
from model import DeepLabModel, prepare_deeplab_model
self.deeplab_model: DeepLabModel = prepare_deeplab_model()
self.final_images = []
self.eval_dataset, self.eval_sentences = load_evaluation_dataset(self.high_level_param)
self.eval_transforms = list(map(lambda x: x[0].state.transform, self.eval_dataset))
self.high_level_sentences = self.eval_sentences
logger.info('fetched {} sentences from {}'.format(
len(self.high_level_sentences), self.high_level_param.eval_keyword.lower()))
self.softmax = torch.nn.Softmax(dim=1)
EvaluationDirectory.__init__(self, *self.eval_info)
self.high_level_data_dict = dict()
@property
def eval_info(self):
return self.control_param.exp_index, self.eval_name, \
self.control_evaluator.step, self.eval_keyword
@property
def segment_image(self):
return np.reshape(((self.agent.segment_frame[:, :, 2] == 7).astype(dtype=np.uint8) * 255), (88, 200, 1))
@property
def custom_segment_image(self):
return np.reshape(self.deeplab_model.run(self.agent.image_frame), (88, 200, 1))
@property
def final_image(self):
if self.image_type == 's':
return self.segment_image
elif self.image_type == 'd':
return self.custom_segment_image
elif self.image_type == 'bgr':
return self.agent.image_frame
elif self.image_type == 'bgrs':
return np.concatenate((self.agent.image_frame, self.segment_image), axis=-1)
elif self.image_type == 'bgrd':
return np.concatenate((self.agent.image_frame, self.custom_segment_image), axis=-1)
else:
raise TypeError('invalid image type {}'.format(self.image_type))
def export_evaluation_data(self, t: int, curr_eval_data: dict) -> bool:
with open(str(self.state_path(t)), 'w') as file:
json.dump(curr_eval_data, file, indent=2)
data_frames = [DriveDataFrame.load_from_str(s) for s in curr_eval_data['data_frames']]
controls = list(map(attrgetter('control'), data_frames))
stops, sub_goals = zip(*curr_eval_data['stop_frames'])
texts = ['th{:+4.2f} st{:+4.2f} {:4s}:{:+4.2f}'.format(c.throttle, c.steer, g[:4], s)
for c, s, g in zip(controls, stops, sub_goals)]
text_dict = {i: t for i, t in zip(range(*curr_eval_data['frame_range']), texts)}
src_image_files = [self.agent.image_path(f) for f in range(*curr_eval_data['frame_range'])]
src_image_files = list(filter(lambda x: x.exists(), src_image_files))
if self.image_type in ['s', 'd']:
final_image_files = [self.segment_dir / '{:08d}.png'.format(i) for i in range(len(self.final_images))]
for p, s in zip(final_image_files, self.final_images):
cv2.imwrite(str(p), s)
video_from_files(final_image_files, self.video_dir / 'segment{:02d}.mp4'.format(t),
texts=[], framerate=EVAL_FRAMERATE_SCALE * DATASET_FRAMERATE, revert=False)
image_frames = set([int(s.stem[:-1]) for s in src_image_files])
drive_frames = set(text_dict.keys())
common_frames = sorted(list(image_frames.intersection(drive_frames)))
src_image_files = [self.agent.image_path(f) for f in common_frames]
dst_image_files = [self.image_dir / p.name for p in src_image_files]
[shutil.copy(str(s), str(d)) for s, d in zip(src_image_files, dst_image_files)]
text_list = [text_dict[f] for f in common_frames]
video_from_files(src_image_files, self.video_path(t),
texts=text_list, framerate=EVAL_FRAMERATE_SCALE * DATASET_FRAMERATE, revert=True)
return self.state_path(t).exists()
def run_single_trajectory(self, t: int, transform: carla.Transform) -> Dict[str, bool]:
status = {
'exited': False, # has to finish the entire loop
'finished': False, # this procedure has been finished successfully
'saved': False, # successfully saved the evaluation data
'collided': False, # the agent has collided
'restart': False, # this has to be restarted
'stopped': True # low-level controller returns stop
}
self.agent.reset()
self.agent.move_vehicle(transform)
self.control_evaluator.initialize()
self.stop_evaluator.initialize()
self.high_level_evaluator.initialize()
self.high_level_data_dict[t] = []
self.final_images = []
sentence = random.choice(self.high_level_sentences)
logger.info('moved the vehicle to the position {}'.format(t))
count = 0
frame = None
clock = pygame.time.Clock() if self.show_image else FrameCounter()
set_world_asynchronous(self.world)
sleep(0.5)
set_world_synchronous(self.world)
agent_len, expert_len = LengthComputer(), LengthComputer()
for l in self.eval_dataset[t]:
expert_len(l.state.transform.location)
criterion_len = 2.5 * expert_len.length # 0.9 * expert_len.length
max_iter = 10.0 * len(self.eval_dataset[t]) # 5.0 * len(self.eval_dataset[t])
stop_buffer = []
while agent_len.length < criterion_len and count < max_iter:
if self.show_image and should_quit():
status['exited'] = True
break
if frame is not None and self.agent.collision_sensor.has_collided(frame):
logger.info('collision was detected at frame #{}'.format(frame))
status['collided'] = True
break
if count > 30 and agent_len.length < 1:
logger.info('simulation has a problem in going forward')
status['exited'] = True
break
clock.tick()
self.world.tick()
try:
ts = self.world.wait_for_tick()
except RuntimeError as e:
logger.error('runtime error: {}'.format(e))
status['restart'] = True
return status
if frame is not None:
if ts.frame_count != frame + 1:
logger.info('frame skip!')
frame = ts.frame_count
if self.agent.image_frame is None:
continue
if self.agent.segment_frame is None:
continue
# run high-level evaluator when stopped was triggered by the low-level controller
final_image = self.final_image
if status['stopped']:
logger.info((final_image.shape))
action = self.high_level_evaluator.run_step(final_image, sentence)
action = self.softmax(action)
logger.info((action, action.shape, sentence))
action_index = torch.argmax(action[-1], dim=0).item()
logger.info('action {}, action_index {}'.format(action, action_index))
location = self.agent.fetch_car_state().transform.location
self.high_level_data_dict[t].append((final_image, {
'sentence': sentence,
'location': (location.x, location.y),
'action_index': action_index}))
if action_index < 4:
self.control_evaluator.cmd = action_index
self.stop_evaluator.cmd = action_index
stop_buffer = []
else:
logger.info('the task was finished by "finish"')
status['finished'] = True
break
# run low-level evaluator to apply control and update stopped status
if count % EVAL_FRAMERATE_SCALE == 0:
control: carla.VehicleControl = self.control_evaluator.run_step(final_image)
stop: float = self.stop_evaluator.run_step(final_image)
sub_goal = fetch_high_level_command_from_index(self.control_evaluator.cmd).lower()
logger.info('throttle {:+6.4f}, steer {:+6.4f}, delayed {}, stop {:+6.4f}'.format(
control.throttle, control.steer, frame - self.agent.image_frame_number, stop))
self.agent.step_from_control(frame, control)
self.agent.save_stop(frame, stop, sub_goal)
agent_len(self.agent.data_frame_dict[self.agent.data_frame_number].state.transform.location)
stop_buffer.append(stop)
recent_buffer = stop_buffer[-3:]
status['stopped'] = len(recent_buffer) > 2 and sum(list(map(lambda x: x > 0.0, recent_buffer))) > 1
if self.show_image and self.agent.image_frame is not None:
self.show(self.agent.image_frame, clock)
self.final_images.append(final_image)
count += 1
if agent_len.length >= criterion_len:
logger.info('trajectory length is longer than the threshold')
if count >= max_iter:
logger.info('reached the maximum number of iterations')
if not status['finished']:
status['finished'] = status['collided'] or agent_len.length >= criterion_len or count >= max_iter
if not status['finished']:
return status
curr_eval_data = self.agent.export_eval_data(status['collided'], sentence)
if curr_eval_data is not None:
status['saved'] = self.export_evaluation_data(t, curr_eval_data)
return status
def save_high_level_data(self):
tmp_dir = mkdir_if_not_exists(Path.home() / '.tmp/high-level')
for key in self.high_level_data_dict.keys():
if len(self.high_level_data_dict[key]) != 4:
continue
data_dir = mkdir_if_not_exists(tmp_dir / '{:03d}'.format(key))
dict_list = []
for i, (image, item_dict) in enumerate(self.high_level_data_dict[key]):
cv2.imwrite(str(data_dir / '{:03d}.png'.format(i)), image)
dict_list.append(item_dict)
with open(str(data_dir / 'data.json'), 'w') as file:
json.dump(dict_list, file, indent=2)
def run(self) -> bool:
assert self.evaluation
if self.world is None:
raise ValueError('world was not initialized')
if self.agent is None:
raise ValueError('agent was not initialized')
if self.control_evaluator is None or self.stop_evaluator is None:
raise ValueError('evluation call function was not set')
old_indices = self.traj_indices_from_state_dir()
exited = False
while len(old_indices) < len(self.eval_transforms) and not exited:
try:
t = 0
while t < len(self.eval_transforms):
if t in old_indices:
t += 1
continue
transform = self.eval_transforms[t]
run_status = self.run_single_trajectory(t, transform)
if run_status['finished']:
break
if run_status['restart']:
continue
if run_status['saved']:
old_indices.add(t)
t += 1
finally:
old_indices = self.traj_indices_from_state_dir()
set_world_asynchronous(self.world)
if self.agent is not None:
self.agent.destroy()
self.save_high_level_data()
return True
def listen_keyboard() -> str:
for event in pygame.event.get():
if event.type == pygame.QUIT:
return 'q'
elif event.type == pygame.KEYUP:
if event.key == pygame.K_ESCAPE:
return 'q'
elif event.key == pygame.K_l:
return 'l'
elif event.key == pygame.K_r:
return 'r'
elif event.key == pygame.K_s:
return 's'
elif event.key == pygame.K_f:
return 'f'
elif event.key == pygame.K_u:
return 'u'
elif event.key == pygame.K_i:
return 'i'
elif event.key == pygame.K_o:
return 'o'
elif event.key == pygame.K_j:
return 'j'
elif event.key == pygame.K_k:
return 'k'
elif event.key == pygame.K_m:
return 'm'
elif event.key == pygame.K_COMMA:
return ','
elif event.key == pygame.K_PERIOD:
return '.'
elif event.key == pygame.K_1:
return '1'
elif event.key == pygame.K_2:
return '2'
elif event.key == pygame.K_3:
return '3'
elif event.key == pygame.K_4:
return '4'
elif event.key == pygame.K_5:
return '5'
return ''
__keyword_from_input__ = {
'j': 'left',
'k': 'straight',
'l': 'right',
'u': 'left,left',
'i': 'left,straight',
'o': 'left,right',
'm': 'right,left',
',': 'right,straight',
'.': 'right,right',
'1': 'straight,straight',
'2': 'firstleft',
'3': 'firstright',
'4': 'secondleft',
'5': 'secondright'
}
__input_from_keyword__ = {v: k for k, v in __keyword_from_input__.items()}
__sentence_library_dict__ = generate_templated_sentence_dict()
def get_random_sentence_from_keyword(keyword: str) -> str:
def replace_word(word: str):
if word.startswith('extrastraight'):
return 'straight'
elif word == 'extraleft':
return 'left'
elif word == 'extraright':
return 'right'
else:
return word
words = list(map(replace_word, keyword.split(',')))
keyword = ','.join(words)
if keyword not in __sentence_library_dict__:
raise KeyError('invalid keyword was given {}'.format(keyword))
sentence_group = __sentence_library_dict__[keyword]
sentences = random.choice(sentence_group)
sentence = random.choice(sentences)
return sentence
class AllOnlineEvaluationEnvironment(GameEnvironment, EvaluationDirectory):
def __init__(self, eval_keyword: str, args):
self.eval_keyword = eval_keyword
args.show_game = True
GameEnvironment.__init__(self, args=args, agent_type='evaluation')
# load params and evaluators
self.eval_name = args.eval_name
self.control_param, self.control_evaluator = load_param_and_evaluator(
eval_keyword=eval_keyword, args=args, model_type='control')
self.stop_param, self.stop_evaluator = load_param_and_evaluator(
eval_keyword=eval_keyword, args=args, model_type='stop')
self.high_param, self.high_evaluator = load_param_and_evaluator(
eval_keyword=eval_keyword, args=args, model_type='high')
# set image type
self.image_type = self.high_param.image_type
if 'd' in self.image_type:
from model import DeepLabModel, prepare_deeplab_model
self.deeplab_model: DeepLabModel = prepare_deeplab_model()
self.final_images = []
self.eval_dataset, self.eval_sentences = load_evaluation_dataset(self.high_param)
self.eval_transforms = list(map(lambda x: x[0].state.transform, self.eval_dataset))
self.high_sentences = self.eval_sentences
self.softmax = torch.nn.Softmax(dim=1)
EvaluationDirectory.__init__(self, *self.eval_info)
self.high_data_dict = dict()
@property
def eval_info(self):
return self.control_param.exp_index, self.eval_name, \
self.control_evaluator.step, 'online'
@property
def segment_image(self):
return np.reshape(((self.agent.segment_frame[:, :, 2] == 7).astype(dtype=np.uint8) * 255), (88, 200, 1))
@property
def custom_segment_image(self):
return np.reshape(self.deeplab_model.run(self.agent.image_frame), (88, 200, 1))
@property
def final_image(self):
if self.image_type == 's':
return self.segment_image
elif self.image_type == 'd':
return self.custom_segment_image
elif self.image_type == 'bgr':
return self.agent.image_frame
elif self.image_type == 'bgrs':
return np.concatenate((self.agent.image_frame, self.segment_image), axis=-1)
elif self.image_type == 'bgrd':
return np.concatenate((self.agent.image_frame, self.custom_segment_image), axis=-1)
else:
raise TypeError('invalid image type {}'.format(self.image_type))
def export_video(self, t: int, camera_keyword: str, curr_eval_data: dict):
_, sub_goals = zip(*curr_eval_data['stop_frames'])
texts = ['sentence: {}\nsub-task: {}'.format(s, g)
for g, s in zip(sub_goals, curr_eval_data['sentences'])]
text_dict = {i: t for i, t in zip(range(*curr_eval_data['frame_range']), texts)}
src_image_files = [self.agent.image_path(f, camera_keyword) for f in range(*curr_eval_data['frame_range'])]
src_image_files = list(filter(lambda x: x.exists(), src_image_files))
image_frames = set([int(s.stem[:-1]) for s in src_image_files])
drive_frames = set(text_dict.keys())
common_frames = sorted(list(image_frames.intersection(drive_frames)))
src_image_files = [self.agent.image_path(f, camera_keyword) for f in common_frames]
dst_image_files = [self.image_dir / p.name for p in src_image_files]
[shutil.copy(str(s), str(d)) for s, d in zip(src_image_files, dst_image_files)]
text_list = [text_dict[f] for f in common_frames]
video_from_files(src_image_files, self.video_path(t, camera_keyword),
texts=text_list, framerate=30, revert=True)
def export_segment_video(self, t: int):
final_image_files = [self.segment_dir / '{:08d}.png'.format(i) for i in range(len(self.final_images))]
logger.info('final_image_files {}'.format(len(final_image_files)))
for p, s in zip(final_image_files, self.final_images):
cv2.imwrite(str(p), s)
video_from_files(final_image_files, self.video_dir / 'segment{:02d}.mp4'.format(t),
texts=[], framerate=30, revert=False)
def export_evaluation_data(self, t: int, curr_eval_data: dict) -> bool:
with open(str(self.state_path(t)), 'w') as file:
json.dump(curr_eval_data, file, indent=2)
data_frames = [DriveDataFrame.load_from_str(s) for s in curr_eval_data['data_frames']]
controls = list(map(attrgetter('control'), data_frames))
stops, sub_goals = zip(*curr_eval_data['stop_frames'])
logger.info('controls, stops, goals {}, {}, {}'.format(len(controls), len(stops), len(sub_goals)))
self.export_video(t, 'center', curr_eval_data)
self.export_video(t, 'extra', curr_eval_data)
self.export_segment_video(t)
return self.state_path(t).exists()
def run_single_trajectory(self, t: int, transform: carla.Transform) -> Dict[str, bool]:
status = {
'exited': False, # has to finish the entire loop
'finished': False, # this procedure has been finished successfully
'saved': False, # successfully saved the evaluation data
'collided': False, # the agent has collided
'restart': False, # this has to be restarted
'stopped': True # low-level controller returns stop
}
self.agent.reset()
self.agent.move_vehicle(transform)
self.control_evaluator.initialize()
self.stop_evaluator.initialize()
self.high_evaluator.initialize()
self.high_data_dict[t] = []
self.final_images = []
self.sentence = get_random_sentence_from_keyword(self.eval_keyword)
logger.info('moved the vehicle to the position {}'.format(t))
count = 0
frame = None
clock = pygame.time.Clock()
set_world_asynchronous(self.world)
sleep(0.5)
set_world_synchronous(self.world)
stop_buffer = []
while not status['exited'] or not status['collided']:
keyboard_input = listen_keyboard()
if keyboard_input == 'q':
status['exited'] = True
break
elif keyboard_input in __keyword_from_input__.keys():
keyword = __keyword_from_input__[keyboard_input]
if keyword != self.eval_keyword:
self.eval_keyword = keyword
self.sentence = get_random_sentence_from_keyword(self.eval_keyword)
self.control_param.eval_keyword = keyword
self.stop_param.eval_keyword = keyword
self.high_param.eval_keyword = keyword
self.control_evaluator.param = self.control_param
self.stop_evaluator.param = self.stop_param
self.high_evaluator.cmd = keyword
self.high_evaluator.param = self.high_param
self.high_evaluator.sentence = keyword.lower()
self.control_evaluator.initialize()
self.stop_evaluator.initialize()
self.high_evaluator.initialize()
logger.info('updated sentence {}'.format(self.sentence))
if frame is not None and self.agent.collision_sensor.has_collided(frame):
logger.info('collision was detected at frame #{}'.format(frame))
status['collided'] = True
break
clock.tick()
self.world.tick()
try:
ts = self.world.wait_for_tick()
except RuntimeError as e:
logger.error('runtime error: {}'.format(e))
status['restart'] = True
return status
if frame is not None:
if ts.frame_count != frame + 1:
logger.info('frame skip!')
frame = ts.frame_count
if self.agent.image_frame is None:
continue
if self.agent.segment_frame is None:
continue
# run high-level evaluator when stopped was triggered by the low-level controller
final_image = self.final_image
if status['stopped']:
action = self.high_evaluator.run_step(final_image, self.sentence)
action = self.softmax(action)
action_index = torch.argmax(action[-1], dim=0).item()
location = self.agent.fetch_car_state().transform.location
self.high_data_dict[t].append((final_image, {
'sentence': self.sentence,
'location': (location.x, location.y),
'action_index': action_index}))
if action_index < 4:
self.control_evaluator.cmd = action_index
self.stop_evaluator.cmd = action_index
stop_buffer = []
else:
logger.info('the task was finished by "finish"')
status['finished'] = True
break
# run low-level evaluator to apply control and update stopped status
if count % EVAL_FRAMERATE_SCALE == 0:
control: carla.VehicleControl = self.control_evaluator.run_step(final_image)
stop: float = self.stop_evaluator.run_step(final_image)
sub_goal = fetch_high_level_command_from_index(self.control_evaluator.cmd).lower()
logger.info('throttle {:+6.4f}, steer {:+6.4f}, delayed {}, current {:d}, stop {:+6.4f}'.
format(control.throttle, control.steer, frame - self.agent.image_frame_number, action_index,
stop))
self.agent.step_from_control(frame, control)
self.agent.save_stop(frame, stop, sub_goal)
self.agent.save_cmd(frame, self.sentence)
stop_buffer.append(stop)
recent_buffer = stop_buffer[-3:]
status['stopped'] = len(recent_buffer) > 2 and sum(list(map(lambda x: x > 0.0, recent_buffer))) > 1
if self.show_image and self.agent.image_frame is not None:
self.show(self.agent.image_frame, clock, extra_str=self.sentence)
self.final_images.append(final_image)
count += 1
logger.info('saving information')
curr_eval_data = self.agent.export_eval_data(status['collided'], self.sentence)
if curr_eval_data is not None:
status['saved'] = self.export_evaluation_data(t, curr_eval_data)
return status
def run(self) -> bool:
assert self.evaluation
if self.world is None:
raise ValueError('world was not initialized')
if self.agent is None:
raise ValueError('agent was not initialized')
if self.control_evaluator is None or self.stop_evaluator is None:
raise ValueError('evaluation call function was not set')
old_indices = self.traj_indices_from_state_dir()
exited = False
while len(old_indices) < len(self.eval_transforms) and not exited:
try:
t = 0
while t < len(self.eval_transforms):
if t in old_indices:
t += 1
continue
transform = self.eval_transforms[t]
run_status = self.run_single_trajectory(t, transform)
if run_status['exited']:
exited = True
break
if run_status['finished']:
break
if run_status['restart']:
continue
if run_status['saved']:
old_indices.add(t)
t += 1
finally:
old_indices = self.traj_indices_from_state_dir()
set_world_asynchronous(self.world)
if self.agent is not None:
self.agent.destroy()
return not exited
def fetch_ip_address():