-
Notifications
You must be signed in to change notification settings - Fork 42
/
mcnn_model.py
67 lines (58 loc) · 1.82 KB
/
mcnn_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
import torch
import torch.nn as nn
class MCNN(nn.Module):
'''
Implementation of Multi-column CNN for crowd counting
'''
def __init__(self):
super(MCNN,self).__init__()
self.branch1=nn.Sequential(
nn.Conv2d(3,16,9,padding=4),
nn.ReLU(inplace=True),
nn.MaxPool2d(2),
nn.Conv2d(16,32,7,padding=3),
nn.ReLU(inplace=True),
nn.MaxPool2d(2),
nn.Conv2d(32,16,7,padding=3),
nn.ReLU(inplace=True),
nn.Conv2d(16,8,7,padding=3),
nn.ReLU(inplace=True)
)
self.branch2=nn.Sequential(
nn.Conv2d(3,20,7,padding=3),
nn.ReLU(inplace=True),
nn.MaxPool2d(2),
nn.Conv2d(20,40,5,padding=2),
nn.ReLU(inplace=True),
nn.MaxPool2d(2),
nn.Conv2d(40,20,5,padding=2),
nn.ReLU(inplace=True),
nn.Conv2d(20,10,5,padding=2),
nn.ReLU(inplace=True)
)
self.branch3=nn.Sequential(
nn.Conv2d(3,24,5,padding=2),
nn.ReLU(inplace=True),
nn.MaxPool2d(2),
nn.Conv2d(24,48,3,padding=1),
nn.ReLU(inplace=True),
nn.MaxPool2d(2),
nn.Conv2d(48,24,3,padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(24,12,3,padding=1),
nn.ReLU(inplace=True)
)
self.fuse=nn.Sequential(nn.Conv2d(30,1,1,padding=0))
def forward(self,img_tensor):
x1=self.branch1(img_tensor)
x2=self.branch2(img_tensor)
x3=self.branch3(img_tensor)
x=torch.cat((x1,x2,x3),1)
x=self.fuse(x)
return x
# test code
if __name__=="__main__":
img=torch.rand((1,3,800,1200),dtype=torch.float)
mcnn=MCNN()
out_dmap=mcnn(img)
print(out_dmap.shape)