Esempio n. 1
0
def _conv_dw(in_channels, out_channels, stride):
    return nn.Sequential(
        nn.Conv2d(in_channels=in_channels, out_channels=in_channels, kernel_size=3, stride=stride, padding=1, groups=in_channels, bias=False),
        nn.BatchNorm2d(in_channels),
        nn.ReLU(inplace=True),
        nn.Conv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=1, stride=1, padding=0, bias=False),
        nn.BatchNorm2d(out_channels),
        nn.ReLU(inplace=True)
    )
Esempio n. 2
0
def conv_dw(inp, oup, stride, leaky=0.1):
    return nn.Sequential(
        nn.Conv2d(inp, inp, 3, stride, 1, groups=inp, bias=False),
        nn.BatchNorm2d(inp),
        nn.LeakyReLU(negative_slope=leaky, inplace=True),
        nn.Conv2d(inp, oup, 1, 1, 0, bias=False),
        nn.BatchNorm2d(oup),
        nn.LeakyReLU(negative_slope=leaky, inplace=True),
    )
 def __init__(self, in_channels, out_channels, kernel_size, stride, padding, **kwargs):
     super(ConvBNReLU, self).__init__()
     self.in_channels = in_channels
     self.out_channels = out_channels
     self.conv = nn.Conv2d(in_channels, out_channels, kernel_size, stride, padding, bias=True, **kwargs)
     self.bn = nn.BatchNorm2d(out_channels)
     self.relu = nn.ReLU(inplace=True)
Esempio n. 4
0
 def __init__(self, inplanes, planes, stride=1, downsample=None):
     super(Bottleneck, self).__init__()
     self.conv1 = nn.Conv2d(inplanes, planes, kernel_size=1, bias=False)
     self.bn1 = nn.BatchNorm2d(planes)
     self.conv2 = nn.Conv2d(planes,
                            planes,
                            kernel_size=3,
                            stride=stride,
                            padding=1,
                            bias=False)
     self.bn2 = nn.BatchNorm2d(planes)
     self.conv3 = nn.Conv2d(planes, planes * 4, kernel_size=1, bias=False)
     self.bn3 = nn.BatchNorm2d(planes * 4)
     self.relu = nn.ReLU(inplace=True)
     self.downsample = downsample
     self.stride = stride
     self.eltadd = nn.EltAdd()
Esempio n. 5
0
    def __init__(self, inp, oup, stride, expand_ratio, model_type=32):
        super(InvertedResidual_dwc, self).__init__()
        self.stride = stride
        assert stride in [1, 2]

        hidden_dim = int(round(inp * expand_ratio))
        self.use_res_connect = self.stride == 1 and inp == oup

        self.conv = []

        if expand_ratio == 1:
            self.conv.append(
                nn.Conv2d(inp,
                          hidden_dim,
                          kernel_size=(3, 3),
                          stride=stride,
                          padding=1,
                          groups=hidden_dim))
            self.conv.append(nn.BatchNorm2d(hidden_dim))
            self.conv.append(nn.PReLU())
            self.conv.append(nn.Conv2d(hidden_dim, oup, 1, 1, 0, bias=False))
            self.conv.append(nn.BatchNorm2d(oup))
            if model_type == 32:
                self.conv.append(nn.PReLU())
        else:
            self.conv.append(nn.Conv2d(inp, hidden_dim, 1, 1, 0, bias=False))
            self.conv.append(nn.BatchNorm2d(hidden_dim))
            self.conv.append(nn.PReLU())
            self.conv.append(
                nn.Conv2d(hidden_dim,
                          hidden_dim,
                          kernel_size=(3, 3),
                          stride=stride,
                          padding=1,
                          groups=hidden_dim))
            self.conv.append(nn.BatchNorm2d(hidden_dim))
            self.conv.append(nn.PReLU())
            self.conv.append(nn.Conv2d(hidden_dim, oup, 1, 1, 0, bias=False))
            self.conv.append(nn.BatchNorm2d(oup))
            if model_type == 32:
                self.conv.append(nn.PReLU())

        self.conv = nn.Sequential(*self.conv)
        if self.use_res_connect:
            self.eltadd = nn.EltAdd()
Esempio n. 6
0
    def __init__(self, in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1):
        super(IdentityBlock, self).__init__()

        out_channels_1, out_channels_2, out_channels_3 = out_channels//4, out_channels//4, out_channels

        self.conv1 = nn.Conv2d(in_channels, out_channels_1, kernel_size=(1, 1))
        self.bn1 = nn.BatchNorm2d(out_channels_1)
        self.relu1 = nn.ReLU(inplace=True)

        self.conv2 = nn.Conv2d(out_channels_1, out_channels_2, kernel_size=(kernel_size, kernel_size), padding=(padding, padding), dilation=(dilation, dilation))
        self.bn2 = nn.BatchNorm2d(out_channels_2)
        self.relu2 = nn.ReLU(inplace=True)

        self.conv3 = nn.Conv2d(out_channels_2, out_channels_3, kernel_size=(1, 1))
        self.bn3 = nn.BatchNorm2d(out_channels_3)

        self.eltadd = nn.EltAdd()
        self.relu_f = nn.ReLU(inplace=True)
Esempio n. 7
0
 def __init__(self, in_planes, out_planes, kernel_size, stride=1, padding=0, dilation=1, groups=1, relu=True, bn=True):
     super(BasicConv, self).__init__()
     self.out_channels = out_planes
     if bn:
         self.conv = nn.Conv2d(in_planes, out_planes, kernel_size=kernel_size, stride=stride, padding=padding, dilation=dilation, groups=groups, bias=False)
         self.bn = nn.BatchNorm2d(out_planes, eps=1e-5, momentum=0.01, affine=True)
         self.relu = nn.ReLU(inplace=True) if relu else None
     else:
         self.conv = nn.Conv2d(in_planes, out_planes, kernel_size=kernel_size, stride=stride, padding=padding, dilation=dilation, groups=groups, bias=True)
         self.bn = None
         self.relu = nn.ReLU(inplace=True) if relu else None
Esempio n. 8
0
    def __init__(self, in_channel, out_channel, kernel_size, stride=1, padding=0, bias=False, add_relu=True, add_bn=True, eps=1e-5):
        super(ConvBlock, self).__init__()

        self.conv = nn.Conv2d(in_channel, out_channel, kernel_size, stride, padding, bias=bias)
        self.relu = None
        self.bn = None

        if add_relu:
            self.relu = nn.ReLU()
        if add_bn:
            self.bn = nn.BatchNorm2d(out_channel, eps=eps)
Esempio n. 9
0
def make_layers(cfg, batch_norm=False):
    layers = []
    in_channels = 3
    for v in cfg:
        if v == 'M':
            layers += [nn.MaxPool2d(kernel_size=2, stride=2)]
        else:
            conv2d = nn.Conv2d(in_channels, v, kernel_size=3, padding=1)
            if batch_norm:
                layers += [conv2d, nn.BatchNorm2d(v), nn.ReLU(inplace=True)]
            else:
                layers += [conv2d, nn.ReLU(inplace=True)]
            in_channels = v
    return layers
Esempio n. 10
0
def upsample(in_channels, out_channels):  # should use F.inpterpolate
    return nn.Sequential(
        nn.Conv2d(in_channels=in_channels,
                  out_channels=in_channels,
                  kernel_size=(3, 3),
                  stride=1,
                  padding=1,
                  groups=in_channels,
                  bias=False),
        nn.Conv2d(in_channels=in_channels,
                  out_channels=out_channels,
                  kernel_size=1,
                  stride=1,
                  padding=0,
                  bias=False), nn.BatchNorm2d(out_channels), nn.ReLU())
Esempio n. 11
0
    def _make_layer(self, block, planes, blocks, stride=1):
        downsample = None
        if stride != 1 or self.inplanes != planes * block.expansion:
            downsample = nn.Sequential(
                nn.Conv2d(self.inplanes,
                          planes * block.expansion,
                          kernel_size=1,
                          stride=stride,
                          bias=False),
                nn.BatchNorm2d(planes * block.expansion),
            )

        layers = []
        layers.append(block(self.inplanes, planes, stride, downsample))
        self.inplanes = planes * block.expansion
        for i in range(1, blocks):
            layers.append(block(self.inplanes, planes))

        return nn.Sequential(*layers)
Esempio n. 12
0
 def __init__(self,
              in_channels,
              out_channels,
              kernel_sizes,
              strides=1,
              paddings=0,
              act='relu',
              bias=False):
     super(Conv_BN, self).__init__()
     self.conv = nn.Conv2d(in_channels,
                           out_channels,
                           kernel_sizes,
                           strides,
                           paddings,
                           bias=bias)
     self.bn = nn.BatchNorm2d(out_channels, eps=0.001, momentum=0.999)
     self.act = None
     if act == 'relu':
         self.act = nn.ReLU(inplace=True)
Esempio n. 13
0
def conv_bn_no_relu(inp, oup, stride):
    return nn.Sequential(
        nn.Conv2d(inp, oup, 3, stride, 1, bias=False),
        nn.BatchNorm2d(oup),
    )
Esempio n. 14
0
 def __init__(self, in_channels, out_channels, **kwargs):
     super(BasicConv2d, self).__init__()
     self.conv = nn.Conv2d(in_channels, out_channels, bias=False, **kwargs)
     self.bn = nn.BatchNorm2d(out_channels, eps=1e-5)
     self.relu = nn.ReLU()
Esempio n. 15
0
    def __init__(self):
        super(DSFD, self).__init__()
        self.size = 640
        self.num_classes = 2

        ######
        # build backbone
        ######
        resnet152 = vision.models.resnet152()
        self.layer1 = nn.Sequential(resnet152.conv1, resnet152.bn1,
                                    resnet152.relu, resnet152.maxpool,
                                    resnet152.layer1)
        self.layer2 = nn.Sequential(resnet152.layer2)
        self.layer3 = nn.Sequential(resnet152.layer3)
        self.layer4 = nn.Sequential(resnet152.layer4)
        self.layer5 = nn.Sequential(*[
            nn.Conv2d(2048, 512, kernel_size=1),
            nn.BatchNorm2d(512),
            nn.ReLU(inplace=True),
            nn.Conv2d(512, 512, kernel_size=3, padding=1, stride=2),
            nn.BatchNorm2d(512),
            nn.ReLU(inplace=True)
        ])
        self.layer6 = nn.Sequential(*[
            nn.Conv2d(
                512,
                128,
                kernel_size=1,
            ),
            nn.BatchNorm2d(128),
            nn.ReLU(inplace=True),
            nn.Conv2d(128, 256, kernel_size=3, padding=1, stride=2),
            nn.BatchNorm2d(256),
            nn.ReLU(inplace=True)
        ])

        ######
        # dsfd specific layers
        ######
        output_channels = [256, 512, 1024, 2048, 512, 256]
        # fpn
        fpn_in = output_channels

        self.latlayer3 = nn.Conv2d(fpn_in[3],
                                   fpn_in[2],
                                   kernel_size=1,
                                   stride=1,
                                   padding=0)
        self.latlayer2 = nn.Conv2d(fpn_in[2],
                                   fpn_in[1],
                                   kernel_size=1,
                                   stride=1,
                                   padding=0)
        self.latlayer1 = nn.Conv2d(fpn_in[1],
                                   fpn_in[0],
                                   kernel_size=1,
                                   stride=1,
                                   padding=0)

        self.smooth3 = nn.Conv2d(fpn_in[2],
                                 fpn_in[2],
                                 kernel_size=1,
                                 stride=1,
                                 padding=0)
        self.smooth2 = nn.Conv2d(fpn_in[1],
                                 fpn_in[1],
                                 kernel_size=1,
                                 stride=1,
                                 padding=0)
        self.smooth1 = nn.Conv2d(fpn_in[0],
                                 fpn_in[0],
                                 kernel_size=1,
                                 stride=1,
                                 padding=0)

        self.upsample = nn.Upsample(scale_factor=2,
                                    mode='bilinear',
                                    align_corners=False)

        self.eltmul = nn.EltMul()

        # fem
        cpm_in = output_channels
        self.cpm3_3 = FEM(cpm_in[0])
        self.cpm4_3 = FEM(cpm_in[1])
        self.cpm5_3 = FEM(cpm_in[2])
        self.cpm7 = FEM(cpm_in[3])
        self.cpm6_2 = FEM(cpm_in[4])
        self.cpm7_2 = FEM(cpm_in[5])

        # pa
        cfg_mbox = [1, 1, 1, 1, 1, 1]
        head = pa_multibox(output_channels, cfg_mbox, self.num_classes)

        # detection head
        self.loc = nn.ModuleList(head[0])
        self.conf = nn.ModuleList(head[1])
        self.softmax = nn.Softmax(dim=-1)
Esempio n. 16
0
from _utils import test_on

import sys
sys.path.append('.')
from flops_counter import nn
from flops_counter.tensorsize import TensorSize

######
# test on BatchNorm2d
######
bn2d = {
    'layers': [
        nn.BatchNorm2d(64)  # same shape
    ],
    'ins': [TensorSize([1, 64, 112, 112])],
    'out_shape': [TensorSize([1, 64, 112, 112])],
    'out_flops': [4816896]
}

test_on(bn2d)

######
# test on L2Norm2d
######
l2norm2d = {
    'layers': [
        nn.L2Norm2d(256)  # same shape
    ],
    'ins': [TensorSize([1, 256, 56, 56])],
    'out_shape': [TensorSize([1, 256, 56, 56])],
    'out_flops': [2408448]
Esempio n. 17
0
def conv_bn(inp, oup, stride=1, leaky=0):
    return nn.Sequential(nn.Conv2d(inp, oup, 3, stride, 1, bias=False),
                         nn.BatchNorm2d(oup),
                         nn.LeakyReLU(negative_slope=leaky, inplace=True))
Esempio n. 18
0
def _conv_bn(in_channels, out_channels, stride):
    return nn.Sequential(
        nn.Conv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=3, stride=stride, padding=1, bias=False),
        nn.BatchNorm2d(num_features=out_channels),
        nn.ReLU(inplace=True)
    )
Esempio n. 19
0
 def __init__(self):
     super(CSP, self).__init__()
     #####
     # Backbone
     #####
     # build resnet50
     # base
     self.base_conv = nn.Conv2d(3, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3))
     self.base_bn = nn.BatchNorm2d(64)
     self.base_relu = nn.ReLU(inplace=True)
     self.base_maxpooling = nn.MaxPool2d(kernel_size=(3, 3), stride=2, padding=1)
     # layer1
     l1ic = [64, 256, 256]
     l1oc = [256, 256, 256]
     self.layer1_bottleneck0 = ConvBlock(    in_channels=l1ic[0], out_channels=l1oc[0], kernel_size=3, padding=1)
     self.layer1_bottleneck1 = IdentityBlock(in_channels=l1ic[1], out_channels=l1oc[1], kernel_size=3, padding=1)
     self.layer1_bottleneck2 = IdentityBlock(in_channels=l1ic[2], out_channels=l1oc[2], kernel_size=3, padding=1)
     # layer2
     l2ic = [256, 512, 512, 512]
     l2oc = [512, 512, 512, 512]
     self.layer2_bottleneck0 = ConvBlock(    in_channels=l2ic[0], out_channels=l2oc[0], kernel_size=3, stride=2, padding=1)
     self.layer2_bottleneck1 = IdentityBlock(in_channels=l2ic[1], out_channels=l2oc[1], kernel_size=3, padding=1)
     self.layer2_bottleneck2 = IdentityBlock(in_channels=l2ic[2], out_channels=l2oc[2], kernel_size=3, padding=1)
     self.layer2_bottleneck3 = IdentityBlock(in_channels=l2ic[3], out_channels=l2oc[3], kernel_size=3, padding=1)
     # layer3
     l3ic = [512, 1024, 1024, 1024, 1024, 1024]
     l3oc = [1024, 1024, 1024, 1024, 1024, 1024]
     self.layer3_bottleneck0 = ConvBlock(    in_channels=l3ic[0], out_channels=l3oc[0], kernel_size=3, stride=2, padding=1)
     self.layer3_bottleneck1 = IdentityBlock(in_channels=l3ic[1], out_channels=l3oc[1], kernel_size=3, padding=1)
     self.layer3_bottleneck2 = IdentityBlock(in_channels=l3ic[2], out_channels=l3oc[2], kernel_size=3, padding=1)
     self.layer3_bottleneck3 = IdentityBlock(in_channels=l3ic[3], out_channels=l3oc[3], kernel_size=3, padding=1)
     self.layer3_bottleneck4 = IdentityBlock(in_channels=l3ic[4], out_channels=l3oc[4], kernel_size=3, padding=1)
     self.layer3_bottleneck5 = IdentityBlock(in_channels=l3ic[5], out_channels=l3oc[5], kernel_size=3, padding=1)
     # layer4
     l4ic = [1024, 2048, 2048]
     l4oc = [2048, 2048, 2048]
     self.layer4_bottleneck0 = ConvBlock(    in_channels=l4ic[0], out_channels=l4oc[0], kernel_size=3, padding=2, dilation=2)
     self.layer4_bottleneck1 = IdentityBlock(in_channels=l4ic[1], out_channels=l4oc[1], kernel_size=3, padding=2, dilation=2)
     self.layer4_bottleneck2 = IdentityBlock(in_channels=l4ic[2], out_channels=l4oc[2], kernel_size=3, padding=2, dilation=2)
     #####
     # CSP specific layers
     #####
     # p3up
     self.p3up_trconv = nn.ConvTranspose2d(512, 256, kernel_size=4, stride=2, padding=1)
     self.p3up_l2norm = nn.L2Norm2d(256, 10)
     # p4up
     self.p4up_trconv = nn.ConvTranspose2d(1024, 256, kernel_size=4, stride=4, padding=0)
     self.p4up_l2norm = nn.L2Norm2d(256, 10)
     # p5up
     self.p5up_trconv = nn.ConvTranspose2d(2048, 256, kernel_size=4, stride=4, padding=0)
     self.p5up_l2norm = nn.L2Norm2d(256, 10)
     # detection head - feat
     self.feat_conv = nn.Conv2d(768, 256, kernel_size=3, padding=1)
     self.feat_bn = nn.BatchNorm2d(256)
     self.feat_relu = nn.ReLU(inplace=True)
     # detection head - class
     self.class_conv = nn.Conv2d(256, 1, kernel_size=1, bias=True)
     self.class_sigmoid = nn.Sigmoid()
     # detection head - regr
     self.regr_conv = nn.Conv2d(256, 2, kernel_size=1)
     self.regr_relu = nn.ReLU(inplace=True)
     # detection head - offset
     self.offset_conv = nn.Conv2d(256, 2, kernel_size=1)
     self.offset_relu = nn.ReLU(inplace=True)
Esempio n. 20
0
 def __init__(self, in_channels, out_channels, **kwargs):
     super(CRelu, self).__init__()
     self.conv = nn.Conv2d(in_channels, out_channels, **kwargs)
     self.bn = nn.BatchNorm2d(out_channels, eps=1e-5)
     self.relu = nn.ReLU(inplace=True)
Esempio n. 21
0
def conv_bn(inp, oup, stride, k_size=3):
    return nn.Sequential(nn.Conv2d(inp, oup, k_size, stride, 1, bias=False),
                         nn.BatchNorm2d(oup), nn.PReLU())
Esempio n. 22
0
    def __init__(self):
        super(SRN, self).__init__()

        block = Bottleneck
        layers = [3, 4, 6, 3]
        self.inplanes = 64
        self.conv1 = nn.Conv2d(3,
                               64,
                               kernel_size=7,
                               stride=2,
                               padding=3,
                               bias=False)
        self.bn1 = nn.BatchNorm2d(64)
        self.relu = nn.ReLU(inplace=True)
        self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
        self.layer1 = self._make_layer(block, 64, layers[0])
        self.layer2 = self._make_layer(block, 128, layers[1], stride=2)
        self.layer3 = self._make_layer(block, 256, layers[2], stride=2)
        self.layer4 = self._make_layer(block, 512, layers[3], stride=2)
        self.layer5 = nn.Conv2d(2048, 1024, kernel_size=3, stride=2, padding=1)
        self.layer6 = nn.Conv2d(1024, 256, kernel_size=3, stride=2, padding=1)

        self.c5_lateral = nn.Conv2d(2048,
                                    256,
                                    kernel_size=1,
                                    stride=1,
                                    padding=0)
        self.c4_lateral = nn.Conv2d(1024,
                                    256,
                                    kernel_size=1,
                                    stride=1,
                                    padding=0)
        self.c3_lateral = nn.Conv2d(512,
                                    256,
                                    kernel_size=1,
                                    stride=1,
                                    padding=0)
        self.c2_lateral = nn.Conv2d(256,
                                    256,
                                    kernel_size=1,
                                    stride=1,
                                    padding=0)

        self.eltadd = nn.EltAdd()
        self.upsample = nn.Upsample(scale_factor=2,
                                    mode='bilinear',
                                    align_corners=False)

        self.p7_conv = nn.Conv2d(256, 256, kernel_size=3, stride=2, padding=1)
        self.p6_conv = nn.Conv2d(256, 256, kernel_size=3, stride=2, padding=1)
        self.p5_conv = nn.Conv2d(256, 256, kernel_size=3, stride=1, padding=1)
        self.p4_conv = nn.Conv2d(256, 256, kernel_size=3, stride=1, padding=1)
        self.p3_conv = nn.Conv2d(256, 256, kernel_size=3, stride=1, padding=1)
        self.p2_conv = nn.Conv2d(256, 256, kernel_size=3, stride=1, padding=1)

        self.c7_conv = nn.Conv2d(256, 256, kernel_size=1, stride=1, padding=0)
        self.c6_conv = nn.Conv2d(1024, 256, kernel_size=1, stride=1, padding=0)
        self.c5_conv = nn.Conv2d(2048, 256, kernel_size=1, stride=1, padding=0)
        self.c4_conv = nn.Conv2d(1024, 256, kernel_size=1, stride=1, padding=0)
        self.c3_conv = nn.Conv2d(512, 256, kernel_size=1, stride=1, padding=0)
        self.c2_conv = nn.Conv2d(256, 256, kernel_size=1, stride=1, padding=0)

        # subnet_first stage
        num_anchors = 2 * 1
        self.cls_subnet = nn.Sequential(
            nn.Conv2d(256, 256, kernel_size=3, stride=1, padding=1),
            nn.ReLU(inplace=True),
            RFE(256, 256),
            nn.Conv2d(256, 256, kernel_size=3, stride=1, padding=1),
            nn.ReLU(inplace=True),
        )
        self.box_subnet = nn.Sequential(
            nn.Conv2d(256, 256, kernel_size=3, stride=1, padding=1),
            nn.ReLU(inplace=True),
            RFE(256, 256),
            nn.Conv2d(256, 256, kernel_size=3, stride=1, padding=1),
            nn.ReLU(inplace=True),
        )
        self.cls_subnet_pred = nn.Conv2d(256,
                                         num_anchors * 1,
                                         kernel_size=1,
                                         stride=1,
                                         padding=0)
        self.box_subnet_pred = nn.Conv2d(256,
                                         num_anchors * 4,
                                         kernel_size=1,
                                         stride=1,
                                         padding=0)

        self.sigmoid = nn.Sigmoid()
Esempio n. 23
0
def conv_1x1_bn(inp, oup):
    return nn.Sequential(nn.Conv2d(inp, oup, 1, 1, 0, bias=False),
                         nn.BatchNorm2d(oup), nn.PReLU())