Esempio n. 1
0
def _write_map_files(b_in, b_out, transform):
  cats = get_categories()

  env = utils.Foo(padding=10, resolution=5, num_point_threshold=2,
                  valid_min=-10, valid_max=200, n_samples_per_face=200)
  robot = utils.Foo(radius=15, base=10, height=140, sensor_height=120,
                    camera_elevation_degree=-15)

  building_loader = factory.get_dataset('sbpd')
  for flip in [False, True]:
    b = nav_env.Building(b_out, robot, env, flip=flip,
                         building_loader=building_loader)
    logging.info("building_in: %s, building_out: %s, transform: %d", b_in,
                 b_out, transform)
    maps = _get_semantic_maps(b_in, transform, b.map, flip, cats)
    maps = np.transpose(np.array(maps), axes=[1,2,0])

    #  Load file from the cache.
    file_name = '{:s}_{:d}_{:d}_{:d}_{:d}_{:d}_{:d}.pkl'
    file_name = file_name.format(b.building_name, b.map.size[0], b.map.size[1],
                                 b.map.origin[0], b.map.origin[1],
                                 b.map.resolution, flip)
    out_file = os.path.join(DATA_DIR, 'processing', 'class-maps', file_name)
    logging.info('Writing semantic maps to %s.', out_file)
    save_variables(out_file, [maps, cats], ['maps', 'cats'], overwrite=True)
def get_default_cmp_args():
  batch_norm_param = {'center': True, 'scale': True,
                      'activation_fn':tf.nn.relu}

  mapper_arch_args = utils.Foo(
      dim_reduce_neurons=64,
      fc_neurons=[1024, 1024],
      fc_out_size=8,
      fc_out_neurons=64,
      encoder='resnet_v2_50',
      deconv_neurons=[64, 32, 16, 8, 4, 2],
      deconv_strides=[2, 2, 2, 2, 2, 2],
      deconv_layers_per_block=2,
      deconv_kernel_size=4,
      fc_dropout=0.5,
      combine_type='wt_avg_logits',
      batch_norm_param=batch_norm_param)

  readout_maps_arch_args = utils.Foo(
      num_neurons=[],
      strides=[],
      kernel_size=None,
      layers_per_block=None)

  arch_args = utils.Foo(
      vin_val_neurons=8, vin_action_neurons=8, vin_ks=3, vin_share_wts=False,
      pred_neurons=[64, 64], pred_batch_norm_param=batch_norm_param,
      conv_on_value_map=0, fr_neurons=16, fr_ver='v2', fr_inside_neurons=64,
      fr_stride=1, crop_remove_each=30, value_crop_size=4,
      action_sample_type='sample', action_sample_combine_type='one_or_other',
      sample_gt_prob_type='inverse_sigmoid_decay', dagger_sample_bn_false=True,
      vin_num_iters=36, isd_k=750., use_agent_loc=False, multi_scale=True,
      readout_maps=False, rom_arch=readout_maps_arch_args)

  return arch_args, mapper_arch_args
def get_default_args():
  summary_args = utils.Foo(display_interval=1, test_iters=26,
                           arop_full_summary_iters=14)

  control_args = utils.Foo(train=False, test=False,
                           force_batchnorm_is_training_at_test=False,
                           reset_rng_seed=False, only_eval_when_done=False,
                           test_mode=None)
  return summary_args, control_args
Esempio n. 4
0
def get_solver_vars(solver_str):
  if solver_str == '': vals = []; 
  else: vals = solver_str.split('_')
  ks = ['clip', 'dlw', 'long', 'typ', 'isdk', 'adam_eps', 'init_lr'];
  ks = ks[:len(vals)]

  # Gradient clipping or not.
  if len(vals) == 0: ks.append('clip'); vals.append('noclip');
  # data loss weight.
  if len(vals) == 1: ks.append('dlw');  vals.append('dlw20')
  # how long to train for.
  if len(vals) == 2: ks.append('long');  vals.append('nolong')
  # Adam
  if len(vals) == 3: ks.append('typ');  vals.append('adam2')
  # reg loss wt
  if len(vals) == 4: ks.append('rlw');  vals.append('rlw1')
  # isd_k
  if len(vals) == 5: ks.append('isdk');  vals.append('isdk415') # 415, inflexion at 2.5k.
  # adam eps
  if len(vals) == 6: ks.append('adam_eps');  vals.append('aeps1en8')
  # init lr
  if len(vals) == 7: ks.append('init_lr');  vals.append('lr1en3')

  assert(len(vals) == 8)
  
  vars = utils.Foo()
  for k, v in zip(ks, vals):
    setattr(vars, k, v)
  LOGGING.error('solver_vars: %s', vars)
  return vars
def process_arch_projected_map(args, arch_vars):
  # Single scale vision based system which does not use a mapper but instead
  # uses an analytically estimated map.
  ds = int(arch_vars.var3[2])
  args.navtask.task_params.input_type = 'analytical_counts'
  args.navtask.task_params.outputs.analytical_counts = True

  assert(args.navtask.task_params.modalities[0] == 'depth')
  args.navtask.camera_param.img_channels = None

  analytical_counts = utils.Foo(map_sizes=[512/ds],
                                xy_resolution=[5.*ds],
                                z_bins=[[-10, 10, 150, 200]],
                                non_linearity=[arch_vars.var2])
  args.navtask.task_params.analytical_counts = analytical_counts

  sc = 1./ds
  args.arch.vin_num_iters = 36
  args.navtask.task_params.map_scales = [sc]
  args.navtask.task_params.map_crop_sizes = [512/ds]

  args.arch.fr_stride = [1,2]
  args.arch.vin_action_neurons = 8
  args.arch.vin_val_neurons = 3
  args.arch.fr_inside_neurons = 32

  map_channels = len(analytical_counts.z_bins[0]) + 1
  args.navtask.task_params.map_channels = map_channels
  args.solver.freeze_conv = False

  return args
def get_camera_matrix(width, height, fov):
    """Returns a camera matrix from image size and fov."""
    xc = (width - 1.) / 2.
    zc = (height - 1.) / 2.
    f = (width / 2.) / np.tan(np.deg2rad(fov / 2.))
    camera_matrix = utils.Foo(xc=xc, zc=zc, f=f)
    return camera_matrix
def get_args_for_config(config_name):
  args = utils.Foo()

  args.summary, args.control = get_default_args()

  exp_name, mode_str = config_name.split('+')
  arch_str, solver_str, navtask_str = exp_name.split('.')
  logging.error('config_name: %s', config_name)
  logging.error('arch_str: %s', arch_str)
  logging.error('navtask_str: %s', navtask_str)
  logging.error('solver_str: %s', solver_str)
  logging.error('mode_str: %s', mode_str)

  args.solver = cc.process_solver_str(solver_str)
  args.navtask = cc.process_navtask_str(navtask_str)

  args = process_arch_str(args, arch_str)
  args.arch.isd_k = args.solver.isd_k

  # Train, test, etc.
  mode, imset = mode_str.split('_')
  args = cc.adjust_args_for_mode(args, mode)
  args.navtask.building_names = args.navtask.dataset.get_split(imset)
  args.control.test_name = '{:s}_on_{:s}'.format(mode, imset)

  # Log the arguments
  logging.error('%s', args)
  return args
def get_default_baseline_args():
    batch_norm_param = {
        'center': True,
        'scale': True,
        'activation_fn': tf.nn.relu
    }
    arch_args = utils.Foo(pred_neurons=[],
                          goal_embed_neurons=[],
                          img_embed_neurons=[],
                          batch_norm_param=batch_norm_param,
                          dim_reduce_neurons=64,
                          combine_type='',
                          encoder='resnet_v2_50',
                          action_sample_type='sample',
                          action_sample_combine_type='one_or_other',
                          sample_gt_prob_type='inverse_sigmoid_decay',
                          dagger_sample_bn_false=True,
                          isd_k=750.,
                          use_visit_count=False,
                          lstm_output=False,
                          lstm_ego=False,
                          lstm_img=False,
                          fc_dropout=0.0,
                          embed_goal_for_state=False,
                          lstm_output_init_state_from_goal=False)
    return arch_args
Esempio n. 9
0
    def __init__(self, env_name, env_kwargs, trainer_kwargs, other_kwargs,
                 logdir):
        self.env_name = env_name
        self.env_kwargs = env_kwargs
        self.trainer_kwargs = trainer_kwargs
        self.other_kwargs = other_kwargs
        self.env_train = gym.make(env_name)
        self.env_val = gym.make(env_name)

        if type(env_kwargs) == list:
            self.env_train.configure(env_kwargs)
            self.env_val.configure(env_kwargs)
        else:
            self.env_train.configure(purpose='training', **env_kwargs)
            self.env_val.configure(purpose='test', **env_kwargs)

        self.pi = lambda *x, **y: _dqn_policy([512, 512, 512],
                                              cnn_model=self.trainer_kwargs[
                                                  'cnn_model'],
                                              env=self.env_train,
                                              *x,
                                              **y)

        self.logdir = logdir
        utils.mkdir_if_missing(self.logdir)
        utils.mkdir_if_missing(os.path.join(logdir, 'val'))
        self.logging = utils.Foo()
        self.logging.writer_val = \
          tf.summary.FileWriter(os.path.join(logdir, 'val'), flush_secs=20)
        self.iter = 0
Esempio n. 10
0
def _setup_args(config_name, logdir):

    configs, mode_str = config_name.split('+')
    configs = configs.split('.')
    arch_str, solver_str, navi_str = configs[0], configs[1], configs[2]

    args = utils.Foo()
    args.logdir = logdir
    args.solver = cfg.setup_args_solver(solver_str)
    args.navi = cfg.setup_args_navi(navi_str)
    args = cfg.setup_args_arch(args, arch_str)
    args.summary, args.control = cfg.setup_args_sum_ctrl()

    args.solver.num_workers = FLAGS.num_workers
    args.solver.task = FLAGS.task
    args.solver.seed = FLAGS.solver_seed
    args.solver.sample_gt_prob_type = FLAGS.sample_gt_action_prob_type

    if mode_str == "test_bench":
        args = cfg.setup_smyctl(args, mode_str)
    elif mode_str == "train_train":
        args.control.train = True

    args.setup_to_run = planner.setup_to_run
    args.setup_train_step_kwargs = tf_utils.setup_train_step_kwargs
    return args
Esempio n. 11
0
def get_arch_vars(arch_str):
    if arch_str == '':
        vals = []
    else:
        vals = arch_str.split('_')
    ks = ['var1', 'var2', 'var3']
    ks = ks[:len(vals)]

    # Exp Ver.
    if len(vals) == 0:
        ks.append('var1')
        vals.append('v0')
    # custom arch.
    if len(vals) == 1:
        ks.append('var2')
        vals.append('')
    # map scape for projection baseline.
    if len(vals) == 2:
        ks.append('var3')
        vals.append('fr2')

    assert (len(vals) == 3)

    vars = utils.Foo()
    for k, v in zip(ks, vals):
        setattr(vars, k, v)

    logging.error('arch_vars: %s', vars)
    return vars
def make_map(padding, resolution, vertex=None, sc=1.):
  """Returns a map structure."""
  min_, max_ = _get_xy_bounding_box(vertex*sc, padding=padding)
  sz = np.ceil((max_ - min_ + 1) / resolution).astype(np.int32)
  max_ = min_ + sz * resolution - 1
  map = utils.Foo(origin=min_, size=sz, max=max_, resolution=resolution,
                  padding=padding)
  return map
def _train(args):
    container_name = ""

    R = lambda: nav_env.get_multiplexer_class(args.navtask, args.solver.task)
    m = utils.Foo()
    m.tf_graph = tf.Graph()

    config = tf.ConfigProto()
    config.device_count['GPU'] = 1

    with m.tf_graph.as_default():
        with tf.device(
                tf.train.replica_device_setter(args.solver.ps_tasks,
                                               merge_devices=True)):
            with tf.container(container_name):
                m = args.setup_to_run(m,
                                      args,
                                      is_training=True,
                                      batch_norm_is_training=True,
                                      summary_mode='train')

                train_step_kwargs = args.setup_train_step_kwargs(
                    m,
                    R(),
                    os.path.join(args.logdir, 'train'),
                    rng_seed=args.solver.task,
                    is_chief=args.solver.task == 0,
                    num_steps=args.navtask.task_params.num_steps *
                    args.navtask.task_params.num_goals,
                    iters=1,
                    train_display_interval=args.summary.display_interval,
                    dagger_sample_bn_false=args.arch.dagger_sample_bn_false)

                delay_start = (
                    args.solver.task *
                    (args.solver.task + 1)) / 2 * FLAGS.delay_start_iters
                logging.error('delaying start for task %d by %d steps.',
                              args.solver.task, delay_start)

                additional_args = {}
                final_loss = slim.learning.train(
                    train_op=m.train_op,
                    logdir=args.logdir,
                    master=args.solver.master,
                    is_chief=args.solver.task == 0,
                    number_of_steps=args.solver.max_steps,
                    train_step_fn=tf_utils.train_step_custom_online_sampling,
                    train_step_kwargs=train_step_kwargs,
                    global_step=m.global_step_op,
                    init_op=m.init_op,
                    init_fn=m.init_fn,
                    sync_optimizer=m.sync_optimizer,
                    saver=m.saver_op,
                    startup_delay_steps=delay_start,
                    summary_op=None,
                    session_config=config,
                    **additional_args)
Esempio n. 14
0
def _train(args):
    agent = navi_env.Environment('5cf0e1e9493994e483e985c436b9d3bc', args.navi)
    Z = utils.Foo()
    Z.tf_graph = tf.Graph()

    config = tf.ConfigProto()
    config.device_count['GPU'] = 1

    with Z.tf_graph.as_default():
        with tf.device(
                tf.train.replica_device_setter(args.solver.ps_tasks,
                                               merge_devices=True)):
            with tf.container("planner"):
                Z = args.setup_to_run(Z,
                                      args,
                                      is_training=True,
                                      batch_norm_is_training=True,
                                      summary_mode='train')
                train_step_kwargs = args.setup_train_step_kwargs(
                    Z,
                    agent,
                    os.path.join(args.logdir, 'train'),
                    rng_seed=args.solver.rng_seed,
                    is_chief=args.solver.rng_seed == 0,
                    num_steps=args.navi.num_steps * args.navi.num_goals,
                    iters=1,
                    train_display_interval=args.summary.display_interval,
                    dagger_sample_bn_false=args.solver.dagger_sample_bn_false)

                delay_start = (args.solver.task *
                               (args.solver.task + 1)) / 2 * DELAY_START_ITERS
                logging.info('delaying start for task %d by %d steps.',
                             args.solver.task, delay_start)

                additional_args = {}
                final_loss = slim.learning.train(
                    train_op=Z.train_op,
                    logdir=args.logdir,
                    is_chief=args.solver.task == 0,
                    number_of_steps=args.solver.max_steps,
                    train_step_fn=tf_utils.train_step_fn,
                    train_step_kwargs=train_step_kwargs,
                    master=args.solver.master,
                    global_step=Z.global_step_op,
                    init_op=Z.init_op,
                    init_fn=Z.init_fn,
                    sync_optimizer=Z.sync_optimizer,
                    saver=Z.saver_op,
                    save_summaries_secs=5000,
                    save_interval_secs=5000,
                    startup_delay_steps=delay_start,
                    summary_op=None,
                    session_config=config,
                    **additional_args)
Esempio n. 15
0
def adjust_args_for_mode(args, mode):
    if mode == 'train':
        args.control.train = True

    elif mode == 'val1':
        # Same settings as for training, to make sure nothing wonky is happening
        # there.
        args.control.test = True
        args.control.test_mode = 'val'
        args.navtask.task_params.batch_size = 32

    elif mode == 'val2':
        # No data augmentation, not sampling but taking the argmax action, not
        # sampling from the ground truth at all.
        args.control.test = True
        args.arch.action_sample_type = 'argmax'
        args.arch.sample_gt_prob_type = 'zero'
        args.navtask.task_params.data_augment = \
          utils.Foo(lr_flip=0, delta_angle=0, delta_xy=0, relight=False,
                    relight_fast=False, structured=False)
        args.control.test_mode = 'val'
        args.navtask.task_params.batch_size = 32

    elif mode == 'bench':
        # Actually testing the agent in settings that are kept same between
        # different runs.
        args.navtask.task_params.batch_size = 16
        args.control.test = True
        args.arch.action_sample_type = 'argmax'
        args.arch.sample_gt_prob_type = 'zero'
        args.navtask.task_params.data_augment = \
          utils.Foo(lr_flip=0, delta_angle=0, delta_xy=0, relight=False,
                    relight_fast=False, structured=False)
        args.summary.test_iters = 250
        args.control.only_eval_when_done = True
        args.control.reset_rng_seed = True
        args.control.test_mode = 'test'
    else:
        logging.fatal('Unknown mode: %s.', mode)
        assert (False)
    return args
Esempio n. 16
0
def get_navtask_vars(navtask_str):
    if navtask_str == '':
        vals = []
    else:
        vals = navtask_str.split('_')

    ks_all = ['dataset_name', 'modality', 'task', 'history', 'max_dist',
              'num_steps', 'step_size', 'n_ori', 'aux_views', 'data_aug']
    ks = ks_all[:len(vals)]

    # All data or not.
    if len(vals) == 0: ks.append('dataset_name'); vals.append('sbpd')
    # modality
    if len(vals) == 1: ks.append('modality'); vals.append('rgb')
    # semantic task?
    if len(vals) == 2: ks.append('task'); vals.append('r2r')
    # number of history frames.
    if len(vals) == 3: ks.append('history'); vals.append('h0')
    # max steps
    if len(vals) == 4: ks.append('max_dist'); vals.append('32')
    # num steps
    if len(vals) == 5: ks.append('num_steps'); vals.append('40')
    # step size
    if len(vals) == 6: ks.append('step_size'); vals.append('8')
    # n_ori
    if len(vals) == 7: ks.append('n_ori'); vals.append('4')
    # Auxiliary views.
    if len(vals) == 8: ks.append('aux_views'); vals.append('nv0')
    # Normal data augmentation as opposed to structured data augmentation (if set
    # to straug.
    if len(vals) == 9: ks.append('data_aug'); vals.append('straug')

    assert (len(vals) == 10)
    for i in range(len(ks)):
        assert (ks[i] == ks_all[i])

    vars = utils.Foo()
    for k, v in zip(ks, vals):
        setattr(vars, k, v)
    logging.error('navtask_vars: %s', vals)
    return vars
def get_arch_vars(arch_str):
    if arch_str == '': vals = []
    else: vals = arch_str.split('_')

    ks = ['ver', 'lstm_dim', 'dropout']

    # Exp Ver
    if len(vals) == 0: vals.append('v0')
    # LSTM dimentsions
    if len(vals) == 1: vals.append('lstm2048')
    # Dropout
    if len(vals) == 2: vals.append('noDO')

    assert (len(vals) == 3)

    vars = utils.Foo()
    for k, v in zip(ks, vals):
        setattr(vars, k, v)

    logging.error('arch_vars: %s', vars)
    return vars
Esempio n. 18
0
    def configure(self, purpose, method_name, graph_n, edges_n, n_init_a,
                  threshold_q, num_seeds, reward_version, seed,
                  graph_obs_version):
        self.purpose = purpose
        self.T = num_seeds  #number of people the agent can seed (max horizon for an episode)
        self.seed = seed
        self.rng = np.random.RandomState(self.seed)
        self.params = utils.Foo(method_name=method_name,
                                graph_n=graph_n,
                                edges_n=edges_n,
                                threshold_q=threshold_q,
                                reward_version=reward_version,
                                n_init_a=n_init_a,
                                graph_obs_version=graph_obs_version)

        #setup observation and action spaces
        self.obs_sizes = (graph_n**2, graph_n, 2)
        self.observation_space = spaces.Box(low=-np.inf,
                                            high=np.inf,
                                            shape=(np.sum(self.obs_sizes), ))
        self.action_space = spaces.Discrete(graph_n)
        self.num_actions = graph_n
Esempio n. 19
0
    def __init__(self, env_name, env_kwargs, trainer_kwargs, other_kwargs,
                 logdir):
        self.env_name = env_name
        self.env_kwargs = env_kwargs
        self.trainer_kwargs = trainer_kwargs
        self.other_kwargs = other_kwargs
        self.env_train = gym.make(env_name)
        self.env_val = gym.make(env_name)

        if type(env_kwargs) == list:
            self.env_train.configure(env_kwargs)
            self.env_val.configure(env_kwargs)
        else:
            self.env_train.configure(purpose='training', **env_kwargs)
            self.env_val.configure(purpose='test', **env_kwargs)

        self.pi = lambda obs: _greedy_policy(obs, self.env_val)

        self.logdir = logdir
        utils.mkdir_if_missing(self.logdir)
        utils.mkdir_if_missing(os.path.join(logdir, 'val'))
        self.logging = utils.Foo()
        self.logging.writer_val = \
          tf.summary.FileWriter(os.path.join(logdir, 'val'), flush_secs=20)
Esempio n. 20
0
def get_default_args():
    robot = utils.Foo(radius=15,
                      base=10,
                      height=140,
                      sensor_height=120,
                      camera_elevation_degree=-15)

    camera_param = utils.Foo(width=225,
                             height=225,
                             z_near=0.05,
                             z_far=20.0,
                             fov=60.,
                             modalities=['rgb', 'depth'])

    env = utils.Foo(padding=10,
                    resolution=5,
                    num_point_threshold=2,
                    valid_min=-10,
                    valid_max=200,
                    n_samples_per_face=200)

    data_augment = utils.Foo(lr_flip=0,
                             delta_angle=1,
                             delta_xy=4,
                             relight=False,
                             relight_fast=False,
                             structured=False)

    task_params = utils.Foo(num_actions=4,
                            step_size=4,
                            num_steps=0,
                            batch_size=32,
                            room_seed=0,
                            base_class='Building',
                            task='mapping',
                            n_ori=6,
                            data_augment=data_augment,
                            output_transform_to_global_map=False,
                            output_canonical_map=False,
                            output_incremental_transform=False,
                            output_free_space=False,
                            move_type='shortest_path',
                            toy_problem=0)

    buildinger_args = utils.Foo(
        building_names=['area1_gates_wingA_floor1_westpart'],
        env_class=None,
        robot=robot,
        task_params=task_params,
        env=env,
        camera_param=camera_param)

    solver_args = utils.Foo(seed=0,
                            learning_rate_decay=0.1,
                            clip_gradient_norm=0,
                            max_steps=120000,
                            initial_learning_rate=0.001,
                            momentum=0.99,
                            steps_per_decay=40000,
                            logdir=None,
                            sync=False,
                            adjust_lr_sync=True,
                            wt_decay=0.0001,
                            data_loss_wt=1.0,
                            reg_loss_wt=1.0,
                            num_workers=1,
                            task=0,
                            ps_tasks=0,
                            master='local')

    summary_args = utils.Foo(display_interval=1, test_iters=100)

    control_args = utils.Foo(train=False,
                             test=False,
                             force_batchnorm_is_training_at_test=False)

    arch_args = utils.Foo(rgb_encoder='resnet_v2_50', d_encoder='resnet_v2_50')

    return utils.Foo(solver=solver_args,
                     summary=summary_args,
                     control=control_args,
                     arch=arch_args,
                     buildinger=buildinger_args)
Esempio n. 21
0
def process_solver_str(solver_str):
    solver = utils.Foo(seed=0,
                       learning_rate_decay=None,
                       clip_gradient_norm=None,
                       max_steps=None,
                       initial_learning_rate=None,
                       momentum=None,
                       steps_per_decay=None,
                       logdir=None,
                       sync=False,
                       adjust_lr_sync=True,
                       wt_decay=0.0001,
                       data_loss_wt=None,
                       reg_loss_wt=None,
                       freeze_conv=True,
                       num_workers=1,
                       task=0,
                       ps_tasks=0,
                       master='local',
                       typ=None,
                       momentum2=None,
                       adam_eps=None)

    # Clobber with overrides from solver str.
    solver_vars = get_solver_vars(solver_str)

    solver.data_loss_wt = float(solver_vars.dlw[3:].replace('x', '.'))
    solver.adam_eps = float(solver_vars.adam_eps[4:].replace('x', '.').replace(
        'n', '-'))
    solver.initial_learning_rate = float(solver_vars.init_lr[2:].replace(
        'x', '.').replace('n', '-'))
    solver.reg_loss_wt = float(solver_vars.rlw[3:].replace('x', '.'))
    solver.isd_k = float(solver_vars.isdk[4:].replace('x', '.'))

    long = solver_vars.long
    if long == 'long':
        solver.steps_per_decay = 40000
        solver.max_steps = 120000
    elif long == 'long2':
        solver.steps_per_decay = 80000
        solver.max_steps = 120000
    elif long == 'nolong' or long == 'nol':
        solver.steps_per_decay = 20000
        solver.max_steps = 60000
    else:
        logging.fatal('solver_vars.long should be long, long2, nolong or nol.')
        assert (False)

    clip = solver_vars.clip
    if clip == 'noclip' or clip == 'nocl':
        solver.clip_gradient_norm = 0
    elif clip[:4] == 'clip':
        solver.clip_gradient_norm = float(clip[4:].replace('x', '.'))
    else:
        logging.fatal('Unknown solver_vars.clip: %s', clip)
        assert (False)

    typ = solver_vars.typ
    if typ == 'adam':
        solver.typ = 'adam'
        solver.momentum = 0.9
        solver.momentum2 = 0.999
        solver.learning_rate_decay = 1.0
    elif typ == 'adam2':
        solver.typ = 'adam'
        solver.momentum = 0.9
        solver.momentum2 = 0.999
        solver.learning_rate_decay = 0.1
    elif typ == 'sgd':
        solver.typ = 'sgd'
        solver.momentum = 0.99
        solver.momentum2 = None
        solver.learning_rate_decay = 0.1
    else:
        logging.fatal('Unknown solver_vars.typ: %s', typ)
        assert (False)

    logging.error('solver: %s', solver)
    return solver
def _train(args):
    #pdb.set_trace()
    container_name = ""
    #tmp setting TRI
    args.solver.max_steps = 500000
    args.solver.steps_per_decay = 50000
    args.solver.initial_learning_rate = 1e-8
    args.navtask.task_params.batch_size = 32

    #pdb.set_trace()
    R = lambda: nav_env.get_multiplexer_class(args.navtask, args.solver.task)
    m = utils.Foo()
    m_cloned = utils.Foo()
    m.tf_graph = tf.Graph()

    #Tri
    #add a cloned building object for checking the exploration result during training
    #m.cloned_obj = R()
    m.batch_size = args.navtask.task_params.batch_size
    m.train_type = 1
    m.suffle = False
    m.is_first_step = True
    m.save_pic_step = 10000
    m.save_pic_count = 0
    m.save_reward_step = 500
    m.save_reward_count = 0

    m.is_main = True
    m_cloned.is_main = False

    config = tf.ConfigProto()
    config.device_count['GPU'] = 1

    with m.tf_graph.as_default():
        with tf.device(
                tf.train.replica_device_setter(args.solver.ps_tasks,
                                               merge_devices=True)):
            with tf.container(container_name):
                #pdb.set_trace()
                m = args.setup_to_run(m,
                                      args,
                                      is_training=True,
                                      batch_norm_is_training=True,
                                      summary_mode='train')

                #pdb.set_trace()

                #with tf.name_scope('cloned'):
                m_cloned.x = m.x
                m_cloned.vars_to_restore = m.vars_to_restore
                m_cloned.batch_norm_is_training_op = m.batch_norm_is_training_op
                m_cloned.input_tensors = m.input_tensors
                with tf.variable_scope('cloned'):
                    m_cloned = args.setup_to_run(m_cloned,
                                                 args,
                                                 is_training=True,
                                                 batch_norm_is_training=True,
                                                 summary_mode='train')
                #pdb.set_trace()
                clonemodel(m, m_cloned)
                set_copying_ops(m)
                m.init_op = tf.group(tf.global_variables_initializer(),
                                     tf.local_variables_initializer())
                m.saver_op = tf.train.Saver(keep_checkpoint_every_n_hours=6,
                                            write_version=tf.train.SaverDef.V2)
                #with tf.Session() as sess:
                #  sess.run(m.init_op)
                #pdb.set_trace()
                train_step_kwargs = args.setup_train_step_kwargs(
                    m,
                    R(),
                    os.path.join(args.logdir, 'train'),
                    rng_seed=args.solver.task,
                    is_chief=args.solver.task == 0,
                    num_steps=args.navtask.task_params.num_steps *
                    args.navtask.task_params.num_goals,
                    iters=1,
                    train_display_interval=args.summary.display_interval,
                    dagger_sample_bn_false=args.arch.dagger_sample_bn_false)
                #pdb.set_trace()
                delay_start = (
                    args.solver.task *
                    (args.solver.task + 1)) / 2 * FLAGS.delay_start_iters
                logging.error('delaying start for task %d by %d steps.',
                              args.solver.task, delay_start)

                #Tri
                #clonemodel(m)
                #set_copying_ops(m)
                #set_tmp_params(m)
                #generating data for testing the learning process during training
                #obj = train_step_kwargs['obj']
                rng_data = train_step_kwargs['rng_data']
                #m.e1 = obj.sample_env(rng_data)
                #m.init_env_state1 = m.e1.reset(rng_data)
                #m.e2 = obj.sample_env(rng_data)
                #m.init_env_state2 = m.e2.reset(rng_data)
                m.rng_data = deepcopy(rng_data)

                #pdb.set_trace()
                additional_args = {}
                final_loss = slim.learning.train(
                    train_op=m.train_op,
                    logdir=args.logdir,
                    master=args.solver.master,
                    is_chief=args.solver.task == 0,
                    number_of_steps=args.solver.max_steps,
                    train_step_fn=tf_utils.train_step_custom_online_sampling,
                    train_step_kwargs=train_step_kwargs,
                    global_step=m.global_step_op,
                    init_op=m.init_op,
                    init_fn=m.init_fn,
                    sync_optimizer=m.sync_optimizer,
                    saver=m.saver_op,
                    startup_delay_steps=delay_start,
                    summary_op=None,
                    session_config=config,
                    **additional_args)
def nav_env_base_config():
    """Returns the base config for stanford navigation environment.

  Returns:
    Base config for stanford navigation environment.
  """
    robot = utils.Foo(radius=15,
                      base=10,
                      height=140,
                      sensor_height=120,
                      camera_elevation_degree=-15)

    env = utils.Foo(padding=10,
                    resolution=5,
                    num_point_threshold=2,
                    valid_min=-10,
                    valid_max=200,
                    n_samples_per_face=200)

    camera_param = utils.Foo(width=225,
                             height=225,
                             z_near=0.05,
                             z_far=20.0,
                             fov=60.,
                             modalities=['rgb'],
                             img_channels=3)

    data_augment = utils.Foo(
        lr_flip=0,
        delta_angle=0.5,
        delta_xy=4,
        relight=True,
        relight_fast=False,
        structured=False
    )  # if True, uses the same perturb for the whole episode.

    outputs = utils.Foo(images=True,
                        rel_goal_loc=False,
                        loc_on_map=True,
                        gt_dist_to_goal=True,
                        ego_maps=False,
                        ego_goal_imgs=False,
                        egomotion=False,
                        visit_count=False,
                        analytical_counts=False,
                        node_ids=True,
                        readout_maps=False)

    # class_map_names=['board', 'chair', 'door', 'sofa', 'table']
    class_map_names = ['chair', 'door', 'table']
    semantic_task = utils.Foo(class_map_names=class_map_names,
                              pix_distance=16,
                              sampling='uniform')

    # time per iteration for cmp is 0.82 seconds per episode with 3.4s overhead per batch.
    task_params = utils.Foo(max_dist=32,
                            step_size=8,
                            num_steps=40,
                            num_actions=4,
                            batch_size=4,
                            building_seed=0,
                            num_goals=1,
                            img_height=None,
                            img_width=None,
                            img_channels=None,
                            modalities=None,
                            outputs=outputs,
                            map_scales=[1.],
                            map_crop_sizes=[64],
                            rel_goal_loc_dim=4,
                            base_class='Building',
                            task='map+plan',
                            n_ori=4,
                            type='room_to_room_many',
                            data_augment=data_augment,
                            room_regex='^((?!hallway).)*$',
                            toy_problem=False,
                            map_channels=1,
                            gt_coverage=False,
                            input_type='maps',
                            full_information=False,
                            aux_delta_thetas=[],
                            semantic_task=semantic_task,
                            num_history_frames=0,
                            node_ids_dim=1,
                            perturbs_dim=4,
                            map_resize_method='linear_noantialiasing',
                            readout_maps_channels=1,
                            readout_maps_scales=[],
                            readout_maps_crop_sizes=[],
                            n_views=1,
                            reward_time_penalty=0.1,
                            reward_at_goal=1.,
                            discount_factor=0.99,
                            rejection_sampling_M=100,
                            min_dist=None)

    navtask_args = utils.Foo(
        building_names=['area1_gates_wingA_floor1_westpart'],
        env_class=nav_env.VisualNavigationEnv,
        robot=robot,
        task_params=task_params,
        env=env,
        camera_param=camera_param,
        cache_rooms=True)
    return navtask_args
Esempio n. 24
0
def get_map_from_images(imgs, mapper_arch, task_params, freeze_conv, wt_decay,
                        is_training, batch_norm_is_training_op, num_maps,
                        split_maps=True):
  # Hit image with a resnet.
  n_views = len(task_params.aux_delta_thetas) + 1
  out = utils.Foo()

  images_reshaped = tf.reshape(imgs, 
      shape=[-1, task_params.img_height,
             task_params.img_width,
             task_params.img_channels], name='re_image')

  x, out.vars_to_restore = get_repr_from_image(
      images_reshaped, task_params.modalities, task_params.data_augment,
      mapper_arch.encoder, freeze_conv, wt_decay, is_training)

  # Reshape into nice things so that these can be accumulated over time steps
  # for faster backprop.
  sh_before = x.get_shape().as_list()
  out.encoder_output = tf.reshape(x, shape=[task_params.batch_size, -1, n_views] + sh_before[1:])
  x = tf.reshape(out.encoder_output, shape=[-1] + sh_before[1:])

  # Add a layer to reduce dimensions for a fc layer.
  if mapper_arch.dim_reduce_neurons > 0:
    ks = 1; neurons = mapper_arch.dim_reduce_neurons;
    init_var = np.sqrt(2.0/(ks**2)/neurons)
    batch_norm_param = mapper_arch.batch_norm_param
    batch_norm_param['is_training'] = batch_norm_is_training_op
    out.conv_feat = slim.conv2d(x, neurons, kernel_size=ks, stride=1,
                    normalizer_fn=slim.batch_norm, normalizer_params=batch_norm_param,
                    padding='SAME', scope='dim_reduce',
                    weights_regularizer=slim.l2_regularizer(wt_decay),
                    weights_initializer=tf.random_normal_initializer(stddev=init_var))
    reshape_conv_feat = slim.flatten(out.conv_feat)
    sh = reshape_conv_feat.get_shape().as_list()
    out.reshape_conv_feat = tf.reshape(reshape_conv_feat, shape=[-1, sh[1]*n_views])

  with tf.variable_scope('fc'):
    # Fully connected layers to compute the representation in top-view space.
    fc_batch_norm_param = {'center': True, 'scale': True, 
                           'activation_fn':tf.nn.relu,
                           'is_training': batch_norm_is_training_op}
    f = out.reshape_conv_feat
    out_neurons = (mapper_arch.fc_out_size**2)*mapper_arch.fc_out_neurons
    neurons = mapper_arch.fc_neurons + [out_neurons]
    f, _ = tf_utils.fc_network(f, neurons=neurons, wt_decay=wt_decay,
                               name='fc', offset=0,
                               batch_norm_param=fc_batch_norm_param,
                               is_training=is_training,
                               dropout_ratio=mapper_arch.fc_dropout)
    f = tf.reshape(f, shape=[-1, mapper_arch.fc_out_size,
                             mapper_arch.fc_out_size,
                             mapper_arch.fc_out_neurons], name='re_fc')

  # Use pool5 to predict the free space map via deconv layers.
  with tf.variable_scope('deconv'):
    x, outs = deconv(f, batch_norm_is_training_op, wt_decay=wt_decay,
                     neurons=mapper_arch.deconv_neurons,
                     strides=mapper_arch.deconv_strides,
                     layers_per_block=mapper_arch.deconv_layers_per_block,
                     kernel_size=mapper_arch.deconv_kernel_size,
                     conv_fn=slim.conv2d_transpose, offset=0, name='deconv')

  # Reshape x the right way.
  sh = x.get_shape().as_list()
  x = tf.reshape(x, shape=[task_params.batch_size, -1] + sh[1:])
  out.deconv_output = x

  # Separate out the map and the confidence predictions, pass the confidence
  # through a sigmoid.
  if split_maps:
    with tf.name_scope('split'):
      out_all = tf.split(value=x, axis=4, num_or_size_splits=2*num_maps)
      out.fss_logits = out_all[:num_maps]
      out.confs_logits = out_all[num_maps:]
    with tf.name_scope('sigmoid'):
      out.confs_probs = [tf.nn.sigmoid(x) for x in out.confs_logits]
  return out
Esempio n. 25
0
def get_default_summary_ops():
    return utils.Foo(summary_ops=None,
                     print_summary_ops=None,
                     additional_return_ops=[],
                     arop_summary_iters=[],
                     arop_eval_fns=[])
Esempio n. 26
0
def main(_):
    args = config_distill.get_args_for_config(FLAGS.config_name)
    args.logdir = FLAGS.logdir
    args.solver.num_workers = FLAGS.num_workers
    args.solver.task = FLAGS.task
    args.solver.ps_tasks = FLAGS.ps_tasks
    args.solver.master = FLAGS.master

    args.buildinger.env_class = nav_env.MeshMapper
    fu.makedirs(args.logdir)
    args.buildinger.logdir = args.logdir
    R = nav_env.get_multiplexor_class(args.buildinger, args.solver.task)

    if False:
        pr = cProfile.Profile()
        pr.enable()
        rng = np.random.RandomState(0)
        for i in range(1):
            b, instances_perturbs = R.sample_building(rng)
            inputs = b.worker(*(instances_perturbs))
            for j in range(inputs['imgs'].shape[0]):
                p = os.path.join('tmp', '{:d}.png'.format(j))
                img = inputs['imgs'][j, 0, :, :, :3] * 1
                img = (img).astype(np.uint8)
                fu.write_image(p, img)
            print(inputs['imgs'].shape)
            inputs = R.pre(inputs)
        pr.disable()
        pr.print_stats(2)

    if args.control.train:
        if not gfile.Exists(args.logdir):
            gfile.MakeDirs(args.logdir)

        m = utils.Foo()
        m.tf_graph = tf.Graph()

        config = tf.ConfigProto()
        config.device_count['GPU'] = 1
        config.gpu_options.allow_growth = True
        config.gpu_options.per_process_gpu_memory_fraction = 0.8

        with m.tf_graph.as_default():
            with tf.device(tf.train.replica_device_setter(
                    args.solver.ps_tasks)):
                m = distill.setup_to_run(m,
                                         args,
                                         is_training=True,
                                         batch_norm_is_training=True)

                train_step_kwargs = distill.setup_train_step_kwargs_mesh(
                    m,
                    R,
                    os.path.join(args.logdir, 'train_bkp'),
                    rng_seed=args.solver.task,
                    is_chief=args.solver.task == 0,
                    iters=1,
                    train_display_interval=args.summary.display_interval)

                final_loss = slim.learning.train(
                    train_op=m.train_op,
                    logdir=args.logdir,
                    master=args.solver.master,
                    is_chief=args.solver.task == 0,
                    number_of_steps=args.solver.max_steps,
                    train_step_fn=tf_utils.train_step_custom,
                    train_step_kwargs=train_step_kwargs,
                    global_step=m.global_step_op,
                    init_op=m.init_op,
                    init_fn=m.init_fn,
                    sync_optimizer=m.sync_optimizer,
                    saver=m.saver_op,
                    summary_op=None,
                    session_config=config)

    if args.control.test:
        m = utils.Foo()
        m.tf_graph = tf.Graph()
        checkpoint_dir = os.path.join(format(args.logdir))
        with m.tf_graph.as_default():
            m = distill.setup_to_run(m,
                                     args,
                                     is_training=False,
                                     batch_norm_is_training=args.control.
                                     force_batchnorm_is_training_at_test)

            train_step_kwargs = distill.setup_train_step_kwargs_mesh(
                m,
                R,
                os.path.join(args.logdir, args.control.test_name),
                rng_seed=args.solver.task + 1,
                is_chief=args.solver.task == 0,
                iters=args.summary.test_iters,
                train_display_interval=None)

            sv = slim.learning.supervisor.Supervisor(
                graph=ops.get_default_graph(),
                logdir=None,
                init_op=m.init_op,
                summary_op=None,
                summary_writer=None,
                global_step=None,
                saver=m.saver_op)

            last_checkpoint = None
            while True:
                last_checkpoint = slim.evaluation.wait_for_new_checkpoint(
                    checkpoint_dir, last_checkpoint)
                checkpoint_iter = int(
                    os.path.basename(last_checkpoint).split('-')[1])
                start = time.time()
                logging.info(
                    'Starting evaluation at %s using checkpoint %s.',
                    time.strftime('%Y-%m-%d-%H:%M:%S', time.localtime()),
                    last_checkpoint)

                config = tf.ConfigProto()
                config.device_count['GPU'] = 1
                config.gpu_options.allow_growth = True
                config.gpu_options.per_process_gpu_memory_fraction = 0.8

                with sv.managed_session(args.solver.master,
                                        config=config,
                                        start_standard_services=False) as sess:
                    sess.run(m.init_op)
                    sv.saver.restore(sess, last_checkpoint)
                    sv.start_queue_runners(sess)
                    vals, _ = tf_utils.train_step_custom(sess,
                                                         None,
                                                         m.global_step_op,
                                                         train_step_kwargs,
                                                         mode='val')
                    if checkpoint_iter >= args.solver.max_steps:
                        break
def _test(args):
    args.solver.master = ''
    container_name = ""
    checkpoint_dir = os.path.join(format(args.logdir))
    logging.error('Checkpoint_dir: %s', args.logdir)

    config = tf.ConfigProto()
    config.device_count['GPU'] = 1

    m = utils.Foo()
    m.tf_graph = tf.Graph()

    rng_data_seed = 0
    rng_action_seed = 0
    R = lambda: nav_env.get_multiplexer_class(args.navtask, rng_data_seed)
    with m.tf_graph.as_default():
        with tf.container(container_name):
            m = args.setup_to_run(m,
                                  args,
                                  is_training=False,
                                  batch_norm_is_training=args.control.
                                  force_batchnorm_is_training_at_test,
                                  summary_mode=args.control.test_mode)
            train_step_kwargs = args.setup_train_step_kwargs(
                m,
                R(),
                os.path.join(args.logdir, args.control.test_name),
                rng_seed=rng_data_seed,
                is_chief=True,
                num_steps=args.navtask.task_params.num_steps *
                args.navtask.task_params.num_goals,
                iters=args.summary.test_iters,
                train_display_interval=None,
                dagger_sample_bn_false=args.arch.dagger_sample_bn_false)

            saver = slim.learning.tf_saver.Saver(
                variables.get_variables_to_restore())

            sv = slim.learning.supervisor.Supervisor(
                graph=ops.get_default_graph(),
                logdir=None,
                init_op=m.init_op,
                summary_op=None,
                summary_writer=None,
                global_step=None,
                saver=m.saver_op)

            last_checkpoint = None
            reported = False
            while True:
                last_checkpoint_ = None
                while last_checkpoint_ is None:
                    last_checkpoint_ = slim.evaluation.wait_for_new_checkpoint(
                        checkpoint_dir,
                        last_checkpoint,
                        seconds_to_sleep=10,
                        timeout=60)
                if last_checkpoint_ is None: break

                last_checkpoint = last_checkpoint_
                checkpoint_iter = int(
                    os.path.basename(last_checkpoint).split('-')[1])

                logging.info(
                    'Starting evaluation at %s using checkpoint %s.',
                    time.strftime('%Y-%m-%d-%H:%M:%S', time.localtime()),
                    last_checkpoint)

                if (args.control.only_eval_when_done == False
                        or checkpoint_iter >= args.solver.max_steps):
                    start = time.time()
                    logging.info(
                        'Starting evaluation at %s using checkpoint %s.',
                        time.strftime('%Y-%m-%d-%H:%M:%S', time.localtime()),
                        last_checkpoint)

                    with sv.managed_session(
                            args.solver.master,
                            config=config,
                            start_standard_services=False) as sess:
                        sess.run(m.init_op)
                        sv.saver.restore(sess, last_checkpoint)
                        sv.start_queue_runners(sess)
                        if args.control.reset_rng_seed:
                            train_step_kwargs['rng_data'] = [
                                np.random.RandomState(rng_data_seed),
                                np.random.RandomState(rng_data_seed)
                            ]
                            train_step_kwargs[
                                'rng_action'] = np.random.RandomState(
                                    rng_action_seed)
                        vals, _ = tf_utils.train_step_custom_online_sampling(
                            sess,
                            None,
                            m.global_step_op,
                            train_step_kwargs,
                            mode=args.control.test_mode)
                        should_stop = False

                        if checkpoint_iter >= args.solver.max_steps:
                            should_stop = True

                        if should_stop:
                            break
Esempio n. 28
0
def _test(args):
    # Give checkpoint directory
    container_name = ""
    checkpoint_dir = os.path.join(format(args.logdir))
    logging.error('Checkpoint_dir: %s', args.logdir)
    # Load Agent
    agent = navi_env.Environment('5cf0e1e9493994e483e985c436b9d3bc', args.navi)
    # Add Configure
    config = tf.compat.v1.ConfigProto()
    config.device_count['GPU'] = 1

    Z = utils.Foo()
    Z.tf_graph = tf.Graph()
    with Z.tf_graph.as_default():
        with tf.compat.v1.container(container_name):
            Z = args.setup_to_run(Z,
                                  args,
                                  is_training=False,
                                  batch_norm_is_training=args.control.
                                  force_batchnorm_is_training_at_test,
                                  summary_mode=args.control.test_mode)
            train_step_kwargs = args.setup_train_step_kwargs(
                Z,
                agent,
                os.path.join(args.logdir, args.control.test_name),
                rng_seed=1008,
                is_chief=True,
                num_steps=args.navi.num_steps * args.navi.num_goals,
                iters=args.summary.test_iters,
                train_display_interval=None,
                dagger_sample_bn_false=args.solver.dagger_sample_bn_false)

            saver = slim.learning.tf_saver.Saver(
                variables.get_variables_to_restore())

            sv = slim.learning.supervisor.Supervisor(
                graph=ops.get_default_graph(),
                logdir=None,
                init_op=Z.init_op,
                summary_op=None,
                summary_writer=None,
                global_step=None,
                saver=Z.saver_op)

            last_checkpoint = None
            # reported = False
            while True:
                last_checkpoint_ = None
                while last_checkpoint_ is None:
                    last_checkpoint_ = slim.evaluation.wait_for_new_checkpoint(
                        checkpoint_dir,
                        last_checkpoint,
                        seconds_to_sleep=10,
                        timeout=60)
                if last_checkpoint_ is None:
                    break

                last_checkpoint = last_checkpoint_
                checkpoint_iter = int(
                    os.path.basename(last_checkpoint).split('-')[1])

                logging.info(
                    'Starting evaluation at %s using checkpoint %s.',
                    time.strftime('%Y-%Z-%d-%H:%Z:%S', time.localtime()),
                    last_checkpoint)

                if (not args.control.only_eval_when_done
                        or checkpoint_iter >= args.solver.max_steps):
                    # start = time.time()
                    logging.info(
                        'Starting evaluation at %s using checkpoint %s.',
                        time.strftime('%Y-%Z-%d-%H:%Z:%S', time.localtime()),
                        last_checkpoint)

                    with sv.managed_session(
                            args.solver.master,
                            config=config,
                            start_standard_services=False) as sess:
                        sess.run(Z.init_op)
                        sv.saver.restore(sess, last_checkpoint)
                        sv.start_queue_runners(sess)
                        if args.control.reset_rng_seed:
                            train_step_kwargs['rng_data'] = [
                                np.random.RandomState(1008),
                                np.random.RandomState(1008)
                            ]
                            train_step_kwargs[
                                'rng_action'] = np.random.RandomState(1008)
                        vals, _ = tf_utils.train_step_fn(
                            sess,
                            None,
                            Z.global_step_op,
                            train_step_kwargs,
                            mode=args.control.test_mode)
                        should_stop = False

                        if checkpoint_iter >= args.solver.max_steps:
                            should_stop = True

                        if should_stop:
                            break