示例#1
0
def SE_block(x,nb_filter,block,stage):
    y = Pooling_v1(x,name='conv'+str(stage)+'_'+str(block)+'_global_pool',kernel=(2,2),pool_type='avg',global_pool=True)
    y = FullyConnected(y, name='conv'+str(stage)+'_'+str(block)+'_1x1_down', num_hidden=int(nb_filter/16))
    y = Activation(y, name='conv'+str(stage)+'_'+str(block)+'_1x1_down_relu', act_type='relu')
    y = FullyConnected(y, name='conv'+str(stage)+'_'+str(block)+'_1x1_up', num_hidden=nb_filter)
    y = Activation(y, name='conv'+str(stage)+'_'+str(block)+'_prob', act_type='sigmoid')
    y = broadcast_mul(Reshape(y,shape=(-1,nb_filter,1,1)),x)
    return y
示例#2
0
def detai_net(inputs,classes=1000,use_att=False,model_name='resnetv20'):
    """Instantiates the ResNet50 architecture.
    """
    if use_att:
        x = inputs[0]
    else:
        x = inputs
        
    x = Norm(x,fix_gamma=True,use_global_stats=False,eps=1e-5,name=model_name+'_batchnorm0')
    x = Convolution(x,
        num_filter=64, kernel=(7, 7), stride=(2, 2), pad=(3,3), no_bias=True, name=model_name+'_conv0')
    x = Norm(x,fix_gamma=False,use_global_stats=False,eps=1e-5,name=model_name+'_batchnorm1')
    x = Activation(x,name=model_name+'_relu0',act_type='relu')
    x = Pooling(x,kernel=(3,3),stride=(2,2),pad=(1,1),pool_type='max',name=model_name+'_pool0')
     
    x = conv_block(x, 3, [64, 64, 256], stage=1, block=0, strides=(1, 1),model_name=model_name)
    x = identity_block(x, 3, [64, 64, 256], stage=1, block=3,model_name=model_name)
    x = identity_block(x, 3, [64, 64, 256], stage=1, block=6,model_name=model_name)
    
    x = conv_block(x, 3, [128, 128, 512], stage=2, block=0,model_name=model_name)
    x = identity_block(x, 3, [128, 128, 512], stage=2, block=3,model_name=model_name)
    x = identity_block(x, 3, [128, 128, 512], stage=2, block=6,model_name=model_name)
    x = identity_block(x, 3, [128, 128, 512], stage=2, block=9,model_name=model_name)
    
    x = conv_block(x, 3, [256, 256, 1024], stage=3, block=0,model_name=model_name)
    x = identity_block(x, 3, [256, 256, 1024], stage=3, block=3,model_name=model_name)
    x = identity_block(x, 3, [256, 256, 1024], stage=3, block=6,model_name=model_name)
    x = identity_block(x, 3, [256, 256, 1024], stage=3, block=9,model_name=model_name)
    x = identity_block(x, 3, [256, 256, 1024], stage=3, block=12,model_name=model_name)
    x = identity_block(x, 3, [256, 256, 1024], stage=3, block=15,model_name=model_name)
    
    x = conv_block(x, 3, [512, 512, 2048], stage=4, block=0,model_name=model_name)
    x = identity_block(x, 3, [512, 512, 2048], stage=4, block=3,model_name=model_name)
    x = identity_block(x, 3, [512, 512, 2048], stage=4, block=6,model_name=model_name)  
    
    x = Norm(x,fix_gamma=False,use_global_stats=False,eps=1e-5,name=model_name+'_batchnorm2')
    x = Activation(x,name=model_name+'_relu1',act_type='relu')
    
    x = Pooling(x,kernel=(3,3),stride=(2, 2),pool_type='avg',global_pool=True,name=model_name+'_pool1')
    x = Flatten(x,name=model_name+'_flatten0')
    
    if use_att:
        x = mx.sym.Concat(x,inputs[1],dim=-1)
        weight = mx.sym.Variable(model_name+'_concat_dense_weight',shape=(classes,4096)) 
        x = FullyConnected(x,num_hidden=classes,weight=weight,no_bias=True,name=model_name+'_dense1')
    else:
        x = FullyConnected(x,num_hidden=classes,name=model_name+'_dense1')
    return x



    
示例#3
0
def ResNet18_V2(inputs,classes=1000,model_name='resnetv20'):
    """Instantiates the ResNet18 architecture.
    """
    x = inputs
    x = Norm(x,fix_gamma=True,use_global_stats=False,eps=1e-5,name=model_name+'_batchnorm0')
    x = Convolution(x,
        num_filter=64, kernel=(7, 7), stride=(2, 2), pad=(3,3), no_bias=True, name=model_name+'_conv0')
    x = Norm(x,fix_gamma=False,use_global_stats=False,eps=1e-5,name=model_name+'_batchnorm1')
    x = Activation(x,name=model_name+'_relu0',act_type='relu')
    x = Pooling(x,kernel=(3,3),stride=(2,2),pad=(1,1),pool_type='max',name=model_name+'_pool0')
     
    x = conv_block(x, 3, [64, 64], stage=1, block=0, strides=(1, 1),short_connect=False,model_name=model_name)
    x = identity_block(x, 3, [64, 64], stage=1, block=2,model_name=model_name)

    x = conv_block(x, 3, [128, 128], stage=2, block=0,model_name=model_name)
    x = identity_block(x, 3, [128, 128], stage=2, block=3,model_name=model_name)

    x = conv_block(x, 3, [256, 256], stage=3, block=0,model_name=model_name)
    x = identity_block(x, 3, [256, 256], stage=3, block=3,model_name=model_name)

    x = conv_block(x, 3, [512, 512], stage=4, block=0, model_name=model_name)
    x = identity_block(x, 3, [512, 512], stage=4, block=3, model_name=model_name)
    

    x = Norm(x,fix_gamma=False,use_global_stats=False,eps=1e-5,name=model_name+'_batchnorm2')
    x = Activation(x,name=model_name+'_relu1',act_type='relu')
    
    x = Pooling(x,kernel=(3,3),stride=(2, 2),pool_type='avg',global_pool=True,name=model_name+'_pool1')
    x = Flatten(x,name=model_name+'_flatten0')
    
    xl2 = mx.sym.norm(x,axis=1,keepdims=True)
    xl2 = mx.sym.clip(xl2,0,8)
    x1 = mx.sym.L2Normalization(x)
    x1 = mx.sym.broadcast_mul(x1,xl2)
    
    weight = mx.sym.Variable(model_name+'_dense1_weight')
    bias = mx.sym.Variable(model_name+'_dense1_bias')
    
    output = FullyConnected(x,num_hidden=classes,weight=weight,bias=bias,name=model_name+'_dense1')
    
#    weight1 =  mx.sym.L2Normalization(weight)
    output1 = FullyConnected(x1,num_hidden=classes,weight=weight,bias=bias,name=model_name+'_dense2')

    return output,output1


#


    
示例#4
0
def atten_net(inputs, classes=200, model_name='resnet50_v10'):
    """Instantiates the ResNet50 architecture.
    """
    x = inputs

    x = Convolution(x,
                    num_filter=64,
                    kernel=(7, 7),
                    stride=(2, 2),
                    pad=(3, 3),
                    no_bias=True,
                    name=model_name + '_conv0')
    x = Norm(x,
             fix_gamma=False,
             use_global_stats=False,
             eps=1e-5,
             name=model_name + '_batchnorm0')
    x = Activation(x, name=model_name + '_relu0', act_type='relu')
    x = Pooling(x,
                kernel=(3, 3),
                stride=(2, 2),
                pad=(1, 1),
                pool_type='max',
                name=model_name + '_pool0')

    x = conv_block(x,
                   3, [64, 64, 256],
                   stage=1,
                   block=0,
                   strides=(1, 1),
                   model_name=model_name)
    x = identity_block(x,
                       3, [64, 64, 256],
                       stage=1,
                       block=4,
                       model_name=model_name)
    x = identity_block(x,
                       3, [64, 64, 256],
                       stage=1,
                       block=7,
                       model_name=model_name)

    x = conv_block(x,
                   3, [128, 128, 512],
                   stage=2,
                   block=0,
                   model_name=model_name)
    x = identity_block(x,
                       3, [128, 128, 512],
                       stage=2,
                       block=4,
                       model_name=model_name)
    x = identity_block(x,
                       3, [128, 128, 512],
                       stage=2,
                       block=7,
                       model_name=model_name)
    x = identity_block(x,
                       3, [128, 128, 512],
                       stage=2,
                       block=10,
                       model_name=model_name)

    x = conv_block(x,
                   3, [256, 256, 1024],
                   stage=3,
                   block=0,
                   strides=(1, 1),
                   dilate=(2, 2),
                   model_name=model_name)
    x = identity_block(x,
                       3, [256, 256, 1024],
                       stage=3,
                       block=4,
                       dilate=(2, 2),
                       model_name=model_name)
    x = identity_block(x,
                       3, [256, 256, 1024],
                       stage=3,
                       block=7,
                       dilate=(2, 2),
                       model_name=model_name)
    x = identity_block(x,
                       3, [256, 256, 1024],
                       stage=3,
                       block=10,
                       dilate=(2, 2),
                       model_name=model_name)
    x = identity_block(x,
                       3, [256, 256, 1024],
                       stage=3,
                       block=13,
                       dilate=(2, 2),
                       model_name=model_name)
    x = identity_block(x,
                       3, [256, 256, 1024],
                       stage=3,
                       block=16,
                       dilate=(2, 2),
                       model_name=model_name)

    x = conv_block(x,
                   3, [512, 512, 2048],
                   stage=4,
                   block=0,
                   strides=(1, 1),
                   dilate=(2, 2),
                   model_name=model_name)
    x = identity_block(x,
                       3, [512, 512, 2048],
                       stage=4,
                       block=4,
                       dilate=(1, 1),
                       model_name=model_name)
    x = identity_block(x,
                       3, [512, 512, 2048],
                       stage=4,
                       block=7,
                       dilate=(1, 1),
                       model_name=model_name)

    att_fea = x
    x = Pooling(x,
                kernel=(3, 3),
                stride=(2, 2),
                pool_type='avg',
                global_pool=True,
                name=model_name + '_pool1')
    x = flatten(x)

    weight = mx.sym.Variable(model_name + '_dense1_weight')
    x = FullyConnected(x,
                       num_hidden=classes,
                       weight=weight,
                       name=model_name + '_dense1')

    #    x = SoftmaxOutput(x, name = 'softmax')

    return x, att_fea, weight
示例#5
0
def ResNet50_V2(inputs, classes=1000, batch_size=144, model_name='resnetv20'):
    """Instantiates the ResNet50 architecture.
    """

    x = inputs[0]
    lam = inputs[1]

    x = Norm(x,
             fix_gamma=True,
             use_global_stats=False,
             eps=1e-5,
             name=model_name + '_batchnorm0')
    x = Convolution(x,
                    num_filter=64,
                    kernel=(7, 7),
                    stride=(2, 2),
                    pad=(3, 3),
                    no_bias=True,
                    name=model_name + '_conv0')
    x = Norm(x,
             fix_gamma=False,
             use_global_stats=False,
             eps=1e-5,
             name=model_name + '_batchnorm1')
    x = Activation(x, name=model_name + '_relu0', act_type='relu')
    x = Pooling(x,
                kernel=(3, 3),
                stride=(2, 2),
                pad=(1, 1),
                pool_type='max',
                name=model_name + '_pool0')

    x = conv_block(x,
                   3, [64, 64, 256],
                   stage=1,
                   block=0,
                   strides=(1, 1),
                   model_name=model_name)
    x = identity_block(x,
                       3, [64, 64, 256],
                       stage=1,
                       block=3,
                       model_name=model_name)
    x = identity_block(x,
                       3, [64, 64, 256],
                       stage=1,
                       block=6,
                       model_name=model_name)

    x_f = mx.sym.slice(x,
                       begin=(None, None, None, None),
                       end=(None, None, None, None),
                       step=(-1, 1, 1, 1))
    lam1 = mx.sym.slice(lam, begin=(0), end=(1))
    lam2 = mx.sym.slice(lam, begin=(1), end=(2))
    x_mix = mx.sym.broadcast_mul(x, lam1) + broadcast_mul(x_f, lam2)
    x = mx.sym.concat(x, x_mix, dim=0)

    x = conv_block(x,
                   3, [128, 128, 512],
                   stage=2,
                   block=0,
                   model_name=model_name)
    x = identity_block(x,
                       3, [128, 128, 512],
                       stage=2,
                       block=3,
                       model_name=model_name)
    x = identity_block(x,
                       3, [128, 128, 512],
                       stage=2,
                       block=6,
                       model_name=model_name)
    x = identity_block(x,
                       3, [128, 128, 512],
                       stage=2,
                       block=9,
                       model_name=model_name)

    x = conv_block(x,
                   3, [256, 256, 1024],
                   stage=3,
                   block=0,
                   model_name=model_name)
    x = identity_block(x,
                       3, [256, 256, 1024],
                       stage=3,
                       block=3,
                       model_name=model_name)
    x = identity_block(x,
                       3, [256, 256, 1024],
                       stage=3,
                       block=6,
                       model_name=model_name)
    x = identity_block(x,
                       3, [256, 256, 1024],
                       stage=3,
                       block=9,
                       model_name=model_name)
    x = identity_block(x,
                       3, [256, 256, 1024],
                       stage=3,
                       block=12,
                       model_name=model_name)
    x = identity_block(x,
                       3, [256, 256, 1024],
                       stage=3,
                       block=15,
                       model_name=model_name)

    x = conv_block(x,
                   3, [512, 512, 2048],
                   stage=4,
                   block=0,
                   model_name=model_name)
    x = identity_block(x,
                       3, [512, 512, 2048],
                       stage=4,
                       block=3,
                       model_name=model_name)
    x = identity_block(x,
                       3, [512, 512, 2048],
                       stage=4,
                       block=6,
                       model_name=model_name)

    x = Norm(x,
             fix_gamma=False,
             use_global_stats=False,
             eps=1e-5,
             name=model_name + '_batchnorm2')
    x = Activation(x, name=model_name + '_relu1', act_type='relu')

    x = Pooling(x,
                kernel=(3, 3),
                stride=(2, 2),
                pool_type='avg',
                global_pool=True,
                name=model_name + '_pool1')
    x = Flatten(x, name=model_name + '_flatten0')

    #    x = mx.sym.L2Normalization(x)

    weight = mx.sym.Variable(model_name + '_dense_weight')
    #    weight = mx.sym.L2Normalization(weight)
    output = FullyConnected(x,
                            num_hidden=classes,
                            weight=weight,
                            no_bias=True,
                            name=model_name + '_dense1')
    #

    output1 = mx.sym.slice_axis(output, axis=0, begin=0, end=batch_size)
    mix_output = mx.sym.slice_axis(output,
                                   axis=0,
                                   begin=batch_size,
                                   end=2 * batch_size)

    output1_f = mx.sym.slice(output1,
                             begin=(None, None),
                             end=(None, None),
                             step=(-1, 1))
    output_mix = mx.sym.broadcast_mul(output1, lam1) + broadcast_mul(
        output1_f, lam2)

    #    x = mx.sym.L2Normalization(x)
    #    weight = mx.sym.Variable(model_name+'_dense1_weight',shape=(classes,2048))
    #    weight = mx.sym.L2Normalization(weight)

    #    alpha_w = mx.sym.Variable(model_name+'_alpha',shape=(1,1),init=mx.init.Constant(3))

    #    alpha_w = mx.sym.clip(alpha_w,2,6)

    #    weight = mx.sym.broadcast_mul(weight, alpha_w)
    #    x = mx.sym.broadcast_mul(x, alpha_w+1)

    #    weight = weight * alpha
    #    x = x * (alpha+2)

    #    weight = mx.sym.expand_dims(weight,0)
    #    x = mx.sym.expand_dims(x,1)
    #    x = mx.sym.tile(x,(1,classes,1))
    #    dis = mx.sym.broadcast_minus(x,weight)
    #    x = -mx.sym.sum(dis*dis,axis=2)
    #    x = x / alpha

    return output1, output_mix, mix_output


#inputs = mx.sym.Variable('data')
#lam = mx.sym.Variable('lam')
#outputs = ResNet50_V2([inputs,lam],200,batch_size=144)
#net = mx.gluon.SymbolBlock(outputs,[inputs,lam])
##import gluoncv
##gluoncv.utils.viz.plot_network(net,shape=)
#
#a = mx.viz.plot_network(outputs[1], shape={'data':(144,3,224,224),'lam':(2,)},save_format='png')
#a.view('12')