Esempio n. 1
0
    def _initialize_actors(self, config):
        """
        Custom initialization

        static_center_transforms, static_center_transforms, vehicle_center_transforms:
        {i:(x_i, y_i)}
        """
        def spawning_actors_within_bounds(object_type):
            final_generated_transforms = []
            if object_type == 'static':
                object_list = self.static_list
            elif object_type == 'pedestrian':
                object_list = self.pedestrian_list
            elif object_type == 'vehicle':
                object_list = self.vehicle_list

            for i, object_i in enumerate(self.customized_data[object_type +
                                                              '_list']):
                if 'add_center' in self.customized_data and self.customized_data[
                        'add_center']:
                    key = object_type + '_center_transform' + '_' + str(i)
                    if key in self.customized_data:
                        center_transform = self.customized_data[key]
                    else:
                        center_transform = self.customized_data[
                            'center_transform']

                    # hack: only x, y should be added
                    center_transform.location.z = 0
                    center_transform.rotation.pitch = 0
                    center_transform.rotation.yaw = 0
                    center_transform.rotation.roll = 0

                    spawn_transform_i = add_transform(center_transform,
                                                      object_i.spawn_transform)
                    # print(object_type, i, object_i.model, 'add center', object_i.spawn_transform, '->', spawn_transform_i)
                else:
                    spawn_transform_i = object_i.spawn_transform
                    center_transform = None

                if 'parameters_max_bounds' in self.customized_data.keys():
                    bounds = [
                        self.customized_data[k1][object_type + '_' + k2 + '_' +
                                                 str(i)]
                        for k1, k2 in [(
                            'parameters_min_bounds',
                            'x_min'), (
                                'parameters_max_bounds',
                                'x_max'), (
                                    'parameters_min_bounds',
                                    'y_min'), ('parameters_max_bounds',
                                               'y_max')]
                    ]
                    # bounds = []
                else:
                    bounds = []

                if object_type == 'vehicle' and hasattr(
                        object_i, 'color'
                ) and object_i.model != 'vehicle.tesla.cybertruck':
                    color = object_i.color
                else:
                    color = None

                if object_type == 'vehicle':
                    is_waypoint_follower = object_i.waypoint_follower
                else:
                    is_waypoint_follower = None

                if object_type == 'pedestrian':
                    simulation_enabled = False
                else:
                    simulation_enabled = True
                actor, generated_transform = self._request_actor(
                    object_type, object_i.model, spawn_transform_i,
                    simulation_enabled, color, bounds, is_waypoint_follower,
                    center_transform)

                if actor and generated_transform:
                    object_list.append((actor, generated_transform))

                    gx = generated_transform.location.x
                    gy = generated_transform.location.y
                    gyaw = generated_transform.rotation.yaw

                    if 'add_center' in self.customized_data and self.customized_data[
                            'add_center']:
                        cx = center_transform.location.x
                        cy = center_transform.location.y
                    else:
                        cx = 0
                        cy = 0

                    final_generated_transforms.append((gx - cx, gy - cy, gyaw))
                else:
                    final_generated_transforms.append((None, None, None))

            # print(object_type, 'saved final generated transform', final_generated_transforms)
            return final_generated_transforms

        all_final_generated_transforms = OrderedDict()
        for object_type in ['static', 'pedestrian', 'vehicle']:
            final_generated_transforms = spawning_actors_within_bounds(
                object_type)

            all_final_generated_transforms[
                object_type] = final_generated_transforms

        # hack:
        tmp_folder = make_hierarchical_dir(['tmp_folder'])
        filename = os.path.join(tmp_folder,
                                str(config.cur_server_port) + '.pickle')

        # print('filename', '\n'*10, filename, '\n'*10)
        with open(filename, 'wb') as f_out:
            pickle.dump(all_final_generated_transforms, f_out)
Esempio n. 2
0
def main():

    arguments = specify_args()
    arguments.debug = True
    statistics_manager = StatisticsManager()

    # Fixed Hyperparameters
    # if use_actors = False, no other actors will be generated
    use_actors = False
    using_customized_route_and_scenario = True
    multi_actors_scenarios = ['Scenario12']
    arguments.scenarios = 'leaderboard/data/fuzzing_scenarios.json'
    town_name = 'Town03'
    scenario = 'Scenario12'
    direction = 'front'
    route = 0
    # sample_factor is an integer between [1, 5]
    sample_factor = 5
    # waypoints_num_limit: the maximum number of waypoints that we consider to perturb.
    waypoints_num_limit = 10
    # lane_width = 3.5
    max_num_of_vehicle = 2
    max_num_of_pedestrians = 2

    # Parameters to optimize
    # Set up environment parameters
    # real, [0, 1]
    friction = 0.1
    # integer, [0, 20]
    weather_index = 2

    # Laundry Stuff-------------------------------------------------------------
    arguments.weather_index = weather_index
    os.environ['WEATHER_INDEX'] = str(weather_index)

    town_scenario_direction = town_name + '/' + scenario

    folders = [os.environ['SAVE_FOLDER'], town_name, scenario]
    if scenario in multi_actors_scenarios:
        town_scenario_direction += '/' + direction
        folders.append(direction)

    cur_folder_name = make_hierarchical_dir(folders)

    os.environ['SAVE_FOLDER'] = cur_folder_name
    arguments.save_folder = os.environ['SAVE_FOLDER']

    route_prefix = 'leaderboard/data/customized_routes/' + town_scenario_direction + '/route_'

    route_str = str(route)
    if route < 10:
        route_str = '0' + route_str
    arguments.routes = route_prefix + route_str + '.xml'
    os.environ['ROUTES'] = arguments.routes

    # extract waypoints along route
    import xml.etree.ElementTree as ET
    tree = ET.parse(arguments.routes)
    route_waypoints = []

    # this iteration should only go once since we only keep one route per file
    for route in tree.iter("route"):
        route_id = route.attrib['id']
        route_town = route.attrib['town']

        for waypoint in route.iter('waypoint'):
            route_waypoints.append(
                carla.Transform(
                    carla.Location(x=float(waypoint.attrib['x']),
                                   y=float(waypoint.attrib['y']),
                                   z=float(waypoint.attrib['z'])),
                    carla.Rotation(float(waypoint.attrib['pitch']),
                                   float(waypoint.attrib['yaw']),
                                   float(waypoint.attrib['roll']))))

    # extract waypoints for the scenario
    world_annotations = RouteParser.parse_annotations_file(arguments.scenarios)
    info = world_annotations[town_name][0]["available_event_configurations"][0]

    center = info["center"]
    RouteParser.convert_waypoint_float(center)
    center_location = carla.Location(float(center['x']), float(center['y']),
                                     float(center['z']))
    center_rotation = carla.Rotation(float(center['pitch']),
                                     float(center['yaw']), 0.0)
    center_transform = carla.Transform(center_location, center_rotation)
    # --------------------------------------------------------------------------

    if use_actors:
        # Set up actors

        # ego car
        ego_car_waypoints_perturbation = []
        for i in range(waypoints_num_limit):
            dx = np.clip(np.random.normal(0, 2, 1)[0], -0.5, 0.5)
            dy = np.clip(np.random.normal(0, 2, 1)[0], -0.5, 0.5)
            ego_car_waypoints_perturbation.append((dx, dy))

        # static
        static_1_transform = center_transform
        static_1 = Static(model='static.prop.barrel',
                          spawn_transform=static_1_transform)
        static_list = [static_1]

        # pedestrians
        pedestrian_1_transform = create_transform(
            route_waypoints[0].location.x - 2,
            route_waypoints[0].location.y - 8, 0, 0,
            route_waypoints[0].rotation.yaw, 0)
        pedestrian_1 = Pedestrian(model='walker.pedestrian.0001',
                                  spawn_transform=pedestrian_1_transform,
                                  trigger_distance=20,
                                  speed=1.5,
                                  dist_to_travel=6,
                                  after_trigger_behavior='stop')
        pedestrian_list = [pedestrian_1]

        # vehicles
        waypoint_follower = True

        vehicle_1_transform = create_transform(
            route_waypoints[1].location.x, route_waypoints[1].location.y - 5,
            0, 0, route_waypoints[1].rotation.yaw, 0)

        # if waypoint_follower == False
        vehicle_1_dist_to_travel = 5
        vehicle_1_target_direction = carla.Vector3D(x=0.2, y=1, z=0)

        # else
        targeted_waypoint = create_transform(
            route_waypoints[1].location.x, route_waypoints[1].location.y - 40,
            0, 0, route_waypoints[1].rotation.yaw, 0)
        # targeted_waypoint = route_waypoints[-1]

        vehicle_1_waypoints_perturbation = []

        for i in range(waypoints_num_limit):
            dx = np.clip(np.random.normal(0, 2, 1)[0], -0.5, 0.5)
            dy = np.clip(np.random.normal(0, 2, 1)[0], -0.5, 0.5)
            vehicle_1_waypoints_perturbation.append((dx, dy))

        vehicle_1 = Vehicle(
            model='vehicle.audi.a2',
            spawn_transform=vehicle_1_transform,
            avoid_collision=True,
            initial_speed=0,
            trigger_distance=10,
            waypoint_follower=waypoint_follower,
            targeted_waypoint=targeted_waypoint,
            dist_to_travel=vehicle_1_dist_to_travel,
            target_direction=vehicle_1_target_direction,
            targeted_speed=10,
            after_trigger_behavior='stop',
            color='(0, 0, 0)',
            waypoints_perturbation=vehicle_1_waypoints_perturbation)

        vehicle_list = [vehicle_1]
    else:
        static_list = []
        pedestrian_list = []
        vehicle_list = []
        ego_car_waypoints_perturbation = []

    customized_data = {
        'friction': friction,
        'static_list': static_list,
        'pedestrian_list': pedestrian_list,
        'vehicle_list': vehicle_list,
        'center_transform': center_transform,
        'using_customized_route_and_scenario': True,
        'destination': route_waypoints[-1].location,
        'sample_factor': sample_factor,
        'ego_car_waypoints_perturbation': ego_car_waypoints_perturbation
    }

    try:
        leaderboard_evaluator = LeaderboardEvaluator(arguments,
                                                     statistics_manager)
        leaderboard_evaluator.run(arguments, customized_data)

    except Exception as e:
        traceback.print_exc()
    finally:
        del leaderboard_evaluator
    os.mkdir(new_all_folder)


def copyfile_wrapper(original_folder_path, new_folder_path, filename):
    path = os.path.join(original_folder_path, filename)
    new_path = os.path.join(new_folder_path, filename)
    copyfile(path, new_path)


for run_folder_name in os.listdir(all_folder):
    print(run_folder_name)
    run_folder_path = os.path.join(all_folder, run_folder_name)
    if os.path.isdir(run_folder_path):
        parent_new_run_folder_path = os.path.join(new_all_folder,
                                                  run_folder_name)
        new_run_folder_path = make_hierarchical_dir(
            [parent_new_run_folder_path, '0_NPC', '0'])

        # copy metadata json
        copyfile_wrapper(run_folder_path, parent_new_run_folder_path,
                         'metadata.json')
        # copy summary json
        copyfile_wrapper(run_folder_path, new_run_folder_path, 'summary.json')

        # copy extra informative files
        for fn in [
                'driving_log.csv', 'events.txt', 'measurements.csv',
                'measurements_loc.csv'
        ]:
            copyfile_wrapper(run_folder_path, parent_new_run_folder_path, fn)

        folder_name_pair = [('rgb', 'rgb_central'), ('rgb_left', 'rgb_left'),
Esempio n. 4
0
def rerun_simulation(pickle_filename, is_save, rerun_save_folder, ind, scenario_file, ego_car_model='lbc'):
    cur_folder = rerun_save_folder+'/'+str(ind)
    if not os.path.exists(cur_folder):
        os.mkdir(cur_folder)


    is_bug = False

    # parameters preparation
    if ind == 0:
        launch_server = True
    else:
        launch_server = False

    with open(pickle_filename, 'rb') as f_in:
        d = pickle.load(f_in)['info']
        x = d['x']
        x[-1] = port
        waypoints_num_limit = d['waypoints_num_limit']
        max_num_of_static = d['num_of_static_max']
        max_num_of_pedestrians = d['num_of_pedestrians_max']
        max_num_of_vehicles = d['num_of_vehicles_max']
        customized_center_transforms = d['customized_center_transforms']

        if 'parameters_min_bounds' in d:
            parameters_min_bounds = d['parameters_min_bounds']
            parameters_max_bounds = d['parameters_max_bounds']
        else:
            parameters_min_bounds = None
            parameters_max_bounds = None


        episode_max_time = 50
        call_from_dt = d['call_from_dt']
        town_name = d['town_name']
        scenario = d['scenario']
        direction = d['direction']
        route_str = d['route_str']




    folder = '_'.join([town_name, scenario, direction, route_str])

    rerun_folder = make_hierarchical_dir(['rerun', folder])

    # extra
    # hack for now before we saved this field
    route_type = 'town05_right_0'
    # customized_d = customized_bounds_and_distributions[scenario_type]
    route_info = customized_routes[route_type]

    location_list = route_info['location_list']

    parse_route_and_scenario(location_list, town_name, scenario, direction, route_str, scenario_file)




    customized_data = convert_x_to_customized_data(x, waypoints_num_limit, max_num_of_static, max_num_of_pedestrians, max_num_of_vehicles, static_types, pedestrian_types, vehicle_types, vehicle_colors, customized_center_transforms, parameters_min_bounds, parameters_max_bounds)
    objectives, loc, object_type, info, save_path = run_simulation(customized_data, launch_server, episode_max_time, call_from_dt, town_name, scenario, direction, route_str, scenario_file, ego_car_model, rerun=True, rerun_folder=cur_folder)


    if objectives[0] > 0.2 or objectives[4] or objectives[5]:
        is_bug = True


    cur_info = {'x':x, 'objectives':objectives, 'loc':loc, 'object_type':object_type}

    if save_rerun_cur_info:
        with open(os.path.join(cur_folder, str(ind)), 'wb') as f_out:
            pickle.dump(cur_info, f_out)

    # copy data to another place
    if is_save:
        try:
            shutil.copytree(save_path, cur_folder)
        except:
            print('fail to copy from', save_path)

    return is_bug, objectives
Esempio n. 5
0
    return is_bug, objectives


if __name__ == '__main__':


    random.seed(0)
    now = datetime.now()
    time_str = now.strftime("%Y_%m_%d_%H_%M_%S")
    # ['bugs', 'non_bugs']
    data = 'bugs'
    # ['train', 'test', 'all']
    mode = 'test'


    rerun_save_folder = make_hierarchical_dir(['rerun', data, mode, time_str])

    scenario_folder = 'scenario_files'
    if not os.path.exists('scenario_files'):
        os.mkdir(scenario_folder)
    scenario_file = scenario_folder+'/'+'current_scenario_'+time_str+'.json'

    atexit.register(exit_handler, [port], rerun_save_folder, scenario_file)



    # if data == 'bugs':
    #     folder = 'bugs/False/nsga2/Town03/Scenario12/front/00/2020_08_11_20_07_07_used_for_retraining'
    # elif data == 'non_bugs':
    #     folder = 'non_bugs/False/nsga2/Town03/Scenario12/front/00/2020_08_11_20_07_07_used_for_retraining'
    #
Esempio n. 6
0
def rerun_simulation(pickle_filename,
                     is_save,
                     rerun_save_folder,
                     ind,
                     sub_folder_name,
                     scenario_file,
                     ego_car_model='lbc',
                     x=[],
                     record_every_n_step=10):
    is_bug = False

    # parameters preparation
    if ind == 0:
        launch_server = True
    else:
        launch_server = False

    with open(pickle_filename, 'rb') as f_in:
        d = pickle.load(f_in)['info']

        if len(x) == 0:
            x = d['x']
            # TBD: save port separately so we won't need to repetitvely save cur_info in ga_fuzzing
            x[-1] = port

        waypoints_num_limit = d['waypoints_num_limit']
        max_num_of_static = d['num_of_static_max']
        max_num_of_pedestrians = d['num_of_pedestrians_max']
        max_num_of_vehicles = d['num_of_vehicles_max']
        customized_center_transforms = d['customized_center_transforms']

        if 'parameters_min_bounds' in d:
            parameters_min_bounds = d['parameters_min_bounds']
            parameters_max_bounds = d['parameters_max_bounds']
        else:
            parameters_min_bounds = None
            parameters_max_bounds = None

        episode_max_time = 60
        call_from_dt = d['call_from_dt']
        town_name = d['town_name']
        scenario = d['scenario']
        direction = d['direction']
        route_str = d['route_str']
        route_type = d['route_type']

    folder = '_'.join([route_type, scenario, ego_car_model, route_str])
    route_info = customized_routes[route_type]
    location_list = route_info['location_list']
    parse_route_and_scenario(location_list, town_name, scenario, direction,
                             route_str, scenario_file)

    customized_data = convert_x_to_customized_data(
        x, waypoints_num_limit, max_num_of_static, max_num_of_pedestrians,
        max_num_of_vehicles, static_types, pedestrian_types, vehicle_types,
        vehicle_colors, customized_center_transforms, parameters_min_bounds,
        parameters_max_bounds)
    print('x', x)

    objectives, loc, object_type, route_completion, info, save_path = run_simulation(
        customized_data,
        launch_server,
        episode_max_time,
        call_from_dt,
        town_name,
        scenario,
        direction,
        route_str,
        scenario_file,
        ego_car_model,
        rerun=True,
        record_every_n_step=record_every_n_step)
    print('\n' * 10, 'save_path', save_path, '\n' * 10)

    is_bug = int(check_bug(objectives))

    # save data
    if is_save:
        rerun_bugs_folder = make_hierarchical_dir(
            [rerun_save_folder, folder, 'rerun_bugs'])
        rerun_non_bugs_folder = make_hierarchical_dir(
            [rerun_save_folder, folder, 'rerun_non_bugs'])
        print('rerun_bugs_folder', rerun_bugs_folder)
        print('sub_folder_name', sub_folder_name)
        if is_bug:

            print('\n' * 3, 'rerun also causes a bug!!!', '\n' * 3)

            try:
                # use this version to merge into the existing folder
                copy_tree(save_path,
                          os.path.join(rerun_bugs_folder, sub_folder_name))
            except:
                print('fail to copy from', save_path)
                traceback.print_exc()
        else:
            try:
                # use this version to merge into the existing folder
                copy_tree(save_path,
                          os.path.join(rerun_non_bugs_folder, sub_folder_name))
            except:
                print('fail to copy from', save_path)
                traceback.print_exc()

    return is_bug, objectives