Skip to content
Snippets Groups Projects
Commit e2a98707 authored by Mélodie Boillet's avatar Mélodie Boillet Committed by Yoann Schneider
Browse files

Remove unused transforms

parent 2fa7905c
No related branches found
No related tags found
1 merge request!152Remove unused transforms
...@@ -8,7 +8,7 @@ import cv2 ...@@ -8,7 +8,7 @@ import cv2
import numpy as np import numpy as np
from cv2 import dilate, erode, normalize from cv2 import dilate, erode, normalize
from numpy import random from numpy import random
from PIL import Image, ImageOps from PIL import Image
from torchvision.transforms import ( from torchvision.transforms import (
ColorJitter, ColorJitter,
GaussianBlur, GaussianBlur,
...@@ -20,24 +20,12 @@ from torchvision.transforms.functional import InterpolationMode ...@@ -20,24 +20,12 @@ from torchvision.transforms.functional import InterpolationMode
from dan.utils import rand, rand_uniform, randint 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: class DPIAdjusting:
""" """
Resolution modification Resolution modification
""" """
def __init__(self, factor, preserve_ratio): def __init__(self, factor):
self.factor = factor self.factor = factor
def __call__(self, x): def __call__(self, x):
...@@ -179,31 +167,6 @@ class ElasticDistortion: ...@@ -179,31 +167,6 @@ class ElasticDistortion:
return Image.fromarray(dst.astype(np.uint8)) 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): def get_list_augmenters(img, aug_configs, fill_value):
""" """
Randomly select a list of data augmentation techniques to used based on aug_configs 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): ...@@ -236,9 +199,7 @@ def get_list_augmenters(img, aug_configs, fill_value):
and factor * img.size[1] < aug_config["min_height"] and factor * img.size[1] < aug_config["min_height"]
) )
) )
augmenters.append( augmenters.append(DPIAdjusting(factor))
DPIAdjusting(factor, preserve_ratio=aug_config["preserve_ratio"])
)
elif aug_config["type"] == "zoom_ratio": elif aug_config["type"] == "zoom_ratio":
ratio_h = rand_uniform(aug_config["min_ratio_h"], aug_config["max_ratio_h"]) 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): ...@@ -351,94 +312,6 @@ def apply_data_augmentation(img, da_config):
return img 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): def aug_config(proba_use_da, p):
return { return {
"order": "random", "order": "random",
...@@ -449,7 +322,6 @@ def aug_config(proba_use_da, p): ...@@ -449,7 +322,6 @@ def aug_config(proba_use_da, p):
"proba": p, "proba": p,
"min_factor": 0.75, "min_factor": 0.75,
"max_factor": 1, "max_factor": 1,
"preserve_ratio": True,
}, },
{ {
"type": "perspective", "type": "perspective",
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment