-
Notifications
You must be signed in to change notification settings - Fork 14
/
datasets.py
executable file
·197 lines (158 loc) · 7.65 KB
/
datasets.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
# --------------------------------------------------------
# Based on BEiT, timm, DINO and DeiT code bases
# https://github.com/microsoft/unilm/tree/master/beit
# https://github.com/rwightman/pytorch-image-models/tree/master/timm
# https://github.com/facebookresearch/deit
# https://github.com/facebookresearch/dino
# --------------------------------------------------------'
import os
import torch
from torchvision.transforms import Compose, RandomResizedCrop, ToTensor, Normalize, Resize
from dataset_folder import ImageFolder, ImageWithFixedHint, ImageWithFixedHintAndCoord
from hint_generator import InteractiveHintGenerator, RandomHintGenerator
class DataAugmentationForIColoriT:
def __init__(self, args):
# No normalization on RGB space
mean = [0., 0., 0.]
std = [1., 1., 1.]
self.transform = Compose([
RandomResizedCrop(args.input_size),
ToTensor(),
Normalize(
mean=torch.tensor(mean),
std=torch.tensor(std))
])
self.hint_generator = RandomHintGenerator(args.input_size, args.hint_size, args.num_hint_range)
def __call__(self, image):
return self.transform(image), self.hint_generator()
def __repr__(self):
repr = "(DataAugmentationForIColoriT,\n"
repr += " transform = %s,\n" % str(self.transform)
repr += " Hint generator = %s,\n" % str(self.hint_generator)
repr += ")"
return repr
class DataTransformationForIColoriT:
def __init__(self, args):
self.transform = Compose([
Resize((args.input_size, args.input_size)),
ToTensor(),
])
if args.hint_generator == 'RandomHintGenerator':
self.hint_generator = RandomHintGenerator(args.input_size, args.hint_size, args.num_hint_range)
elif args.hint_generator == 'InteractiveHintGenerator':
self.hint_generator = InteractiveHintGenerator(args.input_size, args.hint_size)
else:
raise NotImplementedError(f'{args.hint_generator} is not exist.')
def __call__(self, image):
return self.transform(image), self.hint_generator()
def __repr__(self):
repr = "(DataTransformationForIColoriT,\n"
repr += " transform = %s,\n" % str(self.transform)
repr += " Hint generator = %s,\n" % str(self.hint_generator)
repr += ")"
return repr
class DataTransformationFixedHint:
def __init__(self, args) -> None:
self.input_size = args.input_size
self.hint_size = args.hint_size
self.img_transform = Compose([
Resize((self.input_size, self.input_size)),
ToTensor(),
])
hint_dirs = args.hint_dirs
if isinstance(args.hint_dirs, str):
hint_dirs = [args.hint_dirs]
self.num_hint = [int(os.path.basename(hint_dir)[4:])
for hint_dir in hint_dirs] # hint subdir should be formed h#-n##
def __call__(self, img, hint_coords):
return self.img_transform(img), self.coord2hint(hint_coords)
def coord2hint(self, hint_coords):
hint = torch.ones((len(hint_coords), self.input_size // self.hint_size, self.input_size // self.hint_size))
for idx, hint_coord in enumerate(hint_coords):
for x, y in hint_coord:
hint[idx, x // self.hint_size, y // self.hint_size] = 0
return hint
def __repr__(self):
repr = "(DataTransformationFixedHint,\n"
repr += " img_transform = %s,\n" % str(self.img_transform)
repr += f" Hint generator = Fixed, {self.num_hint}\n"
repr += ")"
return repr
class DataTransformationFixedHintContinuousCoords:
def __init__(self, args) -> None:
self.input_size = args.input_size
self.hint_size = args.hint_size
self.img_transform = Compose([
Resize((self.input_size, self.input_size)),
ToTensor(),
])
hint_dirs = args.hint_dirs
if isinstance(args.hint_dirs, str):
hint_dirs = [args.hint_dirs]
self.num_hint = [int(os.path.basename(hint_dir).split(':')[-1]) for hint_dir in hint_dirs]
def __call__(self, img, hint_coords):
hint_coords = [hint_coords[0][:idx] for idx in range(len(hint_coords[0]) + 1)]
return self.img_transform(img), self.coord2hint(hint_coords)
def coord2hint(self, hint_coords):
hint = torch.ones((len(hint_coords), self.input_size // self.hint_size, self.input_size // self.hint_size))
for idx, hint_coord in enumerate(hint_coords):
for x, y in hint_coord:
hint[idx, x // self.hint_size, y // self.hint_size] = 0
return hint
def __repr__(self):
repr = "(DataTransformationFixedHint,\n"
repr += " img_transform = %s,\n" % str(self.img_transform)
repr += f" Hint generator = Fixed, {self.num_hint}\n"
repr += ")"
return repr
class DataTransformationFixedHintPrevCoods:
def __init__(self, args) -> None:
self.input_size = args.input_size
self.hint_size = args.hint_size
self.img_transform = Compose([
Resize((self.input_size, self.input_size)),
ToTensor(),
])
hint_dirs = args.hint_dirs
if isinstance(args.hint_dirs, str):
hint_dirs = [args.hint_dirs]
self.num_hint = [int(os.path.basename(hint_dir).split(':')[-1]) for hint_dir in hint_dirs]
def __call__(self, img, hint_coords):
hint_coords = [[hint_coord[:-1], hint_coord] for hint_coord in hint_coords]
return self.img_transform(img), self.coord2hint_prev(hint_coords)
def coord2hint_prev(self, hint_coords):
hint = torch.ones((len(hint_coords), 2, self.input_size // self.hint_size, self.input_size // self.hint_size))
for idx, (hint_coord_prev, hint_coord) in enumerate(hint_coords):
for x, y in hint_coord_prev:
hint[idx, 0, x // self.hint_size, y // self.hint_size] = 0
for x, y in hint_coord:
hint[idx, 1, x // self.hint_size, y // self.hint_size] = 0
return hint
def __repr__(self):
repr = "(DataTransformationFixedHint,\n"
repr += " img_transform = %s,\n" % str(self.img_transform)
repr += f" Hint generator = Fixed, {self.num_hint}\n"
repr += ")"
return repr
def build_pretraining_dataset(args):
transform = DataAugmentationForIColoriT(args)
print("Data Aug = %s" % str(transform))
return ImageFolder(args.data_path, transform=transform)
def build_validation_dataset(args):
transform = DataTransformationForIColoriT(args)
print("Data Trans = %s" % str(transform))
return ImageFolder(args.val_data_path, transform=transform,
is_valid_file=(lambda x: False if '.pt' in x else True))
def build_fixed_validation_dataset(args):
transform = DataTransformationFixedHint(args)
print("Data Trans = %s" % str(transform))
return ImageWithFixedHint(args.val_data_path, args.hint_dirs, transform=transform,
return_name=args.return_name, gray_file_list_txt=args.gray_file_list_txt)
def build_fixed_validation_dataset_coord(args, without_tf=False):
transform = DataTransformationFixedHintContinuousCoords(args) if not without_tf else None
print("Data Trans = %s" % str(transform))
return ImageWithFixedHintAndCoord(args.val_data_path, args.hint_dirs, transform=transform)
def build_fixed_validation_dataset_coord_2(args, without_tf=False):
transform = DataTransformationFixedHintPrevCoods(args) if not without_tf else None
print("Data Trans = %s" % str(transform))
return ImageWithFixedHintAndCoord(args.val_data_path, args.hint_dirs, transform=transform)