Skip to content

Instantly share code, notes, and snippets.

@Mukosame
Last active January 4, 2023 08:36
Show Gist options
  • Save Mukosame/a0105679108c74efeeb3188a0627dc0d to your computer and use it in GitHub Desktop.
Save Mukosame/a0105679108c74efeeb3188a0627dc0d to your computer and use it in GitHub Desktop.
## This script contains following image augementation methods:
# Random Horizontal Flip
# Random Crop
# Random Sized Crop
# Random Rotation
# Gaussian Noise
# Random Grayscale
# Random Lightning
# Random Contrast
# Multiply
# Add Hue and Saturation
# Add
# Contrast Normalization
# Contrast Normalization per Channel
# Gaussian blur
# Motion blur
# Geometric Transformation
# JPEG compression
# Dropout
# Block Dropout
import torchvision
import random
from imgaug import augmenters as iaa
import imgaug as ia
import numpy as np
import io
from PIL import Image
### They will be achieved by the code below:
# Random Horizontal Flip
RandomHorizontalFlip = torchvision.transforms.RandomHorizontalFlip()
# Random Crop
RandomCrop = torchvision.transforms.RandomCrop(224)
# Random Sized Crop
RandomSizedCrop = torchvision.transforms.RandomSizedCrop(224)
# Random Rotation
# rotation range: (-20, 20)
RandomRotation = torchvision.transforms.RandomRotation(20, resample=PIL.Image.BILINEAR)
# Gaussian Noise
GaussianNoise = iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.05*255), per_channel=0.5)
# Random Grayscale
RandomGrayscale = torchvision.transforms.RandomGrayscale(p=0.1)
# Random Lighting
RandomBrightness = torchvision.transforms.ColorJitter(brightness = 0.3)
# Random Contrast
RandomContrast = torchvision.transforms.ColorJitter(contrast = 0.3)
# Multiply
Multiply = iaa.Multiply((0.7, 1.3), per_channel=0.2)
# Add Hue and Saturation
RandomHueSat = torchvision.transforms.ColorJitter(hue=0.3, saturation=0.3)
# Add
Add = iaa.Add((-10, 10), per_channel=0.5)
# Contrast Normalization
ContrastNorm = iaa.ContrastNormalization((0.5, 2.0), per_channel=0.5)
# Gaussian blur
GaussianBlur = iaa.GaussianBlur(sigma=(0, 3.0))
# Motion blur
class MotionBlur():
def __init__(self, size):
self.size = size
self.kernel_motion_blur = np.zeros((self.size, self.size))
self.kernel_motion_blur[int((self.size-1)/2), :] = np.ones(self.size)
self.kernel_motion_blur = self.kernel_motion_blur / self.size
def _augment_images(self, images, random_state, parents, hooks):
output = []
nb_images = len(images)
offset_xs, offset_ys = self._draw_samples(nb_images, random_state)
for i in sm.xrange(nb_images):
image = images[i]
result = cv2.filter2D(image, -1, self.kernel_motion_blur)
output.append(result)
return output
# Geometric Transformation
RandomAffine = torchvision.transforms.RandomAffine(degrees=10, scale=(0.8,1.1), translate=(0, 0.1), shear=10)
# JPEG compression
class JPEGCompression():
def __init__(self, quality_num):
self.quality_num = quality_num
def _augment_images(self, images, random_state, parents, hooks):
output = []
temp = io.BytesIO()
nb_images = len(images)
offset_xs, offset_ys = self._draw_samples(nb_images, random_state)
for i in sm.xrange(nb_images):
image = images[i]
image.save(temp, format='JPFG', quality=quality_num)
result = Image.open(temp)
output.append(result)
return output
# Dropout
Dropout = iaa.Dropout((0.01, 0.1), per_channel=0.5)
# BlockDropout
BlockDropout = iaa.CoarseDropout(
(0.03, 0.15), size_percent=(0.02, 0.05),
per_channel=0.2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment