Beispiel #1
0
    def reload_obj(self, model_group_name):
        if model_group_name == self.model_group_name:
            return

        if hasattr(self, 'obj_feas'):
            del self.obj_feas
        if hasattr(self, 'objects_vocab'):
            del self.objects_vocab

        # load object features
        obj_fea_name = None
        if model_group_name == 'avg_label':
            obj_fea_name = 'class-fea'
        elif model_group_name == 'onehot_label':
            obj_fea_name = 'class'
        elif model_group_name == 'prob_label':
            obj_fea_name = 'class-prob'

        if obj_fea_name:
            self.obj_feas = []
            for data_split in self.splits:
                if data_split == 'vg':
                    continue
                obj_fname = get_feature_path(data_split, obj_fea_name)
                self.obj_feas.append(np.load(obj_fname))
            if len(self.obj_feas) > 0:
                self.obj_feas = np.vstack(self.obj_feas)

        # load object labels
        if model_group_name in ('onehot_label', 'prob_label'):
            with open('data/objects_vocab.txt') as f:
                self.objects_vocab = f.read().splitlines()
            self.objects_vocab = ['__no_objects__'] + self.objects_vocab

        self.model_group_name = model_group_name
Beispiel #2
0
    def __init__(self, split, model_group_name):
        self.codebook = json.load(open('{}/data.json'.format(cfg.DATA_DIR)))

        data = h5py.File('{}/data.h5'.format(
            cfg.DATA_DIR))['/{}'.format(split)]
        self.img_pos = data['img_pos'].value
        self.que = data['que'].value
        self.que_id = data['que_id'].value
        if 'ans' in data:
            self.ans = data['ans'].value
            if cfg.SOFT_LOSS:
                self.ans = self.ans.astype(np.float32)

        # load image features
        self.splits = cfg[split.upper()].SPLITS
        self.img_feas = []
        for data_split in self.splits:
            if data_split == 'vg':
                continue
            fea_fname = get_feature_path(data_split, 'feature')
            if cfg.LOAD_ALL_DATA:
                img_fea = np.load(fea_fname)
            else:
                img_fea = open_memmap(fea_fname, dtype='float32')
            self.img_feas.append(img_fea)
        self.img_cnts = list(map(len, self.img_feas))

        self.model_group_name = None
        self.reload_obj(model_group_name)
Beispiel #3
0
def get_img_pos(splits, data_ids):
    fea_ids = []
    for split in splits:
        if split == 'vg':
            continue
        fea_ids.append(np.load(get_feature_path(split, 'id')))
    if len(fea_ids) > 0:
        fea_ids = np.hstack(fea_ids)
    id_to_pos = {img_id: i for i, img_id in enumerate(fea_ids)}
    data_poses = [id_to_pos[img_id] for img_id in data_ids]
    return np.array(data_poses, dtype='int64')
Beispiel #4
0
 def _load_box(self):
     data = h5py.File('{}/data.h5'.format(cfg.DATA_DIR))['/test']
     img_pos = data['img_pos'].value
     box_dict_fname = get_feature_path(self._split, 'box')
     return np.load(box_dict_fname)[img_pos]