-
Notifications
You must be signed in to change notification settings - Fork 0
/
torch_ssmctb_3d.py
130 lines (97 loc) · 4.95 KB
/
torch_ssmctb_3d.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
import torch
import torch.nn as nn
import torch.nn.functional as F
from einops import rearrange
class Attention(nn.Module):
def __init__(self, dim, heads=8, dim_head=64, dropout=0.):
super().__init__()
inner_dim = dim_head * heads
project_out = not (heads == 1 and dim_head == dim)
self.heads = heads
self.scale = dim_head ** -0.5
self.attend = nn.Softmax(dim=-1)
self.dropout = nn.Dropout(dropout)
self.to_qkv = nn.Linear(dim, inner_dim * 3, bias=False)
self.to_out = nn.Sequential(
nn.Linear(inner_dim, dim),
nn.Dropout(dropout)
) if project_out else nn.Identity()
def forward(self, x):
qkv = self.to_qkv(x).chunk(3, dim=-1)
q, k, v = map(lambda t: rearrange(t, 'b n (h d) -> b h n d', h=self.heads), qkv)
dots = torch.matmul(q, k.transpose(-1, -2)) * self.scale
attn = self.attend(dots)
attn = self.dropout(attn)
out = torch.matmul(attn, v)
out = rearrange(out, 'b h n d -> b n (h d)')
return self.to_out(out)
class ChannelWiseTransformerBlock(nn.Module):
def __init__(self, num_patches, patch_dim=1, dim=64, heads=5, dim_head=64, dropout=0.):
super().__init__()
self.avg_pool = nn.AdaptiveAvgPool3d(patch_dim)
self.projection = nn.Linear(patch_dim ** 2, dim)
self.pos_embedding = nn.Parameter(torch.randn(1, num_patches, dim))
self.mha = Attention(dim, heads=heads, dim_head=dim_head, dropout=dropout)
self.sigmoid = nn.Sigmoid()
def forward(self, z):
x = self.avg_pool(z)
x = x.flatten(-3)
x = self.projection(x)
x += self.pos_embedding
x = self.mha(x)
x = x.mean(-1).unsqueeze(-1).unsqueeze(-1).unsqueeze(-1)
x = self.sigmoid(x)
return z * x
# SSMCTB implementation
class SSMCTB(nn.Module):
def __init__(self, channels, kernel_dim=1, dilation=1):
'''
channels: The number of filter at the output (usually the same with the number of filter from the input)
kernel_dim: The dimension of the sub-kernels ' k' ' from the paper
dilation: The dilation dimension 'd' from the paper
reduction_ratio: The reduction ratio for the SE block ('r' from the paper)
'''
super(SSMCTB, self).__init__()
self.pad = kernel_dim + dilation
self.border_input = kernel_dim + 2 * dilation + 1
self.relu = nn.ReLU()
self.transformer_sspcab = ChannelWiseTransformerBlock(num_patches=channels, patch_dim=1)
self.conv1 = nn.Conv3d(in_channels=channels,
out_channels=channels,
kernel_size=kernel_dim)
self.conv1_1 = nn.Conv3d(in_channels=channels,
out_channels=channels,
kernel_size=kernel_dim)
self.conv2 = nn.Conv3d(in_channels=channels,
out_channels=channels,
kernel_size=kernel_dim)
self.conv2_2 = nn.Conv3d(in_channels=channels,
out_channels=channels,
kernel_size=kernel_dim)
self.conv3 = nn.Conv3d(in_channels=channels,
out_channels=channels,
kernel_size=kernel_dim)
self.conv3_3 = nn.Conv3d(in_channels=channels,
out_channels=channels,
kernel_size=kernel_dim)
self.conv4 = nn.Conv3d(in_channels=channels,
out_channels=channels,
kernel_size=kernel_dim)
self.conv4_4 = nn.Conv3d(in_channels=channels,
out_channels=channels,
kernel_size=kernel_dim)
def forward(self, x_in):
x = F.pad(x_in, (self.pad, self.pad, self.pad, self.pad, self.pad, self.pad), "constant", 0)
x1 = self.conv1(x[:, :, :-self.border_input, :-self.border_input, self.border_input:])
x11 = self.conv1_1(x[:, :, :-self.border_input, :-self.border_input, :-self.border_input])
x2 = self.conv2(x[:, :, self.border_input:, :-self.border_input, self.border_input:])
x22 = self.conv2_2(x[:, :, self.border_input:, :-self.border_input, :-self.border_input])
x3 = self.conv3(x[:, :, :-self.border_input, self.border_input:, self.border_input:])
x33 = self.conv3_3(x[:, :, :-self.border_input, self.border_input:, :-self.border_input])
x4 = self.conv4(x[:, :, self.border_input:, self.border_input:, self.border_input:])
x44 = self.conv4_4(x[:, :, self.border_input:, self.border_input:, :-self.border_input])
x = self.relu(x1 + x2 + x3 + x4 + x11 + x22 + x33 + x44)
x = self.transformer_sspcab(x)
return x, torch.mean((x - x_in) ** 2) # output, loss
# model = SSMCTB(32)
# model(torch.zeros((3, 32, 6, 64, 64)))