-
Notifications
You must be signed in to change notification settings - Fork 0
/
cost_model.py
206 lines (186 loc) · 7.99 KB
/
cost_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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import torch
import torch.nn as nn
#from mypath import Path
from torch.nn import functional as F
class attentionblock(nn.Module):
def __init__(self,in_channel):
super(attentionblock,self).__init__()
#self.num_classes=num_classes
self.pool=nn.AdaptiveMaxPool3d(1)
self.in_channel=in_channel
self.conv=nn.Conv3d(self.in_channel,self.in_channel,kernel_size=(1,1,1))
self.linear=nn.Linear(self.in_channel*3,self.in_channel*3)
#print(self.linear)
def forward(self,input1,input2,input3):
#print(input1.shape,"inpu1")
#print(input2.shape,"input2")
input1=self.pool(input1)
input2=self.pool(input2)
input3=self.pool(input3)
input1=self.conv(input1)
input2 = self.conv(input2)
input3 = self.conv(input3)
input1=input1.squeeze()
input2 = input2.squeeze()
input3 = input3.squeeze()
#print(input1.shape,"inpu1")
#print(input2.shape,"inpu2")
#print(input3.shape,"inpu3")
a=torch.cat((input1,input2,input3),1)
#print(a.shape)
a=self.linear(a)
#print(a.shape,"attena")
a=F.softmax(a,dim=0)
return a
class costblock(nn.Module):
def __init__(self,in_channel,channel,stride=1):
super(costblock,self).__init__()
#self.size=size
self.stride=stride
#self.num_classes=num_classes
self.in_channel=in_channel
self.channel=channel
self.bn1=nn.BatchNorm3d(self.channel)
self.bn2=nn.BatchNorm3d(self.channel*4)
#self.stride=stride
self.conv1=nn.Conv3d(self.in_channel,self.channel,kernel_size=(1,1,1))
self.conv=nn.Conv2d(self.channel,self.channel,kernel_size=(3,3),padding=(1,1),stride=(self.stride,self.stride))
self.conv2=nn.Conv3d(self.channel,self.channel*4,kernel_size=(1,1,1))
self.attenblock=attentionblock(self.channel)
self.relu=nn.ReLU(inplace=True)
self.batchnorm=nn.BatchNorm2d(self.channel)
self.downsample = nn.Sequential()
if self.stride != 1 or self.in_channel != self.channel * 4:
self.downsample = nn.Sequential(
nn.Conv3d(self.in_channel, self.channel * 4, kernel_size=1, stride=(1,self.stride,self.stride), bias=False),
nn.BatchNorm3d(self.channel * 4)
)
def forward(self, input):
shortcut =self.downsample(input)
#print(shortcut.shape)
input=self.conv1(input)
input=self.bn1(input)
input=self.relu(input)
x1=input.view(input.shape[0],input.shape[1],input.shape[2],input.shape[3]*input.shape[4])
x2=input.transpose(2,3)
x2=x2.contiguous().view(x2.shape[0],x2.shape[1],x2.shape[2],x2.shape[3]*x2.shape[4])
x3=input.transpose(2,4)
x3 = x3.contiguous().view(x3.shape[0], x3.shape[1], x3.shape[2], x3.shape[3] * x3.shape[4])
#print(input.shape[3])
#out1=self.conv(x1).view(input.shape[0],input.shape[1],input.shape[2],int(input.shape[3]/self.stride),int(input.shape[4]/self.stride))
#out1=self.batchnorm(out1)
#out1=self.relu(out1)
#out2=self.conv(x2).view(input.shape[0],input.shape[1],input.shape[2],int(input.shape[3]/self.stride),int(input.shape[4]/self.stride))
#out3=self.conv(x3).view(input.shape[0],input.shape[1],input.shape[2],int(input.shape[3]/self.stride),int(input.shape[4]/self.stride))
out1=self.conv(x1)
out1=self.batchnorm(out1)
out1=self.relu(out1)
out1=out1.view(input.shape[0],input.shape[1],input.shape[2],int(input.shape[3]/self.stride),int(input.shape[4]/self.stride))
out2=self.conv(x2)
out2=self.batchnorm(out2)
out2=self.relu(out2)
out2 = out2.view(input.shape[0], input.shape[1], input.shape[2], int(input.shape[3] / self.stride),int(input.shape[4] / self.stride))
out3=self.conv(x3)
out3=self.batchnorm(out3)
out3=self.relu(out3)
out3 = out3.view(input.shape[0], input.shape[1], input.shape[2], int(input.shape[3] / self.stride),int(input.shape[4] / self.stride))
#out=torch.cat((out1,out2,out3),1)
a=self.attenblock(out1,out2,out3)
a1,a2,a3=a.chunk(3,dim=1)
output1=out1.permute(2,3,4,0,1)*a1+out2.permute(2,3,4,0,1)*a2+out3.permute(2,3,4,0,1)*a3
output1=output1.permute(3,4,0,1,2)
output1=self.conv2(output1)
output1=self.bn2(output1)
#print(output1.shape)
output1=output1+shortcut
output1=self.relu(output1)
return output1
class Cost(nn.Module):
"""
The Cost network.
"""
def __init__(self, num_classes,block, layers,pretrained=False):
super(Cost, self).__init__()
self.in_channels=64
self.conv1=nn.Conv3d(3, 64, kernel_size=(3, 7, 7), stride=(1, 2, 2), padding=(1, 3, 3), bias=False)
self.bn1 = nn.BatchNorm3d(64)
self.relu = nn.ReLU(inplace=True)
self.max_pool = nn.MaxPool3d(kernel_size=(1, 3, 3), stride=(1, 2, 2), padding=(0, 1, 1))
self.layer1 = self._make_layer(block, 64, layers[0], stride=1)
self.layer2 = self._make_layer(block, 128, layers[1], stride=2)
self.layer3 = self._make_layer(block, 256, layers[2], stride=2)
self.layer4 = self._make_layer(block, 512, layers[3], stride=2)
self.avg_pool = nn.AdaptiveAvgPool3d(1)
self.dropout = nn.Dropout(p=0.5)
#self.fc = nn.Linear(512 * 4, num_classes)
self.fc1 = nn.Linear(2048, 1024)
self.fc2 = nn.Linear(1024, 1024)
self.fc3 = nn.Linear(1024, num_classes)
self.__init_weight()
def forward(self, x):
out = self.conv1(x)
out = self.bn1(out)
out = self.relu(out)
out = self.max_pool(out)
#print(out.shape)
out = self.layer1(out)
out = self.layer2(out)
out = self.layer3(out)
out = self.layer4(out)
out = self.avg_pool(out)
out = out.view(out.size(0), -1)
#out = self.fc(out)
out = self.relu(self.fc1(out))
out = self.dropout(out)
out = self.relu(self.fc2(out))
out = self.dropout(out)
out = self.fc3(out)
return out
def _make_layer(self, block, channels, n_blocks, stride=1):
assert n_blocks > 0, "number of blocks should be greater than zero"
layers = []
layers.append(block(self.in_channels, channels, stride))
self.in_channels = channels * 4
for i in range(1, n_blocks):
layers.append(block(self.in_channels, channels))
return nn.Sequential(*layers)
def __init_weight(self):
for m in self.modules():
if isinstance(m, nn.Conv3d):
# n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
# m.weight.data.normal_(0, math.sqrt(2. / n))
torch.nn.init.kaiming_normal_(m.weight)
elif isinstance(m, nn.Conv2d):
torch.nn.init.kaiming_normal_(m.weight)
elif isinstance(m, nn.BatchNorm2d):
m.weight.data.fill_(1)
m.bias.data.zero_()
elif isinstance(m, nn.BatchNorm3d):
m.weight.data.fill_(1)
m.bias.data.zero_()
def get_1x_lr_params(model):
"""
This generator returns all the parameters for conv and two fc layers of the net.
"""
b = [model.conv1, model.layer1, model.layer2, model.layer3, model.layer4, model.fc1,
model.fc2]
for i in range(len(b)):
for k in b[i].parameters():
if k.requires_grad:
yield k
def get_10x_lr_params(model):
"""
This generator returns all the parameters for the last fc layer of the net.
"""
b = [model.fc3]
for j in range(len(b)):
for k in b[j].parameters():
if k.requires_grad:
yield k
if __name__ == "__main__":
inputs = torch.rand(2, 3, 16, 224, 224)
net=Cost(101,costblock, [3,4,6,3])
#net = costblock(64,64,stride=2)
print(net.layer4[1].conv.weight.grad)
outputs = net(inputs)
print(outputs.size())