def __init__(self, inplanes, planes, loss_weight=0.5):
        super(AuxHead, self).__init__()

        self.convs = Conv3DBN(inplanes,
                              inplanes * 2, (1, 3, 3), (1, 2, 2),
                              act='relu')
        self.loss_weight = loss_weight
        self.dropout = fluid.dygraph.Dropout(p=0.5)
        self.fc = Linear(inplanes * 2, planes)
Exemple #2
0
    def __init__(self,
                 depth,
                 num_stages=4,
                 spatial_strides=(1, 2, 2, 2),
                 temporal_strides=(1, 1, 1, 1),
                 dilations=(1, 1, 1, 1),
                 out_indices=(0, 1, 2, 3),
                 conv1_kernel_t=1,
                 conv1_stride_t=1,
                 pool1_kernel_t=1,
                 pool1_stride_t=1,
                 inflate_freqs=(1, 1, 1, 1),
                 inflate_style='3x1x1'):
        super(ResNet3D, self).__init__()

        self.depth = depth
        self.num_stages = num_stages
        self.spatial_strides = spatial_strides
        self.temporal_strides = temporal_strides
        self.dilations = dilations
        self.out_indices = out_indices
        self.inflate_freqs = inflate_freqs if not isinstance(
            inflate_freqs, int) else (inflate_freqs) * num_stages
        self.inflate_style = inflate_style

        self.block, stage_blocks = self.arch_settings[depth]
        self.stage_blocks = stage_blocks[:num_stages]
        self.inplanes = 64
        self.conv1 = Conv3DBN(3,
                              64,
                              filter_size=(conv1_kernel_t, 7, 7),
                              stride=(conv1_stride_t, 2, 2),
                              act='relu')
        self.pool1 = MaxPool3D((pool1_kernel_t, 3, 3),
                               stride=(pool1_stride_t, 2, 2))

        self.res_layers = []
        for i, num_blocks in enumerate(self.stage_blocks):
            spatial_stride = spatial_strides[i]
            temporal_stride = temporal_strides[i]
            dilation = dilations[i]
            planes = 64 * 2**i

            res_layer = self.make_res_layer(i, self.block, self.inplanes,
                                            planes, num_blocks, spatial_stride,
                                            temporal_stride, dilation,
                                            inflate_freqs[i],
                                            self.inflate_style)

            self.inplanes = planes * self.block.expansion
            self.res_layers.append(res_layer)

        self.feature_dim = self.block.expansion * 64 * 2**(
            len(self.stage_blocks) - 1)
    def __init__(self,
                 inplanes,
                 planes,
                 kernel_size=(3, 1, 1),
                 stride=(1, 1, 1),
                 downsample_position='after',
                 downsample_scale=(1, 2, 2)):
        super(Downsampling, self).__init__()

        self.conv = Conv3DBN(inplanes, planes, kernel_size, stride, act='relu')
        self.pool = MaxPool3D(downsample_scale, downsample_scale)
        self.downsample_position = downsample_position
Exemple #4
0
    def __init__(self, in_channels=[256, 512, 1024, 2048], out_channels=256,
        spatial_modulation_config=None, temporal_modulation_config=None, upsampling_config=None,
        downsampling_config=None, level_fusion_config=None, aux_head_config=None):
        super(TPN, self).__init__()
        assert isinstance(in_channels, list)
        assert isinstance(out_channels, int)

        self.in_channels = in_channels
        self.out_channels = out_channels
        self.num_ins = len(in_channels)

        self.temporal_modulation_ops = []
        self.upsampling_ops = []
        self.downsampling_ops = []
        self.level_fusion_op = []
        self.spatial_modulation = SpatialModulation(**spatial_modulation_config)

        for i in range(0, self.num_ins, 1):
            inplanes = in_channels[-1]
            planes = out_channels

            temporal_modulation_config.param.inplanes = inplanes
            temporal_modulation_config.param.planes = planes
            temporal_modulation_config.param.downsample_scale = temporal_modulation_config.scales[i]
            temporal_modulation = self.add_sublayer('temporal_down_%d' % i, 
                TemporalModulation(**temporal_modulation_config.param))
            self.temporal_modulation_ops.append(temporal_modulation)

            if i < self.num_ins - 1:
                upsampling = self.add_sublayer('upsampling_%d' % i, Upsampling(**upsampling_config))
                self.upsampling_ops.append(upsampling)

                downsampling_config.param.inplanes = planes
                downsampling_config.param.planes = planes
                downsampling_config.param.downsample_scale = downsampling_config.scales
                downsampling = self.add_sublayer('downsampling_%d' % i, 
                    Downsampling(**downsampling_config.param))
                self.downsampling_ops.append(downsampling)
        
        self.level_fusion_op = LevelFusion(**level_fusion_config)
        self.level_fusion_ops = LevelFusion(**level_fusion_config)

        out_dims = level_fusion_config.out_channels
        self.pyramid_fusion_op = Conv3DBN(out_dims * 2, 2048, act='relu')
        
        aux_head_config.inplanes = in_channels[-2]
        self.aux_head = AuxHead(**aux_head_config)
    def __init__(self,
                 in_channels=[1024, 1024],
                 mid_channels=[1024, 1024],
                 out_channels=2048,
                 ds_scales=[(1, 1, 1), (1, 1, 1)]):
        super(LevelFusion, self).__init__()

        self.ops = []
        num_ins = len(in_channels)
        for i in range(num_ins):
            op = self.add_sublayer(
                'downsampling_%d' % i,
                Downsampling(in_channels[i],
                             mid_channels[i],
                             kernel_size=(1, 1, 1),
                             stride=(1, 1, 1),
                             downsample_position='before',
                             downsample_scale=ds_scales[i]))
            self.ops.append(op)
        in_dims = np.sum(mid_channels)
        self.fusion_conv = Conv3DBN(in_dims, out_channels, act='relu')
Exemple #6
0
    def __init__(self,
                 in_channels,
                 out_channels,
                 kernel_size,
                 stride=1,
                 reps=3):
        super(XceptionBlock, self).__init__()

        self.skip = None
        if out_channels != in_channels or stride != 1:
            self.skip = Conv3DBN(in_channels,
                                 out_channels,
                                 kernel_size,
                                 stride=stride)

        self.stride = stride
        rep = []
        channels = out_channels
        rep.append(('conv_0',
                    SeparateConv3DBN(in_channels,
                                     channels,
                                     kernel_size,
                                     1,
                                     act='relu')))

        for i in range(1, reps + 1):
            rep.append(('conv_%d' % i,
                        SeparateConv3DBN(channels,
                                         channels,
                                         kernel_size,
                                         1,
                                         act='relu')))

        self.rep = fluid.dygraph.Sequential(*rep)
        self.pool_stride = stride
        self.pool_size = kernel_size
        self.pool_padding = kernel_size // 2 if isinstance(kernel_size, int) else \
            (kernel_size[0] // 2, kernel_size[1] // 2, kernel_size[2] // 2)