Single Shot Multibox Architecture The network is composed of a base VGG network followed by the added multibox conv layers. Each multibox layer branches into 1) conv2d for class conf scores 2) conv2d for localization predictions 3) associated priorbox layer to produc
| 8 | |
| 9 | |
| 10 | class SSD(nn.Module): |
| 11 | """Single Shot Multibox Architecture |
| 12 | The network is composed of a base VGG network followed by the |
| 13 | added multibox conv layers. Each multibox layer branches into |
| 14 | 1) conv2d for class conf scores |
| 15 | 2) conv2d for localization predictions |
| 16 | 3) associated priorbox layer to produce default bounding |
| 17 | boxes specific to the layer's feature map size. |
| 18 | See: https://arxiv.org/pdf/1512.02325.pdf for more details. |
| 19 | |
| 20 | Args: |
| 21 | phase: (string) Can be "test" or "train" |
| 22 | size: input image size |
| 23 | base: VGG16 layers for input, size of either 300 or 500 |
| 24 | extras: extra layers that feed to multibox loc and conf layers |
| 25 | head: "multibox head" consists of loc and conf conv layers |
| 26 | """ |
| 27 | |
| 28 | def __init__(self, phase, size, base, extras, head, num_classes): |
| 29 | super(SSD, self).__init__() |
| 30 | self.phase = phase |
| 31 | self.num_classes = num_classes |
| 32 | self.cfg = (coco, voc)[num_classes == 21] |
| 33 | self.priorbox = PriorBox(self.cfg) |
| 34 | self.priors = Variable(self.priorbox.forward(), volatile=True) |
| 35 | self.size = size |
| 36 | |
| 37 | # SSD network |
| 38 | self.vgg = nn.ModuleList(base) |
| 39 | # Layer learns to scale the l2 normalized features from conv4_3 |
| 40 | self.L2Norm = L2Norm(512, 20) |
| 41 | self.extras = nn.ModuleList(extras) |
| 42 | |
| 43 | self.loc = nn.ModuleList(head[0]) |
| 44 | self.conf = nn.ModuleList(head[1]) |
| 45 | |
| 46 | if phase == 'test': |
| 47 | self.softmax = nn.Softmax(dim=-1) |
| 48 | self.detect = Detect(num_classes, 0, 200, 0.01, 0.45) |
| 49 | |
| 50 | def forward(self, x): |
| 51 | """Applies network layers and ops on input image(s) x. |
| 52 | |
| 53 | Args: |
| 54 | x: input image or batch of images. Shape: [batch,3,300,300]. |
| 55 | |
| 56 | Return: |
| 57 | Depending on phase: |
| 58 | test: |
| 59 | Variable(tensor) of output class label predictions, |
| 60 | confidence score, and corresponding location predictions for |
| 61 | each object detected. Shape: [batch,topk,7] |
| 62 | |
| 63 | train: |
| 64 | list of concat outputs from: |
| 65 | 1: confidence layers, Shape: [batch*num_priors,num_classes] |
| 66 | 2: localization layers, Shape: [batch,num_priors*4] |
| 67 | 3: priorbox layers, Shape: [2,num_priors*4] |