def __init__(self, inc=128, outc=128): super(gated_conv1x1, self).__init__() self.inp = int(inc / 2) self.oup = int(outc / 2) self.conv1x1_1 = nn.Conv2d(self.inp, self.oup, 1, 1, 0, bias=False) self.gate_1 = nn.Conv2d(self.inp, self.oup, 1, 1, 0, bias=True) self.sigmoid1 = nn.Sigmoid() self.eltmul1 = nn.EltMul() self.conv1x1_2 = nn.Conv2d(self.inp, self.oup, 1, 1, 0, bias=False) self.gate_2 = nn.Conv2d(self.inp, self.oup, 1, 1, 0, bias=True) self.sigmoid2 = nn.Sigmoid() self.eltmul2 = nn.EltMul()
def __init__(self): super(light_DSFD, self).__init__() self.conv1 = CRelu(3, 32, kernel_size=7, stride=4, padding=3) self.conv3 = CRelu(64, 64, kernel_size=5, stride=2, padding=2) self.inception1 = Inception2d(64) self.inception2 = Inception2d(64) self.inception3 = Inception2d(128) self.inception4 = Inception2d(128) self.maxpool1 = nn.MaxPool2d(kernel_size=3, stride=2, padding=1) self.maxpool2 = nn.MaxPool2d(kernel_size=3, stride=2, padding=1) self.conv5_1 = BasicConv2d(128, 128, kernel_size=1, stride=1, padding=0) self.conv5_2 = BasicConv2d(128, 256, kernel_size=3, stride=2, padding=1) self.conv6_1 = BasicConv2d(256, 128, kernel_size=1, stride=1, padding=0) self.conv6_2 = BasicConv2d(128, 256, kernel_size=3, stride=2, padding=1) fpn_in = [64, 64, 128, 128, 256, 256] cpm_in = [64, 64, 64, 64, 64, 64] fpn_channel = 64 cpm_channels = 64 output_channels = cpm_in # fpn self.smooth3 = nn.Conv2d( fpn_channel, fpn_channel, kernel_size=1, stride=1, padding=0) self.smooth2 = nn.Conv2d( fpn_channel, fpn_channel, kernel_size=1, stride=1, padding=0) self.smooth1 = nn.Conv2d( fpn_channel, fpn_channel, kernel_size=1, stride=1, padding=0) self.upsample = nn.Upsample(scale_factor=2, mode='bilinear', align_corners=False) self.eltmul = nn.EltMul() self.latlayer6 = nn.Conv2d( fpn_in[5], fpn_channel, kernel_size=1, stride=1, padding=0) self.latlayer5 = nn.Conv2d( fpn_in[4], fpn_channel, kernel_size=1, stride=1, padding=0) self.latlayer4 = nn.Conv2d( fpn_in[3], fpn_channel, kernel_size=1, stride=1, padding=0) self.latlayer3 = nn.Conv2d( fpn_in[2], fpn_channel, kernel_size=1, stride=1, padding=0) self.latlayer2 = nn.Conv2d( fpn_in[1], fpn_channel, kernel_size=1, stride=1, padding=0) self.latlayer1 = nn.Conv2d( fpn_in[0], fpn_channel, kernel_size=1, stride=1, padding=0) # cpm self.cpm1 = Inception2d(cpm_in[0]) self.cpm2 = Inception2d(cpm_in[1]) self.cpm3 = Inception2d(cpm_in[2]) self.cpm4 = Inception2d(cpm_in[3]) self.cpm5 = Inception2d(cpm_in[4]) self.cpm6 = Inception2d(cpm_in[5]) face_head = face_multibox(output_channels, [1, 1, 1, 1, 1, 1], 2 , cpm_channels) self.loc = nn.ModuleList(face_head[0]) self.conf = nn.ModuleList(face_head[1]) self.softmax = nn.Softmax(dim=-1)
def __init__(self, up_from_channels, up_to_channels): super(LFPN, self).__init__() self.conv1 = nn.Conv2d(up_from_channels, up_to_channels, kernel_size=1) self.conv1_relu = nn.ReLU(inplace=True) self.upsampling = nn.ConvTranspose2d(up_to_channels, up_to_channels, kernel_size=4, stride=2, padding=1, groups=up_to_channels, bias=False) self.conv2 = nn.Conv2d(up_to_channels, up_to_channels, kernel_size=1) self.conv2_relu = nn.ReLU(inplace=True) self.eltmul = nn.EltMul()
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)
TensorSize([64, 112, 112]) ], 'out_shape': [ TensorSize([64, 112, 112]) ], 'out_flops': [ 802816 ] } test_on(eltadd) ###### # test on EltMul ###### eltmul = { 'layers': [ nn.EltMul() # same shape ], 'ins': [ TensorSize([1, 64, 112, 112]) ], 'out_shape': [ TensorSize([1, 64, 112, 112]) ], 'out_flops': [ 802816 ] } test_on(eltmul)