示例#1
0
def save_focus_img(dataset, all_focus, save_path, changepoints=[]):
    focus_img = util.extract_neighbor(dataset,
                                      all_focus,
                                      range(len(all_focus)),
                                      nb_size=(15, 20))
    for i, img in enumerate(focus_img):
        # focused neighbor image
        file_name = 'focus_img_%d.png' % (i)
        file_path = os.path.join(save_path, file_name)
        imsave(file_path, img)
    print('saved under', save_path)

    pos_cnt = np.zeros(dataset.get_shape()[-2:])
    cp_mask = np.zeros(len(all_focus), dtype=bool)
    cp_mask[changepoints] = True
    save_subpath = util.get_dir(os.path.join(save_path, 'marker'))
    for i, img in enumerate(dataset.get_frame(0, len(all_focus))):
        # for i, img in enumerate(util.remove_mean(dataset.get_frame(0, len(all_focus)), all_focus)):
        # marker
        file_name = 'marker_%d.png' % (i)
        marker_file_path = os.path.join(save_subpath, file_name)
        marker_pos = (all_focus[i] * dataset.get_shape()[2:]).astype(int)
        img[0, marker_pos[0], :] = 1
        img[0, :, marker_pos[1]] = 1
        if cp_mask[i]:
            img = 1 - img
        pos_cnt[marker_pos[0], marker_pos[1]] += 1
        imsave(marker_file_path, img[0])
    print('focus by marker saved under', save_subpath)

    if np.sum(pos_cnt) > 0:
        pos_cnt = np.sqrt(1.0 - (pos_cnt / np.max(pos_cnt) - 1)**2)
        pos_cnt_file_path = os.path.join(save_path, 'counter_pos.png')
        imsave(pos_cnt_file_path, pos_cnt)
        print('position count saved at', pos_cnt_file_path)
示例#2
0
def save_imgs(imgs, save_path):
    save_subpath = util.get_dir(os.path.join(save_path, 'intensity'))
    for i, img in enumerate(imgs):
        file_name = 'intensity_%d.png' % (i)
        file_path = os.path.join(save_subpath, file_name)
        imsave(file_path, util.feature_normalize(img[0]))
    print('focus intensity saved under', save_subpath)
def save_imgs(imgs, save_path, title_extra=False):
    save_subpath = util.get_dir(os.path.join(save_path, 'intensity'))
    for i, img in enumerate(imgs):
        if title_extra:
            file_name = 'intensity_%d_%.2f.png'%(i, title_extra[i])
        else:
            file_name = 'intensity_%d.png'%(i)
        file_path = os.path.join(save_subpath, file_name)
        util.imsave(file_path, util.feature_normalize(img[0]))
    print('focus intensity saved under', save_subpath)
def load_model(prefix, model_id, net_params, *args, **kwargs):
    params = load_param((util.get_dir(prefix), model_id))
    model = ModelFocusCNN(
        image_shape=(84, 84),
        net_params=net_params,
        *args,
        **kwargs,
    )
    model.set_parameters(params)

    return model
示例#5
0
    def __init__(self, dim, *args, **kwargs):
        self.dim = dim
        self.id =  kwargs.get('id', str(np.random.randint(42000, 42999)))
        self.cmaes_params = kwargs.get('cmaes_params', {})
        self.save_path = util.get_dir(kwargs.get('save_path', 
                                      os.path.join('cmaes_soln', self.id)))
        self.max_niter = kwargs.get('max_niter', 100)
        self.nproc = kwargs.get('nproc', 1)
        self.cheating = kwargs.get('cheating', None)
        self.file_id = 0

        # initialize CMA-ES core
        self.reset()

        logger.info('created wrapper with id= %s', self.id)
示例#6
0
    def __init__(self, game_name, Actor, n_state, save_path, *args, **kwargs):

        # create OpenAI Atari
        self.save_path = util.get_dir(save_path)
        self.atari_env = SubprocVecEnv(
            [make_env('BreakoutNoFrameskip-v4', 1234, 0, self.save_path)])
        self.frame_shape = self.atari_env.observation_space.shape[-2:]
        self.n_state = n_state
        self.binarize = kwargs.get('binarize', None)

        # actor for auto run
        self.action_space = self.atari_env.action_space.n
        self.actor = Actor(self.action_space)

        # TODO: online generatation
        self._generate_all_states()
    else:
        print('using simple linear changepoint detector')
        cpd = LinearCPD(np.pi/4.0)

    """
    Load model
    """
    net_params = json.loads(open(args.net).read())
    r_model = load_model(
        prefix,
        model_id,
        net_params=net_params,
        use_prior=args.prior,
        argmax_mode=args.argmax_mode,
    )
    save_path = util.get_dir(os.path.join(prefix, 'focus_img_%s'%model_id))
    if plot_flags['plot_filter']:
        plot_model_filter(r_model, save_path)

    # boosting with trained models
    if args.boost:
        # partial ball model to be boosted
        ball_model_id = 'results/cmaes_soln/focus_atari_breakout/42080_16.npy'
        ball_net_params_text = open('objRecog/net_params/two_layer.json').read()
        ball_net_params = json.loads(ball_net_params_text)
        ball_params = load_param(ball_model_id)
        ball_model = ModelFocusCNN(
            image_shape=(84, 84),
            net_params=ball_net_params,
        )
        ball_model.set_parameters(ball_params)