in src/data_augmentations/__init__.py [0:0]
def sample_params(self, x, seed: int=None):
if type(x) is torch.Tensor:
assert len(x.size()) == 4
width, height = x.size(3), x.size(2)
elif type(x) is Image.Image:
width, height = x.size
if seed is not None:
random.seed(seed)
flip = random.randint(0, 1) if self.flip else 0
area = width * height
for attempt in range(10):
target_area = random.uniform(*self.scale) * area
log_ratio = (math.log(self.ratio[0]), math.log(self.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 w <= width and h <= height:
i = random.randint(0, height - h)
j = random.randint(0, width - w)
return i, j, h, w, flip
# Fallback to central crop
in_ratio = width / height
if (in_ratio < min(self.ratio)):
w = width
h = int(round(w / min(self.ratio)))
elif (in_ratio > max(self.ratio)):
h = height
w = int(round(h * max(self.ratio)))
else: # whole image
w = width
h = height
i = (height - h) // 2
j = (width - w) // 2
return i, j, h, w, flip