Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def build_graph():
data = mx.sym.var("data")
data = mx.sym.flatten(data=data)
fc1 = mx.sym.FullyConnected(data=data, num_hidden=128)
act1 = mx.sym.Activation(data=fc1, act_type="relu")
fc2 = mx.sym.FullyConnected(data=act1, num_hidden=64)
act2 = mx.sym.Activation(data=fc2, act_type="relu")
fc3 = mx.sym.FullyConnected(data=act2, num_hidden=10)
return mx.sym.SoftmaxOutput(data=fc3, name="softmax")
num_hidden=self._num_hidden * 4,
name='%si2h' % name)
h2h = mx.sym.FullyConnected(data=states[0], weight=self._hW, bias=self._hB,
num_hidden=self._num_hidden * 4,
name='%sh2h' % name)
gates = self._iN(i2h) + self._hN(h2h + mx.sym.zeros_like(i2h))
# pylint: disable=unbalanced-tuple-unpacking
in_gate, forget_gate, in_transform, out_gate = mx.sym.split(gates,
num_outputs=4,
axis=1,
name="%sslice" % name)
in_gate = mx.sym.Activation(in_gate, act_type="sigmoid",
name='%si' % name)
forget_gate = mx.sym.Activation(forget_gate, act_type="sigmoid",
name='%sf' % name)
in_transform = mx.sym.Activation(in_transform, act_type="tanh",
name='%sc' % name)
out_gate = mx.sym.Activation(out_gate, act_type="sigmoid",
name='%so' % name)
next_c = mx.sym._internal._plus(forget_gate * states[1], in_gate * in_transform,
name='%sstate' % name)
next_h = mx.sym._internal._mul(out_gate,
mx.sym.Activation(self._cN(next_c), act_type="tanh"),
name='%sout' % name)
return next_h, [next_h, next_c]
dataset : str
Dataset type, only cifar10 and imagenet supports
workspace : int
Workspace used in convolution operator
"""
rois = None
rpn_cls_loss = None
rpn_bbox_loss = None
num_unit = len(units)
assert(num_unit == num_stage)
data = mx.sym.Variable(name='data')
data = mx.sym.BatchNorm(data=data, fix_gamma=True, eps=2e-5, momentum=bn_mom, use_global_stats=bn_global, name='bn_data')
body = mx.sym.Convolution(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, use_global_stats=bn_global, 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')
for i in range(num_stage):
bn_global_ = bn_global if i < num_stage-1 else False # after roi-pooling, do not use use_global_stats
dilate=True if i == num_stage-1 else False
body = residual_unit(body, filter_list[i+1], (1 if i==0 else 2, 1 if i==0 else 2), False, dilate=dilate,
name='stage%d_unit%d' % (i + 1, 1), bottle_neck=bottle_neck, workspace=workspace,
bn_global=bn_global_)
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, workspace=workspace, bn_global=bn_global_)
if i == num_stage - 2:
# put RPN and ROI Pooling here, i.e.the last of stage 3
if is_train:
body, rois, rpn_cls_loss, rpn_bbox_loss = rpn(body, num_class=num_class, num_anchor=num_anchor, is_train=True)
else:
body, rpn_roi = rpn(body, num_class=num_class, num_anchor=num_anchor, is_train=False)
def generator(ngf, nc, no_bias=True, fix_gamma=True, eps=1e-5 + 1e-12, z_dim=100, activation='sigmoid'):
'''The genrator is a CNN which takes 100 dimensional embedding as input
and reconstructs the input image given to the encoder
'''
BatchNorm = mx.sym.BatchNorm
rand = mx.sym.Variable('rand')
rand = mx.sym.Reshape(rand, shape=(-1, z_dim, 1, 1))
g1 = mx.sym.Deconvolution(rand, name='gen1', kernel=(5,5), stride=(2,2),target_shape=(2,2), num_filter=ngf*8, no_bias=no_bias)
gbn1 = BatchNorm(g1, name='genbn1', fix_gamma=fix_gamma, eps=eps)
gact1 = mx.sym.Activation(gbn1, name="genact1", act_type="relu")
g2 = mx.sym.Deconvolution(gact1, name='gen2', kernel=(5,5), stride=(2,2),target_shape=(4,4), num_filter=ngf*4, no_bias=no_bias)
gbn2 = BatchNorm(g2, name='genbn2', fix_gamma=fix_gamma, eps=eps)
gact2 = mx.sym.Activation(gbn2, name='genact2', act_type='relu')
g3 = mx.sym.Deconvolution(gact2, name='gen3', kernel=(5,5), stride=(2,2), target_shape=(8,8), num_filter=ngf*2, no_bias=no_bias)
gbn3 = BatchNorm(g3, name='genbn3', fix_gamma=fix_gamma, eps=eps)
gact3 = mx.sym.Activation(gbn3, name='genact3', act_type='relu')
g4 = mx.sym.Deconvolution(gact3, name='gen4', kernel=(5,5), stride=(2,2), target_shape=(16,16), num_filter=ngf, no_bias=no_bias)
gbn4 = BatchNorm(g4, name='genbn4', fix_gamma=fix_gamma, eps=eps)
gact4 = mx.sym.Activation(gbn4, name='genact4', act_type='relu')
g5 = mx.sym.Deconvolution(gact4, name='gen5', kernel=(5,5), stride=(2,2), target_shape=(32,32), num_filter=nc, no_bias=no_bias)
gout = mx.sym.Activation(g5, name='genact5', act_type=activation)
Base name of the operators
workspace : int
Workspace used in convolution operator
"""
if bottle_neck:
# the same as https://github.com/facebook/fb.resnet.torch#notes, a bit difference with origin paper
bn1 = mx.sym.BatchNorm(data=data, fix_gamma=False, eps=2e-5, momentum=bn_mom, name=name + '_bn1')
act1 = mx.sym.Activation(data=bn1, act_type='relu', name=name + '_relu1')
conv1 = mx.sym.Convolution(data=act1, num_filter=int(num_filter*0.25), kernel=(1,1), stride=(1,1), pad=(0,0),
no_bias=True, workspace=workspace, name=name + '_conv1')
bn2 = mx.sym.BatchNorm(data=conv1, fix_gamma=False, eps=2e-5, momentum=bn_mom, name=name + '_bn2')
act2 = mx.sym.Activation(data=bn2, act_type='relu', name=name + '_relu2')
conv2 = mx.sym.Convolution(data=act2, num_filter=int(num_filter*0.25), kernel=(3,3), stride=stride, pad=(1,1),
no_bias=True, workspace=workspace, name=name + '_conv2')
bn3 = mx.sym.BatchNorm(data=conv2, fix_gamma=False, eps=2e-5, momentum=bn_mom, name=name + '_bn3')
act3 = mx.sym.Activation(data=bn3, act_type='relu', name=name + '_relu3')
conv3 = mx.sym.Convolution(data=act3, num_filter=num_filter, kernel=(1,1), stride=(1,1), pad=(0,0), no_bias=True,
workspace=workspace, name=name + '_conv3')
if dim_match:
shortcut = data
else:
shortcut = mx.sym.Convolution(data=act1, num_filter=num_filter, kernel=(1,1), stride=stride, no_bias=True,
workspace=workspace, name=name+'_sc')
if memonger:
shortcut._set_attr(mirror_stage='True')
return conv3 + shortcut
else:
bn1 = mx.sym.BatchNorm(data=data, fix_gamma=False, momentum=bn_mom, eps=2e-5, name=name + '_bn1')
act1 = mx.sym.Activation(data=bn1, act_type='relu', name=name + '_relu1')
conv1 = mx.sym.Convolution(data=act1, num_filter=num_filter, kernel=(3,3), stride=stride, pad=(1,1),
no_bias=True, workspace=workspace, name=name + '_conv1')
bn2 = mx.sym.BatchNorm(data=conv1, fix_gamma=False, momentum=bn_mom, eps=2e-5, name=name + '_bn2')
True means channel number between input and output is the same, otherwise means differ
name : str
Base name of the operators
workspace : int
Workspace used in convolution operator
"""
if bottle_neck:
# the same as https://github.com/facebook/fb.resnet.torch#notes, a bit difference with origin paper
bn1 = mx.sym.BatchNorm(data=data, fix_gamma=False, eps=2e-5, momentum=bn_mom, name=name + '_bn1')
act1 = mx.sym.Activation(data=bn1, act_type='relu', name=name + '_relu1')
weight = mx.symbol.Variable(name=name + '_conv1_weight', dtype=np.float32)
weight = mx.symbol.Cast(data=weight, dtype=np.float16)
conv1 = mx.sym.Convolution(data=act1, weight=weight, num_filter=int(num_filter*0.25), kernel=(1,1), stride=(1,1), pad=(0,0),
no_bias=True, workspace=workspace, name=name + '_conv1')
bn2 = mx.sym.BatchNorm(data=conv1, fix_gamma=False, eps=2e-5, momentum=bn_mom, name=name + '_bn2')
act2 = mx.sym.Activation(data=bn2, act_type='relu', name=name + '_relu2')
weight = mx.symbol.Variable(name=name + '_conv2_weight', dtype=np.float32)
weight = mx.symbol.Cast(data=weight, dtype=np.float16)
conv2 = mx.sym.Convolution(data=act2, weight=weight, num_filter=int(num_filter*0.25), kernel=(3,3), stride=stride, pad=(1,1),
no_bias=True, workspace=workspace, name=name + '_conv2')
bn3 = mx.sym.BatchNorm(data=conv2, fix_gamma=False, eps=2e-5, momentum=bn_mom, name=name + '_bn3')
act3 = mx.sym.Activation(data=bn3, act_type='relu', name=name + '_relu3')
weight = mx.symbol.Variable(name=name + '_conv3_weight', dtype=np.float32)
weight = mx.symbol.Cast(data=weight, dtype=np.float16)
conv3 = mx.sym.Convolution(data=act3, weight=weight, num_filter=num_filter, kernel=(1,1), stride=(1,1), pad=(0,0), no_bias=True,
workspace=workspace, name=name + '_conv3')
if dim_match:
shortcut = data
else:
weight = mx.symbol.Variable(name=name + '_sc_weight', dtype=np.float32)
weight = mx.symbol.Cast(data=weight, dtype=np.float16)
shortcut = mx.sym.Convolution(data=act1, weight=weight, num_filter=num_filter, kernel=(1,1), stride=stride, no_bias=True,
def build_graph():
data = mx.sym.var('data')
data = mx.sym.flatten(data=data)
fc1 = mx.sym.FullyConnected(data=data, num_hidden=128)
act1 = mx.sym.Activation(data=fc1, act_type="relu")
fc2 = mx.sym.FullyConnected(data=act1, num_hidden=64)
act2 = mx.sym.Activation(data=fc2, act_type="relu")
fc3 = mx.sym.FullyConnected(data=act2, num_hidden=10)
return mx.sym.SoftmaxOutput(data=fc3, name='softmax')
def _lstm(num_hidden, indata, prev_state, param, seqidx, layeridx):
"""LSTM Cell symbol"""
i2h = mx.sym.FullyConnected(data=indata,
weight=param.i2h_weight,
bias=param.i2h_bias,
num_hidden=num_hidden * 4,
name="t%d_l%d_i2h" % (seqidx, layeridx))
h2h = mx.sym.FullyConnected(data=prev_state.h,
weight=param.h2h_weight,
bias=param.h2h_bias,
num_hidden=num_hidden * 4,
name="t%d_l%d_h2h" % (seqidx, layeridx))
gates = i2h + h2h
slice_gates = mx.sym.SliceChannel(gates, num_outputs=4,
name="t%d_l%d_slice" % (seqidx, layeridx))
in_gate = mx.sym.Activation(slice_gates[0], act_type="sigmoid")
in_transform = mx.sym.Activation(slice_gates[1], act_type="tanh")
forget_gate = mx.sym.Activation(slice_gates[2], act_type="sigmoid")
out_gate = mx.sym.Activation(slice_gates[3], act_type="sigmoid")
next_c = (forget_gate * prev_state.c) + (in_gate * in_transform)
next_h = out_gate * mx.sym.Activation(next_c, act_type="tanh")
return LSTMState(c=next_c, h=next_h)
x_layer2=mx.sym.Flatten(data=x_layer2)
x_layer2_mix=mx.sym.Dropout(data=x_layer2,p=0.2)
x_layer2_mix=mx.sym.FullyConnected(data=x_layer2_mix,num_hidden=stage_num[1])
x_layer2_mix=mx.sym.Activation(data=x_layer2_mix,act_type='relu')
feat_a_s2_pre=s_layer2*x_layer2
delta_s2=mx.sym.FullyConnected(data=feat_a_s2_pre,num_hidden=1,name='delta_s2')
delta_s2=mx.sym.Activation(data=delta_s2,act_type='tanh')
feat_a_s2=s_layer2_mix*x_layer2_mix
feat_a_s2=mx.sym.FullyConnected(data=feat_a_s2,num_hidden=2*stage_num[1])
feat_a_s2=mx.sym.Activation(data=feat_a_s2,act_type='relu')
pred_a_s2=mx.sym.FullyConnected(data=feat_a_s2,num_hidden=stage_num[1],name='pred_age_stag2')
pred_a_s2=mx.sym.Activation(data=pred_a_s2,act_type='relu')
local_s2=mx.sym.FullyConnected(data=feat_a_s2,num_hidden=stage_num[1],name='local_delta_stage2')
local_s2=mx.sym.Activation(data=local_s2,act_type='tanh')
#-------------------------------------------------------------------------------------------------------------
s_layer1=mx.sym.Convolution(data=s_layer1,num_filter=10,kernel=(1,1))
s_layer1=mx.sym.Activation(data=s_layer1,act_type='relu')
s_layer1=mx.sym.Pooling(data=s_layer1, kernel=(8, 8),stride=(8,8), pool_type='max', name='pool9')
s_layer1=mx.sym.Flatten(data=s_layer1)
s_layer1_mix=mx.sym.Dropout(data=s_layer1,p=0.2)
s_layer1_mix=mx.sym.FullyConnected(data=s_layer1_mix,num_hidden=stage_num[2])
s_layer1_mix=mx.sym.Activation(data=s_layer1_mix,act_type='relu')
x_layer1=mx.sym.Convolution(data=x_layer1,num_filter=10,kernel=(1,1))
x_layer1=mx.sym.Activation(data=x_layer1,act_type='relu')
x_layer1=mx.sym.Pooling(data=x_layer1, kernel=(8, 8),stride=(8,8), pool_type='avg', name='pool10')
x_layer1=mx.sym.Flatten(data=x_layer1)
x_layer1_mix=mx.sym.Dropout(data=x_layer1,p=0.2)
x_layer1_mix=mx.sym.FullyConnected(data=x_layer1_mix,num_hidden=stage_num[2])
x_layer1_mix=mx.sym.Activation(data=x_layer1_mix,act_type='relu')
def get_rpn(self, conv_feat, num_anchors):
conv_feat = mx.sym.Cast(data=conv_feat, dtype=np.float32)
rpn_conv = mx.sym.Convolution(
data=conv_feat, kernel=(3, 3), pad=(1, 1), num_filter=512, name="rpn_conv_3x3")
rpn_relu = mx.sym.Activation(data=rpn_conv, act_type="relu", name="rpn_relu")
rpn_cls_score = mx.sym.Convolution(
data=rpn_relu, kernel=(1, 1), pad=(0, 0), num_filter=2 * num_anchors, name="rpn_cls_score")
rpn_bbox_pred = mx.sym.Convolution(
data=rpn_relu, kernel=(1, 1), pad=(0, 0), num_filter=4 * num_anchors, name="rpn_bbox_pred")
return rpn_cls_score, rpn_bbox_pred