-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinference.py
236 lines (185 loc) · 8.01 KB
/
inference.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
"""
Inference Module for MAE-VAE Model.
This module provides functionality for running inference on NIfTI medical images using
the trained MAE-VAE model. It supports reconstruction of individual slices and entire
volumes, with visualization capabilities.
"""
import torch
import torch.nn.functional as F
from torchvision import transforms
import matplotlib.pyplot as plt
from mae_vae import MAEVAEViT
import numpy as np
from einops import rearrange
import nibabel as nib
from skimage.transform import resize
from skimage import exposure
import os
class MAEVAEInference:
"""Inference class for MAE-VAE model.
Args:
checkpoint_path (str): Path to model checkpoint.
device (torch.device, optional): Device to run inference on.
Attributes:
model (MAEVAEViT): The loaded model.
device (torch.device): Device being used.
normalize (transforms.Normalize): Normalization transform.
inverse_normalize (transforms.Normalize): Inverse normalization transform.
"""
def __init__(self, checkpoint_path, device=None):
if device is None:
self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
else:
self.device = device
checkpoint = torch.load(checkpoint_path, map_location=self.device)
self.model = MAEVAEViT(
image_size=224,
patch_size=16,
embed_dim=512,
depth=6,
num_heads=8,
decoder_dim=384,
decoder_depth=4,
mask_ratio=0.75,
latent_dim=256
).to(self.device)
self.model.load_state_dict(checkpoint['model_state_dict'])
self.model.eval()
self.normalize = transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5])
self.inverse_normalize = transforms.Normalize(
mean=[-0.5/0.5, -0.5/0.5, -0.5/0.5],
std=[1/0.5, 1/0.5, 1/0.5]
)
def preprocess_slice(self, slice_data, image_size=224):
"""Preprocess a single slice from a NIfTI file.
Args:
slice_data (numpy.ndarray): Input slice data.
image_size (int, optional): Target image size.
Returns:
torch.Tensor: Preprocessed slice tensor.
"""
slice_data = resize(slice_data, (image_size, image_size),
mode='constant', anti_aliasing=True)
slice_data = exposure.rescale_intensity(slice_data, out_range=(0, 1))
p2, p98 = np.percentile(slice_data, (2, 98))
slice_data = exposure.rescale_intensity(slice_data, in_range=(p2, p98))
slice_data = np.stack([slice_data] * 3, axis=0)
slice_tensor = torch.FloatTensor(slice_data)
slice_tensor = self.normalize(slice_tensor)
return slice_tensor
def get_slice(self, nifti_data, slice_idx, axis=2):
"""Extract a 2D slice from 3D volume.
Args:
nifti_data (numpy.ndarray): Input 3D volume.
slice_idx (int): Index of slice to extract.
axis (int, optional): Axis along which to extract slice.
Returns:
numpy.ndarray: Extracted 2D slice.
"""
if axis == 0:
slice_data = nifti_data[slice_idx, :, :]
elif axis == 1:
slice_data = nifti_data[:, slice_idx, :]
else: # axis == 2
slice_data = nifti_data[:, :, slice_idx]
return slice_data
def reconstruct_slice(self, nifti_path, slice_idx, axis=2, with_mask=True):
"""Reconstruct a specific slice from a NIfTI file.
Args:
nifti_path (str): Path to NIfTI file.
slice_idx (int): Index of slice to reconstruct.
axis (int, optional): Axis along which to extract slice.
with_mask (bool, optional): Whether to use masking.
Returns:
tuple: Original slice, reconstructed slice, and mask (if with_mask=True).
"""
img = nib.load(nifti_path)
nifti_data = img.get_fdata()
slice_data = self.get_slice(nifti_data, slice_idx, axis)
slice_tensor = self.preprocess_slice(slice_data)
slice_tensor = slice_tensor.unsqueeze(0)
with torch.no_grad():
if with_mask:
pred, mask, mu, logvar = self.model(slice_tensor.to(self.device))
else:
pred, mask, mu, logvar = self.model(slice_tensor.to(self.device), mask_ratio=0.0)
pred = pred[:, 1:, :]
patch_size = self.model.patch_size
h = w = int(np.sqrt(pred.shape[1]))
c = 3
reconstructed = rearrange(
pred,
'b (h w) (p1 p2 c) -> b c (h p1) (w p2)',
h=h, w=w, p1=patch_size, p2=patch_size, c=c
)
reconstructed = self.inverse_normalize(reconstructed)
original = self.inverse_normalize(slice_tensor)
return original.cpu(), reconstructed.cpu(), mask.cpu() if with_mask else None
def reconstruct_volume(self, nifti_path, axis=2, with_mask=True, save_dir=None):
"""Reconstruct all slices in a volume.
Args:
nifti_path (str): Path to NIfTI file.
axis (int, optional): Axis along which to extract slices.
with_mask (bool, optional): Whether to use masking.
save_dir (str, optional): Directory to save visualizations.
Yields:
tuple: Original slice, reconstructed slice, and mask (if with_mask=True).
"""
img = nib.load(nifti_path)
nifti_data = img.get_fdata()
n_slices = nifti_data.shape[axis]
if save_dir:
os.makedirs(save_dir, exist_ok=True)
for slice_idx in range(n_slices):
original, recon, mask = self.reconstruct_slice(
nifti_path, slice_idx, axis, with_mask
)
if save_dir:
fig, axes = plt.subplots(1, 3 if with_mask else 2, figsize=(15, 5))
axes[0].imshow(original.squeeze().permute(1, 2, 0).clamp(0, 1), cmap='gray')
axes[0].set_title(f'Original - Slice {slice_idx}')
axes[0].axis('off')
axes[1].imshow(recon.squeeze().permute(1, 2, 0).clamp(0, 1), cmap='gray')
axes[1].set_title(f'Reconstruction - Slice {slice_idx}')
axes[1].axis('off')
if with_mask:
axes[2].imshow(mask.squeeze(), cmap='gray')
axes[2].set_title('Mask')
axes[2].axis('off')
plt.tight_layout()
plt.savefig(os.path.join(save_dir, f'slice_{slice_idx:04d}.png'))
plt.close()
yield original, recon, mask
def main():
"""Main function for running inference."""
inference = MAEVAEInference('best_model.pth')
os.makedirs('outputs', exist_ok=True)
nifti_path = '/path/to/your/nifti/file.nii.gz'
slice_idx = 50
original, recon, mask = inference.reconstruct_slice(
nifti_path,
slice_idx=slice_idx,
axis=2
)
fig, axes = plt.subplots(1, 3, figsize=(15, 5))
axes[0].imshow(original.squeeze().permute(1, 2, 0).clamp(0, 1), cmap='gray')
axes[0].set_title('Original')
axes[0].axis('off')
axes[1].imshow(recon.squeeze().permute(1, 2, 0).clamp(0, 1), cmap='gray')
axes[1].set_title('Reconstruction')
axes[1].axis('off')
axes[2].imshow(mask.squeeze(), cmap='gray')
axes[2].set_title('Mask')
axes[2].axis('off')
plt.tight_layout()
plt.savefig('outputs/single_slice_reconstruction.png')
plt.close()
volume_output_dir = os.path.join('outputs', 'volume_reconstruction')
for original, recon, mask in inference.reconstruct_volume(
nifti_path,
axis=2,
save_dir=volume_output_dir
):
pass
if __name__ == '__main__':
main()