Exemplo n.º 1
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))
Exemplo n.º 2
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.º 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

    """
    logging.info("collecting data")
    # 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
    logging.info("Get Carla Game")
    carla_game = CarlaGame(False, args.debug, WINDOW_WIDTH, WINDOW_HEIGHT,
                           MINI_WINDOW_WIDTH, MINI_WINDOW_HEIGHT)

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

    # The noise object to add noise to some episodes is instanced
    logging.info("Add noiser")
    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
    logging.info("write initialization")
    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.
    logging.info("add metadata")
    writer.add_metadata(args.data_path, settings_module)
    logging.info("add episode metadata")
    # 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
    logging.info("We start the episode number with the one set as parameter")
    episode_number = args.episode_number
    vehicle = episode_aspects['player_vehicle']
    # The collision checker , checks for collision at any moment.
    # logging.info("Get Collision checker")
    collision_checker = CollisionChecker(
        episode_aspects['player_vehicle_class'].get_collision_sensor())
    try:
        world = client.get_world()
        settings = world.get_settings()
        settings.synchronous_mode = True
        world.apply_settings(settings)
    except:
        traceback.print_exc()
    #TODO: add screens using screen manager
    pygame.init()
    screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
    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:
            # logging.info("Alive count: {0}".format(image_count))
            # we add the vehicle and the connection outside of the game.
            # TODO: need to change client.read_data() to 0.9.x interface
            # measurements, sensor_data = client.read_data()
            transform = vehicle.get_transform()
            velocity = vehicle.get_velocity()
            # transform.location
            # velocity.
            # TODO: apply other controlling agent
            # # 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'])
            # logging.info("get run step")
            control = controlling_agent.run_step()
            # logging.info(control)
            # # Get the directions, also important to save those for future training
            #
            # directions = get_directions(transform, episode_aspects['player_target_transform'], planner)
            #
            # controller_state.update({'directions': directions})
            #
            # # if this is a noisy episode, add noise to the controls
            # logging.info("adding noise")
            if episode_longitudinal_noise:
                control_noise, _, _ = longitudinal_noiser.compute_noise(
                    control, velocity.x * 3.6)
            else:
                control_noise = control

            if episode_lateral_noise:
                control_noise_f, _, _ = lateral_noiser.compute_noise(
                    control_noise, velocity.x * 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
            # TODO: implement collision_check behavior
            is_collided = collision_checker.test_collision()

            episode_ended = is_collided  # or \
            #                 reach_timeout(measurements.game_timestamp / 1000.0,
            #                               episode_aspects["timeout"]) or \
            #                 carla_game.is_reset(measurements.player_measurements.transform.location)
            episode_success = not is_collided  # 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:
                logging.info("the episode is ended.")
                world = client.get_world()
                actors = world.get_actors()
                for actor in actors:
                    if actor is not None:
                        if actor.type_id.startswith('controller.ai.walker'):
                            actor.stop()
                            actor.destroy()
                            # https://carla.readthedocs.io/en/latest/core_actors/
                            # To destroy AI pedestrians, stop the AI controller and destroy both, the actor, and the controller.
                episode_aspects['player_vehicle_class'].destroy()
                del collision_checker
                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.
            # actors = world.get_actors()
            # for actor in actors:
            #     actor.destroy()
                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
                vehicle = episode_aspects['player_vehicle']
                collision_checker = CollisionChecker(
                    episode_aspects['player_vehicle_class'].
                    get_collision_sensor())
            #
            # # 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)
            # logging.info("applying vehicle control")
            vehicle.apply_control(control_noise_f)
            # Add one more image to the counting
            image_count += 1
            world.tick()

    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))