def getsymbol(num_classes=136): # define alexnet data = mxy.Variable(name="data") label = mxy.Variable(name="label") # group 1 conv1_1 = mxy.Convolution(data=data, kernel=(3, 3), pad=(1, 1), num_filter=64, name="conv1_1") relu1_1 = mxy.Activation(data=conv1_1, act_type="relu", name="relu1_1") pool1 = mxy.Pooling(data=relu1_1, pool_type="max", kernel=(2, 2), stride=(2,2), name="pool1") # group 2 conv2_1 = mxy.Convolution(data=pool1, kernel=(3, 3), pad=(1, 1), num_filter=128, name="conv2_1") relu2_1 = mxy.Activation(data=conv2_1, act_type="relu", name="relu2_1") pool2 = mxy.Pooling(data=relu2_1, pool_type="max", kernel=(2, 2), stride=(2,2), name="pool2") # group 3 conv3_1 = mxy.Convolution(data=pool2, kernel=(3, 3), pad=(1, 1), num_filter=256, name="conv3_1") relu3_1 = mxy.Activation(data=conv3_1, act_type="relu", name="relu3_1") conv3_2 = mxy.Convolution(data=relu3_1, kernel=(3, 3), pad=(1, 1), num_filter=256, name="conv3_2") relu3_2 = mxy.Activation(data=conv3_2, act_type="relu", name="relu3_2") pool3 = mxy.Pooling(data=relu3_2, pool_type="max", kernel=(2, 2), stride=(2,2), name="pool3") # group 4 conv4_1 = mxy.Convolution(data=pool3, kernel=(3, 3), pad=(1, 1), num_filter=512, name="conv4_1") relu4_1 = mxy.Activation(data=conv4_1, act_type="relu", name="relu4_1") conv4_2 = mxy.Convolution(data=relu4_1, kernel=(3, 3), pad=(1, 1), num_filter=512, name="conv4_2") relu4_2 = mxy.Activation(data=conv4_2, act_type="relu", name="relu4_2") pool4 = mxy.Pooling(data=relu4_2, pool_type="max", kernel=(2, 2), stride=(2,2), name="pool4") # group 5 conv5_1 = mxy.Convolution(data=pool4, kernel=(3, 3), pad=(1, 1), num_filter=512, name="conv5_1") relu5_1 = mxy.Activation(data=conv5_1, act_type="relu", name="relu5_1") conv5_2 = mxy.Convolution(data=relu5_1, kernel=(3, 3), pad=(1, 1), num_filter=512, name="conv5_2") relu5_2 = mxy.Activation(data=conv5_2, act_type="relu", name="conv1_2") pool5 = mxy.Pooling(data=relu5_2, pool_type="max", kernel=(2, 2), stride=(2,2), name="pool5") # group 6 flatten = mxy.Flatten(data=pool5, name="flatten") fc6 = mxy.FullyConnected(data=flatten, num_hidden=4096, name="fc6") relu6 = mxy.Activation(data=fc6, act_type="relu", name="relu6") drop6 = mxy.Dropout(data=relu6, p=0.5, name="drop6") # group 7 fc7 = mxy.FullyConnected(data=drop6, num_hidden=4096, name="fc7") relu7 = mxy.Activation(data=fc7, act_type="relu", name="relu7") drop7 = mxy.Dropout(data=relu7, p=0.5, name="drop7") # output fc8 = mxy.FullyConnected(data=drop7, num_hidden=num_classes, name="fc8") loc_loss = mxy.LinearRegressionOutput(data=fc8, label=label, name="loc_loss") #loc_loss_ = mxy.smooth_l1(name="loc_loss_", data=(fc8 - label), scalar=1.0) #loc_loss_ = mxy.smooth_l1(name="loc_loss_", data=fc8, scalar=1.0) #loc_loss = mx.sym.MakeLoss(name='loc_loss', data=loc_loss_) return loc_loss
def add_forward(self, data: sym.Variable): """Add neural network model.""" conv1 = sym.Convolution( data, name='conv1', num_filter=64, kernel=(3, 3), stride=(2, 2)) relu1 = sym.Activation(conv1, act_type='relu') pool1 = sym.Pooling(relu1, pool_type='max', kernel=(3, 3), stride=(2, 2)) fire2 = self._fire_layer('fire2', pool1, s1x1=16, e1x1=64, e3x3=64) fire3 = self._fire_layer('fire3', fire2, s1x1=16, e1x1=64, e3x3=64) pool3 = sym.Pooling(fire3, name='pool3', kernel=(3, 3), stride=(2, 2), pool_type='max') fire4 = self._fire_layer('fire4', pool3, s1x1=32, e1x1=128, e3x3=128) fire5 = self._fire_layer('fire5', fire4, s1x1=32, e1x1=128, e3x3=128) pool5 = sym.Pooling(fire5, name='pool5', kernel=(3, 3), stride=(2, 2), pool_type='max') fire6 = self._fire_layer('fire6', pool5, s1x1=48, e1x1=192, e3x3=192) fire7 = self._fire_layer('fire7', fire6, s1x1=48, e1x1=192, e3x3=192) fire8 = self._fire_layer('fire8', fire7, s1x1=64, e1x1=256, e3x3=256) fire9 = self._fire_layer('fire9', fire8, s1x1=64, e1x1=256, e3x3=256) fire10 = self._fire_layer('fire10', fire9, s1x1=96, e1x1=384, e3x3=384) fire11 = self._fire_layer('fire11', fire10, s1x1=96, e1x1=384, e3x3=384) dropout11 = sym.Dropout(fire11, p=0.1, name='drop11') return sym.Convolution( dropout11, name='conv12', num_filter=NUM_OUT_CHANNELS, kernel=(3, 3), stride=(1, 1), pad=(1, 1))
def InceptionFactoryA(data, num_1x1, num_3x3red, num_3x3, num_d3x3red, num_d3x3, pool, proj, name): # 1x1 c1x1 = ConvFactory(data=data, num_filter=num_1x1, kernel=(1, 1), name=('%s_1x1' % name)) # 3x3 reduce + 3x3 c3x3r = ConvFactory(data=data, num_filter=num_3x3red, kernel=(1, 1), name=('%s_3x3' % name), suffix='_reduce') c3x3 = ConvFactory(data=c3x3r, num_filter=num_3x3, kernel=(3, 3), pad=(1, 1), name=('%s_3x3' % name)) # double 3x3 reduce + double 3x3 cd3x3r = ConvFactory(data=data, num_filter=num_d3x3red, kernel=(1, 1), name=('%s_double_3x3' % name), suffix='_reduce') cd3x3 = ConvFactory(data=cd3x3r, num_filter=num_d3x3, kernel=(3, 3), pad=(1, 1), name=('%s_double_3x3_0' % name)) cd3x3 = ConvFactory(data=cd3x3, num_filter=num_d3x3, kernel=(3, 3), pad=(1, 1), name=('%s_double_3x3_1' % name)) # pool + proj pooling = mxy.Pooling(data=data, kernel=(3, 3), stride=(1, 1), pad=(1, 1), pool_type=pool, name=('%s_pool_%s_pool' % (pool, name))) cproj = ConvFactory(data=pooling, num_filter=proj, kernel=(1, 1), name=('%s_proj' % name)) # concat concat = mxy.Concat(*[c1x1, c3x3, cd3x3, cproj], name='ch_concat_%s_chconcat' % name) return concat
def InceptionFactoryB(data, num_3x3red, num_3x3, num_d3x3red, num_d3x3, name): # 3x3 reduce + 3x3 c3x3r = ConvFactory(data=data, num_filter=num_3x3red, kernel=(1, 1), name=('%s_3x3' % name), suffix='_reduce') c3x3 = ConvFactory(data=c3x3r, num_filter=num_3x3, kernel=(3, 3), pad=(1, 1), stride=(2, 2), name=('%s_3x3' % name)) # double 3x3 reduce + double 3x3 cd3x3r = ConvFactory(data=data, num_filter=num_d3x3red, kernel=(1, 1), name=('%s_double_3x3' % name), suffix='_reduce') cd3x3 = ConvFactory(data=cd3x3r, num_filter=num_d3x3, kernel=(3, 3), pad=(1, 1), stride=(1, 1), name=('%s_double_3x3_0' % name)) cd3x3 = ConvFactory(data=cd3x3, num_filter=num_d3x3, kernel=(3, 3), pad=(1, 1), stride=(2, 2), name=('%s_double_3x3_1' % name)) # pool + proj pooling = mxy.Pooling(data=data, kernel=(3, 3), stride=(2, 2), pad=(1, 1), pool_type="max", name=('max_pool_%s_pool' % name)) # concat concat = mxy.Concat(*[c3x3, cd3x3, pooling], name='ch_concat_%s_chconcat' % name) return concat
def DownsampleFactory(data, ch_3x3, name, attr): # conv 3x3 conv = ConvFactory(data=data, name=name + '_conv', kernel=(3, 3), stride=(2, 2), num_filter=ch_3x3, pad=(1, 1), attr=attr) # pool pool = mxy.Pooling(data=data, name=name + '_pool', kernel=(3, 3), stride=(2, 2), pad=(1, 1), pool_type='max', attr=attr) # concat concat = mxy.Concat(*[conv, pool], name=name + '_ch_concat') return concat
def SEModule(data, num_filter, name): body = sym.Pooling(data=data, global_pool=True, kernel=(7, 7), pool_type='avg', name=name + '_se_pool1') body = sym.Convolution(data=body, num_filter=num_filter // 8, kernel=(1, 1), stride=(1, 1), pad=(0, 0), name=name + "_se_conv1") body = Act(data=body, act_type="prelu", name=name + '_se_relu1') body = sym.Convolution(data=body, num_filter=num_filter, kernel=(1, 1), stride=(1, 1), pad=(0, 0), name=name + "_se_conv2") body = sym.Activation(data=body, act_type='sigmoid', name=name + "_se_sigmoid") data = sym.broadcast_mul(data, body) return data
def get_symbol(num_classes=136, image_shape=(3, 224, 224), **kwargs): (nchannel, height, width) = image_shape # attr = {'force_mirroring': 'true'} attr = {} # data data = mxy.Variable(name="data") label = mxy.Variable(name="label") if height <= 28: # a simper version conv1 = ConvFactory(data=data, kernel=(3, 3), pad=(1, 1), name="1", num_filter=96, attr=attr) in3a = SimpleFactory(conv1, 32, 32, 'in3a', attr) in3b = SimpleFactory(in3a, 32, 48, 'in3b', attr) in3c = DownsampleFactory(in3b, 80, 'in3c', attr) in4a = SimpleFactory(in3c, 112, 48, 'in4a', attr) in4b = SimpleFactory(in4a, 96, 64, 'in4b', attr) in4c = SimpleFactory(in4b, 80, 80, 'in4c', attr) in4d = SimpleFactory(in4c, 48, 96, 'in4d', attr) in4e = DownsampleFactory(in4d, 96, 'in4e', attr) in5a = SimpleFactory(in4e, 176, 160, 'in5a', attr) in5b = SimpleFactory(in5a, 176, 160, 'in5b', attr) pool = mxy.Pooling(data=in5b, pool_type="avg", kernel=(7, 7), name="global_pool", attr=attr) else: # stage 1 conv1 = ConvFactory(data=data, num_filter=64, kernel=(7, 7), stride=(2, 2), pad=(3, 3), name='1') pool1 = mxy.Pooling(data=conv1, kernel=(3, 3), stride=(2, 2), name='pool_1', pool_type='max') # stage 2 conv2red = ConvFactory(data=pool1, num_filter=64, kernel=(1, 1), stride=(1, 1), name='2_red') conv2 = ConvFactory(data=conv2red, num_filter=192, kernel=(3, 3), stride=(1, 1), pad=(1, 1), name='2') pool2 = mxy.Pooling(data=conv2, kernel=(3, 3), stride=(2, 2), name='pool_2', pool_type='max') # stage 2 in3a = InceptionFactoryA(pool2, 64, 64, 64, 64, 96, "avg", 32, '3a') in3b = InceptionFactoryA(in3a, 64, 64, 96, 64, 96, "avg", 64, '3b') in3c = InceptionFactoryB(in3b, 128, 160, 64, 96, '3c') # stage 3 in4a = InceptionFactoryA(in3c, 224, 64, 96, 96, 128, "avg", 128, '4a') in4b = InceptionFactoryA(in4a, 192, 96, 128, 96, 128, "avg", 128, '4b') in4c = InceptionFactoryA(in4b, 160, 128, 160, 128, 160, "avg", 128, '4c') in4d = InceptionFactoryA(in4c, 96, 128, 192, 160, 192, "avg", 128, '4d') in4e = InceptionFactoryB(in4d, 128, 192, 192, 256, '4e') # stage 4 in5a = InceptionFactoryA(in4e, 352, 192, 320, 160, 224, "avg", 128, '5a') in5b = InceptionFactoryA(in5a, 352, 192, 320, 192, 224, "max", 128, '5b') # global avg pooling pool = mxy.Pooling(data=in5b, kernel=(7, 7), stride=(1, 1), name="global_pool", pool_type='avg') # linear classifier flatten = mxy.Flatten(data=pool) fc1 = mxy.FullyConnected(data=flatten, num_hidden=num_classes) loc_loss = mxy.LinearRegressionOutput(data=fc1, label=label, name="loc_loss") return loc_loss
def conv_bn_relu_pool_siamese(input_a, input_b, kernel, num_filter, pad, stride, name_postfix, use_pooling=False, p_kernel=None, p_stride=None, use_batch_norm=True): conv_weight = mxs.Variable(name='conv' + name_postfix + '_weight') conv_bias = mxs.Variable(name='conv' + name_postfix + '_bias') conv_a = mxs.Convolution(data=input_a, kernel=kernel, num_filter=num_filter, pad=pad, stride=stride, name='conv' + name_postfix + "_a", weight=conv_weight, bias=conv_bias) conv_b = mxs.Convolution(data=input_b, kernel=kernel, num_filter=num_filter, pad=pad, stride=stride, name='conv' + name_postfix + "_b", weight=conv_weight, bias=conv_bias) if use_batch_norm: bn_gamma = mxs.Variable(name='bn' + name_postfix + '_gamma') bn_beta = mxs.Variable(name='bn' + name_postfix + '_beta') bn_moving_mean = mxs.Variable(name='bn' + name_postfix + '_moving_mean') bn_moving_var = mxs.Variable(name='bn' + name_postfix + '_moving_var') batch_norm_a = mxs.BatchNorm(data=conv_a, name='bn' + name_postfix + '_a', gamma=bn_gamma, beta=bn_beta, moving_mean=bn_moving_mean, moving_var=bn_moving_var) batch_norm_b = mxs.BatchNorm(data=conv_b, name='bn' + name_postfix + '_b', gamma=bn_gamma, beta=bn_beta, moving_mean=bn_moving_mean, moving_var=bn_moving_var) else: batch_norm_a = conv_a batch_norm_b = conv_b relu_a = mxs.relu(data=batch_norm_a, name='relu' + name_postfix + '_a') relu_b = mxs.relu(data=batch_norm_b, name='relu' + name_postfix + '_b') if use_pooling: out_a = mxs.Pooling(data=relu_a, kernel=p_kernel, pool_type='max', stride=p_stride, name='pool' + name_postfix + '_a') out_b = mxs.Pooling(data=relu_b, kernel=p_kernel, pool_type='max', stride=p_stride, name='pool' + name_postfix + '_b') else: out_a = relu_a out_b = relu_b return out_a, out_b
def siamese_simp_net(): def conv_bn_relu_pool_siamese(input_a, input_b, kernel, num_filter, pad, stride, name_postfix, use_pooling=False, p_kernel=None, p_stride=None, use_batch_norm=True): conv_weight = mxs.Variable(name='conv' + name_postfix + '_weight') conv_bias = mxs.Variable(name='conv' + name_postfix + '_bias') conv_a = mxs.Convolution(data=input_a, kernel=kernel, num_filter=num_filter, pad=pad, stride=stride, name='conv' + name_postfix + "_a", weight=conv_weight, bias=conv_bias) conv_b = mxs.Convolution(data=input_b, kernel=kernel, num_filter=num_filter, pad=pad, stride=stride, name='conv' + name_postfix + "_b", weight=conv_weight, bias=conv_bias) if use_batch_norm: bn_gamma = mxs.Variable(name='bn' + name_postfix + '_gamma') bn_beta = mxs.Variable(name='bn' + name_postfix + '_beta') bn_moving_mean = mxs.Variable(name='bn' + name_postfix + '_moving_mean') bn_moving_var = mxs.Variable(name='bn' + name_postfix + '_moving_var') batch_norm_a = mxs.BatchNorm(data=conv_a, name='bn' + name_postfix + '_a', gamma=bn_gamma, beta=bn_beta, moving_mean=bn_moving_mean, moving_var=bn_moving_var) batch_norm_b = mxs.BatchNorm(data=conv_b, name='bn' + name_postfix + '_b', gamma=bn_gamma, beta=bn_beta, moving_mean=bn_moving_mean, moving_var=bn_moving_var) else: batch_norm_a = conv_a batch_norm_b = conv_b relu_a = mxs.relu(data=batch_norm_a, name='relu' + name_postfix + '_a') relu_b = mxs.relu(data=batch_norm_b, name='relu' + name_postfix + '_b') if use_pooling: out_a = mxs.Pooling(data=relu_a, kernel=p_kernel, pool_type='max', stride=p_stride, name='pool' + name_postfix + '_a') out_b = mxs.Pooling(data=relu_b, kernel=p_kernel, pool_type='max', stride=p_stride, name='pool' + name_postfix + '_b') else: out_a = relu_a out_b = relu_b return out_a, out_b data_a = mxs.Variable('data_a') data_b = mxs.Variable('data_b') c1_a, c1_b = conv_bn_relu_pool_siamese(data_a, data_b, kernel=(3, 3), num_filter=64, pad=(1, 1), stride=(1, 1), name_postfix='1', use_pooling=False) c1_0_a, c1_0_b = conv_bn_relu_pool_siamese(c1_a, c1_b, kernel=(3, 3), num_filter=32, pad=(1, 1), stride=(1, 1), name_postfix='1_0', use_pooling=False) c2_a, c2_b = conv_bn_relu_pool_siamese(c1_0_a, c1_0_b, kernel=(3, 3), num_filter=32, pad=(1, 1), stride=(1, 1), name_postfix='2', use_pooling=False) c2_1_a, c2_1_b = conv_bn_relu_pool_siamese(c2_a, c2_b, kernel=(3, 3), num_filter=32, pad=(1, 1), stride=(1, 1), name_postfix='2_1', use_pooling=True, p_kernel=(2, 2), p_stride=(2, 2)) c2_2_a, c2_2_b = conv_bn_relu_pool_siamese(c2_1_a, c2_1_b, kernel=(3, 3), num_filter=32, pad=(1, 1), stride=(1, 1), name_postfix='2_2', use_pooling=False) c3_a, c3_b = conv_bn_relu_pool_siamese(c2_2_a, c2_2_b, kernel=(3, 3), num_filter=32, pad=(1, 1), stride=(1, 1), name_postfix='3', use_pooling=False) # conv4 conv4_weight = mxs.Variable(name='conv4_weight') conv4_bias = mxs.Variable(name='conv4_bias') conv4_a = mxs.Convolution(data=c3_a, kernel=(3, 3), num_filter=64, pad=(1, 1), stride=(1, 1), name='conv4_a', weight=conv4_weight, bias=conv4_bias) # xavier conv4_b = mxs.Convolution(data=c3_b, kernel=(3, 3), num_filter=64, pad=(1, 1), stride=(1, 1), name='conv4_b', weight=conv4_weight, bias=conv4_bias) # xavier maxp4_a = mxs.Pooling(data=conv4_a, kernel=(2, 2), pool_type='max', stride=(2, 2), name='pool4_a') maxp4_b = mxs.Pooling(data=conv4_b, kernel=(2, 2), pool_type='max', stride=(2, 2), name='pool4_b') bn4_gamma = mxs.Variable(name='bn4_gamma') bn4_beta = mxs.Variable(name='bn4_beta') bn4_moving_mean = mxs.Variable(name='bn4_moving_mean') bn4_moving_var = mxs.Variable(name='bn4_moving_var') batch_norm_4_a = mxs.BatchNorm(data=maxp4_a, name='bn4_a', gamma=bn4_gamma, beta=bn4_beta, moving_mean=bn4_moving_mean, moving_var=bn4_moving_var) batch_norm_4_b = mxs.BatchNorm(data=maxp4_b, name='bn4_b', gamma=bn4_gamma, beta=bn4_beta, moving_mean=bn4_moving_mean, moving_var=bn4_moving_var) relu4_a = mxs.relu(data=batch_norm_4_a, name='relu4') relu4_b = mxs.relu(data=batch_norm_4_b, name='relu4') c4_1_a, c4_1_b = conv_bn_relu_pool_siamese(relu4_a, relu4_b, kernel=(3, 3), num_filter=64, pad=(1, 1), stride=(1, 1), name_postfix='4_1', use_pooling=False) c4_2_a, c4_2_b = conv_bn_relu_pool_siamese(c4_1_a, c4_1_b, kernel=(3, 3), num_filter=64, pad=(1, 1), stride=(1, 1), name_postfix='4_2', use_pooling=True, p_kernel=(2, 2), p_stride=(2, 2)) c4_0_a, c4_0_b = conv_bn_relu_pool_siamese(c4_2_a, c4_2_b, kernel=(3, 3), num_filter=128, pad=(1, 1), stride=(1, 1), name_postfix='4_0', use_pooling=False) cccp4_a, cccp4_b = conv_bn_relu_pool_siamese(c4_0_a, c4_0_b, kernel=(1, 1), num_filter=256, pad=[], stride=(1, 1), name_postfix='_cccp4', use_pooling=False, use_batch_norm=False) cccp5_a, cccp5_b = conv_bn_relu_pool_siamese(cccp4_a, cccp4_b, kernel=(1, 1), num_filter=64, pad=[], stride=(1, 1), name_postfix='_cccp5', use_pooling=True, p_kernel=(2, 2), p_stride=(2, 2), use_batch_norm=False) cccp6_a, cccp6_b = conv_bn_relu_pool_siamese(cccp5_a, cccp5_b, kernel=(3, 3), num_filter=64, pad=(2, 2), stride=(1, 1), name_postfix='_cccp6', use_pooling=False, use_batch_norm=False) flat_a = mxs.flatten(cccp6_a) flat_b = mxs.flatten(cccp6_b) return flat_a, flat_b