[16dd74]: / dsb2018_topcoders / selim / aug / composition.py

Download this file

53 lines (41 with data), 1.6 kB

 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
import random
import numpy as np
class Compose:
def __init__(self, transforms, prob=1.0):
self.transforms = [t for t in transforms if t is not None]
self.prob = prob
def __call__(self, **data):
if random.random() < self.prob:
for t in self.transforms:
data = t(**data)
return data
class OneOf:
def __init__(self, transforms, prob=.5):
self.transforms = transforms
self.prob = prob
def __call__(self, **data):
if random.random() < self.prob:
t = random.choice(self.transforms)
t.prob = 1.
data = t(**data)
return data
class OneOrOther:
def __init__(self, first, second, prob=.5):
self.first = first
first.prob = 1.
self.second = second
second.prob = 1.
self.prob = prob
def __call__(self, **data):
return self.first(**data) if random.random() < self.prob else self.second(**data)
class GrayscaleOrColor:
def __init__(self, color_transform, grayscale_transform):
self.color_transform = color_transform
self.grayscale_transform = grayscale_transform
def __call__(self, **data):
image = data['image']
grayscale = np.allclose(image[..., 0], image[..., 1], atol=0.001) and np.allclose(image[..., 1], image[..., 2], atol=0.001)
if not grayscale:
return self.color_transform(**data)
else:
return self.grayscale_transform(**data)