Exemple #1
0
def preact_residual_dmixconv_block(data, channels, channels_operating, name, kernels=None, act_type='relu', use_se=True):
    """
    Returns a residual block without any max pooling operation
    :param data: Input data
    :param channels: Number of filters for all CNN-layers
    :param name: Name for the residual block
    :param act_type: Activation function to use
    :return: symbol
    """
    bn1 = mx.sym.BatchNorm(data=data, name=name + '_bn1')
    conv1 = mx.sym.Convolution(data=bn1, num_filter=channels_operating, kernel=(1, 1), pad=(0, 0), no_bias=True,
                               name=name + '_conv1')
    bn2 = mx.sym.BatchNorm(data=conv1, name=name + '_bn2')
    act1 = get_act(data=bn2, act_type=act_type, name=name + '_act1')
    conv2 = mix_conv(data=act1, channels=channels_operating, kernels=kernels, name=name + 'conv2')
    bn3 = mx.sym.BatchNorm(data=conv2, name=name + '_bn3')
    act2 = get_act(data=bn3, act_type=act_type, name=name + '_act2')
    out = mx.sym.Convolution(data=act2, num_filter=channels, kernel=(1, 1),
                               pad=(0, 0), no_bias=True, name=name + '_conv3')
    # out = mx.sym.BatchNorm(data=conv3, name=name + '_bn4')
    if use_se:
        out = channel_squeeze_excitation(out, channels, name=name + '_se', ratio=4, act_type=act_type,
                                         use_hard_sigmoid=True)
    out_sum = mx.sym.broadcast_add(data, out, name=name + '_add')

    return out_sum
def residual_block(data, channels, name, kernel=3, act_type='relu', use_se=False):
    """
    Returns a residual block without any max pooling operation
    :param data: Input data
    :param channels: Number of filters for all CNN-layers
    :param name: Name for the residual block
    :param act_type: Activation function to use
    :param use_se: If true, a squeeze excitation will be used
    :return:
    """

    if use_se:
        se = channel_squeeze_excitation(data, channels, name=name + '_se', ratio=2, act_type=act_type)
        conv1 = mx.sym.Convolution(data=se, num_filter=channels, kernel=(kernel, kernel),
                                   pad=(kernel // 2, kernel // 2), num_group=1,
                                   no_bias=True, name=name + '_conv1')
    else:
        conv1 = mx.sym.Convolution(data=data, num_filter=channels, kernel=(kernel, kernel),
                                   pad=(kernel // 2, kernel // 2), num_group=1,
                                   no_bias=True, name=name + '_conv1')
    act1 = get_act(data=conv1, act_type=act_type, name=name + '_act1')
    bn1 = mx.sym.BatchNorm(data=act1, name=name + '_bn1')

    # kernel = 3
    conv2 = mx.sym.Convolution(data=bn1, num_filter=channels, kernel=(kernel, kernel), stride=(1, 1),
                               num_group=1, pad=(kernel // 2, kernel // 2), no_bias=True,
                               name=name + '_conv2')
    bn2 = mx.sym.BatchNorm(data=conv2, name=name + '_bn2')

    sum = mx.sym.broadcast_add(data, bn2, name=name + '_add')

    return sum
def bottleneck_residual_block(data, channels, channels_operating, name, kernel=3, act_type='relu', use_se=False,
                              data_variant=None):
    """
    Returns a residual block without any max pooling operation
    :param data: Input data
    :param channels: Number of filters for all CNN-layers
    :param channels_operating: Number of filters used for 3x3, 5x5, 7x7,.. convolution
    :param name: Name for the residual block
    :param act_type: Activation function to use
    :param use_se: If true, a squeeze excitation will be used
    :param data_variant: Data input which holds the current active variant information
    :return:
    """

    if data_variant is not None:
        first_input = mx.sym.Concat(*[data, data_variant], name=name + '_concat')
        add_channels = NB_CHANNELS_VARIANTS
    else:
        first_input = data
        add_channels = 0

    if use_se:
        se = channel_squeeze_excitation(first_input, channels+add_channels, name=name + '_se', ratio=2)
        conv1 = mx.sym.Convolution(data=se, num_filter=channels_operating, kernel=(1, 1), pad=(0, 0),
                                   no_bias=True, name=name + '_conv1')
    else:
        conv1 = mx.sym.Convolution(data=first_input, num_filter=channels_operating, kernel=(1, 1), pad=(0, 0),
                                   no_bias=True, name=name + '_conv1')
    bn1 = mx.sym.BatchNorm(data=conv1, name=name + '_bn1')
    act1 = get_act(data=bn1, act_type=act_type, name=name + '_act1')
    conv2 = mx.sym.Convolution(data=act1, num_filter=channels_operating, kernel=(kernel, kernel), stride=(1, 1),
                               num_group=channels_operating, pad=(kernel // 2, kernel // 2), no_bias=True,
                               name=name + '_conv2')
    bn2 = mx.sym.BatchNorm(data=conv2, name=name + '_bn2')
    act2 = get_act(data=bn2, act_type=act_type, name=name + '_act2')
    conv3 = mx.sym.Convolution(data=act2, num_filter=channels, kernel=(1, 1), pad=(0, 0),
                               no_bias=True, name=name + '_conv3')
    bn3 = mx.sym.BatchNorm(data=conv3, name=name + '_bn3')
    sum = mx.sym.broadcast_add(bn3, data, name=name+'_add')

    return sum
Exemple #4
0
def preact_residual_dmixconv_block(data,
                                   channels,
                                   channels_operating,
                                   name,
                                   kernels=None,
                                   act_type='relu',
                                   se_ratio=4,
                                   se_type="se"):
    """
    Returns a residual block without any max pooling operation
    :param data: Input data
    :param channels: Number of filters for all CNN-layers
    :param name: Name for the residual block
    :param act_type: Activation function to use
    :param se_ratio: Squeeze excitation ratio
    :param use_se: Boolean if a squeeze excitation module will be used
    :param se_type: Squeeze excitation module type. Available [None, "se", "cbam", "ca_se", "cm_se", "sa_se", "sm_se"]
    :return: symbol
    """
    bn1 = mx.sym.BatchNorm(data=data, name=name + '_bn1')
    conv1 = mx.sym.Convolution(data=bn1,
                               num_filter=channels_operating,
                               kernel=(1, 1),
                               pad=(0, 0),
                               no_bias=True,
                               name=name + '_conv1')
    bn2 = mx.sym.BatchNorm(data=conv1, name=name + '_bn2')
    act1 = get_act(data=bn2, act_type=act_type, name=name + '_act1')
    conv2 = mix_conv(data=act1,
                     channels=channels_operating,
                     kernels=kernels,
                     name=name + 'conv2')
    bn3 = mx.sym.BatchNorm(data=conv2, name=name + '_bn3')
    out = get_act(data=bn3, act_type=act_type, name=name + '_act2')
    out = mx.sym.Convolution(data=out,
                             num_filter=channels,
                             kernel=(1, 1),
                             pad=(0, 0),
                             no_bias=True,
                             name=name + '_conv3')
    if se_type is not None:
        if se_type == "se":
            out = channel_squeeze_excitation(out,
                                             channels,
                                             name=name + '_se',
                                             ratio=se_ratio,
                                             act_type=act_type,
                                             use_hard_sigmoid=True)
        elif se_type == "cbam":
            out = convolution_block_attention_module(out,
                                                     channels,
                                                     name=name + '_se',
                                                     ratio=se_ratio,
                                                     act_type=act_type,
                                                     use_hard_sigmoid=True)
        elif se_type == "ca_se":
            out = ca_se(out,
                        channels,
                        name=name + '_ca_se',
                        ratio=se_ratio,
                        act_type=act_type,
                        use_hard_sigmoid=True)
        elif se_type == "cm_se":
            out = cm_se(out,
                        channels,
                        name=name + '_cm_se',
                        ratio=se_ratio,
                        act_type=act_type,
                        use_hard_sigmoid=True)
        elif se_type == "sa_se":
            out = sa_se(out, name=name + 'sa_se', use_hard_sigmoid=True)
        elif se_type == "sm_se":
            out = sm_se(out, name=name + 'sm_se', use_hard_sigmoid=True)
        else:
            raise Exception(f'Unsupported se_type "{se_type}"')
    out_sum = mx.sym.broadcast_add(data, out, name=name + '_add')

    return out_sum