from navrep.tools.commonargs import parse_common_args from navrep.envs.e2eenv import E2EIANEnv from navrep.scripts.cross_test_navreptrain_in_ianenv import run_test_episodes from navrep.scripts.test_e2e import E2ECPolicy if __name__ == '__main__': args, _ = parse_common_args() if args.n is None: args.n = 1000 collect_trajectories = False env = E2EIANEnv(silent=True, collect_trajectories=collect_trajectories) policy = E2ECPolicy() S = run_test_episodes(env, policy, render=args.render, num_episodes=args.n) DIR = os.path.expanduser("~/navrep/eval/crosstest") if args.dry_run: DIR = "/tmp/navrep/eval/crosstest" make_dir_if_not_exists(DIR) if collect_trajectories: NAME = "e2enavreptrain_in_ianenv_{}.pckl".format(len(S)) PATH = os.path.join(DIR, NAME) S.to_pickle(PATH) else: NAME = "e2enavreptrain_in_ianenv_{}.csv".format(len(S)) PATH = os.path.join(DIR, NAME) S.to_csv(PATH) print("{} written.".format(PATH))
model_path = os.path.expanduser( "~/navrep/models/M/navreptrainrnn{}.json".format(VAE_TYPE)) vae_model_path = os.path.expanduser( "~/navrep/models/V/navreptrainvae{}.json".format(VAE_TYPE)) if common_args.dry_run: log_path = log_path.replace(os.path.expanduser("~/navrep"), "/tmp/navrep") log_hyperparams_path = log_hyperparams_path.replace( os.path.expanduser("~/navrep"), "/tmp/navrep") model_path = model_path.replace(os.path.expanduser("~/navrep"), "/tmp/navrep") model_hyperparams_path = model_hyperparams_path.replace( os.path.expanduser("~/navrep"), "/tmp/navrep") make_dir_if_not_exists(os.path.dirname(model_path)) make_dir_if_not_exists(os.path.dirname(log_path)) # load preprocessed data files = [] for dirpath, dirnames, filenames in os.walk(dataset_folder): for filename in [f for f in filenames if f.endswith(".npz")]: files.append(os.path.join(dirpath, filename)) all_data = [] for path in files: arrays = np.load(path) all_data.append([ arrays["mus"], arrays["logvars"], arrays["robotstates"], arrays["actions"],
for d in dones_ss: d[-1] = True dones = np.concatenate(dones_ss, axis=0) obs = rings_def["lidar_to_rings"](scans).astype( float) / rings_def["rings_to_bool"] mus = [] logvars = [] batch_size = 100 for i in range(0, len(obs), batch_size): mu, logvar = vae.encode_mu_logvar(obs[i:i + batch_size, :, :, :]) z = mu + np.exp(logvar / 2.0) * np.random.randn(*logvar.shape) mus.extend(mu) logvars.extend(logvar) mus = np.array(mus).astype(np.float32) logvars = np.array(logvars).astype(np.float32) savepath = path.replace(V_dataset_folder, M_dataset_folder).replace("scans", "mus_logvars") make_dir_if_not_exists(os.path.dirname(savepath)) np.savez_compressed( savepath, mus=mus, logvars=logvars, robotstates=robotstates, rewards=rewards, actions=actions, dones=dones, ) print("z saved to {}".format(savepath))
data_regen = "navreptrain" log_path = os.path.expanduser( "~/navrep/logs/W/navreptrain_vaelstm_train_log_{}.csv".format( START_TIME)) checkpoint_path = os.path.expanduser( "~/navrep/models/W/navreptrainvaelstm") else: raise NotImplementedError(args.environment) if args.dry_run: log_path = log_path.replace(os.path.expanduser("~/navrep"), "/tmp/navrep") checkpoint_path = checkpoint_path.replace( os.path.expanduser("~/navrep"), "/tmp/navrep") make_dir_if_not_exists(os.path.dirname(checkpoint_path)) make_dir_if_not_exists(os.path.dirname(log_path)) make_dir_if_not_exists(os.path.expanduser("~/tmp_navrep")) # make deterministic set_seed(42) # set up logging logging.basicConfig( format="%(asctime)s - %(levelname)s - %(name)s - %(message)s", datefmt="%m/%d/%Y %H:%M:%S", level=logging.INFO, ) logger = logging.getLogger(__name__) mconf = VAELSTMConfig(_Z, _H)
def generate_vae_dataset( env, n_sequences, episode_length=1000, subset_index=0, n_subsets=1, render=True, policy=RandomMomentumPolicy(), archive_dir=os.path.expanduser("~/navrep/datasets/V/ian")): """ if n_subsets is None, the whole set of sequences is generated (n_sequences) if n_subsets is a number > 1, this function only generates a portion of the sequences """ indices = np.arange(n_sequences) if n_subsets > 1: # when multiprocessing indices = np.array_split(indices, n_subsets)[subset_index] for n in indices: scans = [] robotstates = [] actions = [] rewards = [] dones = [] policy.reset() obs = env.reset() for i in range(episode_length): # step action = policy.predict(obs, env) if isinstance(action, Suicide): obs = env.reset() rew = 0 action = np.array([0, 0, 0]) done = True else: obs, rew, done, _ = env.step(action) scans.append(obs[0]) robotstates.append(obs[1]) actions.append(action) rewards.append(rew) dones.append(done) if render: env.render() if done: policy.reset() obs = env.reset() print("{} - {} {}".format(n, i, "done" if done else " "), end="\r") dones[-1] = True scans = np.array(scans) robotstates = np.array(robotstates) actions = np.array(actions) rewards = np.array(rewards) dones = np.array(dones) data = dict(scans=scans, robotstates=robotstates, actions=actions, rewards=rewards, dones=dones) if archive_dir is not None: make_dir_if_not_exists(archive_dir) archive_path = os.path.join( archive_dir, "{:03}_scans_robotstates_actions_rewards_dones.npz".format(n)) np.savez_compressed(archive_path, **data) print(archive_path, "written.") env.close() return data