Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def head_symbol(data_shape):
data = mx.symbol.Variable('data', shape=data_shape, dtype='float32')
weight = mx.symbol.Variable('weight', dtype='float32')
bn = mx.sym.BatchNorm(data=data, fix_gamma=True, eps=2e-5, momentum=0.9, name='bn')
return bn, weight
def sym_gen(seq_len):
with mx.AttrScope(ctx_group='dev1'):
data = mx.symbol.Variable('data')
weight = mx.symbol.Variable('dev1_weight')
bias = mx.symbol.Variable('dev1_bias')
fc = data
for i in range(seq_len):
fc = mx.symbol.FullyConnected(data=fc, weight=weight, bias=bias,
name='dev1_fc_%d' % i, num_hidden=num_hidden)
with mx.AttrScope(ctx_group='dev2'):
label = mx.symbol.Variable('label')
weight = mx.symbol.Variable('dev2_weight')
bias = mx.symbol.Variable('dev2_bias')
for i in range(seq_len):
fc = mx.symbol.FullyConnected(data=fc, weight=weight, bias=bias,
name='dev2_fc_%d' % i, num_hidden=num_hidden)
sym = mx.symbol.SoftmaxOutput(fc, label, name='softmax')
return sym, ('data',), ('label',)
def test_operator():
data = mx.symbol.Variable('data')
with mx.AttrScope(__group__='4', __data__='great'):
fc1 = mx.symbol.Activation(data, act_type='relu')
with mx.AttrScope(__init_bias__='0.0'):
fc2 = mx.symbol.FullyConnected(fc1, num_hidden=10, name='fc2')
assert fc1.attr('__data__') == 'great'
assert fc2.attr('__data__') == 'great'
assert fc2.attr('__init_bias__') == '0.0'
fc2copy = pkl.loads(pkl.dumps(fc2))
assert fc2copy.tojson() == fc2.tojson()
fc2weight = fc2.get_internals()['fc2_weight']
def get_symbol(num_classes=1000, **kwargs):
'''
'''
use_global_stats = kwargs['use_global_stats']
data = mx.symbol.Variable(name="data")
label = mx.symbol.Variable(name="label")
conv0 = mx.sym.Convolution(data, name='0/conv',
num_filter=3, kernel=(3, 3), pad=(1, 1), num_group=3, no_bias=True)
conv1 = bn_conv(conv0, '1/',
num_filter=24, kernel=(3, 3), pad=(1, 1), stride=(2, 2),
use_global_stats=use_global_stats)
#
# conv1 = mx.sym.Convolution(data, name='1/conv',
# num_filter=36, kernel=(4, 4), pad=(1, 1), stride=(2, 2), no_bias=True)
concat1 = mx.sym.concat(conv1, -conv1, name='1/concat')
bn1 = batchnorm(concat1, '2/bn', use_global_stats)
pool2 = mx.sym.Pooling(bn1, name='2/pool', kernel=(4, 4), pad=(1, 1), stride=(2, 2), pool_type='max')
conv3 = depthwise_conv(pool2, '3/',
nf_dw=72, nf_sep=72, n_pre_sfl=2, n_post_sfl=2,
def get_symbol(
num_class,
num_block,
num_layer,
growth_rate,
dropout=0.,
l2_reg=1e-4,
init_channels=16
):
n_channels = init_channels
data = mx.symbol.Variable(name='data')
#label = mx.sym.Variable("label")
conv = mx.symbol.Convolution(
name="conv0",
data=data,
num_filter=init_channels,
kernel=(3, 3),
stride=(1, 1),
pad=(1, 1),
no_bias=True
)
#conv = mx.symbol.Pooling(conv, name='conv0_pool', global_pool=False, kernel=(3,3), stride=(2,2), pool_type ='max')
for i in range(num_block - 1):
conv = dense_block(conv, num_layer, growth_rate, name = 'dense'+str(i)+'_',
dropout=dropout, l2_reg=l2_reg)
def get_train_symbol(self, num_classes):
"""
get symbol for training
:param num_classes: num of classes
:return: the symbol for training
"""
data = mx.symbol.Variable(name="data")
seg_cls_gt = mx.symbol.Variable(name='label')
# shared convolutional layers
conv_feat = self.get_resnet_conv(data)
# subsequent fc layers by haozhi
fc6_bias = mx.symbol.Variable('fc6_bias', lr_mult=2.0)
fc6_weight = mx.symbol.Variable('fc6_weight', lr_mult=1.0)
fc6 = mx.symbol.Convolution(data=conv_feat, kernel=(1, 1), pad=(0, 0), num_filter=1024, name="fc6",
bias=fc6_bias, weight=fc6_weight, workspace=self.workspace)
relu_fc6 = mx.sym.Activation(data=fc6, act_type='relu', name='relu_fc6')
score_bias = mx.symbol.Variable('score_bias', lr_mult=2.0)
score_weight = mx.symbol.Variable('score_weight', lr_mult=1.0)
score = mx.symbol.Convolution(data=relu_fc6, kernel=(1, 1), pad=(0, 0), num_filter=num_classes, name="score",
bias=score_bias, weight=score_weight, workspace=self.workspace)
upsampling = mx.symbol.Deconvolution(data=score, num_filter=num_classes, kernel=(32, 32), stride=(16, 16),
num_group=num_classes, no_bias=True, name='upsampling',
attr={'lr_mult': '0.0'}, workspace=self.workspace)
croped_score = mx.symbol.Crop(*[upsampling, data], offset=(8, 8), name='croped_score')
def get_vgg_rpn(num_anchors=config.NUM_ANCHORS):
"""
Region Proposal Network with VGG
:param num_anchors: used to determine output size
:return: Symbol
"""
data = mx.symbol.Variable(name="data")
label = mx.symbol.Variable(name='label')
bbox_target = mx.symbol.Variable(name='bbox_target')
bbox_weight = mx.symbol.Variable(name='bbox_weight')
# shared convolutional layers
relu5_3 = get_vgg_conv(data)
# RPN
rpn_conv = mx.symbol.Convolution(
data=relu5_3, kernel=(3, 3), pad=(1, 1), num_filter=512, name="rpn_conv_3x3")
rpn_relu = mx.symbol.Activation(data=rpn_conv, act_type="relu", name="rpn_relu")
rpn_cls_score = mx.symbol.Convolution(
data=rpn_relu, kernel=(1, 1), pad=(0, 0), num_filter=2 * num_anchors, name="rpn_cls_score")
rpn_bbox_pred = mx.symbol.Convolution(
data=rpn_relu, kernel=(1, 1), pad=(0, 0), num_filter=4 * num_anchors, name="rpn_bbox_pred")
def get_ocrnet():
data = mx.symbol.Variable('data')
label = mx.symbol.Variable('softmax_label')
conv1 = mx.symbol.Convolution(data=data, kernel=(5,5), num_filter=32)
pool1 = mx.symbol.Pooling(data=conv1, pool_type="max", kernel=(2,2), stride=(1, 1))
relu1 = mx.symbol.Activation(data=pool1, act_type="relu")
conv2 = mx.symbol.Convolution(data=relu1, kernel=(5,5), num_filter=32)
pool2 = mx.symbol.Pooling(data=conv2, pool_type="avg", kernel=(2,2), stride=(1, 1))
relu2 = mx.symbol.Activation(data=pool2, act_type="relu")
conv3 = mx.symbol.Convolution(data=relu2, kernel=(3,3), num_filter=32)
pool3 = mx.symbol.Pooling(data=conv3, pool_type="avg", kernel=(2,2), stride=(1, 1))
relu3 = mx.symbol.Activation(data=pool3, act_type="relu")
conv4 = mx.symbol.Convolution(data=relu3, kernel=(3,3), num_filter=32)
pool4 = mx.symbol.Pooling(data=conv4, pool_type="avg", kernel=(2,2), stride=(1, 1))
relu4 = mx.symbol.Activation(data=pool4, act_type="relu")
def get_train_symbol(self, num_classes):
"""
get symbol for training
:param num_classes: num of classes
:return: the symbol for training
"""
data = mx.symbol.Variable(name="data")
seg_cls_gt = mx.symbol.Variable(name='label')
# shared convolutional layers
conv_feat = self.get_resnet_conv(data)
# subsequent fc layers by haozhi
fc6_bias = mx.symbol.Variable('fc6_bias', lr_mult=2.0)
fc6_weight = mx.symbol.Variable('fc6_weight', lr_mult=1.0)
fc6 = mx.symbol.Convolution(data=conv_feat, kernel=(1, 1), pad=(0, 0), num_filter=1024, name="fc6",
bias=fc6_bias, weight=fc6_weight, workspace=self.workspace)
relu_fc6 = mx.sym.Activation(data=fc6, act_type='relu', name='relu_fc6')
score_bias = mx.symbol.Variable('score_bias', lr_mult=2.0)
score_weight = mx.symbol.Variable('score_weight', lr_mult=1.0)
score = mx.symbol.Convolution(data=relu_fc6, kernel=(1, 1), pad=(0, 0), num_filter=num_classes, name="score",
bias=score_bias, weight=score_weight, workspace=self.workspace)
upsampling = mx.symbol.Deconvolution(data=score, num_filter=num_classes, kernel=(32, 32), stride=(16, 16),
num_group=num_classes, no_bias=True, name='upsampling',
attr={'lr_mult': '0.0'}, workspace=self.workspace)
def get_test_symbol(self, num_classes):
"""
get symbol for testing
:param num_classes: num of classes
:return: the symbol for testing
"""
data = mx.symbol.Variable(name="data")
# shared convolutional layers
conv_feat = self.get_resnet_conv(data)
fc6_bias = mx.symbol.Variable('fc6_bias', lr_mult=2.0)
fc6_weight = mx.symbol.Variable('fc6_weight', lr_mult=1.0)
fc6 = mx.symbol.Convolution(
data=conv_feat, kernel=(1, 1), pad=(0, 0), num_filter=1024, name="fc6", bias=fc6_bias, weight=fc6_weight,
workspace=self.workspace)
relu_fc6 = mx.sym.Activation(data=fc6, act_type='relu', name='relu_fc6')
score_bias = mx.symbol.Variable('score_bias', lr_mult=2.0)
score_weight = mx.symbol.Variable('score_weight', lr_mult=1.0)
score = mx.symbol.Convolution(
data=relu_fc6, kernel=(1, 1), pad=(0, 0), num_filter=num_classes, name="score", bias=score_bias,
weight=score_weight, workspace=self.workspace)
upsampling = mx.symbol.Deconvolution(
data=score, num_filter=num_classes, kernel=(32, 32), stride=(16, 16), num_group=num_classes, no_bias=True,