Ejemplo n.º 1
0
    def step(self, action, raise_for_failure=False):

        if action['action'] == 'PutObject':
            receptacle_type = action['receptacleObjectId'].split('|')[0]
            object_type = action['objectId'].split('|')[0]
            if object_type not in RECEPTACLE_OBJECTS[receptacle_type]:
                new_event = ai2thor.server.Event(
                    json.loads(json.dumps(self.last_event.metadata)),
                    self.last_event.image_data)

                new_event.metadata['lastActionSuccess'] = False
                new_event.metadata[
                    'errorCode'] = 'ObjectNotAllowedInReceptacle'
                self.last_event = new_event
                return new_event

        assert self.request_queue.empty()

        # Converts numpy scalars to python scalars so they can be encoded in
        # JSON.
        action_filtered = {}
        for k, v in action.items():
            if isinstance(v, np.generic):
                v = np.asscalar(v)
            action_filtered[k] = v
        action = action_filtered

        self.response_queue.put_nowait(action)
        self.last_event = queue_get(self.request_queue)
        if raise_for_failure:
            assert self.last_event.metadata['lastActionSuccess']

        return self.last_event
Ejemplo n.º 2
0
    def start(self,
              port=0,
              start_unity=True,
              player_screen_width=300,
              player_screen_height=300,
              x_display="0.0"):

        env = os.environ.copy()

        if platform.system() == 'Linux':
            env['DISPLAY'] = ':' + x_display

        self.download_binary()

        self.server = ai2thor.server.Server(self.request_queue,
                                            self.response_queue, port)

        self.server_thread = threading.Thread(target=self._start_thread,
                                              args=(env, player_screen_width,
                                                    player_screen_height, port,
                                                    start_unity))
        self.server_thread.daemon = True
        self.server_thread.start()

        # receive the first request
        self.last_event = queue_get(self.request_queue)

        return self.last_event
Ejemplo n.º 3
0
    def step(self, action, raise_for_failure=False):
        if not self._check_action(action):
            new_event = ai2thor.server.Event(
                json.loads(json.dumps(self.last_event.metadata)),
                self.last_event.image)

            new_event.metadata['lastActionSuccess'] = False
            self.last_event = new_event
            return new_event
        assert self.request_queue.empty()

        # Converts numpy scalars to python scalars so they can be encoded in
        # JSON.
        action_filtered = {}
        for k, v in action.items():
            if isinstance(v, np.generic):
                v = np.asscalar(v)
            action_filtered[k] = v
        action = action_filtered

        self.response_queue.put_nowait(action)
        self.last_event = queue_get(self.request_queue)
        #print(self.last_event.metadata['errorMessage'])
        if raise_for_failure:
            assert self.last_event.metadata['lastActionSuccess']

        return self.last_event
Ejemplo n.º 4
0
    def step(self, action, raise_for_failure=False):
        if not self._check_action(action):
            new_event = ai2thor.server.Event(
                json.loads(json.dumps(self.last_event.metadata)),
                self.last_event.image)

            new_event.metadata['lastActionSuccess'] = False
            self.last_event = new_event
            return new_event
        assert self.request_queue.empty()

        # Converts numpy scalars to python scalars so they can be encoded in
        # JSON.
        action_filtered = {}
        for k,v in action.items():
            if isinstance(v, np.generic):
                v = np.asscalar(v)
            action_filtered[k] = v
        action = action_filtered

        self.response_queue.put_nowait(action)
        self.last_event = queue_get(self.request_queue)
        #print(self.last_event.metadata['errorMessage'])
        if raise_for_failure:
            assert self.last_event.metadata['lastActionSuccess']

        return self.last_event
Ejemplo n.º 5
0
    def reset(self, scene='FloorPlan_Train1_1'):
        if re.match(r'^FloorPlan[0-9]+$', scene):
            scene = scene + "_physics"

        if scene not in self.scenes_in_build:

            def key_sort_func(scene_name):
                m = re.search(
                    'FloorPlan[_]?([a-zA-Z\-]*)([0-9]+)_?([0-9]+)?.*$',
                    scene_name)
                last_val = m.group(3) if m.group(3) is not None else -1
                return m.group(1), int(m.group(2)), int(last_val)

            raise ValueError(
                "\nScene not contained in build (scene names are case sensitive)."
                "\nPlease choose one of the following scene names:\n\n{}".
                format(", ".join(
                    sorted(list(self.scenes_in_build), key=key_sort_func))))

        self.response_queue.put_nowait(
            dict(action='Reset', sceneName=scene, sequenceId=0))
        self.last_event = queue_get(self.request_queue)  # can this be deleted?
        self.last_event = self.step(action='Initialize',
                                    **self.initialization_parameters)

        return self.last_event
Ejemplo n.º 6
0
    def step(self, action, raise_for_failure=False):

        # XXX should be able to get rid of this with some sort of deprecation warning
        if 'AI2THOR_VISIBILITY_DISTANCE' in os.environ:
            action['visibilityDistance'] = float(
                os.environ['AI2THOR_VISIBILITY_DISTANCE'])

        should_fail = False
        self.last_action = action

        if ('objectId' in action and (action['action'] == 'OpenObject'
                                      or action['action'] == 'CloseObject')):

            force_visible = action.get('forceVisible', False)
            if not force_visible and self.last_event.instance_detections2D and action[
                    'objectId'] not in self.last_event.instance_detections2D:
                should_fail = True

            obj_metadata = self.last_event.get_object(action['objectId'])
            if obj_metadata is None or obj_metadata['isopen'] == (
                    action['action'] == 'OpenObject'):
                should_fail = True

        elif action['action'] == 'PutObject':
            receptacle_type = action['receptacleObjectId'].split('|')[0]
            object_type = action['objectId'].split('|')[0]
            if object_type not in RECEPTACLE_OBJECTS[receptacle_type]:
                should_fail = True

        if should_fail:
            new_event = copy.deepcopy(self.last_event)
            new_event.metadata['lastActionSuccess'] = False
            self.last_event = new_event
            return new_event

        assert self.request_queue.empty()

        # Converts numpy scalars to python scalars so they can be encoded in
        # JSON.
        action_filtered = {}
        for k, v in action.items():
            if isinstance(v, np.generic):
                v = np.asscalar(v)
            action_filtered[k] = v
        action = action_filtered

        self.response_queue.put_nowait(action)
        self.last_event = queue_get(self.request_queue)

        if not self.last_event.metadata[
                'lastActionSuccess'] and self.last_event.metadata[
                    'errorCode'] == 'InvalidAction':
            raise ValueError(self.last_event.metadata['errorMessage'])

        if raise_for_failure:
            assert self.last_event.metadata['lastActionSuccess']

        return self.last_event
Ejemplo n.º 7
0
    def reset(self, scene_name=None):
        if not scene_name.endswith('_physics'):
            scene_name = scene_name + "_physics"

        self.response_queue.put_nowait(
            dict(action='Reset', sceneName=scene_name, sequenceId=0))
        self.last_event = queue_get(self.request_queue)

        return self.last_event
Ejemplo n.º 8
0
    def step(self, action, raise_for_failure=False):

        # prevent changes to the action from leaking
        action = copy.deepcopy(action)

        # XXX should be able to get rid of this with some sort of deprecation warning
        if 'AI2THOR_VISIBILITY_DISTANCE' in os.environ:
            action['visibilityDistance'] = float(
                os.environ['AI2THOR_VISIBILITY_DISTANCE'])

        should_fail = False
        self.last_action = action

        if ('objectId' in action and (action['action'] == 'OpenObject'
                                      or action['action'] == 'CloseObject')):

            force_visible = action.get('forceVisible', False)
            if not force_visible and self.last_event.instance_detections2D and action[
                    'objectId'] not in self.last_event.instance_detections2D:
                should_fail = True

            obj_metadata = self.last_event.get_object(action['objectId'])
            if obj_metadata is None or obj_metadata['isopen'] == (
                    action['action'] == 'OpenObject'):
                should_fail = True

        elif action['action'] == 'PutObject':
            receptacle_type = action['receptacleObjectId'].split('|')[0]
            object_type = action['objectId'].split('|')[0]
            if object_type not in RECEPTACLE_OBJECTS[receptacle_type]:
                should_fail = True

        rotation = action.get('rotation')
        if rotation is not None and type(rotation) != dict:
            action['rotation'] = {}
            action['rotation']['y'] = rotation

        if should_fail:
            new_event = copy.deepcopy(self.last_event)
            new_event.metadata['lastActionSuccess'] = False
            self.last_event = new_event
            return new_event

        assert self.request_queue.empty()

        self.response_queue.put_nowait(action)
        self.last_event = queue_get(self.request_queue)

        if not self.last_event.metadata[
                'lastActionSuccess'] and self.last_event.metadata[
                    'errorCode'] == 'InvalidAction':
            raise ValueError(self.last_event.metadata['errorMessage'])

        if raise_for_failure:
            assert self.last_event.metadata['lastActionSuccess']

        return self.last_event
Ejemplo n.º 9
0
    def step(self, action=None, **action_args):

        if type(action) is dict:
            action = copy.deepcopy(action) # prevent changes from leaking
        else:
            action = dict(action=action)

        raise_for_failure = action_args.pop('raise_for_failure', False)
        action.update(action_args)

        if self.headless:
            action["renderImage"] = False

        # prevent changes to the action from leaking
        action = copy.deepcopy(action)

        # XXX should be able to get rid of this with some sort of deprecation warning
        if 'AI2THOR_VISIBILITY_DISTANCE' in os.environ:
            action['visibilityDistance'] = float(os.environ['AI2THOR_VISIBILITY_DISTANCE'])

        should_fail = False
        self.last_action = action

        if ('objectId' in action and (action['action'] == 'OpenObject' or action['action'] == 'CloseObject')):

            force_visible = action.get('forceVisible', False)
            if not force_visible and self.last_event.instance_detections2D and action['objectId'] not in self.last_event.instance_detections2D:
                should_fail = True

            obj_metadata = self.last_event.get_object(action['objectId'])
            if obj_metadata is None or obj_metadata['isOpen'] == (action['action'] == 'OpenObject'):
                should_fail = True


        rotation = action.get('rotation')
        if rotation is not None and type(rotation) != dict:
            action['rotation'] = {}
            action['rotation']['y'] = rotation

        if should_fail:
            new_event = copy.deepcopy(self.last_event)
            new_event.metadata['lastActionSuccess'] = False
            self.last_event = new_event
            return new_event

        assert self.request_queue.empty()

        self.response_queue.put_nowait(action)
        self.last_event = queue_get(self.request_queue)

        if not self.last_event.metadata['lastActionSuccess'] and self.last_event.metadata['errorCode'] == 'InvalidAction':
            raise ValueError(self.last_event.metadata['errorMessage'])

        if raise_for_failure:
            assert self.last_event.metadata['lastActionSuccess']

        return self.last_event
Ejemplo n.º 10
0
    def start(self,
              port=0,
              start_unity=True,
              player_screen_width=300,
              player_screen_height=300,
              x_display=None):

        if player_screen_height < 300 or player_screen_width < 300:
            raise Exception("Screen resolution must be >= 300x300")

        if self.server_thread is not None:
            raise Exception(
                "server has already been started - cannot start more than once"
            )

        env = os.environ.copy()

        image_name = None
        host = '127.0.0.1'

        if start_unity:
            if platform.system() == 'Linux':

                if self.docker_enabled and ai2thor.docker.has_docker(
                ) and ai2thor.docker.nvidia_version() is not None:
                    image_name = ai2thor.docker.build_image()
                    host = ai2thor.docker.bridge_gateway()
                else:

                    if x_display:
                        env['DISPLAY'] = ':' + x_display
                    elif 'DISPLAY' not in env:
                        env['DISPLAY'] = ':0.0'

                    self.download_binary()
            else:
                self.download_binary()

        self.server = ai2thor.server.Server(self.request_queue,
                                            self.response_queue,
                                            host,
                                            port=port)

        self.server_thread = threading.Thread(
            target=self._start_thread,
            args=(env, player_screen_width, player_screen_height, host, port,
                  image_name, start_unity))
        self.server_thread.daemon = True
        self.server_thread.start()

        # receive the first request
        self.last_event = queue_get(self.request_queue)

        return self.last_event
Ejemplo n.º 11
0
    def start(
            self,
            port=0,
            start_unity=True,
            player_screen_width=300,
            player_screen_height=300,
            x_display="0.0"):

        if player_screen_height < 300 or player_screen_width < 300:
            raise Exception("Screen resolution must be >= 300x300")

        if self.server_thread is not None:
            raise Exception("server has already been started - cannot start more than once")

        env = os.environ.copy()

        image_name = None
        host = '127.0.0.1'

        if start_unity:
            if platform.system() == 'Linux':

                if self.docker_enabled and ai2thor.docker.has_docker() and ai2thor.docker.nvidia_version() is not None:
                    image_name = ai2thor.docker.build_image()
                    host = ai2thor.docker.bridge_gateway()
                else:
                    env['DISPLAY'] = ':' + x_display
                    self.download_binary()
            else:
                self.download_binary()

        self.server = ai2thor.server.Server(
            self.request_queue,
            self.response_queue,
            host,
            port=port)

        self.server_thread = threading.Thread(
            target=self._start_thread,
            args=(env, player_screen_width, player_screen_height, host, port, image_name, start_unity))
        self.server_thread.daemon = True
        self.server_thread.start()

        # receive the first request
        self.last_event = queue_get(self.request_queue)

        return self.last_event
Ejemplo n.º 12
0
    def step(self, action, raise_for_failure=False):
        if not self._check_action(action):
            new_event = ai2thor.server.Event(
                json.loads(json.dumps(self.last_event.metadata)),
                self.last_event.image)

            new_event.metadata['lastActionSuccess'] = False
            self.last_event = new_event
            return new_event
        assert self.request_queue.empty()

        self.response_queue.put_nowait(action)
        self.last_event = queue_get(self.request_queue)
        #print(self.last_event.metadata['errorMessage'])
        if raise_for_failure:
            assert self.last_event.metadata['lastActionSuccess']

        return self.last_event
Ejemplo n.º 13
0
    def step(self, action, raise_for_failure=False):

        # XXX should be able to get rid of this with some sort of deprecation warning
        if 'AI2THOR_VISIBILITY_DISTANCE' in os.environ:
            action['visibilityDistance'] = float(
                os.environ['AI2THOR_VISIBILITY_DISTANCE'])

        if action['action'] == 'PutObject':
            receptacle_type = action['receptacleObjectId'].split('|')[0]
            object_type = action['objectId'].split('|')[0]
            if object_type not in RECEPTACLE_OBJECTS[receptacle_type]:
                new_event = ai2thor.server.Event(
                    json.loads(json.dumps(self.last_event.metadata)),
                    self.last_event.image_data)

                new_event.metadata['lastActionSuccess'] = False
                new_event.metadata[
                    'errorCode'] = 'ObjectNotAllowedInReceptacle'
                self.last_event = new_event
                return new_event

        assert self.request_queue.empty()

        # Converts numpy scalars to python scalars so they can be encoded in
        # JSON.
        action_filtered = {}
        for k, v in action.items():
            if isinstance(v, np.generic):
                v = np.asscalar(v)
            action_filtered[k] = v
        action = action_filtered

        self.response_queue.put_nowait(action)
        self.last_event = queue_get(self.request_queue)
        if raise_for_failure:
            assert self.last_event.metadata['lastActionSuccess']

        return self.last_event
Ejemplo n.º 14
0
    def start(self,
              port=0,
              start_unity=True,
              player_screen_width=300,
              player_screen_height=300,
              x_display=None,
              enable_remote_viewer=False):

        if 'AI2THOR_VISIBILITY_DISTANCE' in os.environ:
            import warnings
            warnings.warn(
                "AI2THOR_VISIBILITY_DISTANCE environment variable is deprecated, use the parameter visibilityDistance parameter with the Initialize action instead"
            )

        if player_screen_height < 300 or player_screen_width < 300:
            raise Exception("Screen resolution must be >= 300x300")

        if self.server_thread is not None:
            raise Exception(
                "server has already been started - cannot start more than once"
            )

        env = os.environ.copy()

        image_name = None

        remote_viewer_host = host = '127.0.0.1'

        if self.docker_enabled:
            self.check_docker()
            host = ai2thor.docker.bridge_gateway()
            remote_viewer_host = host
        elif enable_remote_viewer:
            host = '0.0.0.0'
            remote_viewer_host = socket.gethostname()

        self.server = ai2thor.server.Server(self.request_queue,
                                            self.response_queue,
                                            host,
                                            port=port,
                                            threaded=enable_remote_viewer)

        _, port = self.server.wsgi_server.socket.getsockname()
        if enable_remote_viewer:
            print("Remote viewer: http://%s:%s/viewer" %
                  (remote_viewer_host, port))

        self.server_thread = threading.Thread(target=self._start_server_thread)

        self.server_thread.daemon = True
        self.server_thread.start()

        if start_unity:
            if platform.system() == 'Linux':

                if self.docker_enabled:
                    image_name = ai2thor.docker.build_image()
                else:

                    if x_display:
                        env['DISPLAY'] = ':' + x_display
                    elif 'DISPLAY' not in env:
                        env['DISPLAY'] = ':0.0'

                    self.check_x_display(env['DISPLAY'])

            self.download_binary()

            unity_thread = threading.Thread(target=self._start_unity_thread,
                                            args=(env, player_screen_width,
                                                  player_screen_height, host,
                                                  port, image_name))
            unity_thread.daemon = True
            unity_thread.start()

        # receive the first request
        self.last_event = queue_get(self.request_queue)

        return self.last_event
Ejemplo n.º 15
0
    def reset(self, scene_name=None):
        self.response_queue.put_nowait(
            dict(action='Reset', sceneName=scene_name, sequenceId=0))
        self.last_event = queue_get(self.request_queue)

        return self.last_event
Ejemplo n.º 16
0
    def reset(self, scene_name=None):
        self.response_queue.put_nowait(dict(action='Reset', sceneName=scene_name, sequenceId=0))
        self.last_event = queue_get(self.request_queue)

        return self.last_event
Ejemplo n.º 17
0
    def start(
            self,
            port=0,
            start_unity=True,
            width=300,
            height=300,
            x_display=None,
            host='127.0.0.1',
            player_screen_width=None,
            player_screen_height=None
    ):

        if 'AI2THOR_VISIBILITY_DISTANCE' in os.environ:
            import warnings
            warnings.warn("AI2THOR_VISIBILITY_DISTANCE environment variable is deprecated, use \
                the parameter visibilityDistance parameter with the Initialize action instead")

        if player_screen_width is not None:
            warnings.warn("'player_screen_width' parameter is deprecated, use the 'width'"
                          " parameter instead.")
            width = player_screen_width

        if player_screen_height is not None:
            warnings.warn("'player_screen_height' parameter is deprecated, use the 'height'"
                          " parameter instead.")
            height = player_screen_height

        if height < 300 or width < 300:
            raise Exception("Screen resolution must be >= 300x300")

        if self.server_thread is not None:
            import warnings
            warnings.warn('start method depreciated. The server started when the Controller was initialized.')

            # Stops the current server and creates a new one. This is done so
            # that the arguments passed in will be used on the server.
            self.stop()

        env = os.environ.copy()

        image_name = None

        if self.docker_enabled:
            self.check_docker()
            host = ai2thor.docker.bridge_gateway()

        self.server = ai2thor.server.Server(
            self.request_queue,
            self.response_queue,
            host,
            port=port,
            depth_format=self.depth_format,
            add_depth_noise=self.add_depth_noise,
            width=width,
            height=height
        )

        _, port = self.server.wsgi_server.socket.getsockname()

        self.server_thread = threading.Thread(target=self._start_server_thread)

        self.server_thread.daemon = True
        self.server_thread.start()

        if start_unity:
            if platform.system() == 'Linux':

                if self.docker_enabled:
                    image_name = ai2thor.docker.build_image()
                else:

                    if x_display:
                        env['DISPLAY'] = ':' + x_display
                    elif 'DISPLAY' not in env:
                        env['DISPLAY'] = ':0.0'

                    self.check_x_display(env['DISPLAY'])

            if not self.local_executable_path:
                self.download_binary()
                self.lock_release()
                self.prune_releases()

            unity_thread = threading.Thread(
                target=self._start_unity_thread,
                args=(env, width, height, host, port, image_name))
            unity_thread.daemon = True
            unity_thread.start()

        # receive the first request
        self.last_event = queue_get(self.request_queue)

        return self.last_event