Пример #1
0
def main():
    print_config()

    dataset = load_dataset(CONFIG['dataset'], CONFIG['dataset_path'])

    env = TextLocEnv(dataset.image_paths, dataset.bounding_boxes)
    m = CustomModel(10)
    vs = [m(env.reset())]
    g = c.build_computational_graph(vs)
    with open('graph.dot', 'w') as o:
        o.write(g.dump())
def main():
    print_config()

    relative_paths = np.loadtxt(CONFIG['imagefile_path'], dtype=str)
    images_base_path = os.path.dirname(CONFIG['imagefile_path'])
    absolute_paths = [images_base_path + i.strip('.') for i in relative_paths]
    bboxes = np.load(CONFIG['boxfile_path'], allow_pickle=True)

    env = TextLocEnv(absolute_paths, bboxes, -1)
    m = CustomModel(10)
    vs = [m(env.reset())]
    g = c.build_computational_graph(vs)
    with open('graph.dot', 'w') as o:
        o.write(g.dump())
Пример #3
0
def main():
    print_config()

    relative_paths = np.loadtxt(CONFIG['imagefile_path'], dtype=str)
    images_base_path = os.path.dirname(CONFIG['imagefile_path'])
    absolute_paths = [images_base_path + i.strip('.') for i in relative_paths]
    bboxes = np.load(CONFIG['boxfile_path'], allow_pickle=True)

    env = TextLocEnv(absolute_paths, bboxes, -1)
    q_func = chainerrl.q_functions.SingleModelStateQFunctionWithDiscreteAction(
        CustomModel(9))
    optimizer = chainer.optimizers.Adam(eps=1e-2)
    optimizer.setup(q_func)
    replay_buffer = chainerrl.replay_buffer.ReplayBuffer(
        capacity=CONFIG['replay_buffer_capacity'])

    explorer = chainerrl.explorers.ConstantEpsilonGreedy(
        epsilon=0, random_action_func=env.action_space.sample)

    agent = chainerrl.agents.DQN(
        q_func,
        optimizer,
        replay_buffer,
        CONFIG['gamma'],
        explorer,
        gpu=CONFIG['gpu_id'],
        replay_start_size=CONFIG['replay_start_size'],
        update_interval=CONFIG['update_interval'],
        target_update_interval=CONFIG['target_update_interval'])

    agent.load(CONFIG['resultdir_path'] + '/best')
    actions = defaultdict(int)
    with open('iou.txt', 'w') as f:
        i = 0
        for j in range(100):
            obs = env.reset()
            done = False
            while (not done) and i < 100:
                #print(i,j)
                action = agent.act(obs)
                actions[ACTION_MEANINGS[action]] += 1
                obs, reward, done, info = env.step(action)

                print(ACTION_MEANINGS[action], reward, done, info)
                if done:
                    f.write(f'{env.iou}\n')
                #input()
                i += 1
def create_environment(imagefile='image_locations.txt', boxfile='bounding_boxes.npy', gpu=0):
    relative_paths = np.loadtxt(imagefile, dtype=str)
    images_base_path = os.path.dirname(imagefile)
    absolute_paths = [images_base_path + i.strip('.') for i in relative_paths]

    bboxes = np.load(boxfile)

    return TextLocEnv(absolute_paths, bboxes, gpu)
Пример #5
0
def create_env(dataset, config, **kwargs):
    return TextLocEnv(dataset.image_paths,
                      dataset.bounding_boxes,
                      playout_episode=config['playout_episode'],
                      premasking=config['premasking'],
                      max_steps_per_image=config['max_steps_per_image'],
                      bbox_scaling=config['bbox_scaling'],
                      bbox_transformer=config['bbox_transformer'],
                      ior_marker_type=config['ior_marker_type'],
                      has_termination_action=config['has_termination_action'],
                      **kwargs)
Пример #6
0
def create_environment(conf):
    from text_localization_environment import TextLocEnv
    import numpy as np
    import os
    imagefile = conf.get(
        'image', '../last_year/data/simple_generated_data/image_locations.txt')
    boxfile = conf.get(
        'image', '../last_year/data/simple_generated_data/bounding_boxes.npy')
    gpu = conf.get('gpu', 0)
    relative_paths = np.loadtxt(imagefile, dtype=str)
    images_base_path = os.path.dirname(imagefile)
    absolute_paths = [images_base_path + i.strip('.') for i in relative_paths]
    bboxes = np.load(boxfile)
    return TextLocEnv(absolute_paths, bboxes, gpu)
Пример #7
0
def test_mask_array_cpu():
    masker = ImageMasker(0)
    image = Image.new("RGB", (4, 4), color=(255, 255, 255))

    masking_box = [1, 1, 2, 2]

    array_module = np
    actual_image = np.array(image)
    actual_image = masker.mask_array(
        actual_image, TextLocEnv.to_four_corners_array(masking_box),
        array_module)

    expected_image = Image.new("RGB", (4, 4), color=(255, 255, 255))
    draw = ImageDraw.Draw(expected_image)
    draw.rectangle(masking_box, fill=0)
    del draw
    expected_image = np.array(expected_image)

    assert ((actual_image == expected_image).all())
Пример #8
0
def create_env(env_name: str, atari: bool, max_episode_steps=None):
    if atari:
        env = make_atari(env_name)
        env = wrap_deepmind(env, frame_stack=True)
        env = wrap_pytorch(env)
    else:
        # env = gym.make(env_name)
        dataset = SignDataset(path='../../data/600_3_signs_3_words',
                              split='validation')
        env = TextLocEnv(dataset.images,
                         dataset.gt,
                         playout_episode=False,
                         premasking=True,
                         max_steps_per_image=100,
                         bbox_scaling=0,
                         bbox_transformer='base',
                         ior_marker_type='cross',
                         has_termination_action=False,
                         has_intermediate_reward=False)
    return env
Пример #9
0
def test_to_four_corners_array():
    two_corners_aabb = np.array([0, 0, 20, 10])
    four_corners_array = TextLocEnv.to_four_corners_array(two_corners_aabb)
    assert ((four_corners_array == np.array([
        np.array([
            np.array([20, 10]),
            np.array([0, 10]),
            np.array([0, 0]),
            np.array([20, 0])
        ]),
        np.array([
            np.array([20, 10]),
            np.array([0, 10]),
            np.array([0, 0]),
            np.array([20, 0])
        ]),
        np.array([
            np.array([20, 10]),
            np.array([0, 10]),
            np.array([0, 0]),
            np.array([20, 0])
        ])
    ])).all())
def main():
    print_config()

    relative_paths = np.loadtxt(CONFIG['imagefile_path'], dtype=str)
    images_base_path = os.path.dirname(CONFIG['imagefile_path'])
    absolute_paths = [images_base_path + i.strip('.') for i in relative_paths]
    bboxes = np.load(CONFIG['boxfile_path'], allow_pickle=True)

    env = TextLocEnv(absolute_paths, bboxes, CONFIG['gpu_id'])

    n_actions = env.action_space.n
    q_func = chainerrl.q_functions.SingleModelStateQFunctionWithDiscreteAction(
        CustomModel(n_actions))
    if CONFIG['gpu_id'] != -1:
        q_func = q_func.to_gpu(CONFIG['gpu_id'])

    # Use Adam to optimize q_func. eps=1e-2 is for stability.
    optimizer = chainer.optimizers.Adam(eps=CONFIG['epsilon'],
                                        amsgrad=True,
                                        alpha=CONFIG['learning_rate'])
    optimizer.setup(q_func)

    # Use epsilon-greedy for exploration
    explorer = chainerrl.explorers.LinearDecayEpsilonGreedy(
        start_epsilon=CONFIG['start_epsilon'],
        end_epsilon=CONFIG['end_epsilon'],
        decay_steps=CONFIG['decay_steps'],
        random_action_func=env.action_space.sample)

    # DQN uses Experience Replay.
    # Specify a replay buffer and its capacity.
    replay_buffer = chainerrl.replay_buffer.EpisodicReplayBuffer(
        capacity=CONFIG['replay_buffer_capacity'])

    # Now create an agent that will interact with the environment.
    agent = chainerrl.agents.DQN(
        q_func,
        optimizer,
        replay_buffer,
        CONFIG['gamma'],
        explorer,
        gpu=CONFIG['gpu_id'],
        replay_start_size=CONFIG['replay_start_size'],
        update_interval=CONFIG['update_interval'],
        target_update_interval=CONFIG['target_update_interval'])

    logging.basicConfig(level=logging.INFO, stream=sys.stdout, format='')

    eval_run_count = 10

    timestr = time.strftime("%Y%m%d-%H%M%S")
    agentClassName = agent.__class__.__name__[:10]

    step_hooks = []
    logger = None
    if CONFIG['use_tensorboard']:
        writer = SummaryWriter("tensorboard/tensorBoard_exp_" + timestr + "_" +
                               agentClassName)
        step_hooks = [TensorBoardLoggingStepHook(writer)]
        handler = TensorBoardEvaluationLoggingHandler(writer, agent,
                                                      eval_run_count)
        logger = logging.getLogger()
        logger.addHandler(handler)

        gradients_weights_log_interval = 100
        optimizer.add_hook(
            TensorboardGradientPlotter(
                summary_writer=writer,
                log_interval=gradients_weights_log_interval))

    # save config file to results dir after initializing agent
    write_config()

    # Overwrite the normal evaluation method
    # chainerrl.experiments.evaluator.run_evaluation_episodes = run_localization_evaluation_episodes

    train_agent_with_evaluation(
        agent,
        env,
        steps=CONFIG['steps'],  # Train the agent for no of steps
        eval_n_episodes=CONFIG[
            'eval_n_episodes'],  # episodes are sampled for each evaluation
        eval_n_steps=None,
        train_max_episode_len=CONFIG[
            'train_max_episode_len'],  # Maximum length of each episodes
        eval_interval=CONFIG[
            'eval_interval'],  # Evaluate the agent after every no of steps
        outdir=CONFIG['resultdir_path'],  # Save everything to directory
        step_hooks=step_hooks,
        logger=logger)

    agent.save('agent_' + timestr + "_" + agentClassName)
def main(steps, gpu, imagefile, boxfile, tensorboard):
    print(steps)
    print(gpu)
    print(imagefile)
    print(boxfile)

    relative_paths = np.loadtxt(imagefile, dtype=str)
    images_base_path = os.path.dirname(imagefile)
    absolute_paths = [images_base_path + i.strip('.') for i in relative_paths]
    bboxes = np.load(boxfile)

    env = TextLocEnv(absolute_paths, bboxes, gpu)

    obs_size = 2139
    n_actions = env.action_space.n
    q_func = chainerrl.q_functions.FCStateQFunctionWithDiscreteAction(
        obs_size, n_actions,
        n_hidden_layers=2, n_hidden_channels=1024)
    if gpu != -1:
        q_func = q_func.to_gpu(gpu)

    # Use Adam to optimize q_func. eps=1e-2 is for stability.
    optimizer = chainer.optimizers.Adam(eps=1e-2)
    optimizer.setup(q_func)

    # Set the discount factor that discounts future rewards.
    gamma = 0.95

    # Use epsilon-greedy for exploration
    explorer = chainerrl.explorers.LinearDecayEpsilonGreedy(
        start_epsilon=1.0,
        end_epsilon=0.1,
        decay_steps=300000,
        random_action_func=env.action_space.sample)

    # DQN uses Experience Replay.
    # Specify a replay buffer and its capacity.
    replay_buffer = chainerrl.replay_buffer.ReplayBuffer(capacity=10 ** 6)

    # Now create an agent that will interact with the environment.
    agent = chainerrl.agents.DQN(
        q_func, optimizer, replay_buffer, gamma, explorer,
        gpu=gpu,
        replay_start_size=500, update_interval=1,
        target_update_interval=100)

    logging.basicConfig(level=logging.INFO, stream=sys.stdout, format='')

    eval_run_count = 10

    timestr = time.strftime("%Y%m%d-%H%M%S")
    agentClassName = agent.__class__.__name__[:10]

    step_hooks = []
    logger = None
    if tensorboard:
        writer = SummaryWriter("tensorboard/tensorBoard_exp_" + timestr + "_" + agentClassName)
        step_hooks = [TensorBoardLoggingStepHook(writer)]
        handler = TensorBoardEvaluationLoggingHandler(writer, agent, eval_run_count)
        logger = logging.getLogger()
        logger.addHandler(handler)

    # Overwrite the normal evaluation method
    chainerrl.experiments.evaluator.run_evaluation_episodes = run_localization_evaluation_episodes

    train_agent_with_evaluation(
        agent, env,
        steps=steps,  # Train the agent for 5000 steps
        eval_n_runs=eval_run_count,  # 10 episodes are sampled for each evaluation
        max_episode_len=50,  # Maximum length of each episodes
        eval_interval=500,  # Evaluate the agent after every 100 steps
        outdir='result',  # Save everything to 'result' directory
        step_hooks=step_hooks,
        logger=logger)

    agent.save('agent_' + timestr + "_" + agentClassName)