Export to onnx error when i use a batchNorm layer

mxnet 1.3.0

i wirte a test program:

class MyNet( HybridBlock ):

def __init__(self, **kwargs):
	super( MyNet ,self).__init__(**kwargs)

	
	self.cnn_block = mx.gluon.nn.HybridSequential()
	
	with self.name_scope():
		
		self.cnn_block.add(
			#conv1
			nn.Conv2D( channels=64,kernel_size=(3,3),strides=(1,1),padding=(1,1)),
			nn.Activation("relu"),
			nn.BatchNorm(),
			nn.MaxPool2D( pool_size=(2, 2), strides=(2, 2), padding=(0, 0) ),
			
				
		)
		
def hybrid_forward(self, F, x, *args, **kwargs):

	y = self.cnn_block( x ) 

	y = F.transpose(y,axes=(1,2,3,0))

	y = F.reshape(y,shape=(0,0,0))
	
	return y

ctx = mx.cpu()
mynet = MyNet()
mynet.initialize( mx.initializer.Xavier(), ctx=ctx )
mynet.hybridize()
fake_x = nd.zeros(shape=(1,3,300,300))
y = mynet( fake_x )
print(y.shape)
mynet.export("./onnx/mynet")

onnx_mxnet.export_model("./onnx/mynet-symbol.json", “./onnx/mynet-0000.params”, [(1,3,300,300)], np.float32, “./onnx/mynet.onnx”)

#######################################################################

the export is ok when the model didn’t contain the BatchNorm layer.
but report error when i add a BatchNorm layer to the model like this:

onnx.onnx_cpp2py_export.checker.ValidationError: Unrecognized attribute: spatial for operator BatchNormalization

call for help :slight_smile:

Which MXNet version are you using? I tried to reproduce your problem, but for me the example is working fine.

my mxnet verison is 1.3.0 (on python2.7), onnx version is 1.5.0. Do i need to upgrade to mxnet 1.4.1? :slight_smile:

Yes I recommend upgrading it to 1.4 or 1.5. I run your code with MXNet 1.4 and it worked fine.

hi,
i got the same error,and my mxnet is mxnet-cu100 1.4.1 and onnx is 1.5.0,how can i solve this??

MXNet versions <=1.5.0 only support ONNX-versions <= 1.3.0.

pip install onnx==1.3.0

BatchNormalization (Opset7) had a an attribute “spatial” which is being exported from MXNet to ONNX. Looks like this attribute has been dropped in BatchNormalization (Opset 9).” – vandanavk