示例#1
0
    def __init__(self, in_channels, out_channels, kernel_size=3, stride=1, act=None):
        super(SeparateConv3DBN, self).__init__()

        padding = kernel_size // 2 if isinstance(kernel_size, int) else \
            (kernel_size[0] // 2, kernel_size[1] // 2, kernel_size[2] // 2)
        self.depthwise = Conv3D(in_channels, in_channels, filter_size=kernel_size,
            stride=stride, padding=padding, groups=in_channels)
        self.pointwise = Conv3D(in_channels, out_channels, filter_size=1,
            stride=1, padding=0, groups=1)
        self.bn = BatchNorm(out_channels, act=act)
示例#2
0
def conv3x3x3(in_planes, out_planes, stride=1):
    return Conv3D(num_channels=in_planes,
                  num_filters=out_planes,
                  filter_size=3,
                  stride=stride,
                  padding=1,
                  bias_attr=False)
示例#3
0
def build_conv3d(attr, channels=None, conv_bias=False):
    out_channels = attr['num_output']
    ks = attr['kernel_size'] if 'kernel_size' in attr else (attr['kernel_d'],
                                                            attr['kernel_h'],
                                                            attr['kernel_w'])
    if ('pad' in attr) or ('pad_d' in attr and 'pad_w' in attr
                           and 'pad_h' in attr):
        padding = attr['pad'] if 'pad' in attr else (attr['pad_d'],
                                                     attr['pad_h'],
                                                     attr['pad_w'])
    else:
        padding = 0
    if ('stride' in attr) or ('stride_d' in attr and 'stride_w' in attr
                              and 'stride_h' in attr):
        stride = attr['stride'] if 'stride' in attr else (attr['stride_d'],
                                                          attr['stride_h'],
                                                          attr['stride_w'])
    else:
        stride = 1

    conv = Conv3D(channels,
                  out_channels,
                  ks,
                  stride,
                  padding,
                  param_attr=fluid.ParamAttr(learning_rate=5.0),
                  bias_attr=fluid.ParamAttr(learning_rate=10.0))

    return conv, out_channels
示例#4
0
 def __init__(self, in_channels, out_channels, spatial_stride=1, temporal_stride=1,
     dilation=1, act=None):
     super(Conv3x3x3BN, self).__init__()
     self.conv = Conv3D(in_channels, out_channels, filter_size=3,
         stride=(temporal_stride, spatial_stride, spatial_stride),
         padding=(1, dilation, dilation), dilation=dilation, bias_attr=False)
     self.bn = BatchNorm(out_channels, act=act)    
    def __init__(self,
                 num_channels,
                 num_filters,
                 filter_size,
                 stride=1,
                 groups=1,
                 act=None,
                 name=None):
        super(ConvBNLayer, self).__init__()

        if (isinstance(filter_size, int)):
            filter_size_3d = [filter_size] * 3
        else:
            filter_size_3d = filter_size
        if (isinstance(stride, int)):
            stride_3d = [stride] * 3
        else:
            stride_3d = stride
        padding = [
            filter_size_3d[0] // 2, filter_size_3d[1] // 2,
            filter_size_3d[2] // 2
        ]

        self._conv = Conv3D(num_channels=num_channels,
                            num_filters=num_filters,
                            filter_size=filter_size_3d,
                            stride=stride_3d,
                            padding=padding,
                            groups=groups,
                            act=None,
                            param_attr=ParamAttr(name=name + "_weights"),
                            bias_attr=False)
        if name == "conv1":
            bn_name = "bn_" + name
        else:
            bn_name = "bn" + name[3:]
        bn_name = bn_name
        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")
示例#6
0
    def __init__(self,
                 block,
                 layers,
                 block_inplanes,
                 n_input_channels=3,
                 conv1_t_size=7,
                 conv1_t_stride=1,
                 no_max_pool=False,
                 shortcut_type='B',
                 widen_factor=1.0,
                 n_classes=400):
        super().__init__()

        block_inplanes = [int(x * widen_factor) for x in block_inplanes]

        self.in_planes = block_inplanes[0]
        self.no_max_pool = no_max_pool

        self.conv1 = Conv3D(num_channels=n_input_channels,
                            num_filters=self.in_planes,
                            filter_size=(conv1_t_size, 7, 7),
                            stride=(conv1_t_stride, 2, 2),
                            padding=(conv1_t_size // 2, 3, 3),
                            bias_attr=False)
        self.bn1 = BatchNorm(self.in_planes, act='relu')

        # self.maxpool = Pool3D(pool_size=3,
        #                       pool_type='max',
        #                       pool_stride=2,
        #                       pool_padding=1)

        self.layer1 = self._make_layer(block, block_inplanes[0], layers[0],
                                       shortcut_type)
        self.layer2 = self._make_layer(block,
                                       block_inplanes[1],
                                       layers[1],
                                       shortcut_type,
                                       stride=2)
        self.layer3 = self._make_layer(block,
                                       block_inplanes[2],
                                       layers[2],
                                       shortcut_type,
                                       stride=2)
        self.layer4 = self._make_layer(block,
                                       block_inplanes[3],
                                       layers[3],
                                       shortcut_type,
                                       stride=2)

        self.fc = Linear(block_inplanes[3] * block.expansion,
                         n_classes,
                         act='softmax')
        self.last_feature_size = block_inplanes[3] * block.expansion

        for m in self.sublayers():
            if isinstance(m, Conv3D):
                m.weight.initializer = fluid.initializer.MSRAInitializer(
                    uniform=True, fan_in=None, seed=0)
                # nn.init.kaiming_normal_(m.weight,
                #                         mode='fan_out',
                #                         nonlinearity='relu')
            elif isinstance(m, BatchNorm):
                m.weight.initializer = fluid.initializer.ConstantInitializer(
                    value=1)
                m.bias.initializer = fluid.initializer.ConstantInitializer(
                    value=0)
示例#7
0
def conv1x1x1(in_planes, out_planes, stride=1):
    return Conv3D(num_channels=in_planes,
                  num_filters=out_planes,
                  filter_size=1,
                  stride=stride,
                  bias_attr=False)
示例#8
0
 def __init__(self, in_channels, out_channels, filter_size=1, stride=1, act=None):
     super(Conv3DBN, self).__init__()
     padding = filter_size // 2 if isinstance(filter_size, int) else (filter_size[0] // 2, filter_size[1] // 2, filter_size[2] // 2)
     self.conv = Conv3D(in_channels, out_channels, filter_size=filter_size,
         stride=stride, padding=padding, bias_attr=False)
     self.bn = BatchNorm(out_channels, act)
示例#9
0
 def __init__(self, in_channels, out_channels, spatial_stride=1, temporal_stride=1, dilation=1):
     super(Conv3x1x1, self).__init__()
     self.conv = Conv3D(in_channels, out_channels, filter_size=(3, 1, 1),
         stride=(temporal_stride, spatial_stride, spatial_stride),
         padding=(1, 0, 0), dilation=dilation, bias_attr=False)