This repository has been archived by the owner on May 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 149
/
transforms_v2.py
199 lines (171 loc) · 6.19 KB
/
transforms_v2.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
# Copyright (c) Facebook, Inc. and its affiliates.
# All rights reserved.
#
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.
#
import torch
import torchvision.transforms.functional as F
from torchvision import transforms
import torch
import math
import sys
import random
from PIL import Image
try:
import accimage
except ImportError:
accimage = None
import numbers
import types
import collections
import warnings
import numpy as np
try:
import accimage
except ImportError:
accimage = None
def _is_pil_image(img):
if accimage is not None:
return isinstance(img, (Image.Image, accimage.Image))
else:
return isinstance(img, Image.Image)
def crop(img, i, j, h, w):
"""Crop the given PIL Image.
Args:
img (PIL Image): Image to be cropped.
i (int): i in (i,j) i.e coordinates of the upper left corner.
j (int): j in (i,j) i.e coordinates of the upper left corner.
h (int): Height of the cropped image.
w (int): Width of the cropped image.
Returns:
PIL Image: Cropped image.
"""
if not _is_pil_image(img):
raise TypeError('img should be PIL Image. Got {}'.format(type(img)))
return img.crop((j, i, j + w, i + h))
def center_crop_new(img, output_size):
if isinstance(output_size, numbers.Number):
output_size = (int(output_size), int(output_size))
w, h = img.size
th, tw = output_size
i = int(round((h - th) / 2.))
j = int(round((w - tw) / 2.))
jit=0
if j > 0:
jit=np.random.randint(int(j+1))
val=np.random.randint(2)
scale=(1.0)*(val==0)+(-1.0)*(val==1)
return crop(img, i, int(j+scale*jit), th, tw)
class CenterCrop(object):
"""Crops the given PIL Image at the center.
Args:
size (sequence or int): Desired output size of the crop. If size is an
int instead of sequence like (h, w), a square crop (size, size) is
made.
"""
def __init__(self, size):
if isinstance(size, numbers.Number):
self.size = (int(size), int(size))
else:
self.size = size
def __call__(self, img):
"""
Args:
img (PIL Image): Image to be cropped.
Returns:
PIL Image: Cropped image.
"""
return center_crop_new(img, self.size)
def __repr__(self):
return self.__class__.__name__ + '(size={0})'.format(self.size)
class Resize(transforms.Resize):
"""
Resize with a ``largest=False'' argument
allowing to resize to a common largest side without cropping
"""
def __init__(self, size, largest=False, **kwargs):
super().__init__(size, **kwargs)
self.largest = largest
@staticmethod
def target_size(w, h, size, largest=False):
if h < w and largest:
w, h = size, int(size * h / w)
else:
w, h = int(size * w / h), size
size = (h, w)
return size
def __call__(self, img):
size = self.size
w, h = img.size
target_size = self.target_size(w, h, size, self.largest)
return F.resize(img, target_size, self.interpolation)
def __repr__(self):
r = super().__repr__()
return r[:-1] + ', largest={})'.format(self.largest)
class Lighting(object):
"""
PCA jitter transform on tensors
"""
def __init__(self, alpha_std, eig_val, eig_vec):
self.alpha_std = alpha_std
self.eig_val = torch.as_tensor(eig_val, dtype=torch.float).view(1, 3)
self.eig_vec = torch.as_tensor(eig_vec, dtype=torch.float)
def __call__(self, data):
if self.alpha_std == 0:
return data
alpha = torch.empty(1, 3).normal_(0, self.alpha_std)
rgb = ((self.eig_vec * alpha) * self.eig_val).sum(1)
data += rgb.view(3, 1, 1)
data /= 1. + self.alpha_std
return data
class Bound(object):
def __init__(self, lower=0., upper=1.):
self.lower = lower
self.upper = upper
def __call__(self, data):
return data.clamp_(self.lower, self.upper)
def get_transforms(input_size=224,test_size=224, kind='full', crop=True, need=('train', 'val'), backbone=None):
mean, std = [0.485, 0.456, 0.406], [0.229, 0.224, 0.225]
if backbone is not None and backbone in ['pnasnet5large', 'nasnetamobile']:
mean, std = [0.5, 0.5, 0.5], [0.5, 0.5, 0.5]
transformations = {}
if 'train' in need:
if kind == 'torch':
transformations['train'] = transforms.Compose([
transforms.RandomResizedCrop(input_size),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize(mean, std),
])
elif kind == 'full':
transformations['train'] = transforms.Compose([
transforms.RandomResizedCrop(input_size),
transforms.RandomHorizontalFlip(),
transforms.ColorJitter(0.3, 0.3, 0.3),
transforms.ToTensor(),
transforms.Normalize(mean, std),
])
else:
raise ValueError('Transforms kind {} unknown'.format(kind))
if 'val' in need:
if crop:
transformations['val_test'] = transforms.Compose(
[Resize(int((256 / 224) * test_size)), # to maintain same ratio w.r.t. 224 images
transforms.CenterCrop(test_size),
transforms.ToTensor(),
transforms.Normalize(mean, std)])
transformations['val_train'] = transforms.Compose(
[Resize(int((256 / 224) * test_size)), # to maintain same ratio w.r.t. 224 images
transforms.RandomHorizontalFlip(),
transforms.ColorJitter(0.05, 0.05, 0.05),
CenterCrop(test_size),
transforms.ToTensor(),
transforms.Normalize(mean, std)])
else:
transformations['val'] = transforms.Compose(
[Resize(test_size, largest=True),
transforms.ToTensor(),
transforms.Normalize(mean, std)])
return transformations
transforms_list = ['torch', 'full']