-
Notifications
You must be signed in to change notification settings - Fork 2
/
data_loader.py
201 lines (161 loc) · 7.05 KB
/
data_loader.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
from glob import glob
import numpy as np
import torch
import torch.nn.functional as F
from torchvision.transforms import functional as tvF
from torch.utils import data
from torchvision import transforms as T
from torchvision.datasets import ImageFolder
from PIL import Image
import torch
import os
import random
import albumentations as A
from sys import exit
import math
from tps.numpy import warp_image_cv
def rot_crop(x):
"""return maximum width ratio of rotated image without letterbox"""
x = abs(x)
deg45 = math.pi * 0.25
deg135 = math.pi * 0.75
x = x * math.pi / 180
a = (math.sin(deg135 - x) - math.sin(deg45 - x))/(math.cos(deg135-x)-math.cos(deg45-x))
return math.sqrt(2) * (math.sin(deg45-x) - a*math.cos(deg45-x)) / (1-a)
class RandomFRC(T.RandomResizedCrop):
"""RandomHorizontalFlip + RandomRotation + RandomResizedCrop 2 images"""
def __call__(self, img1, img2):
img1 = tvF.resize(img1, self.size, interpolation=Image.LANCZOS)
img2 = tvF.resize(img2, self.size, interpolation=Image.LANCZOS)
if random.random() < 0.5:
img1 = tvF.hflip(img1)
img2 = tvF.hflip(img2)
if random.random() < 0.5:
rot = random.uniform(-10, 10)
crop_ratio = rot_crop(rot)
img1 = tvF.rotate(img1, rot, resample=Image.BILINEAR)
img2 = tvF.rotate(img2, rot, resample=Image.BILINEAR)
img1 = tvF.center_crop(img1, int(img1.size[0] * crop_ratio))
img2 = tvF.center_crop(img2, int(img2.size[0] * crop_ratio))
i, j, h, w = self.get_params(img1, self.scale, self.ratio)
# return the image with the same transformation
return (tvF.resized_crop(img1, i, j, h, w, self.size, self.interpolation),
tvF.resized_crop(img2, i, j, h, w, self.size, self.interpolation))
class Dataset(data.Dataset):
def __init__(self, image_dir, line_dir, transform_common, transform_a, transform_line, transform_original):
self.image_dir = image_dir
self.line_dir = line_dir
self.transform_common = transform_common
self.transform_a = transform_a
self.transform_line = transform_line
self.transform_original = transform_original
self.ids = [f.split('/')[-1] for f in glob(os.path.join(line_dir, '*.png'))]
def __getitem__(self, index):
filename = self.ids[index]
image_path = os.path.join(self.image_dir, filename)
line_path = os.path.join(self.line_dir, filename)
image = Image.open(image_path).convert('RGB')
line = Image.open(line_path).convert('L')
image_ori, line = self.transform_common(image, line)
I_original = self.transform_original(image_ori)
I_gt = self.transform_a(image_ori)
line = self.transform_line(line)
if random.random() <= 0.9:
# I_r = TPS(I_gt)
I_r = TPS(I_gt.unsqueeze(0)).squeeze()
# I_r = I_gt.clone()
else:
I_r = torch.zeros(I_gt.size())
return I_original, I_gt, I_r, line
def __len__(self):
return len(self.ids)
# def TPS(x):
# Pseudo original implementation... But, I do not believe this is correct.
# c,h,w = x.size()
# x = x.numpy()
# common = np.random.rand(4, 2)
# A = np.random.rand(2, 2)
# B = np.random.rand(2, 2)
# c_src = np.concatenate([common, A], 0)
# c_dst = np.concatenate([common, B], 0)
# warped = warp_image_cv(x.transpose(2, 1, 0), c_src, c_dst, dshape=(h, w)).transpose((2, 0, 1)) # HWC -> CHW
# return torch.from_numpy(warped)
def TPS(x):
"""
http://www.mech.tohoku-gakuin.ac.jp/rde/contents/course/robotics/coordtrans.html
"""
def affine_transform(x, theta):
theta = theta.view(-1, 2, 3)
# grid = F.affine_grid(theta, x.size(), align_corners=True)
# x = F.grid_sample(x, grid, align_corners=True)
grid = F.affine_grid(theta, x.size())
x = F.grid_sample(x, grid)
return x
theta1 = np.zeros(9)
theta1[0:6] = np.random.randn(6) * 0.15
theta1 = theta1 + np.array([1,0,0,0,1,0,0,0,1])
affine1 = np.reshape(theta1, (3,3))
affine1 = np.reshape(affine1, -1)[0:6]
affine1 = torch.from_numpy(affine1).type(torch.FloatTensor)
x = affine_transform(x, affine1) # source image
return x
def get_loader(crop_size=256, image_size=266, batch_size=16, dataset='CelebA', mode='train', num_workers=8, line_type='xdog', ROOT='./datasets'):
"""Build and return a data loader."""
transform_common = []
transform_a = []
transform_line = []
transform_original = []
transform_common = RandomFRC(crop_size, scale=(0.9, 1.0), ratio=(0.95, 1.05), interpolation=Image.LANCZOS)
transform_a = T.Compose([
T.ColorJitter(brightness=0.05, contrast=0.2, saturation=0.4, hue=0.4),
T.Resize((crop_size, crop_size), interpolation=Image.LANCZOS),
T.ToTensor(),
T.Normalize(mean=(0.5,0.5,0.5), std=(0.5,0.5,0.5))
])
transform_line = T.Compose([
T.ColorJitter(brightness=0.2, contrast=0.2, saturation=0.2),
T.Resize((crop_size, crop_size), interpolation=Image.LANCZOS),
T.ToTensor(),
# T.RandomErasing(p=0.9, value=1., scale=(0.02, 0.1)) # For experimental
])
transform_original = T.Compose([
T.Resize((crop_size, crop_size), interpolation=Image.LANCZOS),
T.ToTensor(),
T.Normalize(mean=(0.5,0.5,0.5), std=(0.5,0.5,0.5))
])
if dataset == 'line_art':
image_dir = os.path.join(ROOT, 'line_art/train/color')
line_dir = os.path.join(ROOT, 'line_art/train/xdog')
elif dataset == 'tag2pix':
image_dir = os.path.join(ROOT, 'tag2pix/rgb_cropped')
if line_type == 'xdog':
line_dir = os.path.join(ROOT, 'tag2pix/xdog_train')
elif line_type == 'keras':
line_dir = os.path.join(ROOT, 'tag2pix/keras_train')
dataset = Dataset(image_dir, line_dir,
transform_common, transform_a, transform_line, transform_original)
data_loader = data.DataLoader(dataset=dataset,
batch_size=batch_size,
shuffle=(mode=='train'),
num_workers=num_workers,
pin_memory=True,
drop_last=True)
return data_loader
if __name__ == '__main__':
def denorm(x):
"""Convert the range from [-1, 1] to [0, 1]."""
out = (x + 1) / 2
return out.clamp_(0, 1)
from torchvision.utils import save_image
loader = get_loader(
crop_size=256,
image_size=256,
batch_size=20,
dataset='tag2pix', mode='test', num_workers=4, line_type='xdog',
ROOT='./data'
)
loader = iter(loader)
I_ori, I_gt, I_r, I_s = next(loader)
I_concat = denorm(torch.cat([I_ori, I_gt, I_r], dim=2))
I_concat = torch.cat([I_concat, I_s.repeat(1,3,1,1)], dim=2)
save_image(I_concat, 'tmp.png')