这是对于Single-Image Crowd Counting via Multi-Column Convolutional Neural Network(2016 CVPR)中所提出的MCNN模型的基于pytorch的实现
import torch
#import torchstat as te;import numpy
class my_MCNN(torch.nn.Module):
def __init__(self):
super(my_easy_MCNN,self).__init__()
self.conv9_1=torch.nn.Conv2d(3,16,9,padding=4)
self.conv7_1_1=torch.nn.Conv2d(16,32,7,padding=3)
self.conv7_1_2 = torch.nn.Conv2d(32, 16, 7,padding=3)
self.conv7_1_3 = torch.nn.Conv2d(16, 8, 7,padding=3)
self.conv7_2_1 = torch.nn.Conv2d(3, 20, 7,padding=3)
self.conv5_2_1 = torch.nn.Conv2d(20, 40, 5,padding=2)
self.conv5_2_2 = torch.nn.Conv2d(40, 20, 5,padding=2)
self.conv5_2_3 = torch.nn.Conv2d(20, 10, 5,padding=2)
self.conv5_3_1 = torch.nn.Conv2d(3, 24, 5,padding=2)
self.conv3_3_1 = torch.nn.Conv2d(24, 48, 3,padding=1)
self.conv3_3_2 = torch.nn.Conv2d(48, 24, 3,padding=1)
self.conv3_3_3 = torch.nn.Conv2d(24, 12, 3,padding=1)
#补零是为了让三个分支输出形状相同
self.maxpool=torch.nn.MaxPool2d(2)
self.relu=torch.nn.functional.relu
self.fuse=torch.nn.Conv2d(30,1,1)
def forward(self,x):
b1=self.maxpool(self.relu(self.conv9_1(x)))
#print(b1.shape)
b1 = self.maxpool(self.relu(self.conv7_1_1(b1)))
b1=self.relu(self.conv7_1_2(b1))
b1=self.relu(self.conv7_1_3(b1))
b2 = self.maxpool(self.relu(self.conv7_2_1(x)))
b2 = self.maxpool(self.relu(self.conv5_2_1(b2)))
b2 = self.relu(self.conv5_2_2(b2))
b2=self.relu(self.conv5_2_3(b2))
b3 = self.maxpool(self.relu(self.conv5_3_1(x)))
#print(b3.shape)
b3 = self.maxpool(self.relu(self.conv3_3_1(b3)))
b3 = self.relu(self.conv3_3_2(b3))
b3 = self.relu(self.conv3_3_3(b3))
y=torch.cat((b1,b2,b3),1)
y=self.fuse(y)
return y
class mynet(torch.nn.Module):
def __init__(self):
super(mynet,self).__init__()
mynet.c=torch.nn.Linear(1200,1,5)
def forward(self,x):
h=self.c(x)
return h