-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_prior_LIDC.py
More file actions
283 lines (240 loc) · 9.27 KB
/
test_prior_LIDC.py
File metadata and controls
283 lines (240 loc) · 9.27 KB
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
import argparse
import os
os.environ['CUDA_VISIBLE_DEVICES']='0'
from pathlib import Path
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.distributions.normal import Normal
from torch.distributions.uniform import Uniform
from torch.optim import Adam
from torch.utils.data import DataLoader
from torchvision.utils import save_image
from tqdm import tqdm
import matplotlib.pyplot as plt
from flows import ConditionalFlow, OTConditionalFlow
from inference_LIDC import infer
from models.Condition_Unet import Unet
from dataloaders import *
from metrics import *
from models.GTR import GaussianTruncationRepresentation
from scipy.optimize import linear_sum_assignment
import time
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
device1 = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
def compute_hm_iou(Pred, Masks):
lcm = np.lcm(len(Pred), len(Masks))
len1 = len(Pred)
len2 = len(Masks)
for i in range((lcm // len1) - 1):
for j in range(len1):
Pred.append(Pred[j])
for i in range((lcm // len2) - 1):
for j in range(len2):
Masks.append(Masks[j])
#print(len(Pred))
#print(len(Masks))
cost_matrix = np.zeros((lcm, lcm))
for i in range(lcm):
for j in range(lcm):
cost_matrix[i][j] = 1 - IoUIoU(Pred[i], Masks[j])
row_ind, col_ind = linear_sum_assignment(cost_matrix)
HM_IoU = np.mean([(1 - cost_matrix[i][j]) for i, j in zip(row_ind, col_ind)])
return HM_IoU
def compute_max_Dice(Pred, Masks):
len1 = len(Pred)
len2 = len(Masks)
mx_D = 0
for j in range(len2):
mx = 0
for i in range(len1):
Diceij = Dice(Pred[i], Masks[j])
if Diceij > mx:
mx = Diceij
mx_D = mx_D + mx
return mx_D / len2
def Dice(target, predicted_mask):
"""
Args:
target: (torch.tensor (batchxCxHxW)) Binary Target Segmentation from training set
predicted_mask: (torch.tensor (batchxCxHxW)) Predicted Segmentation Mask
Returns:
IoU: (Float) Average IoUs over Batch
"""
target = target.detach()
predicted_mask = predicted_mask.detach()
smooth = 1e-8
true_p = (torch.logical_and(target == 1, predicted_mask == 1)).sum()
# true_n = (torch.logical_and(target == 0, predicted_mask == 0)).sum().item() #Currently not needed for IoU
false_p = (torch.logical_and(target == 0, predicted_mask == 1)).sum()
false_n = (torch.logical_and(target == 1, predicted_mask == 0)).sum()
sample_IoU = (smooth + float(true_p) + float(true_p)) / (float(true_p) + float(true_p) +
float(false_p) + float(false_n) + smooth)
return sample_IoU
def IoUIoU(target, predicted_mask):
"""
Args:
target: (torch.tensor (batchxCxHxW)) Binary Target Segmentation from training set
predicted_mask: (torch.tensor (batchxCxHxW)) Predicted Segmentation Mask
Returns:
IoU: (Float) Average IoUs over Batch
"""
target = target.detach()
predicted_mask = predicted_mask.detach()
smooth = 1e-8
true_p = (torch.logical_and(target == 1, predicted_mask == 1)).sum()
# true_n = (torch.logical_and(target == 0, predicted_mask == 0)).sum().item() #Currently not needed for IoU
false_p = (torch.logical_and(target == 0, predicted_mask == 1)).sum()
false_n = (torch.logical_and(target == 1, predicted_mask == 0)).sum()
sample_IoU = (smooth+float(true_p))/(float(true_p) +
float(false_p)+float(false_n)+smooth)
return sample_IoU
def normalize_tensor(tensor):
min_val = tensor.min()
max_val = tensor.max()
normalized_tensor = (tensor - min_val) / (max_val - min_val)
return normalized_tensor
def loss_fn(
model,
target_flow,
x_0,
x_1,
t,
cond
):
"""Counts MSE loss between predicted and target conditional vector fields.
Check eq. (9) in paper: https://arxiv.org/abs/2210.02747
Args:
model: Model that predicts conditional vector field.
target_flow: Object that models target conditional vector field.
x_0: Samples from base distribution, [batch_size, 1, h, w].
x_1: Samples from target distribution, [batch_size, 1, h, w].
t: Time samples, [batch_size].
Returns:
MSE loss between predicted and target conditional vector fields.
"""
x_t = target_flow.sample_p_t(x_0=x_0, x_1=x_1, t=t).to(device)
predicted_cond_vector_field = model(x_t, t, cond)
target_cond_vector_field = target_flow.get_conditional_vector_field(x_0=x_0, x_1=x_1, t=t)
return F.mse_loss(predicted_cond_vector_field, target_cond_vector_field)
def euler_sampler(model, sampling_times, cond, x, BS):
"""The probability flow ODE sampler with simple Euler discretization.
Args:
model: A velocity model.
z: If present, generate samples from latent code `z`.
Returns:
samples, number of function evaluations.
"""
with torch.no_grad():
dt = 1./sampling_times
eps = 1e-3
for i in range(sampling_times):
num_t = i / sampling_times
t = torch.ones(BS, device=device) * num_t
pred = model(x, t, cond)
#print(f'pmin:{torch.min(pred)}, pmax:{torch.max(pred)}, xmin:{torch.min(x)}, xmax:{torch.max(x)}')
sigma_t = 0
pred_sigma = pred
x = x.detach().clone() + pred_sigma * dt
return x
def test(
prior_model,
model,
target_flow,
testloader,
optimizer,
num_epochs,
save_path
):
"""Infers conditional vector field model.
Args:
model: Model that predicts conditional vector field.
target_flow: Object that models target conditional vector field.
dataloader: Dataloader.
optimizer: Optimizer.
device: Target device.
num_epochs: Num epochs to train.
save_path: Where to save checkpoints and intermediate results.
"""
time_distribution = Uniform(0, 1)
base_distribution = Normal(0, 1)
GED = 0.0
IoU = 0.0
mxD = 0.0
print("\nTesting...")
prior_model.eval()
model.eval()
tcnt = 0
for images, masks, _, _ in tqdm(testloader):
tcnt += 1
BS = images.shape[0]
Masks = []
Pred = []
images = images.to(device1)
logits, output_dict, _ = prior_model(images)
logit_distribution = output_dict["distribution"]
images = images.to(device)
for i in range(4):
Masks.append(masks[i].to(device).int())
num_samples = 1
for i in range(num_samples):
x0 = logit_distribution.sample()
x0 = torch.sigmoid(x0).to(device)
result = euler_sampler(model=model, sampling_times=25, cond=images, x=x0, BS=BS)
To = result.ge(0.5).int()
Pred.append(To.int())
#GED1, _ = ged(Masks, Pred)
#GED += GED1
#print(f'Current Mean GED = {GED/tcnt}')
#now = compute_hm_iou(Pred, Masks)
#IoU += now
#print(f'This HM-IoU = {now}, Current Mean HM-IoU = {IoU/tcnt}')
now2 = compute_max_Dice(Pred, Masks)
mxD += now2
print(f'This maxDice = {now2}, Current Mean maxDice = {mxD/tcnt}')
parser = argparse.ArgumentParser()
parser.add_argument("--image_size", type=int, default=128)
parser.add_argument("--batch_size", type=int, default=6)
parser.add_argument("--num_epochs", type=int, default=200)
parser.add_argument("--lr", type=float, default=1e-4)
parser.add_argument("--output_path", type=str, default="results_LIDC")
parser.add_argument("--resume_training", type=bool, default=False)
parser.add_argument("--resume_filepath", type=str, default=None)
if __name__ == "__main__":
args = parser.parse_args()
#device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
test_loader, _ = get_dataloader_2(
task="LIDC", split="test", batch_size=1, shuffle=False, splitratio=[0.8, 0.0, 0.2], randomsplit=False
)
GTR = GaussianTruncationRepresentation(
name='GTR',
num_channels=1,
rank=10,
num_filters=[32, 64, 128, 192],
diagonal=False,
).to(device1)
checkpoint1 = torch.load("/root/ATFM/saved_models/LIDC/GTR_LIDC.pt")
GTR.load_state_dict(checkpoint1["model_state_dict"])
# Setup model and optimizer
model = Unet(
channels=1,
dim_mults=(1, 2, 4),
dim=args.image_size,
resnet_block_groups=1,
).to(device)
optimizer = Adam(model.parameters(), lr=args.lr)
checkpoint = torch.load("/root/ATFM/saved_models/LIDC/SFM-for-ATFM-LIDC.pth")
print(f'Testing: 1e-3, sampling_time=25, model=199')
model.load_state_dict(checkpoint["model_state_dict"])
model.to(device)
target_flow = OTConditionalFlow(sigma_min=0)
os.makedirs(args.output_path, exist_ok=True)
test(
prior_model=GTR,
model=model,
target_flow=target_flow,
testloader=test_loader,
optimizer=optimizer,
num_epochs=args.num_epochs,
save_path=Path(args.output_path),
)