Example #1
0
    def run_step_carla9(self, observations):
        norm_speed = np.linalg.norm(observations['velocity'])/g_conf.SPEED_FACTOR
        norm_speed = torch.cuda.FloatTensor([norm_speed]).unsqueeze(0)
        directions_tensor = torch.cuda.LongTensor([int(observations['command'])])
        # print ('rgb: ', observations['big_cam'].shape)
        # print ('velocity: ', observations['velocity'])
        # print ('norm velocity: ', np.linalg.norm(observations['velocity']))
        # print ('norm_speed: ', norm_speed.shape, norm_speed.item())
        # print ('directions_tensor: ', directions_tensor.shape, directions_tensor.item())

        model_outputs = self._model.forward_branch(self._process_sensors(observations), norm_speed,
                                                  directions_tensor)

        steer, throttle, brake = self._process_model_outputs(model_outputs[0])

        if self._carla_version == '0.9':
            import carla
            control = carla.VehicleControl()
        else:
            control = VehicleControl()
        # single model
        control.steer = float(steer)
        control.throttle = float(throttle)
        control.brake = float(brake)
        
        return control
Example #2
0
    def run_step(self, measurements, sensor_data, directions, target):
        """
            Run a step on the benchmark simulation
        Args:
            measurements: All the float measurements from CARLA ( Just speed is used)
            sensor_data: All the sensor data used on this benchmark
            directions: The directions, high level commands
            target: Final objective. Not used when the agent is predicting all outputs.

        Returns:
            Controls for the vehicle on the CARLA simulator.

        """
        # Get speed and high-level turning command
        # Take the forward speed and normalize it for it to go from 0-1
        norm_speed = measurements.player_measurements.forward_speed / g_conf.SPEED_FACTOR
        norm_speed = torch.cuda.FloatTensor([norm_speed]).unsqueeze(0)
        directions_tensor = torch.cuda.LongTensor([directions])

        # If we're evaluating squeeze network (so we are using ground truth seg mask)
        if "seg" in g_conf.SENSORS.keys():
            # Run the autopilot agent to get stop intentions
            _, state = self.control_agent.run_step(measurements, [], [], target)
            inputs_vec = []
            for input_name in g_conf.INTENTIONS:
                inputs_vec.append(float(state[input_name]))
            intentions = torch.cuda.FloatTensor(inputs_vec).unsqueeze(0)
            # Run squeeze network
            model_outputs = self._model.forward_branch(self._process_sensors(sensor_data), norm_speed,
                                                       directions_tensor, intentions, benchmark=True)
        else:
            # Run driving model
            model_outputs = self._model.forward_branch(self._process_sensors(sensor_data), norm_speed,
                                                       directions_tensor, benchmark=True)

        steer, throttle, brake = self._process_model_outputs(model_outputs[0])
        if self._carla_version == '0.9':
            import carla
            control = carla.VehicleControl()
        else:
            control = VehicleControl()
        control.steer = float(steer)
        control.throttle = float(throttle)
        control.brake = float(brake)
        # There is the posibility to replace some of the predictions with oracle predictions.
        if g_conf.USE_ORACLE:
            _, control.throttle, control.brake = self._get_oracle_prediction(
                measurements, target)

        if self.first_iter:
            coil_logger.add_message('Iterating', {"Checkpoint": self.checkpoint['iteration'],
                                                  'Agent': str(steer)},
                                    self.checkpoint['iteration'])
        self.first_iter = False
        
        return control
Example #3
0
    def run_step(self, measurements, sensor_data, directions, target):
        """
            Run a step on the benchmark simulation
        Args:
            measurements: All the float measurements from CARLA ( Just speed is used)
            sensor_data: All the sensor data used on this benchmark
            directions: The directions, high level commands
            target: Final objective. Not used when the agent is predicting all outputs.

        Returns:
            Controls for the vehicle on the CARLA simulator.

        """

        # Take the forward speed and normalize it for it to go from 0-1
        norm_speed = measurements.player_measurements.forward_speed / g_conf.SPEED_FACTOR
        norm_speed = torch.cuda.FloatTensor([norm_speed]).unsqueeze(0)
        directions_tensor = torch.cuda.LongTensor([directions])
        # Compute the forward pass processing the sensors got from CARLA.
        rgbs = self._process_sensors(sensor_data)
        with torch.no_grad():
            outputs = self.model_erf(rgbs)
        labels = outputs.max(1)[1].byte().cpu().data

        seg_road = (labels == 0)
        seg_not_road = (labels != 0)
        seg = torch.stack((seg_road, seg_not_road), 1).float()

        model_outputs = self._model.forward_branch(seg.cuda(), norm_speed,
                                                   directions_tensor)

        #        model_outputs = self._model.forward_branch(self._process_sensors(sensor_data), norm_speed,
        #                                                   directions_tensor)

        steer, throttle, brake = self._process_model_outputs(model_outputs[0])
        if self._carla_version == '0.9':
            import carla
            control = carla.VehicleControl()
        else:
            control = VehicleControl()
        control.steer = float(steer)
        control.throttle = float(throttle)
        control.brake = float(brake)
        # There is the posibility to replace some of the predictions with oracle predictions.
        if g_conf.USE_ORACLE:
            _, control.throttle, control.brake = self._get_oracle_prediction(
                measurements, target)

        if self.first_iter:
            coil_logger.add_message('Iterating', {
                "Checkpoint": self.checkpoint['iteration'],
                'Agent': str(steer)
            }, self.checkpoint['iteration'])
        self.first_iter = False

        return control
Example #4
0
    def run_step(self, measurements, sensor_data, directions, target):
        # We basically ignore all the parameters.
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                return VehicleControl()

        return self._get_keyboard_control(pygame.key.get_pressed())
Example #5
0
    def _get_keyboard_control(self, keys):
        """
        Return a VehicleControl message based on the pressed keys.

        Return None
        if a new episode was requested.
        """

        control = VehicleControl()
        if keys[K_LEFT] or keys[K_a]:
            control.steer = -1.0
        if keys[K_RIGHT] or keys[K_d]:
            control.steer = 1.0
        if keys[K_UP] or keys[K_w]:
            control.throttle = 1.0
        if keys[K_DOWN] or keys[K_s]:
            control.brake = 1.0
        if keys[K_SPACE]:
            control.hand_brake = True
        if keys[K_q]:
            self._is_on_reverse = not self._is_on_reverse
        control.reverse = self._is_on_reverse
        return control
Example #6
0
    def get_control(self, wp_angle, wp_angle_speed, speed_factor, current_speed):
        control = VehicleControl()
        current_speed = max(current_speed, 0)

        steer = self.params['steer_gain'] * wp_angle
        if steer > 0:
            control.steer = min(steer, 1)
        else:
            control.steer = max(steer, -1)

        # Don't go0 to fast around corners
        if math.fabs(wp_angle_speed) < 0.1: # 0.04 works better
            target_speed_adjusted = self.params['target_speed'] * speed_factor
        elif math.fabs(wp_angle_speed) < 0.5:
            target_speed_adjusted = 20 * speed_factor # 10 works better
        else:
            target_speed_adjusted = 15 * speed_factor # 10 works better

        self.pid.target = target_speed_adjusted
        pid_gain = self.pid(feedback=current_speed)
        print ('Target: ', self.pid.target, 'Error: ', self.pid.error, 'Gain: ', pid_gain)
        print ('Target Speed: ', target_speed_adjusted, 'Current Speed: ', current_speed, 'Speed Factor: ',
               speed_factor)

        throttle = min(max(self.params['default_throttle'] - 1.3 * pid_gain, 0),
                       self.params['throttle_max'])

        if pid_gain > 0.5:
            brake = min(0.35 * pid_gain * self.params['brake_strength'], 1)
        else:
            brake = 0


        control.throttle = max(throttle, 0)  # Prevent N by putting at least 0.01
        control.brake = brake

        print ('Throttle: ', control.throttle, 'Brake: ', control.brake, 'Steering Angle: ', control.steer)

        # with open('/is/sg2/aprakash/Documents/debug', 'a+') as log:
        #     log.write('Target: %f Error: %f Gain: %f \n'%(self.pid.target, self.pid.error, pid_gain))
        #     log.write('Target Speed: %f Current Speed: %f Speed Factor: %f \n'%(target_speed_adjusted, current_speed, speed_factor))
        #     log.write('Throttle: %f Brake: %f Steering Angle: %f \n'%(control.throttle, control.brake, control.steer))
        #     log.close()

        return control
Example #7
0
    def get_control(self, wp_angle, wp_angle_speed, speed_factor,
                    current_speed):
        control = VehicleControl()
        current_speed = max(current_speed, 0)

        steer = self.params['steer_gain'] * wp_angle
        if steer > 0:
            control.steer = min(steer, 1)
        else:
            control.steer = max(steer, -1)

        # Don't go too fast around corners
        if math.fabs(wp_angle_speed) < 0.1:
            target_speed_adjusted = self.params['target_speed'] * speed_factor
        elif math.fabs(wp_angle_speed) < 0.5:
            target_speed_adjusted = 20 * speed_factor
        else:
            target_speed_adjusted = 15 * speed_factor

        self.pid.target = target_speed_adjusted
        pid_gain = self.pid(feedback=current_speed)
        print('Target: ', self.pid.target, 'Error: ', self.pid.error, 'Gain: ',
              pid_gain)
        print(
            f'Target Speed: {target_speed_adjusted} Current Speed: {current_speed} Speed Factor: {speed_factor}'
        )

        throttle = min(
            max(self.params['default_throttle'] - 1.3 * pid_gain, 0),
            self.params['throttle_max'])

        if pid_gain > 0.5:
            brake = min(0.35 * pid_gain * self.params['brake_strength'], 1)
        else:
            brake = 0

        control.throttle = max(throttle,
                               0)  # Prevent N by putting at least 0.01
        control.brake = brake

        print(
            f'Throttle: {control.throttle} Brake: {control.brake} Steering Angle: {control.steer}'
        )

        return control
 def __init__(self, world, start_in_autopilot):
     self._autopilot_enabled = start_in_autopilot
     self._control = VehicleControl()
     self._command_cache = 2.0
     world.hud.notification("Press 'H' or '?' for help.", seconds=4.0)
Example #9
0
    def run_step(self, measurements, sensor_data, directions, target, **kwargs):
        """
            Run a step on the benchmark simulation
        Args:
            measurements: All the float measurements from CARLA ( Just speed is used)
            sensor_data: All the sensor data used on this benchmark
            directions: The directions, high level commands
            target: Final objective. Not used when the agent is predicting all outputs.

        Returns:
            Controls for the vehicle on the CARLA simulator.

        """
        # only required if using corruptions module
        # self.corruption_number = kwargs.get('corruption_number', None)
        # self.severity = kwargs.get('severity', None)

        # Take the forward speed and normalize it for it to go from 0-1
        norm_speed = measurements.player_measurements.forward_speed / g_conf.SPEED_FACTOR
        norm_speed = torch.cuda.FloatTensor([norm_speed]).unsqueeze(0)
        directions_tensor = torch.cuda.LongTensor([directions])
        # Compute the forward pass processing the sensors got from CARLA.
        model_outputs = self._model.forward_branch(self._process_sensors(sensor_data), norm_speed,
                                                 directions_tensor)
        # run forward pass using felipe model
        # model_outputs_felipe = self._model_felipe.forward_branch(self._process_sensors(sensor_data), norm_speed,
        #                                           directions_tensor)
        
        # model_outputs[0] = torch.FloatTensor([(model_outputs[0][i].item()+model_outputs_felipe[0][i].item())/2.0 for i in range(3)]).cuda()
        steer, throttle, brake = self._process_model_outputs(model_outputs[0])
        # steer_f, throttle_f, brake_f = self._process_model_outputs(model_outputs_felipe[0])

        # ensemble
        '''
        steer_c = []
        throttle_c = []
        brake_c = []
        for i in range(len(self.model_ids)):
            mo = self._ensemble_model_list[i].forward_branch(self._process_sensors(sensor_data), norm_speed,
                                                  directions_tensor)
            s, t, b = self._process_model_outputs(mo[0])
            steer_c.append(s)
            throttle_c.append(t)
            brake_c.append(b)
        '''
        if self._carla_version == '0.9':
            import carla
            control = carla.VehicleControl()
        else:
            control = VehicleControl()
        # single model
        control.steer = float(steer)
        control.throttle = float(throttle)
        control.brake = float(brake)

        # ensemble
        # control.steer = float(np.average(steer_c, weights=self.weights))
        # control.throttle = float(np.average(throttle_c, weights=self.weights))
        # control.brake = float(np.average(brake_c, weights=self.weights))

        # There is the posibility to replace some of the predictions with oracle predictions.
        if g_conf.USE_ORACLE:
            control.steer, control.throttle, control.brake = self._get_oracle_prediction(
                measurements, sensor_data, target)

        if self.first_iter:
            coil_logger.add_message('Iterating', {"Checkpoint": self.checkpoint['iteration'],
                                                  'Agent': str(control.steer)},
                                    self.checkpoint['iteration'])
        self.first_iter = False

        return control
Example #10
0
    def run_step(self, measurements, sensor_data, directions, target):
        control = VehicleControl()
        control.throttle = 0.9

        return control