Repository navigation
Expand file tree
/
Copy pathUnet.py
More file actions
90 lines (71 loc) · 2.72 KB
/
Copy pathUnet.py
File metadata and controls
90 lines (71 loc) · 2.72 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import torch
import torch.nn as nn
#double 3x3 convolution
def dual_conv(in_channel, out_channel):
conv = nn.Sequential(
nn.Conv2d(in_channel, out_channel, kernel_size=3),
nn.ReLU(inplace= True),
nn.Conv2d(out_channel, out_channel, kernel_size=3),
nn.ReLU(inplace= True),
)
return conv
# crop the image(tensor) to equal size
# as shown in architecture image , half left side image is concated with right side image
def crop_tensor(target_tensor, tensor):
target_size = target_tensor.size()[2]
tensor_size = tensor.size()[2]
delta = tensor_size - target_size
delta = delta // 2
return tensor[:, :, delta:tensor_size- delta, delta:tensor_size-delta]
class Unet(nn.Module):
def __init__(self):
super(Unet, self).__init__()
# Left side (contracting path)
self.dwn_conv1 = dual_conv(1, 64)
self.dwn_conv2 = dual_conv(64, 128)
self.dwn_conv3 = dual_conv(128, 256)
self.dwn_conv4 = dual_conv(256, 512)
self.dwn_conv5 = dual_conv(512, 1024)
self.maxpool = nn.MaxPool2d(kernel_size=2, stride=2)
#Right side (expnsion path)
#transpose convolution is used showna as green arrow in architecture image
self.trans1 = nn.ConvTranspose2d(1024,512, kernel_size=2, stride= 2)
self.up_conv1 = dual_conv(1024,512)
self.trans2 = nn.ConvTranspose2d(512,256, kernel_size=2, stride= 2)
self.up_conv2 = dual_conv(512,256)
self.trans3 = nn.ConvTranspose2d(256, 128, kernel_size=2, stride= 2)
self.up_conv3 = dual_conv(256,128)
self.trans4 = nn.ConvTranspose2d(128,64, kernel_size=2, stride= 2)
self.up_conv4 = dual_conv(128,64)
#output layer
self.out = nn.Conv2d(64, 2, kernel_size=1)
def forward(self, image):
#forward pass for Left side
x1 = self.dwn_conv1(image)
x2 = self.maxpool(x1)
x3 = self.dwn_conv2(x2)
x4 = self.maxpool(x3)
x5 = self.dwn_conv3(x4)
x6 = self.maxpool(x5)
x7 = self.dwn_conv4(x6)
x8 = self.maxpool(x7)
x9 = self.dwn_conv5(x8)
#forward pass for Right side
x = self.trans1(x9)
y = crop_tensor(x, x7)
x = self.up_conv1(torch.cat([x,y], 1))
x = self.trans2(x)
y = crop_tensor(x, x5)
x = self.up_conv2(torch.cat([x,y], 1))
x = self.trans3(x)
y = crop_tensor(x, x3)
x = self.up_conv3(torch.cat([x,y], 1))
x = self.trans4(x)
y = crop_tensor(x, x1)
x = self.up_conv4(torch.cat([x,y], 1))
x = self.out(x)
return x
if __name__ == '__main__':
image = torch.rand((1, 1, 572, 572))
model = Unet()
model(image)