diff --git a/dan/transforms.py b/dan/transforms.py index 957a6453635df4043082539c0115d943d16f9ae1..c3ddac4e9d212264914b64a0e6da3b0c8f30c419 100644 --- a/dan/transforms.py +++ b/dan/transforms.py @@ -8,7 +8,7 @@ import cv2 import numpy as np from cv2 import dilate, erode, normalize from numpy import random -from PIL import Image, ImageOps +from PIL import Image from torchvision.transforms import ( ColorJitter, GaussianBlur, @@ -20,24 +20,12 @@ from torchvision.transforms.functional import InterpolationMode from dan.utils import rand, rand_uniform, randint -class SignFlipping: - """ - Color inversion - """ - - def __init__(self): - pass - - def __call__(self, x): - return ImageOps.invert(x) - - class DPIAdjusting: """ Resolution modification """ - def __init__(self, factor, preserve_ratio): + def __init__(self, factor): self.factor = factor def __call__(self, x): @@ -179,31 +167,6 @@ class ElasticDistortion: return Image.fromarray(dst.astype(np.uint8)) -class Tightening: - """ - Reduce interline spacing - """ - - def __init__(self, color=255, remove_proba=0.75): - self.color = color - self.remove_proba = remove_proba - - def __call__(self, x): - x_np = np.array(x) - interline_indices = [np.all(line == 255) for line in x_np] - indices_to_removed = np.logical_and( - np.random.choice( - [True, False], - size=len(x_np), - replace=True, - p=[self.remove_proba, 1 - self.remove_proba], - ), - interline_indices, - ) - new_x = x_np[np.logical_not(indices_to_removed)] - return Image.fromarray(new_x.astype(np.uint8)) - - def get_list_augmenters(img, aug_configs, fill_value): """ Randomly select a list of data augmentation techniques to used based on aug_configs @@ -236,9 +199,7 @@ def get_list_augmenters(img, aug_configs, fill_value): and factor * img.size[1] < aug_config["min_height"] ) ) - augmenters.append( - DPIAdjusting(factor, preserve_ratio=aug_config["preserve_ratio"]) - ) + augmenters.append(DPIAdjusting(factor)) elif aug_config["type"] == "zoom_ratio": ratio_h = rand_uniform(aug_config["min_ratio_h"], aug_config["max_ratio_h"]) @@ -351,94 +312,6 @@ def apply_data_augmentation(img, da_config): return img -def apply_transform(img, transform): - """ - Apply data augmentation technique on input image - """ - img = img[:, :, 0] if img.shape[2] == 1 else img - img = Image.fromarray(img) - img = transform(img) - img = np.array(img) - return np.expand_dims(img, axis=2) if len(img.shape) == 2 else img - - -def line_aug_config(proba_use_da, p): - return { - "order": "random", - "proba": proba_use_da, - "augmentations": [ - { - "type": "dpi", - "proba": p, - "min_factor": 0.5, - "max_factor": 1.5, - "preserve_ratio": True, - }, - { - "type": "perspective", - "proba": p, - "min_factor": 0, - "max_factor": 0.4, - }, - { - "type": "elastic_distortion", - "proba": p, - "min_alpha": 0.5, - "max_alpha": 1, - "min_sigma": 1, - "max_sigma": 10, - "min_kernel_size": 3, - "max_kernel_size": 9, - }, - { - "type": "dilation_erosion", - "proba": p, - "min_kernel": 1, - "max_kernel": 3, - "iterations": 1, - }, - { - "type": "color_jittering", - "proba": p, - "factor_hue": 0.2, - "factor_brightness": 0.4, - "factor_contrast": 0.4, - "factor_saturation": 0.4, - }, - { - "type": "gaussian_blur", - "proba": p, - "min_kernel": 3, - "max_kernel": 5, - "min_sigma": 3, - "max_sigma": 5, - }, - { - "type": "gaussian_noise", - "proba": p, - "std": 0.5, - }, - { - "type": "sharpen", - "proba": p, - "min_alpha": 0, - "max_alpha": 1, - "min_strength": 0, - "max_strength": 1, - }, - { - "type": "zoom_ratio", - "proba": p, - "min_ratio_h": 0.8, - "max_ratio_h": 1, - "min_ratio_w": 0.99, - "max_ratio_w": 1, - "keep_dim": True, - }, - ], - } - - def aug_config(proba_use_da, p): return { "order": "random", @@ -449,7 +322,6 @@ def aug_config(proba_use_da, p): "proba": p, "min_factor": 0.75, "max_factor": 1, - "preserve_ratio": True, }, { "type": "perspective",