-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtta.py
More file actions
67 lines (60 loc) · 1.43 KB
/
Copy pathtta.py
File metadata and controls
67 lines (60 loc) · 1.43 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
import torch
from monai.inferers import sliding_window_inference
# 8 种翻转组合
FLIP_DIMS = [
(),
(2,),
(3,),
(4,),
(2, 3),
(2, 4),
(3, 4),
(2, 3, 4),
]
@torch.no_grad()
def sliding_window_tta(
image,
model,
roi_size,
sw_batch_size=4,
overlap=0.5,
mode="gaussian",
flips=True,
sigmoid=True,
):
"""
支持 TTA 翻转的滑窗推理。返回概率图。
image: (B, C, D, H, W)
"""
def predictor(x):
out = model(x)
if isinstance(out, (list, tuple)):
out = out[0]
return out
if not flips:
logits = sliding_window_inference(
inputs=image,
roi_size=roi_size,
sw_batch_size=sw_batch_size,
predictor=predictor,
overlap=overlap,
mode=mode,
)
return torch.sigmoid(logits) if sigmoid else logits
probs = None
for dims in FLIP_DIMS:
x = torch.flip(image, dims=dims) if dims else image
logits = sliding_window_inference(
inputs=x,
roi_size=roi_size,
sw_batch_size=sw_batch_size,
predictor=predictor,
overlap=overlap,
mode=mode,
)
p = torch.sigmoid(logits) if sigmoid else logits
if dims:
p = torch.flip(p, dims=dims)
probs = p if probs is None else probs + p
probs /= len(FLIP_DIMS)
return probs