d3v-26 commited on
Commit
6fd6195
·
verified ·
1 Parent(s): 4783873

Upload SSL_Head.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. SSL_Head.py +92 -0
SSL_Head.py ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright 2020 - 2022 MONAI Consortium
2
+ # Licensed under the Apache License, Version 2.0 (the "License");
3
+ # you may not use this file except in compliance with the License.
4
+ # You may obtain a copy of the License at
5
+ # http://www.apache.org/licenses/LICENSE-2.0
6
+ # Unless required by applicable law or agreed to in writing, software
7
+ # distributed under the License is distributed on an "AS IS" BASIS,
8
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9
+ # See the License for the specific language governing permissions and
10
+ # limitations under the License.
11
+
12
+ import torch
13
+ import torch.nn as nn
14
+
15
+ from monai.networks.nets.swin_unetr import SwinTransformer as SwinViT
16
+ from monai.utils import ensure_tuple_rep
17
+
18
+
19
+ class SSLHead(nn.Module):
20
+ def __init__(self, args, upsample="vae", dim=768):
21
+ super(SSLHead, self).__init__()
22
+ patch_size = ensure_tuple_rep(2, args.spatial_dims) # YY [2,2,2]
23
+ window_size = ensure_tuple_rep(7, args.spatial_dims) # YY [7,7,7]
24
+ dim = args.bottleneck_depth # YY 768 or 1536
25
+ self.swinViT = SwinViT(
26
+ in_chans=args.in_channels, # 2 for T1T2
27
+ embed_dim=args.feature_size, # YY 48, 96
28
+ window_size=window_size,
29
+ patch_size=patch_size,
30
+ depths = args.num_swin_blocks_per_stage, # YY [2, 2, 2, 2],
31
+ num_heads = args.num_heads_per_stage, # YY [3, 6, 12, 24],
32
+ mlp_ratio=4.0,
33
+ qkv_bias=True,
34
+ drop_rate=0.0,
35
+ attn_drop_rate=0.0,
36
+ drop_path_rate=args.dropout_path_rate,
37
+ norm_layer=torch.nn.LayerNorm,
38
+ use_checkpoint=args.use_checkpoint,
39
+ spatial_dims=args.spatial_dims,
40
+ )
41
+ self.rotation_pre = nn.Identity()
42
+ self.rotation_head = nn.Linear(dim, 4) # YY rotation 4 value
43
+ self.contrastive_pre = nn.Identity()
44
+ self.contrastive_head = nn.Linear(dim, 512) # YY why 512 ?
45
+ if upsample == "large_kernel_deconv":
46
+ self.conv = nn.ConvTranspose3d(dim, args.in_channels, kernel_size=(32, 32, 32), stride=(32, 32, 32))
47
+ elif upsample == "deconv":
48
+ self.conv = nn.Sequential(
49
+ nn.ConvTranspose3d(dim, dim // 2, kernel_size=(2, 2, 2), stride=(2, 2, 2)),
50
+ nn.ConvTranspose3d(dim // 2, dim // 4, kernel_size=(2, 2, 2), stride=(2, 2, 2)),
51
+ nn.ConvTranspose3d(dim // 4, dim // 8, kernel_size=(2, 2, 2), stride=(2, 2, 2)),
52
+ nn.ConvTranspose3d(dim // 8, dim // 16, kernel_size=(2, 2, 2), stride=(2, 2, 2)),
53
+ nn.ConvTranspose3d(dim // 16, args.in_channels, kernel_size=(2, 2, 2), stride=(2, 2, 2)),
54
+ )
55
+ elif upsample == "vae":
56
+ self.conv = nn.Sequential(
57
+ nn.Conv3d(dim, dim // 2, kernel_size=3, stride=1, padding=1),
58
+ nn.InstanceNorm3d(dim // 2),
59
+ nn.LeakyReLU(),
60
+ nn.Upsample(scale_factor=2, mode="trilinear", align_corners=False),
61
+ nn.Conv3d(dim // 2, dim // 4, kernel_size=3, stride=1, padding=1),
62
+ nn.InstanceNorm3d(dim // 4),
63
+ nn.LeakyReLU(),
64
+ nn.Upsample(scale_factor=2, mode="trilinear", align_corners=False),
65
+ nn.Conv3d(dim // 4, dim // 8, kernel_size=3, stride=1, padding=1),
66
+ nn.InstanceNorm3d(dim // 8),
67
+ nn.LeakyReLU(),
68
+ nn.Upsample(scale_factor=2, mode="trilinear", align_corners=False),
69
+ nn.Conv3d(dim // 8, dim // 16, kernel_size=3, stride=1, padding=1),
70
+ nn.InstanceNorm3d(dim // 16),
71
+ nn.LeakyReLU(),
72
+ nn.Upsample(scale_factor=2, mode="trilinear", align_corners=False),
73
+ nn.Conv3d(dim // 16, dim // 16, kernel_size=3, stride=1, padding=1),
74
+ nn.InstanceNorm3d(dim // 16),
75
+ nn.LeakyReLU(),
76
+ nn.Upsample(scale_factor=2, mode="trilinear", align_corners=False),
77
+ nn.Conv3d(dim // 16, args.in_channels, kernel_size=1, stride=1),
78
+ )
79
+
80
+ def forward(self, x):
81
+ x_out = self.swinViT(x.contiguous())[4] # YY why [4]. [x0_out, x1_out, x2_out, x3_out, x4_out] stage4 meaning that get the output of final stage in Encoder
82
+ _, c, h, w, d = x_out.shape # # [4, 768, 3, 3, 3]
83
+ x4_reshape = x_out.flatten(start_dim=2, end_dim=4) # # [4, 768, 27]
84
+ x4_reshape = x4_reshape.transpose(1, 2) # [4, 27, 768]
85
+ x_rot = self.rotation_pre(x4_reshape[:, 0]) # [4, 768]
86
+ x_rot = self.rotation_head(x_rot) # [4, 4]
87
+ x_contrastive = self.contrastive_pre(x4_reshape[:, 1]) # [4, 768]
88
+ x_contrastive = self.contrastive_head(x_contrastive) # [4, 512]
89
+ x_rec = x_out.flatten(start_dim=2, end_dim=4) # [4, 768, 27]
90
+ x_rec = x_rec.view(-1, c, h, w, d) # [4, 768, 3, 3, 3]
91
+ x_rec = self.conv(x_rec) # # [4, in_channel, 96, 96, 96]
92
+ return x_rot, x_contrastive, x_rec