4@contact: sherlockliao01@gmail.com
9from PIL
import Image, ImageOps, ImageEnhance
13 """Convert a ``PIL Image`` or ``numpy.ndarray`` to tensor.
15 See ``ToTensor`` for more details.
18 pic (PIL Image or numpy.ndarray): Image to be converted to tensor.
21 Tensor: Converted image.
23 if isinstance(pic, np.ndarray):
24 assert len(pic.shape)
in (2, 3)
29 img = torch.from_numpy(pic.transpose((2, 0, 1)))
31 if isinstance(img, torch.ByteTensor):
38 img = torch.from_numpy(np.array(pic, np.int32, copy=
False))
39 elif pic.mode ==
'I;16':
40 img = torch.from_numpy(np.array(pic, np.int16, copy=
False))
42 img = torch.from_numpy(np.array(pic, np.float32, copy=
False))
44 img = 255 * torch.from_numpy(np.array(pic, np.uint8, copy=
False))
46 img = torch.ByteTensor(torch.ByteStorage.from_buffer(pic.tobytes()))
48 if pic.mode ==
'YCbCr':
50 elif pic.mode ==
'I;16':
53 nchannel = len(pic.mode)
54 img = img.view(pic.size[1], pic.size[0], nchannel)
57 img = img.transpose(0, 1).transpose(0, 2).contiguous()
58 if isinstance(img, torch.ByteTensor):
65 """Helper function to scale `val` between 0 and maxval .
67 level: Level of the operation that will be between [0, `PARAMETER_MAX`].
68 maxval: Maximum value that the operation can have. This will be scaled to
71 An int that results from scaling `maxval` according to `level`.
73 return int(level * maxval / 10)
77 """Helper function to scale `val` between 0 and maxval.
79 level: Level of the operation that will be between [0, `PARAMETER_MAX`].
80 maxval: Maximum value that the operation can have. This will be scaled to
83 A float that results from scaling `maxval` according to `level`.
85 return float(level) * maxval / 10.
89 return np.random.uniform(low=0.1, high=n)
93 return ImageOps.autocontrast(pil_img)
97 return ImageOps.equalize(pil_img)
102 return ImageOps.posterize(pil_img, 4 - level)
107 if np.random.uniform() > 0.5:
109 return pil_img.rotate(degrees, resample=Image.BILINEAR)
114 return ImageOps.solarize(pil_img, 256 - level)
119 if np.random.uniform() > 0.5:
121 return pil_img.transform(image_size,
122 Image.AFFINE, (1, level, 0, 0, 1, 0),
123 resample=Image.BILINEAR)
128 if np.random.uniform() > 0.5:
130 return pil_img.transform(image_size,
131 Image.AFFINE, (1, 0, 0, level, 1, 0),
132 resample=Image.BILINEAR)
137 if np.random.random() > 0.5:
139 return pil_img.transform(image_size,
140 Image.AFFINE, (1, 0, level, 0, 1, 0),
141 resample=Image.BILINEAR)
146 if np.random.random() > 0.5:
148 return pil_img.transform(image_size,
149 Image.AFFINE, (1, 0, 0, 0, 1, level),
150 resample=Image.BILINEAR)
156 return ImageEnhance.Color(pil_img).enhance(level)
162 return ImageEnhance.Contrast(pil_img).enhance(level)
168 return ImageEnhance.Brightness(pil_img).enhance(level)
174 return ImageEnhance.Sharpness(pil_img).enhance(level)
177augmentations_reid = [
178 autocontrast, equalize, posterize, shear_x, shear_y,
179 color, contrast, brightness, sharpness
183 autocontrast, equalize, posterize, rotate, solarize, shear_x, shear_y,
184 translate_x, translate_y
188 autocontrast, equalize, posterize, rotate, solarize, shear_x, shear_y,
189 translate_x, translate_y, color, contrast, brightness, sharpness