Ejemplo n.º 1
0
 def __init__(self, content, retina=False):
     self.CAMERA_INITIAL_ANGLE_V = deg2rad(10.0)
     # TODO: 他の環境も指定できるようにする
     self.content = content
     self.env = Environment(self.content)
     self.egocentric_images = None
     self.allocentric_images = None
     self.retina = Retina() if retina else None
Ejemplo n.º 2
0
def save_ocoloenv_images(retina, random_rate, retry, datasets, dirname, suffix):
    for content_id, content in enumerate(make_contents(), 1):
        sys.stderr.write('generating images (content_id == {})...'.format(content_id))
        sys.stderr.flush()
        env = Environment(content, on_buffer_width=128, retina=retina)
        for datatype, n in datasets:
            prefix = '{}/{}/{}/'.format(dirname, datatype, content_id)
            subprocess.call(['mkdir', '-p', prefix])
            for i in range(n):
                filename = '{}{}{}'.format(prefix, i, suffix)
                save_screen_images(env, random_rate, retry, filename)
        env.close()
        sys.stderr.write('done\n')
Ejemplo n.º 3
0
    def evaluate(self, agent):
        print("content:{} difficulty:{} start".format(self.content_id, self.difficulty))
        
        content_class_name = content_class_names[self.content_id-1]
        content_class = globals()[content_class_name]
        if self.difficulty >= 0:
            content = content_class(difficulty=self.difficulty)
        else:
            content = content_class()

        env = Environment(content)
        obs = env.reset()

        reward = 0
        done = False

        task_reward = 0

        results = []

        switch_count = 0
        switch_correct = 0
        for i in range(self.duration):
            image, angle = obs['screen'], obs['angle']
            # Choose action by the agent's decision
            action = agent(image, angle, reward, done)
            task = agent.environment.current_task
            if task is not None and task != 'fixation':
                switch_count += 1
                if task == task_names[self.content_id-1]:
                    switch_correct += 1
            # Foward environment one step
            obs, reward, done, info = env.step(action)

            if 'result' in info:
                result = TrialResult(self.content_id,
                                     self.difficulty,
                                     reward,
                                     info,)
                results.append(result)
            
            task_reward += reward

            assert(done is not True)

        print("content:{} difficulty:{} end, reward={}".format(self.content_id,
                                                               self.difficulty,
                                                               task_reward))
        print('switch acc: {}'.format(float(switch_correct) / switch_count))
        return results, task_reward
Ejemplo n.º 4
0
    def __init__(self,
                 content,
                 display_size,
                 model_name='1006-1538806261.ckpt',
                 use_ppo_models=False):
        pygame.init()

        self.surface = pygame.display.set_mode(display_size, 0, 24)
        pygame.display.set_caption('oculomotor')

        self.retina = Retina()
        self.lip = LIP()
        self.vc = VC()
        self.pfc = PFC()
        self.fef = FEF()
        self.bg = BG(model_name=model_name, use_saved_models=use_ppo_models)
        self.sc = SC()
        self.hp = HP()
        self.cb = CB()

        self.step = 0
        self.episode = 0

        self.agent = Agent(retina=self.retina,
                           lip=self.lip,
                           vc=self.vc,
                           pfc=self.pfc,
                           fef=self.fef,
                           bg=self.bg,
                           sc=self.sc,
                           hp=self.hp,
                           cb=self.cb)

        self.env = Environment(content)
        obs = self.env.reset()

        self.last_image = obs['screen']
        self.last_angle = obs['angle']
        self.last_reward = 0
        self.last_done = False

        self.episode_reward = 0

        self.font = pygame.font.Font(None, 20)

        self.display_size = display_size
Ejemplo n.º 5
0
    def __init__(self, content, display_size):
        pygame.init()

        self.surface = pygame.display.set_mode(display_size, 0, 24)
        pygame.display.set_caption('oculomotor')

        self.retina = Retina()
        self.lip = LIP()
        self.vc = VC()
        self.pfc = PFC()
        self.fef = FEF()
        self.bg = BG()
        self.sc = SC()
        self.hp = HP()
        self.cb = CB()

        self.agent = Agent(
            retina=self.retina,
            lip=self.lip,
            vc=self.vc,
            pfc=self.pfc,
            fef=self.fef,
            bg=self.bg,
            sc=self.sc,
            hp=self.hp,
            cb=self.cb
        )

        self.env = Environment(content)

        self.pfc.load_model('data/pfc_task_detection.pth')
        #self.bg.load_model('data/bg_rl.pth')

        obs = self.env.reset()

        self.last_image = obs['screen']
        self.last_angle = obs['angle']
        self.last_reward = 0
        self.last_done = False

        self.episode_reward = 0

        self.font = pygame.font.Font(None, 20)

        self.display_size = display_size
Ejemplo n.º 6
0
    def calc_fps(self, content_type, with_agent):
        content = self.get_content(content_type)

        if with_agent:
            agent = Agent(
                retina=Retina(),
                lip=LIP(),
                vc=VC(),
                pfc=PFC(),
                fef=FEF(),
                bg=BG(),
                sc=SC(),
                hp=HP(),
                cb=CB()
            )
    
        env = Environment(content)
        obs = env.reset()

        reward = 0
        done = False
        
        step_size = 1000
        
        step = 0

        start = time.time()
        
        for i in range(step_size):
            if with_agent:
                image, angle = obs['screen'], obs['angle']
                dummy_action = agent(image, angle, reward, done)
            
            dh = np.random.uniform(low=-0.05, high=0.05)
            dv = np.random.uniform(low=-0.05, high=0.05)
            action = np.array([dh, dv])
            
            obs, reward, done, _ = env.step(action)
            step += 1
            if done:
                obs = env.reset()

        elapsed_time = time.time() - start
        fps = step_size / elapsed_time
        return fps
Ejemplo n.º 7
0
    def evaluate(self, agent):
        print("content:{} difficulty:{} start".format(self.content_id,
                                                      self.difficulty))

        content_class_name = content_class_names[self.content_id - 1]
        content_class = globals()[content_class_name]
        if self.difficulty >= 0:
            content = content_class(difficulty=self.difficulty)
        else:
            content = content_class()

        env = Environment(content)
        obs = env.reset()

        reward = 0
        done = False

        task_reward = 0

        results = []

        for i in range(self.duration):
            image, angle = obs['screen'], obs['angle']
            # Choose action by the agent's decision
            action = agent(image, angle, reward, done)
            # Foward environment one step
            obs, reward, done, info = env.step(action)

            if 'result' in info:
                result = TrialResult(
                    self.content_id,
                    self.difficulty,
                    reward,
                    info,
                )
                results.append(result)

            task_reward += reward

            assert (done is not True)

        print("content:{} difficulty:{} end, reward={}".format(
            self.content_id, self.difficulty, task_reward))
        return results, task_reward
Ejemplo n.º 8
0
def check_offscreen():
    content = PointToTargetContent()
    env = Environment(content)

    frame_size = 10

    for i in range(frame_size):
        dh = np.random.uniform(low=-0.02, high=0.02)
        dv = np.random.uniform(low=-0.02, high=0.02)
        action = np.array([dh, dv])
        obs, reward, done, info = env.step(action)

        image = obs['screen']

        save_img(image)

        if done:
            print("Episode terminated")
            obs = env.reset()
Ejemplo n.º 9
0
    def __init__(self, content, display_size):
        pygame.init()

        self.surface = pygame.display.set_mode(display_size, 0, 24)
        pygame.display.set_caption('oculomotor')

        self.retina = Retina()
        self.lip = LIP()
        self.vc = VC()
        self.pfc = PFC()
        self.fef = FEF()
        #self.bg = BG() # Train mode
        self.bg = BG(train=False, backprop=False)  # Test mode
        self.bg.agent.load("./results/exp20_after")  # Test mode
        self.sc = SC()
        self.hp = HP()
        self.cb = CB()

        self.agent = Agent(retina=self.retina,
                           lip=self.lip,
                           vc=self.vc,
                           pfc=self.pfc,
                           fef=self.fef,
                           bg=self.bg,
                           sc=self.sc,
                           hp=self.hp,
                           cb=self.cb)

        self.env = Environment(content)
        obs = self.env.reset()

        self.last_image = obs['screen']
        self.last_angle = obs['angle']
        self.last_reward = 0
        self.last_done = False

        self.episode_reward = 0

        self.font = pygame.font.Font(None, 20)

        self.display_size = display_size
Ejemplo n.º 10
0
def train(content, step_size, logger, model_name, use_ppo_models):
    retina = Retina()
    lip = LIP()
    vc = VC()
    pfc = PFC()
    fef = FEF()
    bg = BG(model_name, use_saved_models=use_ppo_models)
    sc = SC()
    hp = HP()
    cb = CB()

    agent = Agent(
        retina=retina,
        lip=lip,
        vc=vc,
        pfc=pfc,
        fef=fef,
        bg=bg,
        sc=sc,
        hp=hp,
        cb=cb
    )

    env = Environment(content)

    # If your training code is written inside BG module, please add model load code here like.
    #
    #   bg.load_model("model.pkl")
    #
    # When runnning with Docker, directory under 'oculomotor/' is volume shared
    # with the host, so you can load/save the model data at anywhere under 'oculomotor/' dir.
    
    obs = env.reset()
    
    reward = 0
    done = False
    
    episode_reward = 0
    episode_count = 0

    # Add initial reward log
    logger.log("episode_reward", episode_reward, episode_count)
    
    for i in range(step_size):
        image, angle = obs['screen'], obs['angle']
        # Choose action by the agent's decision
        action = agent(image, angle, reward, done)
        # Foward environment one step
        obs, reward, done, _ = env.step(action)
        episode_reward += reward

        # TODO: remove this
        done = done or i % 180 == 0

        if done:
            obs = env.reset()
            print(
                "\033[93m {} step, {} episode reward={} \033[0m".format(
                    i, episode_count, episode_reward
                )
            )

            # Store log for tensorboard graph
            episode_count += 1
            logger.log("episode_reward", episode_reward, episode_count)
            episode_reward = 0
            # Plase add model save code as you like.
          if i % 5000 == 0:
              bg.save_model()
    print("training finished")
    logger.close()
Ejemplo n.º 11
0
        help="Flag to debug execute step by step with one key press",
        type=bool,
        default=False)

    args = parser.parse_args()

    if args.content == Contents.POINT_TO_TARGET:
        content = PointToTargetContent()
    elif args.content == Contents.CHANGE_DETECTION:
        content = ChangeDetectionContent()
    elif args.content == Contents.ODD_ONE_OUT:
        content = OddOneOutContent()
    elif args.content == Contents.VISUAL_SEARCH:
        content = VisualSearchContent()
    elif args.content == Contents.MULTIPLE_OBJECT_TRACKING:
        content = MultipleObjectTrackingContent()
    elif args.content == Contents.RANDOM_DOT_MOTION_DISCRIMINATION:
        content = RandomDotMotionDiscriminationContent()
    else:
        print("Unknown argument")
        sys.exit(1)

    env = Environment(content)
    env.render()  # env.window is created here

    handler = KeyHandler(env, args.step_debug)

    pyglet.app.run()

    env.close()
        content = OddOneOutContent()
    elif args.content == Contents.VISUAL_SEARCH:
        content = VisualSearchContent()
    elif args.content == Contents.MULTIPLE_OBJECT_TRACKING:
        content = MultipleObjectTrackingContent()
    elif args.content == Contents.RANDOM_DOT_MOTION_DISCRIMINATION:
        content = RandomDotMotionDiscriminationContent()
    elif args.content == Contents.RED_CURSOR:
        content = None
    else:
        print("Unknown argument")
        sys.exit(1)

    env = Environment(content,
                      on_buffer_width=128,
                      skip_red_cursor=args.skip_red_cursor,
                      retina=args.retina,
                      saliency=args.saliency,
                      diff=args.diff) if content else RedCursorEnvironment(
                          None,
                          on_buffer_width=128,
                          retina=args.retina,
                          saliency=args.saliency,
                          diff=args.diff)
    env.render()  # env.window is created here

    handler = KeyHandler(env, args.step_debug)

    pyglet.app.run()

    env.close()
Ejemplo n.º 13
0
    elif args.content == Contents.VISUAL_SEARCH:
        content = VisualSearchContent()
    elif args.content == Contents.MULTIPLE_OBJECT_TRACKING:
        content = MultipleObjectTrackingContent()
    elif args.content == Contents.RANDOM_DOT_MOTION_DISCRIMINATION:
        content = RandomDotMotionDiscriminationContent()
    elif args.content == Contents.RED_CURSOR:
        content = None
    else:
        print("Unknown argument")
        sys.exit(1)

    env = Environment(
        content,
        on_buffer_width=640,
        skip_red_cursor=args.skip_red_cursor,
        retina=args.retina,
        saliency=args.saliency,
        diff=args.diff,
        opt_flow=args.opt_flow) if content else RedCursorEnvironment(
            None,
            on_buffer_width=128,
            retina=args.retina,
            saliency=args.saliency,
            diff=args.diff,
            opt_flow=args.opt_flow)
    env.render()  # env.window is created here

    handler = KeyHandler(env, args.step_debug)

    pyglet.app.run()
Ejemplo n.º 14
0
def train(content, step_size, logger):
    retina = Retina()
    lip = LIP()
    vc = VC()
    pfc = PFC()
    fef = FEF()
    bg = BG(init_weight_path="./data/bg.pth")
    sc = SC()
    hp = HP()
    cb = CB()

    agent = Agent(retina=retina,
                  lip=lip,
                  vc=vc,
                  pfc=pfc,
                  fef=fef,
                  bg=bg,
                  sc=sc,
                  hp=hp,
                  cb=cb)

    env = Environment(content)

    # If your training code is written inside BG module, please add model load code here like.
    #
    #   bg.load_model("model.pkl")
    #
    # When runnning with Docker, directory under 'oculomotor/' is volume shared
    # with the host, so you can load/save the model data at anywhere under 'oculomotor/' dir.

    obs = env.reset()

    reward = 0
    done = False

    episode_reward = 0
    episode_count = 0
    step = 0

    # Add initial reward log
    logger.log("episode_reward", episode_reward, episode_count)

    for i in range(step_size):
        image, angle = obs['screen'], obs['angle']
        # Choose action by the agent's decision
        action = agent(image, angle, reward, done)
        # Foward environment one step
        obs, reward, done, _ = env.step(action)

        episode_reward += reward
        step += 1
        if step % EPI_THRESHOLD == 0:
            done = True

        if done:
            obs = env.reset()
            print("episode reward={}".format(episode_reward))

            # Store log for tensorboard graph
            episode_count += 1
            logger.log("episode_reward", episode_reward, episode_count)

            episode_reward = 0
            step = 0

            # Plase add model save code as you like.
            #
            # if i % 10 == 0:
            #     bg.save_model("model.pkl")

    print("training finished")
    logger.close()
Ejemplo n.º 15
0
def train(content, step_size, logger, log_path):
    starttime = time.time()
    retina = Retina()
    lip = LIP()
    vc = VC()
    pfc = PFC()
    fef = FEF()
    bg = BG(log_path=log_path)
    sc = SC()
    hp = HP()
    cb = CB()

    agent = Agent(retina=retina,
                  lip=lip,
                  vc=vc,
                  pfc=pfc,
                  fef=fef,
                  bg=bg,
                  sc=sc,
                  hp=hp,
                  cb=cb)

    env = Environment(content)

    # If your training code is written inside BG module, please add model load code here like.
    #
    #   bg.load_model("model.pkl")
    #
    # When runnning with Docker, directory under 'oculomotor/' is volume shared
    # with the host, so you can load/save the model data at anywhere under 'oculomotor/' dir.

    obs = env.reset()

    reward = 0
    done = False

    episode_reward = 0
    episode_count = 0

    # Add initial reward log
    logger.log("episode_reward", episode_reward, episode_count)
    #step_size = 10800
    while True:
        for i in range(step_size):
            image, angle = obs['screen'], obs['angle']
            # Choose action by the agent's decision
            action = agent(image, angle, reward, done)
            # Foward environment one step
            obs, reward, done, _ = env.step(action)

            episode_reward += reward

            if done:
                print("episode count={}".format(episode_count))
                obs = env.reset()
                print("episode reward={}".format(episode_reward))

                # Store log for tensorboard graph
                episode_count += 1
                logger.log("episode_reward", episode_reward, episode_count)
                episode_reward = 0
                # Plase add model save code as you like.
                bg.save_model(str(episode_count) + "model")

        episode_count += 1
        logger.log("episode_reward", episode_reward, episode_count)
        episode_reward = 0
        bg.save_model(str(episode_count) + "model")

    print("training finished")
    logger.close()
    endtime = time.time()
    print('whole time:', endtime - starttime)