-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathLoad_Dataset.py
200 lines (163 loc) · 7.08 KB
/
Load_Dataset.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
188
189
190
191
192
193
194
195
196
197
198
199
200
# -*- coding: utf-8 -*-
import numpy as np
import torch
import random
from scipy.ndimage.interpolation import zoom
from torch.utils.data import Dataset
from torchvision import transforms as T
from torchvision.transforms import functional as F
from typing import Callable
import os
import cv2
from scipy import ndimage
from bert_embedding import BertEmbedding
def random_rot_flip(image, label):
k = np.random.randint(0, 4)
image = np.rot90(image, k)
label = np.rot90(label, k)
axis = np.random.randint(0, 2)
image = np.flip(image, axis=axis).copy()
label = np.flip(label, axis=axis).copy()
return image, label
def random_rotate(image, label):
angle = np.random.randint(-20, 20)
image = ndimage.rotate(image, angle, order=0, reshape=False)
label = ndimage.rotate(label, angle, order=0, reshape=False)
return image, label
class RandomGenerator(object):
def __init__(self, output_size):
self.output_size = output_size
def __call__(self, sample):
image, label, text = sample['image'], sample['label'], sample['text']
image, label = image.astype(np.uint8), label.astype(np.uint8)
image, label = F.to_pil_image(image), F.to_pil_image(label)
x, y = image.size
if random.random() > 0.5:
image, label = random_rot_flip(image, label)
elif random.random() > 0.5:
image, label = random_rotate(image, label)
if x != self.output_size[0] or y != self.output_size[1]:
image = zoom(image, (self.output_size[0] / x, self.output_size[1] / y), order=3) # why not 3?
label = zoom(label, (self.output_size[0] / x, self.output_size[1] / y), order=0)
image = F.to_tensor(image)
label = to_long_tensor(label)
text = torch.Tensor(text)
sample = {'image': image, 'label': label, 'text': text}
return sample
class ValGenerator(object):
def __init__(self, output_size):
self.output_size = output_size
def __call__(self, sample):
image, label, text = sample['image'], sample['label'], sample['text']
image, label = image.astype(np.uint8), label.astype(np.uint8) # OSIC
image, label = F.to_pil_image(image), F.to_pil_image(label)
x, y = image.size
if x != self.output_size[0] or y != self.output_size[1]:
image = zoom(image, (self.output_size[0] / x, self.output_size[1] / y), order=3) # why not 3?
label = zoom(label, (self.output_size[0] / x, self.output_size[1] / y), order=0)
image = F.to_tensor(image)
label = to_long_tensor(label)
text = torch.Tensor(text)
sample = {'image': image, 'label': label, 'text': text}
return sample
def to_long_tensor(pic):
# handle numpy array
img = torch.from_numpy(np.array(pic, np.uint8))
# backward compatibility
return img.long()
def correct_dims(*images):
corr_images = []
for img in images:
if len(img.shape) == 2:
corr_images.append(np.expand_dims(img, axis=2))
else:
corr_images.append(img)
if len(corr_images) == 1:
return corr_images[0]
else:
return corr_images
class LV2D(Dataset):
def __init__(self, dataset_path: str, task_name: str, row_text: str, joint_transform: Callable = None,
one_hot_mask: int = False,
image_size: int = 224) -> None:
self.dataset_path = dataset_path
self.image_size = image_size
self.output_path = os.path.join(dataset_path)
self.mask_list = os.listdir(self.output_path)
self.one_hot_mask = one_hot_mask
self.rowtext = row_text
self.task_name = task_name
self.bert_embedding = BertEmbedding()
if joint_transform:
self.joint_transform = joint_transform
else:
to_tensor = T.ToTensor()
self.joint_transform = lambda x, y: (to_tensor(x), to_tensor(y))
def __len__(self):
return len(os.listdir(self.output_path))
def __getitem__(self, idx):
mask_filename = self.mask_list[idx] # Co
mask = cv2.imread(os.path.join(self.output_path, mask_filename), 0)
mask = cv2.resize(mask, (self.image_size, self.image_size))
mask[mask <= 0] = 0
mask[mask > 0] = 1
mask = correct_dims(mask)
text = self.rowtext[mask_filename]
text = text.split('\n')
text_token = self.bert_embedding(text)
text = np.array(text_token[0][1])
if text.shape[0] > 14:
text = text[:14, :]
if self.one_hot_mask:
assert self.one_hot_mask > 0, 'one_hot_mask must be nonnegative'
mask = torch.zeros((self.one_hot_mask, mask.shape[1], mask.shape[2])).scatter_(0, mask.long(), 1)
sample = {'label': mask, 'text': text}
return sample, mask_filename
class ImageToImage2D(Dataset):
def __init__(self, dataset_path: str, task_name: str, row_text: str, joint_transform: Callable = None,
one_hot_mask: int = False,
image_size: int = 224) -> None:
self.dataset_path = dataset_path
self.image_size = image_size
self.input_path = os.path.join(dataset_path, 'img')
self.output_path = os.path.join(dataset_path, 'labelcol')
self.images_list = os.listdir(self.input_path)
self.mask_list = os.listdir(self.output_path)
self.one_hot_mask = one_hot_mask
self.rowtext = row_text
self.task_name = task_name
self.bert_embedding = BertEmbedding()
if joint_transform:
self.joint_transform = joint_transform
else:
to_tensor = T.ToTensor()
self.joint_transform = lambda x, y: (to_tensor(x), to_tensor(y))
def __len__(self):
return len(os.listdir(self.input_path))
def __getitem__(self, idx):
image_filename = self.images_list[idx] # MoNuSeg
mask_filename = image_filename[: -3] + "png" # MoNuSeg
# mask_filename = self.mask_list[idx] # Covid19
# image_filename = mask_filename.replace('mask_', '') # Covid19
image = cv2.imread(os.path.join(self.input_path, image_filename))
image = cv2.resize(image, (self.image_size, self.image_size))
# read mask image
mask = cv2.imread(os.path.join(self.output_path, mask_filename), 0)
mask = cv2.resize(mask, (self.image_size, self.image_size))
mask[mask <= 0] = 0
mask[mask > 0] = 1
# correct dimensions if needed
image, mask = correct_dims(image, mask)
text = self.rowtext[mask_filename]
text = text.split('\n')
text_token = self.bert_embedding(text)
text = np.array(text_token[0][1])
if text.shape[0] > 10:
text = text[:10, :]
if self.one_hot_mask:
assert self.one_hot_mask > 0, 'one_hot_mask must be nonnegative'
mask = torch.zeros((self.one_hot_mask, mask.shape[1], mask.shape[2])).scatter_(0, mask.long(), 1)
sample = {'image': image, 'label': mask, 'text': text}
if self.joint_transform:
sample = self.joint_transform(sample)
return sample, image_filename