Exemplo n.º 1
0
def reset_episode(client, carla_game, settings_module, show_render):
    """
    Reset the episode to a random configuration from the permissible settings

    Args:
        client: carla client object
        carla_game: for visualization
        settings_module: permissible configuration values
        show_render: for visualization

    Returns:
        episode_characteristics: configuration for the current episode
    """

    random_pose = random.choice(settings_module.POSITIONS)
    town_name, player_start_spots, weather, number_of_vehicles, number_of_pedestrians, \
        seeds_vehicles, seeds_pedestrians = new_episode(client,
                                                        settings_module.make_carla_settings(),
                                                        random_pose[0],
                                                        settings_module.NumberOfVehicles,
                                                        settings_module.NumberOfPedestrians,
                                                        settings_module.set_of_weathers)

    # Here when verbose is activated we also show the rendering window.
    carla_game.initialize_game(town_name, render_mode=show_render)
    carla_game.start_timer()

    # An extra planner is needed in order to calculate timeouts
    planner = Planner(town_name)

    carla_game.set_objective(player_start_spots[random_pose[1]])

    player_target_transform = player_start_spots[random_pose[1]]

    last_episode_time = time.time()

    timeout = calculate_timeout(player_start_spots[random_pose[0]],
                                player_target_transform, planner)
    episode_characteristics = {
        "town_name": town_name,
        "player_target_transform": player_target_transform,
        "last_episode_time": last_episode_time,
        "timeout": timeout,
        "weather": weather,
        "number_of_vehicles": number_of_vehicles,
        "number_of_pedestrians": number_of_pedestrians,
        "seeds_vehicles": seeds_vehicles,
        "seeds_pedestrians": seeds_pedestrians,
        "start_pose": random_pose[0],
        "end_pose": random_pose[1]
    }

    return episode_characteristics
Exemplo n.º 2
0
def reset_episode(client, carla_game, settings_module, pos_selector,
                  show_render):

    weather = random.choice(settings_module.set_of_weathers)
    print(weather)
    random_pose = pos_selector.get_pose(weather)
    town_name, player_start_spots, number_of_vehicles, number_of_pedestrians, \
        seeds_vehicles, seeds_pedestrians = new_episode(client,
                                                        settings_module.make_carla_settings(),
                                                        random_pose[0],
                                                        settings_module.NumberOfVehicles,
                                                        settings_module.NumberOfPedestrians,
                                                        weather)

    # Here when verbose is activated we also show the rendering window.
    carla_game.initialize_game(town_name, render_mode=show_render)
    carla_game.start_timer()

    # An extra planner is needed in order to calculate timeouts
    planner = Planner(town_name)

    carla_game.set_objective(player_start_spots[random_pose[1]])

    player_target_transform = player_start_spots[random_pose[1]]

    last_episode_time = time.time()

    timeout = calculate_timeout(player_start_spots[random_pose[0]],
                                player_target_transform, planner)

    episode_characteristics = {
        "town_name": town_name,
        "player_start_transform": player_start_spots[random_pose[0]],
        "player_target_transform": player_target_transform,
        "last_episode_time": last_episode_time,
        "timeout": timeout,
        "weather": weather,
        "pose": random_pose,
        "number_of_vehicles": number_of_vehicles,
        "number_of_pedestrians": number_of_pedestrians,
        "seeds_vehicles": seeds_vehicles,
        "seeds_pedestrians": seeds_pedestrians,
    }

    return episode_characteristics
Exemplo n.º 3
0
def collect(client, args):
    """
    The main loop for the data collection process.
    Args:
        client: carla client object
        args: arguments passed on the data collection main.
    Returns:
        None
    """
    # Here we instantiate a sample carla settings. The name of the configuration should be
    # passed as a parameter.
    settings_module = __import__('dataset_configurations.' +
                                 (args.data_configuration_name),
                                 fromlist=['dataset_configurations'])

    # Suppress output to some logfile, that is useful when running a massive number of collectors
    if not args.verbose:
        suppress_logs(args.episode_number)

    # Instatiate the carlagame debug screen. This is basically a interface to visualize
    # the oracle data collection process
    carla_game = CarlaGame(False, args.debug, WINDOW_WIDTH, WINDOW_HEIGHT,
                           MINI_WINDOW_WIDTH, MINI_WINDOW_HEIGHT)

    # The collision checker , checks for collision at any moment.
    collision_checker = CollisionChecker()
    lane_checker = LaneChecker()
    ##### Start the episode #####
    random_episode = True
    # ! This returns all the aspects from the episodes.
    episode_aspects = reset_episode(client, carla_game, settings_module,
                                    args.debug, random_episode, {},
                                    args.episode_number)
    planner = Planner(episode_aspects["town_name"])
    # We instantiate the agent, depending on the parameter
    controlling_agent = make_controlling_agent(args,
                                               episode_aspects["town_name"])

    # The noise object to add noise to some episodes is instanced
    longitudinal_noiser = Noiser('Throttle',
                                 frequency=15,
                                 intensity=10,
                                 min_noise_time_amount=2.0)
    lateral_noiser = Noiser('Spike',
                            frequency=50,
                            intensity=3,
                            min_noise_time_amount=2.0)

    episode_lateral_noise, episode_longitudinal_noise = check_episode_has_noise(
        args.episode_number, settings_module, True)
    episode_aspects.update({
        'episode_lateral_noise':
        episode_lateral_noise,
        'episode_longitudinal_noise':
        episode_longitudinal_noise
    })
    if ENABLE_WRITER:
        ##### DATASET writer initialization #####
        # here we make the full path for the dataset that is going to be created.
        # Make dataset path
        writer.make_dataset_path(args.data_path)
        # We start by writing the  metadata for the entire data collection process.
        # That basically involves writing the configuration that was set on the settings module.
        writer.add_metadata(args.data_path, settings_module)
        # Also write the metadata for the current episode

    # We start the episode number with the one set as parameter
    episode_number = args.episode_number
    enable_autopilot = False
    autopilot_counter = 0
    noisy_episode = True
    image_count = 0
    # The maximum episode is equal to the current episode plus the number of episodes you
    # want to run
    maximun_episode = int(args.number_of_episodes) + int(args.episode_number)
    try:
        while carla_game.is_running() and episode_number < maximun_episode:
            try:
                # we add the vehicle and the connection outside of the game.
                measurements, sensor_data = client.read_data()

                # run a step for the agent. regardless of the type

                control, controller_state = controlling_agent.run_step(
                    measurements, sensor_data, [],
                    episode_aspects['player_target_transform'],
                    enable_autopilot)

                # Get the directions, also important to save those for future training
                # Check if we made infraction here last time
                if image_count in episode_aspects['expert_points']:
                    autopilot_counter = 0
                    enable_autopilot = False  # Turning off for now
                    print(" Enabling Autopilot ")
                # We are in trouble some state enable autopilot
                if enable_autopilot:
                    autopilot_counter += 1
                    if autopilot_counter > FRAMES_GIVEN_TO_ORACLE:
                        enable_autopilot = False
                        print("Disabling Autopilot")
                directions = get_directions(
                    measurements, episode_aspects['player_target_transform'],
                    planner)
                controller_state.update({'directions': directions})

                # if this is a noisy episode, add noise to the controls
                # if autopilot is ON  curb all noises
                #TODO add a function here.
                if episode_longitudinal_noise and noisy_episode:
                    control_noise, _, _ = longitudinal_noiser.compute_noise(
                        control,
                        measurements.player_measurements.forward_speed * 3.6)
                else:
                    control_noise = control

                if episode_lateral_noise and noisy_episode:
                    control_noise_f, _, _ = lateral_noiser.compute_noise(
                        control_noise,
                        measurements.player_measurements.forward_speed * 3.6)
                else:
                    control_noise_f = control_noise

                # Set the player position
                # if you want to debug also render everything
                if args.debug:
                    objects_to_render = controller_state.copy()
                    objects_to_render[
                        'player_transform'] = measurements.player_measurements.transform
                    objects_to_render[
                        'agents'] = measurements.non_player_agents
                    objects_to_render[
                        "draw_pedestrians"] = args.draw_pedestrians
                    objects_to_render["draw_vehicles"] = args.draw_vehicles
                    objects_to_render[
                        "draw_traffic_lights"] = args.draw_traffic_lights
                    # Comment the following two lines to see the waypoints and routes.
                    objects_to_render['waypoints'] = None
                    objects_to_render['route'] = None

                    # Render with the provided map
                    carla_game.render(sensor_data['CameraRGB'],
                                      objects_to_render)

                # Check two important conditions for the episode, if it has ended
                # and if the episode was a success
                collided = collision_checker.test_collision(
                    measurements.player_measurements)
                lane_crossed = lane_checker.test_lane_crossing(
                    measurements.player_measurements)
                episode_ended =  collided or lane_crossed  or \
                                carla_game.is_reset(measurements.player_measurements.transform.location) #or controller_state['traffic_light_crossed_on_red']
                episode_success = not (
                    collided or lane_crossed
                )  # or controller_state['traffic_light_crossed_on_red']

                # Check if there is collision
                # Start a new episode if there is a collision but repeat the same by not incrementing
                # episode number.

                if episode_ended:
                    if episode_success:
                        episode_aspects.update({
                            "time_taken":
                            measurements.game_timestamp / 1000.0
                        })
                        if ENABLE_WRITER:
                            #if WRITE_H5:
                            '''fileCounter = writer.get_file_counter()
                                                                                                                if fileCounter:
                                                                                                                    data_point_id = (image_count - NUMBER_OF_FRAMES_CAR_FLIES) % (FILE_SIZE * fileCounter)
                                                                                                                else:
                                                                                                                    data_point_id = (image_count - NUMBER_OF_FRAMES_CAR_FLIES)
                                                                                                                writer.writeh5(args.data_path , str(episode_number).zfill(5) , data_point_id)'''
                            writer.add_episode_metadata(
                                args.data_path,
                                str(episode_number).zfill(5), episode_aspects)
                        episode_number += 1
                        random_episode = True
                        noisy_episode = True
                    else:
                        random_episode = False
                        episode_aspects['expert_points'].append(
                            image_count - FRAMES_TO_REWIND)
                        noisy_episode = not noisy_episode
                        if ENABLE_WRITER:
                            writer.delete_episode(args.data_path,
                                                  str(episode_number).zfill(5))
                        if len(
                                episode_aspects['expert_points']
                        ) >= MAX_EXPERT_TAKEOVERS:  # if we repeated the same episode for n times skip it
                            random_episode = True
                            episode_number += 1

                    if ENABLE_WRITER:
                        writer.reset_file_counter()
                    episode_lateral_noise, episode_longitudinal_noise = check_episode_has_noise(
                        episode_number, settings_module, noisy_episode)
                    # We reset the episode and receive all the characteristics of this episode.
                    episode_aspects = reset_episode(client, carla_game,
                                                    settings_module,
                                                    args.debug, random_episode,
                                                    episode_aspects,
                                                    episode_number)
                    controlling_agent.param_controller['target_speed'] = 30
                    episode_aspects.update({
                        'episode_lateral_noise':
                        episode_lateral_noise,
                        'episode_longitudinal_noise':
                        episode_longitudinal_noise
                    })

                    # Reset the image count
                    image_count = 0
                if ENABLE_WRITER:
                    # We do this to avoid the frames that the car is coming from the sky.
                    if image_count >= NUMBER_OF_FRAMES_CAR_FLIES and not args.not_record:
                        #if WRITE_H5:
                        writer.add_data_point(
                            measurements, control, control_noise_f,
                            sensor_data, controller_state, args.data_path,
                            str(episode_number).zfill(5),
                            image_count - NUMBER_OF_FRAMES_CAR_FLIES,
                            settings_module.sensors_frequency)
                        #else:
                        #    writer.add_data_point(measurements, control, control_noise_f, sensor_data,
                        #                      controller_state,
                        #                      args.data_path, str(episode_number).zfill(5),
                        #                      str(image_count - NUMBER_OF_FRAMES_CAR_FLIES),
                        #                      settings_module.sensors_frequency)
                # End the loop by sending control
                client.send_control(control_noise_f)
                # Add one more image to the counting
                image_count += 1

            except TCPConnectionError as error:
                """
                If there is any connection error we delete the current episode, 
                This avoid incomplete episodes
                """
                import traceback
                traceback.print_exc()

                if not args.not_record and ENABLE_WRITER:
                    writer.delete_episode(args.data_path,
                                          str(episode_number).zfill(5))

                time.sleep(10)

    except KeyboardInterrupt:
        import traceback
        traceback.print_exc()
        if not args.not_record and ENABLE_WRITER:
            writer.delete_episode(args.data_path, str(episode_number).zfill(5))
Exemplo n.º 4
0
def reset_episode(client, carla_game, settings_module, show_render,
                  random_episode, episode_aspects, episode_number):
    '''
    The episode is randomized if no collision happened at last episode
    otherwise a repeat of the last episode is initialized
    '''

    if random_episode:
        while episode_number >= len(settings_module.POSITIONS):
            episode_number = episode_number % len(settings_module.POSITIONS)
        pose = settings_module.POSITIONS[episode_number]
        # Every time the seeds for the episode are different
        number_of_vehicles = random.randint(
            settings_module.NumberOfVehicles[0],
            settings_module.NumberOfVehicles[1])
        number_of_pedestrians = random.randint(
            settings_module.NumberOfPedestrians[0],
            settings_module.NumberOfPedestrians[1])
        weather = random.choice(settings_module.set_of_weathers)
        town_name, player_start_spots, weather, number_of_vehicles, number_of_pedestrians, \
            seeds_vehicles, seeds_pedestrians = new_episode(client,
                                                            settings_module.make_carla_settings(),
                                                            pose[0],
                                                            number_of_vehicles,
                                                            number_of_pedestrians,
                                                            weather)

        # Here when verbose is activated we also show the rendering window.
        carla_game.initialize_game(town_name, render_mode=show_render)
        carla_game.start_timer()

        # An extra planner is needed in order to calculate timeouts
        planner = Planner(town_name)

        carla_game.set_objective(player_start_spots[pose[1]])

        player_target_transform = player_start_spots[pose[1]]

        last_episode_time = time.time()

        timeout = calculate_timeout(player_start_spots[pose[0]],
                                    player_target_transform, planner)
        expert_points = []

    else:
        pose = episode_aspects['episode_points']
        number_of_vehicles = episode_aspects['number_of_vehicles']
        number_of_pedestrians = episode_aspects['number_of_pedestrians']
        weather = episode_aspects['weather']
        town_name, player_start_spots, weather, number_of_vehicles, number_of_pedestrians, \
            seeds_vehicles, seeds_pedestrians = new_episode(client,
                                                            settings_module.make_carla_settings(episode_aspects["seeds_vehicles"] ,episode_aspects["seeds_pedestrians"] ),
                                                            pose[0],
                                                            number_of_vehicles,
                                                            number_of_pedestrians,
                                                            weather)
        carla_game.initialize_game(town_name, render_mode=show_render)
        carla_game.start_timer()
        # An extra planner is needed in order to calculate timeouts
        planner = Planner(town_name)

        carla_game.set_objective(player_start_spots[pose[1]])

        player_target_transform = player_start_spots[pose[1]]

        last_episode_time = time.time()

        timeout = calculate_timeout(player_start_spots[pose[0]],
                                    player_target_transform, planner)
        expert_points = episode_aspects['expert_points']
        expert_points.sort()

    episode_characteristics = {
        "town_name": town_name,
        "episode_points": pose,
        "player_target_transform": player_target_transform,
        "last_episode_time": last_episode_time,
        "timeout": timeout,
        "weather": weather,
        "number_of_vehicles": number_of_vehicles,
        "number_of_pedestrians": number_of_pedestrians,
        "seeds_vehicles": seeds_vehicles,
        "seeds_pedestrians": seeds_pedestrians,
        "expert_points": expert_points
    }
    return episode_characteristics
Exemplo n.º 5
0
def collect(client, args):
    """
    The main loop for the data collection process.
    Args:
        client: carla client object
        args: arguments passed on the data collection main.

    Returns:
        None

    """
    # Here we instantiate a sample carla settings. The name of the configuration should be
    # passed as a parameter.
    settings_module = __import__('dataset_configurations.' +
                                 (args.data_configuration_name),
                                 fromlist=['dataset_configurations'])

    # Suppress output to some logfile, that is useful when running a massive number of collectors
    if not args.verbose:
        suppress_logs(args.episode_number)

    # Instatiate the carlagame debug screen. This is basically a interface to visualize
    # the oracle data collection process
    carla_game = CarlaGame(False, args.debug, WINDOW_WIDTH, WINDOW_HEIGHT,
                           MINI_WINDOW_WIDTH, MINI_WINDOW_HEIGHT)

    # The collision checker , checks for collision at any moment.
    collision_checker = CollisionChecker()

    ##### Start the episode #####
    # ! This returns all the aspects from the episodes.
    episode_aspects = reset_episode(client, carla_game, settings_module,
                                    args.debug)
    planner = Planner(episode_aspects["town_name"])
    # We instantiate the agent, depending on the parameter
    controlling_agent = make_controlling_agent(args,
                                               episode_aspects["town_name"])

    # The noise object to add noise to some episodes is instanced
    longitudinal_noiser = Noiser('Throttle',
                                 frequency=15,
                                 intensity=10,
                                 min_noise_time_amount=2.0)
    lateral_noiser = Noiser('Spike',
                            frequency=25,
                            intensity=4,
                            min_noise_time_amount=0.5)

    episode_lateral_noise, episode_longitudinal_noise = check_episode_has_noise(
        settings_module.lat_noise_percent, settings_module.long_noise_percent)

    ##### DATASET writer initialization #####
    # here we make the full path for the dataset that is going to be created.
    # Make dataset path
    writer.make_dataset_path(args.data_path)
    # We start by writing the  metadata for the entire data collection process.
    # That basically involves writing the configuration that was set on the settings module.
    writer.add_metadata(args.data_path, settings_module)
    # Also write the metadata for the current episode
    writer.add_episode_metadata(args.data_path,
                                str(args.episode_number).zfill(5),
                                episode_aspects)

    # We start the episode number with the one set as parameter
    episode_number = args.episode_number
    try:
        image_count = 0
        # The maximum episode is equal to the current episode plus the number of episodes you
        # want to run
        maximun_episode = int(args.number_of_episodes) + int(
            args.episode_number)
        while carla_game.is_running() and episode_number < maximun_episode:

            # we add the vehicle and the connection outside of the game.
            measurements, sensor_data = client.read_data()

            # run a step for the agent. regardless of the type
            control, controller_state = controlling_agent.run_step(
                measurements, sensor_data, [],
                episode_aspects['player_target_transform'])
            # Get the directions, also important to save those for future training

            directions = get_directions(
                measurements, episode_aspects['player_target_transform'],
                planner)

            controller_state.update({'directions': directions})

            # if this is a noisy episode, add noise to the controls
            #TODO add a function here.
            if episode_longitudinal_noise:
                control_noise, _, _ = longitudinal_noiser.compute_noise(
                    control,
                    measurements.player_measurements.forward_speed * 3.6)
            else:
                control_noise = control

            if episode_lateral_noise:
                control_noise_f, _, _ = lateral_noiser.compute_noise(
                    control_noise,
                    measurements.player_measurements.forward_speed * 3.6)
            else:
                control_noise_f = control_noise

            # Set the player position
            # if you want to debug also render everything
            if args.debug:
                objects_to_render = controller_state.copy()
                objects_to_render[
                    'player_transform'] = measurements.player_measurements.transform
                objects_to_render['agents'] = measurements.non_player_agents
                objects_to_render["draw_pedestrians"] = args.draw_pedestrians
                objects_to_render["draw_vehicles"] = args.draw_vehicles
                objects_to_render[
                    "draw_traffic_lights"] = args.draw_traffic_lights
                # Comment the following two lines to see the waypoints and routes.
                objects_to_render['waypoints'] = None
                objects_to_render['route'] = None

                # Render with the provided map
                carla_game.render(sensor_data['CameraRGB'], objects_to_render)

            # Check two important conditions for the episode, if it has ended
            # and if the episode was a success
            episode_ended = collision_checker.test_collision(measurements.player_measurements) or \
                            reach_timeout(measurements.game_timestamp / 1000.0,
                                          episode_aspects["timeout"]) or \
                            carla_game.is_reset(measurements.player_measurements.transform.location)
            episode_success = not (collision_checker.test_collision(
                measurements.player_measurements) or reach_timeout(
                    measurements.game_timestamp / 1000.0,
                    episode_aspects["timeout"]))

            # Check if there is collision
            # Start a new episode if there is a collision but repeat the same by not incrementing
            # episode number.

            if episode_ended:
                if episode_success:
                    episode_number += 1
                else:
                    # If the episode did go well and we were recording, delete this episode
                    if not args.not_record:
                        writer.delete_episode(args.data_path,
                                              str(episode_number - 1).zfill(5))

                episode_lateral_noise, episode_longitudinal_noise = check_episode_has_noise(
                    settings_module.lat_noise_percent,
                    settings_module.long_noise_percent)

                # We reset the episode and receive all the characteristics of this episode.
                episode_aspects = reset_episode(client, carla_game,
                                                settings_module, args.debug)

                writer.add_episode_metadata(args.data_path,
                                            str(episode_number).zfill(5),
                                            episode_aspects)

                # Reset the image count
                image_count = 0

            # We do this to avoid the frames that the car is coming from the sky.
            if image_count >= NUMBER_OF_FRAMES_CAR_FLIES and not args.not_record:
                writer.add_data_point(
                    measurements, control, control_noise_f, sensor_data,
                    controller_state, args.data_path,
                    str(episode_number).zfill(5),
                    str(image_count - NUMBER_OF_FRAMES_CAR_FLIES),
                    settings_module.sensors_frequency)
            # End the loop by sending control
            client.send_control(control_noise_f)
            # Add one more image to the counting
            image_count += 1

    except TCPConnectionError as error:
        """
        If there is any connection error we delete the current episode, 
        This avoid incomplete episodes
        """
        import traceback
        traceback.print_exc()
        if not args.not_record:
            writer.delete_episode(args.data_path, str(episode_number).zfill(5))

        raise error

    except KeyboardInterrupt:
        import traceback
        traceback.print_exc()
        if not args.not_record:
            writer.delete_episode(args.data_path, str(episode_number).zfill(5))