def test_import_box(): s = Simulator(mode='headless') scene = StadiumScene() s.import_scene(scene) print(s.objects) # wall = [pos, dim] wall = [[[0, 7, 1.01], [10, 0.2, 1]], [[0, -7, 1.01], [6.89, 0.1, 1]], [[7, -1.5, 1.01], [0.1, 5.5, 1]], [[-7, -1, 1.01], [0.1, 6, 1]], [[-8.55, 5, 1.01], [1.44, 0.1, 1]], [[8.55, 4, 1.01], [1.44, 0.1, 1]]] obstacles = [[[-0.5, 2, 1.01], [3.5, 0.1, 1]], [[4.5, -1, 1.01], [1.5, 0.1, 1]], [[-4, -2, 1.01], [0.1, 2, 1]], [[2.5, -4, 1.01], [1.5, 0.1, 1]]] for i in range(len(wall)): curr = wall[i] obj = BoxShape(curr[0], curr[1]) s.import_object(obj) for i in range(len(obstacles)): curr = obstacles[i] obj = BoxShape(curr[0], curr[1]) s.import_object(obj) config = parse_config(os.path.join(gibson2.root_path, '../test/test.yaml')) turtlebot1 = Turtlebot(config) turtlebot2 = Turtlebot(config) s.import_robot(turtlebot1) s.import_robot(turtlebot2) turtlebot1.set_position([6., -6., 0.]) turtlebot2.set_position([-3., 4., 0.]) for i in range(100): s.step() s.disconnect()
def test_turtlebot_position(): s = Simulator(mode='headless') scene = StadiumScene() s.import_scene(scene) turtlebot = Turtlebot(config) s.import_robot(turtlebot) turtlebot.set_position([0, 0, 5]) nbody = p.getNumBodies() pos = turtlebot.get_position() s.disconnect() assert nbody == 5 assert np.allclose(pos, np.array([0, 0, 5]))
def test_import_building_viewing(): s = Simulator(mode='headless') scene = BuildingScene('Ohoopee') s.import_scene(scene) assert s.objects == list(range(2)) turtlebot1 = Turtlebot(config) turtlebot2 = Turtlebot(config) turtlebot3 = Turtlebot(config) s.import_robot(turtlebot1) s.import_robot(turtlebot2) s.import_robot(turtlebot3) turtlebot1.set_position([0.5, 0, 0.5]) turtlebot2.set_position([0, 0, 0.5]) turtlebot3.set_position([-0.5, 0, 0.5]) for i in range(10): s.step() #turtlebot1.apply_action(np.random.randint(4)) #turtlebot2.apply_action(np.random.randint(4)) #turtlebot3.apply_action(np.random.randint(4)) s.disconnect()
def show_action_sensor_space(): s = Simulator(mode='headless') scene = StadiumScene() s.import_scene(scene) turtlebot = Turtlebot(config) s.import_robot(turtlebot) turtlebot.set_position([0, 1, 0.5]) ant = Ant(config) s.import_robot(ant) ant.set_position([0, 2, 0.5]) h = Humanoid(config) s.import_robot(h) h.set_position([0, 3, 2]) jr = JR2(config) s.import_robot(jr) jr.set_position([0, 4, 0.5]) jr2 = JR2_Kinova(config) s.import_robot(jr2) jr2.set_position([0, 5, 0.5]) husky = Husky(config) s.import_robot(husky) husky.set_position([0, 6, 0.5]) quad = Quadrotor(config) s.import_robot(quad) quad.set_position([0, 7, 0.5]) for robot in s.robots: print(type(robot), len(robot.ordered_joints), robot.calc_state().shape) for i in range(100): s.step() s.disconnect()
def test_multiagent(): s = Simulator(mode='headless') scene = StadiumScene() s.import_scene(scene) turtlebot1 = Turtlebot(config) turtlebot2 = Turtlebot(config) turtlebot3 = Turtlebot(config) s.import_robot(turtlebot1) s.import_robot(turtlebot2) s.import_robot(turtlebot3) turtlebot1.set_position([1, 0, 0.5]) turtlebot2.set_position([0, 0, 0.5]) turtlebot3.set_position([-1, 0, 0.5]) nbody = p.getNumBodies() for i in range(100): #turtlebot1.apply_action(1) #turtlebot2.apply_action(1) #turtlebot3.apply_action(1) s.step() s.disconnect() assert nbody == 7
def rollout(traj, headless=False): SCENE, traj_id, instr_id, instr, starting_coords = traj # instr = "Keep going and don't stop" # starting_coords = [0, 0, 0] seq = tok.encode_sentence(instr) tokens = tok.split_sentence(instr) seq_lengths = [np.argmax(seq == padding_idx, axis=0)] seq = torch.from_numpy(np.expand_dims(seq, 0)).cuda() # seq_lengths[seq_lengths == 0] = seq.shape[1] # Full length ctx, h_t, c_t, ctx_mask = encoder(seq, seq_lengths) question = h_t pre_feat = torch.zeros(batch_size, opts.img_feat_input_dim).cuda() pre_ctx_attend = torch.zeros(batch_size, opts.rnn_hidden_size).cuda() # Gibson stuff # 72 fov for 600, 60 for 480 # mode = gui for debug, headless for run s = Simulator(mode='gui', resolution=640, fov=75, panorama=True) scene = BuildingScene(SCENE) # scene = StadiumScene() ids = s.import_scene(scene) robot = Turtlebot(config) ped_id = s.import_robot(robot) heading_feat_tensor = torch.Tensor(heading_elevation_feat()).view([im_per_ob, 128]).cuda() s.step() robot.set_position(starting_coords) def apply_action(bot: robot, action_idx: int, depth_ok: list, headless=False) -> bool: print(action_idx) # action_idx is expected to be 0-13, TODO: make nicer... if action_idx == 0 or action_idx > 12 or not depth_ok[action_idx - 1]: print("STOP") return True action_idx -= 1 #if action_idx < 3 or (action_idx < 12 and action_idx > 9): bot.turn_right(0.5235988 * action_idx) s.step() if(not headless): time.sleep(0.2) bot.move_forward(0.5) return False # else: # if action_idx < 7: # bot.turn_left(1.57) # else: # bot.turn_right(1.57) bot_is_running = True while bot_is_running: s.step() gib_out = s.renderer.render_robot_cameras(modes=('rgb', '3d')) rgb = gib_out[::2] depth = np.array(gib_out[1::2]) processed_rgb = list(map(transform_img, rgb)) batch_obs = np.concatenate(processed_rgb) imgnet_input = torch.Tensor(batch_obs).cuda() imgnet_output = torch.zeros([im_per_ob, 2048]).cuda() # depth processing and filtering # depth: [36, ] depth *= depth depth = depth[:, :, :, :3].sum(axis=3) depth = np.sqrt(depth) # filter out 0 distances that are presumably from infinity dist depth[depth < 0.0001] = 10 # TODO: generalize to non-horizontal moves depth_ok = depth[12:24, 200:440, 160:480].min(axis=2).min(axis=1) fig=plt.figure(figsize=(8, 2)) for n, i in enumerate([0, 3, 6, 9]): fig.add_subplot(1, 4, n + 1) plt.imshow(depth[12 + i]) plt.show() # depth_ok *= depth_ok > 1 print(depth_ok) depth_ok = depth_ok > 0.8 print(depth_ok) # Each observation has 36 inputs # We pass rgb images through frozen embedder for i in range(im_per_ob // B_S): def hook_fn(m, last_input, o): imgnet_output[i*B_S:(i+1)*B_S, :] = \ o.detach().squeeze(2).squeeze(2) imgnet_input[B_S * i : B_S * (i + 1)] # imgnet_output[B_S * i : B_S * (i + 1)] = resnet(minibatch).detach() imgnet_output = torch.cat([imgnet_output, heading_feat_tensor], 1) pano_img_feat = imgnet_output.view([1, im_per_ob, 2176]) navigable_feat = torch.zeros([1, 16, 2176]).cuda() navigable_feat[0, 1:13] = imgnet_output[12:24] * torch.Tensor(depth_ok).cuda().view(12, 1) # TODO: make nicer as stated above navigable_index = [list(map(int, depth_ok))] print(navigable_index) # NB: depth_ok replaces navigable_index h_t, c_t, pre_ctx_attend, img_attn, ctx_attn, logit, value, navigable_mask = model( pano_img_feat, navigable_feat, pre_feat, question, h_t, c_t, ctx, pre_ctx_attend, navigable_index, ctx_mask) print("ATTN") print(ctx_attn[0]) print(img_attn[0]) plt.bar(range(len(tokens)), ctx_attn.detach().cpu()[0][:len(tokens)]) plt.xticks(range(len(tokens)), tokens) plt.show() plt.bar(range(16), img_attn.detach().cpu()[0]) plt.show() print("NMASK") print(navigable_mask) logit.data.masked_fill_((navigable_mask == 0).data, -float('inf')) m = torch.Tensor([[False] + list(map(lambda b: not b, navigable_index[0])) + [False, False, False]], dtype=bool).cuda() logit.data.masked_fill_(m, -float('inf')) action = _select_action(logit, [False]) ended = apply_action(robot, action[0], depth_ok) bot_is_running = not ended or not headless if not headless: time.sleep(.3)