dshp.ndim() == 3U (4 vs. 3) : Input data should be 3D in batch-num_filter-x


#1

import mxnet as mx

def Conv(data, num_filter=1, kernel=(1, 1), stride=(1, 1), pad=(0, 0), num_group=1, name=None, suffix=’’):
conv = mx.sym.Convolution(data=data, num_filter=num_filter, kernel=kernel, num_group=num_group, stride=stride,
pad=pad, no_bias=True, name=’%s%s_conv2d’ % (name, suffix))
bn = mx.sym.BatchNorm(data=conv, name=’%s%s_batchnorm’ % (name, suffix), fix_gamma=True)
act = mx.sym.Activation(data=bn, act_type=‘relu’, name=’%s%s_relu’ % (name, suffix))
return act

def BasicConv(data, name, channels, kernel_size, stride=(1,), padding=(0,), dilation=(0,), group=1, relu=True, bn=True,
bias=False):
data = mx.symbol.Convolution(data=data, name=name + ‘_conv’, kernel=kernel_size, stride=stride, dilate=dilation,
pad=padding,
num_filter=channels,
num_group=group)
if bn:
data = mx.symbol.BatchNorm(data, name=name + ‘_bn’, fix_gamma=False, eps=1e-5, momentum=0.01)
if relu:
data = mx.symbol.Activation(data, act_type=‘relu’, name=name + ‘_relu’)
return data

# def BasicSepConv(data, name, kernel_size, stride=1, padding=0, dilation=0, group=1, relu=True, bn=True,
# bias=False):

# data = mx.symbol.Convolution(data, name=name + ‘_conv’, kernel=kernel_size, stride=stride, dilate=dilation,
# pad=padding,
# num_filter=,
# num_group=group)
# data = mx.symbol.BatchNorm(data, name=name + ‘_bn’, fix_gamma=False, eps=1e-5, momentum=0.01) if bn else None
# data = mx.symbol.relu(data, name=name + ‘_relu’) if relu else None
# return data

def BasicRFB(x, name, data_channel, channels, stride=(1, 1), scale=0.1):
inter_channels = data_channel // 8
data = BasicConv(x, name=name + ‘_branch1_conv1’, channels=inter_channels, kernel_size=(1,), stride=(1,))
data = BasicConv(data, name=name + ‘_branch1_conv2’, channels=(inter_channels // 2) 3, kernel_size=(1, 3),
stride=(1, 1),
padding=(0, 1), dilation=(0, 0))
data = BasicConv(data, name=name + ‘_branch1_conv3’, channels=(inter_channels // 2)
3, kernel_size=(3, 1),
stride=stride, padding=(1, 0), dilation=(0, 0))
data = BasicConv(data, name=name + ‘_branch1_conv4’, channels=(inter_channels // 2) * 3, kernel_size=(3,),
stride=(1,),
padding=(3,), dilation=(3,), relu=False)

data1 = BasicConv(x, name=name + ‘_branch2_conv1’, channels=inter_channels, kernel_size=(1,), stride=(1,))
data1 = BasicConv(data1, name=name + ‘_branch2_conv2’, channels=(inter_channels // 2) 3, kernel_size=(3,),
stride=(1,),
padding=(1,))
data1 = BasicConv(data1, name=name + ‘_branch2_conv3’, channels=(inter_channels // 2)
3, kernel_size=(3, 3),
stride=stride, padding=(1, 1), dilation=(0, 0))
data1 = BasicConv(data1, name=name + ‘_branch2_conv4’, channels=(inter_channels // 2) 3, kernel_size=(3,),
stride=(1,),
padding=(5,), dilation=(5,), relu=False)
out = mx.symbol.Concat(
[data, data1], dim=1)
out = BasicConv(out, name=name + ‘_linear’, channels=channels, kernel_size=(1,), relu=False)
if data_channel == channels:
out = out scale + x
else:
short = BasicConv(x, name=name + ‘_shortcut’, channels=channels, kernel_size=(1,), stride=stride, relu=False)
out = out
scale + short
out = mx.symbol.relu(out, name=name + ‘_relu’)
return out

def BasicRFB_a(x, name, data_channel, channels, stride=(1, 1), scale=0.1):
inter_channels = data_channel // 4
data0 = BasicConv(x, name=name + ‘_branch0_conv1’, channels=inter_channels, kernel_size=(1,), stride=(1,))
data0 = BasicConv(data0, name=name + ‘_branch0_conv2’, channels=inter_channels,
kernel_size=(3,),
stride=(1,),
padding=(1,), dilation=(1,), relu=False)

data1 = BasicConv(x, name=name + ‘_branch1_conv1’, channels=inter_channels, kernel_size=(1,), stride=(1,))
data1 = BasicConv(data1, name=name + ‘_branch1_conv2’, channels=inter_channels, kernel_size=(3, 1),
stride=(1, 1), padding=(1, 0), dilation=(0, 0))
data1 = BasicConv(data1, name=name + ‘_branch1_conv3’, channels=inter_channels, kernel_size=(3,),
stride=(1,), padding=(3,), dilation=(3,), relu=False)

data2 = BasicConv(x, name=name + ‘_branch2_conv1’, channels=inter_channels, kernel_size=(1,), stride=(1,))
data2 = BasicConv(data2, name=name + ‘_branch2_conv2’, channels=inter_channels, kernel_size=(1, 3),
stride=stride, padding=(0, 1), dilation=(0, 0))
data2 = BasicConv(data2, name=name + ‘_branch2_conv3’, channels=inter_channels, kernel_size=(3,),
stride=(1,), padding=(3,), dilation=(3,), relu=False)

data3 = BasicConv(x, name=name + ‘_branch3_conv1’, channels=inter_channels // 2, kernel_size=(1,), stride=(1,))
data3 = BasicConv(data3, name=name + ‘_branch3_conv2’, channels=(inter_channels // 4) * 3, kernel_size=(1, 3),
stride=(1, 1), padding=(0, 1), dilation=(0, 0))
data3 = BasicConv(data3, name=name + ‘_branch3_conv3’, channels=inter_channels, kernel_size=(3, 1),
stride=stride, padding=(1, 0), dilation=(0, 0))
data3 = BasicConv(data3, name=name + ‘_branch3_conv4’, channels=inter_channels, kernel_size=(3,),
stride=(1,), padding=(5,), dilation=(5,), relu=False)

out = mx.symbol.Concat([data0, data1, data2, data3], dim=1)
out = BasicConv(out, name=name + ‘_linear’, channels=channels, kernel_size=(1,), relu=False)
out = out
scale + x
out = mx.symbol.relu(out, name=name + ‘_relu’)
return out

def MOBILENET():
data = mx.symbol.Variable(name=“data”) # 224
conv_1 = Conv(data, num_filter=32, kernel=(3, 3), pad=(1, 1), stride=(2, 2), name=“conv_1”) # 224/112
conv_2_dw = Conv(conv_1, num_group=32, num_filter=32, kernel=(3, 3), pad=(1, 1), stride=(1, 1),
name=“conv_2_dw”) # 112/112
conv_2 = Conv(conv_2_dw, num_filter=64, kernel=(1, 1), pad=(0, 0), stride=(1, 1), name=“conv_2”) # 112/112
conv_3_dw = Conv(conv_2, num_group=64, num_filter=64, kernel=(3, 3), pad=(1, 1), stride=(2, 2),
name=“conv_3_dw”) # 112/56
conv_3 = Conv(conv_3_dw, num_filter=128, kernel=(1, 1), pad=(0, 0), stride=(1, 1), name=“conv_3”) # 56/56
conv_4_dw = Conv(conv_3, num_group=128, num_filter=128, kernel=(3, 3), pad=(1, 1), stride=(1, 1),
name=“conv_4_dw”) # 56/56
conv_4 = Conv(conv_4_dw, num_filter=128, kernel=(1, 1), pad=(0, 0), stride=(1, 1), name=“conv_4”) # 56/56
conv_5_dw = Conv(conv_4, num_group=128, num_filter=128, kernel=(3, 3), pad=(1, 1), stride=(2, 2),
name=“conv_5_dw”) # 56/28
conv_5 = Conv(conv_5_dw, num_filter=256, kernel=(1, 1), pad=(0, 0), stride=(1, 1), name=“conv_5”) # 28/28
conv_6_dw = Conv(conv_5, num_group=256, num_filter=256, kernel=(3, 3), pad=(1, 1), stride=(1, 1),
name=“conv_6_dw”) # 28/28
conv_6 = Conv(conv_6_dw, num_filter=256, kernel=(1, 1), pad=(0, 0), stride=(1, 1), name=“conv_6”) # 28/28
conv_7_dw = Conv(conv_6, num_group=256, num_filter=256, kernel=(3, 3), pad=(1, 1), stride=(2, 2),
name=“conv_7_dw”) # 28/14
conv_7 = Conv(conv_7_dw, num_filter=512, kernel=(1, 1), pad=(0, 0), stride=(1, 1), name=“conv_7”) # 14/14

conv_8_dw = Conv(conv_7, num_group=512, num_filter=512, kernel=(3, 3), pad=(1, 1), stride=(1, 1),
name=“conv_8_dw”) # 14/14
conv_8 = Conv(conv_8_dw, num_filter=512, kernel=(1, 1), pad=(0, 0), stride=(1, 1), name=“conv_8”) # 14/14
conv_9_dw = Conv(conv_8, num_group=512, num_filter=512, kernel=(3, 3), pad=(1, 1), stride=(1, 1),
name=“conv_9_dw”) # 14/14
conv_9 = Conv(conv_9_dw, num_filter=512, kernel=(1, 1), pad=(0, 0), stride=(1, 1), name=“conv_9”) # 14/14
conv_10_dw = Conv(conv_9, num_group=512, num_filter=512, kernel=(3, 3), pad=(1, 1), stride=(1, 1),
name=“conv_10_dw”) # 14/14
conv_10 = Conv(conv_10_dw, num_filter=512, kernel=(1, 1), pad=(0, 0), stride=(1, 1), name=“conv_10”) # 14/14
conv_11_dw = Conv(conv_10, num_group=512, num_filter=512, kernel=(3, 3), pad=(1, 1), stride=(1, 1),
name=“conv_11_dw”) # 14/14
conv_11 = Conv(conv_11_dw, num_filter=512, kernel=(1, 1), pad=(0, 0), stride=(1, 1), name=“conv_11”) # 14/14
conv_12_dw = Conv(conv_11, num_group=512, num_filter=512, kernel=(3, 3), pad=(1, 1), stride=(1, 1),
name=“conv_12_dw”) # 14/14
conv_12 = Conv(conv_12_dw, num_filter=512, kernel=(1, 1), pad=(0, 0), stride=(1, 1), name=“conv_12”) # 14/14
#
# conv_13_dw = Conv(conv_12, num_group=512, num_filter=512, kernel=(3, 3), pad=(1, 1), stride=(2, 2),
# name=“conv_13_dw”) # 14/7
# conv_13 = Conv(conv_13_dw, num_filter=1024, kernel=(1, 1), pad=(0, 0), stride=(1, 1), name=“conv_13”) # 7/7
# conv_14_dw = Conv(conv_13, num_group=1024, num_filter=1024, kernel=(3, 3), pad=(1, 1), stride=(1, 1),
# name=“conv_14_dw”) # 7/7
# conv_14 = Conv(conv_14_dw, num_filter=1024, kernel=(1, 1), pad=(0, 0), stride=(1, 1), name=“conv_14”) # 7/7
return conv_12

def RFBNet(num_classes):
body = MOBILENET()
body = BasicRFB_a(x=body, name=“rfb_a”, data_channel=512, channels=512, stride=(1, 1), scale=1.0)
body = BasicRFB(body, name=‘rfb’, data_channel=512, channels=512, stride=(1, 0), scale=1.0)
body = BasicConv(body, ‘rfb_conv1’, channels=128, kernel_size=(1,), stride=(1,))
body = BasicConv(body, ‘rfb_conv2’, channels=256, kernel_size=(3,), stride=(2,), padding=(1,))
body = BasicConv(body, ‘rfb_conv3’, channels=128, kernel_size=(1,), stride=(1,))
body = BasicConv(body, ‘rfb_conv4’, channels=256, kernel_size=(3,), stride=(2,), padding=(1,))
body = BasicConv(body, ‘rfb_conv5’, channels=64, kernel_size=(1,), stride=(1,))
body = BasicConv(body, ‘rfb_conv6’, channels=128, kernel_size=(3,), stride=(2,), padding=(1,))
# pool = mx.sym.Pooling(data=body, kernel=(7, 7), stride=(1, 1), pool_type=“max”, name=“global_pool”,
# global_pool=True)
# flatten = mx.sym.Flatten(data=pool, name=“flatten”)
# fc = mx.symbol.FullyConnected(data=flatten, num_hidden=num_classes, name=‘fc’)
# softmax = mx.symbol.SoftmaxOutput(data=fc, name=‘softmax’)
return body

def get_symbol(num_classes, **kwargs):
return RFBNet(num_classes)

if name == ‘main’:
body = get_symbol(1)
if isinstance(body, mx.symbol.Symbol):
for layer in body.get_internals():
print(layer)


#2

用这个做骨架网络,
然后训练ssd报着个错,希望有大老能帮忙看一下…我知道错误是什么意思但是不知道为什么这个网络会出这个错…麻烦了

RuntimeError: simple_bind error. Arguments:
data: (16, 3, 512, 512)
label: (16, 19, 6)
Error in operator rfb_a_branch0_conv1_conv: [15:38:27] src/operator/nn/convolution.cc:103: Check failed: dshp.ndim() == 3U (4 vs. 3) : Input data should be 3D in batch-num_filter-x


#3

不知道怎么贴代码才看起来比较清晰…


#4

同样的imdb可以训练其他网路 说明data上应该是没问题的 而且出错的是中间的一个conv层…费解啊