-
Notifications
You must be signed in to change notification settings - Fork 11
/
utils.py
226 lines (164 loc) · 5.95 KB
/
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
from ImageDataset import ImageDataset
from torch.utils.data import DataLoader
from ImageDataset2 import ImageDataset2, ImageDataset_Inf, ImageDataset3, ImageDataset_qonly
from ImageDataset import ImageDataset_SPAQ, ImageDataset_TID, ImageDataset_PIPAL, ImageDataset_ava
from torchvision.transforms import Compose, ToTensor, Normalize, RandomHorizontalFlip
from torchvision import transforms
from PIL import Image
try:
from torchvision.transforms import InterpolationMode
BICUBIC = InterpolationMode.BICUBIC
except ImportError:
BICUBIC = Image.BICUBIC
def set_dataset(csv_file, bs, data_set, num_workers, preprocess, num_patch, test):
data = ImageDataset2(
csv_file=csv_file,
img_dir=data_set,
num_patch=num_patch,
test=test,
preprocess=preprocess)
if test:
shuffle = False
else:
shuffle = True
loader = DataLoader(data, batch_size=bs, shuffle=shuffle, pin_memory=True, num_workers=num_workers)
return loader
def set_dataset3(csv_file, bs, data_set, num_workers, preprocess, num_patch, test):
data = ImageDataset3(
csv_file=csv_file,
img_dir=data_set,
num_patch=num_patch,
test=test,
preprocess=preprocess)
if test:
shuffle = False
else:
shuffle = True
loader = DataLoader(data, batch_size=bs, shuffle=shuffle, pin_memory=True, num_workers=num_workers)
return loader
def set_dataset2(csv_file, bs, data_set, num_workers, preprocess, num_patch, test):
data = ImageDataset_Inf(
csv_file=csv_file,
img_dir=data_set,
num_patch=num_patch,
test=test,
preprocess=preprocess)
shuffle = False
loader = DataLoader(data, batch_size=bs, shuffle=shuffle, pin_memory=True, num_workers=num_workers)
return loader
def set_spaq(csv_file, bs, data_set, num_workers, preprocess, num_patch, test):
data = ImageDataset_SPAQ(
csv_file=csv_file,
img_dir=data_set,
num_patch=num_patch,
test=test,
preprocess=preprocess)
if test:
shuffle = False
else:
shuffle = True
loader = DataLoader(data, batch_size=bs, shuffle=shuffle, pin_memory=True, num_workers=num_workers)
return loader
def set_tid(csv_file, bs, data_set, num_workers, preprocess, num_patch, test):
data = ImageDataset_TID(
csv_file=csv_file,
img_dir=data_set,
num_patch=num_patch,
test=test,
preprocess=preprocess)
if test:
shuffle = False
else:
shuffle = True
loader = DataLoader(data, batch_size=bs, shuffle=shuffle, pin_memory=True, num_workers=num_workers)
return loader
def set_pipal(csv_file, bs, data_set, num_workers, preprocess, num_patch, test):
data = ImageDataset_PIPAL(
csv_file=csv_file,
img_dir=data_set,
num_patch=num_patch,
test=test,
preprocess=preprocess)
if test:
shuffle = False
else:
shuffle = True
loader = DataLoader(data, batch_size=bs, shuffle=shuffle, pin_memory=True, num_workers=num_workers)
return loader
def set_ava(csv_file, bs, data_set, num_workers, preprocess, num_patch, test):
data = ImageDataset_ava(
npy_file='./ava_test.npy',
img_dir=data_set,
preprocess=preprocess)
loader = DataLoader(data, batch_size=bs, shuffle=False, pin_memory=True, num_workers=num_workers)
return loader
def set_dataset_qonly(csv_file, bs, data_set, num_workers, preprocess, num_patch, test, set):
data = ImageDataset_qonly(
csv_file=csv_file,
img_dir=data_set,
num_patch=num_patch,
set=set,
test=test,
preprocess=preprocess)
if test:
shuffle = False
else:
shuffle = True
loader = DataLoader(data, batch_size=bs, shuffle=shuffle, pin_memory=True, num_workers=num_workers)
return loader
class AdaptiveResize(object):
"""Resize the input PIL Image to the given size adaptively.
Args:
size (sequence or int): Desired output size. If size is a sequence like
(h, w), output size will be matched to this. If size is an int,
smaller edge of the image will be matched to this number.
i.e, if height > width, then image will be rescaled to
(size * height / width, size)
interpolation (int, optional): Desired interpolation. Default is
``PIL.Image.BILINEAR``
"""
def __init__(self, size, interpolation=InterpolationMode.BILINEAR, image_size=None):
assert isinstance(size, int)
self.size = size
self.interpolation = interpolation
if image_size is not None:
self.image_size = image_size
else:
self.image_size = None
def __call__(self, img):
"""
Args:
img (PIL Image): Image to be scaled.
Returns:
PIL Image: Rescaled image.
"""
h, w = img.size
if self.image_size is not None:
if h < self.image_size or w < self.image_size:
return transforms.Resize(self.image_size, self.interpolation)(img)
if h < self.size or w < self.size:
return img
else:
return transforms.Resize(self.size, self.interpolation)(img)
def _convert_image_to_rgb(image):
return image.convert("RGB")
def _preprocess2():
return Compose([
_convert_image_to_rgb,
AdaptiveResize(768),
ToTensor(),
Normalize((0.48145466, 0.4578275, 0.40821073), (0.26862954, 0.26130258, 0.27577711)),
])
def _preprocess3():
return Compose([
_convert_image_to_rgb,
AdaptiveResize(768),
RandomHorizontalFlip(),
ToTensor(),
Normalize((0.48145466, 0.4578275, 0.40821073), (0.26862954, 0.26130258, 0.27577711)),
])
def convert_models_to_fp32(model):
for p in model.parameters():
p.data = p.data.float()
if p.grad is not None:
p.grad.data = p.grad.data.float()