Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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')
act2 = mx.sym.Activation(data=bn2, act_type='relu', name=name + '_relu2')
conv2 = mx.sym.Convolution(data=act2, num_filter=num_filter, kernel=(3,3), stride=(1,1), pad=(1,1),
no_bias=True, workspace=workspace, name=name + '_conv2')
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 conv2 + shortcut
def resnetc4(self, data, fp16=False):
units = self.units
filter_list = self.filter_list
bn_mom = self.momentum
workspace = self.workspace
num_stage = len(units)
memonger = False
data = mx.sym.BatchNorm(data=data, fix_gamma=True, eps=2e-5, use_global_stats=True, 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)
if fp16:
body = mx.sym.Cast(data=body, dtype=np.float16)
body = mx.sym.BatchNorm(data=body, fix_gamma=False, eps=2e-5, use_global_stats=True, 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-1):
body = self.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), workspace=workspace,
memonger=memonger,fix_bn=(i==0))
for j in range(units[i]-1):
body = self.residual_unit(body, filter_list[i+1], (1,1), True, name='stage%d_unit%d' % (i + 1, j + 2),
workspace=workspace, memonger=memonger,fix_bn=(i==0))
return body
elif data_type == 'float16':
data = mx.sym.Cast(data=data, dtype=np.float16)
conv1_1 = mx.sym.Convolution(data=data, num_filter=filter_list[0], kernel=(3, 3), stride=(1, 1), pad=(1, 1),
no_bias=True, name="conv1_1", workspace=workspace)
conv1_1 = mx.sym.BatchNorm(data=conv1_1, fix_gamma=False, eps=eps, momentum=bn_mom, name='bn1_1')
conv1_1 = mx.sym.Activation(data=conv1_1, act_type='relu', name='relu1_1')
conv1_2 = mx.sym.Convolution(data=conv1_1, num_filter=filter_list[0], kernel=(3, 3), stride=(1, 1), pad=(1, 1),
no_bias=True, name="conv1_2", workspace=workspace)
conv1_2 = mx.sym.BatchNorm(data=conv1_2, fix_gamma=False, eps=eps, momentum=bn_mom, name='bn1_2')
conv1_2 = mx.sym.Activation(data=conv1_2, act_type='relu', name='relu1_2')
conv1_3 = mx.sym.Convolution(data=conv1_2, num_filter=filter_list[0], kernel=(3, 3), stride=(2, 2), pad=(1, 1),
no_bias=True, name="conv1_3", workspace=workspace)
conv1_3 = mx.sym.BatchNorm(data=conv1_3, fix_gamma=False, eps=eps, momentum=bn_mom, name='bn1_3')
conv1_3 = mx.sym.Activation(data=conv1_3, act_type='relu', name='relu1_3')
body = mx.symbol.Pooling(data=conv1_3, kernel=(3, 3), stride=(2, 2), pad=(1, 1), pool_type='max')
for i in range(num_stage):
body = xresidual_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, num_group=num_group,
bn_mom=bn_mom, workspace=workspace, memonger=memonger)
for j in range(units[i] - 1):
body = xresidual_unit(body, filter_list[i + 1], (1, 1), True, name='stage%d_unit%d' % (i + 1, j + 2),
bottle_neck=bottle_neck, num_group=num_group, bn_mom=bn_mom,
workspace=workspace, memonger=memonger)
pool1 = mx.symbol.Pooling(data=body, global_pool=True, kernel=(7, 7), pool_type='avg', name='pool1')
flat = mx.symbol.Flatten(data=pool1)
fc1 = mx.symbol.FullyConnected(data=flat, num_hidden=num_classes, name='fc1')
if data_type == 'float16':
fc1 = mx.sym.Cast(data=fc1, dtype=np.float32)
def inception(data, group_name,
filter_0, filters_1, filters_2, filter_p, filter_out, stride,
use_global, n_curr_ch, final_bn=False):
"""
"""
incep_name = group_name + '/incep'
group_name_0 = incep_name + '/0'
group_name_1 = incep_name + '/1'
group_name_2 = incep_name + '/2'
incep_bn = mx.sym.BatchNorm(name=incep_name+'/bn', data=data,
use_global_stats=use_global, fix_gamma=False, eps=1e-05)
incep_relu = mx.sym.Activation(name=incep_name+'/relu', data=incep_bn, act_type='relu')
incep_0 = conv_bn_relu(data=incep_relu, group_name=group_name_0,
num_filter=filter_0, kernel=(1,1), pad=(0,0), stride=stride, use_global=use_global)
incep_1_reduce = conv_bn_relu(data=incep_relu, group_name=group_name_1+'_reduce',
num_filter=filters_1[0], kernel=(1,1), pad=(0,0), stride=stride, use_global=use_global)
incep_1_0 = conv_bn_relu(data=incep_1_reduce, group_name=group_name_1+'_0',
num_filter=filters_1[1], kernel=(3,3), pad=(1,1), stride=(1,1), use_global=use_global)
incep_2_reduce = conv_bn_relu(data=incep_relu, group_name=group_name_2+'_reduce',
num_filter=filters_2[0], kernel=(1,1), pad=(0,0), stride=stride, use_global=use_global)
incep_2_0 = conv_bn_relu(data=incep_2_reduce, group_name=group_name_2+'_0',
num_filter=filters_2[1], kernel=(3,3), pad=(1,1), stride=(1,1), use_global=use_global)
incep_2_1 = conv_bn_relu(data=incep_2_0, group_name=group_name_2+'_1',
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
conv1 = mx.sym.Convolution(data=data, num_filter=int(num_filter*0.5), kernel=(1,1), stride=(1,1), pad=(0,0),
no_bias=True, workspace=workspace, name=name + '_conv1')
bn1 = mx.sym.BatchNorm(data=conv1, fix_gamma=False, eps=2e-5, momentum=bn_mom, name=name + '_bn1')
act1 = mx.sym.Activation(data=bn1, act_type='relu', name=name + '_relu1')
conv2 = mx.sym.Convolution(data=act1, num_filter=int(num_filter*0.5), num_group=num_group, kernel=(3,3), stride=stride, pad=(1,1),
no_bias=True, workspace=workspace, name=name + '_conv2')
bn2 = mx.sym.BatchNorm(data=conv2, fix_gamma=False, eps=2e-5, momentum=bn_mom, name=name + '_bn2')
act2 = mx.sym.Activation(data=bn2, act_type='relu', name=name + '_relu2')
conv3 = mx.sym.Convolution(data=act2, num_filter=num_filter, kernel=(1,1), stride=(1,1), pad=(0,0), no_bias=True,
workspace=workspace, name=name + '_conv3')
bn3 = mx.sym.BatchNorm(data=conv3, fix_gamma=False, eps=2e-5, momentum=bn_mom, name=name + '_bn3')
if dim_match:
shortcut = data
else:
shortcut_conv = mx.sym.Convolution(data=data, num_filter=num_filter, kernel=(1,1), stride=stride, no_bias=True,
workspace=workspace, name=name+'_sc')
shortcut = mx.sym.BatchNorm(data=shortcut_conv, fix_gamma=False, eps=2e-5, momentum=bn_mom, name=name + '_sc_bn')
if memonger:
shortcut._set_attr(mirror_stage='True')
'''
#se end
squeeze = mx.sym.Pooling(data=bn4, global_pool=True, kernel=(7, 7), pool_type='avg', name=name + '_squeeze')
squeeze = mx.symbol.Flatten(data=squeeze, name=name + '_flatten')
excitation = mx.symbol.FullyConnected(data=squeeze, num_hidden=int(num_filter*ratio), name=name + '_excitation1')
excitation = mx.sym.Activation(data=excitation, act_type='relu', name=name + '_excitation1_relu')
excitation = mx.symbol.FullyConnected(data=excitation, num_hidden=num_filter, name=name + '_excitation2')
excitation = mx.sym.Activation(data=excitation, act_type='sigmoid', name=name + '_excitation2_sigmoid')
bn4 = mx.symbol.broadcast_mul(bn4, mx.symbol.reshape(data=excitation, shape=(-1, num_filter, 1, 1)))
if dim_match:
shortcut = data
else:
conv1sc = Conv(data=data, num_filter=num_filter, kernel=(1,1), stride=stride, no_bias=True,
workspace=workspace, name=name+'_conv1sc')
shortcut = mx.sym.BatchNorm(data=conv1sc, fix_gamma=False, eps=2e-5, momentum=bn_mom, name=name + '_sc')
if memonger:
shortcut._set_attr(mirror_stage='True')
return bn4 + shortcut
else:
bn1 = mx.sym.BatchNorm(data=data, fix_gamma=False, eps=2e-5, momentum=bn_mom, name=name + '_bn1')
conv1 = Conv(data=bn1, num_filter=num_filter, kernel=(3,3), stride=(1,1), pad=(1,1),
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')
act1 = Act(data=bn2, act_type=act_type, name=name + '_relu1')
conv2 = Conv(data=act1, num_filter=num_filter, 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')
if use_se:
#se begin
'''
body = mx.sym.Pooling(data=bn3, global_pool=True, kernel=(7, 7), pool_type='avg', name=name+'_se_pool1')
offset=offset,
num_filter=num_filter, pad=(2, 2), kernel=(3, 3),
num_deformable_group=4,
stride=(1, 1), dilate=(2, 2), no_bias=True, num_group=num_group)
if fix_bn or self.fix_bn:
bn2 = mx.sym.BatchNorm(data=conv2, fix_gamma=False, eps=2e-5, use_global_stats=True, name=name + '_bn2')
else:
bn2 = mx.sym.BatchNorm(data=conv2, fix_gamma=False, eps=2e-5, momentum=self.momentum, name=name + '_bn2')
act2 = mx.sym.Activation(data=bn2, act_type='relu', name=name + '_relu2')
conv3 = mx.sym.Convolution(data=act2, num_filter=num_filter, kernel=(1,1), stride=(1,1), pad=(0,0), no_bias=True,
workspace=workspace, name=name + '_conv3')
if fix_bn or self.fix_bn:
bn3 = mx.sym.BatchNorm(data=conv3, fix_gamma=False, eps=2e-5, use_global_stats=True, name=name + '_bn3')
else:
bn3 = mx.sym.BatchNorm(data=conv3, fix_gamma=False, eps=2e-5, momentum=self.momentum, name=name + '_bn3')
if dim_match:
shortcut = data
else:
shortcut_conv = mx.sym.Convolution(data=data, num_filter=num_filter, kernel=(1,1), stride=stride, no_bias=True,
workspace=workspace, name=name+'_sc')
if fix_bn or self.fix_bn:
shortcut = mx.sym.BatchNorm(data=shortcut_conv, fix_gamma=False, eps=2e-5, momentum=self.momentum, name=name + '_sc_bn')
else:
shortcut = mx.sym.BatchNorm(data=shortcut_conv, fix_gamma=False, eps=2e-5, use_global_stats=True, name=name + '_sc_bn')
if memonger:
shortcut._set_attr(mirror_stage='True')
bnf : int
Bottle neck channels factor with regard to num_filter
stride : tuple
Stride used in convolution
dim_match : Boolean
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
"""
bn_mom = kwargs.get('bn_mom', 0.9)
workspace = kwargs.get('workspace', 256)
memonger = kwargs.get('memonger', False)
#print('in unit3')
bn1 = mx.sym.BatchNorm(data=data, fix_gamma=False, eps=2e-5, momentum=bn_mom, name=name + '_bn1')
conv1 = Conv(data=bn1, num_filter=num_filter, kernel=(3,3), stride=(1,1), pad=(1,1),
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')
act1 = Act(data=bn2, act_type='relu', name=name + '_relu1')
conv2 = Conv(data=act1, num_filter=num_filter, 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')
if dim_match:
shortcut = data
else:
conv1sc = Conv(data=data, num_filter=num_filter, kernel=(1,1), stride=stride, no_bias=True,
workspace=workspace, name=name+'_conv1sc')
shortcut = mx.sym.BatchNorm(data=conv1sc, fix_gamma=False, momentum=bn_mom, eps=2e-5, name=name + '_sc')
if memonger:
shortcut._set_attr(mirror_stage='True')
def get_generator(prefix, im_hw):
data = mx.sym.Variable("%s_data" % prefix)
conv1_1 = mx.sym.Convolution(data, num_filter=48, kernel=(5, 5), pad=(2, 2), no_bias=False)
conv1_1 = mx.sym.BatchNorm(conv1_1, fix_gamma=False)
conv1_1 = mx.sym.LeakyReLU(conv1_1, act_type="leaky")
conv2_1 = mx.sym.Convolution(conv1_1, num_filter=32, kernel=(5, 5), pad=(2, 2), no_bias=False)
conv2_1 = mx.sym.BatchNorm(conv2_1, fix_gamma=False)
conv2_1 = mx.sym.LeakyReLU(conv2_1, act_type="leaky")
conv3_1 = mx.sym.Convolution(conv2_1, num_filter=64, kernel=(3, 3), pad=(1, 1), no_bias=False)
conv3_1 = mx.sym.BatchNorm(conv3_1, fix_gamma=False)
conv3_1 = mx.sym.LeakyReLU(conv3_1, act_type="leaky")
conv4_1 = mx.sym.Convolution(conv3_1, num_filter=32, kernel=(5, 5), pad=(2, 2), no_bias=False)
conv4_1 = mx.sym.BatchNorm(conv4_1, fix_gamma=False)
conv4_1 = mx.sym.LeakyReLU(conv4_1, act_type="leaky")
conv5_1 = mx.sym.Convolution(conv4_1, num_filter=48, kernel=(5, 5), pad=(2, 2), no_bias=False)
conv5_1 = mx.sym.BatchNorm(conv5_1, fix_gamma=False)
conv5_1 = mx.sym.LeakyReLU(conv5_1, act_type="leaky")
conv6_1 = mx.sym.Convolution(conv5_1, num_filter=32, kernel=(5, 5), pad=(2, 2), no_bias=True)
conv6_1 = mx.sym.BatchNorm(conv6_1, fix_gamma=False)
conv6_1 = mx.sym.LeakyReLU(conv6_1, act_type="leaky")
out = mx.sym.Convolution(conv6_1, num_filter=3, kernel=(3, 3), pad=(1, 1), no_bias=True)
out = mx.sym.BatchNorm(out, fix_gamma=False)
bn2 = mx.sym.BatchNorm(data=conv1, fix_gamma=False, eps=2e-5, use_global_stats=True, name=name + '_bn2')
else:
bn2 = mx.sym.BatchNorm(data=conv1, fix_gamma=False, eps=2e-5, momentum=self.momentum, name=name + '_bn2')
act2 = mx.sym.Activation(data=bn2, act_type='relu', name=name + '_relu2')
offset = mx.symbol.Convolution(name=name + '_offset', data=act2,
num_filter=72, pad=(2, 2), kernel=(3, 3), stride=(1, 1),
dilate=(2, 2), cudnn_off=True)
conv2 = mx.contrib.symbol.DeformableConvolution(name=name + '_conv2', data=act2,
offset=offset,
num_filter=512, pad=(2, 2), kernel=(3, 3),
num_deformable_group=4,
stride=(1, 1), dilate=(2, 2), no_bias=True)
if self.fix_bn:
bn3 = mx.sym.BatchNorm(data=conv2, fix_gamma=False, eps=2e-5, use_global_stats=True, name=name + '_bn3')
else:
bn3 = mx.sym.BatchNorm(data=conv2, fix_gamma=False, eps=2e-5, momentum=self.momentum, 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