Spaces:
Sleeping
Sleeping
File size: 24,624 Bytes
7e2a2a5 |
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 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 |
from packaging import version
import random
import numpy as np
from PIL import Image, ImageFilter, ImageOps
from torchvision.transforms.transforms import Lambda, Compose
from torchvision.transforms import functional as F
from collections.abc import Iterable
import torch, torchvision
import numbers
import copy
if version.parse(torchvision.__version__) <= version.parse('0.7.0'):
from torchvision.transforms.transforms import _get_image_size
def check_input_type_perform_action(input, type, action, *args, **kwargs):
output = input
if isinstance(input, list):
for i in range(0, len(input)):
if type is None:
if input[i] is not None: # do not combine with last line, to avoid calling isinstance on None.
output[i] = action(input[i], *args, **kwargs)
elif isinstance(input[i], type):
output[i] = action(input[i], *args, **kwargs)
elif type is None:
if input is not None:
output = action(input, *args, **kwargs)
elif isinstance(input, type):
output = action(input, *args, **kwargs)
return output
"""
Most of these functions are imported from torchvision.transforms.transforms and edited to support 2 or more inputs.
"""
class JointCompose(object):
"""
Composes several transforms together.
"""
def __init__(self, transforms):
self.transforms = transforms
def __call__(self, input1, input2):
for t in self.transforms:
input1, input2 = t(input1, input2)
return input1, input2
class Grayscale(object):
def __init__(self, input1_output_channels=1, input2_output_channels=1):
self.input1_output_channels = input1_output_channels
self.input2_output_channels = input2_output_channels
def __call__(self, input1, input2):
output1 = F.to_grayscale(input1, num_output_channels=self.input1_output_channels) if self.input1_output_channels == 1 else input1
output2 = check_input_type_perform_action(input2, Image.Image, F.to_grayscale, num_output_channels=self.input2_output_channels) \
if self.input2_output_channels == 1 else input2
return output1, output2
class Resize(object):
def __init__(self, size, interpolation=Image.BILINEAR):
assert isinstance(size, int) or (isinstance(size, Iterable) and len(size) == 2)
self.size = size
self.interpolation = interpolation
def __call__(self, input1, input2):
output1 = F.resize(input1, self.size, self.interpolation)
output2 = check_input_type_perform_action(input2, Image.Image, F.resize, self.size, self.interpolation)
return output1, output2
class ScaleWidth:
def __init__(self, target_size, method=Image.BICUBIC):
self.target_size = target_size
self.method = method
def scalewidth(self, img):
ow, oh = img.size
w = self.target_size
h = int(self.target_size * oh / ow)
img_resized = img.resize((w, h), self.method)
if h > w:
# if resized image's height is larger than its width, crop the center
left = 0
top = h // 2 - self.target_size // 2
right = self.target_size
bottom = top + self.target_size
img_resized = img_resized.crop((left, top, right, bottom))
elif h < w:
# pad the heights
delta_w = self.target_size - w
delta_h = self.target_size - h
padding = (delta_w // 2, delta_h // 2, delta_w - (delta_w // 2), delta_h - (delta_h // 2))
img_resized = ImageOps.expand(img_resized, padding)
return img_resized
def __call__(self, input1, input2):
output1 = self.scalewidth(input1)
output2 = check_input_type_perform_action(input2, Image.Image, self.scalewidth)
return output1, output2
class RandomCrop(object):
def __init__(self, size, padding=None, pad_if_needed=False, fill=0, padding_mode='constant'):
if isinstance(size, numbers.Number):
self.size = (int(size), int(size))
else:
self.size = size
self.padding = padding
self.pad_if_needed = pad_if_needed
self.fill = fill
self.padding_mode = padding_mode
@staticmethod
def get_params(img, output_size):
if version.parse(torchvision.__version__) <= version.parse('0.7.0'):
w, h = _get_image_size(img)
else:
w, h = F._get_image_size(img)
th, tw = output_size
if w == tw and h == th:
return 0, 0, h, w
i = random.randint(0, h - th)
j = random.randint(0, w - tw)
return i, j, th, tw
def pad(self, img):
if self.padding is not None:
img = F.pad(img, self.padding, self.fill, self.padding_mode)
# pad the width if needed
if self.pad_if_needed and img.size[0] < self.size[1]:
img = F.pad(img, (self.size[1] - img.size[0], 0), self.fill, self.padding_mode)
# pad the height if needed
if self.pad_if_needed and img.size[1] < self.size[0]:
img = F.pad(img, (0, self.size[0] - img.size[1]), self.fill, self.padding_mode)
return img
def get_crop_range(self, img):
return self.get_params(img, self.size)
def pad_and_crop(self, input, i, j, h, w):
return F.crop(self.pad(input), i, j, h, w)
def __call__(self, input1, input2):
output1 = self.pad(input1)
i, j, h, w = self.get_crop_range(output1)
output1 = F.crop(output1, i, j, h, w)
output2 = check_input_type_perform_action(input2, Image.Image, self.pad_and_crop, i, j, h, w)
return output1, output2
class Crop:
def __init__(self, pos, size):
self.pos = pos
self.size = size
def crop(self, img):
ow, oh = img.size
x1, y1 = self.pos
tw = th = self.size
if (ow > tw or oh > th):
return img.crop((x1, y1, x1 + tw, y1 + th))
return img
def __call__(self, input1, input2):
output1 = self.crop(input1)
output2 = check_input_type_perform_action(input2, Image.Image, self.crop)
return output1, output2
class ColorJitter(object):
def __init__(self, brightness=0, contrast=0, saturation=0, hue=0):
self.brightness = self._check_input(brightness, 'brightness')
self.contrast = self._check_input(contrast, 'contrast')
self.saturation = self._check_input(saturation, 'saturation')
self.hue = self._check_input(hue, 'hue', center=0, bound=(-0.5, 0.5),
clip_first_on_zero=False)
def _check_input(self, value, name, center=1, bound=(0, float('inf')), clip_first_on_zero=True):
if isinstance(value, numbers.Number):
if value < 0:
raise ValueError("If {} is a single number, it must be non negative.".format(name))
value = [center - value, center + value]
if clip_first_on_zero:
value[0] = max(value[0], 0)
elif isinstance(value, (tuple, list)) and len(value) == 2:
if not bound[0] <= value[0] <= value[1] <= bound[1]:
raise ValueError("{} values should be between {}".format(name, bound))
else:
raise TypeError("{} should be a single number or a list/tuple with lenght 2.".format(name))
# if value is 0 or (1., 1.) for brightness/contrast/saturation
# or (0., 0.) for hue, do nothing
if value[0] == value[1] == center:
value = None
return value
@staticmethod
def get_params(brightness, contrast, saturation, hue):
transforms = []
if brightness is not None:
brightness_factor = random.uniform(brightness[0], brightness[1])
transforms.append(Lambda(lambda img: F.adjust_brightness(img, brightness_factor)))
if contrast is not None:
contrast_factor = random.uniform(contrast[0], contrast[1])
transforms.append(Lambda(lambda img: F.adjust_contrast(img, contrast_factor)))
if saturation is not None:
saturation_factor = random.uniform(saturation[0], saturation[1])
transforms.append(Lambda(lambda img: F.adjust_saturation(img, saturation_factor)))
if hue is not None:
hue_factor = random.uniform(hue[0], hue[1])
transforms.append(Lambda(lambda img: F.adjust_hue(img, hue_factor)))
random.shuffle(transforms)
transform = Compose(transforms)
return transform
def __call__(self, input1, input2):
transform = self.get_params(self.brightness, self.contrast,
self.saturation, self.hue)
output1 = transform(input1)
output2 = check_input_type_perform_action(input2, Image.Image, transform)
return output1, output2
class RandomAffine(object):
def __init__(self, degrees, translate=None, scale=None, shear=None, resample=False, fillcolor=0):
if isinstance(degrees, numbers.Number):
if degrees < 0:
raise ValueError("If degrees is a single number, it must be positive.")
self.degrees = (-degrees, degrees)
else:
assert isinstance(degrees, (tuple, list)) and len(degrees) == 2, \
"degrees should be a list or tuple and it must be of length 2."
self.degrees = degrees
if translate is not None:
assert isinstance(translate, (tuple, list)) and len(translate) == 2, \
"translate should be a list or tuple and it must be of length 2."
for t in translate:
if not (0.0 <= t <= 1.0):
raise ValueError("translation values should be between 0 and 1")
self.translate = translate
if scale is not None:
assert isinstance(scale, (tuple, list)) and len(scale) == 2, \
"scale should be a list or tuple and it must be of length 2."
for s in scale:
if s <= 0:
raise ValueError("scale values should be positive")
self.scale = scale
if shear is not None:
if isinstance(shear, numbers.Number):
if shear < 0:
raise ValueError("If shear is a single number, it must be positive.")
self.shear = (-shear, shear)
else:
assert isinstance(shear, (tuple, list)) and \
(len(shear) == 2 or len(shear) == 4), \
"shear should be a list or tuple and it must be of length 2 or 4."
# X-Axis shear with [min, max]
if len(shear) == 2:
self.shear = [shear[0], shear[1], 0., 0.]
elif len(shear) == 4:
self.shear = [s for s in shear]
else:
self.shear = shear
self.resample = resample
self.fillcolor = fillcolor
@staticmethod
def get_params(degrees, translate, scale_ranges, shears, img_size):
angle = random.uniform(degrees[0], degrees[1])
if translate is not None:
max_dx = translate[0] * img_size[0]
max_dy = translate[1] * img_size[1]
translations = (np.round(random.uniform(-max_dx, max_dx)),
np.round(random.uniform(-max_dy, max_dy)))
else:
translations = (0, 0)
if scale_ranges is not None:
scale = random.uniform(scale_ranges[0], scale_ranges[1])
else:
scale = 1.0
if shears is not None:
if len(shears) == 2:
shear = [random.uniform(shears[0], shears[1]), 0.]
elif len(shears) == 4:
shear = [random.uniform(shears[0], shears[1]),
random.uniform(shears[2], shears[3])]
else:
shear = 0.0
return angle, translations, scale, shear
def __call__(self, input1, input2):
params = self.get_params(self.degrees, self.translate, self.scale, self.shear, input1.size)
output1 = F.affine(input1, *params, resample=self.resample, fillcolor=self.fillcolor)
output2 = check_input_type_perform_action(input2, Image.Image, F.affine, *params, resample=self.resample, fillcolor=self.fillcolor)
return output1, output2
class RandomRotation(object):
def __init__(self, degrees, resample=False, expand=False, center=None, fill=None):
if isinstance(degrees, numbers.Number):
if degrees < 0:
raise ValueError("If degrees is a single number, it must be positive.")
self.degrees = (-degrees, degrees)
else:
if len(degrees) != 2:
raise ValueError("If degrees is a sequence, it must be of len 2.")
self.degrees = degrees
self.resample = resample
self.expand = expand
self.center = center
self.fill = fill
@staticmethod
def get_params(degrees):
angle = random.uniform(degrees[0], degrees[1])
return angle
def __call__(self, input1, input2):
angle = self.get_params(self.degrees)
output1 = F.rotate(input1, angle, self.resample, self.expand, self.center, self.fill)
output2 = check_input_type_perform_action(input2, Image.Image, F.rotate, angle, self.resample, self.expand, self.center, self.fill)
return output1, output2
class RandomBlur:
def __init__(self, blur_chance):
self.blur_chance = blur_chance
def get_params(self):
if self.blur_chance > random.random():
kernel = random.randint(3, 12)
while kernel % 2 != 1:
kernel = random.randint(3, 12)
else:
kernel = None
return kernel
def blur(self, image, kernel):
image = image.filter(ImageFilter.GaussianBlur(radius=kernel))
return image
def __call__(self, input1, input2):
kernel = self.get_params()
if kernel is None:
return input1, input2
else:
output1 = self.blur(input1, kernel)
output2 = check_input_type_perform_action(input2, Image.Image, self.blur, kernel)
return output1, output2
class NoiseTransform:
"""code is partly from http://www.xiaoliangbai.com/2016/09/09/more-on-image-noise-generation and edited by Oliver."""
def __init__(self, noise_type):
self.noise_type = noise_type
def get_params(self, image):
params = []
image_np = np.array(image)
row, col, ch = image_np.shape
if random.random() < 0.5:
return None
if self.noise_type == "gauss":
mean = 0.0
std = random.uniform(0.001, 0.3)
gauss = np.random.normal(mean, std, (row, col, ch))
gauss = gauss.reshape(row, col, ch)
params.append(gauss)
return params
elif self.noise_type == "s&p":
s_vs_p = 0.5
amount = random.uniform(0.001, 0.01)
# Generate Salt '1' noise
num_salt = np.ceil(amount * image_np.size * s_vs_p)
coords = [np.random.randint(0, i - 1, int(num_salt))
for i in image_np.shape]
coords[2] = np.random.randint(0, 3, int(num_salt))
params.append(copy.deepcopy(coords))
# Generate Pepper '0' noise
num_pepper = np.ceil(amount * image_np.size * (1. - s_vs_p))
coords = [np.random.randint(0, i - 1, int(num_pepper))
for i in image_np.shape]
params.append(copy.deepcopy(coords))
return params
elif self.noise_type == "poisson":
noisy = np.random.poisson(image_np)
params.append(noisy)
return params
elif self.noise_type == "speckle":
factor = random.uniform(0.01, 0.4)
gauss = np.random.randn(row, col, ch)
gauss = gauss.reshape(row, col, ch) * factor
params.append(gauss)
return params
elif self.noise_type == "band":
smaller_dim = min(col, row)
num_bands = random.randrange(smaller_dim // 2, smaller_dim)
scale = random.uniform(1.0, 10.0)
offset = np.zeros(image_np.shape).astype(np.float64)
# horizontal branding
num_list = list(range(image.width)) # list of integers from 0 to image width-1
# adjust this boundaries to fit your needs
random.shuffle(num_list)
horizontal_bands = num_list[:num_bands]
for w in horizontal_bands:
offset[w, :, :] += random.uniform(-1, 1) * scale
# vertical branding
num_list = list(range(image.height)) # list of integers from 0 to image height-1
# adjust this boundaries to fit your needs
random.shuffle(num_list)
vertical_bands = num_list[:num_bands]
for h in vertical_bands:
offset[:, h, :] += random.uniform(-1, 1) * scale
params.append(offset)
return params
else:
return params
def apply(self, image, params):
"""
image: ndarray (input image data. It will be converted to float)
"""
if params is None:
return image
image_np = np.array(image)
if self.noise_type == "gauss":
gauss = params[0]
noisy = image_np + image_np * gauss
noisy = np.clip(noisy, 0, 255)
return Image.fromarray(noisy.astype('uint8'))
elif self.noise_type == "s&p":
out = image_np
# Generate Salt '1' noise
coords = params[0]
out[tuple(coords)] = 255
# Generate Pepper '0' noise
coords = params[1]
out[tuple(coords)] = 0
out = np.clip(out, 0, 255)
return Image.fromarray(out.astype('uint8'))
elif self.noise_type == "poisson":
noisy = params[0]
noisy = np.clip(noisy, 0, 255)
return Image.fromarray(noisy.astype('uint8'))
elif self.noise_type == "speckle":
gauss = params[0]
noisy = image_np + image_np * gauss
noisy = np.clip(noisy, 0, 255)
return Image.fromarray(noisy.astype('uint8'))
elif self.noise_type == "band":
offset = params[0]
noisy = image_np + offset
noisy = np.clip(noisy, 0, 255)
return Image.fromarray(noisy.astype('uint8'))
else:
return image
def __call__(self, input1, input2):
params = self.get_params(input1)
output1 = self.apply(input1, params)
output2 = check_input_type_perform_action(input2, Image.Image, self.apply, params)
return output1, output2
class MakePower2:
def __init__(self, base, method=Image.BICUBIC):
self.base = base
self.method = method
self.print_size_warning = PrintSizeWarning()
def apply(self, img):
ow, oh = img.size
h = int(round(oh / self.base) * self.base)
w = int(round(ow / self.base) * self.base)
if h == oh and w == ow:
return img
self.print_size_warning(ow, oh, w, h)
return img.resize((w, h), self.method)
def __call__(self, input1, input2):
output1 = self.apply(input1)
output2 = check_input_type_perform_action(input2, Image.Image, self.apply)
return output1, output2
class RandomHorizontalFlip(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 get_params(self):
if random.random() < self.p:
return True
else:
return False
def __call__(self, input1, input2):
flip = self.get_params()
if flip:
output1 = F.hflip(input1)
output2 = check_input_type_perform_action(input2, Image.Image, F.hflip)
else:
output1, output2 = input1, input2
return output1, output2
class Flip:
def __init__(self, flip):
self.flip = flip
def transpose(self, input):
return input.transpose(Image.FLIP_LEFT_RIGHT)
def __call__(self, input1, input2):
if self.flip:
output1 = input1.transpose(Image.FLIP_LEFT_RIGHT)
output2 = check_input_type_perform_action(input2, Image.Image, self.transpose)
else:
output1, output2 = input1, input2
return output1, output2
class ToTensor(object):
"""Convert a ``PIL Image`` or ``numpy.ndarray`` to tensor.
Converts a PIL Image or numpy.ndarray (H x W x C) in the range
[0, 255] to a torch.FloatTensor of shape (C x H x W) in the range [0.0, 1.0]
if the PIL Image belongs to one of the modes (L, LA, P, I, F, RGB, YCbCr, RGBA, CMYK, 1)
or if the numpy.ndarray has dtype = np.uint8
In the other cases, tensors are returned without scaling.
"""
def __call__(self, input1, input2):
output1 = F.to_tensor(input1)
output2 = check_input_type_perform_action(input2, None, F.to_tensor)
return output1, output2
class Normalize(object):
"""Normalize a tensor image with mean and standard deviation.
Given mean: ``(M1,...,Mn)`` and std: ``(S1,..,Sn)`` for ``n`` channels, this transform
will normalize each channel of the input ``torch.*Tensor`` i.e.
``output[channel] = (input[channel] - mean[channel]) / std[channel]``
.. note::
This transform acts out of place, i.e., it does not mutate the input tensor.
Args:
mean (sequence): Sequence of means for each channel.
std (sequence): Sequence of standard deviations for each channel.
inplace(bool,optional): Bool to make this operation in-place.
"""
def __init__(self, first_input_mean, first_input_std, second_input_mean=None, second_input_std=None, inplace=False):
self.first_input_mean = first_input_mean
self.first_input_std = first_input_std
self.second_input_mean = second_input_mean if second_input_mean is not None else first_input_mean
self.second_input_std = second_input_std if second_input_std is not None else first_input_std
self.inplace = inplace
def __call__(self, tensor1, tensor2):
"""
Args:
tensor (Tensor): Tensor image of size (C, H, W) to be normalized.
Returns:
Tensor: Normalized Tensor image.
"""
output1 = F.normalize(tensor1, self.first_input_mean, self.first_input_std, self.inplace)
output2 = check_input_type_perform_action(tensor2, None, F.normalize, self.second_input_mean, self.second_input_std, self.inplace)
return output1, output2
class PrintSizeWarning:
def __init__(self):
self.has_printed = False
def __call__(self, ow, oh, w, h):
if not self.has_printed:
print("The image size needs to be a multiple of 4. "
"The loaded image size was (%d, %d), so it was adjusted to "
"(%d, %d). This adjustment will be done to all images "
"whose sizes are not multiples of 4" % (ow, oh, w, h))
self.has_printed = True
class ImagePathToImage:
"""Convert an image path to an image.
Parameters:
filename -- the input file path.
"""
def load_img(self, path):
return Image.open(path).convert('RGB')
def __call__(self, filename1, filename2):
img1 = self.load_img(filename1)
img2 = check_input_type_perform_action(filename2, None, self.load_img)
return img1, img2
class NumpyToTensor:
"""Convert a numpy array to a tensor.
Parameters:
filename -- the input file path.
"""
def load_numpy(self, filename):
npy = np.load(filename)
if isinstance(npy, np.lib.npyio.NpzFile):
npy = npy['data']
if len(npy.shape) == 2:
npy = np.tile(npy, (1, 1, 1))
else:
npy = np.transpose(npy, (2, 0, 1))
return torch.from_numpy(npy).float()
def __call__(self, filename1, filename2):
tensor1 = self.load_numpy(filename1)
tensor2 = check_input_type_perform_action(filename2, None, self.load_numpy)
return tensor1, tensor2
|