-
Notifications
You must be signed in to change notification settings - Fork 3
/
data.py
187 lines (135 loc) · 5.73 KB
/
data.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import os
import sys
import numpy as np
import cv2
import random
import glob
import chainer
def read_imlist(root_dir, txt_imlist):
with open(txt_imlist, 'r') as f:
ret = [os.path.join(root_dir, path.strip()) for path in f.readlines()]
return ret
def train_test_dataset(train_class_name, args_train, test_class_name, args_test):
mod_name = os.path.splitext(os.path.basename(__file__))[0]
mod_path = os.path.dirname(__file__)
sys.path.insert(0, mod_path)
train_class = getattr(__import__(mod_name), train_class_name)
test_class = getattr(__import__(mod_name), test_class_name)
train = train_class(**args_train)
test = test_class(**args_test)
return train, test
class TestNIRRGB(chainer.dataset.DatasetMixin):
def __init__(self, dir_nir, dir_rgb, imlist_nir, imlist_rgb):
super().__init__()
self.nir = read_imlist(dir_nir, imlist_nir)
self.rgb = read_imlist(dir_rgb, imlist_rgb)
def __len__(self):
return len(self.rgb)
def get_example(self, i):
nir = cv2.imread(self.nir[i], 0).astype(np.float32)
rgb = cv2.imread(self.rgb[i], 1).astype(np.float32)
nirrgb = np.concatenate((nir[:, :, None], rgb), axis=2)
nirrgb = nirrgb.transpose(2, 0, 1) / 127.5 - 1.
return nirrgb,
class TestNIR(chainer.dataset.DatasetMixin):
def __init__(self, dir_nir, imlist_nir):
super().__init__()
self.nir = read_imlist(dir_nir, imlist_nir)
def __len__(self):
return len(self.nir)
def get_example(self, i):
nir = cv2.imread(self.nir[i], 0).astype(np.float32)
nir = nir[None, :, :] / 127.5 - 1.
return nir,
class TestRGB(chainer.dataset.DatasetMixin):
def __init__(self, dir_rgb, imlist_rgb):
super().__init__()
self.rgb = read_imlist(dir_rgb, imlist_rgb)
def __len__(self):
return len(self.rgb)
def get_example(self, i):
rgb = cv2.imread(self.rgb[i], 1).astype(np.float32)
rgb = rgb.transpose(2, 0, 1) / 127.5 - 1.
return rgb,
class BaseTrain(chainer.dataset.DatasetMixin):
def __init__(self):
super().__init__()
def transform(self, x, y):
c, h, w = x.shape
if self.augmentation:
top = random.randint(0, h - self.size - 1)
left = random.randint(0, w - self.size - 1)
if random.randint(0, 1):
x = x[:, :, ::-1]
y = y[:, :, ::-1]
else:
top = (h - self.size) // 2
left = (w - self.size) // 2
bottom = top + self.size
right = left + self.size
x = x[:, top:bottom, left:right]
y = y[:, top:bottom, left:right]
return x, y
class NIRRGB2RGBCLOUD(BaseTrain):
def __init__(self, dir_nir, dir_rgb, dir_cloud, imlist_nir, imlist_rgb, *args, **kwargs):
super().__init__()
self.nir = read_imlist(dir_nir, imlist_nir)
self.rgb = read_imlist(dir_rgb, imlist_rgb)
self.cloud = list(glob.glob(os.path.join(dir_cloud, '*.png')))
self.size = kwargs.pop('size')
self.augmentation = kwargs.pop('augmentation')
def __len__(self):
return len(self.rgb)
def get_example(self, i):
nir = cv2.imread(self.nir[i], 0).astype(np.float32)
rgb = cv2.imread(self.rgb[i], 1).astype(np.float32)
cloud = cv2.imread(random.choice(self.cloud), -1).astype(np.float32)
alpha = cloud[:, :, 3] / 255.
alpha = np.broadcast_to(alpha[:, :, None], alpha.shape + (3,))
clouded_rgb = (1. - alpha) * rgb + alpha * cloud[:, :, :3]
clouded_rgb = np.clip(clouded_rgb, 0., 255.)
nirrgb = np.concatenate((nir[:, :, None], clouded_rgb), axis=2)
cloud = cloud[:, :, 3]
rgbcloud = np.concatenate((rgb, cloud[:, :, None]), axis=2)
nirrgb = nirrgb.transpose(2, 0, 1) / 127.5 - 1.
rgbcloud = rgbcloud.transpose(2, 0, 1) / 127.5 - 1.
nirrgb, rgbcloud = self.transform(nirrgb, rgbcloud)
return nirrgb, rgbcloud
class RGB2RGBCLOUD(BaseTrain):
def __init__(self, dir_rgb, dir_cloud, imlist_rgb, *args, **kwargs):
super().__init__()
self.rgb = read_imlist(dir_rgb, imlist_rgb)
self.cloud = list(glob.glob(os.path.join(dir_cloud, '*.png')))
self.size = kwargs.pop('size')
self.augmentation = kwargs.pop('augmentation')
def __len__(self):
return len(self.rgb)
def get_example(self, i):
rgb = cv2.imread(self.rgb[i], 1).astype(np.float32)
cloud = cv2.imread(random.choice(self.cloud), -1).astype(np.float32)
alpha = cloud[:, :, 3] / 255.
alpha = np.broadcast_to(alpha[:, :, None], alpha.shape + (3,))
clouded_rgb = (1. - alpha) * rgb + alpha * cloud[:, :, :3]
clouded_rgb = np.clip(clouded_rgb, 0., 255.)
cloud = cloud[:, :, 3]
rgbcloud = np.concatenate((rgb, cloud[:, :, None]), axis=2)
rgb = clouded_rgb.transpose(2, 0, 1) / 127.5 - 1.
rgbcloud = rgbcloud.transpose(2, 0, 1) / 127.5 - 1.
rgb, rgbcloud = self.transform(rgb, rgbcloud)
return rgb, rgbcloud
class NIR2RGB(BaseTrain):
def __init__(self, dir_nir, dir_rgb, imlist_nir, imlist_rgb, *args, **kwargs):
super().__init__()
self.nir = read_imlist(dir_nir, imlist_nir)
self.rgb = read_imlist(dir_rgb, imlist_rgb)
self.size = kwargs.pop('size')
self.augmentation = kwargs.pop('augmentation')
def __len__(self):
return len(self.rgb)
def get_example(self, i):
nir = cv2.imread(self.nir[i], 0).astype(np.float32)
rgb = cv2.imread(self.rgb[i], 1).astype(np.float32)
nir = nir[None, :, :] / 127.5 - 1.
rgb = rgb.transpose(2, 0, 1) / 127.5 - 1.
nir, rgb = self.transform(nir, rgb)
return nir, rgb