def __init__(self,in_channels, out_channels, kernel_size=3, stride=1, padding = 0,
              dilation=1, groups=1, bias=True):
     super(ComplexConv2d, self).__init__()
     self.conv_r = Conv2d(in_channels, out_channels, kernel_size, stride, padding, dilation, groups, bias)
     self.conv_i = Conv2d(in_channels, out_channels, kernel_size, stride, padding, dilation, groups, bias)
Beispiel #2
0
def create_Mb_Tiny_RFB_fd(num_classes,
                          is_test=False,
                          device="cuda",
                          without_postprocessing=False):
    base_net = Mb_Tiny_RFB(2)
    base_net_model = base_net.model  # disable dropout layer

    source_layer_indexes = [8, 11, 13]
    extras = ModuleList([
        Sequential(
            Conv2d(in_channels=base_net.base_channel * 16,
                   out_channels=base_net.base_channel * 4,
                   kernel_size=1), ReLU(),
            SeperableConv2d(in_channels=base_net.base_channel * 4,
                            out_channels=base_net.base_channel * 16,
                            kernel_size=3,
                            stride=2,
                            padding=1), ReLU())
    ])

    regression_headers = ModuleList([
        SeperableConv2d(in_channels=base_net.base_channel * 4,
                        out_channels=3 * 4,
                        kernel_size=3,
                        padding=1),
        SeperableConv2d(in_channels=base_net.base_channel * 8,
                        out_channels=2 * 4,
                        kernel_size=3,
                        padding=1),
        SeperableConv2d(in_channels=base_net.base_channel * 16,
                        out_channels=2 * 4,
                        kernel_size=3,
                        padding=1),
        Conv2d(in_channels=base_net.base_channel * 16,
               out_channels=3 * 4,
               kernel_size=3,
               padding=1)
    ])

    classification_headers = ModuleList([
        SeperableConv2d(in_channels=base_net.base_channel * 4,
                        out_channels=3 * num_classes,
                        kernel_size=3,
                        padding=1),
        SeperableConv2d(in_channels=base_net.base_channel * 8,
                        out_channels=2 * num_classes,
                        kernel_size=3,
                        padding=1),
        SeperableConv2d(in_channels=base_net.base_channel * 16,
                        out_channels=2 * num_classes,
                        kernel_size=3,
                        padding=1),
        Conv2d(in_channels=base_net.base_channel * 16,
               out_channels=3 * num_classes,
               kernel_size=3,
               padding=1)
    ])

    return SSD(num_classes,
               base_net_model,
               source_layer_indexes,
               extras,
               classification_headers,
               regression_headers,
               is_test=is_test,
               config=config,
               device=device,
               without_postprocessing=without_postprocessing)
Beispiel #3
0
 def __init__(self,
              in_channels,
              channels,
              kernel_size,
              stride=(1, 1),
              padding=(0, 0),
              dilation=(1, 1),
              groups=1,
              bias=True,
              radix=2,
              reduction_factor=4,
              rectify=False,
              rectify_avg=False,
              norm_layer=None,
              dropblock_prob=0.0,
              **kwargs):
     super(SplAtConv2d, self).__init__()
     padding = _pair(padding)
     self.rectify = rectify and (padding[0] > 0 or padding[1] > 0)
     self.rectify_avg = rectify_avg
     inter_channels = max(in_channels * radix // reduction_factor, 32)
     self.radix = radix
     self.cardinality = groups
     self.channels = channels
     self.dropblock_prob = dropblock_prob
     if self.rectify:
         from rfconv import RFConv2d
         self.conv = RFConv2d(in_channels,
                              channels * radix,
                              kernel_size,
                              stride,
                              padding,
                              dilation,
                              groups=groups * radix,
                              bias=bias,
                              average_mode=rectify_avg,
                              **kwargs)
     else:
         self.conv = Conv2d(in_channels,
                            channels * radix,
                            kernel_size,
                            stride,
                            padding,
                            dilation,
                            groups=groups * radix,
                            bias=bias,
                            **kwargs)
     self.use_bn = norm_layer is not None
     if self.use_bn:
         self.bn0 = norm_layer(channels * radix)
     self.relu = ReLU(inplace=True)
     self.fc1 = Conv2d(channels, inter_channels, 1, groups=self.cardinality)
     if self.use_bn:
         self.bn1 = norm_layer(inter_channels)
     self.fc2 = Conv2d(inter_channels,
                       channels * radix,
                       1,
                       groups=self.cardinality)
     if dropblock_prob > 0.0:
         self.dropblock = DropBlock2D(dropblock_prob, 3)
     self.rsoftmax = rSoftMax(radix, groups)
 def __init__(self, in_c, out_c, kernel=(1, 1), stride=(1, 1), padding=(0, 0), groups=1):
     super(Linear_block, self).__init__()
     self.conv = Conv2d(in_c, out_channels=out_c, kernel_size=kernel, groups=groups, stride=stride, padding=padding, bias=False)
     self.bn = BatchNorm2d(out_c)
    def __init__(self, attention=False, multi_scale=True):
        super(OctMLT, self).__init__()

        self.inplanes = 64
        alpha = 0.5

        self.layer0 = nn.Sequential(
            OctConv2d(3,
                      16,
                      3,
                      stride=1,
                      padding=1,
                      bias=False,
                      alpha=(0, 0.5)), CReLU_IN(16),
            OctConv2d(32, 32, 3, stride=2, padding=1, bias=False),
            CReLU_IN(32))

        self.layer0_1 = nn.Sequential(
            OctConv2d(64, 64, 3, stride=1, padding=1, bias=False),
            #nn.InstanceNorm2d(64, affine=True),
            _ReLU(),
            OctConv2d(64, 64, 3, stride=2, padding=1, bias=False),
            #nn.InstanceNorm2d(64, affine=True),
            _ReLU(inplace=True))

        self.conv5 = OctConv2d(64, 128, 3, padding=1, bias=False, gated=True)
        self.conv6 = OctConv2d(128, 128, 3, padding=1, bias=False, gated=True)
        self.conv7 = OctConv2d(128, 256, 3, padding=1, bias=False, gated=True)
        self.conv8 = OctConv2d(256, 256, 3, padding=1, bias=False, gated=True)
        self.conv9_1 = OctConv2d(256,
                                 256,
                                 3,
                                 padding=1,
                                 bias=False,
                                 gated=True)
        self.conv9_2 = OctConv2d(256,
                                 256,
                                 3,
                                 padding=1,
                                 bias=False,
                                 alpha=(0.5, 0),
                                 gated=True)
        self.conv10_s = GatedConv(256, 256, (2, 3), padding=(0, 1), bias=False)
        self.conv11 = Conv2d(256, 74, 1, padding=(0, 0))

        self.batch5 = _InstanceNorm2d(128,
                                      eps=1e-05,
                                      momentum=0.1,
                                      affine=True)
        self.batch6 = _InstanceNorm2d(128,
                                      eps=1e-05,
                                      momentum=0.1,
                                      affine=True)
        self.batch7 = _InstanceNorm2d(256,
                                      eps=1e-05,
                                      momentum=0.1,
                                      affine=True)
        self.batch8 = _InstanceNorm2d(256,
                                      eps=1e-05,
                                      momentum=0.1,
                                      affine=True)
        self.batch9 = _InstanceNorm2d(256,
                                      eps=1e-05,
                                      momentum=0.1,
                                      affine=True)
        self.batch10_s = InstanceNorm2d(256,
                                        eps=1e-05,
                                        momentum=0.1,
                                        affine=True)
        self.max2_1 = nn.MaxPool2d((2, 1), stride=(2, 1))
        self.max2 = _MaxPool2d((2, 1), stride=(2, 1))
        self.leaky = _LeakyReLU(negative_slope=0.01, inplace=True)
        self.leaky2 = LeakyReLU(negative_slope=0.01, inplace=True)

        self.groups = 3
        self.stage_out_channels = [-1, 24, 240, 480, 960]
        self.stage_repeats = [3, 7, 3]
        self.layer1 = self._make_layer(BasicBlockIn,
                                       24,
                                       3,
                                       stride=1,
                                       alpha=alpha)
        self.layer2 = self._make_stage(2)
        self.layer3 = self._make_stage(3)
        self.layer4 = self._make_stage(4)

        self.feature4 = OctConv2d(960,
                                  256,
                                  1,
                                  stride=1,
                                  padding=0,
                                  bias=False,
                                  alpha=(0.5, 0))
        self.feature3 = OctConv2d(480,
                                  256,
                                  1,
                                  stride=1,
                                  padding=0,
                                  bias=False,
                                  alpha=(0.5, 0))
        self.feature2 = OctConv2d(240,
                                  256,
                                  1,
                                  stride=1,
                                  padding=0,
                                  bias=False,
                                  alpha=(0.5, 0))

        self.upconv2 = conv_dw_plain(256, 256, stride=1, alpha=0)
        self.upconv1 = conv_dw_plain(256, 256, stride=1, alpha=0)

        self.feature1 = OctConv2d(24,
                                  256,
                                  1,
                                  stride=1,
                                  padding=0,
                                  bias=False,
                                  alpha=(0.5, 0))

        self.act = OctConv2d(256, 1, 1, padding=0, stride=1, alpha=0)
        self.rbox = OctConv2d(256, 4, 1, padding=0, stride=1, alpha=0)

        self.angle = OctConv2d(256, 2, 1, padding=0, stride=1, alpha=0)
        self.drop0 = _Dropout2d(p=0.2, inplace=False)
        self.drop1 = Dropout2d(p=0.2, inplace=False)

        self.angle_loss = nn.MSELoss(reduction='elementwise_mean')
        self.h_loss = nn.SmoothL1Loss(reduction='elementwise_mean')
        self.w_loss = nn.SmoothL1Loss(reduction='elementwise_mean')

        self.attention = attention

        if self.attention:
            self.conv_attenton = OctConv2d(256,
                                           1,
                                           kernel_size=1,
                                           stride=1,
                                           padding=0,
                                           bias=True,
                                           alpha=0)

        self.multi_scale = multi_scale
def create_resnet18_ssd(num_classes, is_test=False):

    resnet = resnet18(pretrained=False)
    modules = list(resnet.children())[:-2]  # delete the last fc layer.

    pool5 = nn.MaxPool2d(kernel_size=2, stride=2, padding=0)
    conv6 = nn.Conv2d(512, 1024, kernel_size=1, padding=0, dilation=6)
    # # conv7 = nn.Conv2d(1024, 1024, kernel_size=1)

    # modules += [conv6, nn.ReLU()]
    modules += [pool5, conv6, nn.ReLU()]

    base_net = nn.Sequential(*modules)

    source_layer_indexes = [
        7,
        10,
    ]

    extras = ModuleList([
        Sequential(
            Conv2d(in_channels=1024, out_channels=256, kernel_size=1), ReLU(),
            Conv2d(in_channels=256,
                   out_channels=512,
                   kernel_size=3,
                   stride=2,
                   padding=1), ReLU()),
        Sequential(
            Conv2d(in_channels=512, out_channels=128, kernel_size=1), ReLU(),
            Conv2d(in_channels=128,
                   out_channels=256,
                   kernel_size=3,
                   stride=2,
                   padding=1), ReLU()),
        Sequential(Conv2d(in_channels=256, out_channels=128, kernel_size=1),
                   ReLU(),
                   Conv2d(in_channels=128, out_channels=256, kernel_size=3),
                   ReLU()),
        Sequential(Conv2d(in_channels=256, out_channels=128, kernel_size=1),
                   ReLU(),
                   Conv2d(in_channels=128, out_channels=256, kernel_size=3),
                   ReLU())
    ])

    regression_headers = ModuleList([
        Conv2d(in_channels=512, out_channels=4 * 4, kernel_size=3, padding=1),
        Conv2d(in_channels=1024, out_channels=6 * 4, kernel_size=3, padding=1),
        Conv2d(in_channels=512, out_channels=6 * 4, kernel_size=3, padding=1),
        Conv2d(in_channels=256, out_channels=6 * 4, kernel_size=3, padding=1),
        Conv2d(in_channels=256, out_channels=4 * 4, kernel_size=3, padding=1),
        Conv2d(in_channels=256, out_channels=4 * 4, kernel_size=3,
               padding=1),  # TODO: change to kernel_size=1, padding=0?
    ])

    classification_headers = ModuleList([
        Conv2d(in_channels=512,
               out_channels=4 * num_classes,
               kernel_size=3,
               padding=1),
        Conv2d(in_channels=1024,
               out_channels=6 * num_classes,
               kernel_size=3,
               padding=1),
        Conv2d(in_channels=512,
               out_channels=6 * num_classes,
               kernel_size=3,
               padding=1),
        Conv2d(in_channels=256,
               out_channels=6 * num_classes,
               kernel_size=3,
               padding=1),
        Conv2d(in_channels=256,
               out_channels=4 * num_classes,
               kernel_size=3,
               padding=1),
        Conv2d(in_channels=256,
               out_channels=4 * num_classes,
               kernel_size=3,
               padding=1),  # TODO: change to kernel_size=1, padding=0?
    ])

    return SSD(num_classes,
               base_net,
               source_layer_indexes,
               extras,
               classification_headers,
               regression_headers,
               is_test=is_test,
               config=config)
 def __init__(self, in_f, f, k=3):
     super(ConvNormRelu, self).__init__()
     
     self.conv = Conv2d(in_f, f, k,padding=1, bias=False)
     self.bnorm = BatchNorm2d(f, eps=1e-03)
Beispiel #8
0
 def __init__(self, in_channels, out_channels, **kwargs):
     super(BasicConv2d, self).__init__()
     self.conv = Conv2d(in_channels, out_channels, bias=False, **kwargs)
     self.bn = BatchNorm2d(out_channels, eps=0.001)
Beispiel #9
0
    def __init__(self, attention=False, multi_scale=True):
        super(ModelMLTRCTW, self).__init__()

        self.inplanes = 64

        self.layer0 = nn.Sequential(
            Conv2d(3, 16, 3, stride=1, padding=1, bias=False), CReLU_IN(16),
            Conv2d(32, 32, 3, stride=2, padding=1, bias=False), CReLU_IN(32))

        self.layer0_1 = nn.Sequential(
            Conv2d(64, 64, 3, stride=1, padding=1, bias=False),
            #nn.InstanceNorm2d(64, affine=True),
            nn.ReLU(),
            Conv2d(64, 64, 3, stride=2, padding=1, bias=False),
            #nn.InstanceNorm2d(64, affine=True),
            nn.ReLU(inplace=True))

        self.conv5 = Conv2d(64, 128, (3, 3), padding=(1, 1), bias=False)
        self.conv6 = Conv2d(128, 128, (3, 3), padding=1, bias=False)
        self.conv7 = Conv2d(128, 256, 3, padding=1, bias=False)
        self.conv8 = Conv2d(256, 256, (3, 3), padding=1, bias=False)
        self.conv9 = Conv2d(256, 256, (3, 3), padding=(1, 1), bias=False)
        self.conv10_s = Conv2d(256, 256, (2, 3), padding=(0, 1), bias=False)
        self.conv11 = Conv2d(256, 7500, (1, 1), padding=(0, 0))

        self.batch5 = InstanceNorm2d(128, eps=1e-05, momentum=0.1, affine=True)
        self.batch6 = InstanceNorm2d(128, eps=1e-05, momentum=0.1, affine=True)
        self.batch7 = InstanceNorm2d(256, eps=1e-05, momentum=0.1, affine=True)
        self.batch8 = InstanceNorm2d(256, eps=1e-05, momentum=0.1, affine=True)
        self.batch9 = InstanceNorm2d(256, eps=1e-05, momentum=0.1, affine=True)
        self.batch10_s = InstanceNorm2d(256,
                                        eps=1e-05,
                                        momentum=0.1,
                                        affine=True)
        self.max2 = nn.MaxPool2d((2, 1), stride=(2, 1))
        self.leaky = LeakyReLU(negative_slope=0.01, inplace=True)

        self.layer1 = self._make_layer(BasicBlockIn, 64, 3, stride=1)
        self.inplanes = 64
        self.layer2 = self._make_layer(BasicBlockIn, 128, 4, stride=2)
        self.layer3 = self._make_layer(BasicBlockSepIn, 256, 6, stride=2)
        self.layer4 = self._make_layer(BasicBlockSepIn, 512, 4, stride=2)

        self.feature4 = nn.Conv2d(512, 256, 1, stride=1, padding=0, bias=False)
        self.feature3 = nn.Conv2d(256, 256, 1, stride=1, padding=0, bias=False)
        self.feature2 = nn.Conv2d(128, 256, 1, stride=1, padding=0, bias=False)

        self.upconv2 = conv_dw_plain(256, 256, stride=1)
        self.upconv1 = conv_dw_plain(256, 256, stride=1)

        self.feature1 = nn.Conv2d(64, 256, 1, stride=1, padding=0, bias=False)

        self.act = Conv2d(256, 1, (1, 1), padding=0, stride=1)
        self.rbox = Conv2d(256, 4, (1, 1), padding=0, stride=1)

        self.angle = Conv2d(256, 2, (1, 1), padding=0, stride=1)
        self.drop1 = Dropout2d(p=0.2, inplace=False)

        self.angle_loss = nn.MSELoss(reduction='elementwise_mean')
        self.h_loss = nn.SmoothL1Loss(reduction='elementwise_mean')
        self.w_loss = nn.SmoothL1Loss(reduction='elementwise_mean')

        self.attention = attention

        if self.attention:
            self.conv_attenton = nn.Conv2d(256,
                                           1,
                                           kernel_size=1,
                                           stride=1,
                                           padding=0,
                                           bias=True)

        self.multi_scale = multi_scale
Beispiel #10
0
 def __init__(self, inChannels, outChannels):
     super().__init__()
     # store the convolution and RELU layers
     self.conv1 = Conv2d(inChannels, outChannels, 3)
     self.relu = ReLU()
     self.conv2 = Conv2d(outChannels, outChannels, 3)
Beispiel #11
0
def create_mobilenetv1_ssd(num_classes, is_test=False, device='cpu'):
    base_net = MobileNetV1(1001).model  # disable dropout layer

    source_layer_indexes = [
        12,
        14,
    ]
    extras = ModuleList([
        Sequential(
            Conv2d(in_channels=1024, out_channels=256, kernel_size=1), ReLU(),
            Conv2d(in_channels=256,
                   out_channels=512,
                   kernel_size=3,
                   stride=2,
                   padding=1), ReLU()),
        Sequential(
            Conv2d(in_channels=512, out_channels=128, kernel_size=1), ReLU(),
            Conv2d(in_channels=128,
                   out_channels=256,
                   kernel_size=3,
                   stride=2,
                   padding=1), ReLU()),
        Sequential(
            Conv2d(in_channels=256, out_channels=128, kernel_size=1), ReLU(),
            Conv2d(in_channels=128,
                   out_channels=256,
                   kernel_size=3,
                   stride=2,
                   padding=1), ReLU()),
        Sequential(
            Conv2d(in_channels=256, out_channels=128, kernel_size=1), ReLU(),
            Conv2d(in_channels=128,
                   out_channels=256,
                   kernel_size=3,
                   stride=2,
                   padding=1), ReLU())
    ])

    regression_headers = ModuleList([
        Conv2d(in_channels=512, out_channels=6 * 4, kernel_size=3, padding=1),
        Conv2d(in_channels=1024, out_channels=6 * 4, kernel_size=3, padding=1),
        Conv2d(in_channels=512, out_channels=6 * 4, kernel_size=3, padding=1),
        Conv2d(in_channels=256, out_channels=6 * 4, kernel_size=3, padding=1),
        Conv2d(in_channels=256, out_channels=6 * 4, kernel_size=3, padding=1),
        Conv2d(in_channels=256, out_channels=6 * 4, kernel_size=3,
               padding=1),  # TODO: change to kernel_size=1, padding=0?
    ])

    classification_headers = ModuleList([
        Conv2d(in_channels=512,
               out_channels=6 * num_classes,
               kernel_size=3,
               padding=1),
        Conv2d(in_channels=1024,
               out_channels=6 * num_classes,
               kernel_size=3,
               padding=1),
        Conv2d(in_channels=512,
               out_channels=6 * num_classes,
               kernel_size=3,
               padding=1),
        Conv2d(in_channels=256,
               out_channels=6 * num_classes,
               kernel_size=3,
               padding=1),
        Conv2d(in_channels=256,
               out_channels=6 * num_classes,
               kernel_size=3,
               padding=1),
        Conv2d(in_channels=256,
               out_channels=6 * num_classes,
               kernel_size=3,
               padding=1),  # TODO: change to kernel_size=1, padding=0?
    ])

    return SSD(num_classes,
               base_net,
               source_layer_indexes,
               extras,
               classification_headers,
               regression_headers,
               is_test=is_test,
               config=config,
               device=device)
Beispiel #12
0
    def __init__(self,
                 num_classes: int,
                 is_test=False,
                 config=None,
                 device=None):
        """ Create default SSD model.
        """
        super(SSD, self).__init__()

        self.num_classes = num_classes
        self.base_net = MobileNetV1(self.num_classes).model
        self.source_layer_indexes = [
            12,
            14,
        ]
        self.extras = ModuleList([
            Sequential(
                Conv2d(in_channels=1024, out_channels=256, kernel_size=1),
                ReLU(),
                Conv2d(in_channels=256,
                       out_channels=512,
                       kernel_size=3,
                       stride=2,
                       padding=1), ReLU()),
            Sequential(
                Conv2d(in_channels=512, out_channels=128, kernel_size=1),
                ReLU(),
                Conv2d(in_channels=128,
                       out_channels=256,
                       kernel_size=3,
                       stride=2,
                       padding=1), ReLU()),
            Sequential(
                Conv2d(in_channels=256, out_channels=128, kernel_size=1),
                ReLU(),
                Conv2d(in_channels=128,
                       out_channels=256,
                       kernel_size=3,
                       stride=2,
                       padding=1), ReLU()),
            Sequential(
                Conv2d(in_channels=256, out_channels=128, kernel_size=1),
                ReLU(),
                Conv2d(in_channels=128,
                       out_channels=256,
                       kernel_size=3,
                       stride=2,
                       padding=1), ReLU())
        ])

        self.regression_headers = ModuleList([
            Conv2d(in_channels=512,
                   out_channels=6 * 4,
                   kernel_size=3,
                   padding=1),
            Conv2d(in_channels=1024,
                   out_channels=6 * 4,
                   kernel_size=3,
                   padding=1),
            Conv2d(in_channels=512,
                   out_channels=6 * 4,
                   kernel_size=3,
                   padding=1),
            Conv2d(in_channels=256,
                   out_channels=6 * 4,
                   kernel_size=3,
                   padding=1),
            Conv2d(in_channels=256,
                   out_channels=6 * 4,
                   kernel_size=3,
                   padding=1),
            Conv2d(in_channels=256,
                   out_channels=6 * 4,
                   kernel_size=3,
                   padding=1),
            # TODO: change to kernel_size=1, padding=0?
        ])

        self.classification_headers = ModuleList([
            Conv2d(in_channels=512,
                   out_channels=6 * num_classes,
                   kernel_size=3,
                   padding=1),
            Conv2d(in_channels=1024,
                   out_channels=6 * num_classes,
                   kernel_size=3,
                   padding=1),
            Conv2d(in_channels=512,
                   out_channels=6 * num_classes,
                   kernel_size=3,
                   padding=1),
            Conv2d(in_channels=256,
                   out_channels=6 * num_classes,
                   kernel_size=3,
                   padding=1),
            Conv2d(in_channels=256,
                   out_channels=6 * num_classes,
                   kernel_size=3,
                   padding=1),
            Conv2d(in_channels=256,
                   out_channels=6 * num_classes,
                   kernel_size=3,
                   padding=1),
            # TODO: change to kernel_size=1, padding=0?
        ])

        self.is_test = is_test
        self.config = config

        # register layers in source_layer_indexes by adding them to a module list
        self.source_layer_add_ons = nn.ModuleList(
            [t[1] for t in self.source_layer_indexes if isinstance(t, tuple)])
        if device:
            self.device = device
        else:
            self.device = torch.device(
                "cuda:0" if torch.cuda.is_available() else "cpu")
        if is_test:
            self.config = config
            self.priors = config.priors.to(self.device)
Beispiel #13
0
 def __init__(self, in_channels, out_channels, kernel_size, stride, upsample=None):
     super(UpsampleConLayer, self).__init__()
     self.upsample = upsample
     reflection_padding = kernel_size // 2
     self.reflection_pad = ReflectionPad2d(reflection_padding)
     self.conv2d = Conv2d(in_channels, out_channels, kernel_size, stride)
Beispiel #14
0
    def test_conv_bn_relu(
            self,
            batch_size,
            input_channels_per_group,
            height,
            width,
            output_channels_per_group,
            groups,
            kernel_h,
            kernel_w,
            stride_h,
            stride_w,
            pad_h,
            pad_w,
            dilation,
            padding_mode,
            use_relu,
            eps,
            momentum,
            freeze_bn
    ):
        input_channels = input_channels_per_group * groups
        output_channels = output_channels_per_group * groups
        dilation_h = dilation_w = dilation

        conv_op = Conv2d(
            input_channels,
            output_channels,
            (kernel_h, kernel_w),
            (stride_h, stride_w),
            (pad_h, pad_w),
            (dilation_h, dilation_w),
            groups,
            False,  # No bias
            padding_mode
        ).to(dtype=torch.float)
        bn_op = BatchNorm2d(output_channels, eps, momentum).to(dtype=torch.float)
        relu_op = ReLU()

        cls = ConvBnReLU2d if use_relu else ConvBn2d
        qat_op = cls(
            input_channels,
            output_channels,
            (kernel_h, kernel_w),
            (stride_h, stride_w),
            (pad_h, pad_w),
            (dilation_h, dilation_w),
            groups,
            padding_mode,
            eps,
            momentum,
            freeze_bn,
            default_qat_qconfig.activation,
            default_qat_qconfig.weight
        ).to(dtype=torch.float).disable_fake_quant()

        # align inputs and internal parameters
        input = torch.randn(batch_size, input_channels, height, width, dtype=torch.float)
        input.requires_grad_()
        conv_op.weight = Parameter(qat_op.weight)
        bn_op.running_mean = qat_op.running_mean
        bn_op.running_var = qat_op.running_var
        bn_op.weight = qat_op.gamma
        bn_op.bias = qat_op.beta

        def compose(functions):
            # functions are reversed for natural reading order
            return reduce(lambda f, g: lambda x: f(g(x)), functions[::-1], lambda x: x)

        if not use_relu:
            def relu_op(x):
                return x

        if freeze_bn:
            def ref_op(x):
                x = conv_op(x)
                x = (x - bn_op.running_mean.reshape([1, -1, 1, 1])) * \
                    (bn_op.weight / torch.sqrt(bn_op.running_var + bn_op.eps)) \
                    .reshape([1, -1, 1, 1]) + bn_op.bias.reshape([1, -1, 1, 1])
                x = relu_op(x)
                return x
        else:
            ref_op = compose([conv_op, bn_op, relu_op])

        result_ref = ref_op(input)
        result_actual = qat_op(input)
        self.assertEqual(result_ref, result_actual)

        # backward
        dout = torch.randn(result_ref.size(), dtype=torch.float)
        result_actual.backward(dout, retain_graph=True)
        grad_ref = input.grad.cpu()
        result_actual.backward(dout)
        grad_actual = input.grad.cpu()
        self.assertEqual(grad_ref, grad_actual)
        pred = net(data[0].cuda())
        pred = pred.argmax()
        counter += 1
        if pred.item() == data[1]:
            num_correct += 1

    accuracy = num_correct / (ldr.__len__())
    print("Accuracy: ", accuracy)
    set_grad_enabled(True)
    net.train()
    return accuracy


#Define the binary classifier network
net = Sequential(
    Conv2d(3, filter1, kernel_size=2, stride=1, padding=0),
    ReLU(),
    MaxPool2d(kernel_size=2, stride=2),
    Conv2d(filter1, filter2, kernel_size=4, stride=1, padding=0),
    ReLU(),
    #MaxPool2d(kernel_size=2, stride=2),
    Flatten(),
    Linear(input_dim, layer1),
    ReLU(),
    Linear(layer1, layer2),
    ReLU(),
    Linear(layer2, layer3),
    ReLU(),
    Linear(layer3, out_dim),
)
Beispiel #16
0
    def __init__(self,
                 device,
                 num_nodes,
                 dropout=0.3,
                 supports=None,
                 do_graph_conv=True,
                 addaptadj=True,
                 aptinit=None,
                 in_dim=2,
                 out_dim=12,
                 residual_channels=32,
                 dilation_channels=32,
                 cat_feat_gc=False,
                 skip_channels=256,
                 end_channels=512,
                 kernel_size=2,
                 blocks=4,
                 layers=2,
                 apt_size=10,
                 scale_dim=168,
                 downscale_input=False,
                 upscale_output=False,
                 pwn=False):
        super().__init__()
        self.dropout = dropout
        self.blocks = blocks
        self.layers = layers
        self.do_graph_conv = do_graph_conv
        self.cat_feat_gc = cat_feat_gc
        self.addaptadj = addaptadj
        self.scale_dim = scale_dim
        self.downscale_input = downscale_input
        self.upscale_output = upscale_output
        self.pwn = pwn

        if self.downscale_input:
            self.start_linear = nn.Linear(in_features=self.scale_dim,
                                          out_features=num_nodes,
                                          bias=True)

        if self.upscale_output:
            if num_nodes < self.scale_dim:
                self.end_linear = nn.Linear(in_features=num_nodes,
                                            out_features=self.scale_dim,
                                            bias=True)
            else:
                self.end_linear = nn.Linear(in_features=self.scale_dim,
                                            out_features=num_nodes,
                                            bias=True)

        if self.pwn:
            pwn_params = torch.randn(num_nodes, self.scale_dim)
            self.pwn_embedding = Parameter(pwn_params.to(device),
                                           requires_grad=True)

        if self.cat_feat_gc:
            self.start_conv = nn.Conv2d(
                in_channels=1,  # hard code to avoid errors
                out_channels=residual_channels,
                kernel_size=(1, 1))
            self.cat_feature_conv = nn.Conv2d(in_channels=in_dim - 1,
                                              out_channels=residual_channels,
                                              kernel_size=(1, 1))
        else:
            self.start_conv = nn.Conv2d(in_channels=in_dim,
                                        out_channels=residual_channels,
                                        kernel_size=(1, 1))

        self.fixed_supports = supports or []
        receptive_field = 1

        self.supports_len = len(self.fixed_supports)
        if do_graph_conv and addaptadj:
            if aptinit is None:
                nodevecs = torch.randn(num_nodes, apt_size), torch.randn(
                    apt_size, num_nodes)
            else:
                nodevecs = self.svd_init(apt_size, aptinit)
            self.supports_len += 1
            self.nodevec1, self.nodevec2 = [
                Parameter(n.to(device), requires_grad=True) for n in nodevecs
            ]

        depth = list(range(blocks * layers))

        # 1x1 convolution for residual and skip connections (slightly different see docstring)
        self.residual_convs = ModuleList([
            Conv1d(dilation_channels, residual_channels, (1, 1)) for _ in depth
        ])
        self.skip_convs = ModuleList(
            [Conv1d(dilation_channels, skip_channels, (1, 1)) for _ in depth])
        self.bn = ModuleList([BatchNorm2d(residual_channels) for _ in depth])
        self.graph_convs = ModuleList([
            GraphConvNet(dilation_channels,
                         residual_channels,
                         dropout,
                         support_len=self.supports_len) for _ in depth
        ])

        self.filter_convs = ModuleList()
        self.gate_convs = ModuleList()
        for b in range(blocks):
            additional_scope = kernel_size - 1
            D = 1  # dilation
            for i in range(layers):
                # dilated convolutions
                self.filter_convs.append(
                    Conv2d(residual_channels,
                           dilation_channels, (1, kernel_size),
                           dilation=D))
                self.gate_convs.append(
                    Conv1d(residual_channels,
                           dilation_channels, (1, kernel_size),
                           dilation=D))
                D *= 2
                receptive_field += additional_scope
                additional_scope *= 2
        self.receptive_field = receptive_field

        self.end_conv_1 = Conv2d(skip_channels,
                                 end_channels, (1, 1),
                                 bias=True)
        self.end_conv_2 = Conv2d(end_channels, out_dim, (1, 1), bias=True)
                                     device=None):
    predictor = Predictor(net,
                          config.image_size,
                          config.image_mean,
                          config.image_std,
                          nms_method=nms_method,
                          iou_threshold=config.iou_threshold,
                          candidate_size=candidate_size,
                          sigma=sigma,
                          device=device)
    return predictor


extras = ModuleList([
    Sequential(
        Conv2d(in_channels=1024, out_channels=256, kernel_size=1), ReLU(),
        Conv2d(in_channels=256,
               out_channels=512,
               kernel_size=3,
               stride=2,
               padding=1), ReLU()),
    Sequential(
        Conv2d(in_channels=512, out_channels=128, kernel_size=1), ReLU(),
        Conv2d(in_channels=128,
               out_channels=256,
               kernel_size=3,
               stride=2,
               padding=1), ReLU()),
    Sequential(Conv2d(in_channels=256, out_channels=128,
                      kernel_size=1), ReLU(),
               Conv2d(in_channels=128, out_channels=256, kernel_size=3),
Beispiel #18
0
def create_mobilenetv3_ssd_lite_large(num_classes,
                                      width_mult=1.0,
                                      use_batch_norm=True,
                                      onnx_compatible=False,
                                      is_test=False):
    base_net = MobileNetV3(model_mode="LARGE").features

    source_layer_indexes = [
        GraphPath(15, 'conv', -1),
        22,
    ]
    extras = ModuleList([
        InvertedResidual(1280, 512, stride=2, expand_ratio=0.2),
        InvertedResidual(512, 256, stride=2, expand_ratio=0.25),
        InvertedResidual(256, 256, stride=2, expand_ratio=0.5),
        InvertedResidual(256, 64, stride=2, expand_ratio=0.25)
    ])

    regression_headers = ModuleList([
        SeperableConv2d(in_channels=round(672 * width_mult),
                        out_channels=6 * 4,
                        kernel_size=3,
                        padding=1,
                        onnx_compatible=False),
        SeperableConv2d(in_channels=1280,
                        out_channels=6 * 4,
                        kernel_size=3,
                        padding=1,
                        onnx_compatible=False),
        SeperableConv2d(in_channels=512,
                        out_channels=6 * 4,
                        kernel_size=3,
                        padding=1,
                        onnx_compatible=False),
        SeperableConv2d(in_channels=256,
                        out_channels=6 * 4,
                        kernel_size=3,
                        padding=1,
                        onnx_compatible=False),
        SeperableConv2d(in_channels=256,
                        out_channels=6 * 4,
                        kernel_size=3,
                        padding=1,
                        onnx_compatible=False),
        Conv2d(in_channels=64, out_channels=6 * 4, kernel_size=1),
    ])

    classification_headers = ModuleList([
        SeperableConv2d(in_channels=round(672 * width_mult),
                        out_channels=6 * num_classes,
                        kernel_size=3,
                        padding=1),
        SeperableConv2d(in_channels=1280,
                        out_channels=6 * num_classes,
                        kernel_size=3,
                        padding=1),
        SeperableConv2d(in_channels=512,
                        out_channels=6 * num_classes,
                        kernel_size=3,
                        padding=1),
        SeperableConv2d(in_channels=256,
                        out_channels=6 * num_classes,
                        kernel_size=3,
                        padding=1),
        SeperableConv2d(in_channels=256,
                        out_channels=6 * num_classes,
                        kernel_size=3,
                        padding=1),
        Conv2d(in_channels=64, out_channels=6 * num_classes, kernel_size=1),
    ])

    return SSD(num_classes,
               base_net,
               source_layer_indexes,
               extras,
               classification_headers,
               regression_headers,
               is_test=is_test,
               config=config)
 def __init__(self, in_f, f):
     super(ConvStride2NormRelu, self).__init__()
     
     self.zero = ZeroPad2d((1, 0, 1, 0))
     self.conv = Conv2d(in_f, f, 3, stride=2, bias=False)
     self.bnorm = BatchNorm2d(f, eps=1e-03)
Beispiel #20
0
    def test_conv_bn_relu(self, batch_size, input_channels_per_group, height,
                          width, output_channels_per_group, groups, kernel_h,
                          kernel_w, stride_h, stride_w, pad_h, pad_w, dilation,
                          padding_mode, use_relu, eps, momentum, freeze_bn):
        # **** WARNING: This is used to temporarily disable MKL-DNN convolution due
        # to a bug: https://github.com/pytorch/pytorch/issues/23825
        # Once this bug is fixed, this context manager as well as its callsites
        # should be removed!
        with torch.backends.mkldnn.flags(enabled=False):
            input_channels = input_channels_per_group * groups
            output_channels = output_channels_per_group * groups
            dilation_h = dilation_w = dilation

            conv_op = Conv2d(
                input_channels,
                output_channels,
                (kernel_h, kernel_w),
                (stride_h, stride_w),
                (pad_h, pad_w),
                (dilation_h, dilation_w),
                groups,
                False,  # No bias
                padding_mode).to(dtype=torch.double)
            bn_op = BatchNorm2d(output_channels, eps,
                                momentum).to(dtype=torch.double)
            relu_op = ReLU()

            cls = ConvBnReLU2d if use_relu else ConvBn2d
            qat_op = cls(input_channels, output_channels, (kernel_h, kernel_w),
                         (stride_h, stride_w), (pad_h, pad_w),
                         (dilation_h, dilation_w), groups, padding_mode, eps,
                         momentum, freeze_bn,
                         default_qat_qconfig).to(dtype=torch.double)
            qat_op.apply(torch.quantization.disable_fake_quant)
            # align inputs and internal parameters
            input = torch.randn(batch_size,
                                input_channels,
                                height,
                                width,
                                dtype=torch.double,
                                requires_grad=True)
            conv_op.weight = torch.nn.Parameter(qat_op.weight.detach())
            bn_op.running_mean = qat_op.running_mean.clone()
            bn_op.running_var = qat_op.running_var.clone()
            bn_op.weight = torch.nn.Parameter(qat_op.gamma.detach())
            bn_op.bias = torch.nn.Parameter(qat_op.beta.detach())

            def compose(functions):
                # functions are reversed for natural reading order
                return reduce(lambda f, g: lambda x: f(g(x)), functions[::-1],
                              lambda x: x)

            if not use_relu:

                def relu_op(x):
                    return x

            if freeze_bn:

                def ref_op(x):
                    x = conv_op(x)
                    x = (x - bn_op.running_mean.reshape([1, -1, 1, 1])) * \
                        (bn_op.weight / torch.sqrt(bn_op.running_var + bn_op.eps)) \
                        .reshape([1, -1, 1, 1]) + bn_op.bias.reshape([1, -1, 1, 1])
                    x = relu_op(x)
                    return x
            else:
                ref_op = compose([conv_op, bn_op, relu_op])

            input_clone = input.clone().detach().requires_grad_()
            for i in range(2):
                result_ref = ref_op(input)
                result_actual = qat_op(input_clone)
                self.assertEqual(result_ref, result_actual)

                # backward
                dout = torch.randn(result_ref.size(), dtype=torch.double)
                loss = (result_ref - dout).sum()
                loss.backward()
                input_grad_ref = input.grad.cpu()
                weight_grad_ref = conv_op.weight.grad.cpu()
                gamma_grad_ref = bn_op.weight.grad.cpu()
                beta_grad_ref = bn_op.bias.grad.cpu()
                running_mean_ref = bn_op.running_mean
                running_var_ref = bn_op.running_var
                num_batches_tracked_ref = bn_op.num_batches_tracked
                loss = (result_actual - dout).sum()
                loss.backward()
                input_grad_actual = input_clone.grad.cpu()
                weight_grad_actual = qat_op.weight.grad.cpu()
                gamma_grad_actual = qat_op.gamma.grad.cpu()
                beta_grad_actual = qat_op.beta.grad.cpu()
                running_mean_actual = qat_op.running_mean
                running_var_actual = qat_op.running_var
                num_batches_tracked_actual = qat_op.num_batches_tracked
                precision = 1e-10
                self.assertEqual(input_grad_ref,
                                 input_grad_actual,
                                 prec=precision)
                self.assertEqual(weight_grad_ref,
                                 weight_grad_actual,
                                 prec=precision)
                self.assertEqual(gamma_grad_ref,
                                 gamma_grad_actual,
                                 prec=precision)
                self.assertEqual(beta_grad_ref,
                                 beta_grad_actual,
                                 prec=precision)
                self.assertEqual(num_batches_tracked_ref,
                                 num_batches_tracked_actual,
                                 prec=precision)
                self.assertEqual(running_mean_ref,
                                 running_mean_actual,
                                 prec=precision)
                self.assertEqual(running_var_ref,
                                 running_var_actual,
                                 prec=precision)
Beispiel #21
0
def create_vgg_ssd(num_classes, is_test=False):
    vgg_config = [
        64, 64, 'M', 128, 128, 'M', 256, 256, 256, 'C', 512, 512, 512, 'M',
        512, 512, 512
    ]
    base_net = ModuleList(vgg(vgg_config))

    source_layer_indexes = [
        (23, BatchNorm2d(512)),
        len(base_net),
    ]
    extras = ModuleList([
        Sequential(
            Conv2d(in_channels=1024, out_channels=256, kernel_size=1), ReLU(),
            Conv2d(in_channels=256,
                   out_channels=512,
                   kernel_size=3,
                   stride=2,
                   padding=1), ReLU()),
        Sequential(
            Conv2d(in_channels=512, out_channels=128, kernel_size=1), ReLU(),
            Conv2d(in_channels=128,
                   out_channels=256,
                   kernel_size=3,
                   stride=2,
                   padding=1), ReLU()),
        Sequential(Conv2d(in_channels=256, out_channels=128, kernel_size=1),
                   ReLU(),
                   Conv2d(in_channels=128, out_channels=256, kernel_size=3),
                   ReLU()),
        Sequential(Conv2d(in_channels=256, out_channels=128, kernel_size=1),
                   ReLU(),
                   Conv2d(in_channels=128, out_channels=256, kernel_size=3),
                   ReLU())
    ])

    regression_headers = ModuleList([
        Conv2d(in_channels=512, out_channels=4 * 4, kernel_size=3, padding=1),
        Conv2d(in_channels=1024, out_channels=6 * 4, kernel_size=3, padding=1),
        Conv2d(in_channels=512, out_channels=6 * 4, kernel_size=3, padding=1),
        Conv2d(in_channels=256, out_channels=6 * 4, kernel_size=3, padding=1),
        Conv2d(in_channels=256, out_channels=4 * 4, kernel_size=3, padding=1),
        Conv2d(in_channels=256, out_channels=4 * 4, kernel_size=3,
               padding=1),  # TODO: change to kernel_size=1, padding=0?
    ])

    classification_headers = ModuleList([
        Conv2d(in_channels=512,
               out_channels=4 * num_classes,
               kernel_size=3,
               padding=1),
        Conv2d(in_channels=1024,
               out_channels=6 * num_classes,
               kernel_size=3,
               padding=1),
        Conv2d(in_channels=512,
               out_channels=6 * num_classes,
               kernel_size=3,
               padding=1),
        Conv2d(in_channels=256,
               out_channels=6 * num_classes,
               kernel_size=3,
               padding=1),
        Conv2d(in_channels=256,
               out_channels=4 * num_classes,
               kernel_size=3,
               padding=1),
        Conv2d(in_channels=256,
               out_channels=4 * num_classes,
               kernel_size=3,
               padding=1),  # TODO: change to kernel_size=1, padding=0?
    ])

    return SSD(num_classes,
               base_net,
               source_layer_indexes,
               extras,
               classification_headers,
               regression_headers,
               is_test=is_test,
               config=config)
Beispiel #22
0
 def __init__(self):
     super(TorchModel, self).__init__()
     self.conv = Conv2d(3, 16, 3)
     self.bn = BatchNorm2d(16)
     self.relu = ReLU(inplace=True)
     self.fc = nn.Linear(3136, 3)
    def __init__(self,
                 n_channels=3,
                 num_levels=17,
                 padding="same",
                 momentum=0.1):
        super(FullConvNet, self).__init__()

        self._num_levels = num_levels
        self._actfun = [
            ReLU(),
        ] * (self._num_levels - 1) + [
            None,
        ]
        self._f_size = [
            3,
        ] * self._num_levels
        padding = padding.lower()
        if padding == 'valid':
            self._f_pad = [
                0,
            ] * self._num_levels
        elif padding == 'same':
            self._f_pad = [
                1,
            ] * self._num_levels
        else:
            raise ValueError(
                'Padding must be either "valid" or "same", instead "%s" is given.'
                % padding)
        self._f_num = [
            64,
        ] * (self._num_levels - 1) + [
            1,
        ]
        self._f_in = [
            n_channels,
        ] + [
            64,
        ] * (self._num_levels - 1)
        self._f_stride = [
            1,
        ] * self._num_levels
        self._bnorm = [
            False,
        ] + [
            True,
        ] * (self._num_levels - 2) + [
            False,
        ]
        self._bnorm_epsilon = 1e-5
        self._bnorm_momentum = momentum
        self.decay_list = []

        self.features = Sequential()
        for i in range(self._num_levels):

            # convolution (with bias if batch normalization is not executed in this level)
            self.features.add_module(module=Conv2d(self._f_in[i],
                                                   self._f_num[i],
                                                   self._f_size[i],
                                                   self._f_stride[i],
                                                   self._f_pad[i],
                                                   bias=not self._bnorm[i]),
                                     name='level_%d/conv' % i)
            torch.nn.init.normal_(
                self.features[-1].weight,
                std=sqrt(2.0 / self._f_size[i] * self._f_size[i] *
                         maximum(self._f_in[i], self._f_num[i])))
            self.decay_list.append(self.features[-1].weight)

            # eventual batch normalization
            if self._bnorm[i]:
                self.features.add_module(module=BatchNorm2d(
                    self._f_num[i],
                    eps=self._bnorm_epsilon,
                    momentum=self._bnorm_momentum,
                    affine=True),
                                         name='level_%d/bn' % i)

            # eventual activation
            if self._actfun[i] is not None:
                self.features.add_module(module=self._actfun[i],
                                         name='level_%d/activation' % i)
Beispiel #24
0
 def from_rgb(out_channels):
     return Conv2d(3, out_channels, (1, 1), bias=True)
Beispiel #25
0
    def __init__(self, train_df):
        super(MaskDetector, self).__init__()
        self.train_df = train_df

        self.convLayer1 = Sequential(
            Conv2d(3, 32, kernel_size=(3, 3), padding=(1, 1)),
            # BatchNorm2d(32),
            ReLU(),
            # MaxPool2d(kernel_size=(2, 2))
        )

        self.convLayer2 = Sequential(
            Conv2d(32, 64, kernel_size=(3, 3), padding=(1, 1)),
            # BatchNorm2d(64),
            ReLU(),
            # MaxPool2d(kernel_size=(2, 2))
        )

        self.convLayer3 = Sequential(
            Conv2d(64, 128, kernel_size=(3, 3), padding=(1, 1)),
            # BatchNorm2d(128),
            ReLU(),
            # MaxPool2d(kernel_size=(2, 2))
        )

        self.convLayer4 = Sequential(
            Conv2d(128, 256, kernel_size=(3, 3), padding=(1, 1), stride=(3, 3)),
            # BatchNorm2d(128),
            ReLU(),
            # MaxPool2d(kernel_size=(2, 2))
        )

        self.convLayer5 = Sequential(
            Conv2d(256, 512, kernel_size=(3, 3), padding=(1, 1), stride=(2, 2)),
            # BatchNorm2d(128),
            ReLU(),
            MaxPool2d(kernel_size=(2, 2))
        )

        self.convLayer6 = Sequential(
            Conv2d(512, 256, kernel_size=(3, 3), padding=(1, 1), stride=(2, 2)),
            # BatchNorm2d(128),
            ReLU(),
            MaxPool2d(kernel_size=(2, 2))
        )

        self.linearLayers = Sequential(
            Linear(in_features=1024, out_features=512),
            Dropout(p=0.1),
            ReLU(),
            Linear(in_features=512, out_features=256),
            Dropout(p=0.1),
            ReLU(),
            Linear(in_features=256, out_features=2),
            Dropout(p=0.1),
            Softmax(dim=1)
        )

        # Initialize layers' weights
        for sequential in [self.convLayer1, self.convLayer2, self.convLayer3,
                           self.convLayer4, self.convLayer5, self.convLayer6, self.linearLayers]:
            for layer in sequential.children():
                if isinstance(layer, (Linear, Conv2d)):
                    init.xavier_uniform_(layer.weight)
Beispiel #26
0
 def to_rgb(in_channels):
     return Conv2d(in_channels, 3, (1, 1), bias=True)
Beispiel #27
0
    def __init__(self):
        super().__init__()

        self.prep_conv = Conv2d(3,
                                64,
                                kernel_size=(3, 3),
                                stride=(1, 1),
                                padding=(1, 1),
                                bias=False)
        self.layer1_block0_bn1 = BatchNorm(64,
                                           eps=1e-05,
                                           momentum=0.1,
                                           affine=True,
                                           track_running_stats=True)
        self.layer1_block0_relu1 = ReLU(inplace=True)
        self.layer1_block0_branch_conv1 = Conv2d(64,
                                                 64,
                                                 kernel_size=(3, 3),
                                                 stride=(1, 1),
                                                 padding=(1, 1),
                                                 bias=False)
        self.layer1_block0_branch_bn2 = BatchNorm(64,
                                                  eps=1e-05,
                                                  momentum=0.1,
                                                  affine=True,
                                                  track_running_stats=True)
        self.layer1_block0_branch_relu2 = ReLU(inplace=True)
        self.layer1_block0_branch_conv2 = Conv2d(64,
                                                 64,
                                                 kernel_size=(3, 3),
                                                 stride=(1, 1),
                                                 padding=(1, 1),
                                                 bias=False)
        self.layer1_block0_add = Add()
        self.layer1_block1_bn1 = BatchNorm(64,
                                           eps=1e-05,
                                           momentum=0.1,
                                           affine=True,
                                           track_running_stats=True)
        self.layer1_block1_relu1 = ReLU(inplace=True)
        self.layer1_block1_branch_conv1 = Conv2d(64,
                                                 64,
                                                 kernel_size=(3, 3),
                                                 stride=(1, 1),
                                                 padding=(1, 1),
                                                 bias=False)
        self.layer1_block1_branch_bn2 = BatchNorm(64,
                                                  eps=1e-05,
                                                  momentum=0.1,
                                                  affine=True,
                                                  track_running_stats=True)
        self.layer1_block1_branch_relu2 = ReLU(inplace=True)
        self.layer1_block1_branch_conv2 = Conv2d(64,
                                                 64,
                                                 kernel_size=(3, 3),
                                                 stride=(1, 1),
                                                 padding=(1, 1),
                                                 bias=False)
        self.layer1_block1_add = Add()
        self.layer2_block0_bn1 = BatchNorm(64,
                                           eps=1e-05,
                                           momentum=0.1,
                                           affine=True,
                                           track_running_stats=True)
        self.layer2_block0_relu1 = ReLU(inplace=True)
        self.layer2_block0_branch_conv1 = Conv2d(64,
                                                 128,
                                                 kernel_size=(3, 3),
                                                 stride=(2, 2),
                                                 padding=(1, 1),
                                                 bias=False)
        self.layer2_block0_branch_bn2 = BatchNorm(128,
                                                  eps=1e-05,
                                                  momentum=0.1,
                                                  affine=True,
                                                  track_running_stats=True)
        self.layer2_block0_branch_relu2 = ReLU(inplace=True)
        self.layer2_block0_branch_conv2 = Conv2d(128,
                                                 128,
                                                 kernel_size=(3, 3),
                                                 stride=(1, 1),
                                                 padding=(1, 1),
                                                 bias=False)
        self.layer2_block0_conv3 = Conv2d(64,
                                          128,
                                          kernel_size=(1, 1),
                                          stride=(2, 2),
                                          bias=False)
        self.layer2_block0_add = Add()
        self.layer2_block1_bn1 = BatchNorm(128,
                                           eps=1e-05,
                                           momentum=0.1,
                                           affine=True,
                                           track_running_stats=True)
        self.layer2_block1_relu1 = ReLU(inplace=True)
        self.layer2_block1_branch_conv1 = Conv2d(128,
                                                 128,
                                                 kernel_size=(3, 3),
                                                 stride=(1, 1),
                                                 padding=(1, 1),
                                                 bias=False)
        self.layer2_block1_branch_bn2 = BatchNorm(128,
                                                  eps=1e-05,
                                                  momentum=0.1,
                                                  affine=True,
                                                  track_running_stats=True)
        self.layer2_block1_branch_relu2 = ReLU(inplace=True)
        self.layer2_block1_branch_conv2 = Conv2d(128,
                                                 128,
                                                 kernel_size=(3, 3),
                                                 stride=(1, 1),
                                                 padding=(1, 1),
                                                 bias=False)
        self.layer2_block1_add = Add()
        self.layer3_block0_bn1 = BatchNorm(128,
                                           eps=1e-05,
                                           momentum=0.1,
                                           affine=True,
                                           track_running_stats=True)
        self.layer3_block0_relu1 = ReLU(inplace=True)
        self.layer3_block0_branch_conv1 = Conv2d(128,
                                                 256,
                                                 kernel_size=(3, 3),
                                                 stride=(2, 2),
                                                 padding=(1, 1),
                                                 bias=False)
        self.layer3_block0_branch_bn2 = BatchNorm(256,
                                                  eps=1e-05,
                                                  momentum=0.1,
                                                  affine=True,
                                                  track_running_stats=True)
        self.layer3_block0_branch_relu2 = ReLU(inplace=True)
        self.layer3_block0_branch_conv2 = Conv2d(256,
                                                 256,
                                                 kernel_size=(3, 3),
                                                 stride=(1, 1),
                                                 padding=(1, 1),
                                                 bias=False)
        self.layer3_block0_conv3 = Conv2d(128,
                                          256,
                                          kernel_size=(1, 1),
                                          stride=(2, 2),
                                          bias=False)
        self.layer3_block0_add = Add()
        self.layer3_block1_bn1 = BatchNorm(256,
                                           eps=1e-05,
                                           momentum=0.1,
                                           affine=True,
                                           track_running_stats=True)
        self.layer3_block1_relu1 = ReLU(inplace=True)
        self.layer3_block1_branch_conv1 = Conv2d(256,
                                                 256,
                                                 kernel_size=(3, 3),
                                                 stride=(1, 1),
                                                 padding=(1, 1),
                                                 bias=False)
        self.layer3_block1_branch_bn2 = BatchNorm(256,
                                                  eps=1e-05,
                                                  momentum=0.1,
                                                  affine=True,
                                                  track_running_stats=True)
        self.layer3_block1_branch_relu2 = ReLU(inplace=True)
        self.layer3_block1_branch_conv2 = Conv2d(256,
                                                 256,
                                                 kernel_size=(3, 3),
                                                 stride=(1, 1),
                                                 padding=(1, 1),
                                                 bias=False)
        self.layer3_block1_add = Add()
        self.layer4_block0_bn1 = BatchNorm(256,
                                           eps=1e-05,
                                           momentum=0.1,
                                           affine=True,
                                           track_running_stats=True)
        self.layer4_block0_relu1 = ReLU(inplace=True)
        self.layer4_block0_branch_conv1 = Conv2d(256,
                                                 256,
                                                 kernel_size=(3, 3),
                                                 stride=(2, 2),
                                                 padding=(1, 1),
                                                 bias=False)
        self.layer4_block0_branch_bn2 = BatchNorm(256,
                                                  eps=1e-05,
                                                  momentum=0.1,
                                                  affine=True,
                                                  track_running_stats=True)
        self.layer4_block0_branch_relu2 = ReLU(inplace=True)
        self.layer4_block0_branch_conv2 = Conv2d(256,
                                                 256,
                                                 kernel_size=(3, 3),
                                                 stride=(1, 1),
                                                 padding=(1, 1),
                                                 bias=False)
        self.layer4_block0_conv3 = Conv2d(256,
                                          256,
                                          kernel_size=(1, 1),
                                          stride=(2, 2),
                                          bias=False)
        self.layer4_block0_add = Add()
        self.layer4_block1_bn1 = BatchNorm(256,
                                           eps=1e-05,
                                           momentum=0.1,
                                           affine=True,
                                           track_running_stats=True)
        self.layer4_block1_relu1 = ReLU(inplace=True)
        self.layer4_block1_branch_conv1 = Conv2d(256,
                                                 256,
                                                 kernel_size=(3, 3),
                                                 stride=(1, 1),
                                                 padding=(1, 1),
                                                 bias=False)
        self.layer4_block1_branch_bn2 = BatchNorm(256,
                                                  eps=1e-05,
                                                  momentum=0.1,
                                                  affine=True,
                                                  track_running_stats=True)
        self.layer4_block1_branch_relu2 = ReLU(inplace=True)
        self.layer4_block1_branch_conv2 = Conv2d(256,
                                                 256,
                                                 kernel_size=(3, 3),
                                                 stride=(1, 1),
                                                 padding=(1, 1),
                                                 bias=False)
        self.layer4_block1_add = Add()
        self.final_in = Identity()
        self.final_maxpool = MaxPool2d(kernel_size=4,
                                       stride=4,
                                       padding=0,
                                       dilation=1,
                                       ceil_mode=False)
        self.final_avgpool = AvgPool2d(kernel_size=4, stride=4, padding=0)
        self.final_concat = Concat()
        self.final_flatten = Flatten()
        self.final_linear = Linear(in_features=512, out_features=10, bias=True)
        self.logits = Identity()
Beispiel #28
0
    def __init__(self,
                 num_classes: int = 1,
                 iterations: int = 2,
                 multiplier: float = 1.0,
                 num_layers: int = 4,
                 integrate: bool = False):

        super(BiONet, self).__init__()
        #  参数
        self.iterations = iterations
        self.multiplier = multiplier
        self.num_layers = num_layers
        self.integrate = integrate
        self.batch_norm_momentum = 0.01
        #  生成通道参数,此处通道是从第一个Encoder输出的量开始的,直到语义向量
        self.filters_list = [
            int(32 * (2**i) * self.multiplier)
            for i in range(self.num_layers + 1)
        ]
        #  预处理卷积块,不参与循环,最终输出的是32*256*256
        self.pre_transform_conv_block = Sequential(
            # 这里看代码实现,应该永远和第一个Encoder输出的层数是一样的
            Conv2d(3,
                   self.filters_list[0],
                   kernel_size=(3, 3),
                   padding=(1, 1),
                   stride=(1, 1)),  # 生成f[1]*512*512
            ReLU(),
            BatchNorm2d(self.filters_list[0],
                        momentum=self.batch_norm_momentum),
            Conv2d(self.filters_list[0],
                   self.filters_list[0],
                   kernel_size=(3, 3),
                   padding=(1, 1),
                   stride=(1, 1)),
            # 生成f[1]*512*512
            ReLU(),
            BatchNorm2d(self.filters_list[0],
                        momentum=self.batch_norm_momentum),
            Conv2d(self.filters_list[0],
                   self.filters_list[0],
                   kernel_size=(3, 3),
                   padding=(1, 1),
                   stride=(1, 1)),
            # 生成f[1]*512*512
            ReLU(),
            BatchNorm2d(self.filters_list[0],
                        momentum=self.batch_norm_momentum),
            MaxPool2d(kernel_size=(2, 2), stride=(2, 2), padding=(0, 0)))
        self.reuse_convs = []  # encoder复用的卷积核,每个encoder对应一个元组(共3个卷积核,不包括ReLU)
        self.encoders = [
        ]  # 由encoder构成的列表。由于encoder的一部分不参与循环,因此每个encoder是一个元组(两个CONV的Sequential, DOWN)
        self.reuse_deconvs = [
        ]  # decoder复用的卷积、反卷积核,每个decoder对应一个元组(共3个卷积核,不包括ReLU)
        self.decoders = [
        ]  # 由decoder构成的列表。由于decoder的一部分不参与循环,因此每个decoder是一个元组(两个CONV的Sequential, UP)
        for iteration in range(self.iterations):
            for layer in range(self.num_layers):

                #  创建encoders的部分。虽然部分代码可以合写,但是为了看起来清晰(而且构造函数也不是很要求效率),所以分开考虑encoder和decoder
                #  和层次有关的常数
                in_channel = self.filters_list[
                    layer] * 2  # 由于有输出部分传入的数据,因此需要翻倍输入通道
                mid_channel = self.filters_list[layer]
                out_channel = self.filters_list[layer + 1]
                #  创建encoders模型
                if iteration == 0:
                    #  创建并添加复用卷积核
                    #  只有最后一个卷积核负责升高通道
                    conv1 = Conv2d(in_channel,
                                   mid_channel,
                                   kernel_size=(3, 3),
                                   padding=(1, 1),
                                   stride=(1, 1))
                    conv2 = Conv2d(mid_channel,
                                   mid_channel,
                                   kernel_size=(3, 3),
                                   padding=(1, 1),
                                   stride=(1, 1))
                    conv3 = Conv2d(mid_channel,
                                   out_channel,
                                   kernel_size=(3, 3),
                                   padding=(1, 1),
                                   stride=(1, 1))
                    self.reuse_convs.append((conv1, conv2, conv3))
                #  创建encoder
                #  首先构造两个CONV
                convs = Sequential(
                    self.reuse_convs[layer][0], ReLU(),
                    BatchNorm2d(mid_channel,
                                momentum=self.batch_norm_momentum),
                    self.reuse_convs[layer][1], ReLU(),
                    BatchNorm2d(mid_channel,
                                momentum=self.batch_norm_momentum))
                #  构建DOWN
                down = Sequential(
                    self.reuse_convs[layer][2], ReLU(),
                    BatchNorm2d(out_channel,
                                momentum=self.batch_norm_momentum),
                    MaxPool2d(kernel_size=(2, 2),
                              stride=(2, 2),
                              padding=(0, 0)))
                self.add_module(
                    "iteration{0}_layer{1}_encoder_convs".format(
                        iteration, layer), convs)
                self.add_module(
                    "iteration{0}_layer{1}_encoder_down".format(
                        iteration, layer), down)
                self.encoders.append((convs, down))

                #  创建decoders的部分,仿照encoders
                #  和层次有关的常数,注意本部分不需要mid_channel,因为第一个卷积核就已经升高维度了
                in_channel = self.filters_list[self.num_layers -
                                               layer] + self.filters_list[
                                                   self.num_layers - 1 - layer]
                out_channel = self.filters_list[self.num_layers - 1 - layer]
                #  创建decoders模型
                if iteration == 0:
                    #  创建并添加复用卷积核
                    #  从第一个卷积核就升高通道数
                    conv1 = Conv2d(in_channel,
                                   out_channel,
                                   kernel_size=(3, 3),
                                   padding=(1, 1),
                                   stride=(1, 1))
                    conv2 = Conv2d(out_channel,
                                   out_channel,
                                   kernel_size=(3, 3),
                                   padding=(1, 1),
                                   stride=(1, 1))
                    conv3 = ConvTranspose2d(
                        out_channel,
                        out_channel,
                        kernel_size=(3, 3),
                        padding=(1, 1),
                        stride=(2, 2),
                        output_padding=(
                            1, 1))  # 此处和tensorflow有点区别,为了完整的形状,需要用output补一补
                    self.reuse_deconvs.append((conv1, conv2, conv3))
                #  创建encoder
                #  首先构造两个CONV
                convs = Sequential(
                    self.reuse_deconvs[layer][0], ReLU(),
                    BatchNorm2d(out_channel,
                                momentum=self.batch_norm_momentum),
                    self.reuse_deconvs[layer][1], ReLU(),
                    BatchNorm2d(out_channel,
                                momentum=self.batch_norm_momentum))
                #  构造UP
                up = Sequential(
                    self.reuse_deconvs[layer][2], ReLU(),
                    BatchNorm2d(out_channel,
                                momentum=self.batch_norm_momentum))
                self.add_module(
                    "iteration{0}_layer{1}_decoder_convs".format(
                        iteration, layer), convs)
                self.add_module(
                    "iteration{0}_layer{1}_decoder_up".format(
                        iteration, layer), up)
                self.decoders.append((convs, up))
        #  创建middle层
        self.middles = Sequential(
            Conv2d(self.filters_list[-1],
                   self.filters_list[-1],
                   kernel_size=(3, 3),
                   padding=(1, 1),
                   stride=(1, 1)), ReLU(),
            BatchNorm2d(self.filters_list[-1],
                        momentum=self.batch_norm_momentum),
            Conv2d(self.filters_list[-1],
                   self.filters_list[-1],
                   kernel_size=(3, 3),
                   padding=(1, 1),
                   stride=(1, 1)), ReLU(),
            BatchNorm2d(self.filters_list[-1],
                        momentum=self.batch_norm_momentum),
            ConvTranspose2d(self.filters_list[-1],
                            self.filters_list[-1],
                            kernel_size=(3, 3),
                            padding=(1, 1),
                            stride=(2, 2),
                            output_padding=(1, 1)), ReLU(),
            BatchNorm2d(self.filters_list[-1],
                        momentum=self.batch_norm_momentum))
        self.post_transform_conv_block = Sequential(
            Conv2d(self.filters_list[0] * self.iterations,
                   self.filters_list[0],
                   kernel_size=(3, 3),
                   padding=(1, 1),
                   stride=(1, 1))
            if self.integrate else Conv2d(self.filters_list[0],
                                          self.filters_list[0],
                                          kernel_size=(3, 3),
                                          padding=(1, 1),
                                          stride=(1, 1)),
            ReLU(),
            BatchNorm2d(self.filters_list[0],
                        momentum=self.batch_norm_momentum),
            Conv2d(self.filters_list[0],
                   self.filters_list[0],
                   kernel_size=(3, 3),
                   padding=(1, 1),
                   stride=(1, 1)),
            ReLU(),
            BatchNorm2d(self.filters_list[0],
                        momentum=self.batch_norm_momentum),
            Conv2d(self.filters_list[0], 1, kernel_size=(1, 1), stride=(1, 1)),
            Sigmoid(),
        )
        "loss_function_fn": lambda: CrossEntropyLoss(reduction="sum"),
        "target_fn": lambda: classification_targets((3, 2, 3, 2), 4),
        "id_prefix": "multi-d-CrossEntropyLoss",
    },
]

###############################################################################
#                                Custom Pad mod                               #
###############################################################################
SECONDORDER_SETTINGS += [
    {
        "input_fn":
        lambda: rand(5, 3, 4, 4),
        "module_fn":
        lambda: Sequential(
            Conv2d(in_channels=3, out_channels=2, kernel_size=2),
            Pad((1, 1, 0, 2), mode="constant", value=0.0),
            Conv2d(in_channels=2, out_channels=2, kernel_size=3, stride=2),
            Flatten(),
            Linear(8, 2),
        ),
        "loss_function_fn":
        lambda: MSELoss(reduction="mean"),
        "target_fn":
        lambda: regression_targets((5, 2)),
    },
    {
        "input_fn":
        lambda: rand(5, 3, 4, 4),
        "module_fn":
        lambda: Sequential(
 def __init__(self, ic, oc, *kwargs):
     super().__init__()
     self.conv = Conv2d(ic, oc, bias=False, *kwargs)
     self.batch_norm = BatchNorm2d(oc)
     self.activation = LeakyReLU()