This repository has been archived by the owner on Sep 10, 2021. It is now read-only.
forked from lindawangg/COVID-Net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.py
303 lines (250 loc) · 9.59 KB
/
data.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
import tensorflow as tf
from tensorflow import keras
import numpy as np
import os
import cv2
from tensorflow.keras.preprocessing.image import ImageDataGenerator
def _process_csv_file(file):
with open(file, 'r') as fr:
files = fr.readlines()
return files
class BalanceCovidDataset(keras.utils.Sequence):
'Generates data for Keras'
def __init__(
self,
data_dir,
csv_file,
is_training=True,
batch_size=8,
input_shape=(224, 224),
n_classes=3,
num_channels=3,
mapping={
'normal': 0,
'pneumonia': 1,
'COVID-19': 2
},
shuffle=True,
augmentation=True,
covid_percent=0.3,
class_weights=[1., 1., 6.]
):
'Initialization'
self.datadir = data_dir
self.dataset = _process_csv_file(csv_file)
self.is_training = is_training
self.batch_size = batch_size
self.N = len(self.dataset)
self.input_shape = input_shape
self.n_classes = n_classes
self.num_channels = num_channels
self.mapping = mapping
self.shuffle = True
self.covid_percent = covid_percent
self.class_weights = class_weights
self.n = 0
if augmentation:
self.augmentation = ImageDataGenerator(
featurewise_center=False,
featurewise_std_normalization=False,
rotation_range=10,
width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=True,
brightness_range=(0.9, 1.1),
zoom_range=(0.85, 1.15),
fill_mode='constant',
cval=0.,
)
datasets = {'normal': [], 'pneumonia': [], 'COVID-19': []}
for l in self.dataset:
datasets[l.split()[-1]].append(l)
self.datasets = [
datasets['normal'] + datasets['pneumonia'],
datasets['COVID-19'],
]
print(len(self.datasets[0]), len(self.datasets[1]))
self.on_epoch_end()
def __next__(self):
# Get one batch of data
batch_x, batch_y, weights = self.__getitem__(self.n)
# Batch index
self.n += 1
# If we have processed the entire dataset then
if self.n >= self.__len__():
self.on_epoch_end
self.n = 0
return batch_x, batch_y, weights
def __len__(self):
return int(np.ceil(len(self.datasets[0]) / float(self.batch_size)))
def on_epoch_end(self):
'Updates indexes after each epoch'
if self.shuffle == True:
for v in self.datasets:
np.random.shuffle(v)
def __getitem__(self, idx):
batch_x, batch_y = np.zeros(
(self.batch_size, *self.input_shape,
self.num_channels)), np.zeros(self.batch_size)
batch_files = self.datasets[0][idx * self.batch_size:(idx + 1) *
self.batch_size]
# upsample covid cases
covid_size = max(int(len(batch_files) * self.covid_percent), 1)
covid_inds = np.random.choice(np.arange(len(batch_files)),
size=covid_size,
replace=False)
covid_files = np.random.choice(self.datasets[1],
size=covid_size,
replace=False)
for i in range(covid_size):
batch_files[covid_inds[i]] = covid_files[i]
for i in range(len(batch_files)):
sample = batch_files[i].split()
if self.is_training:
folder = 'train'
else:
folder = 'test'
x = cv2.imread(os.path.join(self.datadir, folder, sample[1]))
h, w, c = x.shape
x = x[int(h/6):, :]
x = cv2.resize(x, self.input_shape)
if self.is_training and hasattr(self, 'augmentation'):
x = self.augmentation.random_transform(x)
x = x.astype('float32') / 255.0
y = self.mapping[sample[2]]
batch_x[i] = x
batch_y[i] = y
class_weights = self.class_weights
weights = np.take(class_weights, batch_y.astype('int64'))
return batch_x, keras.utils.to_categorical(batch_y, num_classes=self.n_classes), weights
class BalanceDataGenerator(keras.utils.Sequence):
'Generates data for Keras'
def __init__(self,
dataset,
is_training=True,
batch_size=8,
input_shape=(224,224),
n_classes=3,
num_channels=3,
mapping={'normal': 0, 'pneumonia': 1, 'COVID-19': 2},
shuffle=True,
augmentation=True,
datadir='data',
class_weights=[1., 1., 25.]
):
'Initialization'
self.datadir = datadir
self.dataset = dataset
self.is_training = is_training
self.batch_size = batch_size
self.N = len(self.dataset)
self.input_shape = input_shape
self.n_classes = n_classes
self.num_channels = num_channels
self.mapping = mapping
self.shuffle = True
self.n = 0
self.class_weights = class_weights
if augmentation:
self.augmentation = ImageDataGenerator(
featurewise_center=False,
featurewise_std_normalization=False,
rotation_range=10,
width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=True,
brightness_range=(0.9, 1.1),
fill_mode='constant',
cval=0.,
)
datasets = {'normal': [], 'pneumonia': [], 'COVID-19': []}
for l in dataset:
datasets[l.split()[-1]].append(l)
self.datasets = [
datasets['normal'] + datasets['pneumonia'],
datasets['COVID-19'],
]
print(len(self.datasets[0]), len(self.datasets[1]))
self.on_epoch_end()
def __next__(self):
# Get one batch of data
batch_x, batch_y, weights = self.__getitem__(self.n)
# Batch index
self.n += 1
# If we have processed the entire dataset then
if self.n >= self.__len__():
self.on_epoch_end
self.n = 0
return batch_x, batch_y, weights
def __len__(self):
return int(np.ceil(len(self.datasets[0]) / float(self.batch_size)))
def on_epoch_end(self):
'Updates indexes after each epoch'
if self.shuffle == True:
for v in self.datasets:
np.random.shuffle(v)
def __getitem__(self, idx):
batch_x, batch_y = np.zeros((self.batch_size, *self.input_shape, self.num_channels)), np.zeros(self.batch_size)
batch_files = self.datasets[0][idx*self.batch_size : (idx+1)*self.batch_size]
batch_files[np.random.randint(self.batch_size)] = np.random.choice(self.datasets[1])
for i in range(self.batch_size):
sample = batch_files[i].split()
if self.is_training:
folder = 'train'
else:
folder = 'test'
x = cv2.imread(os.path.join(self.datadir, folder, sample[1]))
h, w, c = x.shape
x = x[int(h/6):, :]
x = cv2.resize(x, self.input_shape)
if self.is_training and hasattr(self, 'augmentation'):
x = self.augmentation.random_transform(x)
x = x.astype('float32') / 255.0
y = self.mapping[sample[2]]
batch_x[i] = x
batch_y[i] = y
weights = np.take(self.class_weights, batch_y.astype('int64'))
return batch_x, keras.utils.to_categorical(batch_y, num_classes=self.n_classes), weights
class DataGenerator(keras.utils.Sequence):
'Generates data for Keras'
def __init__(self,
dataset,
is_training=True,
batch_size=8,
input_shape=(224,224),
n_classes=3,
num_channels=3,
mapping={'normal': 0, 'pneumonia': 1, 'COVID-19': 2},
shuffle=True):
'Initialization'
self.dataset = dataset
self.is_training = is_training
self.batch_size = batch_size
self.N = len(self.dataset)
self.input_shape = input_shape
self.n_classes = n_classes
self.num_channels = num_channels
self.mapping = mapping
self.shuffle = True
self.on_epoch_end()
def __len__(self):
return int(np.ceil(self.N / float(self.batch_size)))
def on_epoch_end(self):
self.dataset = shuffle(self.dataset, random_state=0)
def __getitem__(self, idx):
batch_x, batch_y = np.zeros((self.batch_size, *self.input_shape, self.num_channels)), np.zeros(self.batch_size)
for i in range(self.batch_size):
index = min((idx * self.batch_size) + i, self.N-1)
sample = self.dataset[index].split()
if self.is_training:
folder = 'train'
else:
folder = 'test'
x = cv2.imread(os.path.join('data', folder, sample[1]))
x = cv2.resize(x, self.input_shape)
x = x.astype('float32') / 255.0
#y = int(sample[1])
y = self.mapping[sample[2]]
batch_x[i] = x
batch_y[i] = y
return batch_x, keras.utils.to_categorical(batch_y, num_classes=self.n_classes)