Ejemplo n.º 1
0
def test_pusher_slider_keypoint_dataset():
    project_root = get_project_root()
    config_file = os.path.join(project_root, "experiments/02/config.yaml")
    config = load_yaml(config_file)

    config["n_history"] = 1
    config["n_roll"] = 0

    # new dataset loading approach
    episodes = load_episodes_from_config(config)
    action_function = ActionFunctionFactory.function_from_config(config)
    observation_function = ObservationFunctionFactory.function_from_config(
        config)

    dataset = MultiEpisodeDataset(config,
                                  action_function=action_function,
                                  observation_function=observation_function,
                                  episodes=episodes,
                                  phase="train")

    # dataset, config = create_pusher_slider_keypoint_dataset(config=config)

    episode_names = dataset.get_episode_names()
    episode_names.sort()
    episode_name = episode_names[0]
    episode = dataset.episode_dict[episode_name]
    obs_raw = episode.get_observation(0)
    obs_raw['slider']['angle'] = 0

    dataset.observation_function(obs_raw)

    print("20 degrees\n\n\n\n")
    obs_raw['slider']['angle'] = np.deg2rad(90)
    dataset.observation_function(obs_raw)
    quit()

    data = dataset[0]  # test the getitem
    print("type(data)", type(data))
    print("data.keys()", data.keys())

    print(type(data["observations"]))
    print("observations.shape", data["observations"].shape)
    print("actions.shape", data["actions"].shape)

    print("observations", data["observations"])
    print("actions", data["actions"])
Ejemplo n.º 2
0
def eval_dynamics(config,
                  train_dir,
                  eval_dir,
                  state_dict_path=None,
                  keypoint_observation=False,
                  debug=False,
                  render_human=False):

    # set random seed for reproduction
    set_seed(config['train']['random_seed'])

    tee = Tee(os.path.join(eval_dir, 'eval.log'), 'w')

    print(config)

    use_gpu = torch.cuda.is_available()
    '''
    model
    '''
    model_dy = DynaNetMLP(config)

    # print model #params
    print("model #params: %d" % count_trainable_parameters(model_dy))

    if state_dict_path is None:
        if config['eval']['eval_dy_epoch'] == -1:
            state_dict_path = os.path.join(train_dir, 'net_best_dy.pth')
        else:
            state_dict_path = os.path.join(
                train_dir, 'net_dy_epoch_%d_iter_%d.pth' % \
                (config['eval']['eval_dy_epoch'], config['eval']['eval_dy_iter']))

        print("Loading saved ckp from %s" % state_dict_path)

    model_dy.load_state_dict(torch.load(state_dict_path))
    model_dy.eval()

    if use_gpu:
        model_dy.cuda()

    criterionMSE = nn.MSELoss()
    bar = ProgressBar()

    st_idx = config['eval']['eval_st_idx']
    ed_idx = config['eval']['eval_ed_idx']

    # load the data
    episodes = load_episodes_from_config(config)

    # generate action/observation functions
    action_function = ActionFunctionFactory.function_from_config(config)
    observation_function = ObservationFunctionFactory.function_from_config(
        config)

    dataset = MultiEpisodeDataset(config,
                                  action_function=action_function,
                                  observation_function=observation_function,
                                  episodes=episodes,
                                  phase="valid")

    episode_names = dataset.get_episode_names()
    episode_names.sort()

    num_episodes = None
    # for backwards compatibility
    if "num_episodes" in config["eval"]:
        num_episodes = config["eval"]["num_episodes"]
    else:
        num_episodes = 10

    episode_list = []
    if debug:
        episode_list = [episode_names[0]]
    else:
        episode_list = episode_names[:num_episodes]

    for roll_idx, episode_name in enumerate(episode_list):
        print("episode_name", episode_name)
        if keypoint_observation:
            eval_episode_keypoint_observations(config,
                                               dataset,
                                               episode_name,
                                               roll_idx,
                                               model_dy,
                                               eval_dir,
                                               start_idx=9,
                                               n_prediction=30,
                                               render_human=render_human)
        else:
            eval_episode(config,
                         dataset,
                         episode_name,
                         roll_idx,
                         model_dy,
                         eval_dir,
                         start_idx=9,
                         n_prediction=30,
                         render_human=render_human)
from key_dynam.utils import dev_utils
from key_dynam.dataset.episode_dataset import MultiEpisodeDataset
from key_dynam.dataset.function_factory import ObservationFunctionFactory, ActionFunctionFactory

multi_episode_dict = dev_utils.load_drake_pusher_slider_episodes()
config = dev_utils.load_simple_config()

action_function = ActionFunctionFactory.function_from_config(config)
observation_function = ObservationFunctionFactory.function_from_config(config)
dataset = MultiEpisodeDataset(config,
                              action_function=action_function,
                              observation_function=observation_function,
                              episodes=multi_episode_dict,
                              phase="train")

episode_name = dataset.get_episode_names()[0]
episode = dataset.episode_dict[episode_name]
idx = 5

data = dataset._getitem(
    episode,
    idx,
    rollout_length=5,
    n_history=2,
    visual_observation=False,
)
print("\n\ndata.keys()", data.keys())

data_w_vision = dataset._getitem(
    episode,
    idx,