-
Notifications
You must be signed in to change notification settings - Fork 0
/
datasets.py
157 lines (137 loc) · 5.4 KB
/
datasets.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
import os
import math
from errno import ENOENT
import numpy as np
import cv2
import scipy.ndimage
from torchvision.transforms import ToTensor, Normalize, Compose
from torch.utils.data import Dataset, DataLoader
from utils import pp
def read_image(name):
raw = cv2.imread(name, cv2.IMREAD_UNCHANGED)
if not type(raw) is np.ndarray:
raise FileNotFoundError(ENOENT, os.strerror(ENOENT), name)
return raw
class Item():
def __init__(self, target_dir, name, is_train=True):
self.name = name
self.x_raw = read_image(os.path.join(target_dir, 'x', f'{name}.jpg'))
self.y_raw = read_image(os.path.join(target_dir, 'y', f'{name}.png'))
assert(self.x_raw.shape[:2] == self.y_raw.shape[:2])
self.is_train = is_train
def get_splitted(self, size):
H, W = self.x_raw.shape[:2]
Y = -(-self.x_raw.shape[0] // size)
X = -(-self.x_raw.shape[1] // size)
ww = [(W + i) // X for i in range(X)]
hh = [(H + i) // Y for i in range(Y)]
pairs = []
pos = [0, 0]
for y, h in enumerate(hh):
pos[0] = 0
pairs.append([])
for x, w in enumerate(ww):
x = self.x_raw[pos[1]:pos[1]+h, pos[0]:pos[0]+w].copy()
y = self.y_raw[pos[1]:pos[1]+h, pos[0]:pos[0]+w].copy()
pairs[-1].append((x, y))
pos[0] += w
pos[1] += h
return pairs
def read_images(target_dir, one=False, is_train=True):
items = []
file_names = sorted(os.listdir(os.path.join(target_dir, 'y')))
count = len(file_names)
i = 0
for file_name in file_names:
i += 1
base_name, ext_name = os.path.splitext(file_name)
pp(f'loading {base_name} {i}/{count}')
items.append(Item(target_dir, base_name, is_train))
if one:
break
pp(f'All images in {target_dir} have been loaded.')
print('')
return items
class BaseDataset(Dataset):
def __init__(self, transform_x=None, transform_y=None, target='train', load_train=True):
self.transform_x = transform_x
self.transform_y = transform_y
if target != 'validation':
self.items = read_images('./train', one=target == 'one') if load_train else []
if target == 'validation' or target == 'all':
self.items += read_images('./validation', one=False, is_train=False)
def transform(self, x, y):
if self.transform_x:
x = self.transform_x(x)
if self.transform_y:
y = self.transform_y(y)
return x, y
class TrainingDataset(BaseDataset):
def __init__(self, tile_size, p_rotation=-1, stricted_roi=False, *args, **kwargs):
super().__init__(*args, **kwargs)
self.tile_size = tile_size
self.p_rotation = p_rotation
self.stricted_roi = stricted_roi
def flip_and_rot90(self, arr, op):
if op > 3:
arr = np.flip(arr, axis=0)
return np.rot90(arr, op % 4)
def rotate(self, arr, degree):
i = scipy.ndimage.rotate(arr, degree, mode='mirror')
return i
def check_available(self, arr):
return (not self.stricted_roi) and np.any(arr != 0)
def select(self):
p = []
for i in self.items:
# p.append((i.shape[0] - self.tile_size) * (i.shape[1] - self.tile_size))
p.append(math.sqrt((i.x_raw.shape[0] - self.tile_size) * (i.x_raw.shape[1] - self.tile_size)))
p = np.array(p / np.sum(p))
use_patch = False
size = self.tile_size
while not use_patch:
i = np.random.choice(len(self.items), 1, p=p)[0]
y_raw = self.items[i].y_raw
x_raw = self.items[i].x_raw
image_h, image_w, _ = y_raw.shape
left = np.random.randint(image_w - size)
top = np.random.randint(image_h - size)
y_arr = y_raw[top:top + size, left:left + size]
use_patch = self.check_available(y_arr)
x_arr = x_raw[top:top + size, left:left + size]
return (x_arr, y_arr)
def __len__(self):
l = 0
for i in self.items:
l += int((i.x_raw.shape[0] / self.tile_size) * (i.x_raw.shape[1] / self.tile_size))
return l * 8
def __getitem__(self, _idx):
x_arr, y_arr = self.select()
op = np.random.randint(8)
x_arr = self.flip_and_rot90(x_arr, op)
y_arr = self.flip_and_rot90(y_arr, op)
size = self.tile_size
if np.random.rand() < self.p_rotation:
degree = np.random.randint(45)
x_arr = self.rotate(x_arr, degree)
y_arr = self.rotate(y_arr, degree)
h, w = x_arr.shape[0:2]
top = np.random.randint(h - size) if h > size else 0
left = np.random.randint(w - size) if w > size else 0
x_arr = x_arr[top:top + size, left:left + size]
y_arr = y_arr[top:top + size, left:left + size]
return self.transform(x_arr.copy(), y_arr.copy())
class ValidationDataset(BaseDataset):
def __len__(self):
return len(self.items)
def __getitem__(self, i):
return self.items[i]
if __name__ == '__main__':
ds = ValidationDataset(one=True)
for item in ds:
print(item.name, item.x_raw.shape)
for y, row in enumerate(item.get_splitted(1000)):
print(type(row))
for x, (input_arr, label_arr) in enumerate(row):
print(type(input_arr))
break