コード例 #1
0
ファイル: data_mr.py プロジェクト: RAIVNLab/Visual_Reaction
 def restart(self):
     ### reset the unity to avoid some latency issue ...
     if isinstance(self.controller, type(None)):
         self.controller = Controller(local_executable_path=PATH,
                                      scene="FloorPlan201_physics",
                                      x_display=self.x_display,
                                      agentMode='drone',
                                      fieldOfView=60,
                                      port=self.port)
     else:
         self.controller.stop()
         self.controller = Controller(local_executable_path=PATH,
                                      scene="FloorPlan201_physics",
                                      x_display=self.x_display,
                                      agentMode='drone',
                                      fieldOfView=60,
                                      port=self.port)
     _ = self.controller.reset(self.trajectory['scene'][0])
     _ = self.controller.step(
         dict(action='ChangeAutoResetTimeScale',
              timeScale=self.cfg.thor.time_scale))
     _ = self.controller.step(
         dict(action='ChangeFixedDeltaTime',
              fixedDeltaTime=self.cfg.thor.delta_time))
     print('Thread:' + str(self.pid) + ' [' + str(self.iters + 1) +
           ']: restart finish')
コード例 #2
0
def get_all_objtype_in_room(room_type: str):
    '''
    Get all Object Types in a certern room type: kitchen, living_room, bedroom, bathroom
    '''
    if room_type == "kitchen":
        room_start_index = 0
    elif room_type == "living_room":
        room_start_index = 200
    elif room_type == "bedroom":
        room_start_index = 300
    else:
        room_start_index = 400

    all_obj_type = []

    controller = Controller(scene="FloorPlan1",
                            renderInstanceSegmentation=True,
                            width=1080,
                            height=1080)

    for i in range(1, 31):
        controller.reset(scene="FloorPlan" + str(room_start_index + i))
        event = controller.step("Done")
        all_obj = get_all_objtype_in_event(event)
        for objtype in all_obj:
            if objtype not in all_obj_type:
                all_obj_type.append(objtype)

    controller.stop()
    return all_obj_type
コード例 #3
0
ファイル: Environment.py プロジェクト: engNagi/Master_Project
    def __init__(self,
                 fov=60.0,
                 camera_Y=0.675,
                 grid_size=0.25,
                 visibility_distance=1.5,
                 player_screen_width=300,
                 player_screen_height=300,
                 full_scrn=False,
                 depth_image=False,
                 class_image=False,
                 top_view_cam=False,
                 object_image=False,
                 third_party_cam=False,
                 scene="FloorPlan220",
                 object_name="Television"):

        self.scene = scene
        self.grid_size = grid_size
        self.depth_image = depth_image
        self.class_image = class_image
        self.object_image = object_image
        self.visibility_distance = visibility_distance
        self.camera_Y = camera_Y
        self.fov = fov
        self.object_name = object_name
        self.player_screen_width = player_screen_width
        self.player_screen_height = player_screen_height
        self.top_view_cam = top_view_cam
        self.third_party_cam = third_party_cam
        self.full_scrn = full_scrn

        self.ctrl = Controller()
コード例 #4
0
    def run_episodes(self):
        self.ep_idx = 0
        # self.objects = []

        for episode in range(self.num_episodes):
            print("STARTING EPISODE ", episode)

            mapname = self.mapnames[episode]
            print("MAPNAME=", mapname)

            self.controller = Controller(
                scene=mapname,
                gridSize=0.25,
                width=self.W,
                height=self.H,
                fieldOfView=self.fov,
                renderObjectImage=True,
                renderDepthImage=True,
            )

            self.basepath = self.homepath + f"/{mapname}_{episode}"
            print("BASEPATH: ", self.basepath)

            # self.basepath = f"/hdd/ayushj/habitat_data/{mapname}_{episode}"
            if not os.path.exists(self.basepath):
                os.mkdir(self.basepath)

            self.run()

            self.controller.stop()
            time.sleep(1)

            self.ep_idx += 1
コード例 #5
0
    def __init__(self, **kwargs):
        self.config = dict(
            rotateStepDegrees=30.0,
            visibilityDistance=1.0,
            gridSize=0.25,
            agentType="stochastic",
            continuousMode=True,
            snapToGrid=False,
            agentMode="bot",
            width=640,
            height=480,
        )
        recursive_update(self.config, {**kwargs, "agentMode": "bot"})
        self.controller = Controller(**self.config)
        self.known_good_locations: Dict[str, Any] = {
            self.scene_name: copy.deepcopy(self.currently_reachable_points)
        }
        assert len(self.known_good_locations[self.scene_name]) > 10

        # onames = [o['objectId'] for o in self.last_event.metadata['objects']]
        # removed = []
        # for oname in onames:
        #     if 'Painting' in oname:
        #         self.controller.step("RemoveFromScene", objectId=oname)
        #         removed.append(oname)
        # get_logger().info("Removed {} Paintings from {}".format(len(removed), self.scene_name))

        # get_logger().warning("init to scene {} in pos {}".format(self.scene_name, self.agent_state()))
        # npoints = len(self.currently_reachable_points)
        # assert npoints > 100, "only {} reachable points after init".format(npoints)
        self.grids: Dict[str, Tuple[Dict[str, np.array], int, int, int,
                                    int]] = {}
        self.initialize_grid()
コード例 #6
0
    def __init__(self, **kwargs):
        self.config = dict(
            rotateStepDegrees=30.0,
            visibilityDistance=1.0,
            gridSize=0.25,
            agentType="stochastic",
            continuousMode=True,
            snapToGrid=False,
            agentMode="bot",
            width=640,
            height=480,
            agentCount=1,
        )

        if "agentCount" in kwargs:
            assert kwargs["agentCount"] > 0

        recursive_update(self.config, {**kwargs, "agentMode": "bot"})
        self.controller = Controller(
            **self.config, server_class=ai2thor.fifo_server.FifoServer)
        self.known_good_locations: Dict[str, Any] = {
            self.scene_name: copy.deepcopy(self.currently_reachable_points)
        }
        self.distance_cache = DynamicDistanceCache(rounding=1)
        assert len(self.known_good_locations[self.scene_name]) > 10

        self.agent_count = self.config["agentCount"]
コード例 #7
0
    def __init__(self, all_metadata_available: bool = True, **kwargs):
        self.config = dict(
            rotateStepDegrees=30.0,
            visibilityDistance=1.0,
            gridSize=0.25,
            agentType="stochastic",
            continuousMode=True,
            snapToGrid=False,
            agentMode="locobot",
            width=640,
            height=480,
            agentCount=1,
            server_class=FifoServer,
        )

        if "agentCount" in kwargs:
            assert kwargs["agentCount"] > 0

        recursive_update(self.config, {**kwargs, "agentMode": "locobot"})
        self.controller = Controller(**self.config, )
        self.all_metadata_available = all_metadata_available

        self.scene_to_reachable_positions: Optional[Dict[str, Any]] = None
        self.distance_cache: Optional[DynamicDistanceCache] = None

        if self.all_metadata_available:
            self.scene_to_reachable_positions = {
                self.scene_name: copy.deepcopy(self.currently_reachable_points)
            }
            assert len(self.scene_to_reachable_positions[self.scene_name]) > 10

            self.distance_cache = DynamicDistanceCache(rounding=1)

        self.agent_count = self.config["agentCount"]
コード例 #8
0
    def run_val(self, mapnames, BATCH_SIZE):

        batch = {"rewards": [], "obs": [], "actions": []}
        episode_rewards = 0.0
        episode_steps = []

        iter_idx = 0
        while True:

            mapname = np.random.choice(mapnames)

            # self.basepath = self.homepath + f"/{mapname}_{episode}"
            # print("BASEPATH: ", self.basepath)

            # # self.basepath = f"/hdd/ayushj/habitat_data/{mapname}_{episode}"
            # if not os.path.exists(self.basepath):
            #     os.mkdir(self.basepath)

            self.controller = Controller(
                scene=mapname,
                gridSize=0.25,
                width=self.W,
                height=self.H,
                fieldOfView=self.fov,
                renderObjectImage=True,
                renderDepthImage=True,
            )

            episode_rewards, obs, actions = self.run()

            print("Total reward for val batch # ", iter_idx, " :",
                  episode_rewards)

            self.controller.stop()
            time.sleep(1)

            if episode_rewards is None:
                print("NO EPISODE REWARDS.. SKIPPING BATCH INSTANCE")
                continue

            batch["rewards"].append(episode_rewards)
            batch["obs"].append(obs)
            batch["actions"].append(actions)

            train, labels, mean_rewards, boudary = self.elite_batch(
                batch, self.percentile)

            with torch.no_grad():
                scores = self.cemnet(train, softmax=False)

            loss_value = self.loss(scores, labels)

            iter_idx += 1

            if len(batch["rewards"]) == BATCH_SIZE:

                break

        return loss_value, mean_rewards
コード例 #9
0
 def create_controller():
     return Controller(
         x_display=self.x_display,
         player_screen_width=self._start_player_screen_width,
         player_screen_height=self._start_player_screen_height,
         local_executable_path=self._local_thor_build,
         quality=self._quality,
     )
コード例 #10
0
ファイル: test_unity.py プロジェクト: ZZAndyZZ/ai2thor
def build_controller(**args):
    default_args = dict(scene="FloorPlan28", local_build=True)
    default_args.update(args)
    # during a ci-build we will get a warning that we are using a commit_id for the
    # build instead of 'local'
    with warnings.catch_warnings():
        warnings.simplefilter("ignore")
        c = Controller(**default_args)
    return c
コード例 #11
0
 def run_load(dataset_path, memory, conn):
     init_logging()
     controller = Controller(
         x_display='0.%d' % global_config.actor_gpu,
         visibilityDistance=actor_config.visibilityDistance,
         renderDepthImage=global_config.depth)
     dataset = ActiveDataset(dataset_path, memory, controller, conn)
     dataset.process()
     controller.stop()
コード例 #12
0
    def make_controller(self):
        if self.controller is None:
            self.controller = Controller()

            self.controller.step({"action": "ChangeQuality", "quality": "Very High"})
            self.controller.step(
                {
                    "action": "ChangeResolution",
                    "x": self.viz_rows_cols[1],
                    "y": self.viz_rows_cols[0],
                }
            )
コード例 #13
0
    def batch_iteration(self, mapnames, NN, BATCH_SIZE):

        batch = {"rewards": [], "obs": [], "actions": []}
        episode_rewards = 0.0
        iter_idx = 0
        while True:

            mapname = np.random.choice(mapnames)

            # self.basepath = self.homepath + f"/{mapname}_{episode}"
            # print("BASEPATH: ", self.basepath)

            # # self.basepath = f"/hdd/ayushj/habitat_data/{mapname}_{episode}"
            # if not os.path.exists(self.basepath):
            #     os.mkdir(self.basepath)

            self.controller = Controller(
                scene=mapname,
                gridSize=0.25,
                width=self.W,
                height=self.H,
                fieldOfView=self.fov,
                renderObjectImage=True,
                renderDepthImage=True,
            )

            episode_rewards, obs, actions = self.run()

            print("Total reward for train batch # ", iter_idx, " :",
                  episode_rewards)

            self.controller.stop()
            time.sleep(1)

            if episode_rewards is None:
                print("NO EPISODE REWARDS.. SKIPPING BATCH INSTANCE")
                continue

            batch["rewards"].append(episode_rewards)
            batch["obs"].append(obs)
            batch["actions"].append(actions)

            iter_idx += 1
            print(len(batch["rewards"]))
            if len(batch["rewards"]) == BATCH_SIZE:

                yield batch
                iter_idx = 0
                batch = {"rewards": [], "obs": [], "actions": []}

            if len(batch["rewards"]) > BATCH_SIZE:
                st()
コード例 #14
0
    def __init__(self,env_name,timelimit,stack_frame=1,color=True,image_size=128,render_all=True):
        assert env_name in ENV_LIST
        self.env_name = env_name
        self.timelimit = timelimit
        if render_all == True:
            self.controller = Controller(scene=self.env_name, agentMode='bot',gridSize=0.25,rotateStepDegrees=10,renderDepthImage=True,renderObjectImage=True,renderClassImage=True)
        else:
            self.controller = Controller(scene=self.env_name, agentMode='bot',gridSize=0.25,rotateStepDegrees=10)
        
        self.time = 0
        self.score = 0

        self.color = color
        self.stack_frame = stack_frame
        self.image_size =image_size
        self.render_all = render_all
        self.action_space = Discrete(5)
        if color==True:
            self.observation_space = Box(-1,1,(image_size,image_size,3*stack_frame,))
            self.channel = 3
        else:
            self.observation_space = Box(-1,1,(image_size,image_size,1*stack_frame,))
            self.channel = 1
        self.state = np.zeros(self.observation_space.shape,dtype=np.float32)
コード例 #15
0
ファイル: environment.py プロジェクト: annie-r/cse573
    def __init__(self,
                 grid_size=0.25,
                 fov=90.0,
                 local_executable_path=None,
                 randomize_objects=False,
                 seed=1):

        random.seed(seed)

        self.controller = Controller()
        if local_executable_path:
            self.controller.local_executable_path = local_executable_path
        self.grid_size = grid_size
        self._reachable_points = {}
        self.fov = fov
        self.offline_data_dir = './datasets/floorplans'
        self.y = None
        self.randomize_objects = randomize_objects
        self.stored_scene_name = None
コード例 #16
0
    def __init__(self, all_metadata_available: bool = True, **kwargs):
        self.config = dict(
            rotateStepDegrees=30.0,
            visibilityDistance=1.0,
            gridSize=0.25,
            continuousMode=True,
            snapToGrid=False,
            agentMode="locobot",
            width=640,
            height=480,
            agentCount=1,
            server_class=FifoServer,
        )

        if "agentCount" in kwargs:
            assert kwargs["agentCount"] > 0

        kwargs["agentMode"] = kwargs.get("agentMode", "locobot")
        if kwargs["agentMode"] not in ["bot", "locobot"]:
            warnings.warn(f"The RoboTHOR environment has not been tested using"
                          f" an agent of mode '{kwargs['agentMode']}'.")

        recursive_update(self.config, kwargs)
        self.controller = Controller(**self.config, )

        self.all_metadata_available = all_metadata_available

        self.scene_to_reachable_positions: Optional[Dict[str, Any]] = None
        self.distance_cache: Optional[DynamicDistanceCache] = None

        if self.all_metadata_available:
            self.scene_to_reachable_positions = {
                self.scene_name: copy.deepcopy(self.currently_reachable_points)
            }
            assert len(self.scene_to_reachable_positions[self.scene_name]) > 10

            self.distance_cache = DynamicDistanceCache(rounding=1)

        self.agent_count = self.config["agentCount"]

        self._extra_teleport_kwargs: Dict[str, Any] = {
        }  # Used for backwards compatability with the teleport action
コード例 #17
0
def main(args):
    c = Controller()
    c.start()
    sence = args.sence
    c.reset("FloorPlan" + str(sence))

    t = get_agent_map_data(c)
    new_frame = add_agent_view_triangle(
        position_to_tuple(c.last_event.metadata["agent"]["position"]),
        c.last_event.metadata["agent"]["rotation"]["y"],
        t["frame"],
        t["pos_translator"],
    )
    plt.imshow(new_frame)
    plt.axis('off')
    plt.savefig('topview.png')
    plt.show()
    im = Image.open('topview.png')
    im = trim(im)
    im.save('topview.png')
コード例 #18
0
    def start(
        self,
        scene_name: Optional[str],
        move_mag: float = 0.25,
        **kwargs,
    ) -> None:
        """Starts the ai2thor controller if it was previously stopped.

        After starting, `reset` will be called with the scene name and move magnitude.

        # Parameters

        scene_name : The scene to load.
        move_mag : The amount of distance the agent moves in a single `MoveAhead` step.
        kwargs : additional kwargs, passed to reset.
        """
        if self._started:
            raise RuntimeError(
                "Trying to start the environment but it is already started.")

        self.controller = Controller(
            x_display=self.x_display,
            width=self._start_player_screen_width,
            height=self._start_player_screen_height,
            local_executable_path=self._local_thor_build,
            quality=self._quality,
            server_class=ai2thor.fifo_server.FifoServer,
        )

        if (
                self._start_player_screen_height,
                self._start_player_screen_width,
        ) != self.current_frame.shape[:2]:
            self.controller.step({
                "action": "ChangeResolution",
                "x": self._start_player_screen_width,
                "y": self._start_player_screen_height,
            })

        self._started = True
        self.reset(scene_name=scene_name, move_mag=move_mag, **kwargs)
コード例 #19
0
ファイル: robothor_viz.py プロジェクト: wayne9qiu/allenact
    def make_top_down_views(self) -> Dict[str, np.ndarray]:
        top_downs = {}
        for roomname in self.iterate_scenes(self.scenes):
            fname = self.cached_image_path(roomname)
            if not os.path.exists(fname):
                if self.controller is None:
                    self.controller = Controller()
                    self.controller.step({
                        "action": "ChangeQuality",
                        "quality": "Very High"
                    })
                    self.controller.step({
                        "action": "ChangeResolution",
                        "x": self.viz_rows_cols[1],
                        "y": self.viz_rows_cols[0],
                    })

                self.dump_top_down_view(roomname, fname)
            top_downs[roomname] = cv2.imread(fname)

        return top_downs
コード例 #20
0
    def __init__(self,
                 fov=60.0,
                 action_n=6,
                 camera_Y=0.675,
                 grid_size=0.20,
                 visibility_distance=1.5,
                 player_screen_width=300,
                 player_screen_height=300,
                 full_scrn=False,
                 depth_image=False,
                 class_image=False,
                 top_view_cam=False,
                 object_image=False,
                 third_party_cam=False,
                 random_init=False,
                 random_goals=False,
                 scene="FloorPlan220",
                 object_name="Television"):

        # self.scene = scene
        self.grid_size = grid_size
        self.depth_image = depth_image
        self.class_image = class_image
        self.object_image = object_image
        self.visibility_distance = visibility_distance
        self.camera_Y = camera_Y
        self.fov = fov
        self.scene = scene
        self.object_name = object_name
        self.player_screen_width = player_screen_width
        self.player_screen_height = player_screen_height
        self.top_view_cam = top_view_cam
        self.third_party_cam = third_party_cam
        self.full_scrn = full_scrn
        self.random_init = random_init
        self.orientations = [0.0, 90.0, 180.0, 270.0, 360.0]
        self.action_n = action_n
        self.random_goal = random_goals

        self.ctrl = Controller()  # headless=True
コード例 #21
0
    def __init__(self,
                 offline_root='',
                 scene='FloorPlan_Train1_1',
                 verbose=False):
        self.verbose = verbose
        self.online_controller = Controller(width=640,
                                            height=480,
                                            rotateStepDegrees=30,
                                            applyActionNoise=False,
                                            gridSize=0.125,
                                            snapToGrid=False,
                                            agentMode='bot')

        # self.online_controller = ai2thor.controller.Controller(
        #     start_unity=True,
        #     width=640,
        #     height=480,
        #     agentMode='bot',
        #     gridSize=0.125,
        #     rotateStepDegrees=30,
        #     applyActionNoise=False,
        #     snapToGrid=False)
        self.scene = scene
        if offline_root != '':
            self.offline_controller = OfflineControllerWithSmallRotation(
                grid_size=0.125,
                fov=90,
                offline_data_dir=offline_root,
                visualize=False,
                rotate_by=30,
                state_decimal=3)
        else:
            self.offline_controller = None

        if scene != '':
            self.online_controller.reset(scene)
            if self.offline_controller is not None:
                self.offline_controller.reset(scene)
コード例 #22
0
    def __init__(self, pipe, gpu, configs, ground_truth=0, run=True):
        self.pipe = pipe
        self.gc, self.ac = configs
        self.ground_truth = ground_truth
        self.directions = [
            dict(x=a, y=b, z=c) for a in [-1, 1] for b in [1, 2]
            for c in [-1, 1]
        ]
        self.controller = Controller(
            x_display='0.%d' % gpu,
            visibilityDistance=self.ac.visibilityDistance,
            renderDepthImage=self.gc.depth or ground_truth > 0,
            renderClassImage=ground_truth > 0,
            renderObjectImage=ground_truth > 0)
        self.grid = [(x, y) for x in range(self.gc.grid_size)
                     for y in range(self.gc.grid_size)]

        self.depth_correction = self._make_depth_correction(
            self.gc.resolution, self.gc.resolution, 90)

        self.kernel_size = (self.ac.check_change_kernel.shape[0] - 1) // 2

        if run:
            self.run()
コード例 #23
0
ファイル: robothor_viz.py プロジェクト: wayne9qiu/allenact
    def get_translator(self) -> Dict[str, Any]:
        roomname = list(ThorViz.iterate_scenes(self.scenes))[0]
        json_file = self.cached_map_data_path(roomname)
        if not os.path.exists(json_file):
            if self.controller is None:
                self.controller = Controller()
            self.controller.reset(roomname)
            map_data = self.get_agent_map_data()
            get_logger().info("Dumping {}".format(json_file))
            with open(json_file, "w") as f:
                json.dump(map_data, f, indent=4, sort_keys=True)
        else:
            with open(json_file, "r") as f:
                map_data = json.load(f)

        pos_translator = ThorPositionTo2DFrameTranslator(
            self.viz_rows_cols,
            self.position_to_tuple(map_data["cam_position"]),
            map_data["cam_orth_size"],
        )
        map_data["pos_translator"] = pos_translator

        get_logger().debug("Using map_data {}".format(map_data))
        return map_data
コード例 #24
0
    def __init__(self, scene, target, room_object_types = G_livingroom_objtype):
        # ai2thor
        self.scene = scene
        self.target = target
        assert self.target in room_object_types
        self.room_object_types = room_object_types
        self.controller = Controller(scene=scene, 
                        renderInstanceSegmentation=True,
                        width=1080,
                        height=1080)
        self.target_type = target

        # register query
        FrameInfo.candidates = self.room_object_types
        self.query_indicates = [True for _ in range(len(FrameInfo.candidates))]

        # Keep track of qa history 
        self.keep_frame_map = False # weather to keep history to avoid duplicated query
        self.history = deque(maxlen = 1000)
        # self.frame_map = {} # (position and rotation) -> FrameInfo

        # RL part
        self.observation = None
        self.last_action = None

        self.episode_done = False

        # reward
        self.time_penalty = -0.01
        self.action_fail_penalty = -0.01

        self.first_seen = False
        self.first_seen_reward = 1 # reward for finding the object initially

        self.first_in_range = False
        self.first_in_range_reward = 5.0 # object in interaction range

        self.mission_success_reward = 10.0 # object in interaction range and done

        # init event
        self.event = None
        self.step(5) # self.controller.step("Done") 
        self.observation = self.get_observation()

        # training
        self.use_gpu = True
        
        # learning
        self.batch_size = 4
        self.learning_rate = 0.001
        self.alpha = 1 # soc temparature
        self.gamma = 0.95 # discount factor
        self.soft_target_tau = 0.01
        self.target_update_period = 1 # updatge target network frequency

        # record
        self.n_train_steps_total = 0
        self.episode_steps = 0
        self.episode_total_reward = 0
        self.episode_policy_loss = []
        self.episode_pf1_loss = []
        self.episode_pf2_loss = []
        

        # policy network
        self.input_dim = len(self.observation)
        self.hidden_dim = 64
        self.action_dim = 6
        self.policy = PolicyNetwork(self.input_dim, self.hidden_dim, self.action_dim)

        self.qf1 = QNetwork(self.input_dim , self.action_dim, self.hidden_dim)
        self.qf2 = QNetwork(self.input_dim , self.action_dim, self.hidden_dim)
        self.target_qf1 = QNetwork(self.input_dim , self.action_dim, self.hidden_dim)
        self.target_qf2 = QNetwork(self.input_dim , self.action_dim, self.hidden_dim)
        
        if self.use_gpu:
            self.policy = self.policy.cuda()
            self.qf1 = self.qf1.cuda()
            self.qf2 = self.qf2.cuda()
            self.target_qf1 = self.target_qf1.cuda()
            self.target_qf2 = self.target_qf2.cuda()

        # loss
        self.qf_criterion = nn.MSELoss()

        self.update_target_networks()

        self.policy_optimizer = optim.Adam(
            self.policy.parameters(),
            lr=self.learning_rate,
        )
        self.qf1_optimizer = optim.Adam(
            self.qf1.parameters(),
            lr=self.learning_rate,
        )
        self.qf2_optimizer = optim.Adam(
            self.qf2.parameters(),
            lr=self.learning_rate,
        )

        self.update_target_networks()
コード例 #25
0
                #         semantic = observations["semantic_sensor"]
                #         depth = observations["depth_sensor"]
                #         self.display_sample(rgb, semantic, depth, mainobj=obj, visualize=False)
                # if self.visualize:
                #         rgb = observations["color_sensor"]
                #         semantic = observations["semantic_sensor"]
                #         depth = observations["depth_sensor"]
                #         self.display_sample(rgb, semantic, depth, mainobj=obj, visualize=False)

                #print("agent_state: position", self.agent.state.position, "rotation", self.agent.state.rotation)


if __name__ == '__main__':
    Ai2Thor()

controller = Controller(scene='FloorPlan28', gridSize=0.25)

# event = controller.step(action='MoveAhead')
# for obj in controller.last_event.metadata['objects']:
#     print(obj['objectId'])

event = controller.step('GetReachablePositions')
positions = event.metadata['reachablePositions']

angle = np.random.uniform(0, 359)
event = controller.step(action='Rotate', rotation=angle)

# for o in event.metadata['objects']:
#     print(o['objectId'])

obj_cur = np.random.choice(event.metadata['objects'])
コード例 #26
0
    def create_controller(self):
        controller = Controller(**self.env_args)

        return controller
コード例 #27
0
ファイル: topview.py プロジェクト: lone17/int3409-robot
        self._update_heatmap(agent_position)

        overlay = self.cmap(self.heatmap)
        overlay = (overlay[..., :3] * 255).astype('int')

        normal_frame = self.topdown_map.copy()
        semantic_frame = self.topdown_map_semantic.copy()

        mask = self.heatmap > self.base_heat

        normal_frame[mask] = overlay[mask]
        semantic_frame[mask] = overlay[mask]

        # return (np.fliplr(np.flipud(normal_frame)),
        #         np.fliplr(np.flipud(semantic_frame)))
        # return (cv2.rotate(normal_frame, cv2.ROTATE_90_COUNTERCLOCKWISE),
        #         cv2.rotate(semantic_frame, cv2.ROTATE_90_COUNTERCLOCKWISE))
        return normal_frame, semantic_frame


if __name__ == "__main__":
    controller = Controller()
    controller.start()
    controller.reset("FloorPlan1")

    drawer = TrajectoryDrawer(controller)
    new_frame = drawer.draw(
        controller.last_event.metadata["agent"]["position"])
    plt.imshow(new_frame)
    plt.show()
コード例 #28
0
ファイル: check_thor.py プロジェクト: strategist922/alfred-1
from ai2thor.controller import Controller

c = Controller()
c.start()
event = c.step(dict(action="MoveAhead"))
assert event.frame.shape == (300, 300, 3)
print(event.frame.shape)
print("Everything works!!!")
コード例 #29
0
ファイル: test.py プロジェクト: duychuong0999/revec
def live_test(testing_scene,
              test_objects,
              shared_model,
              config,
              arguments=dict()):

    f = h5py.File(
        '/home/chuong/Desktop/Robot/tailong/RL-target-driven-navigation-ai2thor/dumped/'
        + testing_scene + '.hdf5', "r")
    states = f['locations'][()]

    model = shared_model
    if model is not None:
        model.eval()

    # test_object = np.random.choice(test_objects)
    test_object = 'Desk'
    env = AI2ThorDumpEnv(testing_scene, test_object, config, arguments)
    print(arguments['angle'])

    #---------
    controller = Controller()
    controller.start()
    controller.reset(testing_scene)

    drawer = TrajectoryDrawer(controller)

    new_test_object = None
    while 1:
        if new_test_object is not None and new_test_object != test_object:
            print("Finding {} ..".format(new_test_object))
            env = AI2ThorDumpEnv(testing_scene, new_test_object, config,
                                 arguments)
        else:
            print("Finding {} ..".format(test_object))

        state, score, target = env.reset()
        start = env.current_state_id
        done = True
        stop = 0

        for step in range(arguments['num_iters']):
            ob = env.observations[env.current_state_id]

            cv2.imshow("Live Test", cv2.resize(ob[:, :, ::-1], (400, 400)))

            new_frame = drawer.draw({
                'x': states[env.current_state_id][0],
                'z': states[env.current_state_id][1]
            })
            cv2.imshow(
                "topview",
                cv2.cvtColor(new_frame.astype(np.uint8), cv2.COLOR_RGB2BGR))

            time.sleep(0.1)
            k = cv2.waitKey(33)

            if k == ord('r'):  # press q to escape
                new_test_object_id = int(
                    input("Specify target: {}\n".format(
                        list(zip(range(len(test_objects)), test_objects)))))
                new_test_object = test_objects[new_test_object_id]
                break
            elif k == ord('q'):  # press q to escape
                sys.exit("End live test.")

            if model is not None:
                with torch.no_grad():
                    value, logit = model(state, score, target)
                prob = F.softmax(logit, dim=-1)
                action = prob.max(1, keepdim=True)[1].numpy()[0, 0]
                # action = prob.multinomial(num_samples=1).detach().numpy()[0, 0]

            else:
                action = np.random.choice(range(arguments['action_size']))

            print("Action: {}".format(
                ['Move Forward', 'Move Backward', 'Turn Right',
                 'Turn Left'][action]))
            state, score, reward, done = env.step(action)
            if env.collided:
                print("Collision occurs.")
            # a quick hack to prevent the agent from stucking
            # i.e. in test mode an agent can repeat an action ad infinitum

            if done:
                stop += 1
                if stop == 2:
                    new_test_object_id = int(
                        input("Specify target: {}\n".format(
                            list(zip(range(len(test_objects)),
                                     test_objects)))))
                    new_test_object = test_objects[new_test_object_id]
                    stop = 0
                    break

        if not done:
            print("Fail")
        else:
            print("Success with {} redundant steps.".format(
                step + 1 - env.shortest[start, env.current_state_id]))
import os
import sys
root_dir = os.path.normpath(
    os.path.dirname(os.path.realpath(__file__)) + "/..")
sys.path.insert(0, root_dir)
import ai2thor.controller

import time
import random

from ai2thor.controller import Controller
c = Controller(scene='FloorPlan1_physics',
               gridSize=0.25,
               width=900,
               height=900,
               agentMode='arm',
               fieldOfView=100,
               agentControllerType='mid-level',
               targetFrameRate=30,
               fixedDeltaTime=0.005)

print(c.build_url())
event = c.step(action='TeleportFull',
               x=-1,
               y=0.9009995460510254,
               z=1,
               rotation=dict(x=0, y=180, z=0),
               horizon=0)
event = c.step(action='MoveMidLevelArm',
               disableRendering=False,
               position=dict(x=0.01, y=0, z=0.01),