def __init__(self, in_channels, num_classes, name=None): super(InceptionAux, self).__init__() self.num_classes = num_classes self.pool0 = AvgPool2D(pool_size=5, pool_stride=3) self.conv0 = ConvBNLayer(in_channels, 128, 1, name=name + '.conv0') self.conv1 = ConvBNLayer(128, 768, 5, name=name + '.conv1') self.pool1 = AvgPool2D(global_pooling=True)
def __init__(self, num_channels, num_filters, filter_size, stride=1, groups=1, is_vd_mode=False, act=None, name=None): super(ConvBNLayer, self).__init__() self.is_vd_mode = is_vd_mode self._pool2d_avg = AvgPool2D(kernel_size=2, stride=2, padding=0, ceil_mode=True) self._conv = Conv2D(in_channels=num_channels, out_channels=num_filters, kernel_size=filter_size, stride=stride, padding=(filter_size - 1) // 2, groups=groups, weight_attr=ParamAttr(name=name + "_weights"), bias_attr=False) bn_name = name + '_bn' self._batch_norm = BatchNorm( num_filters, act=act, param_attr=ParamAttr(name=bn_name + '_scale'), bias_attr=ParamAttr(bn_name + '_offset'), moving_mean_name=bn_name + '_mean', moving_variance_name=bn_name + '_variance')
def __init__(self, num_channels, num_filters, filter_size, stride=1, groups=1, is_vd_mode=False, act=None, lr_mult=1.0, data_format="NCHW"): super().__init__() self.is_vd_mode = is_vd_mode self.act = act self.avg_pool = AvgPool2D( kernel_size=2, stride=2, padding=0, ceil_mode=True) self.conv = Conv2D( in_channels=num_channels, out_channels=num_filters, kernel_size=filter_size, stride=stride, padding=(filter_size - 1) // 2, groups=groups, weight_attr=ParamAttr(learning_rate=lr_mult), bias_attr=False, data_format=data_format) self.bn = BatchNorm( num_filters, param_attr=ParamAttr(learning_rate=lr_mult), bias_attr=ParamAttr(learning_rate=lr_mult), data_layout=data_format) self.relu = nn.ReLU()
def __init__(self, in_channels: int, out_channels: int, kernel_size: int, stride: int = 1, dilation: int = 1, groups: int = 1, is_vd_mode: bool = False, act: str = None, name: str = None): super(ConvBNLayer, self).__init__() self.is_vd_mode = is_vd_mode self._pool2d_avg = AvgPool2D(kernel_size=2, stride=2, padding=0, ceil_mode=True) self._conv = Conv2D( in_channels=in_channels, out_channels=out_channels, kernel_size=kernel_size, stride=stride, padding=(kernel_size - 1) // 2 if dilation == 1 else 0, dilation=dilation, groups=groups, bias_attr=False) self._batch_norm = SyncBatchNorm(out_channels) self._act_op = Activation(act=act)
def __init__(self, num_classes=1000, with_pool=True): super(GoogLeNet, self).__init__() self.num_classes = num_classes self.with_pool = with_pool self._conv = ConvLayer(3, 64, 7, 2) self._pool = MaxPool2D(kernel_size=3, stride=2) self._conv_1 = ConvLayer(64, 64, 1) self._conv_2 = ConvLayer(64, 192, 3) self._ince3a = Inception(192, 192, 64, 96, 128, 16, 32, 32) self._ince3b = Inception(256, 256, 128, 128, 192, 32, 96, 64) self._ince4a = Inception(480, 480, 192, 96, 208, 16, 48, 64) self._ince4b = Inception(512, 512, 160, 112, 224, 24, 64, 64) self._ince4c = Inception(512, 512, 128, 128, 256, 24, 64, 64) self._ince4d = Inception(512, 512, 112, 144, 288, 32, 64, 64) self._ince4e = Inception(528, 528, 256, 160, 320, 32, 128, 128) self._ince5a = Inception(832, 832, 256, 160, 320, 32, 128, 128) self._ince5b = Inception(832, 832, 384, 192, 384, 48, 128, 128) if with_pool: # out self._pool_5 = AdaptiveAvgPool2D(1) # out1 self._pool_o1 = AvgPool2D(kernel_size=5, stride=3) # out2 self._pool_o2 = AvgPool2D(kernel_size=5, stride=3) if num_classes > 0: # out self._drop = Dropout(p=0.4, mode="downscale_in_infer") self._fc_out = Linear( 1024, num_classes, weight_attr=xavier(1024, 1)) # out1 self._conv_o1 = ConvLayer(512, 128, 1) self._fc_o1 = Linear(1152, 1024, weight_attr=xavier(2048, 1)) self._drop_o1 = Dropout(p=0.7, mode="downscale_in_infer") self._out1 = Linear(1024, num_classes, weight_attr=xavier(1024, 1)) # out2 self._conv_o2 = ConvLayer(528, 128, 1) self._fc_o2 = Linear(1152, 1024, weight_attr=xavier(2048, 1)) self._drop_o2 = Dropout(p=0.7, mode="downscale_in_infer") self._out2 = Linear(1024, num_classes, weight_attr=xavier(1024, 1))
def __init__(self, num_channels, channels_7x7): super().__init__() self.branch1x1 = ConvBNLayer(num_channels=num_channels, num_filters=192, filter_size=1, act="relu") self.branch7x7_1 = ConvBNLayer(num_channels=num_channels, num_filters=channels_7x7, filter_size=1, stride=1, act="relu") self.branch7x7_2 = ConvBNLayer(num_channels=channels_7x7, num_filters=channels_7x7, filter_size=(1, 7), stride=1, padding=(0, 3), act="relu") self.branch7x7_3 = ConvBNLayer(num_channels=channels_7x7, num_filters=192, filter_size=(7, 1), stride=1, padding=(3, 0), act="relu") self.branch7x7dbl_1 = ConvBNLayer(num_channels=num_channels, num_filters=channels_7x7, filter_size=1, act="relu") self.branch7x7dbl_2 = ConvBNLayer(num_channels=channels_7x7, num_filters=channels_7x7, filter_size=(7, 1), padding=(3, 0), act="relu") self.branch7x7dbl_3 = ConvBNLayer(num_channels=channels_7x7, num_filters=channels_7x7, filter_size=(1, 7), padding=(0, 3), act="relu") self.branch7x7dbl_4 = ConvBNLayer(num_channels=channels_7x7, num_filters=channels_7x7, filter_size=(7, 1), padding=(3, 0), act="relu") self.branch7x7dbl_5 = ConvBNLayer(num_channels=channels_7x7, num_filters=192, filter_size=(1, 7), padding=(0, 3), act="relu") self.branch_pool = AvgPool2D(kernel_size=3, stride=1, padding=1, exclusive=False) self.branch_pool_conv = ConvBNLayer(num_channels=num_channels, num_filters=192, filter_size=1, act="relu")
def __init__(self, num_channels, num_output_features): super(TransitionLayer, self).__init__() self.conv_ac_func = BNACConvLayer(num_channels=num_channels, num_filters=num_output_features, filter_size=1, pad=0, stride=1) self.pool2d_avg = AvgPool2D(kernel_size=2, stride=2, padding=0)
def __init__(self, name=None): super(InceptionB, self).__init__() self._pool = AvgPool2D(kernel_size=3, stride=1, padding=1) self._conv1 = ConvBNLayer( 1024, 128, 1, act="relu", name="inception_b" + name + "_1x1") self._conv2 = ConvBNLayer( 1024, 384, 1, act="relu", name="inception_b" + name + "_1x1_2") self._conv3_1 = ConvBNLayer( 1024, 192, 1, act="relu", name="inception_b" + name + "_1x7_reduce") self._conv3_2 = ConvBNLayer( 192, 224, (1, 7), padding=(0, 3), act="relu", name="inception_b" + name + "_1x7") self._conv3_3 = ConvBNLayer( 224, 256, (7, 1), padding=(3, 0), act="relu", name="inception_b" + name + "_7x1") self._conv4_1 = ConvBNLayer( 1024, 192, 1, act="relu", name="inception_b" + name + "_7x1_2_reduce") self._conv4_2 = ConvBNLayer( 192, 192, (1, 7), padding=(0, 3), act="relu", name="inception_b" + name + "_1x7_2") self._conv4_3 = ConvBNLayer( 192, 224, (7, 1), padding=(3, 0), act="relu", name="inception_b" + name + "_7x1_2") self._conv4_4 = ConvBNLayer( 224, 224, (1, 7), padding=(0, 3), act="relu", name="inception_b" + name + "_1x7_3") self._conv4_5 = ConvBNLayer( 224, 256, (7, 1), padding=(3, 0), act="relu", name="inception_b" + name + "_7x1_3")
def __init__(self, num_channels): super().__init__() self.branch1x1 = ConvNormActivation(in_channels=num_channels, out_channels=320, kernel_size=1, padding=0, activation_layer=nn.ReLU) self.branch3x3_1 = ConvNormActivation(in_channels=num_channels, out_channels=384, kernel_size=1, padding=0, activation_layer=nn.ReLU) self.branch3x3_2a = ConvNormActivation(in_channels=384, out_channels=384, kernel_size=(1, 3), padding=(0, 1), activation_layer=nn.ReLU) self.branch3x3_2b = ConvNormActivation(in_channels=384, out_channels=384, kernel_size=(3, 1), padding=(1, 0), activation_layer=nn.ReLU) self.branch3x3dbl_1 = ConvNormActivation(in_channels=num_channels, out_channels=448, kernel_size=1, padding=0, activation_layer=nn.ReLU) self.branch3x3dbl_2 = ConvNormActivation(in_channels=448, out_channels=384, kernel_size=3, padding=1, activation_layer=nn.ReLU) self.branch3x3dbl_3a = ConvNormActivation(in_channels=384, out_channels=384, kernel_size=(1, 3), padding=(0, 1), activation_layer=nn.ReLU) self.branch3x3dbl_3b = ConvNormActivation(in_channels=384, out_channels=384, kernel_size=(3, 1), padding=(1, 0), activation_layer=nn.ReLU) self.branch_pool = AvgPool2D(kernel_size=3, stride=1, padding=1, exclusive=False) self.branch_pool_conv = ConvNormActivation(in_channels=num_channels, out_channels=192, kernel_size=1, padding=0, activation_layer=nn.ReLU)
def __init__(self, num_channels, pool_features, name=None): super(InceptionA, self).__init__() self.branch1x1 = ConvBNLayer(num_channels=num_channels, num_filters=64, filter_size=1, act="relu", name="inception_a_branch1x1_" + name) self.branch5x5_1 = ConvBNLayer(num_channels=num_channels, num_filters=48, filter_size=1, act="relu", name="inception_a_branch5x5_1_" + name) self.branch5x5_2 = ConvBNLayer(num_channels=48, num_filters=64, filter_size=5, padding=2, act="relu", name="inception_a_branch5x5_2_" + name) self.branch3x3dbl_1 = ConvBNLayer(num_channels=num_channels, num_filters=64, filter_size=1, act="relu", name="inception_a_branch3x3dbl_1_" + name) self.branch3x3dbl_2 = ConvBNLayer(num_channels=64, num_filters=96, filter_size=3, padding=1, act="relu", name="inception_a_branch3x3dbl_2_" + name) self.branch3x3dbl_3 = ConvBNLayer(num_channels=96, num_filters=96, filter_size=3, padding=1, act="relu", name="inception_a_branch3x3dbl_3_" + name) self.branch_pool = AvgPool2D(kernel_size=3, stride=1, padding=1, exclusive=False) self.branch_pool_conv = ConvBNLayer(num_channels=num_channels, num_filters=pool_features, filter_size=1, act="relu", name="inception_a_branch_pool_" + name)
def __init__(self, in_channels, c7, name=None): super(Fid_inceptionC, self).__init__() self.branch1x1 = ConvBNLayer(in_channels, 192, 1, name=name + '.branch1x1') self.branch7x7_1 = ConvBNLayer(in_channels, c7, 1, name=name + '.branch7x7_1') self.branch7x7_2 = ConvBNLayer(c7, c7, (1, 7), padding=(0, 3), name=name + '.branch7x7_2') self.branch7x7_3 = ConvBNLayer(c7, 192, (7, 1), padding=(3, 0), name=name + '.branch7x7_3') self.branch7x7dbl_1 = ConvBNLayer(in_channels, c7, 1, name=name + '.branch7x7dbl_1') self.branch7x7dbl_2 = ConvBNLayer(c7, c7, (7, 1), padding=(3, 0), name=name + '.branch7x7dbl_2') self.branch7x7dbl_3 = ConvBNLayer(c7, c7, (1, 7), padding=(0, 3), name=name + '.branch7x7dbl_3') self.branch7x7dbl_4 = ConvBNLayer(c7, c7, (7, 1), padding=(3, 0), name=name + '.branch7x7dbl_4') self.branch7x7dbl_5 = ConvBNLayer(c7, 192, (1, 7), padding=(0, 3), name=name + '.branch7x7dbl_5') self.branch_pool0 = AvgPool2D(pool_size=3, pool_stride=1, pool_padding=1, exclusive=True) self.branch_pool = ConvBNLayer(in_channels, 192, 1, name=name + '.branch_pool')
def __init__(self, num_channels): super().__init__() self.branch1x1 = ConvBNLayer(num_channels=num_channels, num_filters=320, filter_size=1, act="relu") self.branch3x3_1 = ConvBNLayer(num_channels=num_channels, num_filters=384, filter_size=1, act="relu") self.branch3x3_2a = ConvBNLayer(num_channels=384, num_filters=384, filter_size=(1, 3), padding=(0, 1), act="relu") self.branch3x3_2b = ConvBNLayer(num_channels=384, num_filters=384, filter_size=(3, 1), padding=(1, 0), act="relu") self.branch3x3dbl_1 = ConvBNLayer(num_channels=num_channels, num_filters=448, filter_size=1, act="relu") self.branch3x3dbl_2 = ConvBNLayer(num_channels=448, num_filters=384, filter_size=3, padding=1, act="relu") self.branch3x3dbl_3a = ConvBNLayer(num_channels=384, num_filters=384, filter_size=(1, 3), padding=(0, 1), act="relu") self.branch3x3dbl_3b = ConvBNLayer(num_channels=384, num_filters=384, filter_size=(3, 1), padding=(1, 0), act="relu") self.branch_pool = AvgPool2D(kernel_size=3, stride=1, padding=1, exclusive=False) self.branch_pool_conv = ConvBNLayer(num_channels=num_channels, num_filters=192, filter_size=1, act="relu")
def __init__(self, name=None): super(InceptionC, self).__init__() self._pool = AvgPool2D(kernel_size=3, stride=1, padding=1) self._conv1 = ConvBNLayer( 1536, 256, 1, act="relu", name="inception_c" + name + "_1x1") self._conv2 = ConvBNLayer( 1536, 256, 1, act="relu", name="inception_c" + name + "_1x1_2") self._conv3_0 = ConvBNLayer( 1536, 384, 1, act="relu", name="inception_c" + name + "_1x1_3") self._conv3_1 = ConvBNLayer( 384, 256, (1, 3), padding=(0, 1), act="relu", name="inception_c" + name + "_1x3") self._conv3_2 = ConvBNLayer( 384, 256, (3, 1), padding=(1, 0), act="relu", name="inception_c" + name + "_3x1") self._conv4_0 = ConvBNLayer( 1536, 384, 1, act="relu", name="inception_c" + name + "_1x1_4") self._conv4_00 = ConvBNLayer( 384, 448, (1, 3), padding=(0, 1), act="relu", name="inception_c" + name + "_1x3_2") self._conv4_000 = ConvBNLayer( 448, 512, (3, 1), padding=(1, 0), act="relu", name="inception_c" + name + "_3x1_2") self._conv4_1 = ConvBNLayer( 512, 256, (1, 3), padding=(0, 1), act="relu", name="inception_c" + name + "_1x3_3") self._conv4_2 = ConvBNLayer( 512, 256, (3, 1), padding=(1, 0), act="relu", name="inception_c" + name + "_3x1_3")
def __init__(self, in_channels, name=None): super(Fid_inceptionE_1, self).__init__() self.branch1x1 = ConvBNLayer(in_channels, 320, 1, name=name + '.branch1x1') self.branch3x3_1 = ConvBNLayer(in_channels, 384, 1, name=name + '.branch3x3_1') self.branch3x3_2a = ConvBNLayer(384, 384, (1, 3), padding=(0, 1), name=name + '.branch3x3_2a') self.branch3x3_2b = ConvBNLayer(384, 384, (3, 1), padding=(1, 0), name=name + '.branch3x3_2b') self.branch3x3dbl_1 = ConvBNLayer(in_channels, 448, 1, name=name + '.branch3x3dbl_1') self.branch3x3dbl_2 = ConvBNLayer(448, 384, 3, padding=1, name=name + '.branch3x3dbl_2') self.branch3x3dbl_3a = ConvBNLayer(384, 384, (1, 3), padding=(0, 1), name=name + '.branch3x3dbl_3a') self.branch3x3dbl_3b = ConvBNLayer(384, 384, (3, 1), padding=(1, 0), name=name + '.branch3x3dbl_3b') self.branch_pool0 = AvgPool2D(pool_size=3, pool_stride=1, pool_padding=1, exclusive=True) self.branch_pool = ConvBNLayer(in_channels, 192, 1, name=name + '.branch_pool')
def __init__(self, num_channels1, num_channels2, num_filters, stride, scales, shortcut=True, if_first=False, name=None): super(BottleneckBlock, self).__init__() self.stride = stride self.scales = scales self.conv0 = ConvBNLayer(num_channels=num_channels1, num_filters=num_filters, filter_size=1, act='relu', name=name + "_branch2a") self.conv1_list = [] for s in range(scales - 1): conv1 = self.add_sublayer( name + '_branch2b_' + str(s + 1), ConvBNLayer(num_channels=num_filters // scales, num_filters=num_filters // scales, filter_size=3, stride=stride, act='relu', name=name + '_branch2b_' + str(s + 1))) self.conv1_list.append(conv1) self.pool2d_avg = AvgPool2D(kernel_size=3, stride=stride, padding=1) self.conv2 = ConvBNLayer(num_channels=num_filters, num_filters=num_channels2, filter_size=1, act=None, name=name + "_branch2c") if not shortcut: self.short = ConvBNLayer(num_channels=num_channels1, num_filters=num_channels2, filter_size=1, stride=1, is_vd_mode=False if if_first else True, name=name + "_branch1") self.shortcut = shortcut
def __init__(self, num_channels, pool_features): super().__init__() self.branch1x1 = ConvNormActivation(in_channels=num_channels, out_channels=64, kernel_size=1, padding=0, activation_layer=nn.ReLU) self.branch5x5_1 = ConvNormActivation(in_channels=num_channels, out_channels=48, kernel_size=1, padding=0, activation_layer=nn.ReLU) self.branch5x5_2 = ConvNormActivation(in_channels=48, out_channels=64, kernel_size=5, padding=2, activation_layer=nn.ReLU) self.branch3x3dbl_1 = ConvNormActivation(in_channels=num_channels, out_channels=64, kernel_size=1, padding=0, activation_layer=nn.ReLU) self.branch3x3dbl_2 = ConvNormActivation(in_channels=64, out_channels=96, kernel_size=3, padding=1, activation_layer=nn.ReLU) self.branch3x3dbl_3 = ConvNormActivation(in_channels=96, out_channels=96, kernel_size=3, padding=1, activation_layer=nn.ReLU) self.branch_pool = AvgPool2D(kernel_size=3, stride=1, padding=1, exclusive=False) self.branch_pool_conv = ConvNormActivation(in_channels=num_channels, out_channels=pool_features, kernel_size=1, padding=0, activation_layer=nn.ReLU)
def __init__(self, in_channels, pool_features, name=None): super(Fid_inceptionA, self).__init__() self.branch1x1 = ConvBNLayer(in_channels, 64, 1, name=name + '.branch1x1') self.branch5x5_1 = ConvBNLayer(in_channels, 48, 1, name=name + '.branch5x5_1') self.branch5x5_2 = ConvBNLayer(48, 64, 5, padding=2, name=name + '.branch5x5_2') self.branch3x3dbl_1 = ConvBNLayer(in_channels, 64, 1, name=name + '.branch3x3dbl_1') self.branch3x3dbl_2 = ConvBNLayer(64, 96, 3, padding=1, name=name + '.branch3x3dbl_2') self.branch3x3dbl_3 = ConvBNLayer(96, 96, 3, padding=1, name=name + '.branch3x3dbl_3') self.branch_pool0 = AvgPool2D(pool_size=3, pool_stride=1, pool_padding=1, exclusive=True) self.branch_pool = ConvBNLayer(in_channels, pool_features, 1, name=name + '.branch_pool')
def __init__(self, in_channels, out_channels, kernel_size, stride=1, groups=1, is_tweaks_mode=False, act=None, lr_mult=1.0, name=None): super(ConvBNLayer, self).__init__() self.is_tweaks_mode = is_tweaks_mode self._pool2d_avg = AvgPool2D(kernel_size=2, stride=2, padding=0, ceil_mode=True) self._conv = Conv2D(in_channels=in_channels, out_channels=out_channels, kernel_size=kernel_size, stride=stride, padding=(kernel_size - 1) // 2, groups=groups, weight_attr=ParamAttr(name=name + "_weights", learning_rate=lr_mult), bias_attr=False) if name == "conv1": bn_name = "bn_" + name else: bn_name = "bn" + name[3:] self._batch_norm = BatchNorm( out_channels, act=act, param_attr=ParamAttr(name=bn_name + '_scale', learning_rate=lr_mult, regularizer=L2Decay(0.0)), bias_attr=ParamAttr(bn_name + '_offset', learning_rate=lr_mult, regularizer=L2Decay(0.0)), moving_mean_name=bn_name + '_mean', moving_variance_name=bn_name + '_variance')
def __init__(self, in_channels, out_channels, kernel_size, stride=1, groups=1, is_tweaks_mode=False, act=None, name=None): super(ConvBNLayer, self).__init__() self.is_tweaks_mode = is_tweaks_mode #ResNet-D 1/2:add a 2×2 average pooling layer with a stride of 2 before the convolution, # whose stride is changed to 1, works well in practice. self._pool2d_avg = AvgPool2D(kernel_size=2, stride=2, padding=0, ceil_mode=True) self._conv = Conv2D(in_channels=in_channels, out_channels=out_channels, kernel_size=kernel_size, stride=stride, padding=(kernel_size - 1) // 2, groups=groups, weight_attr=ParamAttr(name=name + "_weights"), bias_attr=False) if name == "conv1": bn_name = "bn_" + name else: bn_name = "bn" + name[3:] self._act = act self._batch_norm = BatchNorm2D( out_channels, weight_attr=ParamAttr(name=bn_name + "_scale", regularizer=L2Decay(0.0)), bias_attr=ParamAttr(bn_name + "_offset", regularizer=L2Decay(0.0)))
def __init__(self, num_channels, pool_features): super().__init__() self.branch1x1 = ConvBNLayer(num_channels=num_channels, num_filters=64, filter_size=1, act="relu") self.branch5x5_1 = ConvBNLayer(num_channels=num_channels, num_filters=48, filter_size=1, act="relu") self.branch5x5_2 = ConvBNLayer(num_channels=48, num_filters=64, filter_size=5, padding=2, act="relu") self.branch3x3dbl_1 = ConvBNLayer(num_channels=num_channels, num_filters=64, filter_size=1, act="relu") self.branch3x3dbl_2 = ConvBNLayer(num_channels=64, num_filters=96, filter_size=3, padding=1, act="relu") self.branch3x3dbl_3 = ConvBNLayer(num_channels=96, num_filters=96, filter_size=3, padding=1, act="relu") self.branch_pool = AvgPool2D(kernel_size=3, stride=1, padding=1, exclusive=False) self.branch_pool_conv = ConvBNLayer(num_channels=num_channels, num_filters=pool_features, filter_size=1, act="relu")
def __init__(self, name): super(InceptionA, self).__init__() self._pool = AvgPool2D(kernel_size=3, stride=1, padding=1) self._conv1 = ConvBNLayer( 384, 96, 1, act="relu", name="inception_a" + name + "_1x1") self._conv2 = ConvBNLayer( 384, 96, 1, act="relu", name="inception_a" + name + "_1x1_2") self._conv3_1 = ConvBNLayer( 384, 64, 1, act="relu", name="inception_a" + name + "_3x3_reduce") self._conv3_2 = ConvBNLayer( 64, 96, 3, padding=1, act="relu", name="inception_a" + name + "_3x3") self._conv4_1 = ConvBNLayer( 384, 64, 1, act="relu", name="inception_a" + name + "_3x3_2_reduce") self._conv4_2 = ConvBNLayer( 64, 96, 3, padding=1, act="relu", name="inception_a" + name + "_3x3_2") self._conv4_3 = ConvBNLayer( 96, 96, 3, padding=1, act="relu", name="inception_a" + name + "_3x3_3")
def __init__(self, num_channels, ch1x1, ch3x3reduced, ch3x3, doublech3x3reduced, doublech3x3_1, doublech3x3_2, pool_proj): ''' @Brief 传入参数用于定义 `Inception` 结构 @Parameters num_channels : 传入图片通道数 ch1x1 : 1x1卷积操作的输出通道数 ch3x3reduced : 3x3卷积之前的1x1卷积的通道数 ch3x3 : 3x3卷积操作的输出通道数 doublech3x3reduced : 两个3x3卷积叠加之前的1x1卷积的通道数 doublech3x3_1 : 第一个3x3卷积操作的输出通道数 doublech3x3_2 : 第而个3x3卷积操作的输出通道数 pool_proj : 池化操作之后1x1卷积的通道数 @Return 返回 `Inception` 网络模型 @Examples ------------ ''' super(Inception, self).__init__() self.branch1 = ConvBNLayer(num_channels=num_channels, num_filters=ch1x1, filter_size=1, stride=1, padding=0) self.branch2 = paddle.nn.Sequential( ConvBNLayer(num_channels=num_channels, num_filters=ch3x3reduced, filter_size=1, stride=1, padding=0), ConvBNLayer(num_channels=ch3x3reduced, num_filters=ch3x3, filter_size=3, stride=1, padding=1) ) self.branch3 = paddle.nn.Sequential( ConvBNLayer(num_channels=num_channels, num_filters=doublech3x3reduced, filter_size=1, stride=1, padding=0), ConvBNLayer(num_channels=doublech3x3reduced, num_filters=doublech3x3_1, filter_size=3, stride=1, padding=1), ConvBNLayer(num_channels=doublech3x3_1, num_filters=doublech3x3_2, filter_size=3, stride=1, padding=1) ) self.branch4 = paddle.nn.Sequential( AvgPool2D(kernel_size=3, stride=1, padding=1), ConvBNLayer(num_channels=num_channels, num_filters=pool_proj, filter_size=1, stride=1, padding=0) )
def __init__(self, class_num=1000): super(GoogLeNetDY, self).__init__() self._conv = ConvLayer(3, 64, 7, 2, name="conv1") self._pool = MaxPool2D(kernel_size=3, stride=2) self._conv_1 = ConvLayer(64, 64, 1, name="conv2_1x1") self._conv_2 = ConvLayer(64, 192, 3, name="conv2_3x3") self._ince3a = Inception(192, 192, 64, 96, 128, 16, 32, 32, name="ince3a") self._ince3b = Inception(256, 256, 128, 128, 192, 32, 96, 64, name="ince3b") self._ince4a = Inception(480, 480, 192, 96, 208, 16, 48, 64, name="ince4a") self._ince4b = Inception(512, 512, 160, 112, 224, 24, 64, 64, name="ince4b") self._ince4c = Inception(512, 512, 128, 128, 256, 24, 64, 64, name="ince4c") self._ince4d = Inception(512, 512, 112, 144, 288, 32, 64, 64, name="ince4d") self._ince4e = Inception(528, 528, 256, 160, 320, 32, 128, 128, name="ince4e") self._ince5a = Inception(832, 832, 256, 160, 320, 32, 128, 128, name="ince5a") self._ince5b = Inception(832, 832, 384, 192, 384, 48, 128, 128, name="ince5b") self._pool_5 = AdaptiveAvgPool2D(1) self._drop = Dropout(p=0.4, mode="downscale_in_infer") self._fc_out = Linear(1024, class_num, weight_attr=xavier(1024, 1, "out"), bias_attr=ParamAttr(name="out_offset")) self._pool_o1 = AvgPool2D(kernel_size=5, stride=3) self._conv_o1 = ConvLayer(512, 128, 1, name="conv_o1") self._fc_o1 = Linear(1152, 1024, weight_attr=xavier(2048, 1, "fc_o1"), bias_attr=ParamAttr(name="fc_o1_offset")) self._drop_o1 = Dropout(p=0.7, mode="downscale_in_infer") self._out1 = Linear(1024, class_num, weight_attr=xavier(1024, 1, "out1"), bias_attr=ParamAttr(name="out1_offset")) self._pool_o2 = AvgPool2D(kernel_size=5, stride=3) self._conv_o2 = ConvLayer(528, 128, 1, name="conv_o2") self._fc_o2 = Linear(1152, 1024, weight_attr=xavier(2048, 1, "fc_o2"), bias_attr=ParamAttr(name="fc_o2_offset")) self._drop_o2 = Dropout(p=0.7, mode="downscale_in_infer") self._out2 = Linear(1024, class_num, weight_attr=xavier(1024, 1, "out2"), bias_attr=ParamAttr(name="out2_offset"))
def __init__(self, num_channels, channels_7x7): super().__init__() self.branch1x1 = ConvNormActivation(in_channels=num_channels, out_channels=192, kernel_size=1, padding=0, activation_layer=nn.ReLU) self.branch7x7_1 = ConvNormActivation(in_channels=num_channels, out_channels=channels_7x7, kernel_size=1, stride=1, padding=0, activation_layer=nn.ReLU) self.branch7x7_2 = ConvNormActivation(in_channels=channels_7x7, out_channels=channels_7x7, kernel_size=(1, 7), stride=1, padding=(0, 3), activation_layer=nn.ReLU) self.branch7x7_3 = ConvNormActivation(in_channels=channels_7x7, out_channels=192, kernel_size=(7, 1), stride=1, padding=(3, 0), activation_layer=nn.ReLU) self.branch7x7dbl_1 = ConvNormActivation(in_channels=num_channels, out_channels=channels_7x7, kernel_size=1, padding=0, activation_layer=nn.ReLU) self.branch7x7dbl_2 = ConvNormActivation(in_channels=channels_7x7, out_channels=channels_7x7, kernel_size=(7, 1), padding=(3, 0), activation_layer=nn.ReLU) self.branch7x7dbl_3 = ConvNormActivation(in_channels=channels_7x7, out_channels=channels_7x7, kernel_size=(1, 7), padding=(0, 3), activation_layer=nn.ReLU) self.branch7x7dbl_4 = ConvNormActivation(in_channels=channels_7x7, out_channels=channels_7x7, kernel_size=(7, 1), padding=(3, 0), activation_layer=nn.ReLU) self.branch7x7dbl_5 = ConvNormActivation(in_channels=channels_7x7, out_channels=192, kernel_size=(1, 7), padding=(0, 3), activation_layer=nn.ReLU) self.branch_pool = AvgPool2D(kernel_size=3, stride=1, padding=1, exclusive=False) self.branch_pool_conv = ConvNormActivation(in_channels=num_channels, out_channels=192, kernel_size=1, padding=0, activation_layer=nn.ReLU)
def __init__(self, num_channels, name=None): super(InceptionE, self).__init__() self.branch1x1 = ConvBNLayer(num_channels=num_channels, num_filters=320, filter_size=1, act="relu", name="inception_e_branch1x1_" + name) self.branch3x3_1 = ConvBNLayer(num_channels=num_channels, num_filters=384, filter_size=1, act="relu", name="inception_e_branch3x3_1_" + name) self.branch3x3_2a = ConvBNLayer(num_channels=384, num_filters=384, filter_size=(1, 3), padding=(0, 1), act="relu", name="inception_e_branch3x3_2a_" + name) self.branch3x3_2b = ConvBNLayer(num_channels=384, num_filters=384, filter_size=(3, 1), padding=(1, 0), act="relu", name="inception_e_branch3x3_2b_" + name) self.branch3x3dbl_1 = ConvBNLayer(num_channels=num_channels, num_filters=448, filter_size=1, act="relu", name="inception_e_branch3x3dbl_1_" + name) self.branch3x3dbl_2 = ConvBNLayer(num_channels=448, num_filters=384, filter_size=3, padding=1, act="relu", name="inception_e_branch3x3dbl_2_" + name) self.branch3x3dbl_3a = ConvBNLayer( num_channels=384, num_filters=384, filter_size=(1, 3), padding=(0, 1), act="relu", name="inception_e_branch3x3dbl_3a_" + name) self.branch3x3dbl_3b = ConvBNLayer( num_channels=384, num_filters=384, filter_size=(3, 1), padding=(1, 0), act="relu", name="inception_e_branch3x3dbl_3b_" + name) self.branch_pool = AvgPool2D(kernel_size=3, stride=1, padding=1, exclusive=False) self.branch_pool_conv = ConvBNLayer(num_channels=num_channels, num_filters=192, filter_size=1, act="relu", name="inception_e_branch_pool_" + name)
def __init__(self, num_channels, channels_7x7, name=None): super(InceptionC, self).__init__() self.branch1x1 = ConvBNLayer(num_channels=num_channels, num_filters=192, filter_size=1, act="relu", name="inception_c_branch1x1_" + name) self.branch7x7_1 = ConvBNLayer(num_channels=num_channels, num_filters=channels_7x7, filter_size=1, stride=1, act="relu", name="inception_c_branch7x7_1_" + name) self.branch7x7_2 = ConvBNLayer(num_channels=channels_7x7, num_filters=channels_7x7, filter_size=(1, 7), stride=1, padding=(0, 3), act="relu", name="inception_c_branch7x7_2_" + name) self.branch7x7_3 = ConvBNLayer(num_channels=channels_7x7, num_filters=192, filter_size=(7, 1), stride=1, padding=(3, 0), act="relu", name="inception_c_branch7x7_3_" + name) self.branch7x7dbl_1 = ConvBNLayer(num_channels=num_channels, num_filters=channels_7x7, filter_size=1, act="relu", name="inception_c_branch7x7dbl_1_" + name) self.branch7x7dbl_2 = ConvBNLayer(num_channels=channels_7x7, num_filters=channels_7x7, filter_size=(7, 1), padding=(3, 0), act="relu", name="inception_c_branch7x7dbl_2_" + name) self.branch7x7dbl_3 = ConvBNLayer(num_channels=channels_7x7, num_filters=channels_7x7, filter_size=(1, 7), padding=(0, 3), act="relu", name="inception_c_branch7x7dbl_3_" + name) self.branch7x7dbl_4 = ConvBNLayer(num_channels=channels_7x7, num_filters=channels_7x7, filter_size=(7, 1), padding=(3, 0), act="relu", name="inception_c_branch7x7dbl_4_" + name) self.branch7x7dbl_5 = ConvBNLayer(num_channels=channels_7x7, num_filters=192, filter_size=(1, 7), padding=(0, 3), act="relu", name="inception_c_branch7x7dbl_5_" + name) self.branch_pool = AvgPool2D(kernel_size=3, stride=1, padding=1, exclusive=False) self.branch_pool_conv = ConvBNLayer(num_channels=num_channels, num_filters=192, filter_size=1, act="relu", name="inception_c_branch_pool_" + name)
def __init__(self, inplanes, planes, stride=1, radix=1, cardinality=1, bottleneck_width=64, avd=False, avd_first=False, dilation=1, is_first=False, rectify_avg=False, last_gamma=False, avg_down=False, name=None): super(BottleneckBlock, self).__init__() self.inplanes = inplanes self.planes = planes self.stride = stride self.radix = radix self.cardinality = cardinality self.avd = avd self.avd_first = avd_first self.dilation = dilation self.is_first = is_first self.rectify_avg = rectify_avg self.last_gamma = last_gamma self.avg_down = avg_down group_width = int(planes * (bottleneck_width / 64.)) * cardinality self.conv1 = ConvBNLayer(num_channels=self.inplanes, num_filters=group_width, filter_size=1, stride=1, groups=1, act="relu", name=name + "_conv1") if avd and avd_first and (stride > 1 or is_first): self.avg_pool2d_1 = AvgPool2D(kernel_size=3, stride=stride, padding=1) if radix >= 1: self.conv2 = SplatConv(in_channels=group_width, channels=group_width, kernel_size=3, stride=1, padding=dilation, dilation=dilation, groups=cardinality, bias=False, radix=radix, rectify_avg=rectify_avg, name=name + "_splat") else: self.conv2 = ConvBNLayer(num_channels=group_width, num_filters=group_width, filter_size=3, stride=1, dilation=dilation, groups=cardinality, act="relu", name=name + "_conv2") if avd and avd_first == False and (stride > 1 or is_first): self.avg_pool2d_2 = AvgPool2D(kernel_size=3, stride=stride, padding=1) self.conv3 = ConvBNLayer(num_channels=group_width, num_filters=planes * 4, filter_size=1, stride=1, groups=1, act=None, name=name + "_conv3") if stride != 1 or self.inplanes != self.planes * 4: if avg_down: if dilation == 1: self.avg_pool2d_3 = AvgPool2D(kernel_size=stride, stride=stride, padding=0) else: self.avg_pool2d_3 = AvgPool2D(kernel_size=1, stride=1, padding=0, ceil_mode=True) self.conv4 = Conv2D(in_channels=self.inplanes, out_channels=planes * 4, kernel_size=1, stride=1, padding=0, groups=1, weight_attr=ParamAttr( name=name + "_weights", initializer=KaimingNormal()), bias_attr=False) else: self.conv4 = Conv2D(in_channels=self.inplanes, out_channels=planes * 4, kernel_size=1, stride=stride, padding=0, groups=1, weight_attr=ParamAttr( name=name + "_shortcut_weights", initializer=KaimingNormal()), bias_attr=False) bn_decay = 0.0 self._batch_norm = BatchNorm( planes * 4, act=None, param_attr=ParamAttr(name=name + "_shortcut_scale", regularizer=L2Decay(bn_decay)), bias_attr=ParamAttr(name + "_shortcut_offset", regularizer=L2Decay(bn_decay)), moving_mean_name=name + "_shortcut_mean", moving_variance_name=name + "_shortcut_variance")
def __init__(self, layers=50, num_classes=1000): super(ResNet, self).__init__() self.layers = layers supported_layers = [18, 34, 50, 101, 152] assert layers in supported_layers if layers == 18: depth = [2, 2, 2, 2] elif layers == 34: depth = [3, 4, 6, 3] elif layers == 50: depth = [3, 4, 6, 3] elif layers == 101: depth = [3, 4, 23, 3] elif layers == 152: depth = [3, 8, 36, 3] if layers < 50: num_channels = [64, 64, 128, 256] else: num_channels = [64, 256, 512, 1024] num_filters = [64, 128, 256, 512] self.conv = ConvBNLayer(num_channels=3, num_filters=64, filter_size=7, stride=2, act='relu') self.pool2d_max = MaxPool2D(kernel_size=3, stride=2, padding=1) if layers < 50: block = BasicBlock l1_shortcut = True else: block = BottleneckBlock l1_shortcut = False self.layer1 = Sequential(*self.make_layer(block, num_channels[0], num_filters[0], depth[0], stride=1, shortcut=l1_shortcut, name='layer1')) self.layer2 = Sequential(*self.make_layer(block, num_channels[1], num_filters[1], depth[1], stride=2, name='layer2')) self.layer3 = Sequential(*self.make_layer(block, num_channels[2], num_filters[2], depth[2], stride=1, name='layer3', dilation=2)) self.layer4 = Sequential(*self.make_layer(block, num_channels[3], num_filters[3], depth[3], stride=1, name='layer4', dilation=4)) self.last_pool = AvgPool2D( kernel_size=7 # ignore if global_pooling is True ) self.fc = Linear(in_features=num_filters[-1] * block.expansion, out_features=num_classes) self.out_dim = num_filters[-1] * block.expansion
def __init__(self, num_channels, ch1x1, ch3x3reduced, ch3x3, doublech3x3reduced, doublech3x3_1, doublech3x3_2, pool_proj): ''' @Brief `Inception` @Parameters num_channels : channel numbers of input tensor ch1x1 : output channel numbers of 1x1 conv ch3x3reduced : channel numbers of 1x1 conv before 3x3 conv ch3x3 : output channel numbers of 3x3 conv doublech3x3reduced : channel numbers of 1x1 conv before the double 3x3 convs doublech3x3_1 : output channel numbers of the first 3x3 conv doublech3x3_2 : output channel numbers of the second 3x3 conv pool_proj : output channel numbers of 1x1 conv after pool @Return `Inception` model ''' super(Inception, self).__init__() self.branch1 = ConvBNLayer(num_channels=num_channels, num_filters=ch1x1, filter_size=1, stride=1, padding=0) self.branch2 = paddle.nn.Sequential( ConvBNLayer(num_channels=num_channels, num_filters=ch3x3reduced, filter_size=1, stride=1, padding=0), ConvBNLayer(num_channels=ch3x3reduced, num_filters=ch3x3, filter_size=3, stride=1, padding=1)) self.branch3 = paddle.nn.Sequential( ConvBNLayer(num_channels=num_channels, num_filters=doublech3x3reduced, filter_size=1, stride=1, padding=0), ConvBNLayer(num_channels=doublech3x3reduced, num_filters=doublech3x3_1, filter_size=3, stride=1, padding=1), ConvBNLayer(num_channels=doublech3x3_1, num_filters=doublech3x3_2, filter_size=3, stride=1, padding=1)) self.branch4 = paddle.nn.Sequential( AvgPool2D(kernel_size=3, stride=1, padding=1), ConvBNLayer(num_channels=num_channels, num_filters=pool_proj, filter_size=1, stride=1, padding=0))
def __init__(self, config): super(TSN_ResNet, self).__init__() self.layers = config.MODEL.num_layers self.seg_num = config.MODEL.seg_num self.class_dim = config.MODEL.num_classes supported_layers = [18, 34, 50, 101, 152] assert self.layers in supported_layers, \ "supported layers are {} but input layer is {}".format( supported_layers, layers) if self.layers == 18: depth = [2, 2, 2, 2] elif self.layers == 34 or self.layers == 50: depth = [3, 4, 6, 3] elif self.layers == 101: depth = [3, 4, 23, 3] elif self.layers == 152: depth = [3, 8, 36, 3] in_channels = [64, 256, 512, 1024 ] if self.layers >= 50 else [64, 64, 128, 256] out_channels = [64, 128, 256, 512] self.conv = ConvBNLayer(in_channels=3, out_channels=64, kernel_size=7, stride=2, act="relu", name="conv1") self.pool2D_max = MaxPool2D(kernel_size=3, stride=2, padding=1) self.block_list = [] if self.layers >= 50: for block in range(len(depth)): shortcut = False for i in range(depth[block]): if self.layers in [101, 152] and block == 2: if i == 0: conv_name = "res" + str(block + 2) + "a" else: conv_name = "res" + str(block + 2) + "b" + str(i) else: conv_name = "res" + str(block + 2) + chr(97 + i) bottleneck_block = self.add_sublayer( conv_name, BottleneckBlock( in_channels=in_channels[block] if i == 0 else out_channels[block] * 4, out_channels=out_channels[block], stride=2 if i == 0 and block != 0 else 1, shortcut=shortcut, name=conv_name)) self.block_list.append(bottleneck_block) shortcut = True else: for block in range(len(depth)): shortcut = False for i in range(depth[block]): conv_name = "res" + str(block + 2) + chr(97 + i) basic_block = self.add_sublayer( conv_name, BasicBlock(in_channels=in_channels[block] if i == 0 else out_channels[block], out_channels=out_channels[block], stride=2 if i == 0 and block != 0 else 1, shortcut=shortcut, name=conv_name)) self.block_list.append(basic_block) shortcut = True self.pool2D_avg = AvgPool2D(kernel_size=7) self.pool2D_avg_channels = in_channels[-1] * 2 self.out = Linear( self.pool2D_avg_channels, self.class_dim, weight_attr=ParamAttr(initializer=paddle.nn.initializer.Normal( mean=0.0, std=0.01), name="fc_0.w_0"), bias_attr=ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.0), name="fc_0.b_0"))