-
Notifications
You must be signed in to change notification settings - Fork 0
/
datasets.py
228 lines (185 loc) · 8.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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import os
import random
import glob
import numpy as np
from skimage import io
from torch.utils.data import Dataset
import torch
import torchvision.transforms as transforms
from tqdm import tqdm
class ImageDataset(Dataset):
def __init__(self, base_dataset_dir, mode, normalize=True, augmentation=True, seed=101, size=(128, 128), test_idx=None, scale_ratio=(1.0, 1.0), shuffle=False, cutmix=False):
assert mode in ['train', 'test'], "Mode should be 'train' or 'test'"
self.size = size
self.augmentation = augmentation
self.normalize = normalize
self.scale_ratio = scale_ratio
self.mode= mode
self.eps = 1e-7
if seed != None:
torch.manual_seed(seed)
np.random.seed(seed)
random.seed(seed)
if torch.cuda.is_available():
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
data_dir = os.path.join(base_dataset_dir, mode)
self.files_A = glob.glob(os.path.join(data_dir, "A") + "/*.tif")
self.files_B = glob.glob(os.path.join(data_dir, "B") + "/*.tif")
self.data_A = []
self.data_B = []
for file_A in tqdm(self.files_A, desc=f"Loading {mode}ing data from domain 1..."):
img = self.get_image(file_A, 0, normalize,)
if len(img.shape) == 2:
img = img.unsqueeze(0)
if img.shape[1] >= self.size and img.shape[2] >= self.size:
self.data_A.append(img)
for file_B in tqdm(self.files_B, desc=f"Loading {mode}ing data from domain 2..."):
img = self.get_image(file_B, 1, normalize)
if len(img.shape) == 2:
img = img.unsqueeze(0)
if img.shape[1] >= self.size and img.shape[2] >= self.size:
self.data_B.append(img)
def __len__(self):
return max(len(self.data_A), len(self.data_B))
def random_rotate(self, img):
rotation_angle = random.choice([90, 180, 270, 360])
img = transforms.functional.rotate(img, rotation_angle)
return img
def random_flip(self, img):
if random.random() < 0.5:
img = transforms.functional.hflip(img)
if random.random() < 0.5:
img = transforms.functional.vflip(img)
return img
def substring_exists(self, s, sub):
return sub in s
def get_image(self, file, domain, normalize=True):
img = torch.from_numpy(io.imread(file)).float()
while len(img.shape) < 4:
img = img.unsqueeze(0)
img = torch.nn.functional.interpolate(img, scale_factor=(self.scale_ratio[domain], self.scale_ratio[domain]), mode='bilinear').squeeze()
img = np.clip(img, 0, None)
if normalize:
img = torch.clamp(img, 0, np.percentile(img, 99))
img = img / ( img.max() + self.eps )
return img
def __getitem__(self, index):
img_A = self.data_A[index % len(self.data_A)]
img_B = self.data_B[random.randint(0, len(self.data_B) - 1)]
random_idx_A = random.randint(0, img_A.shape[0]-1)
random_idx_B = random.randint(0, img_B.shape[0]-1)
img_A = img_A[random_idx_A:random_idx_A+1, :, :]
img_B = img_B[random_idx_B:random_idx_B+1, :, :]
if self.size != None:
if self.mode == "train":
random_crop = transforms.RandomCrop((self.size, self.size))
img_A = random_crop(img_A)
img_B = random_crop(img_B)
else:
img_A = transforms.functional.center_crop(img_A, (self.size, self.size))
img_B = transforms.functional.center_crop(img_B, (self.size, self.size))
if self.augmentation:
img_A = self.random_rotate(img_A)
img_A = self.random_flip(img_A)
img_B = self.random_rotate(img_B)
img_B = self.random_flip(img_B)
return {"A": img_A, "B": img_B}
class HnEDataset(Dataset):
def __init__(self, base_dataset_dir, mode, normalize=True, augmentation=True, seed=101, size=(128, 128), scale_ratio=(1.0, 1.0), shuffle=False, cutmix=False):
assert mode in ['train', 'test'], "Mode should be 'train' or 'test'"
self.mode = mode
self.size = size
self.augmentation = augmentation
self.normalize = normalize
self.scale_ratio = scale_ratio
self.shuffle = shuffle
self.eps = 1e-7
if seed != None:
torch.manual_seed(seed)
np.random.seed(seed)
random.seed(seed)
if torch.cuda.is_available():
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
data_dir = os.path.join(base_dataset_dir, mode)
self.files_A = sorted(glob.glob(os.path.join(data_dir, "A") + "/*"))
self.files_B = sorted(glob.glob(os.path.join(data_dir, "B") + "/*"))
self.data_A = []
self.data_B = []
for file_A in tqdm(self.files_A, desc=f"Loading {mode}ing data from domain 1..."):
try:
img = self.get_image_255_LivetoHnE(file_A, 0, normalize)
if len(img.shape) == 2:
img = img.unsqueeze(0)
if img.shape[-1] >= self.size or img.shape[-2] >= self.size:
self.data_A.append(img)
except IOError:
print("Image is truncated or corrupted.")
continue
for file_B in tqdm(self.files_B, desc=f"Loading {mode}ing data from domain 2..."):
try:
img = self.get_image_255_LivetoHnE(file_B, 1, normalize)
if len(img.shape) == 2:
img = img.unsqueeze(0)
if img.shape[-1] >= self.size or img.shape[-2] >= self.size:
self.data_B.append(img)
except IOError:
print("Image is truncated or corrupted.")
continue
def __len__(self):
return max(len(self.data_A), len(self.data_B))
def random_rotate(self, img):
rotation_angle = random.choice([90, 180, 270, 360])
img = transforms.functional.rotate(img, rotation_angle)
return img
def random_flip(self, img):
if random.random() < 0.5:
img = transforms.functional.hflip(img)
if random.random() < 0.5:
img = transforms.functional.vflip(img)
return img
def substring_exists(self, s, sub):
return sub in s
def get_image_255_LivetoHnE(self, file, domain, normalize=True):
img = torch.from_numpy(io.imread(file)).to(torch.uint8)
if len(img.shape) == 2:
img = img.unsqueeze(0)
if img.shape[-1] > 3 and img.shape[-1] < img.shape[-2]:
img = img[:, :, :3]
if img.shape[-1] < img.shape[-2]:
img = img.permute(2,0,1)
img = torch.nn.functional.interpolate(img.unsqueeze(0), scale_factor=(self.scale_ratio[domain], self.scale_ratio[domain]), mode='bilinear').squeeze()
return img
def __getitem__(self, index):
img_A = self.data_A[index % len(self.data_A)]
if self.mode == "train":
if self.shuffle == True:
img_B = self.data_B[random.randint(0, len(self.data_B) - 1)]
else:
img_B = self.data_B[index % len(self.data_B)]
else:
img_B = self.data_B[index % len(self.data_B)]
img_A = img_A.float()
img_B = img_B.float()
if self.normalize:
img_A = img_A / 255.0
img_B = img_B / 255.0
if self.size != None:
if self.mode == "train":
random_crop = transforms.RandomCrop((self.size, self.size))
img_A = random_crop(img_A)
img_B = random_crop(img_B)
else:
img_A = transforms.functional.center_crop(img_A, (self.size, self.size))
img_B = transforms.functional.center_crop(img_B, (self.size, self.size))
if self.augmentation:
img_A = self.random_rotate(img_A)
img_A = self.random_flip(img_A)
img_B = self.random_rotate(img_B)
img_B = self.random_flip(img_B)
return {"A": img_A, "B": img_B}