-
Notifications
You must be signed in to change notification settings - Fork 0
/
ThreeBranch_3.py
153 lines (110 loc) · 5.97 KB
/
ThreeBranch_3.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
import torch
import torch.nn as nn
#changed the G generated by itself and add the mask to guide the Infor part
class ThreeBranch_Net(nn.Module):
def __init__(self, Dim=[3,34,31], Depth=3, KS_1=3, KS_2=3, KS_3=3):
super(ThreeBranch_Net, self).__init__()
block1_1 = []
block1_2 = []
block2_1 = []
block2_2 = []
block3_1 = []
block3_2 = []
for i in range(Depth):
block1_1 += [nn.Conv2d(128, 128, KS_1, 1, int(KS_1 / 2)), nn.ReLU()]
block1_2 += [nn.Conv2d(128, 128, KS_1, 1, int(KS_1 / 2)), nn.ReLU()]
block2_1 += [nn.Conv2d(128, 128, KS_2, 1, int(KS_2 / 2)), nn.ReLU()]
block2_2 += [nn.Conv2d(128, 128, KS_2, 1, int(KS_2 / 2)), nn.ReLU()]
block3_1 += [nn.Conv2d(128, 128, KS_3, 1, int(KS_3 / 2)), nn.ReLU()]
block3_2 += [nn.Conv2d(128, 128, KS_3, 1, int(KS_3 / 2)), nn.ReLU()]
self.layerIn1_1 = nn.Conv2d(Dim[0], 64, 3, 1, 1)
self.layerIn1_2 = nn.Conv2d(64, 128, 3, 1, 1)
self.layerIn2_1 = nn.Conv2d(Dim[1], 64, 3, 1, 1)
self.layerIn2_2 = nn.Conv2d(64, 128, 3, 1, 1)
self.layerIn3_1 = nn.Conv2d(Dim[2], 64, 3, 1, 1)
self.layerIn3_2 = nn.Conv2d(64, 128, 3, 1, 1)
#Shared Imformation extraction layer, between three input
self.Infor = nn.Sequential(
*[
nn.Conv2d(387, 128, 3, 1, 1),
nn.ReLU(),
nn.Conv2d(128, 128, 3, 1, 1),
nn.ReLU(),
nn.Conv2d(128, 128, 3, 1, 1),
]
)
#Shared Imformation exchange layer between two stage
self.layerX_G = nn.Conv2d(387, 128, 3, 1, 1)
self.layerX_M = nn.Conv2d(128, 128, 3, 1, 1)
self.layerX_B = nn.Conv2d(128, 128, 3, 1, 1)
self.layerY_G = nn.Conv2d(387, 128, 3, 1, 1)
self.layerY_M = nn.Conv2d(128, 128, 3, 1, 1)
self.layerY_B = nn.Conv2d(128, 128, 3, 1, 1)
self.layerZ_G = nn.Conv2d(387, 128, 3, 1, 1)
self.layerZ_M = nn.Conv2d(128, 128, 3, 1, 1)
self.layerZ_B = nn.Conv2d(128, 128, 3, 1, 1)
self.layerOut1 = nn.Conv2d(384, 256, 3, 1, 1)
self.layerOut2 = nn.Conv2d(256, 128, 3, 1, 1)
self.layerOut3 = nn.Conv2d(128, 31, 3, 1, 1)
#backbone of three branch in two stage
self.branch1_1 = nn.Sequential(*block1_1)
self.branch1_2 = nn.Sequential(*block1_2)
self.branch2_1 = nn.Sequential(*block2_1)
self.branch2_2 = nn.Sequential(*block2_2)
self.branch3_1 = nn.Sequential(*block3_1)
self.branch3_2 = nn.Sequential(*block3_2)
self.Relu = nn.ReLU()
self.Sig = nn.Sigmoid()
def forward(self, x, y, z):
a = torch.ones(x.shape[2], x.shape[3]).unsqueeze(0)
b = torch.zeros(x.shape[2], x.shape[3]).unsqueeze(0)
self.MaskX = torch.cat((b,a,a), 0).unsqueeze(0).cuda()
self.MaskY = torch.cat((a,b,a), 0).unsqueeze(0).cuda()
self.MaskZ = torch.cat((a,a,b), 0).unsqueeze(0).cuda()
#Input processing
outIn_1 = self.layerIn1_2(self.Relu(self.layerIn1_1(x)))
outIn_2 = self.layerIn2_2(self.Relu(self.layerIn2_1(y)))
outIn_3 = self.layerIn3_2(self.Relu(self.layerIn3_1(z)))
#First stage
out1 = self.branch1_1(outIn_1)
out2 = self.branch2_1(outIn_2)
out3 = self.branch3_1(outIn_3)
Infor_x = self.Infor(torch.cat((out1, out2, out3, self.MaskX.repeat(out1.shape[0],1,1,1)),1))
out1_G = self.Sig(self.layerX_G(torch.cat((out1,out2,out3,self.MaskX.repeat(out1.shape[0],1,1,1)),1)))
out1_M = self.layerX_M(Infor_x)
out1_B = self.layerX_B(Infor_x)
out_1 = out1_G*out1 +(1-out1_G)*(out1*out1_M+out1_B)
Infor_y = self.Infor(torch.cat((out1, out2, out3, self.MaskY.repeat(out1.shape[0],1,1,1)),1))
out2_G = self.Sig(self.layerY_G(torch.cat((out1,out2,out3,self.MaskY.repeat(out1.shape[0],1,1,1)),1)))
out2_M = self.layerY_M(Infor_y)
out2_B = self.layerY_B(Infor_y)
out_2 = out2_G*out2 +(1-out2_G)*(out2*out2_M+out2_B)
Infor_Z = self.Infor(torch.cat((out1, out2, out3, self.MaskZ.repeat(out1.shape[0],1,1,1)),1))
out3_G = self.Sig(self.layerZ_G(torch.cat((out1,out2,out3,self.MaskZ.repeat(out1.shape[0],1,1,1)),1)))
out3_M = self.layerZ_M(Infor_Z)
out3_B = self.layerZ_B(Infor_Z)
out_3 = out3_G*out3 +(1-out3_G)*(out3*out3_M+out3_B)
#Second Stage
out1 = self.branch1_2(out_1)
out2 = self.branch2_2(out_2)
out3 = self.branch3_2(out_3)
Infor_x = self.Infor(torch.cat((out1, out2, out3, self.MaskX.repeat(out1.shape[0],1,1,1)),1))
out1_G = self.Sig(self.layerX_G(torch.cat((out1,out2,out3,self.MaskX.repeat(out1.shape[0],1,1,1)),1)))
out1_M = self.layerX_M(Infor_x)
out1_B = self.layerX_B(Infor_x)
out_1 = out1_G * out1 + (1 - out1_G) * (out1 * out1_M + out1_B) + outIn_1
Infor_y = self.Infor(torch.cat((out1, out2, out3, self.MaskY.repeat(out1.shape[0],1,1,1)),1))
out2_G = self.Sig(self.layerY_G(torch.cat((out1,out2,out3,self.MaskY.repeat(out1.shape[0],1,1,1)),1)))
out2_M = self.layerY_M(Infor_y)
out2_B = self.layerY_B(Infor_y)
out_2 = out2_G * out2 + (1 - out2_G) * (out2 * out2_M + out2_B) + outIn_2
Infor_Z = self.Infor(torch.cat((out1, out2, out3, self.MaskZ.repeat(out1.shape[0],1,1,1)),1))
out3_G = self.Sig(self.layerZ_G(torch.cat((out1,out2,out3,self.MaskZ.repeat(out1.shape[0],1,1,1)),1)))
out3_M = self.layerZ_M(Infor_Z)
out3_B = self.layerZ_B(Infor_Z)
out_3 = out3_G * out3 + (1 - out3_G) * (out3 * out3_M + out3_B) + outIn_3
#Output processing
out = self.Relu(self.layerOut1(torch.cat((out_1,out_2,out_3), 1)))
out = self.Relu(self.layerOut2(out))
out = self.layerOut3(out)
return out