def load_models(model_path=save_path, in_size=len(input_columns), out_size=len(output_columns) - 1 if cost_mode == 'RL-MDN' else len(output_columns), hidden_size=hidden_size, num_recurrent_layers=num_recurrent_layers, model=layer_models[0]): initials = [] if not os.path.isfile(model_path): print 'Could not find model file.' sys.exit(0) print 'Loading model from {0}...'.format(model_path) x = tensor.tensor3('features', dtype=theano.config.floatX) y = tensor.tensor3('targets', dtype='floatX') train_flag = [theano.shared(0)] _, latent_size = load_encoder() in_size = latent_size + len(input_columns) y_hat, cost, cells = nn_fprop(x, y, in_size, out_size, hidden_size, num_recurrent_layers, train_flag) main_loop = MainLoop(algorithm=None, data_stream=None, model=Model(cost), extensions=[saveload.Load(model_path)]) for extension in main_loop.extensions: extension.main_loop = main_loop main_loop._run_extensions('before_training') bin_model = main_loop.model print 'Model loaded. Building prediction function...' hiddens = [] for i in range(num_recurrent_layers): brick = [ b for b in bin_model.get_top_bricks() if b.name == layer_models[i] + str(i) ][0] hiddens.extend( VariableFilter(theano_name=brick.name + '_apply_states')( bin_model.variables)) hiddens.extend( VariableFilter(theano_name=brick.name + '_apply_cells')(cells)) initials.extend( VariableFilter(roles=[roles.INITIAL_STATE])(brick.parameters)) predict_func = theano.function([x], hiddens + [y_hat]) encoder, code_size = load_encoder() return predict_func, initials, encoder, code_size
def load_data(): X_train = pd.read_csv(os.path.join('data', 'X_train.csv')) y_train = pd.read_csv(os.path.join('data', 'y_train.csv')).values.ravel() X_test = pd.read_csv(os.path.join('data', 'X_test.csv')) y_test = pd.read_csv(os.path.join('data', 'y_test.csv')).values.ravel() data = pd.concat([X_train, X_test]) data['label'] = pd.Series(np.concatenate((y_train, y_test)), index= data.index) return data, load_encoder()
def getConvFeatures(data_in, img_data): n_batch = 100 encoder, code_size = load_encoder() img_features = np.empty((len(data_in), code_size), dtype=theano.config.floatX) for i in xrange(len(data_in) / n_batch + 1): sys.stdout.write('\r' + str(i) + '/' + str(len(data_in) / n_batch)) sys.stdout.flush() # important start = i * n_batch end = min((i + 1) * n_batch, len(data_in)) images = img_data[start:end] _, img_features[start:end] = encode_image(images, encoder) data_in = np.column_stack((img_features, data_in)) return data_in, data_in.shape[1]
def load_models() -> Tuple[Generator, Encoder, networks.UnetGenerator]: """ Load the generative models Returns: Tuple[Generator, Encoder, networks.UnetGenerator] -- the DCGAN generator, its respective encoder and the Pix2Pix model """ generator = utils.load_generator(args.dcgan_latent_size, args.dcgan_num_filters, join(args.models_save_dir, 'generator')) encoder = utils.load_encoder(args.dcgan_latent_size, args.dcgan_num_filters, join(args.models_save_dir, 'encoder')) pix2pix = load_pix2pix( join(args.models_save_dir, args.dataset_name + "_pix2pix")) return generator, encoder, pix2pix
def final_test(): model = clone(base_model) X_train = pd.read_csv(os.path.join('data', 'X_train.csv')) y_train = pd.read_csv(os.path.join('data', 'y_train.csv')).values.ravel() X_test = pd.read_csv(os.path.join('data', 'X_test.csv')) y_test = pd.read_csv(os.path.join('data', 'y_test.csv')).values.ravel() if(baseline): print('Applying baseline correction...') for idx, row in X_train.iterrows(): X_train.iloc[idx, :] = row - als(row) for idx, row in X_test.iterrows(): X_test.iloc[idx, :] = row - als(row) if(scaler): std = StandardScaler() X_train = std.fit_transform(X_train) X_test = std.transform(X_test) if(pc): pca = PCA(n_components=0.99, random_state = seed) X_train = pca.fit_transform(X_train) X_test = pca.transform(X_test) if(over_sample): ros = RandomOverSampler(random_state = seed) X_train, y_train = ros.fit_resample(X_train, y_train) model.fit(X_train, y_train) total_scores = [log_loss(y_test, model.predict_proba(X_test)), accuracy_score(np.array(y_test), model.predict(X_test)) ] return _results( [build_row(X_test, y_test, model.predict(X_test))], #detailed score [total_scores], 'final_test', load_encoder()), total_scores
# Load config parameters locals().update(config) # DATA train_stream = get_stream(hdf5_file, 'train', batch_size) test_stream = get_stream(hdf5_file, 'test', batch_size) # MODEL x = T.TensorType('floatX', [False] * 3)('features') y = T.tensor3('targets', dtype='floatX') train_flag = [theano.shared(0)] x = x.swapaxes(0, 1) y = y.swapaxes(0, 1) out_size = len(output_columns) - 1 if cost_mode == 'RL-MDN' else len( output_columns) _, latent_size = load_encoder() in_size = latent_size + len(input_columns) # mean = x[:,:,0:latent_size] # var = T.clip(T.exp(x[:,:,latent_size:latent_size*2]), .0001, 1000) # rrng = MRG_RandomStreams(seed) # rand = rrng.normal(var.shape, 0, 1, dtype=theano.config.floatX) # x = ifelse(T.lt(train_flag[0], .5), T.concatenate([mean , x[:,:,latent_size*2:]], axis=2) , T.concatenate([mean + (var * rand), x[:,:,latent_size*2:]], axis=2)) y_hat, cost, cells = nn_fprop(x, y, in_size, out_size, hidden_size, num_recurrent_layers, train_flag) # COST cg = ComputationGraph(cost) extra_updates = [] # Learning optimizer if training_optimizer == 'Adam':
def generate_pix2pix_dataset(generator_params, encoder_params, input_dataset_path='shoes_images', dcgan_image_size=64, pix2pix_image_size=128, output_path='pix2pix/datasets/details_dataset'): phases = ['train', 'test'] out_A_path = join(output_path, 'A') out_B_path = join(output_path, 'B') out_AB_path = join(output_path, 'AB') for path in (out_A_path, out_B_path, out_AB_path): if not exists(path): mkdir(path) for phase in phases: if not exists(join(path, phase)): mkdir(join(path, phase)) # useful transforms transform = transforms.Compose([ transforms.Resize((dcgan_image_size, dcgan_image_size), Image.LANCZOS), transforms.ToTensor(), transforms.Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5)) ]) interpolate = lambda x: F.interpolate( x, scale_factor=pix2pix_image_size / dcgan_image_size, mode='bilinear') # Load DCGAN models: G = load_generator(**generator_params) E = load_encoder(**encoder_params) # Iterate on images images_list = get_jpg_images(input_dataset_path) random.Random(5).shuffle( images_list ) # shuffle dataset with a constant seed (5) for consistency phase_cutoffs = [0.95 * len(images_list), len(images_list)] cur_phase = 0 for i, image_file in tqdm(enumerate(images_list)): if i > phase_cutoffs[cur_phase]: cur_phase += 1 with Image.open(image_file) as image: in_image = transform(image.convert("RGB")).cuda() if tuple(in_image.shape[-3:]) != (3, dcgan_image_size, dcgan_image_size): print( f"WARNING! Unexpected input size: {in_image.shape} in file {image_file}. Skipping..." ) continue B_image = image.resize((pix2pix_image_size, pix2pix_image_size), Image.BILINEAR) B_image.save( join(out_B_path, phases[cur_phase], basename(image_file)[:-3] + "png")) generated_image = G( E(in_image.reshape(1, 3, dcgan_image_size, dcgan_image_size))) upsampled = interpolate(generated_image) fixed_point = np.uint8(np.round( 255 * denorm(upsampled).cpu().numpy()))[0, ...] fixed_point = np.transpose(fixed_point, (1, 2, 0)) A_image = Image.fromarray(fixed_point) A_image.save(out_A_path + '/' + phases[cur_phase] + '/' + basename(image_file)[:-3] + "png") w, h = A_image.size AB_image = Image.new("RGB", (2 * w, h)) AB_image.paste(A_image, (0, 0)) AB_image.paste(B_image, (w, 0)) AB_image.save( join(out_AB_path, phases[cur_phase], basename(image_file)[:-3] + "png"))
def transform(name): le = utils.load_encoder() return le.transform([name])[0]
def dqn_family(conf, outdir): env_id = conf['env'] world = conf['world'] logging_level = conf['logging_level'] interactor = conf['interactor'] downstream_task = conf['downstream_task'] seed = conf['seed'] gpu = conf['gpu'] demo = conf['demo'] monitor = conf['monitor'] load = conf['load'] eval_n_runs = conf['eval_n_runs'] sampling = conf['sampling'] agent_type = conf['agent'] arch = conf['arch'] max_episode_steps = conf['max_episode_steps'] batch_size = conf['batch_size'] update_interval = conf['update_interval'] frame_skip = conf['frame_skip'] gamma = conf['gamma'] clip_delta = conf['clip_delta'] num_step_return = conf['num_step_return'] lr = conf['lr'] adam_eps = conf['adam_eps'] batch_accumulator = conf['batch_accumulator'] gray_scale = conf['gray_scale'] frame_stack = conf['frame_stack'] final_exploration_frames = conf['final_exploration_frames'] final_epsilon = conf['final_epsilon'] eval_epsilon = conf['eval_epsilon'] noisy_net_sigma = conf['noisy_net_sigma'] replay_capacity = conf['replay_capacity'] replay_start_size = conf['replay_start_size'] prioritized = conf['prioritized'] target_update_interval = conf['target_update_interval'] enc_conf = conf['encoder'] data_type = enc_conf['data_type'] world_conf = getConfig('CustomWorlds/' + world) world_conf['downstream_task'] = downstream_task coords = world_conf['coords'] os.environ['MALMO_MINECRAFT_OUTPUT_LOGDIR'] = outdir # Set a random seed used in PFRL. pfrl.utils.set_random_seed(seed) # Set different random seeds for train and test envs. train_seed = seed # noqa: never used in this script test_seed = 2**31 - 1 - seed # Load encoder ##################################### if os.getenv('USER') == 'juanjo': path_weights = Path('./results/') world_conf['path_world'] = Path( '/home/juanjo/Documents/minecraft/mineRL/src/minerl/env/Malmo/Minecraft/run/saves/' ) elif os.getenv('USER') == 'juan.jose.nieto': path_weights = Path( '/home/usuaris/imatge/juan.jose.nieto/mineRL/src/results') world_conf['path_world'] = Path( '/home/usuaris/imatge/juan.jose.nieto/mineRL/src/minerl/env/Malmo/Minecraft/run/saves/' ) else: raise Exception("Sorry, user not identified!") if enc_conf['type'] == 'random': train_encoder = True input_dim = 1024 encoder = None else: train_encoder = False input_dim = enc_conf[enc_conf['type']]['embedding_dim'] * 2 encoder = utils.load_encoder(enc_conf, path_weights) ###################################################### device = torch.device("cuda" if torch.cuda.is_available() else "cpu") # create & wrap env def wrap_env_partial(env, test): randomize_action = test and noisy_net_sigma is None wrapped_env = wrap_env(env=env, test=test, monitor=monitor, outdir=outdir, frame_skip=frame_skip, data_type=data_type, gray_scale=gray_scale, frame_stack=frame_stack, randomize_action=randomize_action, eval_epsilon=eval_epsilon, encoder=encoder, device=device, sampling=sampling, train_encoder=train_encoder, downstream_task=downstream_task, coords=coords) return wrapped_env logger.info( 'The first `gym.make(MineRL*)` may take several minutes. Be patient!') core_env = gym.make(env_id) core_env.custom_update(world_conf) if interactor: core_env.make_interactive(port=6666, realtime=True) # This seed controls which environment will be rendered core_env.seed(0) # training env env = wrap_env_partial(env=core_env, test=False) # env.seed(int(train_seed)) # evaluation env eval_env = wrap_env_partial(env=core_env, test=True) # env.seed(int(test_seed)) # TODO: not supported yet (also requires `core_eval_env = gym.make(args.env)`) # calculate corresponding `steps` and `eval_interval` according to frameskip # 8,000,000 frames = 1333 episodes if we count an episode as 6000 frames, # 8,000,000 frames = 1000 episodes if we count an episode as 8000 frames. maximum_frames = 8000000 if frame_skip is None: steps = maximum_frames eval_interval = 2000 * 20 # (approx.) every 20 episode (counts "1 episode = 2000 steps") else: steps = maximum_frames // frame_skip eval_interval = 2000 * 30 // frame_skip # (approx.) every 100 episode (counts "1 episode = 6000 steps") agent = get_agent(n_actions=4, arch=arch, n_input_channels=env.observation_space.shape[0], noisy_net_sigma=noisy_net_sigma, final_epsilon=final_epsilon, final_exploration_frames=final_exploration_frames, explorer_sample_func=env.action_space.sample, lr=lr, adam_eps=adam_eps, prioritized=prioritized, steps=steps, update_interval=update_interval, replay_capacity=replay_capacity, num_step_return=num_step_return, agent_type=agent_type, gpu=gpu, gamma=gamma, replay_start_size=replay_start_size, target_update_interval=target_update_interval, clip_delta=clip_delta, batch_accumulator=batch_accumulator, batch_size=batch_size, input_dim=input_dim, train_encoder=train_encoder) if load: agent.load(load) print('agent loaded') # experiment if demo: eval_stats = pfrl.experiments.eval_performance( env=eval_env, agent=agent, n_steps=None, max_episode_len=max_episode_steps, n_episodes=eval_n_runs) logger.info('n_runs: {} mean: {} median: {} stdev {}'.format( eval_n_runs, eval_stats['mean'], eval_stats['median'], eval_stats['stdev'])) else: pfrl.experiments.train_agent_with_evaluation( agent=agent, env=env, steps=steps, eval_n_steps=None, eval_n_episodes=eval_n_runs, eval_interval=eval_interval, outdir=outdir, eval_env=eval_env, save_best_so_far_agent=True, use_tensorboard=True) env.close() eval_env.close()