1、一段奇怪函数的研究
for (x, l, c) in zip(sources, self.loc, self.conf):
loc.append(l(x).permute(0, 2, 3, 1).contiguous())
conf.append(c(x).permute(0, 2, 3, 1).contiguous())
结论:这段代码的作用是,通过一个不常见的python迭代器函数zip(),将6个卷积层的输出sources[],经过6个不同的分类器(每个self.loc和self.conf为一对),最后将得的回归和分类结果,并且还reshape了一下。
2、各自的类型
针对上述文章的ssd分类头的研究
(12)目标检测_SSD基于pytorch搭建代码_chencaw的博客-CSDN博客
(1)其中sources和局部变量loc,局部变量conf是list类型,定义为:
2.1 sources的研究
(1)sources是各个分类头的输出tensor的集合 ,
注意,sources是VGG等卷积层的输出,准备给到6个不同分类器的
其中第1段如下所示:
(2)把它的内容都打出来看看
使用方法:
print("List 类型sources的大小:",len(sources))
for i in range(len(sources)):
print("sources[]",i,sources[i].shape)
显示结果
2.2 self.loc和self.conf的内容研究
(1)6个不同分类头的神经网络结构self.loc和self.conf,定义如下所示
(2)假设分类的num为3,也就是2个目标+背景,总个数为3个。
net = SSD300(num_classes=3, backbone_name="vgg",pretrained=False)
x =torch.rand(1,3,300,300)
测试一下 def forward(self, x):中的self.loc和self.conf
print("查看 self.loc内容")
print(self.loc)
print("查看 self.conf")
print(self.conf)
显示结果如下:
2.3 for (x, l, c) in zip(sources, self.loc, self.conf)的含义研究
(1)测试代码
chen_count =0
for (x, l, c) in zip(sources, self.loc, self.conf):
chen_count += 1
print(chen_count, "输入x的shape:", x.shape)
ltest = l(x).permute(0, 2, 3, 1).contiguous().detach().numpy()
ctest = c(x).permute(0, 2, 3, 1).contiguous().detach().numpy()
print("l(x)处理后的shape :", ltest.shape)
print("c(x)处理后的shape :", ctest.shape)
loc.append(l(x).permute(0, 2, 3, 1).contiguous())
conf.append(c(x).permute(0, 2, 3, 1).contiguous())
(2)测试结果