Esempio n. 1
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
Esempio n. 2
0
    def convert2pkl(self, pklfile):

        if not os.path.isfile(pklfile):
            dataset_dir = "/home/rzhao/Projects/deep-saliency/data/"
            thus10000 = dataset_dir + "THUS10000_Imgs_GT/Imgs"
            msra5000 = dataset_dir + "MSRA5000/Imgs"
            msra5000_test = dataset_dir + "MSRA5000/MSRA-B-test"
            img_ext = ".jpg"
            msk_ext = ".png"
            augX = 10

            trn_img = []
            trn_msk = []
            for single_image in sorted(glob(thus10000 + "/*" + img_ext)):
                rsb = glob(msra5000_test + "/*_" + ntpath.basename(single_image)[:-4] + "_smap" + msk_ext)
                if len(rsb) == 0:
                    trn_img.append(single_image)
                    trn_msk.append(single_image[:-4] + msk_ext)

            tst_img = []
            tst_msk = []
            for single_image in sorted(glob(msra5000_test + "/*" + msk_ext)):
                tst_img.append(msra5000 + "/" + ntpath.basename(single_image)[: -len("_smap.png")] + img_ext)
                tst_msk.append(msra5000 + "/" + ntpath.basename(single_image)[: -len("_smap.png")] + msk_ext)

                # read images
            print "reading ..."
            train_img = [imread(fname) for fname in trn_img]
            train_msk = [imread(fname) for fname in trn_msk]
            test_img = [imread(fname) for fname in tst_img]
            test_msk = [imread(fname) for fname in tst_msk]

            # preprocessing
            print "preprocessing ..."
            train_x, train_y = self.preprocessing(train_img, train_msk, augx=augX)
            test_x, test_y = self.preprocessing(test_img, test_msk, augx=0)

            # shuffle training data
            print "shuffle data ..."
            np.random.seed(123)
            np.random.shuffle(train_x)
            np.random.seed(123)
            np.random.shuffle(train_y)

            # flattern and dtype conversion
            print "flatten data ..."
            train_x = np.asarray(train_x, dtype=np.float32)
            train_y = np.asarray(train_y, dtype=np.float32)
            test_x = np.asarray(test_x, dtype=np.float32)
            test_y = np.asarray(test_y, dtype=np.float32)
            train_x = imflatten(train_x)
            train_y = imflatten(train_y)
            test_x = imflatten(test_x)
            test_y = imflatten(test_y)

            # normalize data to have zero mean and unit std
            train_x = normalize01(train_x)
            test_x = normalize01(test_x)

            # split into train and valid
            nValid = np.int(len(train_img) * 0.1) * augX
            train = [train_x[:-nValid], train_y[:-nValid]]
            valid = [train_x[-nValid:], train_y[-nValid:]]
            # train = [train_x[0:7000], train_y[0:7000]]
            # valid = [train_x[7000:], train_y[7000:]]
            test = [test_x, test_y]
            data = [train, valid, test]
            self.save(data, pklfile)

        else:
            print "History pickle file exists!"