-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup_postprocessing.py
70 lines (48 loc) · 2.87 KB
/
setup_postprocessing.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
import torch
torch.cuda.set_per_process_memory_fraction(0.7)
class PostprocessingNetwork(torch.nn.Module):
def __init__(self, hidden_channels = 16, kernel_size = 3):
super(PostprocessingNetwork, self).__init__()
self.conv1 = torch.nn.Conv3d(1, hidden_channels, kernel_size, padding='same', bias=False)
self.conv2 = torch.nn.Conv3d(hidden_channels, hidden_channels, kernel_size, padding='same', bias=False)
self.conv3 = torch.nn.Conv3d(hidden_channels, hidden_channels, kernel_size, padding='same', bias=False)
self.max_pool = torch.nn.MaxPool3d(kernel_size=2)
self.conv4 = torch.nn.Conv3d(hidden_channels, hidden_channels, kernel_size, padding='same', bias=False)
self.conv5 = torch.nn.Conv3d(hidden_channels, hidden_channels, kernel_size, padding='same', bias=False)
self.conv6 = torch.nn.Conv3d(hidden_channels, hidden_channels, kernel_size, padding='same', bias=False)
self.conv7 = torch.nn.Conv3d(hidden_channels, hidden_channels, kernel_size, padding='same', bias=False)
self.conv8 = torch.nn.Conv3d(hidden_channels, hidden_channels, kernel_size, padding='same', bias=False)
self.conv9 = torch.nn.Conv3d(hidden_channels, hidden_channels, kernel_size, padding='same', bias=False)
# interpolate
self.conv10 = torch.nn.Conv3d(hidden_channels, hidden_channels, kernel_size, padding='same', bias=False)
self.conv11 = torch.nn.Conv3d(hidden_channels, 1, kernel_size, padding='same', bias=False)
self.activation = torch.nn.ReLU()
#self.list_of_conv3[-1].weight.data.fill_(0.0)
#self.list_of_conv3[-1].bias.data.fill_(0.0)
def forward(self, x):
shape = x.shape
z = self.activation(self.conv1(x))
z = self.activation(self.conv2(z))
z1 = self.activation(self.conv3(z))
z2 = self.max_pool(z1) # shape // 2
z2 = self.activation(self.conv4(z2))
z2 = self.activation(self.conv5(z2))
z3 = self.max_pool(z2) # shape // 4
z2 = self.activation(self.conv6(z2))
z2 = self.activation(self.conv7(z2))
upsampling_shape = shape[-3:]
upsampling_shape = [s // 2 for s in upsampling_shape]
z4 = torch.nn.functional.interpolate(z2, size=upsampling_shape, mode = "trilinear", align_corners=True)
z4 = z4 + z2
z4 = self.activation(self.conv8(z4))
z4 = self.activation(self.conv9(z4))
z5 = torch.nn.functional.interpolate(z2, size=shape[-3:], mode = "trilinear", align_corners=True)
z5 = z5 + z1
z6 = self.activation(self.conv10(z5))
z_out =self.conv11(z6)
return z_out
DEVICE = "cuda"
postprocessing_model = PostprocessingNetwork()
postprocessing_model = postprocessing_model.to(DEVICE)
postprocessing_model.eval()
postprocessing_model.load_state_dict(torch.load("checkpoint/postprocessing_model.pt", weights_only=True))