Beispiel #1
0
    def __getitem__(self, index ):
        # 从文件中读出HR图片,返回HR、LR图片对
        HR_path , LR_path = None, None
        HR_path = self.HR_paths[index]
        img_HR = util.read_img_(HR_path, self.n_channels)  # return: Numpy float32, HWC, BGR, [0,1]
        scale = self.scale
        patch_size = self.patch_size
        if self.opt.phase == 'train':
            H_s, W_s, _ = img_HR.shape
            img_HR = cv2.resize(np.copy(img_HR), (W_s, H_s), interpolation=cv2.INTER_LINEAR)
            # force to 3 channels
            if img_HR.ndim == 2:
                img_HR = cv2.cvtColor(img_HR, cv2.COLOR_GRAY2BGR)
        # using matlab imresize
        img_LR = util.imresize_np(img_HR, 1 / scale, True)
        H, W, C = img_LR.shape
        if img_LR.ndim == 2:
            img_LR = np.expand_dims(img_LR, axis=2)

        if self.opt.phase == 'train':
            patch_H, patch_L = util.paired_random_crop(img_HR, img_LR, patch_size, scale, gt_path)
            # augmentation - flip, rotate
            img_HR , img_LR = util.augment([patch_H, patch_L], self.opt.use_flip,self.opt.use_rot)


        img_HR  = util.img2tensor(img_HR)
        img_LR = util.img2tensor(img_LR)

        if LR_path is None:
            LR_path = HR_path
        return {'LR': img_LR, 'HR': img_HR, 'LR_path': LR_path, 'HR_path': HR_path}
Beispiel #2
0
    def __getitem__(self, index):
        # 从文件中读出HR图片,返回HR、LR图片对
        HR_path, LR_path = None, None
        HR_path = self.HR_paths[index]
        scale = self.scale[self.idx_scale]
        patch_size = self.patch_size

        img_HR = util.read_img_(HR_path, self.n_colors)
        # 归一化
        img_HR = util.unit2single(img_HR)
        img_HR = util.modcrop(img_HR, self.scale)

        if self.opt['phase'] == 'train':
            l_max = 50
            theta = np.pi * np.random.rand(1)
            l1 = 0.1 + l_max * np.random.rand(1)
            l2 = 0.1 + (l1 - 0.1) * np.random.rand(1)
            kernel = util.anisotropic_Gaussian(ksize=self.ksize,
                                               theta=theta[0],
                                               l1=l1[0],
                                               l2=l2[0])
        else:
            kernel = util.anisotropic_Gaussian(ksize=self.ksize,
                                               theta=np.pi,
                                               l1=0.1,
                                               l2=0.1)
        k = np.reshape(kernel, (-1), order="F")
        k_reduced = np.dot(self.p, k)
        k_reduced = torch.from_numpy(k_reduced).float()

        H, W, _ = img_HR.shape
        img_LR = util.srmd_degradation(img_HR, kernel, self.scale)

        if not self.opt.test_only:
            patch_H, patch_L = util.paired_random_crop(img_HR, img_LR,
                                                       patch_size, scale)
            # augmentation - flip, rotate
            img_HR, img_LR = util.augment([patch_H, patch_L],
                                          self.opt.use_flip, self.opt.use_rot)
            if random.random < 0.1:
                noise_level = np.zeros(1).float()
            else:
                noise_level = np.random.uniform(0, 15)
        else:
            noise_level = np.zeros(self.sigma_test).float

        img_LR = img_LR + np.random.normal(0, noise_level / 255., img_LR.shape)

        img_HR = util.img_single2tensor(img_HR)
        img_LR = util.img_single2tensor(img_LR)

        M_vector = torch.cat((k_reduced, noise_level),
                             0).unsqueeze(1).unsqueeze(1)
        M = M_vector.repeat(1, img_LR.size()[-2], img_LR.size()[-1])

        img_LR = torch.cat((img_LR, M), 0)
        if LR_path is None:
            LR_path = HR_path
        return img_LR, img_HR, LR_path, HR_path
Beispiel #3
0
 def __getitem__(self, index):
     # 从文件中读出HR图片,返回HR、LR图片对
     HR_path = self.HR_paths[index]
     LR_path = self.LR_paths[index]
     img_HR = util.read_img_(HR_path, self.n_colors)
     img_LR = util.read_img_(LR_path, self.n_colors)
     if not self.opt.test_only == 'train':
         patch_H, patch_L = util.paired_random_crop(img_HR, img_LR,
                                                    self.patch_size,
                                                    self.scale)
         img_HR, img_LR = util.augment([patch_H, patch_L],
                                       self.opt.use_flip, self.opt.use_rot)
     img_HR = util.img2tensor(img_HR)
     img_LR = util.img2tensor(img_LR)
     return {'LR': img_LR, 'HR': img_HR}
Beispiel #4
0
 def __getitem__(self, index):
     # 从文件中读出HR图片,返回HR、LR图片对
     HR_path, LR_path = None, None
     HR_path = self.HR_paths[index]
     img_HR = util.read_img_(HR_path, self.n_colors)
     # 归一化
     img_HR = util.unit2single(img_HR)
     img_HR = util.modcrop(img_HR, self.scale)
     kernel = fspecial('gaussian', 15, 2.6)
     H, W, _ = img_HR.shape
     img_LR = util.srmd_degradation(img_HR, kernel, self.scale)
     if self.opt.phase == 'train':
         patch_H, patch_L = util.paired_random_crop(img_HR, img_LR,
                                                    self.patch_size,
                                                    self.scale)
         # augmentation - flip, rotate
         img_HR, img_LR = util.augment([patch_H, patch_L],
                                       self.opt.use_flip, self.opt.use_rot)
         img_HR = util.img_single2tensor(img_HR)
         img_LR = util.img_single2tensor(img_LR)
         if random.random() < 0.1:
             noise_level = torch.zeros(1).float()
         else:
             noise_level = torch.FloatTensor([
                 np.random.uniform(self.sigma_min, self.sigma_max)
             ]) / 255.0
     else:
         img_HR = util.img_single2tensor(img_HR)
         img_LR = util.img_single2tensor(img_LR)
         noise_level = torch.FloatTensor([self.sigma_test])
     noise = torch.randn(img_LR.size()).mul_(noise_level).float()
     img_LR.add_(noise)
     kernel = util.single2tensor3(np.expand_dims(np.float32(kernel),
                                                 axis=2))
     noise_level = torch.FloatTensor([noise_level]).view([1, 1, 1])
     if LR_path is None:
         LR_path = HR_path
     return {
         'LR': img_LR,
         'HR': img_HR,
         'k': kernel,
         'sigma': noise_level,
         'sf': self.scale,
         'LR_path': LR_path,
         'HR_path': HR_path
     }
Beispiel #5
0
    def _scan(self):
        path = self.dir_hr
        names_hr = []
        for dirpath, _, fnames in sorted(os.walk(path)):
            for fname in sorted(fnames):
                img_path = dirpath + '/' + fname
                names_hr.append(img_path)
        # 读取HR图片,生成 LR/BlurMap
        names_lr = [[] for _ in self.scale]
        for f in names_hr:
            img_HR = util.read_img_(f, self.n_colors)
            # 归一化
            img_HR = util.unit2single(img_HR)
            img_HR = util.modcrop(img_HR, self.scale)

            if self.opt['phase'] == 'train':
                l_max = 50
                theta = np.pi * np.random.rand(1)
                l1 = 0.1 + l_max * np.random.rand(1)
                l2 = 0.1 + (l1 - 0.1) * np.random.rand(1)
                kernel = util.anisotropic_Gaussian(ksize=self.ksize,
                                                   theta=theta[0],
                                                   l1=l1[0],
                                                   l2=l2[0])
            else:
                kernel = util.anisotropic_Gaussian(ksize=self.ksize,
                                                   theta=np.pi,
                                                   l1=0.1,
                                                   l2=0.1)
            k = np.reshape(kernel, (-1), order="F")
            k_reduced = np.dot(self.p, k)
            k_reduced = torch.from_numpy(k_reduced).float()

            H, W, _ = img_HR.shape
            img_LR = util.srmd_degradation(img_HR, kernel, self.scale)

            if not self.opt.test_only:
                patch_H, patch_L = util.paired_random_crop(
                    img_HR, img_LR, patch_size, scale)
                # augmentation - flip, rotate
                img_HR, img_LR = util.augment([patch_H, patch_L],
                                              self.opt.use_flip,
                                              self.opt.use_rot)
                if random.random < 0.1:
                    noise_level = np.zeros(1).float()
                else:
                    noise_level = np.random.uniform(0, 15)
            else:
                noise_level = np.zeros(self.sigma_test).float

            img_LR = img_LR + np.random.normal(0, noise_level / 255.,
                                               img_LR.shape)

            img_HR = util.img_single2tensor(img_HR)
            img_LR = util.img_single2tensor(img_LR)

            M_vector = torch.cat((k_reduced, noise_level),
                                 0).unsqueeze(1).unsqueeze(1)
            M = M_vector.repeat(1, img_LR.size()[-2], img_LR.size()[-1])

            img_LR = torch.cat((img_LR, M), 0)
            filename, _ = os.path.splitext(os.path.basename(f))
            for si, s in enumerate(self.scale):

                # 1018测试的图片格式与训练图片格式不符,记得修改!
                if self.name == 'DIV2K':
                    names_lr[si].append(self.dir_lr + '/' + 'X{}'.format(s) +
                                        '/' + 'sig2.6_' + filename + '.jpg')
                elif self.name == 'Set5':
                    names_lr[si].append(self.dir_lr + '/' + 'X{}'.format(s) +
                                        '/' + 'sig2.6_' + filename + '.png')
        return names_hr, names_lr
Beispiel #6
0
    def __getitem__(self, index):
        # 从文件中读出HR图片,返回HR、LR图片对
        HR_path, LR_path = None, None
        HR_path = self.HR_paths[index]
        img_HR = util.read_img_(HR_path, self.n_colors)
        # 归一化
        img_HR = util.unit2single(img_HR)
        img_HR = util.modcrop(img_HR, self.scale)

        if self.opt.phase == 'train':
            l_max = 50
            theta = np.pi * np.random.rand(1)
            l1 = 0.1 + l_max * np.random.rand(1)
            l2 = 0.1 + (l1 - 0.1) * np.random.rand(1)
            kernel = util.anisotropic_Gaussian(ksize=self.ksize,
                                               theta=theta[0],
                                               l1=l1[0],
                                               l2=l2[0])
        else:
            kernel = util.anisotropic_Gaussian(ksize=self.ksize,
                                               theta=np.pi,
                                               l1=0.1,
                                               l2=0.1)
        k = np.reshape(kernel, (-1), order="F")
        k_reduced = np.dot(self.p, k)
        k_reduced = torch.from_numpy(k_reduced).float()
        H, W, _ = img_HR.shape
        img_LR = util.srmd_degradation(img_HR, kernel, self.scale)

        if self.opt.phase == 'train':
            patch_H, patch_L = util.paired_random_crop(img_HR, img_LR,
                                                       self.patch_size,
                                                       self.scale)
            # augmentation - flip, rotate
            img_HR, img_LR = util.augment([patch_H, patch_L],
                                          self.opt.use_flip, self.opt.use_rot)
            img_HR = util.img_single2tensor(img_HR)
            img_LR = util.img_single2tensor(img_LR)

            if random.random() < 0.1:
                noise_level = torch.zeros(1).float()
            else:
                noise_level = torch.FloatTensor([
                    np.random.uniform(self.sigma_min, self.sigma_max)
                ]) / 255.0
        else:
            img_HR = util.img_single2tensor(img_HR)
            img_LR = util.img_single2tensor(img_LR)
            noise_level = torch.FloatTensor([self.sigma_test])
        noise = torch.randn(img_LR.size()).mul_(noise_level).float()
        img_LR.add_(noise)
        M_vector = torch.cat((k_reduced, noise_level),
                             0).unsqueeze(1).unsqueeze(1)
        M = M_vector.repeat(1, img_LR.size()[-2], img_LR.size()[-1])

        img_LR = torch.cat((img_LR, M), 0)
        if LR_path is None:
            LR_path = HR_path
        return {
            'LR': img_LR,
            'HR': img_HR,
            'LR_path': LR_path,
            'HR_path': HR_path
        }