Ejemplo n.º 1
0
    def preprocessing(self, imgs, msks, sz=(48, 48), augx=0):

        print len(imgs)
        if augx > 0:
            print "augmenting train data ..."
            # augx = 2xnSample+1
            n_sample = np.int(augx / 2.0) - 1
            imH, imW = imgs[0].shape[0:2]
            borderH = np.int(imH * 0.2)
            borderW = np.int(imW * 0.2)
            w = imW - borderW
            h = imH - borderH
            x1s = np.random.randint(0, borderW, n_sample)
            y1s = np.random.randint(0, borderH, n_sample)
            imgs_crop = imgs
            msks_crop = msks
            for img, msk in zip(imgs, msks):
                imgs_crop += [imcrop(img, [x1, y1, w, h]) for x1, y1 in zip(x1s, y1s)]
                msks_crop += [imcrop(msk, [x1, y1, w, h]) for x1, y1 in zip(x1s, y1s)]
            print len(imgs_crop)
            imgs_flip = [pl.fliplr(im) for im in imgs_crop]
            msks_flip = [pl.fliplr(im) for im in msks_crop]
            imgs = imgs_crop + imgs_flip
            msks = msks_crop + msks_flip
            print len(imgs)

        imgs_rs = [imresize(im, sz, interp="bicubic") for im in imgs]
        imgs_norm = [imnormalize(im) for im in imgs_rs]
        msks_norm = [imresize(im, sz, interp="bicubic") for im in msks]
        imgs_final, msks_final = self.convert_data(imgs_norm, msks_norm)
        print len(imgs_final)
        return imgs_final, msks_final
Ejemplo n.º 2
0
    def _sampling_patch(self, spidx, augx=0):
        """ sampling local patches for predicting salience score """

        # spf = lambda items, indices: map(lambda i: items[i], indices)
        imgs = [imnormalize(im) for im in self.imgs[spidx]]  # spf(self.imgs, spidx)
        segmsks = self.segmsks[spidx]
        salmsks = self.salmsks[spidx]
        features = self.feats[spidx]

        h, w = self.segmsks[0].shape
        # l = np.int(self.patL/2.)
        # sz = (self.patL, self.patL)
        l = self.patL
        r = np.int(l / 2)

        imgids = []  # image indices
        ctrids = []  # center grid indices
        patches = []  # cropped patches
        salscores = []  # corresponding salience scores
        patfeats = []  # colorsift feature of cropped patch

        for idx, img, seg, sal, feat in zip(spidx, imgs, segmsks, salmsks, features):

            print idx
            ids1 = []
            ids2 = []
            for i in range(len(self.xx)):
                x = self.xx[i]
                y = self.yy[i]
                # if x-r>=0 and x+r<w and y-r>=0 and y+r<h and seg[y, x] > 0 :
                if seg[y, x] > 0:
                    ids1.append(idx)
                    ids2.append(i)

                    # crop patches centering on these points
            imgids += ids1
            ctrids += ids2
            patches += [imcrop(img, [self.xx[k] - r, self.yy[k] - r, l, l]) for k in ids2]
            patfeats += [feat[k] for k in ids2]
            salscores += [np.mean(imcrop(sal, [self.xx[k] - r, self.yy[k] - r, l, l])) for k in ids2]

            # convert image to data format (normalize & roll axis) that is appropriate for training usage
        imgids = np.asarray(imgids)
        ctrids = np.asarray(ctrids)
        patches = self._convert_data(patches)
        patfeats = np.asarray(patfeats)
        salscores = np.asarray(salscores)

        os.system("free -hm")

        return imgids, ctrids, patches, patfeats, salscores
Ejemplo n.º 3
0
    def _convert_data(self, imgs):
        """ this contains operations on images that make image become input data (cannot be shown) """

        # convert from RGB to CIE-LAB color space
        print "RGB to LAB conversion"
        imgs = [imnormalize(im) for im in imgs]
        # roll axis to put channel axis to the first
        print "Roll channel axis"
        imgs = [im.transpose((2, 0, 1)) for im in imgs]
        # flatten images
        print "Flatten images"
        imgs = np.asarray(imgs)
        imgs = imflatten(imgs)
        # normalize to be zero mean and unit std
        print "Zero-mean & unit std normalization"
        imgs = normalize01(imgs)

        return imgs