|
| 1 | +import torch |
| 2 | +import math |
| 3 | +import random |
| 4 | +from PIL import Image |
| 5 | + |
| 6 | + |
| 7 | +class Compose(object): |
| 8 | + def __init__(self, transforms): |
| 9 | + self.transforms = transforms |
| 10 | + |
| 11 | + def __call__(self, img): |
| 12 | + for t in self.transforms: |
| 13 | + img = t(img) |
| 14 | + return img |
| 15 | + |
| 16 | + |
| 17 | +class ToTensor(object): |
| 18 | + def __call__(self, pic): |
| 19 | + img = torch.ByteTensor(torch.ByteStorage.from_buffer(pic.tobytes())) |
| 20 | + img = img.view(pic.size[0], pic.size[1], 3) |
| 21 | + # put it in CHW format |
| 22 | + # yikes, this transpose takes 80% of the loading time/CPU |
| 23 | + img = img.transpose(0, 2).transpose(1, 2).contiguous() |
| 24 | + return img.float() |
| 25 | + |
| 26 | +class Normalize(object): |
| 27 | + def __init__(self, mean, std): |
| 28 | + self.mean = mean |
| 29 | + self.std = std |
| 30 | + |
| 31 | + def __call__(self, tensor): |
| 32 | + for t, m, s in zip(tensor, self.mean, self.std): |
| 33 | + t.sub_(m).div_(s) |
| 34 | + return tensor |
| 35 | + |
| 36 | + |
| 37 | +class Scale(object): |
| 38 | + "Scales the smaller edge to size" |
| 39 | + def __init__(self, size, interpolation=Image.BILINEAR): |
| 40 | + self.size = size |
| 41 | + self.interpolation = interpolation |
| 42 | + |
| 43 | + def __call__(self, img): |
| 44 | + w, h = img.size |
| 45 | + if (w <= h and w == self.size) or (h <= w and h == self.size): |
| 46 | + return img |
| 47 | + if w < h: |
| 48 | + return img.resize((w, int(round(h / w * self.size))), self.interpolation) |
| 49 | + else: |
| 50 | + return img.resize((int(round(w / h * self.size)), h), self.interpolation) |
| 51 | + |
| 52 | + |
| 53 | +class CenterCrop(object): |
| 54 | + "Crop to centered rectangle" |
| 55 | + def __init__(self, size): |
| 56 | + self.size = size |
| 57 | + |
| 58 | + def __call__(self, img): |
| 59 | + w, h = img.size |
| 60 | + x1 = int(round((w - self.size) / 2)) |
| 61 | + y1 = int(round((h - self.size) / 2)) |
| 62 | + return img.crop((x1, y1, x1 + self.size, y1 + self.size)) |
| 63 | + |
| 64 | + |
| 65 | +class RandomCrop(object): |
| 66 | + "Random crop form larger image with optional zero padding" |
| 67 | + def __init__(self, size, padding=0): |
| 68 | + self.size = size |
| 69 | + self.padding = padding |
| 70 | + |
| 71 | + def __call__(self, img): |
| 72 | + if self.padding > 0: |
| 73 | + raise NotImplementedError() |
| 74 | + |
| 75 | + w, h = img.size |
| 76 | + if w == self.size and h == self.size: |
| 77 | + return img |
| 78 | + |
| 79 | + x1 = random.randint(0, w - self.size) |
| 80 | + y1 = random.randint(0, h - self.size) |
| 81 | + return img.crop((x1, y1, x1 + self.size, y1 + self.size)) |
| 82 | + |
| 83 | + |
| 84 | +class RandomHorizontalFlip(object): |
| 85 | + "Horizontal flip with 0.5 probability" |
| 86 | + def __call__(self, img): |
| 87 | + if random.random() < 0.5: |
| 88 | + return img.transpose(Image.FLIP_LEFT_RIGHT) |
| 89 | + return img |
| 90 | + |
| 91 | + |
| 92 | +class RandomSizedCrop(object): |
| 93 | + "Random crop with size 0.08-1 and aspect ratio 3/4 - 4/3 (Inception-style)" |
| 94 | + def __init__(self, size, interpolation=Image.BILINEAR): |
| 95 | + self.size = size |
| 96 | + self.interpolation = interpolation |
| 97 | + |
| 98 | + def __call__(self, img): |
| 99 | + for attempt in range(10): |
| 100 | + area = img.size[0] * img.size[1] |
| 101 | + target_area = random.uniform(0.08, 1.0) * area |
| 102 | + aspect_ratio = random.uniform(3 / 4, 4 / 3) |
| 103 | + |
| 104 | + w = int(round(math.sqrt(target_area * aspect_ratio))) |
| 105 | + h = int(round(math.sqrt(target_area / aspect_ratio))) |
| 106 | + |
| 107 | + if random.random() < 0.5: |
| 108 | + w, h = h, w |
| 109 | + |
| 110 | + if w <= img.size[0] and h <= img.size[1]: |
| 111 | + x1 = random.randint(0, img.size[0] - w) |
| 112 | + y1 = random.randint(0, img.size[1] - h) |
| 113 | + |
| 114 | + img = img.crop((x1, y1, x1 + w, y1 + h)) |
| 115 | + assert(img.size == (w, h)) |
| 116 | + |
| 117 | + return img.resize((self.size, self.size), self.interpolation) |
| 118 | + |
| 119 | + # Fallback |
| 120 | + scale = Scale(self.size, interpolation=self.interpolation) |
| 121 | + crop = CenterCrop(self.size) |
| 122 | + return crop(scale(img)) |
0 commit comments