def train(output_file, params_file, cur_ind, model_path, config): from phase2_utils import ( ReplayBuffer, load_image_model, ) # load pre-trained model net = load_image_model(config['model_args']['backbone'], model_path, device=config['device']) # load CMAES parameters, cur_id is the population member id i.e. [0, pop_size-1] params = np.load(params_file)['params'][cur_ind] # overwrite weights di = net.state_dict() st = 0 for i in range(4): w_key = 'location_pred.{}.1.weight'.format(i) b_key = 'location_pred.{}.1.bias'.format(i) w_shape = di[w_key].shape w_num = np.prod(w_shape) b_shape = di[b_key].shape b_num = np.prod(b_shape) w = params[st:st + w_num] b = params[st + w_num:st + w_num + b_num] st += (w_num + b_num) reshaped_w = np.reshape(w, w_shape) di[w_key] = torch.from_numpy(reshaped_w) di[b_key] = torch.from_numpy(b) net.load_state_dict(di) image_agent_kwargs = {'camera_args': config["agent_args"]['camera_args']} ret = rollout(net, image_agent_kwargs=image_agent_kwargs, port=config['port'], seed=config['seed']) all_dists = [] total_fit = ret all_dists.append(total_fit) avg_dist = np.mean(all_dists) print('Writing average distance..') with open(output_file, 'w+') as w: w.write(str(float(total_fit))) with open('%s.rew' % output_file, 'w') as w: for dist in all_dists: w.write('%f\n' % dist)
def train(config): import utils.bz_utils as bzu bzu.log.init(config['log_dir']) bzu.log.save_config(config) teacher_config = bzu.log.load_config(config['teacher_args']['model_path']) from phase2_utils import (CoordConverter, ReplayBuffer, LocationLoss, load_birdview_model, load_image_model, get_optimizer) criterion = LocationLoss() net = load_image_model(config['model_args']['backbone'], config['phase1_ckpt'], device=config['device']) teacher_net = load_birdview_model(teacher_config['model_args']['backbone'], config['teacher_args']['model_path'], device=config['device']) image_agent_kwargs = {'camera_args': config["agent_args"]['camera_args']} coord_converter = CoordConverter(**config["agent_args"]['camera_args']) replay_buffer = ReplayBuffer(**config["buffer_args"]) # optimizer = get_optimizer(net.parameters(), config["optimizer_args"]["lr"]) for episode in tqdm.tqdm(range(config['max_episode']), desc='Episode'): rollout(replay_buffer, coord_converter, net, teacher_net, episode, image_agent_kwargs=image_agent_kwargs, port=config['port']) # import pdb; pdb.set_trace() _train(replay_buffer, net, teacher_net, criterion, coord_converter, bzu.log, config, episode)
def train(result_file_path, valuation_file_path, params_file, gen, indiv_idx, model_path, config, carla_process): from phase2_utils import ( ReplayBuffer, load_image_model, ) # load pre-trained model net = load_image_model(config['model_args']['backbone'], model_path, device=config['device']) # load CMAES parameters, indiv_idx is the population member id i.e. [0, pop_size-1] params = np.load(params_file)['params'][indiv_idx] # overwrite weights di = net.state_dict() st = 0 for i in range(4): w_key = 'location_pred.{}.1.weight'.format(i) b_key = 'location_pred.{}.1.bias'.format(i) w_shape = di[w_key].shape w_num = np.prod(w_shape) b_shape = di[b_key].shape b_num = np.prod(b_shape) w = params[st:st + w_num] b = params[st + w_num:st + w_num + b_num] st += (w_num + b_num) reshaped_w = np.reshape(w, w_shape) di[w_key] = torch.from_numpy(reshaped_w) di[b_key] = torch.from_numpy(b) net.load_state_dict(di) image_agent_kwargs = {'camera_args': config["agent_args"]['camera_args']} ret = rollout(net, valuation_file_path, indiv_idx, episode_length=1000, image_agent_kwargs=image_agent_kwargs, port=config['port'], env_seed=config['env_seed']) all_dists = [] total_fit = ret all_dists.append(total_fit) avg_dist = np.mean(all_dists) # write to results files by appending new line using format: generation individual_ID score \n print('Writing to results files..') with open(result_file_path, 'a+') as file: file.write( str(int(gen)) + ' ' + str(int(indiv_idx)) + ' ' + str(float(total_fit)) + '\n') file.close() with open('%s.rew' % result_file_path, 'a+') as file: for dist in all_dists: file.write('%f %f %f\n' % (gen, indiv_idx, dist)) file.close()