-
Notifications
You must be signed in to change notification settings - Fork 5
/
data_utils.py
143 lines (101 loc) · 4.92 KB
/
data_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
from os import listdir
from os.path import join
from PIL import Image
from torch.utils.data.dataset import Dataset
from torchvision.transforms import Compose, RandomCrop, ToTensor, ToPILImage, CenterCrop, Scale,RandomHorizontalFlip,RandomVerticalFlip,RandomRotation,Resize,ColorJitter
import skimage.io
def is_image_file(filename):
return any(filename.endswith(extension) for extension in ['.png', '.jpg', '.jpeg', '.PNG', '.JPG', '.JPEG','bmp'])
def calculate_valid_crop_size(crop_size, upscale_factor):
return crop_size - (crop_size % upscale_factor)
def train_hr_transform(crop_size):
return Compose([
RandomCrop(crop_size),
RandomHorizontalFlip(),
RandomVerticalFlip(),
ColorJitter(0.2,0.2,0.1,0.1),
ToTensor(),
])
def train_lr_transform(crop_size, upscale_factor):
return Compose([
ToPILImage(),
Scale(crop_size // upscale_factor, interpolation=Image.BICUBIC),
ToTensor()
])
class TrainDatasetFromFolder(Dataset):
def __init__(self, dataset_dir, crop_size, upscale_factor):
super(TrainDatasetFromFolder, self).__init__()
self.image_filenames = [join(dataset_dir, x) for x in listdir(dataset_dir) if is_image_file(x)]
crop_size = calculate_valid_crop_size(crop_size, upscale_factor)
self.hr_transform = train_hr_transform(crop_size)
self.lr_transform = train_lr_transform(crop_size, upscale_factor)
def __getitem__(self, index):
hr_image = self.hr_transform(Image.open(self.image_filenames[index]))
lr_image = self.lr_transform(hr_image)
return lr_image, hr_image
def __len__(self):
return len(self.image_filenames)
class ValDatasetFromFolder(Dataset):
def __init__(self, dataset_dir, upscale_factor):
super(ValDatasetFromFolder, self).__init__()
self.upscale_factor = upscale_factor
self.image_filenames = [join(dataset_dir, x) for x in listdir(dataset_dir) if is_image_file(x)]
def __getitem__(self, index):
hr_image = Image.open(self.image_filenames[index])
w, h = hr_image.size
crop_size1 = calculate_valid_crop_size(w, 4)
crop_size2 = calculate_valid_crop_size(h, 4)
lr_scale = Resize((crop_size2 // 4, crop_size1 // 4), interpolation=Image.BICUBIC)
hr_scale = Resize((crop_size2, crop_size1), interpolation=Image.BICUBIC)
hr_image = CenterCrop((crop_size2, crop_size1))(hr_image)
lr_image = lr_scale(hr_image)
hr_restore_img = hr_scale(lr_image)
return ToTensor()(lr_image), ToTensor()(hr_restore_img), ToTensor()(hr_image)
def __len__(self):
return len(self.image_filenames)
class TestDatasetFromFolder(Dataset):
def __init__(self, dataset_dir, upscale_factor):
super(TestDatasetFromFolder, self).__init__()
self.upscale_factor = upscale_factor
self.image_filenames = [join(dataset_dir, x) for x in listdir(dataset_dir) if is_image_file(x)]
def __getitem__(self, index):
lrimg_t = skimage.io.imread(self.image_filenames[index])
lrimg = Image.open(self.image_filenames[index])
if len(lrimg_t.shape) !=3 :
lrimg = lrimg.convert('RGB')
w, h = lrimg.size
hr_scale = Resize((self.upscale_factor * h, self.upscale_factor * w), interpolation=Image.BICUBIC)
restore_img = hr_scale(lrimg)
return ToTensor()(lrimg), ToTensor()(restore_img)
def __len__(self):
return len(self.image_filenames)
class TestDatasetFromFolder2(Dataset):
def __init__(self, dataset_dir, upscale_factor):
super(TestDatasetFromFolder2, self).__init__()
self.upscale_factor = upscale_factor
self.image_filenames = [join(dataset_dir, x) for x in listdir(dataset_dir) if is_image_file(x)]
def __getitem__(self, index):
lr_noiseimg1 = skimage.io.imread(self.image_filenames[index])
lr_noiseimg = Image.open(self.image_filenames[index])
if len(lr_noiseimg1.shape) !=3 :
lr_noiseimg = lr_noiseimg.convert('RGB')
w, h = lr_noiseimg.size
hr_scale = Resize((4*h, 4*w), interpolation=Image.BICUBIC)
hr_restore_img = hr_scale(lr_noiseimg)
return ToTensor()(lr_noiseimg), ToTensor()(hr_restore_img)
def __len__(self):
return len(self.image_filenames)
class TestDatasetFromFolder3(Dataset):
def __init__(self, dataset_dir, upscale_factor):
super(TestDatasetFromFolder3, self).__init__()
self.upscale_factor = upscale_factor
self.image_filenames = [join(dataset_dir, x) for x in listdir(dataset_dir) if is_image_file(x)]
def __getitem__(self, index):
hr_imgg = skimage.io.imread(self.image_filenames[index])
hr_image = Image.open(self.image_filenames[index])
if len(hr_imgg.shape) !=3 :
hr_image = hr_image.convert('RGB')
w, h = hr_image.size
return ToTensor()(hr_image)
def __len__(self):
return len(self.image_filenames)