pytorch SSD 源码阅读: ssd-forward

    def forward(self, x):
        """Applies network layers and ops on input image(s) x.

        Args:
            x: input image or batch of images. Shape: [batch,3,300,300].

        Return:
            Depending on phase:
            test:
                Variable(tensor) of output class label predictions,
                confidence score, and corresponding location predictions for
                each object detected. Shape: [batch,topk,7]

            train:
                list of concat outputs from:
                    1: confidence layers, Shape: [batch*num_priors,num_classes]
                    2: localization layers, Shape: [batch,num_priors*4]
                    3: priorbox layers, Shape: [2,num_priors*4]
        """
        sources = list() # sources 是为了保存用于获得回归和置信度评分之前的6个特征图的网络结构
        loc = list()  # 特征图传入位置回归的2d卷积层后的6个结构
        conf = list()  # 特征图传入置信度评分的2d卷积层后的6个结构

sources = list() # sources 是为了保存用于获得回归和置信度评分之前的6个特征图的网络结构
loc = list() # 特征图传入位置回归的2d卷积层后的6个结构
conf = list() # 特征图传入置信度评分的2d卷积层后的6个结构

        # apply vgg up to conv4_3 relu
        for k in range(23):
            x = self.vgg[k](x)

        s = self.L2Norm(x)
        sources.append(s)

        # apply vgg up to fc7
        for k in range(23, len(self.vgg)):
            x = self.vgg[k](x)
        sources.append(x)

        # apply extra layers and cache source layer outputs
        for k, v in enumerate(self.extras):
            x = F.relu(v(x), inplace=True)
            if k % 2 == 1:
                sources.append(x)

添加vgg结构的第23层卷积层+ L2Norm作为第一个特征图
添加vgg网络的倒数第二层(也就是去掉relu的最后一个卷积层)作为第二个特征图
添加extra_layers中的4层卷积层作为剩下的4个特征图

        # apply multibox head to source layers
        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())

        loc = torch.cat([o.view(o.size(0), -1) for o in loc], 1)
        conf = torch.cat([o.view(o.size(0), -1) for o in conf], 1)
        if self.phase == "test":
            output = self.detect(
                loc.view(loc.size(0), -1, 4),  # loc preds
                self.softmax(conf.view(conf.size(0), -1,
                                       self.num_classes)),  # conf preds
                self.priors.type(type(x.data))  # default boxes
            )
        else:
            output = (
                loc.view(loc.size(0), -1, 4),
                conf.view(conf.size(0), -1, self.num_classes),
                self.priors
            )
        return output

第一个循环构建,特征图传入位置回归的2d卷积层后的6个结构 和 特征图传入置信度评分的2d卷积层后的6个结构。剩下的如果是test则构建softmax用以分类,如果是train就将预测的结果返回。





除非注明,否则均为一叶呼呼原创文章,转载必须以链接形式标明本文链接

本文链接:http://www.yiyehu.tech/archives/2020/04/01/ssd-sourcecode-ssd-forward

发表评论

电子邮件地址不会被公开。 必填项已用*标注