-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.py
137 lines (107 loc) · 3.86 KB
/
utils.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
import numpy as np
import torch
class Crop(object):
"""
Crop randomly the image in a sample.
Args: output_size (tuple or int): Desired output size. If int, square crop is made.
"""
def __init__(self, output_size):
assert isinstance(output_size, (int, tuple, list))
if isinstance(output_size, int):
self.output_size = (output_size, output_size)
else:
assert len(output_size) == 2
self.output_size = output_size
def __call__(self, sample):
image, label = sample['image'], sample['label']
top, left = sample['top'], sample['left']
new_h, new_w = self.output_size
sample['image'] = image[top: top + new_h,
left: left + new_w]
sample['label'] = label[top: top + new_h,
left: left + new_w]
return sample
class Crop2(object):
"""
Crop randomly the image in a sample.
Args: output_size (tuple or int): Desired output size. If int, square crop is made.
"""
def __init__(self, output_size):
assert isinstance(output_size, (int, tuple, list))
if isinstance(output_size, int):
self.output_size = (output_size, output_size)
else:
assert len(output_size) == 2
self.output_size = output_size
def __call__(self, sample):
image, label = sample['image'], sample['label']
image_width, image_height = 1280, 720
new_h, new_w = self.output_size
top = int(round((image_height - new_h) / 2.))
left = int(round((image_width - new_w) / 2.))
# top, left = sample['top'], sample['left']
sample['image'] = image[top: top + new_h,
left: left + new_w]
sample['label'] = label[top: top + new_h,
left: left + new_w]
return sample
class Flip(object):
"""
shape is (h,w,c)
"""
def __call__(self, sample):
flag_lr = sample['flip_lr']
flag_ud = sample['flip_ud']
if flag_lr == 1:
sample['image'] = np.fliplr(sample['image'])
sample['label'] = np.fliplr(sample['label'])
if flag_ud == 1:
sample['image'] = np.flipud(sample['image'])
sample['label'] = np.flipud(sample['label'])
return sample
class Rotate(object):
"""
shape is (h,w,c)
"""
def __call__(self, sample):
flag = sample['rotate']
if flag == 1:
sample['image'] = sample['image'].transpose(1, 0, 2)
sample['label'] = sample['label'].transpose(1, 0, 2)
return sample
class Sharp2Sharp(object):
def __call__(self, sample):
flag = sample['s2s']
if flag < 1:
sample['image'] = sample['label'].copy()
return sample
class ToTensor(object):
"""
Convert ndarrays in sample to Tensors.
"""
def __call__(self, sample):
image, label = sample['image'], sample['label']
# swap color axis because
# numpy image: H x W x C
# torch image: C X H X W
image = np.ascontiguousarray(image.transpose((2, 0, 1))[np.newaxis, :])
label = np.ascontiguousarray(label.transpose((2, 0, 1))[np.newaxis, :])
sample['image'] = torch.from_numpy(image).float()
sample['label'] = torch.from_numpy(label).float()
return sample
def normalize(x, centralize=False, normalize=False, val_range=255.0):
if centralize:
x = x - val_range / 2
if normalize:
x = x / val_range
return x
def normalize_reverse(x, centralize=False, normalize=False, val_range=255.0):
if normalize:
x = x * val_range
if centralize:
x = x + val_range / 2
return x
def is_image_file(filename):
if filename.startswith('._'):
return
return any(filename.endswith(extension) for extension in [".png", ".jpg", ".jpeg"])