-
Notifications
You must be signed in to change notification settings - Fork 0
/
transforms_overlap.py
258 lines (213 loc) · 8.12 KB
/
transforms_overlap.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.
from __future__ import division
import numpy as np
import torch
import math
import random
from PIL import Image
import warnings
from torchvision.transforms import functional as F
_pil_interpolation_to_str = {
Image.NEAREST: "PIL.Image.NEAREST",
Image.BILINEAR: "PIL.Image.BILINEAR",
Image.BICUBIC: "PIL.Image.BICUBIC",
Image.LANCZOS: "PIL.Image.LANCZOS",
Image.HAMMING: "PIL.Image.HAMMING",
Image.BOX: "PIL.Image.BOX",
}
def _get_image_size(img):
if F._is_pil_image(img):
return img.size
elif isinstance(img, torch.Tensor) and img.dim() > 2:
return img.shape[-2:][::-1]
else:
raise TypeError("Unexpected type {}".format(type(img)))
class ComposeOverLap(object):
"""Composes several transforms together.
Args:
transforms (list of ``Transform`` objects): list of transforms to compose.
Example:
>>> transforms.Compose([
>>> transforms.CenterCrop(10),
>>> transforms.ToTensor(),
>>> ])
"""
def __init__(self, transforms):
self.transforms = transforms
def __call__(self, img, mapping=None):
for t in self.transforms:
if "RandomResizedCropOverLap" in t.__class__.__name__:
img, mapping = t(img)
elif "FlipOverLap" in t.__class__.__name__:
img, mapping = t(img, mapping)
elif "ComposeOverLap" in t.__class__.__name__:
img, mapping = t(img, mapping)
else:
img = t(img)
mapping = np.array(mapping)
return img, mapping
def __repr__(self):
format_string = self.__class__.__name__ + "("
for t in self.transforms:
format_string += "\n"
format_string += " {0}".format(t)
format_string += "\n)"
return format_string
class RandomHorizontalFlipOverLap(object):
"""Horizontally flip the given PIL Image randomly with a given probability.
Args:
p (float): probability of the image being flipped. Default value is 0.5
"""
def __init__(self, p=0.5):
self.p = p
def __call__(self, img, mapping):
"""
Args:
img (PIL Image): Image to be flipped.
Returns:
PIL Image: Randomly flipped image.
"""
if random.random() < self.p:
mapping.append(1)
return F.hflip(img), mapping
mapping.append(0)
return img, mapping
def __repr__(self):
return self.__class__.__name__ + "(p={})".format(self.p)
class RandomResizedCropOverLap(object):
"""Crop the given PIL Image to random size and aspect ratio.
A crop of random size (default: of 0.08 to 1.0) of the original size and a random
aspect ratio (default: of 3/4 to 4/3) of the original aspect ratio is made. This crop
is finally resized to given size.
This is popularly used to train the Inception networks.
Args:
size: expected output size of each edge
scale: range of size of the origin size cropped
ratio: range of aspect ratio of the origin aspect ratio cropped
interpolation: Default: PIL.Image.BILINEAR
"""
def __init__(
self,
size,
scale=(0.08, 1.0),
ratio=(3.0 / 4.0, 4.0 / 3.0),
interpolation=Image.BILINEAR,
):
if isinstance(size, (tuple, list)):
self.size = size
else:
self.size = (size, size)
if (scale[0] > scale[1]) or (ratio[0] > ratio[1]):
warnings.warn("range should be of kind (min, max)")
self.interpolation = interpolation
self.scale = scale
self.ratio = ratio
@staticmethod
def get_params(img, scale, ratio):
"""Get parameters for ``crop`` for a random sized crop.
Args:
img (PIL Image): Image to be cropped.
scale (tuple): range of size of the origin size cropped
ratio (tuple): range of aspect ratio of the origin aspect ratio cropped
Returns:
tuple: params (i, j, h, w) to be passed to ``crop`` for a random
sized crop.
"""
width, height = _get_image_size(img)
area = height * width
for attempt in range(10):
target_area = random.uniform(*scale) * area
log_ratio = (math.log(ratio[0]), math.log(ratio[1]))
aspect_ratio = math.exp(random.uniform(*log_ratio))
w = int(round(math.sqrt(target_area * aspect_ratio)))
h = int(round(math.sqrt(target_area / aspect_ratio)))
if 0 < w <= width and 0 < h <= height:
i = random.randint(0, height - h)
j = random.randint(0, width - w)
return i, j, h, w
# Fallback to central crop
in_ratio = float(width) / float(height)
if in_ratio < min(ratio):
w = width
h = int(round(w / min(ratio)))
elif in_ratio > max(ratio):
h = height
w = int(round(h * max(ratio)))
else: # whole image
w = width
h = height
i = (height - h) // 2
j = (width - w) // 2
return i, j, h, w
def __call__(self, img):
"""
Args:
img (PIL Image): Image to be cropped and resized.
Returns:
PIL Image: Randomly cropped and resized image.
"""
i, j, h, w = self.get_params(img, self.scale, self.ratio)
mapping = [j, i, w, h]
return (
F.resized_crop(img, i, j, h, w, self.size, self.interpolation),
mapping,
)
def __repr__(self):
interpolate_str = _pil_interpolation_to_str[self.interpolation]
format_string = self.__class__.__name__ + "(size={0}".format(self.size)
format_string += ", scale={0}".format(tuple(round(s, 4) for s in self.scale))
format_string += ", ratio={0}".format(tuple(round(r, 4) for r in self.ratio))
format_string += ", interpolation={0})".format(interpolate_str)
return format_string
def get_grid(rectq, rectk, size):
grid = float(size - 1)
overlap = [
max(rectq[0], rectk[0]),
max(rectq[1], rectk[1]),
min(rectq[0] + rectq[2], rectk[0] + rectk[2]),
min(rectq[1] + rectq[3], rectk[1] + rectk[3]),
]
if overlap[0] < overlap[2] and overlap[1] < overlap[3]:
q_overlap = torch.FloatTensor(
[
(overlap[0] - rectq[0]) / rectq[2],
(overlap[1] - rectq[1]) / rectq[3],
(overlap[2] - overlap[0]) / rectq[2],
(overlap[3] - overlap[1]) / rectq[3],
]
)
k_overlap = torch.FloatTensor(
[
(overlap[0] - rectk[0]) / rectk[2],
(overlap[1] - rectk[1]) / rectk[3],
(overlap[2] - overlap[0]) / rectk[2],
(overlap[3] - overlap[1]) / rectk[3],
]
)
q_grid = torch.zeros(size=(size, size, 2), dtype=torch.float32)
k_grid = torch.zeros(size=(size, size, 2), dtype=torch.float32)
q_grid[:, :, 0] = torch.FloatTensor(
[q_overlap[0] + i * q_overlap[2] / grid for i in range(size)]
).view(1, size)
q_grid[:, :, 1] = torch.FloatTensor(
[q_overlap[1] + i * q_overlap[3] / grid for i in range(size)]
).view(size, 1)
k_grid[:, :, 0] = torch.FloatTensor(
[k_overlap[0] + i * k_overlap[2] / grid for i in range(size)]
).view(1, size)
k_grid[:, :, 1] = torch.FloatTensor(
[k_overlap[1] + i * k_overlap[3] / grid for i in range(size)]
).view(size, 1)
# flip
if rectq[4] > 0:
q_grid[:, :, 0] = 1 - q_grid[:, :, 0]
if rectk[4] > 0:
k_grid[:, :, 0] = 1 - k_grid[:, :, 0]
k_grid = 2 * k_grid - 1
q_grid = 2 * q_grid - 1
else:
# fill zero
q_grid = torch.full(fill_value=-2, size=(size, size, 2), dtype=torch.float32)
k_grid = torch.full(fill_value=-2, size=(size, size, 2), dtype=torch.float32)
return q_grid, k_grid