Exemplo n.º 1
0
def get_symbol(fixed_param_names):
    num_classes = config.emb_size
    print('in_network', config)
    fc_type = config.net_output
    data = mx.symbol.Variable(name="data")
    if config.fp_16:
        data = mx.sym.Cast(data=data, dtype=np.float16)

    data = data-127.5
    data = data*0.0078125
    blocks = config.net_blocks

    # do stn part
    if config.add_stn:
        ## mx.sym中写好了STN层包括grid generator和sampler,只需要送入相应参数θ
        data = mx.sym.SpatialTransformer(data=data, loc=symbol_utils.get_loc(data), target_shape=(112, 112),
                                      transform_type="affine", sampler_type="bilinear")

    conv_1 = Conv(data, num_filter=64, kernel=(3, 3), pad=(1, 1), stride=(2, 2), name="conv_1")
    if blocks[0]==1:
      conv_2_dw = Conv(conv_1, num_group=64, num_filter=64, kernel=(3, 3), pad=(1, 1), stride=(1, 1), name="conv_2_dw")
    else:
      conv_2_dw = Residual(conv_1, fixed_param_names, num_block=blocks[0], num_out=64, kernel=(3, 3), stride=(1, 1), pad=(1, 1), num_group=64, name="res_2")
    conv_23 = DResidual(conv_2_dw, fixed_param_names, num_out=64, kernel=(3, 3), stride=(2, 2), pad=(1, 1), num_group=128, name="dconv_23")
    conv_3 = Residual(conv_23, fixed_param_names, num_block=blocks[1], num_out=64, kernel=(3, 3), stride=(1, 1), pad=(1, 1), num_group=128, name="res_3")
    conv_34 = DResidual(conv_3, fixed_param_names, num_out=128, kernel=(3, 3), stride=(2, 2), pad=(1, 1), num_group=256, name="dconv_34")
    conv_4 = Residual(conv_34, fixed_param_names, num_block=blocks[2], num_out=128, kernel=(3, 3), stride=(1, 1), pad=(1, 1), num_group=256, name="res_4")
    conv_45 = DResidual(conv_4, fixed_param_names, num_out=128, kernel=(3, 3), stride=(2, 2), pad=(1, 1), num_group=512, name="dconv_45")
    conv_5 = Residual(conv_45, fixed_param_names, num_block=blocks[3], num_out=128, kernel=(3, 3), stride=(1, 1), pad=(1, 1), num_group=256, name="res_5")
    conv_6_sep = Conv(conv_5, num_filter=512, kernel=(1, 1), pad=(0, 0), stride=(1, 1), name="conv_6sep")

    fc1 = symbol_utils.get_fc1(conv_6_sep, num_classes, fc_type)
    return fc1
Exemplo n.º 2
0
def get_symbol():
    num_classes = config.embedding_size
    print('in_network', config)
    fc_type = config.net_output
    data = mx.symbol.Variable(name="data")
    data = data - 127.5
    data = data * 0.0078125
    if config.fp16:
        data = mx.sym.Cast(data=data, dtype=np.float16)
    #blocks = config.net_blocks
    blocks = [1, 4, 6, 2, 2]
    channels = [32, 32, 32, 64, 64, 128]
    conv_1 = Conv(data,
                  num_filter=channels[0],
                  kernel=(3, 3),
                  pad=(1, 1),
                  stride=(2, 2),
                  name="conv_1")  # 56
    if blocks[0] == 1:
        conv_2_dw = Conv(conv_1,
                         num_group=channels[1],
                         num_filter=channels[1],
                         kernel=(3, 3),
                         pad=(1, 1),
                         stride=(1, 1),
                         name="conv_2_dw")
    else:
        conv_2_dw = Residual(conv_1,
                             num_block=blocks[0],
                             num_out=channels[1],
                             kernel=(3, 3),
                             stride=(1, 1),
                             pad=(1, 1),
                             num_group=channels[1],
                             name="res_2")
    conv_23 = DResidual(conv_2_dw,
                        dim_match=0,
                        num_in=channels[1],
                        num_out=channels[2],
                        kernel=(3, 3),
                        stride=(2, 2),
                        pad=(1, 1),
                        num_group=channels[2] * 2,
                        name="dconv_23")  # 28
    slice_index = [[0, 16], [12, 28]]
    layers = []
    for i in range(4):
        x = i // 2
        y = i % 2
        data = mx.sym.slice(conv_23,
                            begin=(None, None, slice_index[x][0],
                                   slice_index[y][0]),
                            end=(None, None, slice_index[x][1],
                                 slice_index[y][1]))
        conv_3 = Residual(data,
                          num_block=blocks[1],
                          num_out=channels[2],
                          kernel=(3, 3),
                          stride=(1, 1),
                          pad=(1, 1),
                          num_group=channels[2] * 2,
                          name="res_3",
                          suffix=i)
        conv_34 = DResidual(conv_3,
                            dim_match=0,
                            num_in=channels[2],
                            num_out=channels[3],
                            kernel=(3, 3),
                            stride=(2, 2),
                            pad=(1, 1),
                            num_group=channels[3] * 2,
                            name="dconv_34",
                            suffix=i)  # 14
        conv_4 = Residual(conv_34,
                          num_block=blocks[2],
                          num_out=channels[3],
                          kernel=(3, 3),
                          stride=(1, 1),
                          pad=(1, 1),
                          num_group=channels[3] * 2,
                          name="res_4",
                          suffix=i)
        conv_45 = DResidual(conv_4,
                            dim_match=0,
                            num_in=channels[3],
                            num_out=channels[4],
                            kernel=(3, 3),
                            stride=(2, 2),
                            pad=(1, 1),
                            num_group=channels[4] * 4,
                            name="dconv_45",
                            suffix=i)  # 7
        conv_5 = Residual(conv_45,
                          num_block=blocks[3],
                          num_out=channels[4],
                          kernel=(3, 3),
                          stride=(1, 1),
                          pad=(1, 1),
                          num_group=channels[4] * 2,
                          name="res_5",
                          suffix=i)
        layers.append(conv_5)
    s1, s2, s3, s4 = layers
    s1 = mx.sym.concat(s1, s2, dim=3)
    s2 = mx.sym.concat(s3, s4, dim=3)
    conv_5 = mx.sym.concat(s1, s2, dim=2)

    conv_56 = DResidual(conv_5,
                        dim_match=0,
                        num_in=channels[4],
                        num_out=channels[5],
                        kernel=(3, 3),
                        stride=(2, 2),
                        pad=(1, 1),
                        num_group=channels[4] * 4,
                        name="dconv_45")  # 4

    conv_6 = FcResidual(conv_56,
                        num_block=blocks[4],
                        num_out=channels[5],
                        H=4,
                        W=4,
                        name="fc_conv")
    fc1 = symbol_utils.get_fc1(conv_6, num_classes, fc_type)
    fc1 = mx.sym.Cast(data=fc1, dtype=np.float32)
    return fc1
Exemplo n.º 3
0
def get_symbol(fixed_param_names):
    num_classes = config.emb_size
    num_layers = config.num_layers

    # block numbers
    if num_layers == 50:
        units = [3, 4, 6, 3]
    elif num_layers == 101:
        units = [3, 4, 23, 3]
    elif num_layers == 200:
        units = [3, 24, 36, 3]
    elif num_layers == 269:
        units = [3, 30, 48, 8]
    else:
        raise ValueError(
            "no experiments done on num_layers {}, you can do it yourself".
            format(num_layers))

    # bottleneck configuration  2s4x64d : s(radix), x(cardinality), d(bottleneck width)
    radix, tmpStr = config.bottleneckStr.split('s')
    cardinality, tmpStr = tmpStr.split('x')
    bottleneckWidth = int(tmpStr.split('d')[0])
    radix = int(radix)
    cardinality = int(cardinality)

    norm_kwargs = {}
    norm_kwargs['momentum'] = config.bn_mom
    norm_kwargs['epsilon'] = 2e-5

    net = ResNet(Bottleneck,
                 units,
                 radix=radix,
                 cardinality=cardinality,
                 bottleneck_width=bottleneckWidth,
                 deep_stem=config.deepStem,
                 avg_down=config.avgDown,
                 stem_width=config.stemWidth,
                 avd=config.avd,
                 avd_first=config.avdFirst,
                 use_splat=config.useSplat,
                 dropblock_prob=config.dropblockProb,
                 final_drop=config.finalDrop,
                 input_size=config.image_shape[0],
                 norm_kwargs=norm_kwargs,
                 last_gamma=True,
                 name_prefix=('resnest_%d' % num_layers))

    # remove weight decay on bias, and beta/gamma for batchnorm layers
    if config.no_wd:
        for k, v in net.collect_params('.*beta|.*gamma|.*bias').items():
            v.wd_mult = 0.0

    data = mx.sym.Variable(name='data')
    if config.fp_16:
        data = mx.sym.Cast(data=data, dtype=np.float16)
        net.cast(np.float16)
    data = data - 127.5
    data = data * 0.0078125
    body = net(data)

    fc1 = symbol_utils.get_fc1(body, config.emb_size, config.net_output)

    return fc1
Exemplo n.º 4
0
def resnet(units, num_stages, filter_list, num_classes, bottle_neck):
    bn_mom = config.bn_mom
    workspace = config.workspace
    kwargs = {
        'version_se': config.net_se,
        'version_input': config.net_input,
        'version_output': config.net_output,
        'version_unit': config.net_unit,
        'version_act': config.net_act,
        'bn_mom': bn_mom,
        'workspace': workspace,
        'memonger': config.memonger,
    }
    """Return ResNet symbol of
    Parameters
    ----------
    units : list
        Number of units in each stage
    num_stages : int
        Number of stage
    filter_list : list
        Channel size of each stage
    num_classes : int
        Ouput size of symbol
    dataset : str
        Dataset type, only cifar10 and imagenet supports
    workspace : int
        Workspace used in convolution operator
    """
    version_se = kwargs.get('version_se', 1)
    version_input = kwargs.get('version_input', 1)
    assert version_input >= 0
    version_output = kwargs.get('version_output', 'E')
    fc_type = version_output
    version_unit = kwargs.get('version_unit', 3)
    act_type = kwargs.get('version_act', 'prelu')
    memonger = kwargs.get('memonger', False)
    print(version_se, version_input, version_output, version_unit, act_type,
          memonger)
    num_unit = len(units)
    assert (num_unit == num_stages)
    data = mx.sym.Variable(name='data')

    if config.fp16:
        data = mx.sym.Cast(data=data, dtype=np.float16)

    if version_input == 0:
        # data = mx.sym.BatchNorm(data=data, fix_gamma=True, eps=2e-5, momentum=bn_mom, name='bn_data')
        data = mx.sym.identity(data=data, name='id')
        data = data - 127.5
        data = data * 0.0078125
        body = Conv(data=data,
                    num_filter=filter_list[0],
                    kernel=(7, 7),
                    stride=(2, 2),
                    pad=(3, 3),
                    no_bias=True,
                    name="conv0",
                    workspace=workspace)
        body = mx.sym.BatchNorm(data=body,
                                fix_gamma=False,
                                eps=2e-5,
                                momentum=bn_mom,
                                name='bn0')
        body = Act(data=body, act_type=act_type, name='relu0')
        # body = mx.sym.Pooling(data=body, kernel=(3, 3), stride=(2,2), pad=(1,1), pool_type='max')
    elif version_input == 2:
        data = mx.sym.BatchNorm(data=data,
                                fix_gamma=True,
                                eps=2e-5,
                                momentum=bn_mom,
                                name='bn_data')
        body = Conv(data=data,
                    num_filter=filter_list[0],
                    kernel=(3, 3),
                    stride=(1, 1),
                    pad=(1, 1),
                    no_bias=True,
                    name="conv0",
                    workspace=workspace)
        body = mx.sym.BatchNorm(data=body,
                                fix_gamma=False,
                                eps=2e-5,
                                momentum=bn_mom,
                                name='bn0')
        body = Act(data=body, act_type=act_type, name='relu0')
    else:
        data = mx.sym.identity(data=data, name='id')
        data = data - 127.5
        data = data * 0.0078125
        body = data
        body = Conv(data=body,
                    num_filter=filter_list[0],
                    kernel=(3, 3),
                    stride=(1, 1),
                    pad=(1, 1),
                    no_bias=True,
                    name="conv0",
                    workspace=workspace)
        body = mx.sym.BatchNorm(data=body,
                                fix_gamma=False,
                                eps=2e-5,
                                momentum=bn_mom,
                                name='bn0')
        body = Act(data=body, act_type=act_type, name='relu0')

    for i in range(num_stages):
        # if version_input==0:
        #  body = residual_unit(body, filter_list[i+1], (1 if i==0 else 2, 1 if i==0 else 2), False,
        #                       name='stage%d_unit%d' % (i + 1, 1), bottle_neck=bottle_neck, **kwargs)
        # else:
        #  body = residual_unit(body, filter_list[i+1], (2, 2), False,
        #    name='stage%d_unit%d' % (i + 1, 1), bottle_neck=bottle_neck, **kwargs)
        body = residual_unit(body,
                             filter_list[i + 1], (2, 2),
                             False,
                             name='stage%d_unit%d' % (i + 1, 1),
                             bottle_neck=bottle_neck,
                             **kwargs)
        for j in range(units[i] - 1):
            body = residual_unit(body,
                                 filter_list[i + 1], (1, 1),
                                 True,
                                 name='stage%d_unit%d' % (i + 1, j + 2),
                                 bottle_neck=bottle_neck,
                                 **kwargs)
    if config.fp16:
        body = mx.sym.Cast(data=body, dtype=np.float32)

    if bottle_neck:
        body = Conv(data=body,
                    num_filter=512,
                    kernel=(1, 1),
                    stride=(1, 1),
                    pad=(0, 0),
                    no_bias=True,
                    name="convd",
                    workspace=workspace)
        body = mx.sym.BatchNorm(data=body,
                                fix_gamma=False,
                                eps=2e-5,
                                momentum=bn_mom,
                                name='bnd')
        body = Act(data=body, act_type=act_type, name='relud')

    fc1 = symbol_utils.get_fc1(body, num_classes, fc_type)
    return fc1
def resnet(units, num_stages, filter_list, num_classes, bottle_neck, fixed_param_names):
    input_shape = (default.per_batch_size, config.image_shape[-1], config.image_shape[0], config.image_shape[1])
    bn_mom = config.bn_mom
    workspace = config.workspace
    kwargs = {'version_se' : config.net_se,
        'version_input': config.net_input,
        'version_output': config.net_output,
        'version_unit': config.net_unit,
        'version_act': config.net_act,
        'bn_mom': bn_mom,
        'workspace': workspace
        }
    """Return ResNet symbol of
    Parameters
    ----------
    units : list
        Number of units in each stage
    num_stages : int
        Number of stage
    filter_list : list
        Channel size of each stage
    num_classes : int
        Ouput size of symbol
    dataset : str
        Dataset type, only cifar10 and imagenet supports
    workspace : int
        Workspace used in convolution operator
    """
    version_se = kwargs.get('version_se', 1)
    version_input = kwargs.get('version_input', 1)
    assert version_input>=0
    version_output = kwargs.get('version_output', 'E')
    fc_type = version_output
    version_unit = kwargs.get('version_unit', 3)
    act_type = kwargs.get('version_act', 'prelu')
    print(version_se, version_input, version_output, version_unit, act_type)
    num_unit = len(units)
    assert(num_unit == num_stages)
    data = mx.sym.Variable(name='data', shape=input_shape)
    if config.fp_16:
        data = mx.sym.Cast(data=data, dtype=np.float16)
    if version_input==0:
        data = mx.sym.identity(data=data, name='id')
        data = data-127.5
        data = data*0.0078125
        body = Conv(data=data, num_filter=filter_list[0], kernel=(7, 7), stride=(2,2), pad=(3, 3),
                                  no_bias=True, name="conv0", workspace=workspace)
        body = mx.sym.BatchNorm(data=body, fix_gamma=False, eps=2e-5, momentum=bn_mom, name='bn0')
        body = Act(data=body, act_type=act_type, name='relu0')
    elif version_input==2:
        data = mx.sym.BatchNorm(data=data, fix_gamma=True, eps=2e-5, momentum=bn_mom, name='bn_data')
        body = Conv(data=data, num_filter=filter_list[0], kernel=(3,3), stride=(1,1), pad=(1,1),
                                  no_bias=True, name="conv0", workspace=workspace)
        body = mx.sym.BatchNorm(data=body, fix_gamma=False, eps=2e-5, momentum=bn_mom, name='bn0')
        body = Act(data=body, act_type=act_type, name='relu0')
    else:
        data = mx.sym.identity(data=data, name='id')
        data = data-127.5
        data = data*0.0078125
        body = data
        body = Conv(data=body, num_filter=filter_list[0], kernel=(3,3), stride=(1,1), pad=(1, 1),
                                  no_bias=True, name="conv0", workspace=workspace)
        body = mx.sym.BatchNorm(data=body, fix_gamma=False, eps=2e-5, momentum=bn_mom, name='bn0')
        body = Act(data=body, act_type=act_type, name='relu0')
        
    for i in range(num_stages):      
        body = residual_unit(body, filter_list[i+1], (2, 2), False,
            name='stage%d_unit%d' % (i + 1, 1), bottle_neck=bottle_neck, fixed_param_names=fixed_param_names, **kwargs)
        
        for j in range(units[i]-1):
            body = residual_unit(body, filter_list[i+1], (1,1), True, name='stage%d_unit%d' % (i+1, j+2),
                bottle_neck=bottle_neck, fixed_param_names=fixed_param_names, **kwargs)
            
    if bottle_neck:
        body = Conv(data=body, num_filter=512, kernel=(1,1), stride=(1,1), pad=(0,0),
                                no_bias=True, name="convd", workspace=workspace)      
        body = mx.sym.BatchNorm(data=body, fix_gamma=False, eps=2e-5, momentum=bn_mom, name='bnd')
        body = Act(data=body, act_type=act_type, name='relud')# modified: move act before bn because of prelu                          
    if (fc_type == 'ECCV'):
      fc1, class_branch,body = symbol_utils.get_fc1(body, num_classes, fc_type)
      return fc1, class_branch, body
    else:
      fc1 = symbol_utils.get_fc1(body, num_classes, fc_type)
          
    return fc1