-
Notifications
You must be signed in to change notification settings - Fork 8
/
SG_Nav.py
1036 lines (908 loc) · 46.5 KB
/
SG_Nav.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 argparse
import copy
import math
import os
from matplotlib import colors
import cv2
import numpy as np
import pandas
import skimage
import torch
import habitat
from GLIP.maskrcnn_benchmark.config import cfg as glip_cfg
from GLIP.maskrcnn_benchmark.engine.predictor_glip import GLIPDemo
from pslpython.model import Model as PSLModel
from pslpython.partition import Partition
from pslpython.predicate import Predicate
from pslpython.rule import Rule
from scenegraph import SceneGraph
import utils.utils_fmm.control_helper as CH
import utils.utils_fmm.pose_utils as pu
from utils.utils_fmm.fmm_planner import FMMPlanner
from utils.utils_fmm.mapping import Semantic_Mapping
from utils.utils_glip import *
from utils.image_process import (
add_resized_image,
add_rectangle,
add_text,
add_text_list,
crop_around_point,
draw_agent,
draw_goal,
line_list
)
ADDITIONAL_PSL_OPTIONS = {
'log4j.threshold': 'INFO'
}
ADDITIONAL_CLI_OPTIONS = [
# '--postgres'
]
class SG_Nav_Agent():
"""
New in this version:
1. use obj and room reasoning by record object locations and build a room map
experiments: v4_4
"""
def __init__(self, task_config, args=None):
self._POSSIBLE_ACTIONS = task_config.TASK.POSSIBLE_ACTIONS
self.config = task_config
self.args = args
self.panoramic = []
self.panoramic_depth = []
self.turn_angles = 0
self.device = (
torch.device("cuda:{}".format(0))
if torch.cuda.is_available()
else torch.device("cpu")
)
self.prev_action = 0
self.navigate_steps = 0
self.move_steps = 0
self.total_steps = 0
self.found_goal = False
self.found_goal_times = 0
self.threshold_list = {'bathtub': 3, 'bed': 3, 'cabinet': 2, 'chair': 1, 'chest_of_drawers': 3, 'clothes': 2, 'counter': 1, 'cushion': 3, 'fireplace': 3, 'gym_equipment': 2, 'picture': 3, 'plant': 3, 'seating': 0, 'shower': 2, 'sink': 2, 'sofa': 2, 'stool': 2, 'table': 1, 'toilet': 3, 'towel': 2, 'tv_monitor': 0}
self.found_goal_times_threshold = 3
self.distance_threshold = 5
self.correct_room = False
self.changing_room = False
self.changing_room_steps = 0
self.move_after_new_goal = False
self.former_check_step = -10
self.goal_disappear_step = 100
self.force_change_room = False
self.current_room_search_step = 0
self.target_room = ''
self.current_rooms = []
self.nav_without_goal_step = 0
self.former_collide = 0
self.history_pose = []
self.visualize_image_list = []
self.count_episodes = -1
self.loop_time = 0
self.stuck_time = 0
self.rooms = rooms
self.rooms_captions = rooms_captions
self.split = (self.args.split_l >= 0)
self.metrics = {'distance_to_goal': 0., 'spl': 0., 'softspl': 0.}
### ------ init glip model ------ ###
config_file = "GLIP/configs/pretrain/glip_Swin_L.yaml"
weight_file = "GLIP/MODEL/glip_large_model.pth"
# config_file = "GLIP/configs/pretrain/glip_Swin_T_O365_GoldG.yaml"
# weight_file = "GLIP/MODEL/glip_tiny_model_o365_goldg_cc_sbu.pth"
glip_cfg.local_rank = 0
glip_cfg.num_gpus = 1
glip_cfg.merge_from_file(config_file)
glip_cfg.merge_from_list(["MODEL.WEIGHT", weight_file])
glip_cfg.merge_from_list(["MODEL.DEVICE", "cuda"])
self.glip_demo = GLIPDemo(
glip_cfg,
min_image_size=800,
confidence_threshold=0.61,
show_mask_heatmaps=False
)
print('glip init finish!!!')
### ----- init some static variables ----- ###
self.map_size_cm = 4000
self.resolution = self.map_resolution = 5
self.camera_horizon = 0
self.dilation_deg = 0
self.collision_threshold = 0.08
self.col_width = 5
self.selem = skimage.morphology.square(1)
self.explanation = ''
### ----- init maps ----- ###
self.init_map()
self.sem_map_module = Semantic_Mapping(self).to(self.device)
self.free_map_module = Semantic_Mapping(self, max_height=10,min_height=-150).to(self.device)
self.room_map_module = Semantic_Mapping(self, max_height=200,min_height=-10, num_cats=9).to(self.device)
self.free_map_module.eval()
self.free_map_module.set_view_angles(self.camera_horizon)
self.sem_map_module.eval()
self.sem_map_module.set_view_angles(self.camera_horizon)
self.room_map_module.eval()
self.room_map_module.set_view_angles(self.camera_horizon)
self.camera_matrix = self.free_map_module.camera_matrix
print('FMM navigate map init finish!!!')
### ----- load commonsense from LLMs ----- ###
self.goal_idx = {}
for key in projection:
self.goal_idx[projection[key]] = categories_21.index(projection[key]) # each goal corresponding to which column in co-orrcurance matrix
self.co_occur_mtx = np.load('tools/obj.npy')
self.co_occur_mtx -= self.co_occur_mtx.min()
self.co_occur_mtx /= self.co_occur_mtx.max()
self.co_occur_room_mtx = np.load('tools/room.npy')
self.co_occur_room_mtx -= self.co_occur_room_mtx.min()
self.co_occur_room_mtx /= self.co_occur_room_mtx.max()
### ----- option: using PSL optimization ADMM ----- ###
if self.args.PSL_infer:
self.psl_model = PSLModel('objnav1') ## important: please use different name here for different process in the same machine. eg. objnav, objnav2, ...
# Add Predicates
self.add_predicates(self.psl_model)
# Add Rules
self.add_rules(self.psl_model)
### ----- load scene graph module ----- ###
self.scenegraph = SceneGraph(map_resolution=self.map_resolution, map_size_cm=self.map_size_cm, map_size=self.map_size, camera_matrix=self.camera_matrix)
self.experiment_name = 'test_2'
if self.split:
self.experiment_name = self.experiment_name + f'/[{self.args.split_l}:{self.args.split_r}]'
self.visualization_dir = f'data/visualization/{self.experiment_name}/'
print('scene graph module init finish!!!')
def add_predicates(self, model):
"""
add predicates for ADMM PSL inference
"""
if self.args.reasoning in ['both', 'obj']:
predicate = Predicate('IsNearObj', closed = True, size = 2)
model.add_predicate(predicate)
predicate = Predicate('ObjCooccur', closed = True, size = 1)
model.add_predicate(predicate)
if self.args.reasoning in ['both', 'room']:
predicate = Predicate('IsNearRoom', closed = True, size = 2)
model.add_predicate(predicate)
predicate = Predicate('RoomCooccur', closed = True, size = 1)
model.add_predicate(predicate)
predicate = Predicate('Choose', closed = False, size = 1)
model.add_predicate(predicate)
predicate = Predicate('ShortDist', closed = True, size = 1)
model.add_predicate(predicate)
def add_rules(self, model):
"""
add rules for ADMM PSL inference
"""
if self.args.reasoning in ['both', 'obj']:
model.add_rule(Rule('2: ObjCooccur(O) & IsNearObj(O,F) -> Choose(F)^2'))
model.add_rule(Rule('2: !ObjCooccur(O) & IsNearObj(O,F) -> !Choose(F)^2'))
if self.args.reasoning in ['both', 'room']:
model.add_rule(Rule('2: RoomCooccur(R) & IsNearRoom(R,F) -> Choose(F)^2'))
model.add_rule(Rule('2: !RoomCooccur(R) & IsNearRoom(R,F) -> !Choose(F)^2'))
model.add_rule(Rule('2: ShortDist(F) -> Choose(F)^2'))
model.add_rule(Rule('Choose(+F) = 1 .'))
def reset(self):
"""
reset variables for each episodes
"""
self.navigate_steps = 0
self.turn_angles = 0
self.move_steps = 0
self.total_steps = 0
self.current_room_search_step = 0
self.found_goal = False
self.found_goal_times = 0
self.ever_long_goal = False
self.correct_room = False
self.changing_room = False
self.goal_loc = None
self.changing_room_steps = 0
self.move_after_new_goal = False
self.former_check_step = -10
self.goal_disappear_step = 100
self.prev_action = 0
self.col_width = 5
self.former_collide = 0
self.goal_gps = np.array([0.,0.])
self.long_goal_temp_gps = np.array([0.,0.])
self.last_gps = np.array([11100.,11100.])
self.has_panarama = False
self.init_map()
self.last_loc = self.full_pose
self.panoramic = []
self.panoramic_depth = []
self.current_rooms = []
self.dist_to_frontier_goal = 10
self.first_fbe = False
self.goal_map = np.zeros(self.full_map.shape[-2:])
self.found_long_goal = False
self.history_pose = []
self.visualize_image_list = []
self.count_episodes = self.count_episodes + 1
self.loop_time = 0
self.stuck_time = 0
self.metrics = {'distance_to_goal': 0., 'spl': 0., 'softspl': 0.}
self.obj_goal = self.simulator._env.current_episode.object_category
###########
self.current_obj_predictions = []
self.obj_locations = [[] for i in range(21)] # length equal to all the objects in reference matrix
self.not_move_steps = 0
self.move_since_random = 0
self.using_random_goal = False
self.fronter_this_ex = 0
self.random_this_ex = 0
########### error analysis
self.detect_true = False
self.goal_appear = False
self.frontiers_gps = []
self.last_location = np.array([0.,0.])
self.current_stuck_steps = 0
self.total_stuck_steps = 0
self.explanation = ''
self.text_node = ''
self.text_edge = ''
self.scenegraph.reset()
def detect_objects(self, observations):
"""
detect objects from current observations and update semantic map.
"""
self.current_obj_predictions = self.glip_demo.inference(observations["rgb"][:,:,[2,1,0]], object_captions) # GLIP object detection, time cosuming
new_labels = self.get_glip_real_label(self.current_obj_predictions) # transfer int labels to string labels
self.current_obj_predictions.add_field("labels", new_labels)
shortest_distance = 120
shortest_distance_angle = 0
goal_prediction = copy.deepcopy(self.current_obj_predictions)
obj_labels = self.current_obj_predictions.get_field("labels")
goal_bbox = []
### save the bounding boxes if there is a goal object
for j, label in enumerate(obj_labels):
if self.obj_goal in label:
goal_bbox.append(self.current_obj_predictions.bbox[j])
elif self.obj_goal == 'gym_equipment' and (label in ['treadmill', 'exercise machine']):
goal_bbox.append(self.current_obj_predictions.bbox[j])
### record the location of object center in the semantic map for object reasoning.
if self.args.reasoning == 'both' or 'obj':
for j, label in enumerate(obj_labels):
if label in categories_21_origin:
confidence = self.current_obj_predictions.get_field("scores")[j]
bbox = self.current_obj_predictions.bbox[j].to(torch.int64)
center_point = (bbox[:2] + bbox[2:]) // 2
temp_direction = (center_point[0] - 320) * 79 / 640
temp_distance = self.depth[center_point[1],center_point[0],0]
if temp_distance >= 4.999:
continue
obj_gps = self.get_goal_gps(observations, temp_direction, temp_distance)
x = int(self.map_size_cm/10-obj_gps[1]*100/self.resolution)
y = int(self.map_size_cm/10+obj_gps[0]*100/self.resolution)
self.obj_locations[categories_21_origin.index(label)].append([confidence, x, y])
### if detect a goal object, determine if it's beyond 5 meters or not.
if len(goal_bbox) > 0:
long_goal_detected_before = copy.deepcopy(self.found_long_goal)
goal_prediction.bbox = torch.stack(goal_bbox)
for box in goal_prediction.bbox: ## select the closest goal as the detected goal
box = box.to(torch.int64)
center_point = (box[:2] + box[2:]) // 2
temp_direction = (center_point[0] - 320) * 79 / 640
temp_distance = self.depth[center_point[1],center_point[0],0]
k = 0
pos_neg = 1
## case that a detected goal is within 0.5 meters, maybe it's because the image is corrupted, let's find another points in the image instead of the center point
while temp_distance >= 100 and 0<center_point[1]+int(pos_neg*k)<479 and 0<center_point[0]+int(pos_neg*k)<639:
pos_neg *= -1
k += 0.5
temp_distance = max(self.depth[center_point[1]+int(pos_neg*k),center_point[0],0],
self.depth[center_point[1],center_point[0]+int(pos_neg*k),0])
if temp_distance >= 4.999:
self.found_long_goal = True
self.ever_long_goal = True
else:
if self.found_goal:
if temp_distance < self.distance_threshold:
self.found_goal_times = self.found_goal_times + 1
self.found_goal = True
self.found_long_goal = False
## select the closest goal
direction = temp_direction
distance = temp_distance
if distance < shortest_distance:
shortest_distance = distance
shortest_distance_angle = direction
box_shortest = copy.deepcopy(box)
if self.found_goal:
self.goal_gps = self.get_goal_gps(observations, shortest_distance_angle, shortest_distance)
elif not long_goal_detected_before:
# if detected a long goal before, then don't change it until see a goal within 5 meters
self.long_goal_temp_gps = self.get_goal_gps(observations, shortest_distance_angle, shortest_distance)
if self.args.error_analysis and self.found_goal:
if (observations['semantic'][box_shortest[0]:box_shortest[2],box_shortest[1]:box_shortest[3]] == self.goal_mp3d_idx).sum() > min(300, 0.2 * (box_shortest[2]-box_shortest[0])*(box_shortest[3]-box_shortest[1])):
self.detect_true = True
else:
if self.found_goal:
self.found_goal = False
self.found_goal_times = 0
def act(self, observations):
"""
observations:
"""
if self.total_steps >= 500:
return {"action": 0}
self.total_steps += 1
if self.navigate_steps == 0:
# self.obj_goal = projection[int(observations["objectgoal"])]
self.prob_array_room = self.co_occur_room_mtx[self.goal_idx[self.obj_goal]]
self.prob_array_obj = self.co_occur_mtx[self.goal_idx[self.obj_goal]]
## ADMM PSL optim only
if self.args.PSL_infer == 'optim':
if self.args.reasoning in ['both','room']:
for predicate in self.psl_model.get_predicates().values():
if predicate.name() in ['ROOMCOOCCUR']:
predicate.clear_data()
prob_array_room_list = list(self.prob_array_room)
data = pandas.DataFrame([[i, prob_array_room_list[i]] for i in range(len(prob_array_room_list))], columns = list(range(2)))
self.psl_model.get_predicate('RoomCooccur').add_data(Partition.OBSERVATIONS, data)
if self.args.reasoning in ['both','obj']:
for predicate in self.psl_model.get_predicates().values():
if predicate.name() in ['OBJCOOCCUR']:
predicate.clear_data()
prob_array_obj_list = list(self.prob_array_obj)
data = pandas.DataFrame([[i, prob_array_obj_list[i]] for i in range(len(prob_array_obj_list))], columns = list(range(2)))
self.psl_model.get_predicate('ObjCooccur').add_data(Partition.OBSERVATIONS, data)
observations["depth"][observations["depth"]==0.5] = 100 # don't construct unprecise map with distance less than 0.5 m
self.depth = observations["depth"]
self.rgb = observations["rgb"][:,:,[2,1,0]]
observations["rgb_annotated"] = observations["rgb"]
self.scenegraph.set_agent(self)
self.scenegraph.set_navigate_steps(self.navigate_steps)
self.scenegraph.set_obj_goal(self.obj_goal)
self.scenegraph.set_room_map(self.room_map)
self.scenegraph.set_fbe_free_map(self.fbe_free_map)
self.scenegraph.set_observations(observations)
self.scenegraph.set_full_map(self.full_map)
self.scenegraph.set_full_pose(self.full_pose)
self.scenegraph.update_scenegraph()
self.update_map(observations)
self.update_free_map(observations)
# look down twice and look around at first to initialize map
if self.total_steps == 1:
# look down
self.sem_map_module.set_view_angles(30)
self.free_map_module.set_view_angles(30)
# self.observed_map_module.set_view_angles(30)
return {"action": 5}
elif self.total_steps <= 7:
return {"action": 6}
elif self.total_steps == 8:
# look down
self.sem_map_module.set_view_angles(60)
self.free_map_module.set_view_angles(60)
# self.observed_map_module.set_view_angles(60)
return {"action": 5}
elif self.total_steps <= 14:
return {"action": 6}
elif self.total_steps <= 15:
self.sem_map_module.set_view_angles(30)
self.free_map_module.set_view_angles(30)
# self.observed_map_module.set_view_angles(30)
return {"action": 4}
elif self.total_steps <= 16:
self.sem_map_module.set_view_angles(0)
self.free_map_module.set_view_angles(0)
# self.observed_map_module.set_view_angles(0)
return {"action": 4}
# get panoramic view at first
if self.total_steps <= 22 and not self.found_goal:
self.panoramic.append(observations["rgb"][:,:,[2,1,0]])
self.panoramic_depth.append(observations["depth"])
self.detect_objects(observations)
room_detection_result = self.glip_demo.inference(observations["rgb"][:,:,[2,1,0]], rooms_captions)
self.update_room_map(observations, room_detection_result)
if not self.found_goal: # if found a goal, directly go to it
return {"action": 6}
if not (observations["gps"] == self.last_gps).all():
self.move_steps += 1
self.not_move_steps = 0
if self.using_random_goal:
self.move_since_random += 1
else:
self.not_move_steps += 1
self.last_gps = observations["gps"]
self.scenegraph.perception()
### ------ generate action using FMM ------ ###
## update pose and map
self.history_pose.append(self.full_pose.cpu().detach().clone())
input_pose = np.zeros(7)
input_pose[:3] = self.full_pose.cpu().numpy()
input_pose[1] = self.map_size_cm/100 - input_pose[1]
input_pose[2] = -input_pose[2]
input_pose[4] = self.full_map.shape[-2]
input_pose[6] = self.full_map.shape[-1]
traversible, cur_start, cur_start_o = self.get_traversible(self.full_map.cpu().numpy()[0,0,::-1], input_pose)
if self.found_goal:
## directly go to goal
self.not_use_random_goal()
self.goal_map = np.zeros(self.full_map.shape[-2:])
self.goal_map[max(0,min(self.map_size - 1,int(self.map_size_cm/10+self.goal_gps[1]*100/self.resolution))), max(0,min(self.map_size - 1,int(self.map_size_cm/10+self.goal_gps[0]*100/self.resolution)))] = 1
elif self.found_long_goal:
## go to long goal
self.not_use_random_goal()
self.goal_map = np.zeros(self.full_map.shape[-2:])
self.goal_map[max(0,min(self.map_size - 1,int(self.map_size_cm/10+self.long_goal_temp_gps[1]*100/self.resolution))), max(0,min(self.map_size - 1,int(self.map_size_cm/10+self.long_goal_temp_gps[0]*100/self.resolution)))] = 1
elif not self.first_fbe: # first FBE process
self.goal_loc = self.fbe(traversible, cur_start)
self.not_use_random_goal()
self.first_fbe = True
self.goal_map = np.zeros(self.full_map.shape[-2:])
if self.goal_loc is None:
self.random_this_ex += 1
self.goal_map = self.set_random_goal()
self.using_random_goal = True
else:
self.fronter_this_ex += 1
self.goal_map[self.goal_loc[0], self.goal_loc[1]] = 1
self.goal_map = self.goal_map[::-1]
stg_y, stg_x, number_action = self._plan(traversible, self.goal_map, self.full_pose, cur_start, cur_start_o, self.found_goal)
if self.found_long_goal and number_action == 0: # didn't detect goal when arrive at long goal, start over FBE.
self.found_long_goal = False
if (not self.found_goal and not self.found_long_goal and number_action == 0) or (self.using_random_goal and self.move_since_random > 20):
# FBE if arrive at a selected frontier, or randomly explore for some steps
self.goal_loc = self.fbe(traversible, cur_start)
self.not_use_random_goal()
self.goal_map = np.zeros(self.full_map.shape[-2:])
if self.goal_loc is None:
self.random_this_ex += 1
self.goal_map = self.set_random_goal()
self.using_random_goal = True
else:
self.fronter_this_ex += 1
self.goal_map[self.goal_loc[0], self.goal_loc[1]] = 1
self.goal_map = self.goal_map[::-1]
stg_y, stg_x, number_action = self._plan(traversible, self.goal_map, self.full_pose, cur_start, cur_start_o, self.found_goal)
self.loop_time = 0
while (not self.found_goal and number_action == 0) or self.not_move_steps >= 7:
# the agent is stuck, then random explore
self.loop_time += 1
self.random_this_ex += 1
self.stuck_time += 1
if self.loop_time > 20 or self.stuck_time == 5:
return {"action": 0}
self.not_move_steps = 0
self.goal_map = self.set_random_goal()
self.using_random_goal = True
stg_y, stg_x, number_action = self._plan(traversible, self.goal_map, self.full_pose, cur_start, cur_start_o, self.found_goal)
if self.args.visualize:
self.visualize(traversible, observations, number_action)
observations["pointgoal_with_gps_compass"] = self.get_relative_goal_gps(observations)
###-----------------------------------###
self.last_loc = copy.deepcopy(self.full_pose)
self.prev_action = number_action
self.navigate_steps += 1
torch.cuda.empty_cache()
return {"action": number_action}
def not_use_random_goal(self):
self.move_since_random = 0
self.using_random_goal = False
def get_glip_real_label(self, prediction):
labels = prediction.get_field("labels").tolist()
new_labels = []
if self.glip_demo.entities and self.glip_demo.plus:
for i in labels:
if i <= len(self.glip_demo.entities):
new_labels.append(self.glip_demo.entities[i - self.glip_demo.plus])
else:
new_labels.append('object')
else:
new_labels = ['object' for i in labels]
return new_labels
def fbe(self, traversible, start):
"""
fontier: unknown area and free area
unknown area: not free and not obstacle
select a frontier using commonsense and PSL and return a GPS
"""
fbe_map = torch.zeros_like(self.full_map[0,0])
fbe_map[self.fbe_free_map[0,0]>0] = 1 # first free
fbe_map[skimage.morphology.binary_dilation(self.full_map[0,0].cpu().numpy(), skimage.morphology.disk(4))] = 3 # then dialte obstacle
fbe_cp = copy.deepcopy(fbe_map)
fbe_cpp = copy.deepcopy(fbe_map)
fbe_cp[fbe_cp==0] = 4 # don't know space is 4
fbe_cp[fbe_cp<4] = 0 # free and obstacle
selem = skimage.morphology.disk(1)
fbe_cpp[skimage.morphology.binary_dilation(fbe_cp.cpu().numpy(), selem)] = 0 # don't know space is 0 dialate unknown space
diff = fbe_map - fbe_cpp # intersection between unknown area and free area
frontier_map = diff == 1
frontier_locations = torch.stack([torch.where(frontier_map)[0], torch.where(frontier_map)[1]]).T
num_frontiers = len(torch.where(frontier_map)[0])
if num_frontiers == 0:
return None
# for each frontier, calculate the inverse of distance
planner = FMMPlanner(traversible, None)
state = [start[0] + 1, start[1] + 1]
planner.set_goal(state)
fmm_dist = planner.fmm_dist[::-1]
frontier_locations += 1
frontier_locations = frontier_locations.cpu().numpy()
distances = fmm_dist[frontier_locations[:,0],frontier_locations[:,1]] / 20
## use the threshold of 1.6 to filter close frontiers to encourage exploration
idx_16 = np.where(distances>=1.6)
distances_16 = distances[idx_16]
distances_16_inverse = 1 - (np.clip(distances_16,0,11.6)-1.6) / (11.6-1.6)
frontier_locations_16 = frontier_locations[idx_16]
self.frontier_locations = frontier_locations
self.frontier_locations_16 = frontier_locations_16
if len(distances_16) == 0:
return None
num_16_frontiers = len(idx_16[0]) # 175
# scores = np.zeros((num_16_frontiers))
scores = self.scenegraph.score(frontier_locations_16, num_16_frontiers)
# select the frontier with highest score
if self.args.PSL_infer != 'optim':
if self.args.reasoning == 'both': # True
scores += 2 * distances_16_inverse
else:
scores += 1 * distances_16_inverse
idx_16_max = idx_16[0][np.argmax(scores)]
goal = frontier_locations[idx_16_max] - 1
else:
data = pandas.DataFrame([[i] for i in range(num_16_frontiers)], columns = list(range(1)))
self.psl_model.get_predicate('Choose').add_data(Partition.TARGETS, data)
data = pandas.DataFrame([[i, distances_16_inverse[i]] for i in range(num_16_frontiers)], columns = list(range(2)))
self.psl_model.get_predicate('ShortDist').add_data(Partition.OBSERVATIONS, data)
result = self.psl_model.infer(additional_cli_options = ADDITIONAL_CLI_OPTIONS, psl_config = ADDITIONAL_PSL_OPTIONS)
for key, value in result.items():
result_dt_frame = value
scores = result_dt_frame.loc[:,'truth']
idx_16_max = idx_16[0][np.argmax(scores)]
goal = frontier_locations[idx_16_max]
self.scores = scores
return goal
def get_goal_gps(self, observations, angle, distance):
### return goal gps in the original agent coordinates
if type(angle) is torch.Tensor:
angle = angle.cpu().numpy()
agent_gps = observations['gps']
agent_compass = observations['compass']
goal_direction = agent_compass - angle/180*np.pi
goal_gps = np.array([(agent_gps[0]+np.cos(goal_direction)*distance).item(),
(agent_gps[1]-np.sin(goal_direction)*distance).item()])
return goal_gps
def get_relative_goal_gps(self, observations, goal_gps=None):
if goal_gps is None:
goal_gps = self.goal_gps
direction_vector = goal_gps - np.array([observations['gps'][0].item(),observations['gps'][1].item()])
rho = np.sqrt(direction_vector[0]**2 + direction_vector[1]**2)
phi_world = np.arctan2(direction_vector[1], direction_vector[0])
agent_compass = observations['compass']
phi = phi_world - agent_compass
return np.array([rho, phi.item()], dtype=np.float32)
def init_map(self):
self.map_size = self.map_size_cm // self.map_resolution
full_w, full_h = self.map_size, self.map_size
self.full_map = torch.zeros(1,1 ,full_w, full_h).float().to(self.device)
self.room_map = torch.zeros(1,9 ,full_w, full_h).float().to(self.device)
self.visited = self.full_map[0,0].cpu().numpy()
self.collision_map = self.full_map[0,0].cpu().numpy()
self.fbe_free_map = copy.deepcopy(self.full_map).to(self.device) # 0 is unknown, 1 is free
self.full_pose = torch.zeros(3).float().to(self.device)
# Origin of local map
self.origins = np.zeros((2))
def init_map_and_pose():
self.full_map.fill_(0.)
self.full_pose.fill_(0.)
# full_pose[:, 2] = 90
self.full_pose[:2] = self.map_size_cm / 100.0 / 2.0 # put the agent in the middle of the map
init_map_and_pose()
def update_map(self, observations):
"""
full pose: gps and angle in the initial coordinate system, where 0 is towards the x axis
"""
self.full_pose[0] = self.map_size_cm / 100.0 / 2.0+torch.from_numpy(observations['gps']).to(self.device)[0]
self.full_pose[1] = self.map_size_cm / 100.0 / 2.0-torch.from_numpy(observations['gps']).to(self.device)[1]
self.full_pose[2:] = torch.from_numpy(observations['compass'] * 57.29577951308232).to(self.device) # input degrees and meters
self.full_map = self.sem_map_module(torch.squeeze(torch.from_numpy(observations['depth']), dim=-1).to(self.device), self.full_pose, self.full_map)
def update_free_map(self, observations):
"""
update free map using visual projection
"""
self.full_pose[0] = self.map_size_cm / 100.0 / 2.0+torch.from_numpy(observations['gps']).to(self.device)[0]
self.full_pose[1] = self.map_size_cm / 100.0 / 2.0-torch.from_numpy(observations['gps']).to(self.device)[1]
self.full_pose[2:] = torch.from_numpy(observations['compass'] * 57.29577951308232).to(self.device) # input degrees and meters
self.fbe_free_map = self.free_map_module(torch.squeeze(torch.from_numpy(observations['depth']), dim=-1).to(self.device), self.full_pose, self.fbe_free_map)
self.fbe_free_map[int(self.map_size_cm / 10) - 3:int(self.map_size_cm / 10) + 4, int(self.map_size_cm / 10) - 3:int(self.map_size_cm / 10) + 4] = 1
def update_room_map(self, observations, room_prediction_result):
new_room_labels = self.get_glip_real_label(room_prediction_result)
type_mask = np.zeros((9,self.config.SIMULATOR.DEPTH_SENSOR.HEIGHT, self.config.SIMULATOR.DEPTH_SENSOR.WIDTH))
bboxs = room_prediction_result.bbox
score_vec = torch.zeros((9)).to(self.device)
for i, box in enumerate(bboxs):
box = box.to(torch.int64)
idx = rooms.index(new_room_labels[i])
type_mask[idx,box[1]:box[3],box[0]:box[2]] = 1
score_vec[idx] = room_prediction_result.get_field("scores")[i]
self.room_map = self.room_map_module(torch.squeeze(torch.from_numpy(observations['depth']), dim=-1).to(self.device), self.full_pose, self.room_map, torch.from_numpy(type_mask).to(self.device).type(torch.float32), score_vec)
# self.room_map_refine = copy.deepcopy(self.room_map)
# other_room_map_sum = self.room_map_refine[0,0] + torch.sum(self.room_map_refine[0,2:],axis=0)
# self.room_map_refine[0,1][other_room_map_sum>0] = 0
def get_traversible(self, map_pred, pose_pred):
"""
update traversible map
"""
grid = np.rint(map_pred)
# Get pose prediction and global policy planning window
start_x, start_y, start_o, gx1, gx2, gy1, gy2 = pose_pred
gx1, gx2, gy1, gy2 = int(gx1), int(gx2), int(gy1), int(gy2)
planning_window = [gx1, gx2, gy1, gy2]
# Get curr loc
r, c = start_y, start_x
start = [int(r*100/self.map_resolution - gy1),
int(c*100/self.map_resolution - gx1)]
# start = [int(start_x), int(start_y)]
start = pu.threshold_poses(start, grid.shape)
self.visited[gy1:gy2, gx1:gx2][start[0]-2:start[0]+3,
start[1]-2:start[1]+3] = 1
#Get traversible
def add_boundary(mat, value=1):
h, w = mat.shape
new_mat = np.zeros((h+2,w+2)) + value
new_mat[1:h+1,1:w+1] = mat
return new_mat
def delete_boundary(mat):
new_mat = copy.deepcopy(mat)
return new_mat[1:-1,1:-1]
[gx1, gx2, gy1, gy2] = planning_window
x1, y1, = 0, 0
x2, y2 = grid.shape
traversible = skimage.morphology.binary_dilation(
grid[y1:y2, x1:x2],
self.selem) != True
if not(traversible[start[0], start[1]]):
print("Not traversible, step is ", self.navigate_steps)
# obstacle dilation do not dilate collision
traversible = 1 - traversible
selem = skimage.morphology.disk(4)
traversible = skimage.morphology.binary_dilation(
traversible, selem) != True
traversible[int(start[0]-y1)-1:int(start[0]-y1)+2,
int(start[1]-x1)-1:int(start[1]-x1)+2] = 1
traversible = traversible * 1.
traversible[self.visited[gy1:gy2, gx1:gx2][y1:y2, x1:x2] == 1] = 1
traversible[self.collision_map[gy1:gy2, gx1:gx2][y1:y2, x1:x2] == 1] = 0
traversible = add_boundary(traversible)
return traversible, start, start_o
def _plan(self, traversible, goal_map, agent_pose, start, start_o, goal_found):
"""Function responsible for planning
Args:
planner_inputs (dict):
dict with following keys:
'map_pred' (ndarray): (M, M) map prediction
'goal' (ndarray): (M, M) goal locations
'pose_pred' (ndarray): (7,) array denoting pose (x,y,o)
and planning window (gx1, gx2, gy1, gy2)
'found_goal' (bool): whether the goal object is found
Returns:
action (int): action id
"""
# if newly_goal_set:
# self.action_5_count = 0
if self.prev_action == 1:
x1, y1, t1 = self.last_loc.cpu().numpy()
x2, y2, t2 = self.full_pose.cpu()
y1 = self.map_size_cm/100 - y1
y2 = self.map_size_cm/100 - y2
t1 = -t1
t2 = -t2
buf = 4
length = 5
if abs(x1 - x2)< 0.05 and abs(y1 - y2) < 0.05:
self.col_width += 1
self.col_width = min(self.col_width, 3)
else:
self.col_width = 1
# self.col_width = 4
dist = pu.get_l2_distance(x1, x2, y1, y2)
col_threshold = self.collision_threshold
if dist < col_threshold: # Collision
self.former_collide += 1
width = self.col_width
for i in range(length):
for j in range(width):
wx = x1 + 0.05*((i+buf) * np.cos(np.deg2rad(t1)) + \
(j-width//2) * np.sin(np.deg2rad(t1)))
wy = y1 + 0.05*((i+buf) * np.sin(np.deg2rad(t1)) - \
(j-width//2) * np.cos(np.deg2rad(t1)))
r, c = wy, wx
r, c = int(round(r*100/self.map_resolution)), \
int(round(c*100/self.map_resolution))
[r, c] = pu.threshold_poses([r, c],
self.collision_map.shape)
self.collision_map[r,c] = 1
else:
self.former_collide = 0
stg, stop, = self._get_stg(traversible, start, np.copy(goal_map), goal_found)
# Deterministic Local Policy
if stop:
action = 0
(stg_y, stg_x) = stg
else:
(stg_y, stg_x) = stg
angle_st_goal = math.degrees(math.atan2(stg_y - start[0],
stg_x - start[1]))
angle_agent = (start_o)%360.0
if angle_agent > 180:
angle_agent -= 360
relative_angle = (angle_st_goal- angle_agent)%360.0
if relative_angle > 180:
relative_angle -= 360
if self.former_collide < 10:
if relative_angle > 16:
action = 3 # Right
elif relative_angle < -16:
action = 2 # Left
else:
action = 1
elif self.prev_action == 1:
if relative_angle > 0:
action = 3 # Right
else:
action = 2 # Left
else:
action = 1
if self.former_collide >= 10 and self.prev_action != 1:
self.former_collide = 0
if stg_y == start[0] and stg_x == start[1]:
action = 1
return stg_y, stg_x, action
def _get_stg(self, traversible, start, goal, goal_found):
def add_boundary(mat, value=1):
h, w = mat.shape
new_mat = np.zeros((h+2,w+2)) + value
new_mat[1:h+1,1:w+1] = mat
return new_mat
def delete_boundary(mat):
new_mat = copy.deepcopy(mat)
return new_mat[1:-1,1:-1]
goal = add_boundary(goal, value=0)
original_goal = copy.deepcopy(goal)
centers = []
if len(np.where(goal !=0)[0]) > 1:
goal, centers = CH._get_center_goal(goal)
state = [start[0] + 1, start[1] + 1]
self.planner = FMMPlanner(traversible, None)
if self.dilation_deg!=0:
#if self.args.debug_local:
# self.print_log("dilation added")
goal = CH._add_cross_dilation(goal, self.dilation_deg, 3)
if goal_found:
# if self.args.debug_local:
# self.print_log("goal found!")
try:
goal = CH._block_goal(centers, goal, original_goal, goal_found)
except:
goal = self.set_random_goal(goal)
self.planner.set_multi_goal(goal, state) # time cosuming
decrease_stop_cond =0
if self.dilation_deg >= 6:
decrease_stop_cond = 0.2 #decrease to 0.2 (7 grids until closest goal)
stg_y, stg_x, replan, stop = self.planner.get_short_term_goal(state, found_goal = goal_found, decrease_stop_cond=decrease_stop_cond)
stg_x, stg_y = stg_x - 1, stg_y - 1
if stop:
a = 1
# self.closest_goal = CH._get_closest_goal(start, goal)
return (stg_y, stg_x), stop
def set_random_goal(self):
"""
return a random goal in the map
"""
obstacle_map = self.full_map.cpu().numpy()[0,0,::-1]
goal = np.zeros_like(obstacle_map)
goal_index = np.where((obstacle_map<1))
np.random.seed(self.total_steps)
if len(goal_index[0]) != 0:
i = np.random.choice(len(goal_index[0]), 1)[0]
h_goal = goal_index[0][i]
w_goal = goal_index[1][i]
else:
h_goal = np.random.choice(goal.shape[0], 1)[0]
w_goal = np.random.choice(goal.shape[1], 1)[0]
goal[h_goal, w_goal] = 1
return goal
def update_metrics(self, metrics):
self.metrics['distance_to_goal'] = metrics['distance_to_goal']
self.metrics['spl'] = metrics['spl']
self.metrics['softspl'] = metrics['softspl']
if self.args.visualize:
if self.simulator._env.episode_over or self.total_steps == 500:
self.save_video()
def visualize(self, traversible, observations, number_action):
if self.args.visualize:
save_map = copy.deepcopy(torch.from_numpy(traversible))
gray_map = torch.stack((save_map, save_map, save_map))
paper_obstacle_map = copy.deepcopy(gray_map)[:,1:-1,1:-1]
paper_map = torch.zeros_like(paper_obstacle_map)
paper_map_trans = paper_map.permute(1,2,0)
unknown_rgb = colors.to_rgb('#FFFFFF')
paper_map_trans[:,:,:] = torch.tensor( unknown_rgb)
free_rgb = colors.to_rgb('#E7E7E7')
paper_map_trans[self.fbe_free_map.cpu().numpy()[0,0,::-1]>0.5,:] = torch.tensor( free_rgb).double()
obstacle_rgb = colors.to_rgb('#A2A2A2')
paper_map_trans[skimage.morphology.binary_dilation(self.full_map.cpu().numpy()[0,0,::-1]>0.5,skimage.morphology.disk(1)),:] = torch.tensor(obstacle_rgb).double()
paper_map_trans = paper_map_trans.permute(2,0,1)
self.visualize_agent_and_goal(paper_map_trans)
agent_coordinate = (int(self.history_pose[-1][0]*100/self.resolution), int((self.map_size_cm/100-self.history_pose[-1][1])*100/self.resolution))
occupancy_map = crop_around_point((paper_map_trans.permute(1, 2, 0) * 255).numpy().astype(np.uint8), agent_coordinate, (150, 200))
visualize_image = np.full((450, 800, 3), 255, dtype=np.uint8)
visualize_image = add_resized_image(visualize_image, observations["rgb_annotated"], (10, 60), (320, 240))
visualize_image = add_resized_image(visualize_image, occupancy_map, (340, 60), (180, 240))
visualize_image = add_rectangle(visualize_image, (10, 60), (330, 300), (128, 128, 128), thickness=1)
visualize_image = add_rectangle(visualize_image, (340, 60), (520, 300), (128, 128, 128), thickness=1)
visualize_image = add_rectangle(visualize_image, (540, 60), (790, 165), (128, 128, 128), thickness=1)
visualize_image = add_rectangle(visualize_image, (540, 195), (790, 300), (128, 128, 128), thickness=1)
visualize_image = add_rectangle(visualize_image, (10, 350), (790, 400), (128, 128, 128), thickness=1)
visualize_image = add_text(visualize_image, "Observation (Goal: Chair)", (70, 50), font_scale=0.5, thickness=1)
visualize_image = add_text(visualize_image, "Occupancy Map", (370, 50), font_scale=0.5, thickness=1)
visualize_image = add_text(visualize_image, "Scene Graph Nodes", (580, 50), font_scale=0.5, thickness=1)
visualize_image = add_text(visualize_image, "Scene Graph Edges", (580, 185), font_scale=0.5, thickness=1)
visualize_image = add_text(visualize_image, "LLM Explanation", (330, 340), font_scale=0.5, thickness=1)
visualize_image = add_text_list(visualize_image, line_list(self.text_node, 40), (550, 80), font_scale=0.3, thickness=1)
visualize_image = add_text_list(visualize_image, line_list(self.text_edge, 40), (550, 215), font_scale=0.3, thickness=1)
visualize_image = add_text_list(visualize_image, line_list(self.explanation, 150), (20, 370), font_scale=0.3, thickness=1)
visualize_image = visualize_image[:, :, ::-1]
self.visualize_image_list.append(visualize_image)
def save_video(self):
save_video_dir = os.path.join(self.visualization_dir, 'video')
save_video_path = f'{save_video_dir}/vid_{self.count_episodes:06d}.mp4'
if not os.path.exists(save_video_dir):
os.makedirs(save_video_dir)
height, width, layers = self.visualize_image_list[0].shape
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
video = cv2.VideoWriter(save_video_path, fourcc, 4.0, (width, height))
for visualize_image in self.visualize_image_list:
video.write(visualize_image)
video.release()
def visualize_agent_and_goal(self, map):
for idx, pose in enumerate(self.history_pose):
draw_step_num = 30
alpha = max(0, 1 - (len(self.history_pose) - idx) / draw_step_num)
agent_size = 1
if idx == len(self.history_pose) - 1:
agent_size = 2
draw_agent(agent=self, map=map, pose=pose, agent_size=agent_size, color_index=0, alpha=alpha)
draw_goal(agent=self, map=map, goal_size=2, color_index=1)
return map
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
"--evaluation", default="local", type=str, choices=["local", "remote"]
)
parser.add_argument(
"--PSL_infer", default="one_hot", type=str, choices=["optim", "one_hot"]
)
parser.add_argument(