-
Notifications
You must be signed in to change notification settings - Fork 0
/
custom_transforms.py
88 lines (62 loc) · 2.52 KB
/
custom_transforms.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import numpy as np
import albumentations as A
from albumentations.pytorch.transforms import ToTensorV2
import cv2
MEAN = [0.485, 0.456, 0.406]
STD = [0.229, 0.224, 0.225]
def train_images(normalize=True, resize=224):
"""
Creates a transformation pipeline for training images.
Args:
normalize (bool, optional): Whether to normalize the images. Default is True.
resize (int, optional): The size to which images should be resized. Default is 224.
Returns:
albumentations.core.composition.Compose: A composed transformation pipeline.
"""
transform_list = []
transform_list.extend([
A.Resize(width=resize, height=resize, interpolation=cv2.INTER_LINEAR),
A.HorizontalFlip(p=0.5),
A.VerticalFlip(p=0.5),
A.ColorJitter(p=0.5, hue=(-0.15, 0.15), saturation=(0.8, 1.2), brightness=(0.7, 1.2), contrast=(0.7, 1.5)),
])
if normalize:
transform_list.append(A.Normalize(mean=MEAN, std=STD))
transform_list.append(ToTensorV2())
return A.Compose(transform_list)
def val_images(normalize=True, resize=224):
"""
Creates a transformation pipeline for validation images.
Args:
normalize (bool, optional): Whether to normalize the images. Default is True.
resize (int, optional): The size to which images should be resized. Default is 224.
Returns:
albumentations.core.composition.Compose: A composed transformation pipeline.
"""
transform_list = []
#transform_list.append(A.PadIfNeeded(min_height=224, min_width=224, border_mode=cv2.BORDER_CONSTANT))
transform_list.append(A.Resize(width=resize, height=resize, interpolation=cv2.INTER_LINEAR))
if normalize:
transform_list.append(A.Normalize(mean=MEAN, std=STD))
transform_list.append(ToTensorV2())
return A.Compose(transform_list)
def train_df(sigma=0.1):
"""
Creates a Gaussian blur transformation for deep features.
Args:
sigma (float, optional): Standard deviation for Gaussian kernel. Default is 0.1.
Returns:
GaussianBlur: An instance of the GaussianBlur transformation.
"""
return GaussianBlur(sigma=sigma)
class GaussianBlur():
"""
Gaussian blur transformation for deep features.
Args:
sigma (float, optional): Standard deviation for Gaussian kernel. Default is 0.1.
"""
def __init__(self, sigma=0.1):
self.sigma = sigma
def __call__(self, input):
s = np.random.uniform(0,self.sigma)
return input+np.random.normal(size=input.shape, scale=s)