Esempio n. 1
0
def get_symbol(num_classes=1000, num_layers=92, **kwargs):
    if num_layers == 68:
        k_R = 128
        G = 32
        k_sec = {2: 3, \
                 3: 4, \
                 4: 12, \
                 5: 3}
        inc_sec = {2: 16, \
                   3: 32, \
                   4: 32, \
                   5: 64}
    elif num_layers == 92:
        k_R = 96
        G = 32
        k_sec = {2: 3, \
                 3: 4, \
                 4: 20, \
                 5: 3}
        inc_sec = {2: 16, \
                   3: 32, \
                   4: 24, \
                   5: 128}
    elif num_layers == 107:
        k_R = 200
        G = 50
        k_sec = {2: 4, \
                 3: 8, \
                 4: 20, \
                 5: 3}
        inc_sec = {2: 20, \
                   3: 64, \
                   4: 64, \
                   5: 128}
    elif num_layers == 131:
        k_R = 160
        G = 40
        k_sec = {2: 4, \
                 3: 8, \
                 4: 28, \
                 5: 3}
        inc_sec = {2: 16, \
                   3: 32, \
                   4: 32, \
                   5: 128}
    else:
        raise ValueError(
            "no experiments done on dpn num_layers {}, you can do it yourself".
            format(num_layers))

    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)
    print(version_se, version_input, version_output, version_unit)

    ## define Dual Path Network
    data = mx.symbol.Variable(name="data")
    # data = data-127.5
    # data = data*0.0078125
    # if version_input==0:
    #  conv1_x_1  = Conv(data=data,  num_filter=128,  kernel=(7, 7), name='conv1_x_1', pad=(3,3), stride=(2,2))
    # else:
    #  conv1_x_1  = Conv(data=data,  num_filter=128,  kernel=(3, 3), name='conv1_x_1', pad=(3,3), stride=(1,1))
    # conv1_x_1  = BN_AC(conv1_x_1, name='conv1_x_1__relu-sp')
    # conv1_x_x  = mx.symbol.Pooling(data=conv1_x_1, pool_type="max", kernel=(3, 3),  pad=(1,1), stride=(2,2), name="pool1")
    conv1_x_x = symbol_utils.get_head(data, version_input, 128)

    # conv2
    bw = 256
    inc = inc_sec[2]
    R = (k_R * bw) / 256
    conv2_x_x = DualPathFactory(conv1_x_x, R, R, bw, 'conv2_x__1', inc, G,
                                'proj')
    for i_ly in range(2, k_sec[2] + 1):
        conv2_x_x = DualPathFactory(conv2_x_x, R, R, bw,
                                    ('conv2_x__%d' % i_ly), inc, G, 'normal')

    # conv3
    bw = 512
    inc = inc_sec[3]
    R = (k_R * bw) / 256
    conv3_x_x = DualPathFactory(conv2_x_x, R, R, bw, 'conv3_x__1', inc, G,
                                'down')
    for i_ly in range(2, k_sec[3] + 1):
        conv3_x_x = DualPathFactory(conv3_x_x, R, R, bw,
                                    ('conv3_x__%d' % i_ly), inc, G, 'normal')

    # conv4
    bw = 1024
    inc = inc_sec[4]
    R = (k_R * bw) / 256
    conv4_x_x = DualPathFactory(conv3_x_x, R, R, bw, 'conv4_x__1', inc, G,
                                'down')
    for i_ly in range(2, k_sec[4] + 1):
        conv4_x_x = DualPathFactory(conv4_x_x, R, R, bw,
                                    ('conv4_x__%d' % i_ly), inc, G, 'normal')

    # conv5
    bw = 2048
    inc = inc_sec[5]
    R = (k_R * bw) / 256
    conv5_x_x = DualPathFactory(conv4_x_x, R, R, bw, 'conv5_x__1', inc, G,
                                'down')
    for i_ly in range(2, k_sec[5] + 1):
        conv5_x_x = DualPathFactory(conv5_x_x, R, R, bw,
                                    ('conv5_x__%d' % i_ly), inc, G, 'normal')

    # output: concat
    conv5_x_x = mx.symbol.Concat(*[conv5_x_x[0], conv5_x_x[1]],
                                 name='conv5_x_x_cat-final')
    # conv5_x_x = BN_AC(conv5_x_x, name='conv5_x_x__relu-sp')
    before_pool = conv5_x_x
    fc1 = symbol_utils.get_fc1(before_pool, num_classes, fc_type)
    return fc1
Esempio n. 2
0
def get_symbol(num_classes, num_layers, **kwargs):
    """Return DenseNet symbol of imagenet
    Parameters
    ----------
    units : list
        Number of units in each stage
    num_stage : int
        Number of stage
    growth_rate : int
        Number of output channels
    num_class : int
        Ouput size of symbol
    data_type : str
        the type of dataset
    reduction : float
        Compression ratio. Default = 0.5
    drop_out : float
        Probability of an element to be zeroed. Default = 0.2
    workspace : int
        Workspace used in convolution operator
    """
    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)
    print(version_input, version_output, version_unit)

    num_stage = 4
    reduction = 0.5
    drop_out = 0.2
    bottle_neck = True
    bn_mom = 0.9
    workspace = 256
    growth_rate = 32
    if num_layers == 121:
        units = [6, 12, 24, 16]
    elif num_layers == 169:
        units = [6, 12, 32, 32]
    elif num_layers == 201:
        units = [6, 12, 48, 32]
    elif num_layers == 161:
        units = [6, 12, 36, 24]
        growth_rate = 48
    else:
        raise ValueError("no experiments done on num_layers {}, you can do it yourself".format(num_layers))
    num_unit = len(units)
    assert(num_unit == num_stage)
    init_channels = 2 * growth_rate
    n_channels = init_channels

    data = mx.sym.Variable(name='data')
    #data = data-127.5
    #data = data*0.0078125
    #if version_input==0:
    #  body = mx.sym.Convolution(data=data, num_filter=growth_rate*2, kernel=(7, 7), stride=(2,2), pad=(3, 3),
    #                            no_bias=True, name="conv0", workspace=workspace)
    #else:
    #  body = mx.sym.Convolution(data=data, num_filter=growth_rate*2, 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 = mx.sym.Activation(data=body, act_type='relu', name='relu0')
    #body = mx.symbol.Pooling(data=body, kernel=(3, 3), stride=(2,2), pad=(1,1), pool_type='max')
    body = symbol_utils.get_head(data, version_input, growth_rate*2)

    for i in range(num_stage-1):
        body = DenseBlock(units[i], body, growth_rate=growth_rate, name='DBstage%d' % (i + 1), bottle_neck=bottle_neck, drop_out=drop_out, bn_mom=bn_mom, workspace=workspace)
        n_channels += units[i]*growth_rate
        n_channels = int(math.floor(n_channels*reduction))
        body = TransitionBlock(i, body, n_channels, stride=(1,1), name='TBstage%d' % (i + 1), drop_out=drop_out, bn_mom=bn_mom, workspace=workspace)
    body = DenseBlock(units[num_stage-1], body, growth_rate=growth_rate, name='DBstage%d' % (num_stage), bottle_neck=bottle_neck, drop_out=drop_out, bn_mom=bn_mom, workspace=workspace)
    fc1 = symbol_utils.get_fc1(body, num_classes, fc_type)
    return fc1
Esempio n. 3
0
def get_symbol(num_classes, num_layers, **kwargs):
    """Return DenseNet symbol of imagenet
    Parameters
    ----------
    units : list
        Number of units in each stage
    num_stage : int
        Number of stage
    growth_rate : int
        Number of output channels
    num_class : int
        Ouput size of symbol
    data_type : str
        the type of dataset
    reduction : float
        Compression ratio. Default = 0.5
    drop_out : float
        Probability of an element to be zeroed. Default = 0.2
    workspace : int
        Workspace used in convolution operator
    """
    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)
    print(version_input, version_output, version_unit)

    num_stage = 4
    reduction = 0.5
    drop_out = 0.2
    bottle_neck = True
    bn_mom = 0.9
    workspace = 256
    growth_rate = 32
    if num_layers == 121:
        units = [6, 12, 24, 16]
    elif num_layers == 169:
        units = [6, 12, 32, 32]
    elif num_layers == 201:
        units = [6, 12, 48, 32]
    elif num_layers == 161:
        units = [6, 12, 36, 24]
        growth_rate = 48
    else:
        raise ValueError(
            "no experiments done on num_layers {}, you can do it yourself".
            format(num_layers))
    num_unit = len(units)
    assert (num_unit == num_stage)
    init_channels = 2 * growth_rate
    n_channels = init_channels

    data = mx.sym.Variable(name='data')
    #data = data-127.5
    #data = data*0.0078125
    #if version_input==0:
    #  body = mx.sym.Convolution(data=data, num_filter=growth_rate*2, kernel=(7, 7), stride=(2,2), pad=(3, 3),
    #                            no_bias=True, name="conv0", workspace=workspace)
    #else:
    #  body = mx.sym.Convolution(data=data, num_filter=growth_rate*2, 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 = mx.sym.Activation(data=body, act_type='relu', name='relu0')
    #body = mx.symbol.Pooling(data=body, kernel=(3, 3), stride=(2,2), pad=(1,1), pool_type='max')
    body = symbol_utils.get_head(data, version_input, growth_rate * 2)

    for i in range(num_stage - 1):
        body = DenseBlock(units[i],
                          body,
                          growth_rate=growth_rate,
                          name='DBstage%d' % (i + 1),
                          bottle_neck=bottle_neck,
                          drop_out=drop_out,
                          bn_mom=bn_mom,
                          workspace=workspace)
        n_channels += units[i] * growth_rate
        n_channels = int(math.floor(n_channels * reduction))
        body = TransitionBlock(i,
                               body,
                               n_channels,
                               stride=(1, 1),
                               name='TBstage%d' % (i + 1),
                               drop_out=drop_out,
                               bn_mom=bn_mom,
                               workspace=workspace)
    body = DenseBlock(units[num_stage - 1],
                      body,
                      growth_rate=growth_rate,
                      name='DBstage%d' % (num_stage),
                      bottle_neck=bottle_neck,
                      drop_out=drop_out,
                      bn_mom=bn_mom,
                      workspace=workspace)
    fc1 = symbol_utils.get_fc1(body, num_classes, fc_type)
    return fc1
Esempio n. 4
0
def get_symbol(num_classes = 1000, num_layers=92, **kwargs):
    if num_layers==68:
      k_R = 128
      G   = 32
      k_sec  = {  2: 3,  \
                  3: 4,  \
                  4: 12, \
                  5: 3   }
      inc_sec= {  2: 16, \
                  3: 32, \
                  4: 32, \
                  5: 64  }
    elif num_layers==92:
      k_R = 96
      G   = 32
      k_sec  = {  2: 3, \
                  3: 4, \
                  4: 20, \
                  5: 3   }
      inc_sec= {  2: 16, \
                  3: 32, \
                  4: 24, \
                  5: 128 }
    elif num_layers==107:
      k_R = 200
      G   = 50
      k_sec  = {  2: 4, \
                  3: 8, \
                  4: 20, \
                  5: 3   }
      inc_sec= {  2: 20, \
                  3: 64, \
                  4: 64, \
                  5: 128 }
    elif num_layers==131:
      k_R = 160
      G   = 40
      k_sec  = {  2: 4, \
                  3: 8, \
                  4: 28, \
                  5: 3   }
      inc_sec= {  2: 16, \
                  3: 32, \
                  4: 32, \
                  5: 128 }
    else:
      raise ValueError("no experiments done on dpn num_layers {}, you can do it yourself".format(num_layers))

    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)
    print(version_se, version_input, version_output, version_unit)

    ## define Dual Path Network
    data = mx.symbol.Variable(name="data")
    #data = data-127.5
    #data = data*0.0078125
    #if version_input==0:
    #  conv1_x_1  = Conv(data=data,  num_filter=128,  kernel=(7, 7), name='conv1_x_1', pad=(3,3), stride=(2,2))
    #else:
    #  conv1_x_1  = Conv(data=data,  num_filter=128,  kernel=(3, 3), name='conv1_x_1', pad=(3,3), stride=(1,1))
    #conv1_x_1  = BN_AC(conv1_x_1, name='conv1_x_1__relu-sp')
    #conv1_x_x  = mx.symbol.Pooling(data=conv1_x_1, pool_type="max", kernel=(3, 3),  pad=(1,1), stride=(2,2), name="pool1")
    conv1_x_x = symbol_utils.get_head(data, version_input, 128)

    # conv2
    bw = 256
    inc= inc_sec[2]
    R  = (k_R*bw)/256 
    conv2_x_x  = DualPathFactory(     conv1_x_x,   R,   R,   bw,  'conv2_x__1',           inc,   G,  'proj'  )
    for i_ly in range(2, k_sec[2]+1):
        conv2_x_x  = DualPathFactory( conv2_x_x,   R,   R,   bw, ('conv2_x__%d'% i_ly),   inc,   G,  'normal')

    # conv3
    bw = 512
    inc= inc_sec[3]
    R  = (k_R*bw)/256
    conv3_x_x  = DualPathFactory(     conv2_x_x,   R,   R,   bw,  'conv3_x__1',           inc,   G,  'down'  )
    for i_ly in range(2, k_sec[3]+1):
        conv3_x_x  = DualPathFactory( conv3_x_x,   R,   R,   bw, ('conv3_x__%d'% i_ly),   inc,   G,  'normal')

    # conv4
    bw = 1024
    inc= inc_sec[4]
    R  = (k_R*bw)/256
    conv4_x_x  = DualPathFactory(     conv3_x_x,   R,   R,   bw,  'conv4_x__1',           inc,   G,  'down'  )
    for i_ly in range(2, k_sec[4]+1):
        conv4_x_x  = DualPathFactory( conv4_x_x,   R,   R,   bw, ('conv4_x__%d'% i_ly),   inc,   G,  'normal')

    # conv5
    bw = 2048
    inc= inc_sec[5]
    R  = (k_R*bw)/256
    conv5_x_x  = DualPathFactory(     conv4_x_x,   R,   R,   bw,  'conv5_x__1',           inc,   G,  'down'  )
    for i_ly in range(2, k_sec[5]+1):
        conv5_x_x  = DualPathFactory( conv5_x_x,   R,   R,   bw, ('conv5_x__%d'% i_ly),   inc,   G,  'normal')

    # output: concat
    conv5_x_x  = mx.symbol.Concat(*[conv5_x_x[0], conv5_x_x[1]],  name='conv5_x_x_cat-final')
    #conv5_x_x = BN_AC(conv5_x_x, name='conv5_x_x__relu-sp')
    before_pool = conv5_x_x
    fc1 = symbol_utils.get_fc1(before_pool, num_classes, fc_type)
    return fc1