forked from pdima/kaggle_RSNA_Pneumonia_Detection
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdetection_dataset.py
277 lines (225 loc) · 9.57 KB
/
detection_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
import numpy as np
import pandas as pd
import math
import os
from torch.utils.data import Dataset
import skimage.color
import skimage.io
from tqdm import tqdm
import utils
import glob
import pickle
import pydicom
import skimage.transform
from collections import namedtuple, defaultdict
from imgaug import augmenters as iaa
import matplotlib.pyplot as plt
from config import *
class DetectionDataset(Dataset):
def __init__(self, fold, is_training, img_size, images=None, augmentation_level=10, crop_source=1024):
self.fold = fold
self.is_training = is_training
self.img_size = img_size
self.crop_source = crop_source
self.augmentation_level = augmentation_level
self.categories = ['No Lung Opacity / Not Normal', 'Normal', 'Lung Opacity']
samples = pd.read_csv('../input/stage_1_train_labels.csv')
samples = samples.merge(pd.read_csv('../input/folds.csv'), on='patientId', how='left')
if images is None:
self.images = self.load_images(samples)
else:
self.images = images
if is_training:
self.samples = samples[samples.fold != fold]
else:
self.samples = samples[samples.fold == fold]
self.patient_ids = list(sorted(self.samples.patientId.unique()))
self.patient_categories = {}
self.annotations = defaultdict(list)
for _, row in self.samples.iterrows():
patient_id = row['patientId']
self.patient_categories[patient_id] = self.categories.index(row['class'])
if row['Target'] > 0:
x, y, w, h = row.x, row.y, row.width, row.height
points = np.array([
[x, y + h / 3],
[x, y + h * 2 / 3],
[x + w, y + h / 3],
[x + w, y + h * 2 / 3],
[x + w / 3, y],
[x + w * 2 / 3, y],
[x + w / 3, y + h],
[x + w * 2 / 3, y + h],
])
self.annotations[patient_id].append(points)
def load_images(self, samples):
try:
images = pickle.load(open(f'{CACHE_DIR}/train_images.pkl', 'rb'))
except FileNotFoundError:
os.makedirs(CACHE_DIR, exist_ok=True)
images = {}
for patient_id in tqdm(list(sorted(samples.patientId.unique()))):
dcm_data = pydicom.read_file(f'{TRAIN_DIR}/{patient_id}.dcm')
img = dcm_data.pixel_array
# img = skimage.transform.resize(img, (img.shape[0] / 2, img.shape[1] / 2), anti_aliasing=True)
# img = np.clip(img*255, 0, 255).astype(np.uint8)
images[patient_id] = img
pickle.dump(images, open(f'{CACHE_DIR}/train_images.pkl', 'wb'))
return images
def load_image(self, patient_id):
if patient_id in self.images:
return self.images[patient_id]
else:
dcm_data = pydicom.read_file(f'{TRAIN_DIR}/{patient_id}.dcm')
img = dcm_data.pixel_array
self.images[patient_id] = img
return img
def num_classes(self):
return 1
def __len__(self):
return len(self.patient_ids)
def __getitem__(self, idx):
patient_id = self.patient_ids[idx]
img = self.load_image(patient_id)
if self.crop_source != 1024:
img_source_w = self.crop_source
img_source_h = self.crop_source
else:
img_source_h, img_source_w = img.shape[:2]
img_h, img_w = img.shape[:2]
augmentation_sigma = {
10: dict(scale=0.1, angle=5.0, shear=2.5, gamma=0.2, hflip=False),
15: dict(scale=0.15, angle=6.0, shear=4.0, gamma=0.2, hflip=np.random.choice([True, False])),
20: dict(scale=0.15, angle=6.0, shear=4.0, gamma=0.25, hflip=np.random.choice([True, False])),
}[self.augmentation_level]
if self.is_training:
cfg = utils.TransformCfg(
crop_size=self.img_size,
src_center_x=img_w/2 + np.random.uniform(-32, 32),
src_center_y=img_h/2 + np.random.uniform(-32, 32),
scale_x=self.img_size / img_source_w * (2 ** np.random.normal(0, augmentation_sigma['scale'])),
scale_y=self.img_size / img_source_h * (2 ** np.random.normal(0, augmentation_sigma['scale'])),
angle=np.random.normal(0, augmentation_sigma['angle']),
shear=np.random.normal(0, augmentation_sigma['shear']),
hflip=augmentation_sigma['hflip'],
vflip=False
)
else:
cfg = utils.TransformCfg(
crop_size=self.img_size,
src_center_x=img_w / 2,
src_center_y=img_h / 2,
scale_x=self.img_size / img_source_w,
scale_y=self.img_size / img_source_h,
angle=0,
shear=0,
hflip=False,
vflip=False
)
crop = cfg.transform_image(img)
if self.is_training:
crop = np.power(crop, 2.0 ** np.random.normal(0, augmentation_sigma['gamma']))
if self.augmentation_level == 20:
aug = iaa.Sequential(
[
iaa.Sometimes(0.1, iaa.CoarseSaltAndPepper(p=(0.01, 0.01), size_percent=(0.1, 0.2))),
iaa.Sometimes(0.5, iaa.GaussianBlur(sigma=(0.0, 2.0))),
iaa.Sometimes(0.5, iaa.AdditiveGaussianNoise(scale=(0, 0.04 * 255)))
]
)
crop = aug.augment_image(np.clip(np.stack([crop, crop, crop], axis=2) * 255, 0, 255).astype(np.uint8))[:,:,0].astype(np.float32) / 255.0
if self.augmentation_level == 15:
aug = iaa.Sequential(
[
iaa.Sometimes(0.25, iaa.GaussianBlur(sigma=(0.0, 1.0))),
iaa.Sometimes(0.25, iaa.AdditiveGaussianNoise(scale=(0, 0.02 * 255)))
]
)
crop = aug.augment_image(np.clip(np.stack([crop, crop, crop], axis=2) * 255, 0, 255).astype(np.uint8))[:,:,0].astype(np.float32) / 255.0
annotations = []
# print('patient_id', patient_id)
for annotation in self.annotations[patient_id]:
points = cfg.transform().inverse(annotation)
res = np.zeros((1, 5))
p0 = np.min(points, axis=0)
p1 = np.max(points, axis=0)
res[0, 0:2] = p0
res[0, 2:4] = p1
res[0, 4] = 0
annotations.append(res)
if len(annotations):
annotations = np.row_stack(annotations)
else:
annotations = np.zeros((0, 5))
sample = {'img': crop, 'annot': annotations, 'scale': 1.0, 'category': self.patient_categories[patient_id]}
return sample
def check_dataset():
with utils.timeit_context('load ds'):
ds = DetectionDataset(fold=0, is_training=True, img_size=512)
# print(ds.annotations(ds.patient_ids[0]))
# patient_id = 10056 #ds.patient_ids[0]
# plt.imshow(ds.images[patient_id])
#
# annotation_list = ds.training_samples.loc[[patient_id]]
#
# for _, row in annotation_list.iterrows():
# plt.plot(
# [row[f'p{i}_x'] for i in [1, 2, 3, 4, 1]],
# [row[f'p{i}_y'] for i in [1, 2, 3, 4, 1]],
# c='y'
# )
# plt.show()
ds.is_training = False
plt.imshow(ds[0]['img'])
plt.figure()
ds.is_training = True
for sample in ds:
plt.cla()
plt.imshow(sample['img'])
for annot in sample['annot']:
p0 = annot[0:2]
p1 = annot[2:4]
# print(p0, p1)
plt.gca().add_patch(plt.Rectangle(p0, width=(p1-p0)[0], height=(p1-p0)[1], fill=False, edgecolor='r', linewidth=2))
plt.show()
def check_augmentations():
with utils.timeit_context('load ds'):
ds = DetectionDataset(fold=0, is_training=True, img_size=512, images={}, augmentation_level=20)
sample_num = 2
ds.is_training = False
plt.imshow(ds[sample_num]['img'])
for annot in ds[sample_num]['annot']:
p0 = annot[0:2]
p1 = annot[2:4]
# print(p0, p1)
plt.gca().add_patch(
plt.Rectangle(p0, width=(p1 - p0)[0], height=(p1 - p0)[1], fill=False, edgecolor='r', linewidth=2))
plt.figure()
ds.is_training = True
for i in range(100):
sample = ds[sample_num]
plt.imshow(sample['img'])
for annot in sample['annot']:
p0 = annot[0:2]
p1 = annot[2:4]
# print(p0, p1)
plt.gca().add_patch(
plt.Rectangle(p0, width=(p1 - p0)[0], height=(p1 - p0)[1], fill=False, edgecolor='r', linewidth=2))
plt.show()
def check_performance():
import pytorch_retinanet.dataloader
import torch
with utils.timeit_context('load ds'):
ds = DetectionDataset(fold=0, is_training=True, img_size=512)
dataloader_train = torch.utils.data.DataLoader(ds, num_workers=16, batch_size=12,
shuffle=True,
collate_fn=pytorch_retinanet.dataloader.collater2d)
data_iter = tqdm(enumerate(dataloader_train), total=len(dataloader_train))
with utils.timeit_context('1000 batches:'):
for iter_num, data in data_iter:
if iter_num > 1000:
break
if __name__ == '__main__':
# check_dataset()
check_augmentations()
# check_performance()