-
Notifications
You must be signed in to change notification settings - Fork 1
/
Unet.py
130 lines (114 loc) · 3.79 KB
/
Unet.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 numpy as np
import torch.nn as nn
from torch.nn import functional as F
import matplotlib.pyplot as plt
def double_conv(in_channels, out_channels):
"""
This function applies to convolution layers
each followed by a ReLU activation function
-> in_channels: number of input channels
-> out_channels: number of output channels
-> a down conv layer
"""
conv = nn.Sequential(
nn.Conv2d(in_channels, out_channels, kernel_size=3),
nn.ReLU(inplace=True),
nn.Conv2d(out_channels, out_channels, kernel_size=3),
nn.ReLU(inplace=True)
)
return conv
def crop_tensor(tensor, target_tensor):
"""
Crops a tensor to the size of a given target tensor size.
Please note that this function is applicable to only this
implementation of unet. There are a few assumptions in this
implementation that might not be applicable to all networks
all other use-cases.
Both tensors are of shape (bs, c, h, w)
-> tensor: a tensor tat needs to be cropped
-> target_tensor: target tensor of smaller size
-> cropped tensor
"""
target_size = target_tensor.size()[2]
tensor_size = tensor.size()[2]
delta = (tensor_size - target_size)//2
return tensor[:, :, delta:tensor_size-delta, delta:tensor_size-delta]
class UNet(nn.Module):
def __init__(self):
super(UNet, self).__init__()
# we only need one max pool as it is not learned
self.max_pool_2x2 = nn.MaxPool2d(kernel_size=2, stride=2)
self.down_conv_1 = double_conv(1, 64)
self.down_conv_2 = double_conv(64, 128)
self.down_conv_3 = double_conv(128, 256)
self.down_conv_4 = double_conv(256, 512)
self.down_conv_5 = double_conv(512, 1024)
self.up_trans_1 = nn.ConvTranspose2d(
in_channels=1024,
out_channels=512,
kernel_size=2,
stride=2
)
self.up_conv_1 = double_conv(1024, 512)
self.up_trans_2 = nn.ConvTranspose2d(
in_channels=512,
out_channels=256,
kernel_size=2,
stride=2
)
self.up_conv_2 = double_conv(512, 256)
self.up_trans_3 = nn.ConvTranspose2d(
in_channels=256,
out_channels=128,
kernel_size=2,
stride=2
)
self.up_conv_3 = double_conv(256, 128)
self.up_trans_4 = nn.ConvTranspose2d(
in_channels=128,
out_channels=64,
kernel_size=2,
stride=2
)
self.up_conv_4 = double_conv(128, 64)
self.out = nn.Conv2d(
in_channels=64,
out_channels=2,
kernel_size=1
)
def forward(self, image):
# Encoder
x1 = self.down_conv_1(image)
x2 = self.max_pool_2x2(x1)
x3 = self.down_conv_2(x2)
x4 = self.max_pool_2x2(x3)
x5 = self.down_conv_3(x4)
x6 = self.max_pool_2x2(x5)
x7 = self.down_conv_4(x6)
x8 = self.max_pool_2x2(x7)
x9 = self.down_conv_5(x8)
# Decoder
x = self.up_trans_1(x9)
y = crop_tensor(x7, x)
x = self.up_conv_1(torch.cat([x, y], axis=1))
x = self.up_trans_2(x)
y = crop_tensor(x5, x)
x = self.up_conv_2(torch.cat([x, y], axis=1))
x = self.up_trans_3(x)
y = crop_tensor(x3, x)
x = self.up_conv_3(torch.cat([x, y], axis=1))
x = self.up_trans_4(x)
y = crop_tensor(x1, x)
x = self.up_conv_4(torch.cat([x, y], axis=1))
# output layer
out = self.out(x)
return out
if __name__ == "__main__":
image = torch.rand((1, 1, 572, 572))
model = UNet()
x = model(image)
y = x.detach().cpu().numpy()
plt.imshow(np.squeeze(y))
plt.show()
#print(model(image))