예제 #1
0
    def init(self, context: Context):
        context.info("Check GPU...")

        self.check_gpu_available(context)

        # Model predicts linear and angular velocity but we need to issue
        # wheel velocity commands to robot, this wrapper converts to the latter.
        self.convertion_wrapper = SteeringToWheelVelWrapper()

        context.info('init()')

        model = Modelv1()
        model.load_state_dict(
            torch.load(
                "modelv1_maserati_bill_simulated_amadobot_base_epoch_200.pth",
                # "modelv1_maserati_bill_simulated_amadobot_base.pth",
                # 'modelv1_maserati_plus_simulated.pth',
                map_location=self.device,
        ))
        model.eval()
        self.model = model.to(self.device)

        self.current_image = np.zeros(self.expect_shape)
        self.input_image = np.zeros((150, 200, 3))
        self.to_predictor = np.expand_dims(self.input_image, axis=0)
예제 #2
0
    def init(self, context: Context):
        context.info("Check GPU...")

        self.check_gpu_available(context)

        # Model predicts linear and angular velocity but we need to issue
        # wheel velocity commands to robot, this wrapper converts to the latter.
        self.convertion_wrapper = SteeringToWheelVelWrapper()

        context.info('init()')

        model_lin = NvidiaModel()
        model_lin.load_state_dict(
            torch.load(
                'saved_nvidia_model_lin.pth',
                map_location=self.device,
            ))
        model_lin.eval()
        self.model_lin = model_lin.to(self.device)

        model_ang = NvidiaModel()
        model_ang.load_state_dict(
            torch.load(
                'saved_nvidia_model_ang.pth',
                map_location=self.device,
            ))
        model_ang.eval()
        self.model_ang = model_ang.to(self.device)

        self.current_image = np.zeros(self.expect_shape)
        self.input_image = np.zeros((150, 200, 3))
        self.to_predictor = np.expand_dims(self.input_image, axis=0)
    def __init__(self, load_model=False, model_path=None):
        logger.info('PytorchRLTemplateAgent init')
        self.preprocessor = DTPytorchWrapper()
        self.image_size = (120,160, 3)
        self.wrapper  = SteeringToWheelVelWrapper()
        self.model = Dronet()
        self._device = self.model._device
        self.model.to(self._device)
        self.current_image = np.zeros((3,self.image_size[0],self.image_size[1]))

        if load_model:
            logger.info('PytorchRLTemplateAgent loading models')
            fp = model_path if model_path else "model.pt"
            self.model.load(fp, "models", for_inference=True)
            logger.info('PytorchRLTemplateAgent model loaded')
        logger.info('PytorchRLTemplateAgent init complete')
예제 #4
0
class Agent:
    def __init__(self, expect_shape=(480, 640, 3)):
        self.expect_shape: Tuple[int, int, int] = expect_shape

    def init(self, context: Context):
        context.info("Check GPU...")

        self.check_gpu_available(context)

        # Model predicts linear and angular velocity but we need to issue
        # wheel velocity commands to robot, this wrapper converts to the latter.
        self.convertion_wrapper = SteeringToWheelVelWrapper()

        context.info('init()')

        model = Modelv1()
        model.load_state_dict(
            torch.load(
                "modelv1_maserati_bill_simulated_amadobot_base_epoch_200.pth",
                # "modelv1_maserati_bill_simulated_amadobot_base.pth",
                # 'modelv1_maserati_plus_simulated.pth',
                map_location=self.device,
        ))
        model.eval()
        self.model = model.to(self.device)

        self.current_image = np.zeros(self.expect_shape)
        self.input_image = np.zeros((150, 200, 3))
        self.to_predictor = np.expand_dims(self.input_image, axis=0)

    def check_gpu_available(self, context: Context):
        available = torch.cuda.is_available()
        req = os.environ.get('AIDO_REQUIRE_GPU', None)
        context.info(f'torch.cuda.is_available = {available!r} AIDO_REQUIRE_GPU = {req!r}')
        context.info('init()')
        self.device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
        if available:
            i = torch.cuda.current_device()
            count = torch.cuda.device_count()
            name = torch.cuda.get_device_name(i)
            context.info(f'device {i} of {count}; name = {name!r}')
        else:
            if req is not None:
                msg = 'No GPU found'
                context.error(msg)

    def on_received_seed(self, data: int):
        np.random.seed(data)

    def on_received_episode_start(self, context: Context, data: EpisodeStart):
        context.info(f'Starting episode "{data.episode_name}".')

    def on_received_observations(self, data: DB20Observations):
        """
        Processes images.

        Robot and simulator images are 480x600, but our model expects
        150x200, so resize before setting self.to_predictor tensor
        which is passed to the model.
        """
        camera: JPGImage = data.camera
        self.current_image = jpg2rgb(camera.jpg_data)
        self.input_image = image_resize(self.current_image, width=200)
        self.input_image = self.input_image[0:150, 0:200]
        self.input_image = cv2.cvtColor(self.input_image, cv2.COLOR_RGB2YUV)
        self.to_predictor = np.expand_dims(self.input_image, axis=0)

    def compute_action(self, observation):
        """
        Use prior observation to predict best action, return predictions for velocities.
        """
        obs = torch.from_numpy(observation).float().to(self.device)
        linear, angular = self.model(obs)
        linear = linear.to(torch.device('cpu')).data.numpy().flatten()
        angular = angular.to(torch.device('cpu')).data.numpy().flatten()
        return linear, angular

    # ! Major Manipulation here. Should not always change.
    def on_received_get_commands(self, context: Context):
        """
        Converts requested linear/angular velocities to control commands
        and issues them to the robot.

        Don't change this, standard stuff from the submission template.
        """
        linear, angular = self.compute_action(self.to_predictor)
        # Inverse kinematics
        pwm_left, pwm_right = self.convertion_wrapper.convert(linear, angular)
        pwm_left = float(np.clip(pwm_left, -1, +1))
        pwm_right = float(np.clip(pwm_right, -1, +1))

        # LED commands
        grey = RGB(0.0, 0.0, 0.0)
        red = RGB(1.0, 0.0, 0.0)
        blue = RGB(0.0, 0.0, 1.0)
        led_commands = LEDSCommands(red, grey, blue, red, blue)

        # Send PWM command
        pwm_commands = PWMCommands(motor_left=pwm_left, motor_right=pwm_right)
        commands = DB20Commands(pwm_commands, led_commands)
        context.write('commands', commands)

    def finish(self, context: Context):
        context.info('finish()')
class PytorchRLTemplateAgent:
    def __init__(self, load_model=False, model_path=None):
        logger.info('PytorchRLTemplateAgent init')
        self.preprocessor = DTPytorchWrapper()
        self.image_size = (120,160, 3)
        self.wrapper  = SteeringToWheelVelWrapper()
        self.model = Dronet()
        self._device = self.model._device
        self.model.to(self._device)
        self.current_image = np.zeros((3,self.image_size[0],self.image_size[1]))

        if load_model:
            logger.info('PytorchRLTemplateAgent loading models')
            fp = model_path if model_path else "model.pt"
            self.model.load(fp, "models", for_inference=True)
            logger.info('PytorchRLTemplateAgent model loaded')
        logger.info('PytorchRLTemplateAgent init complete')

    def init(self, context: Context):
        available = torch.cuda.is_available()
        req = os.environ.get('AIDO_REQUIRE_GPU', None)
        context.info(f'torch.cuda.is_available = {available!r} AIDO_REQUIRE_GPU = {req!r}')
        context.info('init()')
        if available:
            i = torch.cuda.current_device()
            count = torch.cuda.device_count()
            name = torch.cuda.get_device_name(i)
            context.info(f'device {i} of {count}; name = {name!r}')

        else:
            if req is not None:
                msg = 'I need a GPU; bailing.'
                context.error(msg)
                raise Exception(msg)


    def on_received_seed(self, data: int):
        np.random.seed(data)

    def on_received_episode_start(self, context: Context, data: EpisodeStart):
        context.info(f'Starting episode "{data.episode_name}".')

    def on_received_observations(self, data: DB20Observations):
        camera: JPGImage = data.camera
        obs = jpg2rgb(camera.jpg_data)
        self.current_image = self.preprocessor.preprocess(obs)

    def compute_action(self, observation):
        observation = self.preprocessor.toTensor(observation).to(self._device).unsqueeze(0)
        action = self.model.predict(observation)
        return action.astype(float)

    def on_received_get_commands(self, context: Context):
        velocity, omega = self.compute_action(self.current_image)
        # multiplying steering angle by a gain
        omega *= 7
        pwm_left, pwm_right = self.wrapper.convert(velocity, omega)
        grey = RGB(0.0, 0.0, 0.0)
        led_commands = LEDSCommands(grey, grey, grey, grey, grey)
        pwm_commands = PWMCommands(motor_left=pwm_left, motor_right=pwm_right)
        commands = DB20Commands(pwm_commands, led_commands)
        context.write('commands', commands)

    def finish(self, context: Context):
        context.info('finish()')