NGain commited on
Commit
199b0ea
·
verified ·
1 Parent(s): 2c411c9

Delete utils_data

Browse files
utils_data/crop_paired_data.py DELETED
@@ -1,73 +0,0 @@
1
- import os
2
- import sys
3
- sys.path.append(os.getcwd())
4
- import cv2
5
-
6
- import torch
7
- import torch.nn.functional as F
8
- from pytorch_lightning import seed_everything
9
- import argparse
10
-
11
- from basicsr.utils import FileClient, get_root_logger, imfrombytes, img2tensor
12
- from basicsr.utils import DiffJPEG, USMSharp
13
- parser = argparse.ArgumentParser()
14
-
15
- parser.add_argument("--save_dir", type=str, default='preset/datasets/train_datasets/training_for_seesr', help='the save path of the training dataset.')
16
-
17
- args = parser.parse_args()
18
-
19
- gt_path = os.path.join(args.save_dir, 'gt')
20
- lr_path = os.path.join(args.save_dir, 'lr')
21
- sr_bicubic_path = os.path.join(args.save_dir, 'sr_bicubic')
22
- print(gt_path)
23
- os.makedirs(gt_path, exist_ok=True)
24
- os.makedirs(lr_path, exist_ok=True)
25
- os.makedirs(sr_bicubic_path, exist_ok=True)
26
- hr_dir = '/media/ssd8T/wyw/Data/NTIRE2025/test/hr'
27
- lr_dir = '/media/ssd8T/wyw/Data/NTIRE2025/test/lr'
28
- hr_files = sorted(os.listdir(hr_dir))
29
- lr_files = sorted(os.listdir(lr_dir))
30
- usm_sharpener = USMSharp().cuda()
31
- step = 0
32
- for i, (hr_file, lr_file) in enumerate(zip(hr_files, lr_files)):
33
- step += 1
34
- print('process {} images...'.format(step))
35
-
36
- with open(os.path.join(hr_dir, hr_file), 'rb') as f:
37
- img_bytes = f.read()
38
- img_gt = imfrombytes(img_bytes, float32=True)
39
- with open(os.path.join(lr_dir, lr_file), 'rb') as f:
40
- img_bytes = f.read()
41
- img_lr = imfrombytes(img_bytes, float32=True)
42
-
43
- h, w = img_gt.shape[0:2]
44
- crop_pad_size = 512
45
- if h < crop_pad_size or w < crop_pad_size:
46
- pad_h = max(0, crop_pad_size - h)
47
- pad_w = max(0, crop_pad_size - w)
48
- img_gt = cv2.copyMakeBorder(img_gt, 0, pad_h, 0, pad_w, cv2.BORDER_REFLECT_101)
49
-
50
- if img_gt.shape[0] > crop_pad_size or img_gt.shape[1] > crop_pad_size:
51
- h, w = img_gt.shape[0:2]
52
- top = 500
53
- left = 250
54
- img_gt = img_gt[top:top + crop_pad_size, left:left + crop_pad_size, ...]
55
- img_lr = img_lr[top//4:top//4 + crop_pad_size//4, left//4:left//4 + crop_pad_size//4, ...]
56
-
57
-
58
- img_gt = img2tensor([img_gt], bgr2rgb=True, float32=True)[0]
59
- img_lr = img2tensor([img_lr], bgr2rgb=True, float32=True)[0]
60
- img_gt = img_gt.unsqueeze(0).cuda()
61
- img_gt = usm_sharpener(img_gt).squeeze(0)
62
- gt = torch.clamp(img_gt, 0, 1)
63
- lr = torch.clamp(img_lr, 0, 1)
64
-
65
- sr_bicubic = F.interpolate(lr.unsqueeze(0), size=(gt.size(-2), gt.size(-1)), mode='bicubic',).squeeze(0)
66
-
67
- lr_save_path = os.path.join(lr_path,'{}.png'.format(str(step).zfill(7)))
68
- gt_save_path = os.path.join(gt_path, '{}.png'.format(str(step).zfill(7)))
69
- sr_bicubic_save_path = os.path.join(sr_bicubic_path, '{}.png'.format(str(step).zfill(7)))
70
-
71
- cv2.imwrite(lr_save_path, 255*lr.detach().cpu().squeeze().permute(1,2,0).numpy()[..., ::-1])
72
- cv2.imwrite(gt_save_path, 255*gt.detach().cpu().squeeze().permute(1,2,0).numpy()[..., ::-1])
73
- cv2.imwrite(sr_bicubic_save_path, 255*sr_bicubic.detach().cpu().squeeze().permute(1,2,0).numpy()[..., ::-1])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
utils_data/make_paired_data.py DELETED
@@ -1,330 +0,0 @@
1
- '''
2
- * SeeSR: Towards Semantics-Aware Real-World Image Super-Resolution
3
- * Modified from diffusers by Rongyuan Wu
4
- * 24/12/2023
5
- '''
6
- import os
7
- import sys
8
- sys.path.append(os.getcwd())
9
- import cv2
10
-
11
- import torch
12
- import torch.nn.functional as F
13
- from pytorch_lightning import seed_everything
14
-
15
- import argparse
16
- from basicsr.data.realesrgan_dataset import RealESRGANDataset
17
- from ram.models import ram
18
- from ram import inference_ram as inference
19
-
20
- parser = argparse.ArgumentParser()
21
- parser.add_argument("--gt_path", nargs='+', default=['PATH 1', 'PATH 2'], help='the path of high-resolution images')
22
- parser.add_argument("--save_dir", type=str, default='preset/datasets/train_datasets/training_for_seesr', help='the save path of the training dataset.')
23
- parser.add_argument("--start_gpu", type=int, default=1, help='if you have 5 GPUs, you can set it to 1/2/3/4/5 on five gpus for parallel processing., which will save your time. ')
24
- parser.add_argument("--batch_size", type=int, default=10, help='smaller batch size means much time but more extensive degradation for making the training dataset.')
25
- parser.add_argument("--epoch", type=int, default=1, help='decide how many epochs to create for the dataset.')
26
- args = parser.parse_args()
27
-
28
- print(f'====== START GPU: {args.start_gpu} =========')
29
- seed_everything(24+args.start_gpu*1000)
30
-
31
- from torchvision.transforms import Normalize, Compose
32
- args_training_dataset = {}
33
-
34
- # Please set your gt path here. If you have multi dirs, you can set it as ['PATH1', 'PATH2', 'PATH3', ...]
35
- args_training_dataset['gt_path'] = args.gt_path
36
-
37
- #################### REALESRGAN SETTING ###########################
38
- args_training_dataset['queue_size'] = 160
39
- args_training_dataset['crop_size'] = 512
40
- args_training_dataset['io_backend'] = {}
41
- args_training_dataset['io_backend']['type'] = 'disk'
42
-
43
- # args_training_dataset['blur_kernel_size'] = 21
44
- # args_training_dataset['kernel_list'] = ['iso', 'aniso', 'generalized_iso', 'generalized_aniso', 'plateau_iso', 'plateau_aniso']
45
- # args_training_dataset['kernel_prob'] = [0.45, 0.25, 0.12, 0.03, 0.12, 0.03]
46
- # args_training_dataset['sinc_prob'] = 0.1
47
- # args_training_dataset['blur_sigma'] = [0.2, 3]
48
- # args_training_dataset['betag_range'] = [0.5, 4]
49
- # args_training_dataset['betap_range'] = [1, 2]
50
-
51
- args_training_dataset['blur_kernel_size'] = 2
52
- args_training_dataset['kernel_list'] = ['iso', 'aniso', 'generalized_iso', 'generalized_aniso', 'plateau_iso', 'plateau_aniso']
53
- args_training_dataset['kernel_prob'] = [0.6, 0.2, 0.1, 0.05, 0.05, 0]
54
- args_training_dataset['sinc_prob'] = 0.05
55
- args_training_dataset['blur_sigma'] = [0.01, 0.02]
56
- args_training_dataset['betag_range'] = [0.01, 0.02]
57
- args_training_dataset['betap_range'] = [0.01, 0.02]
58
-
59
- # args_training_dataset['blur_kernel_size2'] = 11
60
- # args_training_dataset['kernel_list2'] = ['iso', 'aniso', 'generalized_iso', 'generalized_aniso', 'plateau_iso', 'plateau_aniso']
61
- # args_training_dataset['kernel_prob2'] = [0.45, 0.25, 0.12, 0.03, 0.12, 0.03]
62
- # args_training_dataset['sinc_prob2'] = 0.1
63
- # args_training_dataset['blur_sigma2'] = [0.2, 1.5]
64
- # args_training_dataset['betag_range2'] = [0.5, 4.0]
65
- # args_training_dataset['betap_range2'] = [1, 2]
66
-
67
- args_training_dataset['blur_kernel_size2'] = 2
68
- args_training_dataset['kernel_list2'] = ['iso', 'aniso', 'generalized_iso', 'generalized_aniso', 'plateau_iso', 'plateau_aniso']
69
- args_training_dataset['kernel_prob2'] = [0.6, 0.2, 0.1, 0.05, 0.05, 0]
70
- args_training_dataset['sinc_prob2'] = 0.05
71
- args_training_dataset['blur_sigma2'] = [0.01, 0.02]
72
- args_training_dataset['betag_range2'] = [0.01, 0.02]
73
- args_training_dataset['betap_range2'] = [0.01, 0.02]
74
-
75
- args_training_dataset['final_sinc_prob'] = 0.4
76
-
77
- args_training_dataset['use_hflip'] = True
78
- args_training_dataset['use_rot'] = False
79
-
80
- train_dataset = RealESRGANDataset(args_training_dataset)
81
- batch_size = args.batch_size
82
- train_dataloader = torch.utils.data.DataLoader(
83
- train_dataset,
84
- shuffle=False,
85
- batch_size=batch_size,
86
- num_workers=4,
87
- drop_last=True,
88
- )
89
-
90
- #################### REALESRGAN SETTING ###########################
91
- args_degradation = {}
92
- # the first degradation process
93
- # args_degradation['resize_prob'] = [0.2, 0.7, 0.1] # up, down, keep
94
- # args_degradation['resize_range'] = [0.15, 1.5]
95
- # args_degradation['gaussian_noise_prob'] = 0.5
96
- # args_degradation['noise_range'] = [1, 30]
97
- # args_degradation['poisson_scale_range'] = [0.05, 3.0]
98
- # args_degradation['gray_noise_prob'] = 0.4
99
- # args_degradation['jpeg_range'] = [30, 95]
100
-
101
- args_degradation['resize_prob'] = [0.4, 0.4, 0.2] # up, down, keep
102
- args_degradation['resize_range'] = [0.95, 1.05]
103
- args_degradation['gaussian_noise_prob'] = 0.03
104
- args_degradation['noise_range'] = [0.01, 0.03]
105
- args_degradation['poisson_scale_range'] = [0.01, 0.1]
106
- args_degradation['gray_noise_prob'] = 0.1
107
- args_degradation['jpeg_range'] = [80, 100]
108
-
109
- # the second degradation process
110
- # args_degradation['second_blur_prob'] = 0.8
111
- # args_degradation['resize_prob2'] = [0.3, 0.4, 0.3] # up, down, keep
112
- # args_degradation['resize_range2'] = [0.3, 1.2]
113
- # args_degradation['gaussian_noise_prob2'] = 0.5
114
- # args_degradation['noise_range2'] = [1, 25]
115
- # args_degradation['poisson_scale_range2'] = [0.05, 2.5]
116
- # args_degradation['gray_noise_prob2'] = 0.4
117
- # args_degradation['jpeg_range2'] = [30, 95]
118
-
119
- args_degradation['second_blur_prob'] = 0.1
120
- args_degradation['resize_prob2'] = [0.4, 0.4, 0.2] # up, down, keep
121
- args_degradation['resize_range2'] = [0.95, 1.05]
122
- args_degradation['gaussian_noise_prob2'] = 0.03
123
- args_degradation['noise_range2'] = [0.01, 0.03]
124
- args_degradation['poisson_scale_range2'] = [0.01, 0.1]
125
- args_degradation['gray_noise_prob2'] = 0.1
126
- args_degradation['jpeg_range2'] = [80,100]
127
-
128
- args_degradation['gt_size']= 512
129
- args_degradation['no_degradation_prob']= 0.01
130
-
131
-
132
- from basicsr.utils import DiffJPEG, USMSharp
133
- from basicsr.utils.img_process_util import filter2D
134
- from basicsr.data.transforms import paired_random_crop, triplet_random_crop
135
- from basicsr.data.degradations import random_add_gaussian_noise_pt, random_add_poisson_noise_pt, random_add_speckle_noise_pt, random_add_saltpepper_noise_pt, bivariate_Gaussian
136
- import random
137
- import torch.nn.functional as F
138
-
139
- def realesrgan_degradation(batch, args_degradation, use_usm=True, sf=4, resize_lq=True):
140
- jpeger = DiffJPEG(differentiable=False).cuda()
141
- usm_sharpener = USMSharp().cuda() # do usm sharpening
142
- im_gt = batch['gt'].cuda()
143
- im_gt_ori = im_gt
144
- if use_usm:
145
- im_gt = usm_sharpener(im_gt)
146
- im_gt_ori = im_gt_ori.to(memory_format=torch.contiguous_format).float()
147
- im_gt = im_gt.to(memory_format=torch.contiguous_format).float()
148
- kernel1 = batch['kernel1'].cuda()
149
- kernel2 = batch['kernel2'].cuda()
150
- sinc_kernel = batch['sinc_kernel'].cuda()
151
-
152
- ori_h, ori_w = im_gt.size()[2:4]
153
-
154
- # ----------------------- The first degradation process ----------------------- #
155
-
156
- if random.random()<0.5:
157
- out = im_gt_ori
158
- out = F.interpolate(
159
- out,
160
- size=(ori_h // sf,ori_w // sf),
161
- mode='bicubic',
162
- )
163
- else:
164
- print('degration')
165
- # blur
166
- out = filter2D(im_gt_ori, kernel1)
167
- # random resize
168
- updown_type = random.choices(
169
- ['up', 'down', 'keep'],
170
- args_degradation['resize_prob'],
171
- )[0]
172
- if updown_type == 'up':
173
- scale = random.uniform(1, args_degradation['resize_range'][1])
174
- elif updown_type == 'down':
175
- scale = random.uniform(args_degradation['resize_range'][0], 1)
176
- else:
177
- scale = 1
178
- mode = random.choice(['area', 'bilinear', 'bicubic'])
179
- out = F.interpolate(out, scale_factor=scale, mode=mode)
180
- # add noise
181
- gray_noise_prob = args_degradation['gray_noise_prob']
182
- if random.random() < args_degradation['gaussian_noise_prob']:
183
- out = random_add_gaussian_noise_pt(
184
- out,
185
- sigma_range=args_degradation['noise_range'],
186
- clip=True,
187
- rounds=False,
188
- gray_prob=gray_noise_prob,
189
- )
190
- else:
191
- out = random_add_poisson_noise_pt(
192
- out,
193
- scale_range=args_degradation['poisson_scale_range'],
194
- gray_prob=gray_noise_prob,
195
- clip=True,
196
- rounds=False)
197
- # JPEG compression
198
- jpeg_p = out.new_zeros(out.size(0)).uniform_(*args_degradation['jpeg_range'])
199
- out = torch.clamp(out, 0, 1) # clamp to [0, 1], otherwise JPEGer will result in unpleasant artifacts
200
- out = jpeger(out, quality=jpeg_p)
201
-
202
- # ----------------------- The second degradation process ----------------------- #
203
- # blur
204
- if random.random() < args_degradation['second_blur_prob']:
205
- out = filter2D(out, kernel2)
206
- # random resize
207
- updown_type = random.choices(
208
- ['up', 'down', 'keep'],
209
- args_degradation['resize_prob2'],
210
- )[0]
211
- if updown_type == 'up':
212
- scale = random.uniform(1, args_degradation['resize_range2'][1])
213
- elif updown_type == 'down':
214
- scale = random.uniform(args_degradation['resize_range2'][0], 1)
215
- else:
216
- scale = 1
217
- mode = random.choice(['area', 'bilinear', 'bicubic'])
218
- out = F.interpolate(
219
- out,
220
- size=(int(ori_h / sf * scale),
221
- int(ori_w / sf * scale)),
222
- mode=mode,
223
- )
224
- # add noise
225
- gray_noise_prob = args_degradation['gray_noise_prob2']
226
- if random.random() < args_degradation['gaussian_noise_prob2']:
227
- out = random_add_gaussian_noise_pt(
228
- out,
229
- sigma_range=args_degradation['noise_range2'],
230
- clip=True,
231
- rounds=False,
232
- gray_prob=gray_noise_prob,
233
- )
234
- else:
235
- out = random_add_poisson_noise_pt(
236
- out,
237
- scale_range=args_degradation['poisson_scale_range2'],
238
- gray_prob=gray_noise_prob,
239
- clip=True,
240
- rounds=False,
241
- )
242
-
243
- # JPEG compression + the final sinc filter
244
- # We also need to resize images to desired sizes. We group [resize back + sinc filter] together
245
- # as one operation.
246
- # We consider two orders:
247
- # 1. [resize back + sinc filter] + JPEG compression
248
- # 2. JPEG compression + [resize back + sinc filter]
249
- # Empirically, we find other combinations (sinc + JPEG + Resize) will introduce twisted lines.
250
- if random.random() < 0.5:
251
- # resize back + the final sinc filter
252
- mode = random.choice(['area', 'bilinear', 'bicubic'])
253
- out = F.interpolate(
254
- out,
255
- size=(ori_h // sf,
256
- ori_w // sf),
257
- mode=mode,
258
- )
259
- out = filter2D(out, sinc_kernel)
260
- # JPEG compression
261
- jpeg_p = out.new_zeros(out.size(0)).uniform_(*args_degradation['jpeg_range2'])
262
- out = torch.clamp(out, 0, 1)
263
- out = jpeger(out, quality=jpeg_p)
264
- else:
265
- # JPEG compression
266
- jpeg_p = out.new_zeros(out.size(0)).uniform_(*args_degradation['jpeg_range2'])
267
- out = torch.clamp(out, 0, 1)
268
- out = jpeger(out, quality=jpeg_p)
269
- # resize back + the final sinc filter
270
- mode = random.choice(['area', 'bilinear', 'bicubic'])
271
- out = F.interpolate(
272
- out,
273
- size=(ori_h // sf,
274
- ori_w // sf),
275
- mode=mode,
276
- )
277
- out = filter2D(out, sinc_kernel)
278
-
279
- # clamp and round
280
- im_lq = torch.clamp(out, 0, 1.0)
281
-
282
- # random crop
283
- gt_size = args_degradation['gt_size']
284
- im_gt, im_lq = paired_random_crop(im_gt, im_lq, gt_size, sf)
285
- lq, gt = im_lq, im_gt
286
-
287
-
288
- gt = torch.clamp(gt, 0, 1)
289
- lq = torch.clamp(lq, 0, 1)
290
-
291
- return lq, gt
292
-
293
-
294
- root_path = args.save_dir
295
- gt_path = os.path.join(root_path, 'gt')
296
- lr_path = os.path.join(root_path, 'lr')
297
- sr_bicubic_path = os.path.join(root_path, 'sr_bicubic')
298
- os.makedirs(gt_path, exist_ok=True)
299
- os.makedirs(lr_path, exist_ok=True)
300
- os.makedirs(sr_bicubic_path, exist_ok=True)
301
-
302
-
303
- epochs = args.epoch
304
- step = len(train_dataset) * epochs * args.start_gpu
305
- step = 0
306
- with torch.no_grad():
307
- for epoch in range(epochs):
308
- for num_batch, batch in enumerate(train_dataloader):
309
- lr_batch, gt_batch = realesrgan_degradation(batch, args_degradation=args_degradation)
310
- sr_bicubic_batch = F.interpolate(lr_batch, size=(gt_batch.size(-2), gt_batch.size(-1)), mode='bicubic',)
311
-
312
- for i in range(batch_size):
313
- step += 1
314
- print('process {} images...'.format(step))
315
- lr = lr_batch[i, ...]
316
- gt = gt_batch[i, ...]
317
- sr_bicubic = sr_bicubic_batch[i, ...]
318
-
319
- lr_save_path = os.path.join(lr_path,'{}.png'.format(str(step).zfill(7)))
320
- gt_save_path = os.path.join(gt_path, '{}.png'.format(str(step).zfill(7)))
321
- sr_bicubic_save_path = os.path.join(sr_bicubic_path, '{}.png'.format(str(step).zfill(7)))
322
-
323
- cv2.imwrite(lr_save_path, 255*lr.detach().cpu().squeeze().permute(1,2,0).numpy()[..., ::-1])
324
- cv2.imwrite(gt_save_path, 255*gt.detach().cpu().squeeze().permute(1,2,0).numpy()[..., ::-1])
325
- cv2.imwrite(sr_bicubic_save_path, 255*sr_bicubic.detach().cpu().squeeze().permute(1,2,0).numpy()[..., ::-1])
326
-
327
-
328
- del lr_batch, gt_batch, sr_bicubic_batch
329
- torch.cuda.empty_cache()
330
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
utils_data/make_paired_data_DAPE.py DELETED
@@ -1,284 +0,0 @@
1
- '''
2
- * SeeSR: Towards Semantics-Aware Real-World Image Super-Resolution
3
- * Modified from diffusers by Rongyuan Wu
4
- * 24/12/2023
5
- '''
6
- import os
7
- import cv2
8
- import torch
9
- import torch.nn.functional as F
10
- from pytorch_lightning import seed_everything
11
- import argparse
12
- import sys
13
- sys.path.append(os.getcwd())
14
-
15
- from basicsr.data.realesrgan_dataset import RealESRGANDataset
16
- from dataloaders.simple_dataset import SimpleDataset
17
-
18
- from ram.models import ram
19
- from ram import inference_ram as inference
20
-
21
- parser = argparse.ArgumentParser()
22
- parser.add_argument("--gt_path", nargs='+', default=['PATH 1', 'PATH 2'], help='the path of high-resolution images')
23
- parser.add_argument("--save_dir", type=str, default='preset/datasets/train_datasets/training_for_dape', help='the save path of the training dataset.')
24
- parser.add_argument("--start_gpu", type=int, default=1, help='if you have 5 GPUs, you can set it to 1/2/3/4/5 on five gpus for parallel processing., which will save your time. ')
25
- parser.add_argument("--batch_size", type=int, default=10, help='smaller batch size means much time but more extensive degradation for making the training dataset.')
26
- parser.add_argument("--epoch", type=int, default=1, help='decide how many epochs to create for the dataset.')
27
- args = parser.parse_args()
28
-
29
- print(f'====== START GPU: {args.start_gpu} =========')
30
- seed_everything(24+args.start_gpu*1000)
31
-
32
- from torchvision.transforms import Normalize, Compose
33
- args_training_dataset = {}
34
-
35
- # Please set your gt path here. If you have multi dirs, you can set it as ['PATH1', 'PATH2', 'PATH3', ...]
36
- args_training_dataset['gt_path'] = args.gt_path
37
-
38
- #################### REALESRGAN SETTING ###########################
39
- args_training_dataset['queue_size'] = 160
40
- args_training_dataset['crop_size'] = 512
41
- args_training_dataset['io_backend'] = {}
42
- args_training_dataset['io_backend']['type'] = 'disk'
43
-
44
- args_training_dataset['blur_kernel_size'] = 21
45
- args_training_dataset['kernel_list'] = ['iso', 'aniso', 'generalized_iso', 'generalized_aniso', 'plateau_iso', 'plateau_aniso']
46
- args_training_dataset['kernel_prob'] = [0.45, 0.25, 0.12, 0.03, 0.12, 0.03]
47
- args_training_dataset['sinc_prob'] = 0.1
48
- args_training_dataset['blur_sigma'] = [0.2, 3]
49
- args_training_dataset['betag_range'] = [0.5, 4]
50
- args_training_dataset['betap_range'] = [1, 2]
51
-
52
- args_training_dataset['blur_kernel_size2'] = 11
53
- args_training_dataset['kernel_list2'] = ['iso', 'aniso', 'generalized_iso', 'generalized_aniso', 'plateau_iso', 'plateau_aniso']
54
- args_training_dataset['kernel_prob2'] = [0.45, 0.25, 0.12, 0.03, 0.12, 0.03]
55
- args_training_dataset['sinc_prob2'] = 0.1
56
- args_training_dataset['blur_sigma2'] = [0.2, 1.5]
57
- args_training_dataset['betag_range2'] = [0.5, 4.0]
58
- args_training_dataset['betap_range2'] = [1, 2]
59
-
60
- args_training_dataset['final_sinc_prob'] = 0.8
61
-
62
- args_training_dataset['use_hflip'] = True
63
- args_training_dataset['use_rot'] = False
64
-
65
- train_dataset = SimpleDataset(args_training_dataset, fix_size=512)
66
- batch_size = args.batch_size
67
- train_dataloader = torch.utils.data.DataLoader(
68
- train_dataset,
69
- shuffle=False,
70
- batch_size=batch_size,
71
- num_workers=11,
72
- drop_last=True,
73
- )
74
-
75
-
76
- #################### REALESRGAN SETTING ###########################
77
- args_degradation = {}
78
- # the first degradation process
79
- args_degradation['resize_prob'] = [0.2, 0.7, 0.1] # up, down, keep
80
- args_degradation['resize_range'] = [0.15, 1.5]
81
- args_degradation['gaussian_noise_prob'] = 0.5
82
- args_degradation['noise_range'] = [1, 30]
83
- args_degradation['poisson_scale_range'] = [0.05, 3.0]
84
- args_degradation['gray_noise_prob'] = 0.4
85
- args_degradation['jpeg_range'] = [30, 95]
86
-
87
- # the second degradation process
88
- args_degradation['second_blur_prob'] = 0.8
89
- args_degradation['resize_prob2'] = [0.3, 0.4, 0.3] # up, down, keep
90
- args_degradation['resize_range2'] = [0.3, 1.2]
91
- args_degradation['gaussian_noise_prob2'] = 0.5
92
- args_degradation['noise_range2'] = [1, 25]
93
- args_degradation['poisson_scale_range2'] = [0.05, 2.5]
94
- args_degradation['gray_noise_prob2'] = 0.4
95
- args_degradation['jpeg_range2'] = [30, 95]
96
-
97
- args_degradation['gt_size']= 512
98
- args_degradation['no_degradation_prob']= 0.01
99
-
100
- from basicsr.utils import DiffJPEG, USMSharp
101
- from basicsr.utils.img_process_util import filter2D
102
- from basicsr.data.transforms import paired_random_crop, triplet_random_crop
103
- from basicsr.data.degradations import random_add_gaussian_noise_pt, random_add_poisson_noise_pt, random_add_speckle_noise_pt, random_add_saltpepper_noise_pt, bivariate_Gaussian
104
- import random
105
- import torch.nn.functional as F
106
-
107
- def realesrgan_degradation(batch, args_degradation, use_usm=True, sf=4, resize_lq=True):
108
- jpeger = DiffJPEG(differentiable=False).cuda()
109
- usm_sharpener = USMSharp().cuda() # do usm sharpening
110
- im_gt = batch['gt'].cuda()
111
- if use_usm:
112
- im_gt = usm_sharpener(im_gt)
113
- im_gt = im_gt.to(memory_format=torch.contiguous_format).float()
114
- kernel1 = batch['kernel1'].cuda()
115
- kernel2 = batch['kernel2'].cuda()
116
- sinc_kernel = batch['sinc_kernel'].cuda()
117
-
118
- ori_h, ori_w = im_gt.size()[2:4]
119
-
120
- # ----------------------- The first degradation process ----------------------- #
121
- # blur
122
- out = filter2D(im_gt, kernel1)
123
- # random resize
124
- updown_type = random.choices(
125
- ['up', 'down', 'keep'],
126
- args_degradation['resize_prob'],
127
- )[0]
128
- if updown_type == 'up':
129
- scale = random.uniform(1, args_degradation['resize_range'][1])
130
- elif updown_type == 'down':
131
- scale = random.uniform(args_degradation['resize_range'][0], 1)
132
- else:
133
- scale = 1
134
- mode = random.choice(['area', 'bilinear', 'bicubic'])
135
- out = F.interpolate(out, scale_factor=scale, mode=mode)
136
- # add noise
137
- gray_noise_prob = args_degradation['gray_noise_prob']
138
- if random.random() < args_degradation['gaussian_noise_prob']:
139
- out = random_add_gaussian_noise_pt(
140
- out,
141
- sigma_range=args_degradation['noise_range'],
142
- clip=True,
143
- rounds=False,
144
- gray_prob=gray_noise_prob,
145
- )
146
- else:
147
- out = random_add_poisson_noise_pt(
148
- out,
149
- scale_range=args_degradation['poisson_scale_range'],
150
- gray_prob=gray_noise_prob,
151
- clip=True,
152
- rounds=False)
153
- # JPEG compression
154
- jpeg_p = out.new_zeros(out.size(0)).uniform_(*args_degradation['jpeg_range'])
155
- out = torch.clamp(out, 0, 1) # clamp to [0, 1], otherwise JPEGer will result in unpleasant artifacts
156
- out = jpeger(out, quality=jpeg_p)
157
-
158
- # ----------------------- The second degradation process ----------------------- #
159
- # blur
160
- if random.random() < args_degradation['second_blur_prob']:
161
- out = filter2D(out, kernel2)
162
- # random resize
163
- updown_type = random.choices(
164
- ['up', 'down', 'keep'],
165
- args_degradation['resize_prob2'],
166
- )[0]
167
- if updown_type == 'up':
168
- scale = random.uniform(1, args_degradation['resize_range2'][1])
169
- elif updown_type == 'down':
170
- scale = random.uniform(args_degradation['resize_range2'][0], 1)
171
- else:
172
- scale = 1
173
- mode = random.choice(['area', 'bilinear', 'bicubic'])
174
- out = F.interpolate(
175
- out,
176
- size=(int(ori_h / sf * scale),
177
- int(ori_w / sf * scale)),
178
- mode=mode,
179
- )
180
- # add noise
181
- gray_noise_prob = args_degradation['gray_noise_prob2']
182
- if random.random() < args_degradation['gaussian_noise_prob2']:
183
- out = random_add_gaussian_noise_pt(
184
- out,
185
- sigma_range=args_degradation['noise_range2'],
186
- clip=True,
187
- rounds=False,
188
- gray_prob=gray_noise_prob,
189
- )
190
- else:
191
- out = random_add_poisson_noise_pt(
192
- out,
193
- scale_range=args_degradation['poisson_scale_range2'],
194
- gray_prob=gray_noise_prob,
195
- clip=True,
196
- rounds=False,
197
- )
198
-
199
- # JPEG compression + the final sinc filter
200
- # We also need to resize images to desired sizes. We group [resize back + sinc filter] together
201
- # as one operation.
202
- # We consider two orders:
203
- # 1. [resize back + sinc filter] + JPEG compression
204
- # 2. JPEG compression + [resize back + sinc filter]
205
- # Empirically, we find other combinations (sinc + JPEG + Resize) will introduce twisted lines.
206
- if random.random() < 0.5:
207
- # resize back + the final sinc filter
208
- mode = random.choice(['area', 'bilinear', 'bicubic'])
209
- out = F.interpolate(
210
- out,
211
- size=(ori_h // sf,
212
- ori_w // sf),
213
- mode=mode,
214
- )
215
- out = filter2D(out, sinc_kernel)
216
- # JPEG compression
217
- jpeg_p = out.new_zeros(out.size(0)).uniform_(*args_degradation['jpeg_range2'])
218
- out = torch.clamp(out, 0, 1)
219
- out = jpeger(out, quality=jpeg_p)
220
- else:
221
- # JPEG compression
222
- jpeg_p = out.new_zeros(out.size(0)).uniform_(*args_degradation['jpeg_range2'])
223
- out = torch.clamp(out, 0, 1)
224
- out = jpeger(out, quality=jpeg_p)
225
- # resize back + the final sinc filter
226
- mode = random.choice(['area', 'bilinear', 'bicubic'])
227
- out = F.interpolate(
228
- out,
229
- size=(ori_h // sf,
230
- ori_w // sf),
231
- mode=mode,
232
- )
233
- out = filter2D(out, sinc_kernel)
234
-
235
- # clamp and round
236
- im_lq = torch.clamp(out, 0, 1.0)
237
-
238
- # random crop
239
- gt_size = args_degradation['gt_size']
240
- im_gt, im_lq = paired_random_crop(im_gt, im_lq, gt_size, sf)
241
- lq, gt = im_lq, im_gt
242
-
243
-
244
- gt = torch.clamp(gt, 0, 1)
245
- lq = torch.clamp(lq, 0, 1)
246
-
247
- return lq, gt
248
-
249
-
250
- root_path = args.save_dir
251
- gt_path = os.path.join(root_path, 'gt')
252
- lr_path = os.path.join(root_path, 'lr')
253
- sr_bicubic_path = os.path.join(root_path, 'sr_bicubic')
254
- os.makedirs(gt_path, exist_ok=True)
255
- os.makedirs(lr_path, exist_ok=True)
256
- os.makedirs(sr_bicubic_path, exist_ok=True)
257
-
258
- epochs = args.epoch
259
- step = len(train_dataset) * epochs * args.start_gpu
260
- with torch.no_grad():
261
- for epoch in range(epochs):
262
- for num_batch, batch in enumerate(train_dataloader):
263
- lr_batch, gt_batch = realesrgan_degradation(batch, args_degradation=args_degradation)
264
- sr_bicubic_batch = F.interpolate(lr_batch, size=(gt_batch.size(-2), gt_batch.size(-1)), mode='bicubic',)
265
-
266
- for i in range(batch_size):
267
- step += 1
268
- print('process {} images...'.format(step))
269
- lr = lr_batch[i, ...]
270
- gt = gt_batch[i, ...]
271
- sr_bicubic = sr_bicubic_batch[i, ...]
272
-
273
- lr_save_path = os.path.join(lr_path,'{}.png'.format(str(step).zfill(7)))
274
- gt_save_path = os.path.join(gt_path, '{}.png'.format(str(step).zfill(7)))
275
- sr_bicubic_save_path = os.path.join(sr_bicubic_path, '{}.png'.format(str(step).zfill(7)))
276
-
277
- cv2.imwrite(lr_save_path, 255*lr.detach().cpu().squeeze().permute(1,2,0).numpy()[..., ::-1])
278
- cv2.imwrite(gt_save_path, 255*gt.detach().cpu().squeeze().permute(1,2,0).numpy()[..., ::-1])
279
- cv2.imwrite(sr_bicubic_save_path, 255*sr_bicubic.detach().cpu().squeeze().permute(1,2,0).numpy()[..., ::-1])
280
-
281
-
282
- del lr_batch, gt_batch, sr_bicubic_batch
283
- torch.cuda.empty_cache()
284
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
utils_data/make_tags.py DELETED
@@ -1,90 +0,0 @@
1
- '''
2
- * SeeSR: Towards Semantics-Aware Real-World Image Super-Resolution
3
- * Modified from diffusers by Rongyuan Wu
4
- * 24/12/2023
5
- '''
6
- import torch
7
- import torch.nn as nn
8
- import torch.nn.functional as F
9
- import torch.utils.data as data
10
- from torch.utils.data import DataLoader
11
- from torchvision import transforms
12
- from typing import Mapping, Any
13
-
14
- import random
15
- import os
16
- import cv2
17
- import glob
18
- import json
19
- import math
20
- from tqdm import tqdm
21
-
22
-
23
- import numpy as np
24
- from PIL import Image
25
-
26
- import sys
27
- sys.path.append(os.getcwd())
28
-
29
- from ram.models.ram import ram
30
- from ram import inference_ram as inference
31
- from ram import get_transform
32
- from ram.utils import build_openset_label_embedding
33
-
34
- from basicsr.data.ram_tag_dataset import RAMTagDataset
35
-
36
- ram_transforms = transforms.Compose([
37
- transforms.ToTensor(),
38
- transforms.Resize((384, 384)),
39
- transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
40
- ])
41
-
42
- import argparse
43
- parser = argparse.ArgumentParser()
44
- parser.add_argument("--root_path", type=str, default='preset/datasets/train_datasets/training_for_seesr', help='the dataset you want to tag.') #
45
- parser.add_argument("--start_gpu", type=int, default=0, help='if you have 5 GPUs, you can set it to 0/1/2/3/4 when using different GPU for parallel processing. It will save your time.')
46
- parser.add_argument("--all_gpu", type=int, default=1, help='if you set --start_gpu max to 5, please set it to 5')
47
- args = parser.parse_args()
48
-
49
- gt_path = os.path.join(args.root_path, 'gt')
50
- tag_path = os.path.join(args.root_path, 'tag')
51
- os.makedirs(tag_path, exist_ok=True)
52
-
53
- lq_lists = glob.glob(os.path.join(gt_path, '*.png'))
54
- print(f'There are {len(lq_lists)} imgs' )
55
-
56
- model = ram(pretrained='/media/ssd8T/ly/SeeSR/preset/models/ram_swin_large_14m.pth',
57
- image_size=384,
58
- vit='swin_l')
59
- model = model.eval()
60
- model = model.to('cuda')
61
-
62
- start_num = args.start_gpu * len(lq_lists)//args.all_gpu
63
- end_num = (args.start_gpu+1) * len(lq_lists)//args.all_gpu
64
-
65
- print(f'===== process [{start_num} {end_num}] =====')
66
- with torch.no_grad():
67
- for lq_idx, lq_path in enumerate(lq_lists[start_num:end_num]):
68
- print(f' ====== process {lq_idx,lq_path} imgs... =====')
69
- basename = os.path.basename(lq_path).split('.')[0]
70
- lq = ram_transforms(Image.open(lq_path)).unsqueeze(0).to('cuda')
71
- gt_captions = inference(lq, model)
72
- gt_prompt = f"{gt_captions[0]},"
73
- tag_save_path = tag_path + f'/{basename}.txt'
74
- f = open(f"{tag_save_path}", "w")
75
- f.write(gt_prompt)
76
- f.close()
77
- print(f'The GT tag of {basename}.txt: {gt_prompt}')
78
-
79
-
80
-
81
-
82
-
83
-
84
-
85
-
86
-
87
-
88
-
89
-
90
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
utils_data/metrics.py DELETED
@@ -1,64 +0,0 @@
1
- import os
2
- import pyiqa
3
- import argparse
4
- from tqdm import tqdm
5
-
6
- def test_image_quality(image_dir, metrics, weight_paths):
7
- """
8
- 测试指定文件夹中所有 PNG 图像的质量指标。
9
-
10
- Args:
11
- image_dir (str): 包含 PNG 图像的文件夹路径。
12
- metrics (list): 需要测试的指标列表,例如 ['musiq', 'maniqa', 'clipiqa'].
13
- weight_paths (dict): 每个指标的本地权重文件路径。
14
- """
15
- # 初始化指标模型
16
- metric_models = {}
17
- for metric in metrics:
18
- if metric in weight_paths:
19
- # 如果提供了本地权重路径,则加载本地权重
20
- model = pyiqa.create_metric(metric, pretrained_model_path=weight_paths[metric])
21
- else:
22
- # 否则使用默认权重(需要网络下载)
23
- model = pyiqa.create_metric(metric)
24
- metric_models[metric] = model
25
-
26
- # 获取所有 PNG 图像路径
27
- image_paths = [os.path.join(image_dir, f) for f in os.listdir(image_dir) if f.endswith('.png')]
28
- if not image_paths:
29
- print(f"未找到 PNG 图像:{image_dir}")
30
- return
31
-
32
-
33
- # 遍历图像并计算指标
34
- results = {metric: [] for metric in metrics}
35
- for image_path in tqdm(image_paths, desc="Processing images"):
36
- for metric, model in metric_models.items():
37
- score = model(image_path) # 计算指标分数
38
- results[metric].append(score.item()) # 将分数添加到结果中
39
-
40
- # 打印结果
41
- print("\n测试结果:")
42
- for metric, scores in results.items():
43
- avg_score = sum(scores) / len(scores)
44
- # print(f"{metric.upper()} - 平均分数: {avg_score:.4f}")
45
- print(avg_score)
46
- # print(f"{metric.upper()} - 单张图像分数: {scores}")
47
-
48
- if __name__ == "__main__":
49
- # 解析命令行参数
50
- parser = argparse.ArgumentParser(description="测试图像质量指标")
51
- parser.add_argument("--image_dir", type=str, required=True, help="包含 PNG 图像的文件夹路径")
52
- args = parser.parse_args()
53
-
54
- # 需要测试的指标
55
- metrics_to_test = ['musiq', 'maniqa', 'clipiqa']
56
-
57
- # 每个指标的本地权重文件路径
58
- weight_paths = {
59
- 'musiq': '/media/ssd8T/wyw/Pretrained/musiq/musiq_koniq_ckpt-e95806b9.pth',
60
- 'maniqa': '/media/ssd8T/wyw/Pretrained/clipiqa/ckpt_koniq10k.pt',
61
- }
62
-
63
- # 运行测试
64
- test_image_quality(args.image_dir, metrics_to_test, weight_paths)