文章目录
- 高阶退化过程
- 生成用于一阶、二阶退化过程的各种模糊核,以及sinc滤波器
- 具体的一阶、二阶退化过程
高阶退化过程
生成用于一阶、二阶退化过程的各种模糊核,以及sinc滤波器
文件位置 “basicsr/data/realesrgan_dataset.py”
# ------------------------ Generate kernels (used in the first degradation) 一阶退化过程的模糊核------------------------ #kernel_size = random.choice(self.kernel_range) # 从7到21的奇数中,随机选取一个作为核的尺寸# ------使用sic滤波器------if np.random.uniform() < self.opt['sinc_prob']: # sinc_prob:0.1 kernel_range:从(0, 1)的均匀分布中随机取样# this sinc filter setting is for kernels ranging from [7, 21]if kernel_size < 13: # 根据sinc核的大小,选择不同的参数ωomega_c = np.random.uniform(np.pi / 3, np.pi)else:omega_c = np.random.uniform(np.pi / 5, np.pi)# 生成sinc滤波器kernel = circular_lowpass_kernel(omega_c, kernel_size, pad_to=False)else:# ------使用其他的模糊算法:iso/aniso:各向同性/异性、generalized_iso/generalized_aniso:广义各向同性/异性、plateau_iso/plateau_aniso平台各向同性/异性------kernel = random_mixed_kernels(self.kernel_list,self.kernel_prob,kernel_size,self.blur_sigma,self.blur_sigma, [-math.pi, math.pi],self.betag_range,self.betap_range,noise_range=None)# pad kernel 为了保证模糊核的尺寸为固定的21*21pad_size = (21 - kernel_size) // 2kernel = np.pad(kernel, ((pad_size, pad_size), (pad_size, pad_size)))# ------------------------ Generate kernels (used in the second degradation) 二阶退化过程的模糊核------------------------ #kernel_size = random.choice(self.kernel_range)# ------使用sic滤波器------if np.random.uniform() < self.opt['sinc_prob2']: # sinc_prob2: 0.1if kernel_size < 13:omega_c = np.random.uniform(np.pi / 3, np.pi)else:omega_c = np.random.uniform(np.pi / 5, np.pi)kernel2 = circular_lowpass_kernel(omega_c, kernel_size, pad_to=False)else: # ------使用其他的模糊算法:iso/aniso:各向同性/异性、generalized_iso/generalized_aniso:广义各向同性/异性、plateau_iso/plateau_aniso平台各向同性/异性------kernel2 = random_mixed_kernels(self.kernel_list2,self.kernel_prob2,kernel_size,self.blur_sigma2,self.blur_sigma2, [-math.pi, math.pi],self.betag_range2,self.betap_range2,noise_range=None)# pad kernelpad_size = (21 - kernel_size) // 2kernel2 = np.pad(kernel2, ((pad_size, pad_size), (pad_size, pad_size))) # 最终blur核尺寸为21*21# ------------------------------------- the final sinc kernel (最终用于模拟振铃伪影核过冲伪影的sinc滤波器)------------------------------------- #if np.random.uniform() < self.opt['final_sinc_prob']: # final_sinc_prob:0.8kernel_size = random.choice(self.kernel_range)omega_c = np.random.uniform(np.pi / 3, np.pi)sinc_kernel = circular_lowpass_kernel(omega_c, kernel_size, pad_to=21) # 最终sinc滤波器尺寸为21*21sinc_kernel = torch.FloatTensor(sinc_kernel)else:sinc_kernel = self.pulse_tensor # 一个全为1的卷积核,对图像无影响# BGR to RGB, HWC to CHW, numpy to tensor 将数据转换为张量tensorimg_gt = img2tensor([img_gt], bgr2rgb=True, float32=True)[0]kernel = torch.FloatTensor(kernel)kernel2 = torch.FloatTensor(kernel2)return_d = {'gt': img_gt, 'kernel1': kernel, 'kernel2': kernel2, 'sinc_kernel': sinc_kernel, 'gt_path': gt_path}return return_d
结果返回一字典dict,其中包含
{
'gt': img_gt, # ground truth图像对应的张量tensor[c, h, w]
'kernel1': kernel, # 用于一阶退化过程的模糊核
'kernel2': kernel2, # 用于二阶退化过程的模糊核
'sinc_kernel': sinc_kernel, # 用于最终模拟振铃伪影和过冲伪影的sinc滤波器
'gt_path': gt_path # ground truth图像的存放路径}
具体的一阶、二阶退化过程
def feed_data(self, data):"""Accept data from dataloader, and then add two-order degradations to obtain LQ images.从dataloader中接受数据,然后添加二阶退化过程去获取 LQ 图像。"""if self.is_train and self.opt.get('high_order_degradation', True):# training data synthesisself.gt = data['gt'].to(self.device)self.gt_usm = self.usm_sharpener(self.gt)self.kernel1 = data['kernel1'].to(self.device)self.kernel2 = data['kernel2'].to(self.device)self.sinc_kernel = data['sinc_kernel'].to(self.device)ori_h, ori_w = self.gt.size()[2:4]# ----------------------- The first degradation process ----------------------- ## blur 公式(1)中的模糊操作out = filter2D(self.gt_usm, self.kernel1) # 对GT图像先进锐化操作,然后与模糊核做卷积运算# random resize 公式(1)中的缩放操作updown_type = random.choices(['up', 'down', 'keep'], self.opt['resize_prob'])[0] # resize_prob: [0.2, 0.7, 0.1]if updown_type == 'up': # 如果r是上采样,则放大比例为:[1, 1.5]的均匀分布scale = np.random.uniform(1, self.opt['resize_range'][1]) # resize_range: [0.15, 1.5]elif updown_type == 'down': # 如果r是下采样,则缩放比例为:[0.15, 1]的均匀分布scale = np.random.uniform(self.opt['resize_range'][0], 1)else:scale = 1mode = random.choice(['area', 'bilinear', 'bicubic']) # 下采样操作的三种算法out = F.interpolate(out, scale_factor=scale, mode=mode)# add noise # 公式(1)中的添加噪声操作gray_noise_prob = self.opt['gray_noise_prob']if np.random.uniform() < self.opt['gaussian_noise_prob']: # gaussian_noise_prob: 0.5out = random_add_gaussian_noise_pt( # 使用高斯噪声out, sigma_range=self.opt['noise_range'], clip=True, rounds=False, gray_prob=gray_noise_prob)else:out = random_add_poisson_noise_pt( # 使用泊松噪声out,scale_range=self.opt['poisson_scale_range'],gray_prob=gray_noise_prob, # gray_noise_prob: 0.4 灰度噪声默认40%, 颜色噪声为60%clip=True,rounds=False)# JPEG compression # 公式(1)中的JPEG压缩操作jpeg_p = out.new_zeros(out.size(0)).uniform_(*self.opt['jpeg_range'])# torch.clamp(): 将输入input张量每个元素的范围限制到区间 [min,max],返回结果到一个新张量。out = torch.clamp(out, 0, 1) # clamp to [0, 1], otherwise JPEGer will result in unpleasant artifactsout = self.jpeger(out, quality=jpeg_p)# ----------------------- The second degradation process ----------------------- ## blur 模糊if np.random.uniform() < self.opt['second_blur_prob']: # second_blur_prob: 0.8out = filter2D(out, self.kernel2)# random resize 下采样updown_type = random.choices(['up', 'down', 'keep'], self.opt['resize_prob2'])[0] # resize_prob2: [0.3, 0.4, 0.3]if updown_type == 'up': # 上采样: [1, 1,2]scale = np.random.uniform(1, self.opt['resize_range2'][1]) # resize_range2: [0.3, 1.2]elif updown_type == 'down': # 下采样: [0.3, 1]scale = np.random.uniform(self.opt['resize_range2'][0], 1)else:scale = 1mode = random.choice(['area', 'bilinear', 'bicubic'])out = F.interpolate(out, size=(int(ori_h / self.opt['scale'] * scale), int(ori_w / self.opt['scale'] * scale)), mode=mode)# add noise 噪声gray_noise_prob = self.opt['gray_noise_prob2'] # gaussian_noise_prob2: 0.5if np.random.uniform() < self.opt['gaussian_noise_prob2']:out = random_add_gaussian_noise_pt(out, sigma_range=self.opt['noise_range2'], clip=True, rounds=False, gray_prob=gray_noise_prob)else:out = random_add_poisson_noise_pt(out,scale_range=self.opt['poisson_scale_range2'],gray_prob=gray_noise_prob,clip=True,rounds=False)# JPEG compression + the final sinc filter JPEG压缩+sinc滤波器# We also need to resize images to desired sizes. We group [resize back + sinc filter] together# as one operation.# We consider two orders:# 1. [resize back + sinc filter] + JPEG compression# 2. JPEG compression + [resize back + sinc filter]# Empirically, we find other combinations (sinc + JPEG + Resize) will introduce twisted lines.# 我们发现其他组合(sinc + JPEG + Resize)会引入扭曲线。if np.random.uniform() < 0.5:# resize back + the final sinc filtermode = random.choice(['area', 'bilinear', 'bicubic'])out = F.interpolate(out, size=(ori_h // self.opt['scale'], ori_w // self.opt['scale']), mode=mode)out = filter2D(out, self.sinc_kernel)# JPEG compressionjpeg_p = out.new_zeros(out.size(0)).uniform_(*self.opt['jpeg_range2'])out = torch.clamp(out, 0, 1)out = self.jpeger(out, quality=jpeg_p)else:# JPEG compressionjpeg_p = out.new_zeros(out.size(0)).uniform_(*self.opt['jpeg_range2'])out = torch.clamp(out, 0, 1)out = self.jpeger(out, quality=jpeg_p)# resize back + the final sinc filtermode = random.choice(['area', 'bilinear', 'bicubic'])out = F.interpolate(out, size=(ori_h // self.opt['scale'], ori_w // self.opt['scale']), mode=mode)out = filter2D(out, self.sinc_kernel)# clamp and round #将图像的像素值限制在[0, 1]的范围内,同时进行四舍五入处理self.lq = torch.clamp((out * 255.0).round(), 0, 255) / 255.# random crop 随机裁剪给定的高分辨率图像和低分辨率图像,使它们具有相同的裁剪区域gt_size = self.opt['gt_size'](self.gt, self.gt_usm), self.lq = paired_random_crop([self.gt, self.gt_usm], self.lq, gt_size,self.opt['scale'])# training pair poolself._dequeue_and_enqueue()# sharpen self.gt again, as we have changed the self.gt with self._dequeue_and_enqueueself.gt_usm = self.usm_sharpener(self.gt)self.lq = self.lq.contiguous() # for the warning: grad and param do not obey the gradient layout contractelse:# for paired training or validationself.lq = data['lq'].to(self.device)if 'gt' in data:self.gt = data['gt'].to(self.device)self.gt_usm = self.usm_sharpener(self.gt)