-
Notifications
You must be signed in to change notification settings - Fork 0
/
baseunet.py
158 lines (121 loc) · 6.23 KB
/
baseunet.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
143
144
145
146
147
148
149
150
151
152
153
154
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
class Unet(nn.Module):
def __init__(self, in_channels=3, out_channels=3):
super(Unet, self).__init__()
# device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
self.conv1_1 = nn.Conv2d(in_channels, 32, kernel_size=3, stride=1, padding=1)
self.conv1_2 = nn.Conv2d(32, 32, kernel_size=3, stride=1, padding=1)
self.stride1 = nn.Conv2d(32, 32, kernel_size=3, stride=2, padding=1)
self.conv2_1 = nn.Conv2d(32, 64, kernel_size=3, stride=1, padding=1)
self.conv2_2 = nn.Conv2d(64, 64, kernel_size=3, stride=1, padding=1)
self.stride2 = nn.Conv2d(64, 64, kernel_size=3, stride=2, padding=1)
self.conv3_1 = nn.Conv2d(64, 128, kernel_size=3, stride=1, padding=1)
self.conv3_2 = nn.Conv2d(128, 128, kernel_size=3, stride=1, padding=1)
self.stride3 = nn.Conv2d(128, 128, kernel_size=3, stride=2, padding=1)
self.conv4_1 = nn.Conv2d(128, 256, kernel_size=3, stride=1, padding=1)
self.conv4_2 = nn.Conv2d(256, 256, kernel_size=3, stride=1, padding=1)
self.stride4 = nn.Conv2d(256, 256, kernel_size=3, stride=2, padding=1)
self.conv5_1 = nn.Conv2d(256, 512, kernel_size=3, stride=1, padding=1)
self.conv5_2 = nn.Conv2d(512, 512, kernel_size=3, stride=1, padding=1)
self.upv6 = nn.ConvTranspose2d(512, 256, 2, stride=2)
self.conv6_1 = nn.Conv2d(512, 256, kernel_size=3, stride=1, padding=1)
self.conv6_2 = nn.Conv2d(256, 256, kernel_size=3, stride=1, padding=1)
self.upv7 = nn.ConvTranspose2d(256, 128, 2, stride=2)
self.conv7_1 = nn.Conv2d(256, 128, kernel_size=3, stride=1, padding=1)
self.conv7_2 = nn.Conv2d(128, 128, kernel_size=3, stride=1, padding=1)
self.upv8 = nn.ConvTranspose2d(128, 64, 2, stride=2)
self.conv8_1 = nn.Conv2d(128, 64, kernel_size=3, stride=1, padding=1)
self.conv8_2 = nn.Conv2d(64, 64, kernel_size=3, stride=1, padding=1)
self.upv9 = nn.ConvTranspose2d(64, 32, 2, stride=2)
self.conv9_1 = nn.Conv2d(64, 32, kernel_size=3, stride=1, padding=1)
self.conv9_2 = nn.Conv2d(32, 32, kernel_size=3, stride=1, padding=1)
self.conv9_1_line = nn.Conv2d(64, 32, kernel_size=3, stride=1, padding=1)
self.conv9_2_line = nn.Conv2d(32, 32, kernel_size=3, stride=1, padding=1)
self.conv10_1 = nn.Conv2d(32, out_channels, kernel_size=1, stride=1)
self.conv10_1_line = nn.Conv2d(32, out_channels, kernel_size=1, stride=1)
# Just for four individual
# self.conv10_1 = nn.Conv2d(32, 32, kernel_size=3, stride=1, padding=1)
# self.conv10_1_line = nn.Conv2d(32, 32, kernel_size=3, stride=1, padding=1)
# self.conv10_2 = nn.Conv2d(32, out_channels, kernel_size=1, stride=1)
# self.conv10_2_line = nn.Conv2d(32, out_channels, kernel_size=1, stride=1)
def forward(self, x):
n, c, h, w = x.shape
h_pad = 32 - h % 32 if not h % 32 == 0 else 0
w_pad = 32 - w % 32 if not w % 32 == 0 else 0
padded_image = F.pad(x, (0, w_pad, 0, h_pad), 'replicate')
conv1 = self.leaky_relu(self.conv1_1(padded_image))
conv1 = self.leaky_relu(self.conv1_2(conv1))
stride1 = self.stride1(conv1)
conv2 = self.leaky_relu(self.conv2_1(stride1))
conv2 = self.leaky_relu(self.conv2_2(conv2))
stride2 = self.stride2(conv2)
conv3 = self.leaky_relu(self.conv3_1(stride2))
conv3 = self.leaky_relu(self.conv3_2(conv3))
stride3 = self.stride3(conv3)
conv4 = self.leaky_relu(self.conv4_1(stride3))
conv4 = self.leaky_relu(self.conv4_2(conv4))
stride4 = self.stride4(conv4)
conv5 = self.leaky_relu(self.conv5_1(stride4))
conv5 = self.leaky_relu(self.conv5_2(conv5))
up6 = self.upv6(conv5)
up6 = torch.cat([up6, conv4], 1)
conv6 = self.leaky_relu(self.conv6_1(up6))
conv6 = self.leaky_relu(self.conv6_2(conv6))
up7 = self.upv7(conv6)
up7 = torch.cat([up7, conv3], 1)
conv7 = self.leaky_relu(self.conv7_1(up7))
conv7 = self.leaky_relu(self.conv7_2(conv7))
up8 = self.upv8(conv7)
up8 = torch.cat([up8, conv2], 1)
conv8 = self.leaky_relu(self.conv8_1(up8))
conv8 = self.leaky_relu(self.conv8_2(conv8))
up9 = self.upv9(conv8)
up9 = torch.cat([up9, conv1], 1)
# ONE individual
# conv9 = self.leaky_relu(self.conv9_1(up9))
# conv9 = self.leaky_relu(self.conv9_2(conv9))
# conv10 = self.conv10_1(conv9)
# conv10_line = self.conv10_1_line(conv9)
# TWO individual
# conv9 = self.leaky_relu(self.conv9_1(up9))
# conv9_img = self.leaky_relu(self.conv9_2(conv9))
# conv9_line = self.leaky_relu(self.conv9_2_line(conv9))
#
# conv10 = self.conv10_1(conv9_img)
# conv10_line = self.conv10_1_line(conv9_line)
# THREE individual
conv9 = self.leaky_relu(self.conv9_1(up9))
conv9 = self.leaky_relu(self.conv9_2(conv9))
conv9_line = self.leaky_relu(self.conv9_1_line(up9))
conv9_line = self.leaky_relu(self.conv9_2_line(conv9_line))
conv10 = self.conv10_1(conv9)
conv10_line = self.conv10_1_line(conv9_line)
out_image = conv10[:, :, :h, :w]
out_line = conv10_line[:, :, :h, :w]
return out_image, out_line
# FOUR individual
# conv9 = self.leaky_relu(self.conv9_1(up9))
# conv9 = self.leaky_relu(self.conv9_2(conv9))
# conv9_line = self.leaky_relu(self.conv9_1_line(up9))
# conv9_line = self.leaky_relu(self.conv9_2_line(conv9_line))
#
# conv10 = self.conv10_1(conv9)
# conv10_line = self.conv10_1_line(conv9_line)
# conv10 = self.conv10_2(conv10)
# conv10_line = self.conv10_2_line(conv10_line)
#
# out_image = conv10[:, :, :h, :w]
# out_line = conv10_line[:, :, :h, :w]
#
# return out_image, out_line
def leaky_relu(self, x):
out = torch.max(0.2 * x, x)
return out
if __name__ == "__main__":
test_input = torch.from_numpy(np.random.randn(1, 4, 512, 512)).float()
net = Unet()
output = net(test_input)
print("test over")