-
Notifications
You must be signed in to change notification settings - Fork 0
/
AggreegateVAE.py
298 lines (227 loc) · 7.96 KB
/
AggreegateVAE.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
from monai.utils import first, set_determinism
from monai.transforms import (
AsDiscrete,
AsDiscreted,
EnsureChannelFirstd,
Compose,
LoadImaged,
SaveImaged,
EnsureTyped,
EnsureType,
Invertd,
ToDeviced,
Resized,
)
import torch.nn as nn
from monai.networks.layers.convutils import calculate_out_shape, same_padding
from monai.handlers.utils import from_engine
from monai.networks.nets import AutoEncoder,VarAutoEncoder
from monai.networks.layers import Norm
from monai.metrics import DiceMetric
from monai.losses import DiceLoss
from monai.inferers import Inferer, SimpleInferer
from monai.data import DataLoader, Dataset, decollate_batch
from monai.networks.blocks import Convolution, ResidualUnit
import torch
import matplotlib.pyplot as plt
import tempfile
import shutil
import os
import glob
import torch
import numpy as np
import argparse
import nrrd
from torchsummary import summary
from torch.nn import functional as F
from sklearn.decomposition import PCA
from scipy.spatial.distance import cdist
from scipy.spatial import cKDTree
from sklearn.neighbors import KDTree
directory = os.environ.get("MONAI_DATA_DIRECTORY")
root_dir = tempfile.mkdtemp() if directory is None else directory
print(root_dir)
train_images = sorted(
glob.glob(os.path.join( "./vae_dataset/train/input/", "*.nii.gz")))
train_labels = sorted(
glob.glob(os.path.join( "./vae_dataset/train/gt/", "*.nii.gz")))
data_dicts = [
{"image": image_name, "label": label_name}
for image_name, label_name in zip(train_images, train_labels)
]
train_files = data_dicts
test_images = sorted(
glob.glob(os.path.join('./vae_dataset/train/gt/', "*.nii.gz")))
test_data = [{"image": image} for image in test_images]
'''########## Transforms
'''
set_determinism(seed=0)
test_org_transforms = Compose(
[
LoadImaged(keys="image"),
EnsureChannelFirstd(keys="image"),
Resized(keys=["image"],spatial_size=(256, 256, 128),mode='nearest'),
EnsureTyped(keys="image"),
]
)
train_transforms = Compose(
[
LoadImaged(keys=["image", "label"]),
EnsureChannelFirstd(keys=["image", "label"]),
Resized(keys=["image", "label"],spatial_size=(256, 256, 128),mode='nearest'),
EnsureTyped(keys=["image", "label"]),
#SaveImaged(keys=["image"],output_dir='./transformed_data')
#ToDeviced(keys=["image", "label"],device='cuda:0'),
]
)
val_transforms = Compose(
[
LoadImaged(keys=["image", "label"]),
EnsureChannelFirstd(keys=["image", "label"]),
Resized(keys=["image", "label"],spatial_size=(256, 256, 128),mode="nearest"),
EnsureTyped(keys=["image", "label"]),
]
)
test_org_post_transforms = Compose(
[
LoadImaged(keys="image"),
EnsureChannelFirstd(keys="image"),
EnsureTyped(keys="image"),
]
)
post_transforms = Compose([
EnsureTyped(keys="pred"),
Invertd(
keys="pred",
transform=test_org_post_transforms,
orig_keys="image",
meta_keys="pred_meta_dict",
orig_meta_keys="image_meta_dict",
meta_key_postfix="meta_dict",
nearest_interp=False,
to_tensor=True,
),
AsDiscreted(keys="pred", argmax=True),
# Specify here the output directory. Default is './out_cranial_monai' in the current directory
SaveImaged(keys="pred", meta_keys="pred_meta_dict", output_dir="./disentangle_output", output_postfix="completed", resample=False),
])
'''########## Load datasets and apply transforms
'''
train_ds = Dataset(data=train_files, transform=train_transforms)
train_loader = DataLoader(train_ds, batch_size=4, shuffle=True, num_workers=4)
test_org_ds = Dataset(data=test_data, transform=test_org_transforms)
test_org_loader = DataLoader(test_org_ds, batch_size=1, num_workers=4)
device = torch.device("cuda:0")
model = VarAutoEncoder(
dimensions=3,
in_shape=(1,256,256,128),
out_channels=2,
channels=(32, 64, 64, 128, 128, 256),
latent_size=32,
strides=(2, 2, 2, 2, 2, 2),
num_res_units=0,
norm=Norm.BATCH,
).to(device)
class myDecoder(nn.Module):
def __init__(self):
super(myDecoder, self).__init__()
self.norm=Norm.BATCH
self.decodeL = nn.Linear(32, 8192)
self.conv1 = Convolution(
spatial_dims=3,
in_channels=256,
out_channels=128,
strides=2,
norm=self.norm,
is_transposed=True,
)
self.conv2 = Convolution(
spatial_dims=3,
in_channels=128,
out_channels=128,
strides=2,
norm=self.norm,
is_transposed=True,
)
self.conv3 = Convolution(
spatial_dims=3,
in_channels=128,
out_channels=64,
strides=2,
norm=self.norm,
is_transposed=True,
)
self.conv4 = Convolution(
spatial_dims=3,
in_channels=64,
out_channels=64,
strides=2,
norm=self.norm,
is_transposed=True,
)
self.conv5 = Convolution(
spatial_dims=3,
in_channels=64,
out_channels=32,
strides=2,
norm=self.norm,
is_transposed=True,
)
self.conv6 = Convolution(
spatial_dims=3,
in_channels=32,
out_channels=2,
strides=2,
norm=self.norm,
is_transposed=True,
)
def forward(self, x):
x = F.relu(self.decodeL(x))
x = x.view(x.shape[0],256, 4,4,2)
x=F.relu(self.conv1(x))
x=F.relu(self.conv2(x))
x=F.relu(self.conv3(x))
x=F.relu(self.conv4(x))
x=F.relu(self.conv5(x))
x=F.relu(self.conv6(x))
return x
if __name__ == '__main__':
decoder_weight='./vae_weights/decoder/'
# the latent variables produced by beta=100
# for the three skull classes (cranial, facial, complete)
latentVar=np.load('./vae_weights/zu_beta100.npy')
latentVar=np.mean(latentVar,axis=1)
cranial=latentVar[0:100]
facial=latentVar[100:200]
complete=latentVar[200:300]
# use the header 'h' to save
# the output to nrrd
temp,h=nrrd.read('000.nrrd')
# deviation vectors
cranial_complete_diff=np.mean(complete-cranial,axis=0)
facial_complete_diff=np.mean(complete-facial,axis=0)
# load the weight of the retrained decoder
newDecoder=myDecoder().to(device)
newDecoder.load_state_dict(torch.load(os.path.join(decoder_weight, "retrained_decoder.pth"),map_location='cuda:0'))
newDecoder.eval()
# d=0. reconstruction; d=1. completion
# d \in (0,) interpolation
d=1
for i in range(len(cranial)):
z_cranial=cranial[i]+d*cranial_complete_diff
z_cranial=np.expand_dims(z_cranial,axis=0)
z_cranial=torch.tensor(z_cranial,dtype=torch.float).to(device)
rec_cranial=newDecoder(z_cranial)
rec_cranial=rec_cranial.cpu().detach().numpy()[0]
rec_cranial=np.argmax(rec_cranial,axis=0)
cranial_name='./large_betat_output/cranial_completion/'+str(i).zfill(3)+'.nrrd'
nrrd.write(cranial_name,rec_cranial.astype('int32'),h)
for i in range(len(facial)):
z_facial=facial[i]+d*facial_complete_diff
z_facial=np.expand_dims(z_facial,axis=0)
z_facial=torch.tensor(z_facial,dtype=torch.float).to(device)
rec_facial=newDecoder(z_facial)
rec_facial=rec_facial.cpu().detach().numpy()[0]
rec_facial=np.argmax(rec_facial,axis=0)
facial_name='./large_betat_output/facial_completion/'+str(i).zfill(3)+'.nrrd'
nrrd.write(facial_name,rec_facial.astype('int32'),h)