示例#1
0
    def __init__(self, params, result):
        self.result = result
        self.batch_size = params['batch_size']
        self.number_classes = params['number_classes']
        self.img_ext = params['img_ext']
        self.dataset_root = params['dataset_root']
        self.im_shape = params['im_shape']

        # get list of image indexes.
        list_file = params['split'] + '.csv'
        self.indexlist = [
            line.rstrip('\n').split(',')[0]
            for line in open(osp.join(self.dataset_root, list_file))
        ]
        self.labellist = [
            line.rstrip('\n').split(',')[1]
            for line in open(osp.join(self.dataset_root, list_file))
        ]

        self._cur = 0  # current image
        # this class does some simple data-manipulations
        self.transformer = SimpleTransformer()

        print("BatchLoader initialized with {} images".format(
            len(self.indexlist)))
    def __init__(self, params, result):
        self.result = result
        self.batch_size = params['batch_size']
        self.data_root = params['data_root']
        self.im_shape = params['im_shape']
        self.split = params['split']
        self.mean_file = params['mean_file']

        list_file = '../prepare_data/AFEW-VA/crop/{}_data.txt'.format(
            self.split)
        self.indexlist = [line.split(' ')[0] for line in open(list_file)]
        # get list of image indexes.
        # Read the mat file and assign to X
        mat_contents = scipy.io.loadmat(
            '../prepare_data/AFEW-VA/crop/{}_labels.mat'.format(self.split))
        self.X = np.zeros(mat_contents['{}_labels'.format(self.split)].shape,
                          dtype=np.float16)
        self.X[:, :] = mat_contents['{}_labels'.format(self.split)]

        self._cur = 0  # current image
        # this class does some simple data-manipulations
        self.transformer = SimpleTransformer(self.mean_file)

        print("BatchLoader initialized with {} images".format(
            len(self.indexlist)))
示例#3
0
    def __init__(self, params, result):
        self.result = result
        self.batch_size = params['batch_size']
        # get list of image indexs
        self.indexlist = [
            line.rstrip('\n') for line in open(params['data']).readlines()
        ]

        self._cur = 0  # current image
        # this class does some simple data-manipulation
        self.transformer = SimpleTransformer()

        print "BatchLoader initialized with {} images".format(
            len(self.indexlist))
示例#4
0
    def __init__(self, params, result):
        self.result = result
        self.batch_size = params['batch_size']
        self.yelp_picture_root = params['yelp_picture_root']
        self.yelp_csv_root = params['yelp_csv_root']
        self.im_shape = params['im_shape']
        self.split = params['split']

        # get list of image indexes.
        if self.split in ["train", "validation"]:
            list_csv = self.yelp_csv_root + self.split + '_photo_to_biz_ids2.csv'
        else:
            list_csv = self.yelp_csv_root + self.split + '_photo_to_biz.csv'
        self.image_key = []
        with open(list_csv) as csv_file:
            reader = csv.DictReader(csv_file)
            for row in reader:
                self.image_key.append(row)

        self._cur = 0  # current image
        # this class does some simple data-manipulations
        self.transformer = SimpleTransformer()

        print "BatchLoader initialized with {} images".format(
            len(self.image_key))

        if self.split in ["train", "validation", "poster"]:
            attributes_csv = osp.join(self.yelp_csv_root, self.split + '2.csv')
            self.attributes_dict = {}

            if self.split == "poster":
                attributes_csv = osp.join(self.yelp_picture_root,
                                          self.split + '.csv')

            with open(attributes_csv) as csv_file:
                reader = csv.DictReader(csv_file)
                for row in reader:
                    attr_string = row["labels"]
                    self.attributes_dict[row["business_id"]] = [
                        int(label) for label in row["labels"].split()
                    ]

        if self.split == 'validation':
            lmdb_dir = self.yelp_picture_root + 'val_lmdb'
        else:
            lmdb_dir = self.yelp_picture_root + self.split + '_lmdb'
        lmdb_env = lmdb.open(lmdb_dir)
        self.lmdb_txn = lmdb_env.begin()
        self.lmdb_cursor = self.lmdb_txn.cursor()
    def __init__(self, params, result):
        self.result = result
        self.batch_size = params['batch_size']
        self.pascal_root = params['pascal_root']
        self.im_shape = params['im_shape']
        # get list of image indexes.
        list_file = params['split'] + '.txt'
        self.indexlist = [line.rstrip('\n') for line in open(
            osp.join(self.pascal_root, 'ImageSets/Main', list_file))]
        self._cur = 0  # current image
        # this class does some simple data-manipulations
        self.transformer = SimpleTransformer()

        print "BatchLoader initialized with {} images".format(
            len(self.indexlist))
示例#6
0
def createPatch(x, y, label):
    print x, y
    patch = im[x - half_image_size:x + half_image_size,
               y - half_image_size:y + half_image_size]

    print np.mean(patch[:, :, 0]), np.mean(patch[:, :, 1]), np.mean(patch[:, :,
                                                                          2])
    st = SimpleTransformer()
    patch = st.preprocess(patch)  # no mean
    savePatch(patch, x, y, label)
    print np.mean(patch[:, :, 0]), np.mean(patch[:, :, 1]), np.mean(patch[:, :,
                                                                          2])
    bgrMean(np.mean(patch[:, :, 0]), np.mean(patch[:, :, 1]),
            np.mean(patch[:, :, 2]))

    return patch.tobytes()
示例#7
0
    def __init__(self, params, lexicon, channel):

        self.im_shape = params['im_shape']
        self.data_folder = params['data_folder']
        self.lexicon = lexicon
        self.channel = channel

        self.indexlist = [
            line.rstrip('\n').split(",") for line in open(
                osp.join(self.data_folder, params['split'] +
                         '.txt'), 'r').readlines()
        ]

        self._cur = 0
        self.transformer = SimpleTransformer(channel)

        print "BatchLoader initialized with {} images".format(
            len(self.indexlist))
    def __init__(self, params, result):
        self.result = result
        self.batch_size = params['batch_size']
        self.viper_root = params[
            'viper_root']  #'/home/zf/caffe/multible_channel_identification/data/VIPeR'
        self.im_shape_h = params['im_shape'][0]
        self.im_shape_w = params['im_shape'][1]
        # get list of image indexes.
        list_file_a = params['split'][0] + '.txt'  #split:cam_a或者cam_b
        list_file_b = params['split'][1] + '.txt'
        self.indexlist_a = [
            line.rstrip('\n\r')
            for line in open(osp.join(params['viper_root'][0], list_file_a))
        ]  #0000到0631
        self.indexlist_b = [
            line.rstrip('\n\r')
            for line in open(osp.join(params['viper_root'][1], list_file_b))
        ]
        self.i_a = 0  # current image
        # this class does some simple data-manipulations
        self.transformer = SimpleTransformer()

        print "BatchLoader initialized with {} images".format(
            len(self.indexlist_a))  #632
示例#9
0
    def __init__(self, params, result):
        self.result = result
        self.batch_size = params['batch_size']
        self.anno_list = params['anno_list']
        self.im_shape = params['im_shape']
        self.attribute_list_path = params['attribute_list_path']
        self.memimages = bool(params.get('memimages', 0))
        self.img_transform = params.get('img_transform', 'resize')
        self.ynorm = bool(params.get('ynorm', 0))   # y := y / ||y||_1

        # this class does some simple data-manipulations
        self.transformer = SimpleTransformer(mean=[104, 117, 123])
        self.dictlist = []  # Annotation objects (image + labels) will be stored as dict here

        # Load Attributes ----------------------------------------------------------------------------------------------
        self.attr_id_to_name, self.attr_id_to_idx = load_attributes(self.attribute_list_path)
        self.idx_to_attr_id = {v: k for k, v in self.attr_id_to_idx.iteritems()}
        self.attr_id_list = self.attr_id_to_idx.keys()
        self.n_attr = len(self.attr_id_list)

        # Load Data ----------------------------------------------------------------------------------------------------
        # Store the list of annotation files as indexlist
        self.indexlist = [osp.join(DS_ROOT, line.rstrip('\n')) for line in open(self.anno_list)]

        if self.memimages:
            print "Loading images into memory"
        print "Loading {} annotations".format(len(self.indexlist))

        # Store each image-label object as a dict
        # But, do not store the images. Only store the image file path
        self.dictlist = [json.load(open(filename)) for filename in self.indexlist]
        shuffle(self.dictlist)
        self._cur = 0  # current image

        # Add additional information to each dict
        for idx, this_anno in enumerate(self.dictlist):
            # Prepare the multilabel
            # Get the list of attributes this corresponds to
            attr_set = set(this_anno['labels'])
            multilabel = labels_to_vec(attr_set, self.attr_id_to_idx)
            if self.ynorm and np.sum(multilabel) > 0:
                multilabel /= np.sum(multilabel)
            assert np.sum(multilabel) > 0, 'Failed: np.sum(multilabel) > 0'
            this_anno['label_vec'] = multilabel

            this_anno['image_path'] = osp.join(DS_ROOT, this_anno['image_path'])
            # Images can sometimes be huge (>5mb), which makes loading data extremely slow
            # So, resize and stash them to enable quick loading
            image_resized_path = this_anno['image_path'].replace('/images/', '/images_250/')
            if os.path.exists(image_resized_path):
                this_anno['image_path'] = image_resized_path

            # To make training even faster, load the images into memory before it begins
            if self.memimages:
                im = imread(this_anno['image_path'])
                if len(im.shape) == 2:
                    # This is a grayscale image
                    im = np.asarray(Image.open(this_anno['image_path']).convert('RGB'))
                elif len(im.shape) == 3 and im.shape[2] == 4:
                    # CMYK Image
                    im = np.asarray(Image.open(this_anno['image_path']).convert('RGB'))

                if self.img_transform == 'resize':
                    # Resize the image to the required shape
                    im = scipy.misc.imresize(im, self.im_shape)

                this_anno['im'] = im

                if idx % 100 == 0:
                    sys.stdout.write("processing %d/%d (%.2f%% done)   \r" % (
                    idx, len(self.dictlist), idx * 100.0 / len(self.dictlist)))
                    sys.stdout.flush()

        print "BatchLoader initialized with {} images".format(len(self.indexlist))
示例#10
0
    def __init__(self, params, result):
        self.result = result
        self.batch_size = params['batch_size']
        self.anno_list = params['anno_list']
        self.im_shape = params['im_shape']
        self.label_shortlist_path = params['label_shortlist']
        self.num_labels = params.get('nlabels', NUM_LABELS)
        self.memimages = bool(params.get('memimages', 0))
        self.img_transform = params.get('img_transform', 'resize')
        self.ynorm = bool(params.get('ynorm', 0))  # y := y / ||y||_1
        self.wloss = bool(params.get('wloss', 0))
        self.user_prefs = params.get('user_prefs', None)
        self.scale_user_pref = bool(params.get('scale_user_pref', 0))

        # Possible options:
        # 'uniform' (Default) : Sample uniformly
        # 'weighted': Sample a weight uniformly (usually between 1-5). Then sample an example from on of these.
        self.sampling = params.get('sampling', 'uniform')

        if self.label_shortlist_path is not None:
            self.attr_id_to_idx = dict()
            self.attr_id_to_weight = dict()
            with open(self.label_shortlist_path, 'r') as f:
                f.readline()  # Skip header line
                for line in f:
                    idx, attr_id, count, weight = line.strip().split('\t')
                    idx = int(idx)
                    count = int(count)
                    weight = float(weight)
                    self.attr_id_to_idx[attr_id] = idx
                    self.attr_id_to_weight[attr_id] = weight
        else:
            assert False, "Not Supported"
            # self.attr_id_to_idx = load_attr_to_idx()

        self.attr_id_list = self.attr_id_to_idx.keys()
        self.n_attr = len(self.attr_id_list)

        self.user_mat = None
        if self.user_prefs is not None:
            '''
            This is a file of the format:
            <attribute_id>  <attribute_name>    <score_1>   <score_2> .... <score_U>
            where U = # of users
            score_U indicates how important this attribute is to him/her
            '''
            with open(self.user_prefs) as uf:
                uf.readline()  # Skip header line
                pref_dct = dict(
                )  # Store mapping: attr_id = [..., score_i, ...]
                for line in uf:
                    if line.strip() == '':
                        continue
                    tokens = line.strip().split('\t')
                    attr_id = tokens[0]
                    attr_name = tokens[1]
                    scores = [float(s) for s in tokens[2:]]
                    if attr_id in self.attr_id_to_idx:
                        pref_dct[attr_id] = scores

                # Check n_users is consistent
                n_users = len(pref_dct.values()[0])
                assert all([n_users == len(x) for x in pref_dct.values()
                            ]), Counter([len(x) for x in pref_dct.values()])

                # Manually fill-in safe
                pref_dct[SAFE_ATTR_ID] = np.ones(n_users) * SAFE_WEIGHT
                # Make sure we have preferences for all attributes that we need
                assert all([
                    pref_attr_id in self.attr_id_to_idx
                    for pref_attr_id in pref_dct.keys()
                ])

                # Represent as a matrix: A x U
                # Where col_j represents attribute preferences for user j
                n_attr = len(self.attr_id_to_idx)
                self.user_mat = np.zeros((n_attr, n_users))
                for attr_id, idx in self.attr_id_to_idx.iteritems():
                    attr_scores = pref_dct[attr_id]
                    self.user_mat[idx] = attr_scores

            print 'User preferences: '
            print self.user_mat
            print 'user_mat.shape = ', self.user_mat.shape

            # Normalize user_mat
            if self.scale_user_pref:
                self.user_mat -= 2.5  # Assuming mean of scores = 2.5, so scale to [-2.5, 2.5]
                self.user_mat /= 2.5  # Scale to [-1, 1]

        # Store the list of annotation files as indexlist
        self.indexlist = [line.rstrip('\n') for line in open(self.anno_list)]

        if self.memimages:
            print "Loading images into memory"
        print "Loading {} annotations".format(len(self.indexlist))

        # Store each image-label object as a dict
        # But, do not store the images. Only store the image file path
        self.dictlist = [json.load(open(aidx)) for aidx in self.indexlist]
        shuffle(self.dictlist)

        # Create a weight vector
        self.idx_to_attr_id = {
            v: k
            for k, v in self.attr_id_to_idx.iteritems()
        }
        self.idx_to_weight = np.ones(68)
        if self.wloss:
            for idx in sorted(self.idx_to_attr_id.keys()):
                attr_id = self.idx_to_attr_id[idx]
                self.idx_to_weight[idx] = self.attr_id_to_weight[attr_id]

        print 'Class weights: '
        print self.idx_to_weight

        if self.sampling == 'weighted':
            '''
            1. Create a mapping of WEIGHT (int) -> attribute_idx
               weight of example = max(weight of attribute i in example)
            2. When sampling next image:
              a. Sample weight ~ [1, 2, 3, 4, 5]
              b. Sample an example corresponding to this weight
            Maintain a dict:
                {
                    1: [3, 10, 4, ...],
                    2: [45, 11, 90, ...],
                    ...
                }
            and pop an idx from the list when asked for next image
            '''
            self.weight_to_idx_list = get_w2idx(self.dictlist,
                                                self.attr_id_to_weight)
            # Maintain a copy of this, because it will mutate in each iteration (pop() to consume)
            self.org_weight_to_idx_list = copy.deepcopy(
                self.weight_to_idx_list)
        elif self.sampling == 'class_weighted':
            '''
            1. Create a mapping of LABEL (attr_id) -> DICT_IDX
            2. When sampling next image:
              a. Sample class ~ [attr_1, attr_2, ..., attr_L]
              b. Sample an example corresponding to this label
            Maintain a dict:
                {
                    attr_1: [3, 10, 4, ...],
                    attr_2: [45, 11, 90, ...],
                    ...
                }
            and pop an idx from the list when asked for next image
            '''
            self.class_to_idx_list = get_class2idx(self.dictlist)
            # Maintain a copy of this, because it will mutate in each iteration (pop() to consume)
            self.org_class_to_idx_list = copy.deepcopy(self.class_to_idx_list)
        else:
            self._cur = 0  # current image
            self.weight_to_idx_list = None
            self.class_to_idx_list = None

        # Add to each dict the label vector
        for idx, this_anno in enumerate(self.dictlist):
            # Prepare the multilabel
            # Get the list of attributes this corresponds to
            if 'labels' in this_anno:
                attr_set = set(this_anno['labels'])
            else:
                this_attr_list = []
                for categ_id, attr_id_list in this_anno[
                        'attributes'].iteritems():
                    this_attr_list += attr_id_list
                attr_set = set(this_attr_list)
            multilabel = attribute_set_to_vec(self.attr_id_to_idx,
                                              attr_set,
                                              is_safe=this_anno['safe'])
            if self.ynorm and np.sum(multilabel) > 0:
                multilabel /= np.sum(multilabel)
            assert np.sum(multilabel) > 0, 'Failed: np.sum(multilabel) > 0'
            this_anno['label_vec'] = multilabel

            image_path = this_anno['image_path']
            image_resized_path = image_path.replace('/images_chunks/',
                                                    '/images_chunks_resized/')
            if os.path.exists(image_resized_path):
                this_anno['image_path'] = image_resized_path

            if self.memimages:
                im = imread(this_anno['image_path'])
                if len(im.shape) == 2:
                    # This is a grayscale image
                    im = np.asarray(
                        Image.open(this_anno['image_path']).convert('RGB'))
                elif len(im.shape) == 3 and im.shape[2] == 4:
                    # CMYK Image
                    im = np.asarray(
                        Image.open(this_anno['image_path']).convert('RGB'))

                if self.img_transform == 'resize':
                    # Resize the image to the required shape
                    im = scipy.misc.imresize(im, self.im_shape)

                this_anno['im'] = im

                if idx % 100 == 0:
                    sys.stdout.write("processing %d/%d (%.2f%% done)   \r" %
                                     (idx, len(self.dictlist),
                                      idx * 100.0 / len(self.dictlist)))
                    sys.stdout.flush()

        print 'multilabel.shape = ', multilabel.shape
        # this class does some simple data-manipulations
        self.transformer = SimpleTransformer(mean=[104, 117, 123])

        print "BatchLoader initialized with {} images".format(
            len(self.indexlist))