parent
8891752f07
commit
dd29d864b4
@ -0,0 +1,52 @@
|
||||
import random
|
||||
|
||||
import numpy as np
|
||||
from skimage.filters import gaussian
|
||||
import torch
|
||||
from PIL import Image, ImageFilter
|
||||
|
||||
|
||||
class RandomVerticalFlip(object):
|
||||
def __call__(self, img):
|
||||
if random.random() < 0.5:
|
||||
return img.transpose(Image.FLIP_TOP_BOTTOM)
|
||||
return img
|
||||
|
||||
|
||||
class DeNormalize(object):
|
||||
def __init__(self, mean, std):
|
||||
self.mean = mean
|
||||
self.std = std
|
||||
|
||||
def __call__(self, tensor):
|
||||
for t, m, s in zip(tensor, self.mean, self.std):
|
||||
t.mul_(s).add_(m)
|
||||
return tensor
|
||||
|
||||
|
||||
class MaskToTensor(object):
|
||||
def __call__(self, img):
|
||||
return torch.from_numpy(np.array(img, dtype=np.int32)).long()
|
||||
|
||||
|
||||
class FreeScale(object):
|
||||
def __init__(self, size, interpolation=Image.BILINEAR):
|
||||
self.size = tuple(reversed(size)) # size: (h, w)
|
||||
self.interpolation = interpolation
|
||||
|
||||
def __call__(self, img):
|
||||
return img.resize(self.size, self.interpolation)
|
||||
|
||||
|
||||
class FlipChannels(object):
|
||||
def __call__(self, img):
|
||||
img = np.array(img)[:, :, ::-1]
|
||||
return Image.fromarray(img.astype(np.uint8))
|
||||
|
||||
|
||||
class RandomGaussianBlur(object):
|
||||
def __call__(self, img):
|
||||
sigma = 0.15 + random.random() * 1.15
|
||||
blurred_img = gaussian(np.array(img), sigma=sigma, multichannel=True)
|
||||
blurred_img *= 255
|
||||
return Image.fromarray(blurred_img.astype(np.uint8))
|
||||
Loading…
Reference in new issue