-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess_digestpath.py
312 lines (254 loc) · 13.4 KB
/
process_digestpath.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
# Credit: This code is modified from the original code {https://github.com/PathologyFoundation/plip/blob/main/reproducibility/generate_validation_datasets}
# =============================================================================
import os, platform, sys
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from tqdm import tqdm
from PIL import Image, ImageFile
Image.MAX_IMAGE_PIXELS = None
ImageFile.LOAD_TRUNCATED_IMAGES = True
import copy
import json
import argparse
opj = os.path.join
machine_name = list(platform.uname())[1]
print("Currently working on %s Machine" % machine_name)
workdir = os.getcwd()
assert workdir.endswith('digestpath'), f"Please make sure this script is in main 'digestpath' dataset directory and run it from the 'digestpath' directory. Current working directory is: {workdir}"
def background_ratio(rgb,
threshold=200
):
bg_mask = (rgb[..., 0] >= threshold) & (rgb[..., 1] >= threshold) & (rgb[..., 2] >= threshold)
bg_pixel_count = np.sum(bg_mask)
ratio = bg_pixel_count / (rgb.shape[0]*rgb.shape[1])
return ratio
def random_crop(img,
msk=None,
downsample=1,
cropsize=224,
crop_overlap=0.1,
non_bg_threshold=0.5,
):
new_size = (int(np.round(img.size[0]/downsample)), int(np.round(img.size[1]/downsample)))
img = img.resize(new_size)
if img.size[0] < cropsize or img.size[1] < cropsize:
return None, None
img_np = np.array(img)
if msk is not None:
msk = msk.resize(new_size, Image.Resampling.NEAREST)
msk_np = np.array(msk)
'''
For some reason, the mask is not binary images.
Probably due to jpg compression. So we need to manually binarize the image.
So, whatever pixel value <= 10, we will consider it as 0.
'''
msk_np = (msk_np > 10).astype(int)
x_list = np.arange(0, img_np.shape[0], cropsize*(1-crop_overlap)).astype(int)
y_list = np.arange(0, img_np.shape[1], cropsize*(1-crop_overlap)).astype(int)
imgs_all = []
tumor_to_patch_ratio_all = []
tumor_to_tissue_ratio_all = []
tissue_ratio_all = []
for x1 in x_list:
for y1 in y_list:
x2, y2 = x1+cropsize, y1+cropsize
if x2 >= img_np.shape[0] or y2 >= img_np.shape[1]: continue
img_patch_np = img_np[x1:x2, y1:y2, :]
if msk is not None:
msk_patch_np = msk_np[x1:x2, y1:y2]
bg_ratio = background_ratio(img_patch_np)
tissue_ratio = 1-bg_ratio
if tissue_ratio < non_bg_threshold: continue
if msk is not None:
tumor_to_patch_ratio = np.sum(msk_patch_np > 0) / (msk_patch_np.shape[0] * msk_patch_np.shape[1])
tumor_to_tissue_ratio = np.sum(msk_patch_np > 0) / (msk_patch_np.shape[0] * msk_patch_np.shape[1] * tissue_ratio)
else:
# negative images
tumor_to_patch_ratio = 0
tumor_to_tissue_ratio = 0
imgs_all.append(img_patch_np)
tissue_ratio_all.append(tissue_ratio)
tumor_to_patch_ratio_all.append(tumor_to_patch_ratio)
tumor_to_tissue_ratio_all.append(tumor_to_tissue_ratio)
if len(imgs_all) == 0:
return None, None
imgs_all = np.stack(imgs_all)
df_stat = pd.DataFrame(np.c_[tissue_ratio_all, tumor_to_patch_ratio_all, tumor_to_tissue_ratio_all],
columns=['tissue_ratio','tumor_to_patch_ratio','tumor_to_tissue_ratio'])
df_stat['downsample'] = downsample
df_stat['cropsize'] = cropsize
df_stat['crop_overlap'] = crop_overlap
df_stat['non_bg_threshold'] = non_bg_threshold
return imgs_all, df_stat
def run_step_1_get_images(cropsize, crop_overlap, non_bg_threshold, downsample_list, resultdir):
# =============================================================================
# Positives
# =============================================================================
print('Getting positives ...')
list_of_imgs = np.sort([v for v in os.listdir(path2pos) if not v.endswith('_mask.jpg')])
all_pos_imgs = None
all_pos_stat = None
for fname_img in tqdm(list_of_imgs):
uniq_filename = fname_img.replace('.jpg', '')
fname_mask = fname_img.replace('.jpg','_mask.jpg')
img_source = Image.open(opj(path2pos, fname_img))
msk_source = Image.open(opj(path2pos, fname_mask))
this_file_imgs = None
this_file_stat = None
for downsample in downsample_list:
imgs, stat = None, None
imgs, stat = random_crop(img_source, msk_source, downsample, cropsize, crop_overlap, non_bg_threshold)
if imgs is None: continue
stat['filename'] = uniq_filename
stat['downsample'] = downsample
assert len(imgs) == len(stat)
if this_file_imgs is None:
this_file_imgs = imgs
this_file_stat = stat
else:
this_file_imgs = np.concatenate([this_file_imgs, imgs], axis=0)
this_file_stat = pd.concat([this_file_stat, stat], axis=0)
assert len(this_file_imgs) == len(this_file_stat)
if this_file_imgs is not None:
if all_pos_imgs is None:
all_pos_imgs = this_file_imgs
all_pos_stat = this_file_stat
else:
all_pos_imgs = np.concatenate([all_pos_imgs, this_file_imgs], axis=0)
all_pos_stat = pd.concat([all_pos_stat, this_file_stat], axis=0)
assert len(all_pos_imgs) == len(all_pos_stat)
all_pos_stat = all_pos_stat.reset_index(drop=True)
all_pos_stat['from'] = 'tissue-train-pos-v1'
np.save(opj(resultdir, 'imgs_from_pos_v1.npy'), all_pos_imgs)
all_pos_stat.to_csv(opj(resultdir, 'stat_from_pos_v1.csv'))
# =============================================================================
# Positives
# =============================================================================
print('Getting negatives ...')
list_of_imgs = np.sort([v for v in os.listdir(path2neg) if not v.endswith('_mask.jpg')])
all_neg_imgs = None
all_neg_stat = None
for fname_img in tqdm(list_of_imgs):
uniq_filename = fname_img.replace('.jpg', '')
img_source = Image.open(opj(path2neg, fname_img))
msk_source = None
this_file_imgs = None
this_file_stat = None
for downsample in downsample_list:
imgs, stat = None, None
imgs, stat = random_crop(img_source, msk_source, downsample, cropsize, crop_overlap, non_bg_threshold)
if imgs is None: continue
stat['filename'] = uniq_filename
stat['downsample'] = downsample
assert len(imgs) == len(stat)
if this_file_imgs is None:
this_file_imgs = imgs
this_file_stat = stat
else:
this_file_imgs = np.concatenate([this_file_imgs, imgs], axis=0)
this_file_stat = pd.concat([this_file_stat, stat], axis=0)
if this_file_imgs is not None:
if all_neg_imgs is None:
all_neg_imgs = this_file_imgs
all_neg_stat = this_file_stat
else:
all_neg_imgs = np.concatenate([all_neg_imgs, this_file_imgs], axis=0)
all_neg_stat = pd.concat([all_neg_stat, this_file_stat], axis=0)
all_neg_stat = all_neg_stat.reset_index(drop=True)
all_neg_stat['from'] = 'tissue-train-neg'
np.save(opj(resultdir, 'imgs_from_neg.npy'), all_neg_imgs)
all_neg_stat.to_csv(opj(resultdir, 'stat_from_neg.csv'))
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('--step', default=1, type=int, choices=[1,2,3])
return parser.parse_args()
if __name__=='__main__':
args = parse_args()
step = args.step
# dd = opj(workdir, 'data_validation', 'DigestPath2019', 'Colonoscopy_tissue_segment_dataset')
dd = workdir
path2neg = opj(dd, 'tissue-train-neg')
path2pos = opj(dd, 'tissue-train-pos-v1')
# =============================================================================
# Hyperparameters
# =============================================================================
cropsize = 224
crop_overlap = 0.1
non_bg_threshold = 0.5
downsample_list = [2, 4, 8, 16, 32]
tumor2patch_ratio_threshold = 0.5
step_1_resultdir = opj(dd, 'processed',
'cropsize=%d_overlap=%.2f_nonbgthreshold=%.2f_downsamplelist=%s' % \
(cropsize, crop_overlap, non_bg_threshold, '_'.join(str(x) for x in downsample_list)),
'step_1'
)
step_2_resultdir = opj(dd, 'processed',
'cropsize=%d_overlap=%.2f_nonbgthreshold=%.2f_downsamplelist=%s' % \
(cropsize, crop_overlap, non_bg_threshold, '_'.join(str(x) for x in downsample_list)),
'step_2_tumor2patch_ratio_threshold=%.2f' % tumor2patch_ratio_threshold,
)
if step == 1:
# =============================================================================
# Run step 1
# =============================================================================
os.makedirs(step_1_resultdir, exist_ok=True)
run_step_1_get_images(cropsize, crop_overlap, non_bg_threshold, downsample_list, step_1_resultdir)
elif step == 2:
# =============================================================================
# Run step 2
# =============================================================================
'''
You can clear memory right here.
'''
os.makedirs(step_2_resultdir, exist_ok=True)
# step 2: prepare dataset
imgs_from_neg = np.load(opj(step_1_resultdir, 'imgs_from_neg.npy'))
stat_from_neg = pd.read_csv(opj(step_1_resultdir, 'stat_from_neg.csv'), index_col=0)
imgs_from_pos_v1 = np.load(opj(step_1_resultdir, 'imgs_from_pos_v1.npy'))
stat_from_pos_v1 = pd.read_csv(opj(step_1_resultdir, 'stat_from_pos_v1.csv'), index_col=0)
pos_index = stat_from_pos_v1['tumor_to_patch_ratio'].values >= tumor2patch_ratio_threshold
neg_index = stat_from_pos_v1['tumor_to_patch_ratio'].values == 0
print('%d negative patches from pos_v1.' % np.sum(neg_index))
print('%d positive patches (tumor2patch_ratio >= %.2f) from pos_v1.' % (np.sum(pos_index), tumor2patch_ratio_threshold))
final_negative_images = np.concatenate([imgs_from_neg, imgs_from_pos_v1[neg_index, ...]], axis=0)
final_negative_stats = pd.concat([stat_from_neg, stat_from_pos_v1.loc[neg_index]], axis=0).reset_index(drop=True)
final_positive_images = imgs_from_pos_v1[pos_index, ...]
final_positive_stats = stat_from_pos_v1.loc[pos_index]
print('Finally, %d of negative images and %d of positive images' % (len(final_negative_stats), len(final_positive_stats)))
#raise Exception()
np.save(opj(step_2_resultdir, 'final_negative_images.npy'), final_negative_images)
final_negative_stats.to_csv(opj(step_2_resultdir, 'final_negative_stats.csv'))
np.save(opj(step_2_resultdir, 'final_positive_images.npy'), final_positive_images)
final_positive_stats.to_csv(opj(step_2_resultdir, 'final_positive_stats.csv'))
elif step == 3:
# =============================================================================
# Run step 3: Convert npy files to png images.
# =============================================================================
'''
You can clear memory right here.
'''
print('Run step 3: Convert npy files to png images.')
imgs_from_neg = np.load(opj(step_2_resultdir, 'final_negative_images.npy'))
imgs_from_pos_v1 = np.load(opj(step_2_resultdir, 'final_positive_images.npy'))
stat_from_neg = pd.read_csv(opj(step_2_resultdir, 'final_negative_stats.csv'), index_col=0)
stat_from_pos_v1 = pd.read_csv(opj(step_2_resultdir, 'final_positive_stats.csv'), index_col=0)
# unstack negatives
png_savedir_neg = opj(step_2_resultdir, 'images', 'negative')
os.makedirs(png_savedir_neg, exist_ok=True)
for i in tqdm(range(len(imgs_from_neg))):
img_np = imgs_from_neg[i, ...]
filename = stat_from_neg.iloc[i]['filename']
downsample = stat_from_neg.iloc[i]['downsample']
img = Image.fromarray(img_np)
img.save(opj(png_savedir_neg, '%s_downsample=%d_%05d.png' % (filename, downsample, i)))
# unstack positives
png_savedir_pos = opj(step_2_resultdir, 'images', 'positive')
os.makedirs(png_savedir_pos, exist_ok=True)
for i in tqdm(range(len(imgs_from_pos_v1))):
img_np = imgs_from_pos_v1[i, ...]
filename = stat_from_pos_v1.iloc[i]['filename']
downsample = stat_from_pos_v1.iloc[i]['downsample']
img = Image.fromarray(img_np)
img.save(opj(png_savedir_pos, '%s_downsample=%d_%05d.png' % (filename, downsample, i)))
print('All done.')