def generate_mod_LR_bic(): # set parameters up_scale = 8 mod_scale = 8 # set data dir sourcedir = '/home/cydiachen/Desktop/cr/HR/cr' savedir = '/home/cydiachen/Desktop/cr/11' saveHRpath = os.path.join(savedir, 'HR', 'x' + str(mod_scale)) saveLRpath = os.path.join(savedir, 'LR', 'x' + str(up_scale)) saveBicpath = os.path.join(savedir, 'Bic', 'x' + str(up_scale)) if not os.path.isdir(sourcedir): print('Error: No source data found') exit(0) if not os.path.isdir(savedir): os.mkdir(savedir) if not os.path.isdir(os.path.join(savedir, 'HR')): os.mkdir(os.path.join(savedir, 'HR')) if not os.path.isdir(os.path.join(savedir, 'LR')): os.mkdir(os.path.join(savedir, 'LR')) if not os.path.isdir(os.path.join(savedir, 'Bic')): os.mkdir(os.path.join(savedir, 'Bic')) if not os.path.isdir(saveHRpath): os.mkdir(saveHRpath) else: print('It will cover ' + str(saveHRpath)) if not os.path.isdir(saveLRpath): os.mkdir(saveLRpath) else: print('It will cover ' + str(saveLRpath)) if not os.path.isdir(saveBicpath): os.mkdir(saveBicpath) else: print('It will cover ' + str(saveBicpath)) filepaths = [f for f in os.listdir(sourcedir) if f.endswith('.jpg')] num_files = len(filepaths) # prepare data with augementation for i in range(num_files): filename = filepaths[i] print('No.{} -- Processing {}'.format(i, filename)) # read image image = cv2.imread(os.path.join(sourcedir, filename)) #image = cv2.resize(image,(128,128),interpolation=cv2.INTER_AREA) width = int(np.floor(image.shape[1] / mod_scale)) height = int(np.floor(image.shape[0] / mod_scale)) # modcrop if len(image.shape) == 3: image_HR = image[0:mod_scale * height, 0:mod_scale * width, :] else: print("Image is not RGB\n") print(os.path.join(sourcedir, filename)) image_HR = image[0:mod_scale * height, 0:mod_scale * width] # LR image_LR = imresize_np(image_HR, 1 / up_scale, True) # bic image_Bic = imresize_np(image_LR, up_scale, True) cv2.imwrite(os.path.join(saveHRpath, filename), image_HR) cv2.imwrite(os.path.join(saveLRpath, filename), image_LR) cv2.imwrite(os.path.join(saveBicpath, filename), image_Bic)
def __getitem__(self, index): # print("EJecuta GeT ITEM") HR_path, LR_path = None, None scale = 0 #self.opt['scale'] HR_size = self.opt['HR_size'] # get HR image HR_path = self.paths_HR[index] img_HR = util.read_img(self.HR_env, HR_path) # modcrop in the validation / test phase if self.opt['phase'] != 'train': # print("Opcion: ", self.opt["phase"]) img_HR = util.modcrop(img_HR, scale) # change color space if necessary if self.opt['color']: img_HR = util.channel_convert(img_HR.shape[2], self.opt['color'], [img_HR])[0] # get LR image if self.paths_LR: LR_path = self.paths_LR[index] img_LR = util.read_img(self.LR_env, LR_path) else: # down-sampling on-the-fly # randomly scale during training if self.opt['phase'] == 'train': random_scale = random.choice(self.random_scale_list) H_s, W_s, _ = img_HR.shape def _mod(n, random_scale, scale, thres): rlt = int(n * random_scale) rlt = (rlt // scale) * scale return thres if rlt < thres else rlt H_s = _mod(H_s, random_scale, scale, HR_size) W_s = _mod(W_s, random_scale, scale, HR_size) 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) H, W, _ = img_HR.shape # using matlab imresize img_LR = util.imresize_np(img_HR, 1 / scale, True) if img_LR.ndim == 2: img_LR = np.expand_dims(img_LR, axis=2) if self.opt['phase'] == 'train': # if the image size is too small # print("1...........") H, W, _ = img_HR.shape if H < HR_size or W < HR_size: img_HR = cv2.resize( np.copy(img_HR), (HR_size, HR_size), interpolation=cv2.INTER_LINEAR) # using matlab imresize img_LR = util.imresize_np(img_HR, 1 / scale, True) if img_LR.ndim == 2: img_LR = np.expand_dims(img_LR, axis=2) H, W, C = img_LR.shape LR_size = HR_size #// scale # randomly crop rnd_h = random.randint(0, max(0, H - LR_size)) rnd_w = random.randint(0, max(0, W - LR_size)) img_LR = img_LR[rnd_h:rnd_h + LR_size, rnd_w:rnd_w + LR_size, :] rnd_h_HR, rnd_w_HR = int(rnd_h * scale), int(rnd_w * scale) img_HR = img_HR[rnd_h_HR:rnd_h_HR + HR_size, rnd_w_HR:rnd_w_HR + HR_size, :] # augmentation - flip, rotate img_LR, img_HR = util.augment([img_LR, img_HR], self.opt['use_flip'], \ self.opt['use_rot']) # change color space if necessary if self.opt['color']: img_LR = util.channel_convert(C, self.opt['color'], [img_LR])[0] # TODO during val no definetion # BGR to RGB, HWC to CHW, numpy to tensor if img_HR.shape[2] == 3: img_HR = img_HR[:, :, [2, 1, 0]] img_LR = img_LR[:, :, [2, 1, 0]] img_HR = torch.from_numpy(np.ascontiguousarray(np.transpose(img_HR, (2, 0, 1)))).float() img_LR = torch.from_numpy(np.ascontiguousarray(np.transpose(img_LR, (2, 0, 1)))).float() # print("2.................................") # # print(img_HR.shape) if LR_path is None: LR_path = HR_path return {'LR': img_LR, 'HR': img_HR, 'LR_path': LR_path, 'HR_path': HR_path}
def __getitem__(self, index): HR_path, LR_path = None, None scale = self.opt['scale'] HR_size = self.opt['HR_size'] # get HR image if self.opt['phase'] == 'train' and \ random.choice(list(range(self.ration))) == 0: # read bg image bg_index = random.randint(0, len(self.paths_HR_bg) - 1) HR_path = self.paths_HR_bg[bg_index] img_HR = util.read_img(self.HR_env_bg, HR_path) seg = torch.FloatTensor(8, img_HR.shape[0], img_HR.shape[1]).fill_(0) seg[0, :, :] = 1 # background else: HR_path = self.paths_HR[index] img_HR = util.read_img(self.HR_env, HR_path) seg = torch.load( HR_path.replace('/img/', '/bicseg/').replace('.png', '.pth')) # modcrop in validation phase if self.opt['phase'] != 'train': img_HR = util.modcrop(img_HR, 8) seg = np.transpose(seg.numpy(), (1, 2, 0)) # get LR image if self.paths_LR: LR_path = self.paths_LR[index] img_LR = util.read_img(self.LR_env, LR_path) else: # down-sampling on-the-fly # randomly scale during training if self.opt['phase'] == 'train': random_scale = random.choice(self.random_scale_list) H_s, W_s, _ = seg.shape def _mod(n, random_scale, scale, thres): rlt = int(n * random_scale) rlt = (rlt // scale) * scale return thres if rlt < thres else rlt H_s = _mod(H_s, random_scale, scale, HR_size) W_s = _mod(W_s, random_scale, scale, HR_size) img_HR = cv2.resize(np.copy(img_HR), (W_s, H_s), interpolation=cv2.INTER_LINEAR) seg = cv2.resize(np.copy(seg), (W_s, H_s), interpolation=cv2.INTER_NEAREST) H, W, _ = img_HR.shape # using matlab imresize img_LR = util.imresize_np(img_HR, 1 / scale, True) if img_LR.ndim == 2: img_LR = np.expand_dims(img_LR, axis=2) H, W, C = img_LR.shape if self.opt['phase'] == 'train': LR_size = HR_size // scale # randomly crop rnd_h = random.randint(0, max(0, H - LR_size)) rnd_w = random.randint(0, max(0, W - LR_size)) img_LR = img_LR[rnd_h:rnd_h + LR_size, rnd_w:rnd_w + LR_size, :] rnd_h_HR, rnd_w_HR = int(rnd_h * scale), int(rnd_w * scale) img_HR = img_HR[rnd_h_HR:rnd_h_HR + HR_size, rnd_w_HR:rnd_w_HR + HR_size, :] seg = seg[rnd_h_HR:rnd_h_HR + HR_size, rnd_w_HR:rnd_w_HR + HR_size, :] # augmentation - flip, rotate img_LR, img_HR, seg = util.augment([img_LR, img_HR, seg], self.opt['use_flip'], self.opt['use_rot']) # category if 'building' in HR_path: category = 1 elif 'plant' in HR_path: category = 2 elif 'mountain' in HR_path: category = 3 elif 'water' in HR_path: category = 4 elif 'sky' in HR_path: category = 5 elif 'grass' in HR_path: category = 6 elif 'animal' in HR_path: category = 7 else: category = 0 # background else: category = -1 # during val, useless # BGR to RGB, HWC to CHW, numpy to tensor if img_HR.shape[2] == 3: img_HR = img_HR[:, :, [2, 1, 0]] img_LR = img_LR[:, :, [2, 1, 0]] img_HR = torch.from_numpy( np.ascontiguousarray(np.transpose(img_HR, (2, 0, 1)))).float() img_LR = torch.from_numpy( np.ascontiguousarray(np.transpose(img_LR, (2, 0, 1)))).float() seg = torch.from_numpy( np.ascontiguousarray(np.transpose(seg, (2, 0, 1)))).float() if LR_path is None: LR_path = HR_path return { 'LR': img_LR, 'HR': img_HR, 'seg': seg, 'category': category, 'LR_path': LR_path, 'HR_path': HR_path }
def generate_mod_LR_bic(): # set parameters up_scale = 4 mod_scale = 4 # set data dir sourcedir = "/data/DIV2K_public/gt_k_x4" #'/mnt/yjchai/SR_data/DIV2K_test_HR' #'/mnt/yjchai/SR_data/Flickr2K/Flickr2K_HR' savedir = "/data/DIV2KRK_public/x4HRblur.lmdb" #'/mnt/yjchai/SR_data/DIV2K_test' #'/mnt/yjchai/SR_data/Flickr2K_train' # set random seed util.set_random_seed(0) # load PCA matrix of enough kernel print("load PCA matrix") pca_matrix = torch.load( "/data/IKC/pca_aniso_matrix.pth", map_location=lambda storage, loc: storage ) print("PCA matrix shape: {}".format(pca_matrix.shape)) saveHRpath = os.path.join(savedir, "HR", "x" + str(mod_scale)) saveLRpath = os.path.join(savedir, "LR", "x" + str(up_scale)) saveBicpath = os.path.join(savedir, "Bic", "x" + str(up_scale)) saveLRblurpath = os.path.join(savedir, "LRblur", "x" + str(up_scale)) if not os.path.isdir(sourcedir): print("Error: No source data found") exit(0) if not os.path.isdir(savedir): os.mkdir(savedir) if not os.path.isdir(os.path.join(savedir, "HR")): os.mkdir(os.path.join(savedir, "HR")) if not os.path.isdir(os.path.join(savedir, "LR")): os.mkdir(os.path.join(savedir, "LR")) if not os.path.isdir(os.path.join(savedir, "Bic")): os.mkdir(os.path.join(savedir, "Bic")) if not os.path.isdir(os.path.join(savedir, "LRblur")): os.mkdir(os.path.join(savedir, "LRblur")) if not os.path.isdir(saveHRpath): os.mkdir(saveHRpath) else: print("It will cover " + str(saveHRpath)) if not os.path.isdir(saveLRpath): os.mkdir(saveLRpath) else: print("It will cover " + str(saveLRpath)) if not os.path.isdir(saveBicpath): os.mkdir(saveBicpath) else: print("It will cover " + str(saveBicpath)) if not os.path.isdir(saveLRblurpath): os.mkdir(saveLRblurpath) else: print("It will cover " + str(saveLRblurpath)) filepaths = sorted([f for f in os.listdir(sourcedir) if f.endswith(".png")]) print(filepaths) num_files = len(filepaths) # kernel_map_tensor = torch.zeros((num_files, 1, 10)) # each kernel map: 1*10 # prepare data with augementation for i in range(num_files): filename = filepaths[i] print("No.{} -- Processing {}".format(i, filename)) # read image image = cv2.imread(os.path.join(sourcedir, filename)) width = int(np.floor(image.shape[1] / mod_scale)) height = int(np.floor(image.shape[0] / mod_scale)) # modcrop if len(image.shape) == 3: image_HR = image[0 : mod_scale * height, 0 : mod_scale * width, :] else: image_HR = image[0 : mod_scale * height, 0 : mod_scale * width] # LR_blur, by random gaussian kernel img_HR = util.img2tensor(image_HR) C, H, W = img_HR.size() # sig_list = [1.8, 2.0, 2.2, 2.4, 2.6, 2.8, 3.0, 3.2] # # sig = 2.6 for sig in np.linspace(1.8, 3.2, 8): prepro = util.SRMDPreprocessing( up_scale, pca_matrix, random=True, para_input=10, kernel=11, noise=False, cuda=True, sig=0, sig_min=0.6, sig_max=5, rate_iso=0, scaling=3, rate_cln=0.2, noise_high=0.0, ) # random(sig_min, sig_max) | stable kernel(sig) LR_img, ker_map = prepro(img_HR.view(1, C, H, W)) image_LR_blur = util.tensor2img(LR_img) cv2.imwrite(os.path.join(saveLRblurpath, 'sig{}_{}'.format(sig,filename)), image_LR_blur) cv2.imwrite(os.path.join(saveHRpath, 'sig{}_{}'.format(sig,filename)), image_HR) # LR image_LR = imresize_np(image_HR, 1 / up_scale, True) # bic image_Bic = imresize_np(image_LR, up_scale, True) # cv2.imwrite(os.path.join(saveHRpath, filename), image_HR) cv2.imwrite(os.path.join(saveLRpath, filename), image_LR) cv2.imwrite(os.path.join(saveBicpath, filename), image_Bic) # kernel_map_tensor[i] = ker_map # save dataset corresponding kernel maps # torch.save(kernel_map_tensor, './Set5_sig2.6_kermap.pth') print("Image Blurring & Down smaple Done: X" + str(up_scale))
def __getitem__(self, index): HR_path, LR_path = None, None scale = self.opt['scale'] HR_size = self.opt['HR_size'] #v if self.opt['rand_flip_LR_HR'] and self.LR_scale and self.opt['phase'] == 'train': LRHRchance = random.uniform(0, 1) if self.opt['flip_chance']: flip_chance = self.opt['flip_chance'] else: flip_chance = 0.05 #print("Random Flip Enabled") else: LRHRchance = 0. flip_chance = 0. #print("No Random Flip") # get HR image if LRHRchance < (1- flip_chance): HR_path = self.paths_HR[index] #print("HR kept") else: HR_path = self.paths_LR[index] #print("HR flipped") #v img_HR = util.read_img(self.HR_env, HR_path) # modcrop in the validation / test phase if self.opt['phase'] != 'train': img_HR = util.modcrop(img_HR, scale) # change color space if necessary if self.opt['color']: img_HR = util.channel_convert(img_HR.shape[2], self.opt['color'], [img_HR])[0] #v if self.HR_crop and (self.HR_rrot != True): crop_size = (HR_size, HR_size) img_HR, _ = augmentations.random_resize_img(img_HR, crop_size) elif self.HR_rrot and (self.HR_crop != True): img_HR, _ = augmentations.random_rotate(img_HR) elif self.HR_crop and self.HR_rrot: if np.random.rand() > 0.5: crop_size = (HR_size, HR_size) img_HR, _ = augmentations.random_resize_img(img_HR, crop_size) else: img_HR, _ = augmentations.random_rotate(img_HR) #v #v if self.HR_noise: img_HR, hr_noise_algo = augmentations.noise_img(img_HR, self.hr_noise_types) #v # get LR image if self.paths_LR: if self.HR_crop or self.HR_rrot: #v img_LR = img_HR else: if LRHRchance < (1- flip_chance): LR_path = self.paths_LR[index] #print("LR kept") else: LR_path = self.paths_HR[index] #print("LR flipped") img_LR = util.read_img(self.LR_env, LR_path) #""" #v scale if self.LR_scale: img_LR, scale_interpol_algo = augmentations.scale_img(img_LR, scale) #""" #""" #v blur if self.LR_blur: img_LR, blur_algo, blur_kernel_size = augmentations.blur_img(img_LR, self.blur_algos) #""" #""" #v noise if self.LR_noise: img_LR, noise_algo = augmentations.noise_img(img_LR, self.noise_types) if self.LR_noise2: img_LR, noise_algo2 = augmentations.noise_img(img_LR, self.noise_types2) #""" #""" #v LR cutout / LR random erasing if self.LR_cutout and (self.LR_erasing != True): img_LR = augmentations.cutout(img_LR, img_LR.shape[0] // 2) elif self.LR_erasing and (self.LR_cutout != True): #only do cutout or erasing, not both img_LR = augmentations.random_erasing(img_LR) elif self.LR_cutout and self.LR_erasing: if np.random.rand() > 0.5: img_LR = augmentations.cutout(img_LR, img_LR.shape[0] // 2, p=0.5) else: img_LR = augmentations.random_erasing(img_LR, p=0.5, modes=[3]) #""" else: # down-sampling on-the-fly # randomly scale during training if self.opt['phase'] == 'train': random_scale = random.choice(self.random_scale_list) H_s, W_s, _ = img_HR.shape def _mod(n, random_scale, scale, thres): rlt = int(n * random_scale) rlt = (rlt // scale) * scale return thres if rlt < thres else rlt H_s = _mod(H_s, random_scale, scale, HR_size) W_s = _mod(W_s, random_scale, scale, HR_size) 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) H, W, _ = img_HR.shape # using matlab imresize img_LR = util.imresize_np(img_HR, 1 / scale, True) if img_LR.ndim == 2: img_LR = np.expand_dims(img_LR, axis=2) if self.opt['phase'] == 'train': # if the image size is too small H, W, _ = img_HR.shape if H < HR_size or W < HR_size: img_HR = cv2.resize( np.copy(img_HR), (HR_size, HR_size), interpolation=cv2.INTER_LINEAR) # using matlab imresize img_LR = util.imresize_np(img_HR, 1 / scale, True) if img_LR.ndim == 2: img_LR = np.expand_dims(img_LR, axis=2) H, W, C = img_LR.shape LR_size = HR_size // scale # randomly crop rnd_h = random.randint(0, max(0, H - LR_size)) rnd_w = random.randint(0, max(0, W - LR_size)) img_LR = img_LR[rnd_h:rnd_h + LR_size, rnd_w:rnd_w + LR_size, :] rnd_h_HR, rnd_w_HR = int(rnd_h * scale), int(rnd_w * scale) img_HR = img_HR[rnd_h_HR:rnd_h_HR + HR_size, rnd_w_HR:rnd_w_HR + HR_size, :] # augmentation - flip, rotate img_LR, img_HR = util.augment([img_LR, img_HR], self.opt['use_flip'], \ self.opt['use_rot']) # change color space if necessary if self.opt['color']: #img_LR = util.channel_convert(C, self.opt['color'], [img_LR])[0] # TODO during val no definetion img_LR = util.channel_convert(img_LR.shape[2], self.opt['color'], [img_LR])[0] # v appears to work ok # BGR to RGB, HWC to CHW, numpy to tensor if img_HR.shape[2] == 3: img_HR = img_HR[:, :, [2, 1, 0]] img_LR = img_LR[:, :, [2, 1, 0]] img_HR = torch.from_numpy(np.ascontiguousarray(np.transpose(img_HR, (2, 0, 1)))).float() img_LR = torch.from_numpy(np.ascontiguousarray(np.transpose(img_LR, (2, 0, 1)))).float() if LR_path is None: LR_path = HR_path return {'LR': img_LR, 'HR': img_HR, 'LR_path': LR_path, 'HR_path': HR_path}
def __getitem__(self, index): HR_path, LR_path = None, None scale = self.opt["scale"] HR_size = self.opt["HR_size"] # get HR image if (self.opt["phase"] == "train" and random.choice( list(range(self.ratio))) == 0): # read background images bg_index = random.randint(0, len(self.paths_HR_bg) - 1) HR_path = self.paths_HR_bg[bg_index] img_HR = util.read_img(self.HR_env_bg, HR_path) seg = torch.FloatTensor(8, img_HR.shape[0], img_HR.shape[1]).fill_(0) seg[0, :, :] = 1 # background else: HR_path = self.paths_HR[index] img_HR = util.read_img(self.HR_env, HR_path) seg = torch.load( HR_path.replace("/img/", "/bicseg/").replace(".png", ".pth")) # read segmentatin files, you should change it to your settings. # modcrop in the validation / test phase if self.opt["phase"] != "train": img_HR = util.modcrop(img_HR, 8) seg = np.transpose(seg.numpy(), (1, 2, 0)) # get LR image if self.paths_LR: LR_path = self.paths_LR[index] img_LR = util.read_img(self.LR_env, LR_path) else: # down-sampling on-the-fly # randomly scale during training if self.opt["phase"] == "train": random_scale = random.choice(self.random_scale_list) H_s, W_s, _ = seg.shape def _mod(n, random_scale, scale, thres): rlt = int(n * random_scale) rlt = (rlt // scale) * scale return thres if rlt < thres else rlt H_s = _mod(H_s, random_scale, scale, HR_size) W_s = _mod(W_s, random_scale, scale, HR_size) img_HR = cv2.resize(np.copy(img_HR), (W_s, H_s), interpolation=cv2.INTER_LINEAR) seg = cv2.resize(np.copy(seg), (W_s, H_s), interpolation=cv2.INTER_NEAREST) H, W, _ = img_HR.shape # using matlab imresize img_LR = util.imresize_np(img_HR, 1 / scale, True) if img_LR.ndim == 2: img_LR = np.expand_dims(img_LR, axis=2) H, W, C = img_LR.shape if self.opt["phase"] == "train": LR_size = HR_size // scale # randomly crop rnd_h = random.randint(0, max(0, H - LR_size)) rnd_w = random.randint(0, max(0, W - LR_size)) img_LR = img_LR[rnd_h:rnd_h + LR_size, rnd_w:rnd_w + LR_size, :] rnd_h_HR, rnd_w_HR = int(rnd_h * scale), int(rnd_w * scale) img_HR = img_HR[rnd_h_HR:rnd_h_HR + HR_size, rnd_w_HR:rnd_w_HR + HR_size, :] seg = seg[rnd_h_HR:rnd_h_HR + HR_size, rnd_w_HR:rnd_w_HR + HR_size, :] # augmentation - flip, rotate img_LR, img_HR, seg = util.augment([img_LR, img_HR, seg], self.opt["use_flip"], self.opt["use_rot"]) # category if "building" in HR_path: category = 1 elif "plant" in HR_path: category = 2 elif "mountain" in HR_path: category = 3 elif "water" in HR_path: category = 4 elif "sky" in HR_path: category = 5 elif "grass" in HR_path: category = 6 elif "animal" in HR_path: category = 7 else: category = 0 # background else: category = -1 # during val, useless # BGR to RGB, HWC to CHW, numpy to tensor if img_HR.shape[2] == 3: img_HR = img_HR[:, :, [2, 1, 0]] img_LR = img_LR[:, :, [2, 1, 0]] img_HR = torch.from_numpy( np.ascontiguousarray(np.transpose(img_HR, (2, 0, 1)))).float() img_LR = torch.from_numpy( np.ascontiguousarray(np.transpose(img_LR, (2, 0, 1)))).float() seg = torch.from_numpy( np.ascontiguousarray(np.transpose(seg, (2, 0, 1)))).float() if LR_path is None: LR_path = HR_path return { "LR": img_LR, "HR": img_HR, "seg": seg, "category": category, "LR_path": LR_path, "HR_path": HR_path, }
def generate_mod_LR_bic(): # set parameters up_scale = 4 mod_scale = 4 # set data dir # sourcedir = '/data/datasets/img' # savedir = '/data/datasets/mod' sourcedir = '/DATA7_DB7/data/4khdr/data/Dataset/train_4k/99849849' savedir = '/DATA7_DB7/data/4khdr/data/Dataset/99849849/' saveHRpath = os.path.join(savedir, 'HR', 'x' + str(mod_scale)) saveLRpath = os.path.join(savedir, 'LR', 'x' + str(up_scale)) saveBicpath = os.path.join(savedir, 'Bic', 'x' + str(up_scale)) if not os.path.isdir(sourcedir): print('Error: No source data found') exit(0) if not os.path.isdir(savedir): os.mkdir(savedir) if not os.path.isdir(os.path.join(savedir, 'HR')): os.mkdir(os.path.join(savedir, 'HR')) if not os.path.isdir(os.path.join(savedir, 'LR')): os.mkdir(os.path.join(savedir, 'LR')) if not os.path.isdir(os.path.join(savedir, 'Bic')): os.mkdir(os.path.join(savedir, 'Bic')) if not os.path.isdir(saveHRpath): os.mkdir(saveHRpath) else: print('It will cover ' + str(saveHRpath)) if not os.path.isdir(saveLRpath): os.mkdir(saveLRpath) else: print('It will cover ' + str(saveLRpath)) if not os.path.isdir(saveBicpath): os.mkdir(saveBicpath) else: print('It will cover ' + str(saveBicpath)) filepaths = [f for f in os.listdir(sourcedir) if f.endswith('.png')] filepaths = sorted(filepaths) num_files = len(filepaths) # prepare data with augementation for i in range(num_files): filename = filepaths[i] print('No.{} -- Processing {}'.format(i, filename)) # read image image = cv2.imread(os.path.join(sourcedir, filename)) width = int(np.floor(image.shape[1] / mod_scale)) height = int(np.floor(image.shape[0] / mod_scale)) # modcrop if len(image.shape) == 3: image_HR = image[0:mod_scale * height, 0:mod_scale * width, :] else: image_HR = image[0:mod_scale * height, 0:mod_scale * width] # LR image_LR = imresize_np(image_HR, 1 / up_scale, True) # bic image_Bic = imresize_np(image_LR, up_scale, True) cv2.imwrite(os.path.join(saveHRpath, filename), image_HR) cv2.imwrite(os.path.join(saveLRpath, filename), image_LR) cv2.imwrite(os.path.join(saveBicpath, filename), image_Bic)
def generate_mod_LR_bic(): # set parameters up_scale = 4 mod_scale = 4 # set data dir sourcedir = "/data/DIV2K_Flickr2K/source/" savedir = "/data/DIV2K_Flickr2K/" saveHRpath = os.path.join(savedir, "HR", "x" + str(mod_scale)) saveLRpath = os.path.join(savedir, "LR", "x" + str(up_scale)) saveBicpath = os.path.join(savedir, "Bic", "x" + str(up_scale)) if not os.path.isdir(sourcedir): print("Error: No source data found") exit(0) if not os.path.isdir(savedir): os.mkdir(savedir) if not os.path.isdir(os.path.join(savedir, "HR")): os.mkdir(os.path.join(savedir, "HR")) if not os.path.isdir(os.path.join(savedir, "LR")): os.mkdir(os.path.join(savedir, "LR")) if not os.path.isdir(os.path.join(savedir, "Bic")): os.mkdir(os.path.join(savedir, "Bic")) if not os.path.isdir(saveHRpath): os.mkdir(saveHRpath) else: print("It will cover " + str(saveHRpath)) if not os.path.isdir(saveLRpath): os.mkdir(saveLRpath) else: print("It will cover " + str(saveLRpath)) if not os.path.isdir(saveBicpath): os.mkdir(saveBicpath) else: print("It will cover " + str(saveBicpath)) filepaths = [f for f in os.listdir(sourcedir) if f.endswith(".png")] num_files = len(filepaths) # prepare data with augementation for i in range(num_files): filename = filepaths[i] print("No.{} -- Processing {}".format(i, filename)) # read image image = cv2.imread(os.path.join(sourcedir, filename)) width = int(np.floor(image.shape[1] / mod_scale)) height = int(np.floor(image.shape[0] / mod_scale)) # modcrop if len(image.shape) == 3: image_HR = image[0:mod_scale * height, 0:mod_scale * width, :] else: image_HR = image[0:mod_scale * height, 0:mod_scale * width] # LR image_LR = imresize_np(image_HR, 1 / up_scale, True) # bic image_Bic = imresize_np(image_LR, up_scale, True) cv2.imwrite(os.path.join(saveHRpath, filename), image_HR) cv2.imwrite(os.path.join(saveLRpath, filename), image_LR) cv2.imwrite(os.path.join(saveBicpath, filename), image_Bic)
def __getitem__(self, index): if self.data_type == 'mc': self._ensure_memcached() elif self.data_type == 'lmdb' and (self.GT_env is None or self.LQ_env is None): self._init_lmdb() scale = self.opt['scale'] GT_size = self.opt['GT_size'] if self.data_type == 'lmdb': key = self.paths_GT[index] name_a, name_b = key.split('_') elif self.data_type == 'img': key = self.paths_GT[index] name_a = key.split('/')[-2] name_b = key.split('/')[-1].split('.')[0] center_frame_idx = int(name_b) #### determine the neighbor frames interval = random.choice(self.interval_list) if self.opt['border_mode']: direction = 1 # 1: forward; 0: backward N_frames = self.opt['N_frames'] if self.random_reverse and random.random() < 0.5: direction = random.choice([0, 1]) if center_frame_idx + interval * (N_frames - 1) > 99: direction = 0 elif center_frame_idx - interval * (N_frames - 1) < 0: direction = 1 # get the neighbor list if direction == 1: neighbor_list = list( range(center_frame_idx, center_frame_idx + interval * N_frames, interval)) else: neighbor_list = list( range(center_frame_idx, center_frame_idx - interval * N_frames, -interval)) name_b = '{:03d}'.format(neighbor_list[0]) else: # ensure not exceeding the borders while (center_frame_idx + self.half_N_frames * interval > 99) or ( center_frame_idx - self.half_N_frames * interval < 1): center_frame_idx = random.randint(0, 99) # get the neighbor list neighbor_list = list( range(center_frame_idx - self.half_N_frames * interval, center_frame_idx + self.half_N_frames * interval + 1, interval)) if self.random_reverse and random.random() < 0.5: neighbor_list.reverse() name_b = '{:03d}'.format(neighbor_list[self.half_N_frames]) assert len(neighbor_list) == self.opt[ 'N_frames'], 'Wrong length of neighbor list: {}'.format( len(neighbor_list)) #### get the GT image (as the center frame) if self.data_type == 'mc': img_GT = self._read_img_mc_BGR(self.GT_root, name_a, name_b) img_GT = img_GT.astype(np.float32) / 255. elif self.data_type == 'lmdb': img_GT = util.read_img(self.GT_env, key, (3, 2160, 3840)) else: img_GT = util.read_img( None, osp.join(self.GT_root, name_a, name_b + '.png')) #### get LQ images LQ_size_tuple = (3, 540, 960) if self.LR_input else (3, 2160, 3840) img_LQ_l = [] for v in neighbor_list: img_LQ_path = osp.join(self.LQ_root, name_a, '{:03d}.png'.format(v)) if self.data_type == 'mc': if self.LR_input: img_LQ = self._read_img_mc(img_LQ_path) else: img_LQ = self._read_img_mc_BGR(self.LQ_root, name_a, '{:03d}'.format(v)) img_LQ = img_LQ.astype(np.float32) / 255. elif self.data_type == 'lmdb': img_LQ = util.read_img(self.LQ_env, '{}_{:03d}'.format(name_a, v), LQ_size_tuple) else: img_LQ = util.read_img(None, img_LQ_path) img_LQ_l.append(img_LQ) if self.opt['phase'] == 'train': C, H, W = LQ_size_tuple # LQ size # randomly crop if self.LR_input: LQ_size = GT_size // scale # choose patch whose variance is larger than threshod rnd_h = random.randint(0, max(0, H - LQ_size)) rnd_w = random.randint(0, max(0, W - LQ_size)) img_LQ_l = [ v[rnd_h:rnd_h + LQ_size, rnd_w:rnd_w + LQ_size, :] for v in img_LQ_l ] rnd_h_HR, rnd_w_HR = int(rnd_h * scale), int(rnd_w * scale) img_GT = img_GT[rnd_h_HR:rnd_h_HR + GT_size, rnd_w_HR:rnd_w_HR + GT_size, :] else: rnd_h = random.randint(0, max(0, H - GT_size)) rnd_w = random.randint(0, max(0, W - GT_size)) img_LQ_l = [ v[rnd_h:rnd_h + GT_size, rnd_w:rnd_w + GT_size, :] for v in img_LQ_l ] img_GT = img_GT[rnd_h:rnd_h + GT_size, rnd_w:rnd_w + GT_size, :] # augmentation - flip, rotate img_LQ_l.append(img_GT) rlt = util.augment(img_LQ_l, self.opt['use_flip'], self.opt['use_rot']) img_LQ_l = rlt[0:-1] img_GT = rlt[-1] # stack LQ images to NHWC, N is the frame number img_LQs = np.stack(img_LQ_l, axis=0) # BGR to RGB, HWC to CHW, numpy to tensor img_GT = img_GT[:, :, [2, 1, 0]] img_LQs = img_LQs[:, :, :, [2, 1, 0]] img_GT_bic4x = util.imresize_np(img_GT, 1 / 4, True) ''' import matplotlib.pyplot as plt plt.subplot(2,2,1) plt.imshow(img_GT) plt.subplot(2,2,2) plt.imshow(img_LQs[2,:,...]) plt.subplot(2,2,3) plt.imshow(img_GT_bic4x) plt.subplot(2,2,4) plt.imshow(img_GT_bic4x-img_LQs[2,:,...]) plt.show() exit() ''' img_GT = torch.from_numpy( np.ascontiguousarray(np.transpose(img_GT, (2, 0, 1)))).float() img_GT_bic4x = torch.from_numpy( np.ascontiguousarray(np.transpose(img_GT_bic4x, (2, 0, 1)))).float() img_LQs = torch.from_numpy( np.ascontiguousarray(np.transpose(img_LQs, (0, 3, 1, 2)))).float() return { 'LQs': img_LQs, 'GT': img_GT, 'img_GT_bic4x': img_GT_bic4x, 'key': key }
def __getitem__(self, index): if self.data_type == 'mc': self._ensure_memcached() elif self.data_type == 'lmdb' and (self.GT_env is None or self.LQ_env is None): self._init_lmdb() scale = self.opt['scale'] GT_size = self.opt['GT_size'] if self.data_type == 'lmdb': key = self.paths_GT[index] name_a, name_b = key.split('_') elif self.data_type == 'img': key = self.paths_GT[index] name_a = key.split('/')[-2] name_b = key.split('/')[-1].split('.')[0] first_frame_idx = int(name_b) #### determine the neighbor frames interval = random.choice(self.interval_list) while first_frame_idx + (self.N_frames - 1) * interval > 100: first_frame_idx = random.randint(1, 100) neighbor_list = list( range(first_frame_idx, first_frame_idx + self.N_frames * interval, interval)) if self.random_reverse and random.random() < 0.5: neighbor_list.reverse() assert len( neighbor_list ) == self.N_frames, 'Wrong length of neighbor list: {}'.format( len(neighbor_list)) #### get LQ images LQ_size_tuple = (3, 540, 960) if self.LR_input else (3, 2160, 3840) GT_size_tuple = (3, 2160, 3840) img_LQ_l = [] img_GT_l = [] for v in neighbor_list: img_LQ_path = osp.join(self.LQ_root, name_a, '{:03d}.png'.format(v)) img_GT_path = osp.join(self.GT_root, name_a, '{:03d}.png'.format(v)) if self.data_type == 'mc': # LQ if self.LR_input: img_LQ = self._read_img_mc(img_LQ_path) else: img_LQ = self._read_img_mc_BGR(self.LQ_root, name_a, '{:03d}'.format(v)) img_LQ = img_LQ.astype(np.float32) / 255. # GT img_GT = self._read_img_mc_BGR(self.GT_root, name_a, '{:03d}'.format(v)) elif self.data_type == 'lmdb': # LQ img_LQ = util.read_img(self.LQ_env, '{}_{:03d}'.format(name_a, v), LQ_size_tuple) # GT img_GT = util.read_img(self.GT_env, '{}_{:03d}'.format(name_a, v), GT_size_tuple) else: # LQ img_LQ = util.read_img(None, img_LQ_path) # GT img_GT = util.read_img(None, img_GT_path) img_LQ_l.append(img_LQ) img_GT_l.append(img_GT) if self.opt['phase'] == 'train': C, H, W = LQ_size_tuple # LQ size # randomly crop if self.LR_input: LQ_size = GT_size // scale # choose patch whose variance is larger than threshod rnd_h = random.randint(0, max(0, H - LQ_size)) rnd_w = random.randint(0, max(0, W - LQ_size)) img_LQ_l = [ v[rnd_h:rnd_h + LQ_size, rnd_w:rnd_w + LQ_size, :] for v in img_LQ_l ] rnd_h_HR, rnd_w_HR = int(rnd_h * scale), int(rnd_w * scale) img_GT_l = [ v[rnd_h_HR:rnd_h_HR + GT_size, rnd_w_HR:rnd_w_HR + GT_size, :] for v in img_GT_l ] else: rnd_h = random.randint(0, max(0, H - GT_size)) rnd_w = random.randint(0, max(0, W - GT_size)) img_LQ_l = [ v[rnd_h:rnd_h + GT_size, rnd_w:rnd_w + GT_size, :] for v in img_LQ_l ] img_GT_l = [ v[rnd_h:rnd_h + GT_size, rnd_w:rnd_w + GT_size, :] for v in img_GT_l ] # augmentation - flip, rotate img_LQ_l += img_GT_l rlt = util.augment(img_LQ_l, self.opt['use_flip'], self.opt['use_rot']) img_LQ_l = rlt[0:self.N_frames] img_GT_l = rlt[self.N_frames:] img_GT_bic4x_l = [ util.imresize_np(v.copy(), 1 / 4, True) for v in img_GT_l ] # stack LQ images to NHWC, N is the frame number img_LQs = np.stack(img_LQ_l, axis=0) img_GTs = np.stack(img_GT_l, axis=0) img_GTs_bic4x = np.stack(img_GT_bic4x_l, axis=0) # BGR to RGB, HWC to CHW, numpy to tensor img_LQs = img_LQs[:, :, :, [2, 1, 0]] img_GTs = img_GTs[:, :, :, [2, 1, 0]] img_GTs_bic4x = img_GTs_bic4x[:, :, :, [2, 1, 0]] ''' import matplotlib.pyplot as plt plt.subplot(2,2,1) plt.imshow(img_GT) plt.subplot(2,2,2) plt.imshow(img_LQs[2,:,...]) plt.subplot(2,2,3) plt.imshow(img_GT_bic4x) plt.subplot(2,2,4) plt.imshow(img_GT_bic4x-img_LQs[2,:,...]) plt.show() exit() ''' img_LQs = torch.from_numpy( np.ascontiguousarray(np.transpose(img_LQs, (0, 3, 1, 2)))).float() img_GTs = torch.from_numpy( np.ascontiguousarray(np.transpose(img_GTs, (0, 3, 1, 2)))).float() img_GTs_bic4x = torch.from_numpy( np.ascontiguousarray(np.transpose(img_GTs_bic4x, (0, 3, 1, 2)))).float() return { 'LQs': img_LQs, 'GT': img_GTs, 'img_GT_bic4x': img_GTs_bic4x, 'key': key }
def __getitem__(self, index): HR_path, LR_path = None, None HR_size = self.opt['HR_size'] scale = self.opt['scale'] LR_path, HR_path = self.imgs[index].split() LR_path = '/home/spl/anaconda2/envs/pth10-py36-cu10/deraining_esrgan/datasets/Real_Rain_Streaks_Dataset_CVPR19_spanet/Training' + LR_path HR_path = '/home/spl/anaconda2/envs/pth10-py36-cu10/deraining_esrgan/datasets/Real_Rain_Streaks_Dataset_CVPR19_spanet/Training' + HR_path img_HR = util.read_img(None, HR_path) img_LR = util.read_img(None, LR_path) # modcrop in the validation / test phase if self.opt['color']: img_HR = util.channel_convert(img_HR.shape[2], self.opt['color'], [img_HR])[0] # if the image size is too small H, W, _ = img_HR.shape if H < HR_size or W < HR_size: img_HR = cv2.resize(np.copy(img_HR), (HR_size, HR_size), interpolation=cv2.INTER_LINEAR) # using matlab imresize img_LR = util.imresize_np(img_HR, 1 / scale, True) if img_LR.ndim == 2: img_LR = np.expand_dims(img_LR, axis=2) # #################################### H, W, C = img_LR.shape LR_size = HR_size // scale # randomly crop rnd_h = random.randint(0, max(0, H - LR_size)) rnd_w = random.randint(0, max(0, W - LR_size)) img_LR = img_LR[rnd_h:rnd_h + LR_size, rnd_w:rnd_w + LR_size, :] rnd_h_HR, rnd_w_HR = int(rnd_h * scale), int(rnd_w * scale) img_HR = img_HR[rnd_h_HR:rnd_h_HR + HR_size, rnd_w_HR:rnd_w_HR + HR_size, :] # augmentation - flip, rotate img_LR, img_HR = util.augment([img_LR, img_HR], self.opt['use_flip'], \ self.opt['use_rot']) ############################################## # change color space if necessary if self.opt['color']: img_LR = util.channel_convert( C, self.opt['color'], [img_LR])[0] # TODO during val no definetion # BGR to RGB, HWC to CHW, numpy to tensor if img_HR.shape[2] == 3: img_HR = img_HR[:, :, [2, 1, 0]] img_LR = img_LR[:, :, [2, 1, 0]] img_HR = torch.from_numpy( np.ascontiguousarray(np.transpose(img_HR, (2, 0, 1)))).float() img_LR = torch.from_numpy( np.ascontiguousarray(np.transpose(img_LR, (2, 0, 1)))).float() return { 'LR': img_LR, 'HR': img_HR, 'LR_path': LR_path, 'HR_path': HR_path }
def generate_mod_LR_bic(): # set parameters up_scale = 16 mod_scale = 16 # set data dir sourcedir = '/media/andreis/storage/datasets/8K/validationHR' savedir = '/media/andreis/storage/datasets/8K/validationHR_2048_processed' saveHRpath = os.path.join(savedir, 'HR', 'x' + str(mod_scale)) saveLRpath = os.path.join(savedir, 'LR', 'x' + str(up_scale)) saveBicpath = os.path.join(savedir, 'Bic', 'x' + str(up_scale)) if not os.path.isdir(sourcedir): print('Error: No source data found') exit(0) if not os.path.isdir(savedir): os.mkdir(savedir) if not os.path.isdir(os.path.join(savedir, 'HR')): os.mkdir(os.path.join(savedir, 'HR')) if not os.path.isdir(os.path.join(savedir, 'LR')): os.mkdir(os.path.join(savedir, 'LR')) if not os.path.isdir(os.path.join(savedir, 'Bic')): os.mkdir(os.path.join(savedir, 'Bic')) if not os.path.isdir(saveHRpath): os.mkdir(saveHRpath) else: print('It will cover ' + str(saveHRpath)) if not os.path.isdir(saveLRpath): os.mkdir(saveLRpath) else: print('It will cover ' + str(saveLRpath)) if not os.path.isdir(saveBicpath): os.mkdir(saveBicpath) else: print('It will cover ' + str(saveBicpath)) filepaths = [f for f in os.listdir(sourcedir) if f.endswith('.png')] num_files = len(filepaths) # prepare data with augementation for i in range(num_files): filename = filepaths[i] print('No.{} -- Processing {}'.format(i, filename)) # read image image = cv2.imread(os.path.join(sourcedir, filename)) width = image.shape[1] height = image.shape[0] # center crop 2048 x 2048 -> might be a better candidate for scoring top = int(height/2) - 1024 left = int(width / 2) - 1024 image = image[top:top+2048, left:left+2048, :] width = int(np.floor(image.shape[1] / mod_scale)) height = int(np.floor(image.shape[0] / mod_scale)) # modcrop if len(image.shape) == 3: image_HR = image[0:mod_scale * height, 0:mod_scale * width, :] else: image_HR = image[0:mod_scale * height, 0:mod_scale * width] # LR image_LR = imresize_np(image_HR, 1 / up_scale, True) # bic image_Bic = imresize_np(image_LR, up_scale, True) cv2.imwrite(os.path.join(saveHRpath, filename), image_HR) cv2.imwrite(os.path.join(saveLRpath, filename), image_LR) cv2.imwrite(os.path.join(saveBicpath, filename), image_Bic)
def generate_mod_LR_bic(): # set parameters up_scale = 4 mod_scale = 4 # set data dir sourcedir = '/Users/lurz/Documents/Undergraduate/SU2019/VE450/data/video_blur_low' savedir = '/Users/lurz/Documents/Undergraduate/SU2019/VE450/GithubCode/video_data/video_blur_low' saveHRpath = os.path.join(savedir, 'HR', 'x' + str(mod_scale)) saveLRpath = os.path.join(savedir, 'LR', 'x' + str(up_scale)) saveBicpath = os.path.join(savedir, 'Bic', 'x' + str(up_scale)) if not os.path.isdir(sourcedir): print('Error: No source data found') exit(0) if not os.path.isdir(savedir): os.mkdir(savedir) if not os.path.isdir(os.path.join(savedir, 'HR')): os.mkdir(os.path.join(savedir, 'HR')) if not os.path.isdir(os.path.join(savedir, 'LR')): os.mkdir(os.path.join(savedir, 'LR')) if not os.path.isdir(os.path.join(savedir, 'Bic')): os.mkdir(os.path.join(savedir, 'Bic')) if not os.path.isdir(saveHRpath): os.mkdir(saveHRpath) else: print('It will cover ' + str(saveHRpath)) if not os.path.isdir(saveLRpath): os.mkdir(saveLRpath) else: print('It will cover ' + str(saveLRpath)) if not os.path.isdir(saveBicpath): os.mkdir(saveBicpath) else: print('It will cover ' + str(saveBicpath)) filepaths = [f for f in os.listdir(sourcedir) if f.endswith('.jpg')] num_files = len(filepaths) # prepare data with augementation for i in range(num_files): filename = filepaths[i] print('No.{} -- Processing {}'.format(i, filename)) # read image image = cv2.imread(os.path.join(sourcedir, filename)) width = int(np.floor(image.shape[1] / mod_scale)) height = int(np.floor(image.shape[0] / mod_scale)) # modcrop if len(image.shape) == 3: image_HR = image[0:mod_scale * height, 0:mod_scale * width, :] else: image_HR = image[0:mod_scale * height, 0:mod_scale * width] # LR image_LR = imresize_np(image_HR, 1 / up_scale, True) # bic image_Bic = imresize_np(image_LR, up_scale, True) cv2.imwrite(os.path.join(saveHRpath, filename), image_HR) cv2.imwrite(os.path.join(saveLRpath, filename), image_LR) cv2.imwrite(os.path.join(saveBicpath, filename), image_Bic)
def generate_mod_LR_bic(): # set parameters up_scale = 4 mod_scale = 4 # set data dir sourcedir = '/mnt/yjchai/SR_data/Set5' #'/mnt/yjchai/SR_data/DIV2K_test_HR' #'/mnt/yjchai/SR_data/Flickr2K/Flickr2K_HR' savedir = '/mnt/yjchai/SR_data/Set5_test' #'/mnt/yjchai/SR_data/DIV2K_test' #'/mnt/yjchai/SR_data/Flickr2K_train' # set random seed util.set_random_seed(0) # load PCA matrix of enough kernel print('load PCA matrix') pca_matrix = torch.load('/media/sdc/yjchai/IKC/codes/pca_matrix.pth', map_location=lambda storage, loc: storage) print('PCA matrix shape: {}'.format(pca_matrix.shape)) saveHRpath = os.path.join(savedir, 'HR', 'x' + str(mod_scale)) saveLRpath = os.path.join(savedir, 'LR', 'x' + str(up_scale)) saveBicpath = os.path.join(savedir, 'Bic', 'x' + str(up_scale)) saveLRblurpath = os.path.join(savedir, 'LRblur', 'x' + str(up_scale)) if not os.path.isdir(sourcedir): print('Error: No source data found') exit(0) if not os.path.isdir(savedir): os.mkdir(savedir) if not os.path.isdir(os.path.join(savedir, 'HR')): os.mkdir(os.path.join(savedir, 'HR')) if not os.path.isdir(os.path.join(savedir, 'LR')): os.mkdir(os.path.join(savedir, 'LR')) if not os.path.isdir(os.path.join(savedir, 'Bic')): os.mkdir(os.path.join(savedir, 'Bic')) if not os.path.isdir(os.path.join(savedir, 'LRblur')): os.mkdir(os.path.join(savedir, 'LRblur')) if not os.path.isdir(saveHRpath): os.mkdir(saveHRpath) else: print('It will cover ' + str(saveHRpath)) if not os.path.isdir(saveLRpath): os.mkdir(saveLRpath) else: print('It will cover ' + str(saveLRpath)) if not os.path.isdir(saveBicpath): os.mkdir(saveBicpath) else: print('It will cover ' + str(saveBicpath)) if not os.path.isdir(saveLRblurpath): os.mkdir(saveLRblurpath) else: print('It will cover ' + str(saveLRblurpath)) filepaths = sorted( [f for f in os.listdir(sourcedir) if f.endswith('.png')]) print(filepaths) num_files = len(filepaths) kernel_map_tensor = torch.zeros( (num_files, 1, 10)) # each kernel map: 1*10 # prepare data with augementation for i in range(num_files): filename = filepaths[i] print('No.{} -- Processing {}'.format(i, filename)) # read image image = cv2.imread(os.path.join(sourcedir, filename)) width = int(np.floor(image.shape[1] / mod_scale)) height = int(np.floor(image.shape[0] / mod_scale)) # modcrop if len(image.shape) == 3: image_HR = image[0:mod_scale * height, 0:mod_scale * width, :] else: image_HR = image[0:mod_scale * height, 0:mod_scale * width] # LR_blur, by random gaussian kernel img_HR = util.img2tensor(image_HR) C, H, W = img_HR.size() # sig_list = [1.8, 2.0, 2.2, 2.4, 2.6, 2.8, 3.0, 3.2] sig = 2.6 prepro = util.SRMDPreprocessing( up_scale, pca_matrix, random=False, para_input=10, kernel=21, noise=False, cuda=True, sig=sig, sig_min=0.2, sig_max=4.0, rate_iso=1.0, scaling=3, rate_cln=0.2, noise_high=0.0) #random(sig_min, sig_max) | stable kernel(sig) LR_img, ker_map = prepro(img_HR.view(1, C, H, W)) image_LR_blur = util.tensor2img(LR_img) cv2.imwrite( os.path.join(saveLRblurpath, 'sig{}_'.format(str(sig)) + filename), image_LR_blur) # LR image_LR = imresize_np(image_HR, 1 / up_scale, True) # bic image_Bic = imresize_np(image_LR, up_scale, True) cv2.imwrite(os.path.join(saveHRpath, filename), image_HR) cv2.imwrite(os.path.join(saveLRpath, filename), image_LR) cv2.imwrite(os.path.join(saveBicpath, filename), image_Bic) kernel_map_tensor[i] = ker_map # save dataset corresponding kernel maps torch.save(kernel_map_tensor, './Set5_sig2.6_kermap.pth') print("Image Blurring & Down smaple Done: X" + str(up_scale))
code_val = torch.randn(1, int(opt['network_G']['in_code_nc']), visuals['SR'].shape[1], visuals['SR'].shape[2]) cur_data = { 'LR': visuals['SR'].reshape((1, ) + visuals['SR'].shape) } model.feed_data(cur_data, code=code_val, need_HR=need_HR) model.test() # test visuals = model.get_current_visuals(need_HR=need_HR) sr_img = util.tensor2img(visuals['SR']) # uint8 if opt["down_scale"]: sr_img = imresize_np(sr_img, opt["down_scale"], True) if need_HR: # load GT image and calculate psnr gt_img = util.tensor2img(visuals['HR']) crop_border = test_loader.dataset.opt['scale'] cropped_sr_img = sr_img[crop_border:-crop_border, crop_border:-crop_border, :] cropped_gt_img = gt_img[crop_border:-crop_border, crop_border:-crop_border, :] psnr = util.psnr(cropped_sr_img, cropped_gt_img) ssim = util.ssim(cropped_sr_img, cropped_gt_img, multichannel=True) test_results['psnr'].append(psnr) test_results['ssim'].append(ssim)
def generate_mod_LR_bic(): # set parameters up_scale = 4 mod_scale = 4 # set data dir sourcedir = '/media/malchul/Новый том/Deep_Learning/datasets/superresolution/DIV2K_train_HR-002/DIV2K_train_HR' savedir = '/media/malchul/Новый том/Deep_Learning/datasets/superresolution/DIV2K_cropped/' saveHRpath = os.path.join(savedir, 'HR', 'X' + str(mod_scale)) saveLRpath = os.path.join(savedir, 'LR', 'X' + str(up_scale)) saveBicpath = os.path.join(savedir, 'Bic', 'X' + str(up_scale)) if not os.path.isdir(sourcedir): print('Error: No source data found') exit(0) if not os.path.isdir(savedir): os.mkdir(savedir) if not os.path.isdir(os.path.join(savedir, 'HR')): os.mkdir(os.path.join(savedir, 'HR')) if not os.path.isdir(os.path.join(savedir, 'LR')): os.mkdir(os.path.join(savedir, 'LR')) if not os.path.isdir(os.path.join(savedir, 'Bic')): os.mkdir(os.path.join(savedir, 'Bic')) if not os.path.isdir(saveHRpath): os.mkdir(saveHRpath) else: print('It will cover ' + str(saveHRpath)) if not os.path.isdir(saveLRpath): os.mkdir(saveLRpath) else: print('It will cover ' + str(saveLRpath)) if not os.path.isdir(saveBicpath): os.mkdir(saveBicpath) else: print('It will cover ' + str(saveBicpath)) filepaths = [f for f in os.listdir(sourcedir) if f.endswith('.png')] num_files = len(filepaths) # prepare data with augementation for i in range(num_files): filename = filepaths[i] print('No.{} -- Processing {}'.format(i, filename)) # read image image = cv2.imread(os.path.join(sourcedir, filename)) width = int(np.floor(image.shape[1] / mod_scale)) height = int(np.floor(image.shape[0] / mod_scale)) # modcrop if len(image.shape) == 3: image_HR = image[0:mod_scale * height, 0:mod_scale * width, :] else: image_HR = image[0:mod_scale * height, 0:mod_scale * width] # LR image_LR = imresize_np(image_HR, 1 / up_scale, True) # bic image_Bic = imresize_np(image_LR, up_scale, True) cv2.imwrite(os.path.join(saveHRpath, filename), image_HR) cv2.imwrite(os.path.join(saveLRpath, filename), image_LR) cv2.imwrite(os.path.join(saveBicpath, filename), image_Bic)
def __getitem__(self, index): # HR_path, LR_path = None, None fake real scale = self.opt['scale'] HR_size = self.opt['HR_size'] index_fake, index_real = index, np.random.randint(0, len(self.paths_real_LR)) # get LR image LR_fake_path = self.paths_fake_LR[index_fake] LR_real_path = self.paths_real_LR[index_real] # fake_w_path = self.paths_fake_weights[index_fake] # real_w_path = self.paths_real_weights[index_real] img_LR_fake = util.read_img(self.LR_env, LR_fake_path) img_LR_real = util.read_img(self.LR_env, LR_real_path) # fake_w = np.load(fake_w_path)[0].transpose((1, 2, 0)) # real_w = np.load(real_w_path)[0].transpose((1, 2, 0)) # 1, H, W # fake_w = cv2.resize(fake_w, (img_LR_fake.shape[1], img_LR_fake.shape[0]), interpolation=cv2.INTER_LINEAR) # real_w = cv2.resize(real_w, (img_LR_real.shape[1], img_LR_real.shape[0]), interpolation=cv2.INTER_LINEAR) # fake_w = np.reshape(fake_w, list(fake_w.shape)+[1]) # real_w = np.reshape(real_w, list(real_w.shape)+[1]) # H, W, 1 # get HR image HR_path = self.paths_HR[index_fake] index_unpair = np.random.randint(0, len(self.paths_HR)) HR_unpair = self.paths_HR[index_unpair] img_HR = util.read_img(self.HR_env, HR_path) img_unpair_HR = util.read_img(self.HR_env, HR_unpair) # modcrop in the validation / test phase if self.opt['phase'] != 'train': img_HR = util.modcrop(img_HR, scale) # change color space if necessary if self.opt['color']: img_HR = util.channel_convert(img_HR.shape[2], self.opt['color'], [img_HR])[0] if self.opt['phase'] == 'train': # if the image size is too small H, W, _ = img_HR.shape if H < HR_size or W < HR_size: img_HR = cv2.resize(np.copy(img_HR), (HR_size, HR_size), interpolation=cv2.INTER_LINEAR) # using matlab imresize img_LR = util.imresize_np(img_HR, 1 / scale, True) if img_LR.ndim == 2: img_LR = np.expand_dims(img_LR, axis=2) H, W, C = img_LR_fake.shape H_r, W_r, C = img_LR_real.shape LR_size = HR_size // scale # randomly crop rnd_h_fake = random.randint(0, max(0, H - LR_size)) rnd_w_fake = random.randint(0, max(0, W - LR_size)) rnd_h_real = random.randint(0, max(0, H_r - LR_size)) rnd_w_real = random.randint(0, max(0, W_r - LR_size)) img_LR_fake = img_LR_fake[rnd_h_fake:rnd_h_fake + LR_size, rnd_w_fake:rnd_w_fake + LR_size, :] img_LR_real = img_LR_real[rnd_h_real:rnd_h_real + LR_size, rnd_w_real:rnd_w_real + LR_size, :] # fake_w = fake_w[rnd_h_fake:rnd_h_fake + LR_size, rnd_w_fake:rnd_w_fake + LR_size, :] # real_w = real_w[rnd_h_real:rnd_h_real + LR_size, rnd_w_real:rnd_w_real + LR_size, :] H, W, C = img_HR.shape H_real, W_real, C_real = img_unpair_HR.shape rnd_h = int(rnd_h_fake*scale) rnd_w = int(rnd_w_fake*scale) rnd_h_real = random.randint(0, max(0, H_real - HR_size)) rnd_w_real = random.randint(0, max(0, W_real - HR_size)) img_HR = img_HR[rnd_h:rnd_h + HR_size, rnd_w:rnd_w + HR_size, :] img_unpair_HR = img_unpair_HR[rnd_h_real:rnd_h_real + HR_size, rnd_w_real:rnd_w_real + HR_size, :] # augmentation - flip, rotate img_LR_fake, img_LR_real, img_HR, img_unpair_HR \ = util.augment([img_LR_fake, img_LR_real, img_HR, img_unpair_HR], self.opt['use_flip'], self.opt['use_rot']) # if self.paths_real_weights: # real_w = util.augment([real_w], # self.opt['use_flip'], self.opt['use_rot']) # change color space if necessary if self.opt['color']: img_LR = util.channel_convert(C, self.opt['color'], [img_LR])[0] # TODO during val no definetion # BGR to RGB, HWC to CHW, numpy to tensor if img_HR.shape[2] == 3: img_HR = img_HR[:, :, [2, 1, 0]] img_unpair_HR = img_unpair_HR[:, :, [2, 1, 0]] img_LR_real = img_LR_real[:, :, [2, 1, 0]] img_LR_fake = img_LR_fake[:, :, [2, 1, 0]] img_HR = torch.from_numpy(np.ascontiguousarray(np.transpose(img_HR, (2, 0, 1)))).float() img_unpair_HR = torch.from_numpy(np.ascontiguousarray(np.transpose(img_unpair_HR, (2, 0, 1)))).float() img_LR_real = torch.from_numpy(np.ascontiguousarray(np.transpose(img_LR_real, (2, 0, 1)))).float() img_LR_fake = torch.from_numpy(np.ascontiguousarray(np.transpose(img_LR_fake, (2, 0, 1)))).float() return {'LR_real': img_LR_real, 'LR_fake': img_LR_fake, 'HR': img_HR, 'HR_unpair': img_unpair_HR, 'LR_real_path': LR_real_path, 'LR_fake_path': LR_fake_path, 'HR_path': HR_path}
def bicubic_degradation(x, sf=3): x = util.imresize_np(x, scale=1 / sf) return x
def __getitem__(self, index): if self.data_type == 'lmdb': if (self.GT_env is None) or (self.LQ_env is None): self._init_lmdb() GT_path, LQ_path = None, None scale = self.opt['scale'] GT_size = self.opt['GT_size'] GT_path = self.paths_GT[index] if self.data_type == 'lmdb': resolution = [int(s) for s in self.sizes_GT[index].split('_')] else: resolution = None img_GT = util.read_img(self.GT_env, GT_path, resolution) if self.opt['phase'] != 'train': img_GT = util.modcrop(img_GT, scale) if self.opt['color']: img_GT = util.channel_convert(img_GT.shape[2], self.opt['color'], [img_GT])[0] if self.paths_LQ: LQ_path = self.paths_LQ[index] if self.data_type == 'lmdb': resolution = [int(s) for s in self.sizes_LQ[index].split('_')] else: resolution = None img_LQ = util.read_img(self.LQ_env, LQ_path, resolution) else: if self.opt['phase'] == 'train': random_scale = random.choice(self.random_scale_list) H_s, W_s, _ = img_GT.shape def _mod(n, random_scale, scale, thres): rlt = int(n * random_scale) rlt = (rlt // scale) * scale return thres if rlt < thres else rlt H_s = _mod(H_s, random_scale, scale, GT_size) W_s = _mod(W_s, random_scale, scale, GT_size) img_GT = cv2.resize(np.copy(img_GT), (W_s, H_s), interpolation=cv2.INTER_LINEAR) if img_GT.ndim == 2: img_GT = cv2.cvtColor(img_GT, cv2.COLOR_GRAY2BGR) H, W, _ = img_GT.shape img_LQ = util.imresize_np(img_GT, 1 / scale, True) if img_LQ.ndim == 2: img_LQ = np.expand_dims(img_LQ, axis=2) if self.opt['phase'] == 'train': # if the image size is too small H, W, _ = img_GT.shape if H < GT_size or W < GT_size: img_GT = cv2.resize(np.copy(img_GT), (GT_size, GT_size), interpolation=cv2.INTER_LINEAR) # using matlab imresize img_LQ = util.imresize_np(img_GT, 1 / scale, True) if img_LQ.ndim == 2: img_LQ = np.expand_dims(img_LQ, axis=2) H, W, C = img_LQ.shape LQ_size = GT_size // scale rnd_h = random.randint(0, max(0, H - LQ_size)) rnd_w = random.randint(0, max(0, W - LQ_size)) img_LQ = img_LQ[rnd_h:rnd_h + LQ_size, rnd_w:rnd_w + LQ_size, :] rnd_h_GT, rnd_w_GT = int(rnd_h * scale), int(rnd_w * scale) img_GT = img_GT[rnd_h_GT:rnd_h_GT + GT_size, rnd_w_GT:rnd_w_GT + GT_size, :] img_LQ, img_GT = util.augment([img_LQ, img_GT], self.opt['use_flip'], self.opt['use_rot']) if self.opt['color']: img_LQ = util.channel_convert( C, self.opt['color'], [img_LQ])[0] # TODO during val no definition if img_GT.shape[2] == 3: img_GT = img_GT[:, :, [2, 1, 0]] img_LQ = img_LQ[:, :, [2, 1, 0]] img_GT = torch.from_numpy( np.ascontiguousarray(np.transpose(img_GT, (2, 0, 1)))).float() img_LQ = torch.from_numpy( np.ascontiguousarray(np.transpose(img_LQ, (2, 0, 1)))).float() if self.opt['phase'] == 'train': if self.opt['aug'] and 'noise' in self.opt['aug']: noise = self.noises[np.random.randint(0, len(self.noises))] img_LQ = torch.clamp(img_LQ + noise, 0, 1) if LQ_path is None: LQ_path = GT_path return { 'LQ': img_LQ, 'GT': img_GT, 'LQ_path': LQ_path, 'GT_path': GT_path }
def generate_mod_LR_bic(): # set parameters up_scale = 4 mod_scale = 4 # set data dir sourcedir = '/mnt/yjchai/SR_data/Flickr2K/Flickr2K_HR' #'/mnt/yjchai/SR_data/DIV2K_test_HR' #'/mnt/yjchai/SR_data/Flickr2K/Flickr2K_HR' savedir = '/mnt/yjchai/SR_data/Flickr2K_train' #'/mnt/yjchai/SR_data/DIV2K_test' #'/mnt/yjchai/SR_data/Flickr2K_train' saveHRpath = os.path.join(savedir, 'HR', 'x' + str(mod_scale)) saveLRpath = os.path.join(savedir, 'LR', 'x' + str(up_scale)) saveBicpath = os.path.join(savedir, 'Bic', 'x' + str(up_scale)) if not os.path.isdir(sourcedir): print('Error: No source data found') exit(0) if not os.path.isdir(savedir): os.mkdir(savedir) if not os.path.isdir(os.path.join(savedir, 'HR')): os.mkdir(os.path.join(savedir, 'HR')) if not os.path.isdir(os.path.join(savedir, 'LR')): os.mkdir(os.path.join(savedir, 'LR')) if not os.path.isdir(os.path.join(savedir, 'Bic')): os.mkdir(os.path.join(savedir, 'Bic')) if not os.path.isdir(saveHRpath): os.mkdir(saveHRpath) else: print('It will cover ' + str(saveHRpath)) if not os.path.isdir(saveLRpath): os.mkdir(saveLRpath) else: print('It will cover ' + str(saveLRpath)) if not os.path.isdir(saveBicpath): os.mkdir(saveBicpath) else: print('It will cover ' + str(saveBicpath)) kernel_list = [] filepaths = sorted( [f for f in os.listdir(sourcedir) if f.endswith('.png')]) print(filepaths) num_files = len(filepaths) # prepare data with augementation for i in range(num_files): filename = filepaths[i] print('No.{} -- Processing {}'.format(i, filename)) # read image image = cv2.imread(os.path.join(sourcedir, filename)) # gaussian random kernel if up_scale == 2: blur_ker = util.random_isotropic_gaussian_kernel(sig_min=0.2, sig_max=2.0, l=21, tensor=False) elif up_scale == 3: blur_ker = util.random_isotropic_gaussian_kernel(sig_min=0.2, sig_max=3.0, l=21, tensor=False) elif up_scale == 4: blur_ker = util.random_isotropic_gaussian_kernel(sig_min=0.2, sig_max=4.0, l=21, tensor=False) width = int(np.floor(image.shape[1] / mod_scale)) height = int(np.floor(image.shape[0] / mod_scale)) # modcrop if len(image.shape) == 3: image_HR = image[0:mod_scale * height, 0:mod_scale * width, :] else: image_HR = image[0:mod_scale * height, 0:mod_scale * width] image_HR_blurred = cv2.filter2D(image_HR, -1, blur_ker) # LR image_LR = imresize_np(image_HR_blurred, 1 / up_scale, True) # bic image_Bic = imresize_np(image_LR, up_scale, True) cv2.imwrite(os.path.join(saveHRpath, filename), image_HR) cv2.imwrite(os.path.join(saveLRpath, filename), image_LR) cv2.imwrite(os.path.join(saveBicpath, filename), image_Bic) kernel_list.append(blur_ker) print("Image Blurring & Down smaple Done: X" + str(up_scale))
def generate_mod_LR_bic(): # set parameters up_scale = 4 mod_scale = 4 # set data dir #sourcedir = '/home/joeyliu/mmsr/datasets/val_set5/Set5' #savedir = '/home/joeyliu/mmsr/datasets/val_set5' #sourcedir = '/home/joeyliu/mmsr/test_dataset/marvel_val' #savedir = '/home/joeyliu/mmsr/test_dataset/maKrvel_val_test' sourcedir = '/home/joeyliu/aster.pytorch/IIIT5K/test' savedir = '/home/joeyliu/mmsr/test_dataset/IIIT5K_test' saveHRpath = os.path.join(savedir, 'HR', 'x' + str(mod_scale)) saveLRpath = os.path.join(savedir, 'LR', 'x' + str(up_scale)) saveBicpath = os.path.join(savedir, 'Bic', 'x' + str(up_scale)) if not os.path.isdir(sourcedir): print('Error: No source data found') exit(0) if not os.path.isdir(savedir): os.mkdir(savedir) if not os.path.isdir(os.path.join(savedir, 'HR')): os.mkdir(os.path.join(savedir, 'HR')) if not os.path.isdir(os.path.join(savedir, 'LR')): os.mkdir(os.path.join(savedir, 'LR')) if not os.path.isdir(os.path.join(savedir, 'Bic')): os.mkdir(os.path.join(savedir, 'Bic')) if not os.path.isdir(saveHRpath): os.mkdir(saveHRpath) else: print('It will cover ' + str(saveHRpath)) if not os.path.isdir(saveLRpath): os.mkdir(saveLRpath) else: print('It will cover ' + str(saveLRpath)) if not os.path.isdir(saveBicpath): os.mkdir(saveBicpath) else: print('It will cover ' + str(saveBicpath)) filepaths = [f for f in os.listdir(sourcedir) if f.endswith('.png')] num_files = len(filepaths) # prepare data with augementation for i in range(num_files): filename = filepaths[i] print('No.{} -- Processing {}'.format(i, filename)) # read image image = cv2.imread(os.path.join(sourcedir, filename)) width = int(np.floor(image.shape[1] / mod_scale)) height = int(np.floor(image.shape[0] / mod_scale)) # modcrop if len(image.shape) == 3: image_HR = image[0:mod_scale * height, 0:mod_scale * width, :] else: image_HR = image[0:mod_scale * height, 0:mod_scale * width] # LR image_LR = imresize_np(image_HR, 1 / up_scale, True) # bic image_Bic = imresize_np(image_LR, up_scale, True) cv2.imwrite(os.path.join(saveHRpath, filename), image_HR) cv2.imwrite(os.path.join(saveLRpath, filename), image_LR) cv2.imwrite(os.path.join(saveBicpath, filename), image_Bic)
import math import pickle import random import numpy as np import glob import torch import cv2 import sys sys.path.append("..") import data.util as util #we first downsample the original images with scaling factors 0.6, 0.7, 0.8, 0.9 to generate the HR/LR images. for scale in [1, 0.9, 0.8, 0.7, 0.6]: GT_folder = '/data0/xtkong/data/DIV2K800_GT' save_GT_folder = '/data0/xtkong/data/DIV2K800_scale/GT' for i in [save_GT_folder]: if os.path.exists(i): pass else: os.makedirs(i) img_GT_list = util._get_paths_from_images(GT_folder) for path_GT in img_GT_list: img_GT = cv2.imread(path_GT) img_GT = img_GT # imresize rlt_GT = util.imresize_np(img_GT, scale, antialiasing=True) print(str(scale) + "_" + os.path.basename(path_GT)) cv2.imwrite(os.path.join(save_GT_folder, str(scale) + "_" + os.path.basename(path_GT)), rlt_GT)
def __getitem__(self, index): if self.opt['data_type'] == 'lmdb': if (self.GT_env is None) or (self.LR_env is None): self._init_lmdb() GT_path, LR_path = None, None scale = self.opt['scale'] GT_size = self.opt['GT_size'] LR_size = self.opt['LR_size'] # get GT image GT_path = self.GT_paths[index] if self.opt['data_type'] == 'lmdb': resolution = [int(s) for s in self.GT_sizes[index].split('_')] else: resolution = None img_GT = util.read_img(self.GT_env, GT_path, resolution) #return: Numpy float32, HWC, BGR, [0,1] # modcrop in the validation / test phase if self.opt['phase'] != 'train': img_GT = util.modcrop(img_GT, scale) # get LR image if self.LR_paths: # LR exist LR_path = self.LR_paths[index] if self.opt['data_type'] == 'lmdb': resolution = [int(s) for s in self.LR_sizes[index].split('_')] else: resolution = None img_LR = util.read_img(self.LR_env, LR_path, resolution) else: # down-sampling on-the-fly # randomly scale during training if self.opt['phase'] == 'train': random_scale = random.choice(self.random_scale_list) H_s, W_s, _ = img_GT.shape def _mod(n, random_scale, scale, thres): rlt = int(n * random_scale) rlt = (rlt // scale) * scale return thres if rlt < thres else rlt H_s = _mod(H_s, random_scale, scale, GT_size) W_s = _mod(W_s, random_scale, scale, GT_size) img_GT = cv2.resize(np.copy(img_GT), (W_s, H_s), interpolation=cv2.INTER_LINEAR) # force to 3 channels if img_GT.ndim == 2: img_GT = cv2.cvtColor(img_GT, cv2.COLOR_GRAY2BGR) H, W, _ = img_GT.shape # using matlab imresize img_LR = util.imresize_np(img_GT, 1 / scale, True) if img_LR.ndim == 2: img_LR = np.expand_dims(img_LR, axis=2) if self.opt['phase'] == 'train': H, W, C = img_LR.shape assert LR_size == GT_size // scale, 'GT size does not match LR size' # randomly crop rnd_h = random.randint(0, max(0, H - LR_size)) rnd_w = random.randint(0, max(0, W - LR_size)) img_LR = img_LR[rnd_h:rnd_h + LR_size, rnd_w:rnd_w + LR_size, :] rnd_h_GT, rnd_w_GT = int(rnd_h * scale), int(rnd_w * scale) img_GT = img_GT[rnd_h_GT:rnd_h_GT + GT_size, rnd_w_GT:rnd_w_GT + GT_size, :] # augmentation - flip, rotate img_LR, img_GT = util.augment([img_LR, img_GT], self.opt['use_flip'], self.opt['use_rot'], self.opt['mode']) # change color space if necessary if self.opt['color']: img_LR = util.channel_convert(C, self.opt['color'], [img_LR])[0] # TODO during val no definition img_GT = util.channel_convert(img_GT.shape[2], self.opt['color'], [img_GT])[0] # BGR to RGB, HWC to CHW, numpy to tensor if img_GT.shape[2] == 3: img_GT = img_GT[:, :, [2, 1, 0]] img_LR = img_LR[:, :, [2, 1, 0]] img_GT = torch.from_numpy(np.ascontiguousarray(np.transpose(img_GT, (2, 0, 1)))).float() img_LR = torch.from_numpy(np.ascontiguousarray(np.transpose(img_LR, (2, 0, 1)))).float() if LR_path is None: LR_path = GT_path return {'LQ': img_LR, 'GT': img_GT, 'LQ_path': LR_path, 'GT_path': GT_path}
def __getitem__(self, index): HR_path, LR_path = None, None scale = self.opt['scale'] HR_size = self.opt['HR_size'] # get HR image HR_path = self.paths_HR[index] img_HR = util.read_img(self.HR_env, HR_path) # modcrop in the validation / test phase if self.opt['phase'] != 'train': img_HR = util.modcrop(img_HR, scale) # change color space if necessary if self.opt['color']: img_HR = util.channel_convert(img_HR.shape[2], self.opt['color'], [img_HR])[0] if self.opt['resize'] < 1: print('Resize by %.2f' % self.opt['resize']) img_HR = cv2.resize(img_HR, None, fx=self.opt['resize'], fy=self.opt['resize'], interpolation=cv2.INTER_LINEAR) # print(img_HR.shape) # get LR image if self.paths_LR: LR_path = self.paths_LR[index] img_LR = util.read_img(self.LR_env, LR_path) else: # down-sampling on-the-fly # randomly scale during training if self.opt['phase'] == 'train': random_scale = random.choice(self.random_scale_list) H_s, W_s, _ = img_HR.shape def _mod(n, random_scale, scale, thres): rlt = int(n * random_scale) rlt = (rlt // scale) * scale return thres if rlt < thres else rlt H_s = _mod(H_s, random_scale, scale, HR_size) W_s = _mod(W_s, random_scale, scale, HR_size) 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) H, W, _ = img_HR.shape if self.opt['downsample'] == 'cubic': img_LR = cv2.resize(img_HR, dsize=None, fx=1.0/scale, fy=1.0/scale, interpolation=cv2.INTER_CUBIC) elif self.opt['downsample'] == 'numpy': img_LR = util.imresize_np(img_HR, 1 / scale, True) elif self.opt['downsample'] == 'linear': img_LR = cv2.resize(img_HR, dsize=None, fx=1.0 / scale, fy=1.0 / scale, interpolation=cv2.INTER_LINEAR) if img_LR.ndim == 2: img_LR = np.expand_dims(img_LR, axis=2) # print(img_HR.shape) # print(img_LR.shape) if self.opt['phase'] == 'train': # if the image size is too small H, W, _ = img_HR.shape if H < HR_size or W < HR_size or not self.opt['crop']: img_HR = cv2.resize( np.copy(img_HR), (HR_size, HR_size), interpolation=cv2.INTER_LINEAR) # using matlab imresize if self.opt['downsample'] == 'cubic': img_LR = cv2.resize(img_HR, dsize=None, fx=1.0 / scale, fy=1.0 / scale, interpolation=cv2.INTER_CUBIC) elif self.opt['downsample'] == 'numpy': img_LR = util.imresize_np(img_HR, 1 / scale, True) elif self.opt['downsample'] == 'linear': img_LR = cv2.resize(img_HR, dsize=None, fx=1.0 / scale, fy=1.0 / scale, interpolation=cv2.INTER_LINEAR) # # img_LR = util.imresize_np(img_HR, 1 / scale, True) if img_LR.ndim == 2: img_LR = np.expand_dims(img_LR, axis=2) H, W, C = img_LR.shape LR_size = HR_size // scale # randomly crop if self.opt['crop']: rnd_h = random.randint(0, max(0, H - LR_size)) rnd_w = random.randint(0, max(0, W - LR_size)) img_LR = img_LR[rnd_h:rnd_h + LR_size, rnd_w:rnd_w + LR_size, :] rnd_h_HR, rnd_w_HR = int(rnd_h * scale), int(rnd_w * scale) img_HR = img_HR[rnd_h_HR:rnd_h_HR + HR_size, rnd_w_HR:rnd_w_HR + HR_size, :] # augmentation - flip, rotate img_LR, img_HR = util.augment([img_LR, img_HR], self.opt['use_flip'], \ self.opt['use_rot']) if self.LR_random_fuzzy is not None: random_fuzzy = random.choice(self.LR_random_fuzzy) assert self.opt['downsample'] == 'numpy' init_LR = np.copy(img_LR) img_LR = util.imresize(img_LR, random_fuzzy, True) img_LR = util.imresize(img_LR, 1 / random_fuzzy, True) if img_LR.shape[0] != LR_size or img_LR.shape[1] != LR_size: print('Warning: LR shape changed after random fuzzy. Using initial one.', img_LR.shape[0], img_LR.shape[1], LR_size) img_LR = init_LR # change color space if necessary if self.opt['color']: img_LR = util.channel_convert(C, self.opt['color'], [img_LR])[0] # TODO during val no definetion # BGR to RGB, HWC to CHW, numpy to tensor if img_HR.shape[2] == 3: img_HR = img_HR[:, :, [2, 1, 0]] img_LR = img_LR[:, :, [2, 1, 0]] img_HR = torch.from_numpy(np.ascontiguousarray(np.transpose(img_HR, (2, 0, 1)))).float() img_LR = torch.from_numpy(np.ascontiguousarray(np.transpose(img_LR, (2, 0, 1)))).float() # return: 0-1 if LR_path is None: LR_path = HR_path return {'LR': img_LR, 'HR': img_HR, 'LR_path': LR_path, 'HR_path': HR_path}
def __getitem__(self, index): if self.data_type == 'lmdb' and (self.GT_env is None or self.LQ_env is None): self._init_lmdb() GT_path, LQ_path = None, None scale = self.opt['scale'] GT_size = self.opt['GT_size'] # get GT image GT_path = self.paths_GT[index] resolution = [int(s) for s in self.sizes_GT[index].split('_') ] if self.data_type == 'lmdb' else None img_GT = util.read_img(self.GT_env, GT_path, resolution) if self.opt['phase'] != 'train': # modcrop in the validation / test phase img_GT = util.modcrop(img_GT, scale) if self.opt['color']: # change color space if necessary img_GT = util.channel_convert(img_GT.shape[2], self.opt['color'], [img_GT])[0] # get LQ image if self.paths_LQ: LQ_path = self.paths_LQ[index] resolution = [int(s) for s in self.sizes_LQ[index].split('_') ] if self.data_type == 'lmdb' else None img_LQ = util.read_img(self.LQ_env, LQ_path, resolution) else: # down-sampling on-the-fly # randomly scale during training random_scale = random.choice(self.random_scale_list) H_s, W_s, _ = img_GT.shape def _mod(n, random_scale, scale, thres): rlt = int(n * random_scale) rlt = (rlt // scale) * scale return thres if rlt < thres else rlt H_s = _mod(H_s, random_scale, scale, GT_size) W_s = _mod(W_s, random_scale, scale, GT_size) img_GT = cv2.resize(img_GT, (W_s, H_s), interpolation=cv2.INTER_LINEAR) if img_GT.ndim == 2: img_GT = cv2.cvtColor(img_GT, cv2.COLOR_GRAY2BGR) H, W, _ = img_GT.shape # using matlab imresize img_LQ = util.imresize_np(img_GT, 1 / scale, True) if img_LQ.ndim == 2: img_LQ = np.expand_dims(img_LQ, axis=2) # get lr_large image img_LQ_Large = cv2.resize(img_LQ, (GT_size, GT_size), cv2.INTER_CUBIC) # get the pts, heatmaps and masks f_anno = osp.join(self.landmarks_folder_256, GT_path + '.txt') # load the landmarks pts, point_set = util.anno_parser(f_anno, 68) # if self.opt['phase'] == 'train': # # if the image size is too small # H, W, _ = img_GT.shape # if H < GT_size or W < GT_size: # img_GT = cv2.resize(img_GT, (GT_size, GT_size), interpolation=cv2.INTER_LINEAR) # # using matlab imresize # img_LQ = util.imresize_np(img_GT, 1 / scale, True) # if img_LQ.ndim == 2: # img_LQ = np.expand_dims(img_LQ, axis=2) H, W, C = img_LQ.shape # augmentation - flip, rotate imgs = [] imgs.append(img_LQ) imgs.append(img_GT) rlt, pts = util.augment_imgs_landmarks(GT_size, imgs, pts, self.opt['use_flip'], self.opt['use_rot']) img_LQ = rlt[0] img_GT = rlt[-1] if self.opt['color']: # change color space if necessary img_LQ = util.channel_convert(C, self.opt['color'], [img_LQ])[0] # TODO during val no definition # BGR to RGB, HWC to CHW, numpy to tensor if img_GT.shape[2] == 3: img_GT = img_GT[:, :, [2, 1, 0]] img_LQ = img_LQ[:, :, [2, 1, 0]] img_LQ_Large = img_LQ_Large[:, :, [2, 1, 0]] img_GT = torch.from_numpy(np.ascontiguousarray(np.transpose(img_GT, (2, 0, 1)))).float() img_LQ = torch.from_numpy(np.ascontiguousarray(np.transpose(img_LQ, (2, 0, 1)))).float() img_LQ_Large = torch.from_numpy(np.ascontiguousarray(np.transpose(img_LQ_Large, (2, 0, 1)))).float() pts = util.apply_bound(pts, 256, 256) # H*W*C GT_heatmaps, GT_mask = util.generate_label_map(pts, 16, 16, self.sigma, 16.0, self.heatmap_type) GT_heatmaps = torch.from_numpy(GT_heatmaps.transpose((2, 0, 1))).type(torch.FloatTensor) GT_mask = torch.from_numpy(GT_mask.transpose((2,0,1))).type(torch.ByteTensor) if LQ_path is None: LQ_path = GT_path return {'LQ': img_LQ, 'GT': img_GT, 'LQ_Large': img_LQ_Large, 'GT_heatmaps': GT_heatmaps, 'GT_mask': GT_mask}
def __getitem__(self, index): HR_path, LR_path = None, None scale = self.opt['scale'] HR_size = self.opt['HR_size'] # get HR image index1 = index % len(self.paths_HR) HR_path = self.paths_HR[index1] img_HR = util.read_img(self.HR_env, HR_path) # modcrop in the validation / test phase if self.opt['phase'] != 'train': img_HR = util.modcrop(img_HR, scale) # change color space if necessary if self.opt['color']: img_HR = util.channel_convert(img_HR.shape[2], self.opt['color'], [img_HR])[0] # get LR image if self.paths_LR: index2 = index % len(self.paths_LR) LR_path = self.paths_LR[index2] img_LR = util.read_img(self.LR_env, LR_path) if self.opt['phase'] == 'train': # if the image size is too small H, W, _ = img_HR.shape if H < HR_size or W < HR_size: img_HR = cv2.resize( np.copy(img_HR), (HR_size, HR_size), interpolation=cv2.INTER_LINEAR) # using matlab imresize img_LR = util.imresize_np(img_HR, 1 / scale, True) if img_LR.ndim == 2: img_LR = np.expand_dims(img_LR, axis=2) H, W, C = img_LR.shape LR_size = HR_size // scale # randomly crop rnd_h = random.randint(0, max(0, H - LR_size)) rnd_w = random.randint(0, max(0, W - LR_size)) img_LR = img_LR[rnd_h:rnd_h + LR_size, rnd_w:rnd_w + LR_size, :] H, W, C = img_HR.shape # randomly crop rnd_h = random.randint(0, max(0, H - HR_size)) rnd_w = random.randint(0, max(0, W - HR_size)) img_HR = img_HR[rnd_h:rnd_h + HR_size, rnd_w:rnd_w + HR_size, :] # augmentation - flip, rotate img_LR, img_HR = util.augment([img_LR, img_HR], self.opt['use_flip'], \ self.opt['use_rot']) # change color space if necessary if self.opt['color']: img_LR = util.channel_convert(C, self.opt['color'], [img_LR])[0] # TODO during val no definetion # BGR to RGB, HWC to CHW, numpy to tensor if img_HR.shape[2] == 3: img_HR = img_HR[:, :, [2, 1, 0]] img_LR = img_LR[:, :, [2, 1, 0]] img_HR = torch.from_numpy(np.ascontiguousarray(np.transpose(img_HR, (2, 0, 1)))).float() img_LR = torch.from_numpy(np.ascontiguousarray(np.transpose(img_LR, (2, 0, 1)))).float() if LR_path is None: LR_path = HR_path return {'LR': img_LR, 'HR': img_HR, 'LR_path': LR_path, 'HR_path': HR_path}
def __getitem__(self, index): if self.data_type == 'lmdb' and (self.GT_env is None or self.LQ_env is None): self._init_lmdb() GT_path, LQ_path = None, None scale = self.opt['scale'] GT_size = self.opt['GT_size'] # get GT image GT_path = self.paths_GT[index] resolution = [int(s) for s in self.sizes_GT[index].split('_') ] if self.data_type == 'lmdb' else None img_GT = util.read_img(self.GT_env, GT_path, resolution) if self.opt[ 'phase'] != 'train': # modcrop in the validation / test phase img_GT = util.modcrop(img_GT, scale) if self.opt['color']: # change color space if necessary img_GT = util.channel_convert(img_GT.shape[2], self.opt['color'], [img_GT])[0] # get LQ image if self.paths_LQ: LQ_path = self.paths_LQ[index] resolution = [int(s) for s in self.sizes_LQ[index].split('_') ] if self.data_type == 'lmdb' else None img_LQ = util.read_img(self.LQ_env, LQ_path, resolution) else: # down-sampling on-the-fly # randomly scale during training if self.opt['phase'] == 'train': random_scale = random.choice(self.random_scale_list) H_s, W_s, _ = img_GT.shape def _mod(n, random_scale, scale, thres): rlt = int(n * random_scale) rlt = (rlt // scale) * scale return thres if rlt < thres else rlt H_s = _mod(H_s, random_scale, scale, GT_size) W_s = _mod(W_s, random_scale, scale, GT_size) img_GT = cv2.resize(img_GT, (W_s, H_s), interpolation=cv2.INTER_LINEAR) if img_GT.ndim == 2: img_GT = cv2.cvtColor(img_GT, cv2.COLOR_GRAY2BGR) H, W, _ = img_GT.shape # using matlab imresize img_LQ = util.imresize_np(img_GT, 1 / scale, True) if img_LQ.ndim == 2: img_LQ = np.expand_dims(img_LQ, axis=2) if self.opt['phase'] == 'train': # if the image size is too small H, W, _ = img_GT.shape if H < GT_size or W < GT_size: img_GT = cv2.resize(img_GT, (GT_size, GT_size), interpolation=cv2.INTER_LINEAR) # using matlab imresize img_LQ = util.imresize_np(img_GT, 1 / scale, True) if img_LQ.ndim == 2: img_LQ = np.expand_dims(img_LQ, axis=2) H, W, C = img_LQ.shape LQ_size = GT_size // scale # randomly crop rnd_h = random.randint(0, max(0, H - LQ_size)) rnd_w = random.randint(0, max(0, W - LQ_size)) img_LQ = img_LQ[rnd_h:rnd_h + LQ_size, rnd_w:rnd_w + LQ_size, :] rnd_h_GT, rnd_w_GT = int(rnd_h * scale), int(rnd_w * scale) img_GT = img_GT[rnd_h_GT:rnd_h_GT + GT_size, rnd_w_GT:rnd_w_GT + GT_size, :] # augmentation - flip, rotate img_LQ, img_GT = util.augment([img_LQ, img_GT], self.opt['use_flip'], self.opt['use_rot']) if self.opt['color']: # change color space if necessary img_LQ = util.channel_convert( C, self.opt['color'], [img_LQ])[0] # TODO during val no definition # BGR to RGB, HWC to CHW, numpy to tensor if img_GT.shape[2] == 3: img_GT = img_GT[:, :, [2, 1, 0]] img_LQ = img_LQ[:, :, [2, 1, 0]] img_GT = torch.from_numpy( np.ascontiguousarray(np.transpose(img_GT, (2, 0, 1)))).float() img_LQ = torch.from_numpy( np.ascontiguousarray(np.transpose(img_LQ, (2, 0, 1)))).float() if LQ_path is None: LQ_path = GT_path return { 'LQ': img_LQ, 'GT': img_GT, 'LQ_path': LQ_path, 'GT_path': GT_path }
def __getitem__(self, index: int) -> Dict[str, Union[Any, str]]: GT_path: Optional[str] = None LQ_path: Optional[str] = None scale: int = int(self.opt["scale"]) GT_size: int = cast(int, self.opt["GT_size"]) # only train # get GT image self.paths_GT = cast(List[str], self.paths_GT) GT_path = self.paths_GT[index] img_GT: Any = util.read_img(GT_path) H: int W: int C: int # get LQ image if self.paths_LQ: LQ_path = self.paths_LQ[index] img_LQ: Any = util.read_img(LQ_path) else: # down-sampling on-the-fly # ??? 왜 하는거지 if self.opt["phase"] == "train": random_scale: int = random.choice(self.random_scale_list) H_s: int W_s: int H_s, W_s, _ = img_GT.shape def _mod(n: int, random_scale: int, scale: int, thres: int) -> int: rlt: int = int(n * random_scale) rlt = (rlt // scale) * scale return thres if rlt < thres else rlt H_s = _mod(H_s, random_scale, scale, GT_size) W_s = _mod(W_s, random_scale, scale, GT_size) img_GT = cv2.resize(np.copy(img_GT), (W_s, H_s), interpolation=cv2.INTER_LINEAR) if img_GT.ndim == 2: img_GT = cv2.cvtColor(img_GT, cv2.COLOR_GRAY2BGR) H, W, _ = img_GT.shape # TODO: resize 비교 # img_LQ = cv2.resize(np.copy(img_GT), (H_s // scale, W_s // scale)) img_LQ = util.imresize_np(img_GT, 1 / scale, True) # augmentation if self.opt["phase"] == "train": H, W, _ = img_GT.shape # ??? 이것도 왜 하는걸까 if H < GT_size or W < GT_size: img_GT = cv2.resize(np.copy(img_GT), (GT_size, GT_size), interpolation=cv2.INTER_LINEAR) img_LQ = util.imresize_np(img_GT, 1 / scale, True) if img_LQ.ndim == 2: img_LQ = np.expand_dims(img_LQ, axis=2) H, W, C = img_LQ.shape LQ_size: int = GT_size // scale # randomly crop rnd_h: int rnd_w: int rnd_h_GT: int rnd_w_GT: int rnd_h = random.randint(0, max(0, H - LQ_size)) rnd_w = random.randint(0, max(0, W - LQ_size)) img_LQ = img_LQ[rnd_h:rnd_h + LQ_size, rnd_w:rnd_w + LQ_size, :] rnd_h_GT, rnd_w_GT = int(rnd_h * scale), int(rnd_w * scale) img_GT = img_GT[rnd_h_GT:rnd_h_GT + GT_size, rnd_w_GT:rnd_w_GT + GT_size, :] img_LQ, img_GT = util.augment([img_LQ, img_GT], bool(self.opt["use_flip"]), bool(self.opt["use_rot"])) # BGR to RGB, numpy to tensor if img_GT.shape[2] == 3: img_GT = img_GT[:, :, [2, 1, 0]] img_LQ = img_LQ[:, :, [2, 1, 0]] img_GT = torch.from_numpy( np.ascontiguousarray(np.transpose(img_GT, (2, 0, 1)))).float() img_LQ = torch.from_numpy( np.ascontiguousarray(np.transpose(img_LQ, (2, 0, 1)))).float() if LQ_path is None: LQ_path = GT_path return { "LQ": img_LQ, "GT": img_GT, "LQ_path": LQ_path, "GT_path": GT_path }