def get_ssd(phase, num_classes): vgg, extra_layers = add_vgg(3), add_extras(1024) loc_layers = [] conf_layers = [] vgg_source = [21, -2] for k, v in enumerate(vgg_source): loc_layers += [ nn.Conv2d(vgg[v].out_channels, mbox[k] * 4, kernel_size=3, padding=1) ] conf_layers += [ nn.Conv2d(vgg[v].out_channels, mbox[k] * num_classes, kernel_size=3, padding=1) ] for k, v in enumerate(extra_layers[1::2], 2): loc_layers += [ nn.Conv2d(v.out_channels, mbox[k] * 4, kernel_size=3, padding=1) ] conf_layers += [ nn.Conv2d(v.out_channels, mbox[k] * num_classes, kernel_size=3, padding=1) ] SSD_MODEL = SSD(phase, vgg, extra_layers, (loc_layers, conf_layers), num_classes) return SSD_MODEL
def get_ssd(phase, num_classes): vgg, extra_layers = add_vgg(3), add_extras(1024) loc_layers = [] conf_layers = [] vgg_source = [21, -2] for k, v in enumerate( vgg_source): # k是索引,v是枚举出来得对象(0 21) (1 -2)提取出21层和33层对应(4,6) loc_layers += [ nn.Conv2d(vgg[v].out_channels, mbox[k] * 4, kernel_size=3, padding=1) ] conf_layers += [ nn.Conv2d(vgg[v].out_channels, mbox[k] * num_classes, kernel_size=3, padding=1) ] for k, v in enumerate(extra_layers[1::2], 2): # (2,1)(3,3)(4,5)(5,7) loc_layers += [ nn.Conv2d(v.out_channels, mbox[k] * 4, kernel_size=3, padding=1) ] conf_layers += [ nn.Conv2d(v.out_channels, mbox[k] * num_classes, kernel_size=3, padding=1) ] SSD_MODEL = SSD(phase, vgg, extra_layers, (loc_layers, conf_layers), num_classes) return SSD_MODEL
def get_ssd(phase, num_classes, confidence=0.5, nms_iou=0.45): #---------------------------------------------------# # add_vgg指的是加入vgg主干特征提取网络。 # 该网络的最后一个特征层是conv7后的结果。 # shape为19,19,1024。 # # 为了更好的提取出特征用于预测。 # SSD网络会继续进行下采样。 # add_extras是额外下采样的部分。 #---------------------------------------------------# vgg, extra_layers = add_vgg(3), add_extras(1024) loc_layers = [] conf_layers = [] vgg_source = [21, -2] #---------------------------------------------------# # 在add_vgg获得的特征层里 # 第21层和-2层可以用来进行回归预测和分类预测。 # 分别是conv4-3(38,38,512)和conv7(19,19,1024)的输出 #---------------------------------------------------# for k, v in enumerate(vgg_source): loc_layers += [ nn.Conv2d(vgg[v].out_channels, mbox[k] * 4, kernel_size=3, padding=1) ] conf_layers += [ nn.Conv2d(vgg[v].out_channels, mbox[k] * num_classes, kernel_size=3, padding=1) ] #-------------------------------------------------------------# # 在add_extras获得的特征层里 # 第1层、第3层、第5层、第7层可以用来进行回归预测和分类预测。 # shape分别为(10,10,512), (5,5,256), (3,3,256), (1,1,256) #-------------------------------------------------------------# for k, v in enumerate(extra_layers[1::2], 2): loc_layers += [ nn.Conv2d(v.out_channels, mbox[k] * 4, kernel_size=3, padding=1) ] conf_layers += [ nn.Conv2d(v.out_channels, mbox[k] * num_classes, kernel_size=3, padding=1) ] #-------------------------------------------------------------# # add_vgg和add_extras,一共获得了6个有效特征层,shape分别为: # (38,38,512), (19,19,1024), (10,10,512), # (5,5,256), (3,3,256), (1,1,256) #-------------------------------------------------------------# SSD_MODEL = SSD(phase, vgg, extra_layers, (loc_layers, conf_layers), num_classes, confidence, nms_iou) return SSD_MODEL
def __init__(self, num_classes, backbone_name, pretrained = False): super(SSD300, self).__init__() self.num_classes = num_classes if backbone_name == "vgg": self.vgg = add_vgg(pretrained) self.extras = add_extras(1024, backbone_name) self.L2Norm = L2Norm(512, 20) mbox = [4, 6, 6, 6, 4, 4] loc_layers = [] conf_layers = [] backbone_source = [21, -2] #---------------------------------------------------# # 在add_vgg获得的特征层里 # 第21层和-2层可以用来进行回归预测和分类预测。 # 分别是conv4-3(38,38,512)和conv7(19,19,1024)的输出 #---------------------------------------------------# for k, v in enumerate(backbone_source): loc_layers += [nn.Conv2d(self.vgg[v].out_channels, mbox[k] * 4, kernel_size = 3, padding = 1)] conf_layers += [nn.Conv2d(self.vgg[v].out_channels, mbox[k] * num_classes, kernel_size = 3, padding = 1)] #-------------------------------------------------------------# # 在add_extras获得的特征层里 # 第1层、第3层、第5层、第7层可以用来进行回归预测和分类预测。 # shape分别为(10,10,512), (5,5,256), (3,3,256), (1,1,256) #-------------------------------------------------------------# for k, v in enumerate(self.extras[1::2], 2): loc_layers += [nn.Conv2d(v.out_channels, mbox[k] * 4, kernel_size = 3, padding = 1)] conf_layers += [nn.Conv2d(v.out_channels, mbox[k] * num_classes, kernel_size = 3, padding = 1)] else: self.mobilenet = mobilenet_v2(pretrained).features self.extras = add_extras(1280, backbone_name) self.L2Norm = L2Norm(96, 20) mbox = [6, 6, 6, 6, 6, 6] loc_layers = [] conf_layers = [] backbone_source = [13, -1] for k, v in enumerate(backbone_source): loc_layers += [nn.Conv2d(self.mobilenet[v].out_channels, mbox[k] * 4, kernel_size = 3, padding = 1)] conf_layers += [nn.Conv2d(self.mobilenet[v].out_channels, mbox[k] * num_classes, kernel_size = 3, padding = 1)] for k, v in enumerate(self.extras, 2): loc_layers += [nn.Conv2d(v.out_channels, mbox[k] * 4, kernel_size = 3, padding = 1)] conf_layers += [nn.Conv2d(v.out_channels, mbox[k] * num_classes, kernel_size = 3, padding = 1)] self.loc = nn.ModuleList(loc_layers) self.conf = nn.ModuleList(conf_layers) self.backbone_name = backbone_name
def get_ssd(phase,num_classes): #获得这些层 vgg, extra_layers = add_vgg(3), add_extras(1024)#in chnnels loc_layers = [] conf_layers = [] vgg_source = [21, -2]#取出conv4-3进行回归预测,分类预测 for k, v in enumerate(vgg_source):# 0,21,,,,,,1,-2 loc_layers += [nn.Conv2d(vgg[v].out_channels, mbox[k] * 4, kernel_size=3, padding=1)]#*4是因为中心点坐标2个,长宽2个,共可能调整的参数有4个,回归预测 conf_layers += [nn.Conv2d(vgg[v].out_channels, mbox[k] * num_classes, kernel_size=3, padding=1)]# * num_classes是分类的种类,mbox[k]是在小格内预先设定k种不同尺寸anchor,分类预测 for k, v in enumerate(extra_layers[1::2], 2): loc_layers += [nn.Conv2d(v.out_channels, mbox[k] * 4, kernel_size=3, padding=1)]# 回归预测 conf_layers += [nn.Conv2d(v.out_channels, mbox[k] * num_classes, kernel_size=3, padding=1)]# 分类预测 SSD_MODEL = SSD(phase, vgg, extra_layers, (loc_layers, conf_layers), num_classes) return SSD_MODEL