-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimageutil.py
executable file
·140 lines (120 loc) · 4.7 KB
/
imageutil.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#! /usr/bin/python3
# vim: expandtab shiftwidth=4 tabstop=4
"""This class performs image transformations useful for a convnet"""
import numpy as np
import torch
import cv2
from albumentations import (
Compose,
OneOf,
IAAAdditiveGaussianNoise,
GaussNoise,
MotionBlur,
MedianBlur,
Blur,
Rotate,
ShiftScaleRotate,
OpticalDistortion,
GridDistortion,
IAAPiecewiseAffine,
CLAHE,
IAASharpen,
IAAEmboss,
RandomContrast,
RandomBrightness,
RandomGamma,
HueSaturationValue,
Resize
)
class ImageUtil:
def __init__(self, insz=360, outsz=227):
self.insz = insz
self.outsz = outsz
def __enter__(self):
return self
def __exit__(self, _, value, traceback):
pass
def transform(self, randomize, images):
return_value = None
if randomize:
return_value = self._transform_random(images)
else:
return_value = self._transform_center(images)
return return_value
def transform_np(self, randomize, images):
return_value = None
if randomize:
return_value = self._transform_random_np(images)
else:
return_value = self._transform_center_np(images)
return return_value
def _transform_generic(self, images, prob):
"""So, we assume these images are batched torch values, scaled from -1..1
We need to convert them to numpy arrays.
"""
dummy_batchsz, height, width, channels = images.shape
assert height == width, "We assume squares as inputs."
assert channels == 3, "We assume RGB images."
assert images.dtype == torch.float32
assert -1 <= images.min() and images.max() <= 1
npimages = ((images.cpu().numpy() + 1.0) * 255.0 / 2.0).astype(np.uint8)
outputs = self._transform_generic_np(npimages, prob)
return (torch.Tensor(outputs).type_as(images) * 2.0 / 255.0) - 1.0
def _transform_generic_np(self, npimages, prob):
"""So, we assume these images are batched numpy values, scaled from 0..255"""
batchsz, height, width, channels = npimages.shape
assert height == width, "We assume squares as inputs."
assert channels == 3, "We assume RGB images."
assert npimages.dtype == np.uint8
assert 0 <= npimages.min() and npimages.max() <= 255
# make these the images that albumentation requires.
outputs = np.zeros((batchsz, self.outsz, self.outsz, channels), dtype=np.uint8)
ops = Compose( \
[ \
Compose( \
[ \
OneOf([ \
IAAAdditiveGaussianNoise(p=1.0), \
GaussNoise(p=1.0), \
], p=0.5), \
OneOf([ \
MotionBlur(p=1.0), \
MedianBlur(blur_limit=3, p=1.0), \
Blur(blur_limit=3, p=1.0), \
], p=0.5), \
RandomGamma(p=0.5), \
Rotate(limit=45, interpolation=cv2.INTER_CUBIC, p=0.5), \
ShiftScaleRotate(shift_limit=0.0625, scale_limit=0.2, rotate_limit=45, interpolation=cv2.INTER_CUBIC, p=0.5), \
OneOf([ \
OpticalDistortion(interpolation=cv2.INTER_CUBIC, p=1.0), \
GridDistortion(interpolation=cv2.INTER_CUBIC, p=1.0), \
IAAPiecewiseAffine(p=1.0), \
], p=0.5), \
OneOf([ \
CLAHE(clip_limit=2, p=1.0), \
IAASharpen(p=1.0), \
IAAEmboss(p=1.0), \
RandomContrast(p=1.0), \
RandomBrightness(p=1.0), \
], p=0.5), \
HueSaturationValue(p=0.5), \
], \
p=prob \
), \
Resize(self.outsz, self.outsz, interpolation=cv2.INTER_CUBIC), \
], \
p=1.0 \
)
# So, the output of ops, should be a dictionary containing an image
for idx in range(0, batchsz):
vvv = ops(image=npimages[idx])["image"]
outputs[idx] = vvv
return outputs
def _transform_random(self, images):
return self._transform_generic(images, prob=0.8)
def _transform_center(self, images):
return self._transform_generic(images, prob=0.0)
def _transform_random_np(self, images):
return self._transform_generic_np(images, prob=0.8)
def _transform_center_np(self, images):
return self._transform_generic_np(images, prob=0.0)