示例#1
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
示例#2
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
示例#3
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
示例#4
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
示例#5
0
def evaluate(logger, log_path):
    retina = Retina()
    lip = LIP()
    vc = VC()
    pfc = PFC()
    fef = FEF()
    bg = BG(use_saved_models=True)
    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
    )

    #bg.load_model("model.pkl")

    total_reward = 0

    all_trial_results = []
    
    for i, task in enumerate(tasks):
        trial_results, task_reward = task.evaluate(agent)
        all_trial_results += trial_results
        total_reward += task_reward
        logger.log("evaluation_reward", total_reward, i)

    # Save result csv files
    save_results(all_trial_results, log_path)
    
    print("evaluation finished:")
    logger.close()
示例#6
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()
示例#7
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()
示例#8
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)