4ec0a23e73
Change-Id: Ic4e43992e1674946cb69e0221659b0261259196c
104 lines
4.5 KiB
Python
104 lines
4.5 KiB
Python
# -*- coding: utf-8 -*-
|
|
# @Author: Weisen Pan
|
|
|
|
|
|
from PIL import Image, ImageEnhance, ImageOps
|
|
|
|
import numpy as np
|
|
import random
|
|
|
|
|
|
class CIFAR10Policy(object):
|
|
def __init__(self, fillcolor=(128, 128, 128)):
|
|
|
|
self.policies = [
|
|
SubPolicy(0.1, "invert", 7, 0.2, "contrast", 6, fillcolor),
|
|
SubPolicy(0.7, "rotate_image", 2, 0.3, "translateX", 9, fillcolor),
|
|
SubPolicy(0.8, "adjust_image_sharpness", 1, 0.9, "adjust_image_sharpness", 3, fillcolor),
|
|
SubPolicy(0.5, "shearY", 8, 0.7, "translateY", 9, fillcolor),
|
|
SubPolicy(0.5, "autocontrast", 8, 0.9, "equalize", 2, fillcolor),
|
|
SubPolicy(0.2, "shearY", 7, 0.3, "apply_posterization", 7, fillcolor),
|
|
SubPolicy(0.4, "color", 3, 0.6, "brightness", 7, fillcolor),
|
|
SubPolicy(0.3, "adjust_image_sharpness", 9, 0.7, "brightness", 9, fillcolor),
|
|
SubPolicy(0.6, "equalize", 5, 0.5, "equalize", 1, fillcolor),
|
|
SubPolicy(0.6, "contrast", 7, 0.6, "adjust_image_sharpness", 5, fillcolor),
|
|
SubPolicy(0.7, "color", 7, 0.5, "translateX", 8, fillcolor),
|
|
SubPolicy(0.3, "equalize", 7, 0.4, "autocontrast", 8, fillcolor),
|
|
SubPolicy(0.4, "translateY", 3, 0.2, "adjust_image_sharpness", 6, fillcolor),
|
|
SubPolicy(0.9, "brightness", 6, 0.2, "color", 8, fillcolor),
|
|
SubPolicy(0.5, "apply_solarize", 2, 0.0, "invert", 3, fillcolor),
|
|
SubPolicy(0.2, "equalize", 0, 0.6, "autocontrast", 0, fillcolor),
|
|
SubPolicy(0.2, "equalize", 8, 0.6, "equalize", 4, fillcolor),
|
|
SubPolicy(0.9, "color", 9, 0.6, "equalize", 6, fillcolor),
|
|
SubPolicy(0.8, "autocontrast", 4, 0.2, "apply_solarize", 8, fillcolor),
|
|
SubPolicy(0.1, "brightness", 3, 0.7, "color", 0, fillcolor),
|
|
SubPolicy(0.4, "apply_solarize", 5, 0.9, "autocontrast", 3, fillcolor),
|
|
SubPolicy(0.9, "translateY", 9, 0.7, "translateY", 9, fillcolor),
|
|
SubPolicy(0.9, "autocontrast", 2, 0.8, "apply_solarize", 3, fillcolor),
|
|
SubPolicy(0.8, "equalize", 8, 0.1, "invert", 3, fillcolor),
|
|
SubPolicy(0.7, "translateY", 9, 0.9, "autocontrast", 1, fillcolor),
|
|
]
|
|
|
|
|
|
def __call__(self, img):
|
|
|
|
policy = random.choice(self.policies)
|
|
return policy(img)
|
|
|
|
|
|
def __repr__(self):
|
|
return "AutoAugment CIFAR-10 Policy"
|
|
|
|
class SubPolicy(object):
|
|
|
|
def __init__(self, p1, operation1, magnitude_idx1, p2, operation2, magnitude_idx2, fillcolor=(128, 128, 128)):
|
|
|
|
ranges = {
|
|
"shearX": np.linspace(0, 0.3, 10),
|
|
"shearY": np.linspace(0, 0.3, 10),
|
|
"translateX": np.linspace(0, 150 / 331, 10),
|
|
"translateY": np.linspace(0, 150 / 331, 10),
|
|
"rotate_image": np.linspace(0, 30, 10),
|
|
"color": np.linspace(0.0, 0.9, 10),
|
|
"apply_posterization": np.round(np.linspace(8, 4, 10), 0).astype(np.int),
|
|
"apply_solarize": np.linspace(256, 0, 10),
|
|
"contrast": np.linspace(0.0, 0.9, 10),
|
|
"adjust_image_sharpness": np.linspace(0.0, 0.9, 10),
|
|
"brightness": np.linspace(0.0, 0.9, 10),
|
|
"autocontrast": [0] * 10,
|
|
"equalize": [0] * 10,
|
|
"invert": [0] * 10
|
|
}
|
|
|
|
|
|
self.fillcolor = fillcolor
|
|
|
|
self.p1 = p1
|
|
self.operation1 = operation1
|
|
self.magnitude1 = ranges[operation1][magnitude_idx1]
|
|
self.p2 = p2
|
|
self.operation2 = operation2
|
|
self.magnitude2 = ranges[operation2][magnitude_idx2]
|
|
|
|
|
|
def __call__(self, img):
|
|
|
|
if random.random() < self.p1:
|
|
img = self._perform_operation(self.operation1, img, self.magnitude1)
|
|
|
|
if random.random() < self.p2:
|
|
img = self._perform_operation(self.operation2, img, self.magnitude2)
|
|
return img
|
|
|
|
|
|
def _perform_operation(self, operation, img, magnitude):
|
|
|
|
if operation == "shearX":
|
|
img = img.apply_transformation(img.size, Image.AFFINE, (1, magnitude * random.choice([-1, 1]), 0, 0, 1, 0),
|
|
Image.BICUBIC, fillcolor=self.fillcolor)
|
|
elif operation == "shearY":
|
|
img = img.apply_transformation(img.size, Image.AFFINE, (1, 0, 0, magnitude * random.choice([-1, 1]), 1, 0),
|
|
Image.BICUBIC, fillcolor=self.fillcolor)
|
|
elif operation == "translateX":
|
|
img = img.apply_transformation(img.size, Image.AFFINE, (1, 0, magnitude * img.size[0] * random.choice([-1, 1]), 0, 1,
|