This repository has been archived by the owner on Oct 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 152
/
Copy pathgenerators.py
227 lines (175 loc) · 9.15 KB
/
generators.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
import os
from glob import glob
import numpy as np
import pandas as pd
import h5py
import random
import _pickle as pickle
from config import *
from visual_utils import plot_middle_slices_comparison
def get_meta_dict():
cache_file = '{}/all_meta_cache.meta'.format(PREPROCESS_PATH)
if os.path.exists(cache_file):
print('get meta_dict from cache')
with open(cache_file, 'rb') as f:
return pickle.load(f)
meta_dict = {}
for f in glob('{}/*.meta'.format(PREPROCESS_PATH)):
seriesuid = f[-15:-5]
if not os.path.exists('{}/{}.h5'.format(PREPROCESS_PATH, seriesuid)):
continue
with open(f, 'rb') as f:
meta = pickle.load(f)
meta_dict[meta['seriesuid']] = meta
# cache it
with open(cache_file, 'wb') as f:
pickle.dump(meta_dict, f)
return meta_dict
def get_tumor_records():
numpy_files = glob('{}/*.h5'.format(PREPROCESS_PATH))
meta_dict = get_meta_dict()
fields = ['img_numpy_file', 'origin', 'spacing', 'shape', 'pixels', 'cover_ratio', 'process_duration']
def fill_info(seriesuid):
data = [None] * len(fields)
for f in numpy_files:
if f[-13:-3] == seriesuid:
data[0] = f
if seriesuid in meta_dict:
t = meta_dict[seriesuid]
data[1:] = [t['origin'], t['spacing'], t['shape'], t['pixels'], t['cover_ratio'], t['process_duration']]
return pd.Series(data, index=fields)
records = pd.read_csv('{}/train/annotations.csv'.format(ANNOTATIONS_PATH))
records[fields] = records['seriesuid'].apply(fill_info)
records.dropna(inplace=True)
print('tumor record size {}'.format(records.shape))
if DEBUG_ONLY_TRAIN_FINE_CUT_BIG_TUMOR_SWITCHER:
records.drop(records[records.cover_ratio < DEBUG_ONLY_TRAIN_COVER_RATIO_BIGGER_THAN].index, axis=0, inplace=True)
records.drop(records[records.diameter_mm < DEBUG_ONLY_TRAIN_TUMOR_DIAMETER_LARGER_THAN].index, axis=0, inplace=True)
print('after drop, tumor record size {}'.format(records.shape))
return records
tumor_records = get_tumor_records()
tumor_records_len = tumor_records.shape[0]
if tumor_records_len == 0:
print('no tumor records, generator cannot work')
exit()
if RANDOMIZE_RECORDS:
tumor_records = tumor_records.iloc[np.random.permutation(tumor_records_len)]
print('tumor_records_train randomized')
tumor_records_train = tumor_records[:int(tumor_records_len * TRAIN_VAL_RATIO)]
tumor_records_val = tumor_records[int(tumor_records_len * TRAIN_VAL_RATIO):]
del tumor_records, tumor_records_len
re_sample = (TRAIN_CLASSIFY_NOT_SEGMENTATION and TRAIN_CLASSIFY_ENABLE_DATA_AUGUMENTATION) \
or ((not TRAIN_CLASSIFY_NOT_SEGMENTATION) and TRAIN_SEG_ENABLE_DATA_AUGUMENTATION)
if re_sample:
r_10 = tumor_records_train[tumor_records_train.diameter_mm < 10.0]
r_30 = tumor_records_train[(tumor_records_train.diameter_mm >= 10.0) & (tumor_records_train.diameter_mm < 30.0)]
r_more = tumor_records_train[tumor_records_train.diameter_mm >= 30.0]
concats = []
for _ in range(RESAMPLE_DATA_LESS_10_RATIO):
concats.append(r_10)
for _ in range(RESAMPLE_DATA_LESS_30_RATIO):
concats.append(r_30)
concats.append(r_more)
tumor_records_train = pd.concat(concats, axis=0)
print('after resample, got {} samples'.format(tumor_records_train.shape[0]))
# random_offset works iff around_tumor=True
def get_block(record, around_tumor=True, random_offset=(0, 0, 0), shape=(INPUT_WIDTH, INPUT_HEIGHT, INPUT_DEPTH)):
with h5py.File(record['img_numpy_file'], 'r') as hf:
W, H, D = hf['img'].shape[0], hf['img'].shape[1], hf['img'].shape[2]
if around_tumor:
coord = np.array([record['coordX'], record['coordY'], record['coordZ']])
coord = np.abs((coord - record['origin']) / record['spacing'])
coord = coord + random_offset
w, h, d = int(coord[0] - shape[0] // 2), int(coord[1] - shape[1] // 2), int(coord[2] - shape[2] // 2)
w, h, d = max(w, 0), max(h, 0), max(d, 0)
w, h, d = min(w, W - shape[0] - 1), min(h, H - shape[1] - 1), min(d, D - shape[2] - 1)
else:
w, h, d = random.randint(0, W - shape[0] - 1), random.randint(0, H - shape[1] - 1), random.randint(0, D - shape[2] - 1)
block = hf['img'][w:w + shape[0], h:h + shape[1], d:d + shape[2]]
block[block==0] = np.min(hf['img'])
block = (block - MIN_BOUND) / (MAX_BOUND - MIN_BOUND)
block = np.clip(block, 0.0, 1.0)
# DenseNet paper suggests to (img-mean)/std, use this for simple
if TRAIN_CLASSIFY_NOT_SEGMENTATION and TRAIN_CLASSIFY_MODEL.lower() == 'densenet':
block = block - 0.5
return block
def plot_batch_sample(X, y=None):
assert X.shape[0] == y.shape[0]
for b in range(X.shape[0]):
plot_middle_slices_comparison([X[b, :, :, :, 0], y[b, :, :, :, 0]])
def get_seg_batch(batch_size=32, from_train=True, random_choice=False):
idx = 0
records = tumor_records_train if from_train else tumor_records_val
X = np.zeros((batch_size, INPUT_WIDTH, INPUT_HEIGHT, INPUT_DEPTH, INPUT_CHANNEL))
y = np.zeros((batch_size, INPUT_WIDTH, INPUT_HEIGHT, INPUT_DEPTH, OUTPUT_CHANNEL))
while True:
for b in range(batch_size):
if random_choice:
idx = random.randint(0, records.shape[0] - 1)
record = records.iloc[idx]
is_positive_sample = random.random() < TRAIN_SEG_POSITIVE_SAMPLE_RATIO
random_offset = np.array([0, 0, 0])
if TRAIN_SEG_ENABLE_DATA_AUGUMENTATION:
random_offset = np.array([
random.randrange(-TRAIN_SEG_SAMPLE_RANDOM_OFFSET, TRAIN_SEG_SAMPLE_RANDOM_OFFSET),
random.randrange(-TRAIN_SEG_SAMPLE_RANDOM_OFFSET, TRAIN_SEG_SAMPLE_RANDOM_OFFSET),
random.randrange(-TRAIN_SEG_SAMPLE_RANDOM_OFFSET, TRAIN_SEG_SAMPLE_RANDOM_OFFSET)
])
X[b,:,:,:,0] = get_block(record, around_tumor=is_positive_sample, random_offset=random_offset,
shape=(INPUT_WIDTH, INPUT_HEIGHT, INPUT_DEPTH))
y[b,:,:,:,0] = make_seg_mask(record, create_mask=is_positive_sample, random_offset=random_offset)
idx = idx + 1 if idx < records.shape[0] - 1 else 0
# rotate
if TRAIN_SEG_ENABLE_DATA_AUGUMENTATION:
for b in range(batch_size):
_perm = np.random.permutation(3)
X[b, :, :, :, 0] = np.transpose(X[b, :, :, :, 0], _perm)
y[b, :, :, :, 0] = np.transpose(y[b, :, :, :, 0], _perm)
if DEBUG_PLOT_WHEN_GETTING_SEG_BATCH:
plot_batch_sample(X, y)
yield X, y
def make_seg_mask(record, create_mask=True, random_offset=(0, 0, 0)):
mask = np.zeros((INPUT_WIDTH, INPUT_HEIGHT, INPUT_DEPTH))
if create_mask:
r = record['diameter_mm'] / 2 + DIAMETER_BUFFER
radius = np.array([r, r, r])
if DIAMETER_SPACING_EXPAND:
radius = radius / record['spacing']
coord = np.array([INPUT_WIDTH / 2, INPUT_HEIGHT / 2, INPUT_DEPTH / 2])
coord = coord - random_offset
radius, coord = radius.astype(np.uint16), coord.astype(np.uint16)
mask[coord[0] - radius[0]:coord[0] + radius[0] + 1,
coord[1] - radius[1]:coord[1] + radius[1] + 1,
coord[2] - radius[2]:coord[2] + radius[2] + 1] = 1.0
return mask
def get_classify_batch(batch_size=32, from_train=True, random_choice=False):
idx = 0
records = tumor_records_train if from_train else tumor_records_val
shape = (CLASSIFY_INPUT_WIDTH, CLASSIFY_INPUT_HEIGHT, CLASSIFY_INPUT_DEPTH)
positive_num = int(batch_size * TRAIN_CLASSIFY_POSITIVE_SAMPLE_RATIO)
X = np.zeros((batch_size, CLASSIFY_INPUT_WIDTH, CLASSIFY_INPUT_HEIGHT, CLASSIFY_INPUT_DEPTH, CLASSIFY_INPUT_CHANNEL))
y = np.zeros((batch_size, 2))
while True:
for b in range(positive_num):
if random_choice:
idx = random.randint(0, records.shape[0] - 1)
record = records.iloc[idx]
random_offset = np.array([0, 0, 0])
if TRAIN_CLASSIFY_ENABLE_DATA_AUGUMENTATION:
random_offset = np.array([
random.randrange(-TRAIN_CLASSIFY_SAMPLE_RANDOM_OFFSET, TRAIN_CLASSIFY_SAMPLE_RANDOM_OFFSET),
random.randrange(-TRAIN_CLASSIFY_SAMPLE_RANDOM_OFFSET, TRAIN_CLASSIFY_SAMPLE_RANDOM_OFFSET),
random.randrange(-TRAIN_CLASSIFY_SAMPLE_RANDOM_OFFSET, TRAIN_CLASSIFY_SAMPLE_RANDOM_OFFSET)
])
X[b, :, :, :, 0] = get_block(record, around_tumor=True, random_offset=random_offset, shape=shape)
y[b, 0] = 1
idx = idx + 1 if idx < records.shape[0] - 1 else 0
for b in range(positive_num, batch_size):
record = records.iloc[random.randint(0, records.shape[0] - 1)]
X[b, :, :, :, 0] = get_block(record, around_tumor=False, shape=shape)
y[b, 1] = 1
# rotate
if TRAIN_CLASSIFY_ENABLE_DATA_AUGUMENTATION:
for b in range(batch_size):
X[b, :, :, :, 0] = np.transpose(X[b, :, :, :, 0], np.random.permutation(3))
yield X, y