-
Notifications
You must be signed in to change notification settings - Fork 1
/
transforms.py
70 lines (54 loc) · 1.81 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
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
import random
import paddle
import numpy as np
import cv2
class Compose:
def __init__(self, transforms):
if not isinstance(transforms, list):
raise TypeError('The transforms must be a list!')
self.transforms = transforms
def __call__(self, im1, im2):
if isinstance(im1, str):
im1 = cv2.imread(im1)
if isinstance(im2, str):
im2 = cv2.imread(im2)
if im1 is None or im2 is None:
raise ValueError('Can\'t read The image file {} and {}!'.format(im1, im2))
for op in self.transforms:
outputs = op(im1, im2)
im1 = outputs[0]
im2 = outputs[1]
im1 = normalize(im1)
im2 = normalize(im2)
return im1, im2
class Flip(object):
def __init__(self, prob_lr=0.5, prob_ud=0.5):
self.prob_lr = prob_lr
self.prob_ud = prob_ud
def __call__(self, im1, im2):
if random.random() < self.prob_lr:
im1 = np.fliplr(im1)
im2 = np.fliplr(im2)
if random.random() < self.prob_ud:
im1 = np.flipud(im1)
im2 = np.flipud(im2)
return im1, im2
class ToTensor(object):
def __call__(self, im1, im2):
im1 = np.ascontiguousarray(im1.transpose((2, 0, 1)).astype(np.float32))
im2 = np.ascontiguousarray(im2.transpose((2, 0, 1)).astype(np.float32))
im1 = paddle.to_tensor(im1)
im2 = paddle.to_tensor(im2)
return im1, im2
def normalize(x, centralize=True, normalize=True, val_range=255.0):
if centralize:
x = x - val_range / 2
if normalize:
x = x / val_range
return x
def normalize_reverse(x, centralize=True, normalize=True, val_range=255.0):
if normalize:
x = x * val_range
if centralize:
x = x + val_range / 2
return x