-
Notifications
You must be signed in to change notification settings - Fork 9
/
XrayDataLoader.py
187 lines (143 loc) · 5.72 KB
/
XrayDataLoader.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
# ==============================================================================
# Copyright (C) 2020 Kevin Leung, Bofei Zhang, Jimin Tan, Yiqiu Shen,
# Krzysztof J. Geras, James S. Babb, Kyunghyun Cho, Gregory Chang, Cem M. Deniz
#
# This file is part of oai-xray-tkr-klg
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
# ==============================================================================
import os
from skimage import io
import torch
from torchvision import transforms
import torchvision
from skimage import color
import pandas as pd
from torch.utils.data import Dataset
import h5py
import numpy as np
import scipy.ndimage as ndimage
class XrayDataset(Dataset):
def __init__(self, csv_file, root_dir, transform=None):
"""
Args:
csv_file (string): Path to the csv file filename information.
root_dir (string): Directory with all the images.
transform (callable, optional): Optional transform to be applied
on a sample.
"""
self.data_frame = pd.read_csv(csv_file)
self.root_dir = root_dir
self.transform = transform
def __len__(self):
return len(self.data_frame)
def __getitem__(self, idx):
img_name = os.path.join(self.root_dir,
self.data_frame.iloc[idx, self.data_frame.columns.get_loc('h5Name')])
f = h5py.File(img_name, 'r')
image = f.get('data').value
image = image[...,np.newaxis]
f.close()
image_class = self.data_frame.iloc[idx, self.data_frame.columns.get_loc('Label')]
patientID = self.data_frame.iloc[idx, self.data_frame.columns.get_loc('ID')]
kneeSide = self.data_frame.iloc[idx, self.data_frame.columns.get_loc('Side')]
klgrade = self.data_frame.iloc[idx, self.data_frame.columns.get_loc('KLG')]
if self.transform:
image = self.transform(image)
sample = {'x': image, 'y': image_class, 'id': patientID, 'side': kneeSide, 'kl': klgrade}
return sample
class RandomCrop(object):
"""Crop randomly the image in a sample.
Args:
output_size (tuple or int): Desired output size. If int, square crop
is made.
"""
def __init__(self, output_size):
assert isinstance(output_size, (int, tuple))
if isinstance(output_size, int):
self.output_size = (output_size, output_size)
else:
assert len(output_size) == 2
self.output_size = output_size
def __call__(self, sample):
image = sample
h, w = image.shape[:2]
new_h, new_w = self.output_size
top = np.random.randint(0, h - new_h)
left = np.random.randint(0, w - new_w)
image = image[top: top + new_h,
left: left + new_w]
return image
class CenterCrop(object):
"""Center Crop the image in a sample.
Args:
output_size (tuple or int): Desired output size. If int, square crop
is made.
"""
def __init__(self, output_size):
assert isinstance(output_size, (int, tuple))
if isinstance(output_size, int):
self.output_size = (output_size, output_size)
else:
assert len(output_size) == 2
self.output_size = output_size
def __call__(self, sample):
image = sample
h, w = image.shape[:2]
new_h, new_w = self.output_size
x = (h - new_h) // 2
y = (w - new_w) // 2
image = image[y:(y + new_h),x:(x + new_w)]
return image
class Resize(object):
def __init__(self, output_size):
assert isinstance(output_size, (int, tuple))
if isinstance(output_size, int):
self.output_size = (output_size, output_size)
else:
assert len(output_size) == 2
self.output_size = output_size
def __call__(self, sample):
image = sample
h, w = image.shape[:2]
new_h, new_w = self.output_size
image_array_rescaled = ndimage.zoom(image, [new_h/h, new_w/w, 1])
return image_array_rescaled
class RandomHorizontalFlip(object):
def __call__(self, sample):
image = sample
step = np.random.choice([1,-1])
if step == -1:
image = torch.flip(image,[2]) # fliplr on the width axis of C x H x W Tensor
return image
class ToTensor(object):
def __call__(self, sample):
image = sample
# swap color axis because
# numpy image: H x W x C
# torch image: C X H X W
image = image.transpose((2, 0, 1))
return torch.from_numpy(image)
class ToRGB(object):
def __call__(self, sample):
image = np.tile(sample,3)
return image
class Identity(object):
def __call__(self, sample):
return sample
class Normalize(object):
def __call__(self, data):
new_data = np.empty([data.shape[0], data.shape[1], data.shape[2]], dtype = np.float64)
for i in range(data.shape[0]):
new_data[i,:,:] = data[i,:,:] - np.amin(data[i,:,:])
new_data[i,:,:] /= np.amax([np.amax(data[i,:,:])- np.amin(data[i,:,:]),1e-8])
return new_data