-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModel.py
143 lines (93 loc) · 4.41 KB
/
Model.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
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/env python
# coding: utf-8
# In[ ]:
import os
from pathlib import Path
import numpy as np
from ipywidgets import widgets
import matplotlib.pyplot as plt
import nilearn.plotting as nip
import nibabel as nib
import torch
import torch.nn as nn
from torch.utils.data import Dataset, DataLoader
import torchvision.transforms.functional as TF
from torch.optim import Adam
from tqdm.notebook import tqdm, trange
# In[ ]:
# In[ ]:
class DoubleConv(nn.Module):
def __init__(self,in_channels,out_channels):
super(DoubleConv,self).__init__()
self.conv = nn.Sequential(
nn.Conv3d(in_channels,in_channels,3,1,1, bias = False), # batch, channels, depth,height,width
nn.BatchNorm3d(in_channels),
nn.ReLU(inplace=True),
nn.Conv3d(in_channels,out_channels,3,1,1, bias = False),
nn.BatchNorm3d(out_channels),
nn.ReLU(inplace=True))
def forward(self,x):
return self.conv(x)
class DoubleConvUp(nn.Module):
def __init__(self,in_channels,out_channels):
super(DoubleConvUp,self).__init__()
self.convUp = nn.Sequential(
nn.Conv3d(in_channels,out_channels,3,1,1, bias = False), # batch, channels, depth,height,width
nn.BatchNorm3d(out_channels),
nn.ReLU(inplace=True),
nn.Conv3d(out_channels,out_channels,3,1,1, bias = False),
nn.BatchNorm3d(out_channels),
nn.ReLU(inplace=True))
def forward(self,x):
return self.convUp(x)
class Unet_3D(nn.Module):
def __init__(self, in_channels= 1,out_channels= 2, filters = [64,128,256]):
super(Unet_3D,self).__init__()
self.up = nn.ModuleList()
self.down = nn.ModuleList()
self.pool = nn.MaxPool3d(kernel_size = 2, stride = 2)
#Encoder of the U-net
# In the paper, they convolve the image from 3 to 32 to 64
self.conv32_64 = self.conv = nn.Sequential(
nn.Conv3d(in_channels,32,3,1,1, bias = False), # batch, channels, depth,height,width
nn.BatchNorm3d(32),
nn.ReLU(inplace=True),
nn.Conv3d(32,64,3,1,1, bias = False),
nn.BatchNorm3d(64),
nn.ReLU(inplace=True))
in_channels = filters[0]
for filter in filters:
### ignoring 64 because i computed it in conv_32_64
if filter ==filters[0]:
continue
self.down.append(DoubleConv(in_channels,filter)) ## add DoubleCon layer to the module list, maps 3 to 64, 64 to 128 ...
in_channels = filter
## Decoder of the U-Net
for filter in reversed(filters):
self.up.append(nn.ConvTranspose3d(filter*2,filter, kernel_size = 2,stride=2)) ## features * 2 , adding the skip connection
self.up.append(DoubleConvUp(filter*2,filter))
##Bottleneck / Bottom
self.bottleneck = DoubleConv(filters[-1],filters[-1]*2)
## 1x1x1 Conv
self.final_conv = nn.Conv3d(filters[0],out_channels,kernel_size= 1)
def forward(self,x):
skip_connections = []
## first convolutions
x = self.conv32_64(x)
skip_connections.append(x)
x = self.pool(x)
## the
for down in self.down:
x = down(x) ## double conv layer
skip_connections.append(x) # add resulat of the doubleconv layer to the list, order important
x = self.pool(x)
x = self.bottleneck(x)
skip_connections = skip_connections[::-1]# reverse the list, first element ,e.g 512 feature maps, bottom skip
for idx in range(0,len(self.up),2): #step size two, because of the transpose conv and DoubleConv operation
x = self.up[idx](x)
print(x.shape)
skip_connection = skip_connections[idx//2] ## division by 2 because of the step size
concat_skip = torch.cat((skip_connection,x),dim=1) # batch,channel, hight, width , adding along channel dim
x = self.up[idx+1](concat_skip)## upsampling step
print(x.shape)
return self.final_conv(x)