-
Notifications
You must be signed in to change notification settings - Fork 2
/
extract_reconstructions.py
286 lines (220 loc) · 9.75 KB
/
extract_reconstructions.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
import argparse
import glob
import os.path
import pickle
import cv2 as cv
import matplotlib.pyplot as plt
import numpy as np
import torch
from skimage.transform import resize
from tqdm import tqdm
import models_mae
DATASETS = ['brats', 'luna16_unnorm']
def prepare_model(chkpt_dir, arch='mae_vit_large_patch16'):
# build model
img_size = 64
patch_size = 16
if args.dataset == 'brats':
img_size = 224
patch_size = 16
model = getattr(models_mae, arch)(img_size=img_size, patch_size=patch_size)
checkpoint = torch.load(chkpt_dir)
msg = model.load_state_dict(checkpoint['model'], strict=False)
print(msg)
model = model.to('cuda')
return model
def apply_gaussian_filter(image):
sigma = np.random.random() * 3 + 2
image = cv.GaussianBlur(image, (5, 5), sigma)
return image
def degrade_reconstruction(img_, apply_blur):
if args.dataset == 'brats':
h = np.random.randint(10, 40) # max H
w = np.random.randint(10, 40) # max w
elif args.dataset == 'luna16_unnorm':
h = np.random.randint(5, 12) # max H
w = np.random.randint(5, 12) # max W
else:
raise ValueError(f'Dataset {args.dataset} not recognized!')
start_x = np.random.randint(img_.shape[1] - w)
start_y = np.random.randint(img_.shape[0] - h)
# print(h, w, ratio)
end_x = start_x + w
end_y = start_y + h
if apply_blur:
img_[start_y: end_y, start_x: end_x, 0] = apply_gaussian_filter(img_[start_y: end_y, start_x: end_x, 0])
else:
ratio = np.random.random()
img_[start_y: end_y, start_x: end_x] *= ratio
return img_
def apply_degradation(img_, num_iter, apply_blur):
for _ in range(num_iter):
img_ = degrade_reconstruction(img_, apply_blur)
return img_
def get_reconstructions(model_, imgs_, idx):
x = torch.tensor(imgs_)
x = torch.einsum('nhwc->nchw', x)
x = x.to('cuda')
loss, result, mask = model_(x.float(), mask_ratio=args.mask_ratio, idx_masking=idx, is_testing=False)
result = model_.unpatchify(result)
result = torch.einsum('nchw->nhwc', result).detach().cpu()
# visualize the mask
mask = mask.detach()
mask = mask.unsqueeze(-1).repeat(1, 1, model_.patch_embed.patch_size[0]**2 * 1) # (N, H*W, p*p*3)
mask = model_.unpatchify(mask) # 1 is removing, 0 is keeping
mask = torch.einsum('nchw->nhwc', mask).detach().cpu()
# MAE reconstruction pasted with visible patches
im_paste = torch.einsum('nchw->nhwc', x).detach().cpu() * (1 - mask) + result * mask
return im_paste.numpy()
def get_reconstructions_multi(model_, imgs_):
num_fwd = args.num_trials
results = None
for idx in range(num_fwd):
result = get_reconstructions(model_, imgs_, idx)
if results is None:
results = result
else:
results += result
results = results / num_fwd
return results
# change it to match your own path.
def get_normal_images_paths_train():
if args.dataset == 'luna16_unnorm':
return glob.glob('/media/lili/SSD2/datasets/luna16/luna16/train_unnorm/normal/*.npy')
elif args.dataset == 'brats':
return glob.glob('/media/lili/SSD2/datasets/brats/BraTS2020_training_data/split/train/normal/*.npy')
else:
raise ValueError(f'Data set {args.dataset} not recognized.')
# change it to match your own path.
def get_normal_images_paths():
if args.dataset == 'luna16_unnorm':
if args.use_val:
return glob.glob('/media/lili/SSD2/datasets/luna16/luna16/val_unnorm/normal/*.npy')
else:
return glob.glob('/media/lili/SSD2/datasets/luna16/luna16/test_unnorm/normal/*.npy')
elif args.dataset == 'brats':
if args.use_val:
return glob.glob('/media/lili/SSD2/datasets/brats/BraTS2020_training_data/split/val/normal/*.npy')
else:
return glob.glob('/media/lili/SSD2/datasets/brats/BraTS2020_training_data/split/test/normal/*.npy')
else:
raise ValueError(f'Data set {args.dataset} not recognized.')
# change it to match your own path.
def get_abnormal_images_paths():
if args.dataset == 'luna16_unnorm':
if args.use_val:
return glob.glob('/media/lili/SSD2/datasets/luna16/luna16/val_unnorm/abnormal/*.npy')
else:
return glob.glob('/media/lili/SSD2/datasets/luna16/luna16/test_unnorm/abnormal/*.npy')
elif args.dataset == 'brats':
if args.use_val:
return glob.glob('/media/lili/SSD2/datasets/brats/BraTS2020_training_data/split/val/abnormal/*.npy')
else:
return glob.glob('/media/lili/SSD2/datasets/brats/BraTS2020_training_data/split/test/abnormal/*.npy')
else:
raise ValueError(f'Data set {args.dataset} not recognized.')
def load_image(img_path_):
image_np = np.float32(np.load(img_path_))
if args.dataset == 'brats':
image_np = image_np[:, :, 0]
image_np = np.expand_dims(image_np, axis=2)
return image_np
def process_image(img_):
if args.dataset == 'brats':
mean_ = np.array([0.])
std_ = np.array([1.])
elif args.dataset == 'luna16_unnorm':
mean_ = np.array([0.])
std_ = np.array([100.])
else:
raise ValueError(f'Data set {args.dataset} not recognized.')
img_ = img_ - mean_
img_ = img_ / std_
return img_
def visualize(imgs_, reconstructions_, old_reconstructions_, paths_):
num_imgs = 5
for (img_, recon_, old_recon_, path_) in zip(imgs_, reconstructions_, old_reconstructions_, paths_):
plt.subplot(1, num_imgs, 1)
plt.imshow(img_, cmap='gray')
plt.subplot(1, num_imgs, 2)
plt.imshow(recon_, cmap='gray')
plt.subplot(1, num_imgs, 3)
plt.imshow(old_recon_, cmap='gray')
plt.subplot(1, num_imgs, 4)
plt.imshow(np.abs(img_ - recon_), cmap='gray')
plt.subplot(1, num_imgs, 5)
plt.imshow(np.abs(old_recon_ - recon_), cmap='gray')
plt.show()
def save(imgs, reconstructions, used_paths, is_abnormal, iter_):
base_dir = args.output_folder
if is_abnormal:
base_dir = os.path.join(base_dir, 'abnormal')
else:
base_dir = os.path.join(base_dir, 'normal')
for (img_, recon_, path_) in zip(imgs, reconstructions, used_paths):
if is_abnormal and img_.sum() == 0:
continue
info_ = {'img': img_, 'recon': recon_}
short_filename = os.path.split(path_)[-1][:-4] + f'_{iter_}.pkl'
with open(os.path.join(base_dir, short_filename), 'wb') as handle:
pickle.dump(info_, handle)
def write_reconstructions(model_mae, paths, is_abnormal: bool = False, iter_: int = 0):
for start_index in tqdm(range(0, len(paths), args.batch_size)):
imgs = []
used_paths = []
for idx_path in range(start_index, start_index + args.batch_size):
if idx_path < len(paths):
path_ = paths[idx_path]
img_ = load_image(path_)
if args.dataset == 'brats':
img_ = resize(img_, (224, 224), order=3) # 3: Bi-cubic
else:
img_ = resize(img_, (64, 64), order=3) # 3: Bi-cubic
img_ = process_image(img_)
imgs.append(img_)
used_paths.append(path_)
imgs = np.array(imgs, np.float32)
old_reconstructions = get_reconstructions_multi(model_mae, imgs)
if is_abnormal and args.test is False:
reconstructions = [apply_degradation(recon.copy(), num_iter=np.random.randint(1, 10), apply_blur=False) for recon in old_reconstructions]
else:
reconstructions = old_reconstructions
# visualize(imgs, reconstructions, old_reconstructions, used_paths)
save(imgs, reconstructions, used_paths, is_abnormal, iter_=iter_)
def load_model(model_path):
model_mae = prepare_model(model_path, 'mae_vit_base_patch16')
return model_mae
parser = argparse.ArgumentParser(description='PyTorch Medical Images')
parser.add_argument('--model-path', type=str)
parser.add_argument('--mask-ratio', type=float)
parser.add_argument('--dataset', type=str)
parser.add_argument('--batch-size', type=int, default=1)
parser.add_argument('--output-folder', type=str, required=True)
parser.add_argument('--num-trials', type=int, default=1)
parser.add_argument('--use_val', action='store_true',
help='Test on val data.')
parser.add_argument('--test', action='store_true')
parser.set_defaults(use_val=False)
args = parser.parse_args()
assert args.dataset in DATASETS
"""
python3 extract_reconstructions.py --dataset=brats --mask-ratio=0.85 \
--model-path=mae_brats_mask_ratio_0.85/checkpoint-1599.pth --batch-size=64 --num-trials=4 \
--output-folder=/media/lili/SSD2/datasets/brats/BraTS2020_training_data/reconstructions/mae_mask_ratio_0.85/val --use_val --test
python3 extract_reconstructions.py --dataset=luna16_unnorm --mask-ratio=0.75 \
--model-path=models/mae_luna16_patch_16_mask_ratio_0.75_unnorm/checkpoint-1599.pth --batch-size=64 --num-trials=4 \
--output-folder=/media/lili/SSD2/datasets/luna16/reconstructions/mae_luna16_patch_16_mask_ratio_0.75_unnorm_3_6/train
"""
if __name__ == '__main__':
os.makedirs(os.path.join(args.output_folder, 'normal'), exist_ok=True)
os.makedirs(os.path.join(args.output_folder, 'abnormal'), exist_ok=True)
model_path = args.model_path
model_mae_ = load_model(model_path)
# Data
if args.test:
write_reconstructions(model_mae_, paths=get_normal_images_paths(), is_abnormal=False)
write_reconstructions(model_mae_, paths=get_abnormal_images_paths(), is_abnormal=True)
else:
normal_paths = get_normal_images_paths_train()
write_reconstructions(model_mae_, paths=normal_paths, is_abnormal=False)
write_reconstructions(model_mae_, paths=normal_paths, is_abnormal=True, iter_=1)