def main(params): save_dir = Path(params.dataset_path) save_dir.mkdir(parents=True, exist_ok=True) total = 0 for i in tqdm.tqdm(range(params.n_episodes), desc='Episode'): with make_suite('FullTown01-v1', port=params.port, planner=params.planner) as env: filepath = save_dir.joinpath('%03d' % i) if filepath.exists(): continue data = None while data is None: data = get_episode(env, params) lmdb_env = lmdb.open(str(filepath), map_size=int(1e10)) n = len(data) with lmdb_env.begin(write=True) as txn: txn.put('len'.encode(), str(n).encode()) for i, x in enumerate(data): txn.put(('rgb_%04d' % i).encode(), np.ascontiguousarray(x['rgb']).astype(np.uint8)) txn.put( ('depth_%04d' % i).encode(), np.ascontiguousarray(x['depth']).astype(np.float32)) txn.put(('segmentation_%04d' % i).encode(), np.ascontiguousarray(x['segmentation']).astype( np.uint8)) txn.put( ('birdview_%04d' % i).encode(), np.ascontiguousarray(x['birdview']).astype(np.uint8)) txn.put(('measurements_%04d' % i).encode(), np.ascontiguousarray(x['measurements']).astype( np.float32)) txn.put( ('control_%04d' % i).encode(), np.ascontiguousarray(x['control']).astype(np.float32)) total += len(data) print('Total frames: %d' % total)
def run(model_path, port, suite, big_cam, seed, autopilot, resume, max_run=10, show=False): log_dir = model_path.parent config = bzu.load_json(str(log_dir / 'config.json')) total_time = 0.0 for suite_name in get_suites(suite): tick = time.time() import pdb pdb.set_trace() benchmark_dir = log_dir / 'benchmark' / model_path.stem / ( '%s_seed%d' % (suite_name, seed)) benchmark_dir.mkdir(parents=True, exist_ok=True) with make_suite(suite_name, port=port, big_cam=big_cam) as env: agent_maker = _agent_factory_hack(model_path, config, autopilot) run_benchmark(agent_maker, env, benchmark_dir, seed, autopilot, resume, max_run=max_run, show=show) elapsed = time.time() - tick total_time += elapsed print('%s: %.3f hours.' % (suite_name, elapsed / 3600)) print('Total time: %.3f hours.' % (total_time / 3600))
def run(args, model_path, port, suite, seed, resume, max_run): log_dir = model_path total_time = 0.0 for suite_name in get_suites(suite): tick = time.time() benchmark_dir = log_dir / "benchmark" / ("%s_seed%d" % (suite_name, seed)) benchmark_dir.mkdir(parents=True, exist_ok=True) with make_suite(suite_name, port=port, crop_sky=args.crop_sky) as env: from bird_view.models import agent_IAs_RL agent_class = agent_IAs_RL.AgentIAsRL agent_maker = lambda: agent_class(args) run_benchmark(agent_maker, env, benchmark_dir, seed, resume, max_run=max_run) elapsed = time.time() - tick total_time += elapsed print("%s: %.3f hours." % (suite_name, elapsed / 3600)) print("Total time: %.3f hours." % (total_time / 3600))
def rollout(replay_buffer, coord_converter, net, teacher_net, episode, image_agent_kwargs=dict(), birdview_agent_kwargs=dict(), episode_length=1000, n_vehicles=100, n_pedestrians=250, port=2000, planner="new"): from models.image import ImageAgent from models.birdview import BirdViewAgent decay = np.array([0.7**i for i in range(5)]) xy_bias = np.array([0.7, 0.3]) weathers = list(cu.TRAIN_WEATHERS.keys()) def _get_weight(a, b): loss_weight = np.mean((np.abs(a - b) * xy_bias).sum(axis=-1) * decay, axis=-1) x_weight = np.maximum( np.mean(a[..., 0], axis=-1), np.mean(a[..., 0] * -1.4, axis=-1), ) return loss_weight num_data = 0 progress = tqdm.tqdm(range(episode_length * len(weathers)), desc='Frame') for weather in weathers: data = list() while len(data) < episode_length: with make_suite('FullTown01-v1', port=port, planner=planner) as env: start, target = env.pose_tasks[np.random.randint( len(env.pose_tasks))] env_params = { 'weather': weather, 'start': start, 'target': target, 'n_pedestrians': n_pedestrians, 'n_vehicles': n_vehicles, } env.init(**env_params) env.success_dist = 5.0 image_agent_kwargs['model'] = net birdview_agent_kwargs['model'] = teacher_net image_agent = ImageAgent(**image_agent_kwargs) birdview_agent = BirdViewAgent(**birdview_agent_kwargs) while not env.is_success() and not env.collided: env.tick() observations = env.get_observations() image_control, _image_points = image_agent.run_step( observations, teaching=True) _image_points = coord_converter(_image_points) image_points = _image_points / (0.5 * CROP_SIZE) - 1 birdview_control, birdview_points = birdview_agent.run_step( observations, teaching=True) weight = _get_weight(birdview_points, image_points) control = get_control(image_control, birdview_control, episode) env.apply_control(control) data.append({ 'rgb_img': observations["rgb"].copy(), 'cmd': int(observations["command"]), 'speed': np.linalg.norm(observations["velocity"]), 'target': birdview_points, 'weight': weight, 'birdview_img': crop_birdview(observations['birdview'], dx=-10), }) progress.update(1) # DEBUG if len(data) >= episode_length: break # DEBUG END print("Collided: ", env.collided) print("Success: ", env.is_success()) if env.collided: data = data[:-5] for datum in data: replay_buffer.add_data(**datum) num_data += 1
def rollout(net, image_agent_kwargs=dict(), episode_length=1000, n_vehicles=100, n_pedestrians=250, port=2000, planner="new", seed=2020): from models.image import ImageAgent num_data = 0 weathers = list(cu.TRAIN_WEATHERS.keys()) fit = 0 tot_t = 0 for weather in weathers: data = list() with make_suite('FullTown01-v1', port=port, planner=planner) as env: start, target = env.pose_tasks[np.random.randint( len(env.pose_tasks))] env_params = { 'weather': weather, 'start': start, 'target': target, 'n_pedestrians': n_pedestrians, 'n_vehicles': n_vehicles, } # seems hacky, taken from https://github.com/dianchen96/LearningByCheating/blob/release-0.9.6/benchmark/run_benchmark.py#L167 env.seed = seed env.init(**env_params) # success distance threshold from target env.success_dist = 10.0 image_agent_kwargs['model'] = net image_agent = ImageAgent(**image_agent_kwargs) coll_actors = None route_comp = 0 red_count = -1 stop_count = -1 time_step = 0 # TODO: just using (https://carlachallenge.org/challenge/) as temp sol # i.e. terminating only if critical infraction as defined above # also keeping a timeout episode_length st_t = time.time() while True: if env.is_success() or time_step >= episode_length: break env.tick() coll_actors = env.collision_actors route_comp = env.route_completed red_count = env.red_count stop_count = env.stop_count if time_step % 100 == 0: print('weather {}'.format(weather)) print('time steps so far {}'.format(time_step)) print('route completed so far {}'.format(route_comp)) print('coll actors so far {}'.format(coll_actors)) print('red count so far {}'.format(red_count)) print('stop count so far {}'.format(stop_count)) observations = env.get_observations() control = image_agent.run_step(observations) diagnostic = env.apply_control(control) time_step += 1 stop_pen = 0.8**stop_count red_pen = 0.7**red_count coll_stat_pen = 1. coll_veh_pen = 1. coll_ped_pen = 1. for c in coll_actors: if c == TrafficEventType.COLLISION_STATIC: coll_stat_pen *= 0.65 elif c == TrafficEventType.COLLISION_VEHICLE: coll_veh_pen *= 0.6 elif c == TrafficEventType.COLLISION_PEDESTRIAN: coll_ped_pen *= 0.5 # computing driving score of episode infraction_pen = stop_pen * red_pen * (coll_stat_pen * coll_veh_pen * coll_ped_pen) score = route_comp * infraction_pen end_t = time.time() print('========= Episode Stats (Weather {}) ========='.format( weather)) print('Total time: {}'.format(time_step)) print('Route Completion Percentage: {}'.format(route_comp)) print('Collided into: {}'.format(coll_actors)) print('Number of Red Lights run: {}'.format(red_count)) print('Number of Stop Signs run: {}'.format(stop_count)) print('Driving Score: {}'.format(score)) print('Epsiode Time (real-time, seconds): {}'.format(end_t - st_t)) print('=================================') fit += score tot_t += (end_t - st_t) print('Total Fitness: {}'.format(fit)) print('Total Time (s): {}'.format(tot_t)) return fit
def env_rollout(weather, start, target, n_pedestrians, n_vehicles, net, image_agent_kwargs, ImageAgent, suite_name, port, planner, env_seed, valuation_file_path, indiv_idx, episode_length): # Instantiate environment "env" through make_suite (in benchmark.__init__.py) # Note: make_suite provides a wrapper to launch and access the CARLA client and server; # magic functions, performs __init__, __enter__, __exit__ are executed in this order inside the "with" loop with make_suite(suite_name, port=port, planner=planner, env_seed=env_seed) as env: # Set environment parameters and initialize environment env_params = { 'weather': weather, 'start': start, 'target': target, 'n_pedestrians': n_pedestrians, 'n_vehicles': n_vehicles, } # env.seed = env_seed # Initialize environment env.init(**env_params) # success distance threshold from target env.success_dist = 10.0 image_agent_kwargs['model'] = net image_agent = ImageAgent(**image_agent_kwargs) coll_actors = None route_comp = 0 red_count = -1 stop_count = -1 time_step = 0 # TODO: just using (https://carlachallenge.org/challenge/) as temp sol # i.e. terminating only if critical infraction as defined above # also keeping a timeout episode_length st_t = time.time() while not env.is_success() and not time_step >= episode_length: env.tick() coll_actors = env.collision_actors route_comp = env.route_completed red_count = env.red_count stop_count = env.stop_count # if time_step % 100 == 0: # print('weather {}'.format(weather)) # print('time steps so far {}'.format(time_step)) # print('route completed so far {}'.format(route_comp)) # print('coll actors so far {}'.format(coll_actors)) # print('red count so far {}'.format(red_count)) # print('stop count so far {}'.format(stop_count)) observations = env.get_observations() control = image_agent.run_step(observations) env.apply_control(control) time_step += 1 stop_pen = 0.8**stop_count red_pen = 0.7**red_count coll_stat_pen = 1. coll_veh_pen = 1. coll_ped_pen = 1. for c in coll_actors: if c == TrafficEventType.COLLISION_STATIC: coll_stat_pen *= 0.65 elif c == TrafficEventType.COLLISION_VEHICLE: coll_veh_pen *= 0.6 elif c == TrafficEventType.COLLISION_PEDESTRIAN: coll_ped_pen *= 0.5 # computing driving score of episode infraction_pen = stop_pen * red_pen * (coll_stat_pen * coll_veh_pen * coll_ped_pen) score = route_comp * infraction_pen end_t = time.time() print('========= Episode Stats (Weather {}) ========='.format(weather)) print('Total time: {}'.format(time_step)) print('Route Completion Percentage: {}'.format(route_comp)) print('Collided into: {}'.format(coll_actors)) print('Number of Red Lights run: {}'.format(red_count)) print('Number of Stop Signs run: {}'.format(stop_count)) print('Driving Score: {}'.format(score)) print('Episode Time (real-time, seconds): {}'.format(end_t - st_t)) print('=================================') # Save weather valuation details to valuation file with open(valuation_file_path, 'a+') as file: file.write( 'Indiv:{} Weather:{} Total_time:{} Route_Comp:{} Collisions:{} Red_lights:{} Stop_signs:{} Score:{}' ' Ep_time:{} St_t:{} End_t:{} Start-Target_poses:{}\n'.format( indiv_idx, weather, time_step, route_comp, coll_actors, red_count, stop_count, score, end_t - st_t, st_t, end_t, (start, target))) file.close() rollout_time = (end_t - st_t) # env.terminate_criterion_test() return score, rollout_time