forked from bamos/densenet.pytorch
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtransforms.py
43 lines (39 loc) · 1.2 KB
/
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
import random
import torch
class Brightness(object):
def __init__(self, value):
"""
Alter the Brightness of an image
Arguments
---------
value : brightness factor
=-1 = completely black
<0 = darker
0 = no change
>0 = brighter
=1 = completely white
"""
self.value = max(min(value,1.0),-1.0)
def __call__(self, *inputs):
outputs = []
for idx, _input in enumerate(inputs):
_input = torch.clamp(_input.float().add(self.value).type(_input.type()), 0, 1)
outputs.append(_input)
return outputs if idx > 1 else outputs[0]
class RandomBrightness(object):
def __init__(self, min_val, max_val):
"""
Alter the Brightness of an image with a value randomly selected
between `min_val` and `max_val`
Arguments
---------
min_val : float
min range
max_val : float
max range
"""
self.values = (min_val, max_val)
def __call__(self, *inputs):
value = random.uniform(self.values[0], self.values[1])
outputs = Brightness(value)(*inputs)
return outputs