forked from chokyungjin/MuSiC-ViT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
datasets.py
219 lines (185 loc) · 8.31 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
import os
import sys
import numpy as np
import torch
import random
import glob
from torch.utils.data import Dataset
import torchvision
import torchvision.transforms as transforms
from config import parse_arguments
from PIL import Image
import json
import albumentations as A
from albumentations.pytorch import ToTensorV2
from skimage import exposure
class ClassPairDataset(Dataset):
def __init__(self, input_path, dataset, mode, margin=False, sample_data=None, fov=False, mean=0.2, std=0.4, aug=False,transform=None):
if dataset == 'toy':
input_path = input_path + '_toy'
self.input_path = os.path.join(input_path, '{}set/512'.format(mode))
self.disease_label_path = os.path.join(input_path, 'label_csv/disease.json')
self.aug = aug
self.fov = fov
self.mean = mean
self.std = std
self.sample_data = sample_data
self.margin = margin
with open(self.disease_label_path, "r") as f:
self.disease_label = json.load(open(self.disease_label_path))
if self.margin is not None:
print("[*] Margin true")
else:
print("[*] Margin false")
if self.fov is not None:
print("FOV true")
if self.sample_data is not None:
print("[*] Sample data loaded")
json_name = './json/4class_datasets_{}_sample_512_fov_{}.json'.format(dataset,mode)
else:
json_name = './json/4class_datasets_{}_512_fov_{}.json'.format(dataset,mode)
else:
json_name = './json/4class_datasets_{}_512_{}.json'.format(dataset,mode)
if os.path.exists(json_name) is True:
print('[*] {} is already exist. Loading Json from {}'.format(json_name, json_name))
with open(json_name, "r") as f:
self.samples = json.load(f)
else:
print('[*] There is no {}. Start making new Json'.format(json_name, json_name))
self.samples = self._make_dataset(mode)
with open(json_name, "w") as f:
json.dump(self.samples, f)
if mode == 'train':
if self.aug is not None:
print("[*] Augmentation On")
self.transform = A.Compose([
A.Resize(512, 512),
A.OneOf([
A.MedianBlur(blur_limit=3, p=0.1),
A.MotionBlur(p=0.2),
A.Sharpen(p=0.2),
], p=0.2),
A.ShiftScaleRotate(shift_limit=0.0625, scale_limit=0.2, rotate_limit=10, p=0.2),
A.OneOf([
A.OpticalDistortion(p=0.3),
], p=0.2),
A.OneOf([
A.CLAHE(clip_limit=4.0),
A.Equalize(),
], p=0.2),
A.OneOf([
A.GaussNoise(p=0.2),
A.MultiplicativeNoise(p=0.2),
], p=0.2),
A.HueSaturationValue(hue_shift_limit=0, sat_shift_limit=0, val_shift_limit=0.1, p=0.3),
A.Normalize(mean=(self.mean), std=(self.std,)),
ToTensorV2(),
], additional_targets={'image0': 'image' , 'image1': 'image' , 'image2': 'image'})
else:
self.transform = A.Compose([
A.Resize(512, 512),
A.Normalize(mean=(self.mean), std=(self.std,)),
ToTensorV2(),
], additional_targets={'image0': 'image' , 'image1': 'image' , 'image2': 'image'})
else:
self.transform = A.Compose([
A.Resize(512, 512),
A.Normalize(mean=(self.mean,), std=(self.std,)),
ToTensorV2(),
], additional_targets={'image0': 'image' , 'image1': 'image' , 'image2': 'image'})
def _find_disease_label(self, exam_id):
if exam_id in self.disease_label['normal']:
return 0 #normal
elif exam_id in self.disease_label['abnormal']:
return 1 #abnormal
else:
return 2
def _check_crop_label(self, crop_label):
x_start, x_end, y_start, y_end = crop_label
x_margin = int(511-x_start)
y_margin = int(y_end-y_start)
if x_margin < 300:
x_start = 47
if y_margin < 300:
y_start, y_end = 55, 453
return [x_start, x_end, y_start, y_end]
def _check_crop_label_margin(self, crop_label, margin=True, ratio=0.08):
x_start, x_end, y_start, y_end = crop_label
x_margin = int(511-x_start)
y_margin = int(y_end-y_start)
if x_margin < 300:
x_start, x_end = 47, 445
if y_margin < 300:
y_start, y_end = 55, 453
if margin is True:
margin = int((x_end - x_start) * ratio // 2)
x_start -= margin
x_end += margin
margin = int((y_end - y_start) * ratio // 2)
y_start -= margin
y_end += margin
if x_start < 0:
x_start = 0
if y_start < 0:
y_start = 0
if x_end > 511:
x_end = 511
if y_end > 511:
y_end = 511
return [x_start, x_end, y_start, y_end]
def __getitem__(self, idx):
if self.fov is not None:
if self.margin is not None:
x_min, _, y_min, y_max = self._check_crop_label_margin(self.samples['fov'][idx][0])
else:
x_min, _, y_min, y_max = self._check_crop_label(self.samples['fov'][idx][0])
base_img = np.array(Image.open(self.samples['imgs'][idx][0]))
base_img = base_img[x_min:, y_min:y_max]
if self.margin is not None:
x_min, _, y_min, y_max = self._check_crop_label_margin(self.samples['fov'][idx][1])
else:
x_min, _, y_min, y_max = self._check_crop_label(self.samples['fov'][idx][1])
pair_img = np.array(Image.open(self.samples['imgs'][idx][1]))
pair_img = pair_img[x_min:, y_min:y_max]
else:
base_img = np.array(Image.open(self.samples['imgs'][idx][0]))
pair_img = np.array(Image.open(self.samples['imgs'][idx][1]))
transformed = self.transform(image=base_img, image0=pair_img)
base_img = transformed['image']
base_img = self._catch_exception(base_img)
pair_img = transformed['image0']
pair_img = self._catch_exception(pair_img)
change_labels = self.samples['change_labels'][idx]
disease_labels = self.samples['disease_labels'][idx]
patient_name = self.samples['imgs'][idx][0]#.split('/')[-2]
return base_img, pair_img, change_labels, disease_labels, patient_name
def __len__(self):
return len(self.samples['change_labels'])
def _catch_exception(self, img):
return img[0, :, :].unsqueeze(0) if img.shape[0] == 3 else img
def _get_change_label_num(self, label, label_list):
specific_labels = []
for i in label_list:
if label == i:
specific_labels.append(i)
return len(specific_labels)
def _get_disease_label_num(self, label, label_list):
specific_labels = []
for i in label_list:
for j in i:
if label == j:
specific_labels.append(i)
return len(specific_labels)
def get_data_property(self):
if len(self.samples['change_labels']):
print('images(pair): {}\nlabels(change): {}\nlabels(nochange): {}\nlabels(normal): {}\nlabels(abnormal): {}\nlabels(unknown): {}'.format(
len(self.samples['imgs']),
self._get_change_label_num(0, self.samples['change_labels']),
self._get_change_label_num(1, self.samples['change_labels']),
self._get_disease_label_num(0, self.samples['disease_labels']),
self._get_disease_label_num(1, self.samples['disease_labels']),
self._get_disease_label_num(2, self.samples['disease_labels']),
)
)
if __name__ == '__main__':
args = parse_arguments(sys.argv[1:])