Example #1
0
    def __init__(self, args):
        self.args = args
        self.model_cfg = configs.BaseConfig(args.model_config_path)
        self.run_cfg = configs.Run(args.run_config_path)
        self.dataset_cfg = datasets.more(configs.BaseConfig(args.dataset_config_path))

        self._init()
        self._get_component()
Example #2
0
def configure_app(app, config):
    app.config.from_object(configs.BaseConfig())

    if not config:
        config = configs.config_map['dev']

    app.config.from_object(config)
Example #3
0
def test_epi_batching(db):
    env_config = configs.BaseConfig(
        'test', data.StepCoder(data.NumpyCoder(num_axes=2, dtype=np.float)))
    rollout = db.create_rollout(env_config)
    episode = rollout.create_episode()

    for _ in range(3):

        o1 = np.random.rand(80, 80)
        episode.append(data.Step(o1, 1, 1.0, False), batch=10)
        o2 = np.random.rand(80, 80)
        episode.append(data.Step(o2, 1, 1.0, False), batch=10)
        o3 = np.random.rand(80, 80)
        episode.append(data.Step(o3, 1, 1.0, False), batch=10)

    assert len(episode) == 0
    episode.append(data.Step(o3, 1, 1.0, False), batch=10)
    assert len(episode) == 10

    episode = rollout.create_episode()
    for _ in range(3):

        o1 = np.random.rand(80, 80)
        episode.append(data.Step(o1, 1, 1.0, False), batch=10)
        o2 = np.random.rand(80, 80)
        episode.append(data.Step(o2, 1, 1.0, False), batch=10)
        o3 = np.random.rand(80, 80)
        episode.append(data.Step(o3, 1, 1.0, False), batch=10)

    episode.end()

    assert len(episode) == 9
 def __init__(self, cfg, data_cfg, run, **kwargs):
     super().__init__(cfg, data_cfg, run, **kwargs)
     assert hasattr(self.cfg, 'noise_times')
     assert hasattr(self.cfg, 'input_list')
     self.noise_test = configs.BaseConfig(
         dict(name=self.__class__.__name__,
              times=self.cfg.noise_times,
              flag=False))
 def more(cfg):
     cfg.path = configs.env.getdir(cfg.path)
     cfg.source.elements = cfg.source.width * cfg.source.height * cfg.source.time
     out = dict()
     out['elements'] = 1
     cfg.out = configs.BaseConfig(out)
     cfg.cross_folder = 0
     cfg.index_cross = 0
     return cfg
Example #6
0
 def __init__(self, cfg, data_cfg, run, **kwargs):
     super().__init__(cfg, data_cfg, run, **kwargs)
     assert hasattr(self.cfg, 'return_list')
     self._save_list.append('boost')
     self.boost = configs.BaseConfig(
         dict(name=self.__class__.__name__,
              error=list(),
              alpha=list(),
              eps=1e-20))
 def run(self):
     for p in self.path:
         name = os.path.splitext(os.path.split(p)[1])[0]
         logger = utils.Logger(os.path.join(os.path.dirname(__file__), 'test'), name)
         if isinstance(p, str) or isinstance(p, dict):
             logger.info('Testing ' + str(p) + ' ...')
             logger.info(configs.BaseConfig(p))
             logger.info('Testing ' + str(p) + ' completed.')
         else:
             raise TypeError('got {} but need dict or str!'.format(type(p)))
Example #8
0
 def __init__(self, cfg, data_cfg, run, **kwargs):
     super().__init__(cfg, data_cfg, run, **kwargs)
     assert hasattr(self.cfg, 'bag_times')
     self.msg['Bagging'] = 'bag' + str(self.main_msg['while_idx'])
     self._save_list.append('bag')
     self.bag = configs.BaseConfig(
         dict(name=self.__class__.__name__,
              times=self.cfg.bag_times,
              size=getattr(self.cfg, 'bag_size', None),
              index=self.main_msg['while_idx']))
Example #9
0
def configure_app(app, config):
    app.config.from_object(configs.BaseConfig())

    if not config:
        config = configs.config_map['dev']

    app.config.from_object(config)

    # register blueprints
    for bp in blueprints:
        app.register_blueprint(bp)
Example #10
0
    def __init__(self, args):
        self.args = args
        self.model_cfg = configs.BaseConfig(
            utils.path.real_config_path(args.model_config_path,
                                        configs.env.paths.model_cfgs_folder))
        self.run_cfg = configs.Run(utils.path.real_config_path(
            args.run_config_path, configs.env.paths.run_cfgs_folder),
                                   gpus=args.gpus)
        self.dataset_cfg = datasets.functional.common.more(
            configs.BaseConfig(
                utils.path.real_config_path(
                    args.dataset_config_path,
                    configs.env.paths.dataset_cfgs_folder)))

        if not self.run_cfg.distributed or (self.run_cfg.distributed
                                            and self.run_cfg.local_rank == 0):
            print(args)

        self._init()
        self._get_component()
Example #11
0
def test_redis_write_step(db):
    env_config = configs.BaseConfig(
        'test', data.StepCoder(data.NumpyCoder(num_axes=2, dtype=np.float)))
    rollout = db.create_rollout(env_config)
    lookup = {}

    episode = rollout.create_episode()
    o1 = np.random.rand(80, 80)
    episode.append(data.Step(o1, 1, 1.0, False))
    episode.append(data.Step(o1, 1, 1.0, False))
    episode.append(data.Step(o1, 1, 1.0, False))
    episode.end()

    episode = rollout.create_episode()
    o2 = np.random.rand(80, 80)
    episode.append(data.Step(o1, 1, 1.0, False))
    episode.append(data.Step(o1, 1, 1.0, False))
    episode.append(data.Step(o1, 1, 1.0, False))
    episode.end()

    episode = rollout.create_episode()
    o3 = np.random.rand(80, 80)
    episode.append(data.Step(o1, 1, 1.0, False))
    episode.append(data.Step(o1, 1, 1.0, False))
    episode.append(data.Step(o1, 1, 1.0, False))
    episode.end()

    rollout.finalize()

    step = rollout[0]
    step = rollout[1]
    step = rollout[2]

    np.testing.assert_array_equal(step.observation, o1)

    step = rollout[3]
    step = rollout[4]
    step = rollout[5]

    np.testing.assert_array_equal(step.observation, o1)

    step = rollout[6]
    step = rollout[7]
    step = rollout[8]

    np.testing.assert_array_equal(step.observation, o1)

    assert len(rollout) == 9
    assert step.reward == 1.0
    assert step.action == 1
Example #12
0
 def __init__(self):
     super(Env, self).__init__(dict())
     cfg_dir = os.path.abspath(
         os.path.join(os.path.dirname(__file__), '../res/env'))
     if not os.path.exists(cfg_dir):
         os.makedirs(cfg_dir)
     for file in os.listdir(cfg_dir):
         setattr(self,
                 os.path.splitext(file)[0],
                 configs.BaseConfig(os.path.join(cfg_dir, file)))
     if hasattr(self, 'paths') and hasattr(self.paths, 'root_folder'):
         self.paths.root_folder = os.path.abspath(
             os.path.join(os.path.dirname(__file__), '..',
                          self.paths.root_folder))
     else:
         raise ValueError(
             'Lack of `res/env/paths.json` file or `root_folder` value')
Example #13
0
def test_epi_iter(db):
    env_config = configs.BaseConfig(
        'test', StepCoder(observation_coder=NumpyCoder(2, dtype=np.float64)))
    rollout = db.create_rollout(env_config)

    lookup = {}

    episode = rollout.create_episode()
    o1 = np.random.rand(80, 80)
    episode.append(data.Step(o1, 1, 1.0, False))
    o2 = np.random.rand(80, 80)
    episode.append(data.Step(o2, 1, 1.0, False))
    o3 = np.random.rand(80, 80)
    episode.append(data.Step(o3, 1, 1.0, False))
    lookup[episode.id] = [o1, o2, o3]
    episode.end()

    episode = rollout.create_episode()
    o4 = np.random.rand(80, 80)
    episode.append(data.Step(o4, 1, 1.0, False))
    o5 = np.random.rand(80, 80)
    episode.append(data.Step(o5, 1, 1.0, False))
    o6 = np.random.rand(80, 80)
    episode.append(data.Step(o6, 1, 1.0, False))
    lookup[episode.id] = [o4, o5, o6]
    episode.end()

    episode = rollout.create_episode()
    o7 = np.random.rand(80, 80)
    episode.append(data.Step(o7, 1, 1.0, False))
    o8 = np.random.rand(80, 80)
    episode.append(data.Step(o8, 1, 1.0, False))
    o9 = np.random.rand(80, 80)
    episode.append(data.Step(o9, 1, 1.0, False))
    lookup[episode.id] = [o7, o8, o9]
    episode.end()

    rollout.finalize()

    assert len(rollout) == 9

    for episode in rollout:
        assert len(episode) == 3
        ob = lookup[episode.id]
        for step, o in zip(episode, ob):
            np.testing.assert_array_equal(step.observation, o)
Example #14
0
def test_step_iter(db):
    env_config = configs.BaseConfig(
        'test', data.StepCoder(data.NumpyCoder(num_axes=2, dtype=np.float)))
    rollout = db.create_rollout(env_config)

    episode = rollout.create_episode()
    o1 = np.random.rand(80, 80)
    episode.append(data.Step(o1, 1, 1.0, False))
    o2 = np.random.rand(80, 80)
    episode.append(data.Step(o2, 1, 1.0, False))
    o3 = np.random.rand(80, 80)
    episode.append(data.Step(o3, 1, 1.0, False))
    episode.end()

    assert len(episode) == 3

    for step, o in zip(episode, [o1, o2, o3]):
        np.testing.assert_array_equal(step.observation, o)
Example #15
0
def test_epi_total_reward(db):
    env_config = configs.BaseConfig(
        'test', data.StepCoder(data.NumpyCoder(num_axes=2, dtype=np.float)))
    rollout = db.create_rollout(env_config)
    episode = rollout.create_episode()

    assert episode.total_reward() == 0

    o1 = np.random.rand(80, 80)
    episode.append(data.Step(o1, 1, 1.0, False))

    assert episode.total_reward() == 1.0

    o2 = np.random.rand(80, 80)
    episode.append(data.Step(o2, 1, 1.0, False))

    assert episode.total_reward() == 2.0

    o3 = np.random.rand(80, 80)
    episode.append(data.Step(o3, 1, 1.0, False))

    assert episode.total_reward() == 3.0
Example #16
0
 def _more(cfg):
     for name, value in configs.env.dataset.dict().items():
         setattr(cfg, name, getattr(cfg, name, value))
     cfg.out = configs.BaseConfig({'elements': 1})
     return cfg
Example #17
0
def test_advantage(db):
    env_config = configs.BaseConfig(
        'test_data',
        StepCoder(observation_coder=NumpyCoder(2, dtype=np.float64)))

    rollout = db.create_rollout(env_config)
    obs = []

    episode = rollout.create_episode()
    o1 = np.random.rand(80, 80)
    episode.append(data.Step(o1, 1, 0.0, False))
    o2 = np.random.rand(80, 80)
    episode.append(data.Step(o2, 1, 0.0, False))
    o3 = np.random.rand(80, 80)
    episode.append(data.Step(o3, 1, 1.0, False))
    obs.append([o1, o2, o3])
    episode.end()

    episode = rollout.create_episode()
    o1 = np.random.rand(80, 80)
    episode.append(data.Step(o1, 1, 0.0, False))
    o2 = np.random.rand(80, 80)
    episode.append(data.Step(o2, 1, 0.0, False))
    o3 = np.random.rand(80, 80)
    episode.append(data.Step(o3, 1, 1.0, False))
    obs.append([o1, o2, o3])
    episode.end()

    rollout.finalize()

    dataset = data.RolloutDatasetBase(env_config, rollout)

    a = []
    a.append(1.0 * env_config.discount_factor**2)
    a.append(1.0 * env_config.discount_factor)
    a.append(1.0)
    a.append(1.0 * env_config.discount_factor**2)
    a.append(1.0 * env_config.discount_factor)
    a.append(1.0)

    mu = mean(a)
    sigma = stdev(a)

    adv = [(vl - mu) / (sigma + 1e-12) for vl in a]

    observation, action, reward, advantage = dataset[0]
    assert reward == 0.00
    assert advantage == adv[0]

    observation, action, reward, advantage = dataset[1]
    assert reward == 0.0
    assert advantage == adv[1]

    observation, action, reward, advantage = dataset[2]
    assert reward == 1.0
    assert advantage == adv[2]

    observation, action, reward, advantage = dataset[3]
    assert reward == 0.00
    assert advantage == adv[3]

    observation, action, reward, advantage = dataset[4]
    assert reward == 0.0
    assert advantage == adv[4]

    observation, action, reward, advantage = dataset[5]
    assert reward == 1.0
    assert advantage == adv[5]