-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdataset.py
359 lines (281 loc) · 12.2 KB
/
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
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
import os
import cv2
import json
import numpy as np
import torch
from torch.utils.data import Dataset
import pandas as pd
# 상수 정의
CLASSES = [
'finger-1', 'finger-2', 'finger-3', 'finger-4', 'finger-5',
'finger-6', 'finger-7', 'finger-8', 'finger-9', 'finger-10',
'finger-11', 'finger-12', 'finger-13', 'finger-14', 'finger-15',
'finger-16', 'finger-17', 'finger-18', 'finger-19', 'Trapezium',
'Trapezoid', 'Capitate', 'Hamate', 'Scaphoid', 'Lunate',
'Triquetrum', 'Pisiform', 'Radius', 'Ulna',
]
PALETTE = [
(220, 20, 60), (119, 11, 32), (0, 0, 142), (0, 0, 230), (106, 0, 228),
(0, 60, 100), (0, 80, 100), (0, 0, 70), (0, 0, 192), (250, 170, 30),
(100, 170, 30), (220, 220, 0), (175, 116, 175), (250, 0, 30), (165, 42, 42),
(255, 77, 255), (0, 226, 252), (182, 182, 255), (0, 82, 0), (120, 166, 157),
(110, 76, 0), (174, 57, 255), (199, 100, 0), (72, 0, 118), (255, 179, 240),
(0, 125, 92), (209, 0, 151), (188, 208, 182), (0, 220, 176),
]
CLASS2IND = {v: i for i, v in enumerate(CLASSES)}
IND2CLASS = {v: k for k, v in CLASS2IND.items()}
class XRayDataset(Dataset):
def __init__(self, image_root, label_root, is_train=True, transforms=None,
use_cv=False, n_splits=5, fold=0):
self.image_root = image_root
self.label_root = label_root
self.transforms = transforms
self.use_cv = use_cv
self.n_splits = n_splits
self.fold = fold
self.is_train = is_train
# 이미지와 라벨 파일 목록 가져오기
self.pngs = self._get_image_files()
self.jsons = self._get_label_files()
# train/valid 분할
self._split_dataset(is_train)
def _get_image_files(self):
return sorted({
os.path.relpath(os.path.join(root, fname), start=self.image_root)
for root, _dirs, files in os.walk(self.image_root)
for fname in files
if os.path.splitext(fname)[1].lower() == ".png"
})
def _get_label_files(self):
return sorted({
os.path.relpath(os.path.join(root, fname), start=self.label_root)
for root, _dirs, files in os.walk(self.label_root)
for fname in files
if os.path.splitext(fname)[1].lower() == ".json"
})
def _split_dataset(self, is_train):
from sklearn.model_selection import GroupKFold
_filenames = np.array(self.pngs)
_labelnames = np.array(self.jsons)
# 환자 단위로 그룹화
groups = [os.path.dirname(fname) for fname in _filenames]
gkf = GroupKFold(n_splits=self.n_splits)
folds = list(gkf.split(_filenames, [0]*len(_filenames), groups))
# fold 분할
if is_train:
train_indices = []
for i, (_, fold_indices) in enumerate(folds):
if i != self.fold:
train_indices.extend(fold_indices)
self.filenames = list(_filenames[train_indices])
self.labelnames = list(_labelnames[train_indices])
else:
_, val_indices = folds[self.fold]
self.filenames = list(_filenames[val_indices])
self.labelnames = list(_labelnames[val_indices])
def __len__(self):
return len(self.filenames)
def __getitem__(self, item):
image_name = self.filenames[item]
image_path = os.path.join(self.image_root, image_name)
image = cv2.imread(image_path)
if image is None:
print(f"이미지를 불러올 수 없습니다: {image_path}")
image = np.zeros((2048, 2048, 3), dtype=np.uint8)
image = image / 255.
label_name = self.labelnames[item]
label_path = os.path.join(self.label_root, label_name)
label_shape = tuple(image.shape[:2]) + (len(CLASSES), )
label = np.zeros(label_shape, dtype=np.uint8)
with open(label_path, "r") as f:
annotations = json.load(f)["annotations"]
for ann in annotations:
c = ann["label"]
class_ind = CLASS2IND[c]
points = np.array(ann["points"])
class_label = np.zeros(image.shape[:2], dtype=np.uint8)
cv2.fillPoly(class_label, [points], 1)
label[..., class_ind] = class_label
if self.transforms is not None:
transformed = self.transforms(image=image, mask=label)
image = transformed["image"]
label = transformed["mask"]
image = image.transpose(2, 0, 1)
label = label.transpose(2, 0, 1)
image = torch.from_numpy(image).float()
label = torch.from_numpy(label).float()
return image, label
class XRayInferenceDataset(Dataset):
def __init__(self, image_root, transforms=None):
self.image_root = image_root
self.transforms = transforms
self.filenames = self._get_image_files()
def _get_image_files(self):
return sorted({
os.path.relpath(os.path.join(root, fname), start=self.image_root)
for root, _dirs, files in os.walk(self.image_root)
for fname in files
if os.path.splitext(fname)[1].lower() == ".png"
})
def __len__(self):
return len(self.filenames)
def __getitem__(self, item):
image_name = self.filenames[item]
image_path = os.path.join(self.image_root, image_name)
image = cv2.imread(image_path)
if image is None:
print(f"이미지를 불러올 수 없습니다: {image_path}")
image = np.zeros((2048, 2048, 3), dtype=np.uint8)
image = image / 255.
if self.transforms is not None:
transformed = self.transforms(image=image)
image = transformed["image"]
image = image.transpose(2, 0, 1)
image = torch.from_numpy(image).float()
return image, image_name
class RoiXRayDataset(Dataset):
def __init__(self, image_root, label_root, csv_file, is_train=True, transforms=None,
use_cv=False, n_splits=5, fold=0):
self.image_root = image_root
self.label_root = label_root
self.transforms = transforms
self.use_cv = use_cv
self.n_splits = n_splits
self.fold = fold
self.is_train = is_train
# CSV 파일에서 bbox 정보를 읽어오기
self.bbox_data = self._load_bbox_data(csv_file)
# 이미지와 라벨 파일 목록 가져오기
self.pngs = self._get_image_files()
self.jsons = self._get_label_files()
# train/valid 분할
self._split_dataset(is_train)
def _load_bbox_data(self, csv_file):
"""CSV 파일에서 bbox 데이터를 로드"""
bbox_df = pd.read_csv(csv_file)
bbox_dict = {}
for idx, row in bbox_df.iterrows():
image_name = row['image_name']
bbox = eval(row['bbox']) # bbox는 문자열 형태이므로 파싱
bbox_dict[image_name] = bbox
return bbox_dict
def _get_image_files(self):
return sorted({
os.path.relpath(os.path.join(root, fname), start=self.image_root)
for root, _dirs, files in os.walk(self.image_root)
for fname in files
if os.path.splitext(fname)[1].lower() == ".png"
})
def _get_label_files(self):
return sorted({
os.path.relpath(os.path.join(root, fname), start=self.label_root)
for root, _dirs, files in os.walk(self.label_root)
for fname in files
if os.path.splitext(fname)[1].lower() == ".json"
})
def _split_dataset(self, is_train):
from sklearn.model_selection import GroupKFold
_filenames = np.array(self.pngs)
_labelnames = np.array(self.jsons)
# 환자 단위로 그룹화
groups = [os.path.dirname(fname) for fname in _filenames]
gkf = GroupKFold(n_splits=self.n_splits)
folds = list(gkf.split(_filenames, [0]*len(_filenames), groups))
# fold 분할
if is_train:
train_indices = []
for i, (_, fold_indices) in enumerate(folds):
if i != self.fold:
train_indices.extend(fold_indices)
self.filenames = list(_filenames[train_indices])
self.labelnames = list(_labelnames[train_indices])
else:
_, val_indices = folds[self.fold]
self.filenames = list(_filenames[val_indices])
self.labelnames = list(_labelnames[val_indices])
def __len__(self):
return len(self.filenames)
def __getitem__(self, item):
image_name = self.filenames[item]
image_path = os.path.join(self.image_root, image_name)
image = cv2.imread(image_path)
if image is None:
print(f"이미지를 불러올 수 없습니다: {image_path}")
image = np.zeros((2048, 2048, 3), dtype=np.uint8)
image = image / 255.
label_name = self.labelnames[item]
label_path = os.path.join(self.label_root, label_name)
label_shape = tuple(image.shape[:2]) + (len(CLASSES), )
label = np.zeros(label_shape, dtype=np.uint8)
with open(label_path, "r") as f:
annotations = json.load(f)["annotations"]
for ann in annotations:
c = ann["label"]
class_ind = CLASS2IND[c]
points = np.array(ann["points"])
class_label = np.zeros(image.shape[:2], dtype=np.uint8)
cv2.fillPoly(class_label, [points], 1)
label[..., class_ind] = class_label
# bbox 정보에 맞게 crop하기
if image_name in self.bbox_data:
bbox = self.bbox_data[image_name] # CSV에서 가져온 bbox
x_min, y_min, x_max, y_max = bbox
image = image[y_min:y_max, x_min:x_max]
label = label[y_min:y_max, x_min:x_max, :]
else:
print(f"None image_name : {image_name}")
if self.transforms is not None:
transformed = self.transforms(image=image, mask=label)
image = transformed["image"]
label = transformed["mask"]
image = image.transpose(2, 0, 1)
label = label.transpose(2, 0, 1)
image = torch.from_numpy(image).float()
label = torch.from_numpy(label).float()
return image, label
class RoiXRayInferenceDataset(Dataset):
def __init__(self, image_root, csv_file, transforms=None):
self.image_root = image_root
self.transforms = transforms
self.filenames = self._get_image_files()
# CSV 파일에서 bbox 정보를 읽어오기
self.bbox_data = self._load_bbox_data(csv_file)
def _load_bbox_data(self, csv_file):
"""CSV 파일에서 bbox 데이터를 로드"""
bbox_df = pd.read_csv(csv_file)
bbox_dict = {}
for idx, row in bbox_df.iterrows():
image_name = row['image_name']
bbox = eval(row['bbox']) # bbox는 문자열 형태이므로 파싱
bbox_dict[image_name] = bbox
return bbox_dict
def _get_image_files(self):
return sorted({
os.path.relpath(os.path.join(root, fname), start=self.image_root)
for root, _dirs, files in os.walk(self.image_root)
for fname in files
if os.path.splitext(fname)[1].lower() == ".png"
})
def __len__(self):
return len(self.filenames)
def __getitem__(self, item):
image_name = self.filenames[item]
image_path = os.path.join(self.image_root, image_name)
image = cv2.imread(image_path)
if image is None:
print(f"이미지를 불러올 수 없습니다: {image_path}")
image = np.zeros((2048, 2048, 3), dtype=np.uint8)
image = image / 255.
# bbox 정보에 맞게 crop하기
if image_name in self.bbox_data:
bbox = self.bbox_data[image_name] # CSV에서 가져온 bbox
x_min, y_min, x_max, y_max = bbox
image = image[y_min:y_max, x_min:x_max]
else:
print(f"None image_name : {image_name}")
if self.transforms is not None:
transformed = self.transforms(image=image)
image = transformed["image"]
image = image.transpose(2, 0, 1)
image = torch.from_numpy(image).float()
return image, image_name