Batch Permutation op problem when converting caffe2 to onnx

As the title suggests, I have a caffe2 model (model_init.pb and model.pb) and wanted to convert it into onnx via the following code snippet.

import onnx
import caffe2.python.onnx.frontend
from caffe2.proto import caffe2_pb2
from PIL import Image
import numpy as np

# We need to provide type and shape of the model inputs, 
# see above Note section for explanation
data_type = onnx.TensorProto.FLOAT

IMAGE_LOCATION = "/home/an1/caffe2_model/input.jpg"
INIT_NET = "/home/an1/caffe2_model/model_init.pb"
PREDICT_NET = "/home/an1/caffe2_model/model.pb"

# Read single image
img =
img = np.array(img)

# Convert HWC -> CHW
img = img.swapaxes(1, 2).swapaxes(0, 1)

# Convert CHW -> NCHW
img = np.array([img])

# Im info N x 3 tensor of (height, width, scale)
im_info = np.reshape(np.array([img.shape[2], img.shape[3], 1.0]), (1,-1))

value_info = {
    'data': (data_type, img.shape),
    'im_info': (data_type, (1,3))

predict_net = caffe2_pb2.NetDef()
with open(PREDICT_NET, 'rb') as f:

init_net = caffe2_pb2.NetDef()
with open(INIT_NET, 'rb') as f:

onnx_model = caffe2.python.onnx.frontend.caffe2_net_to_onnx_model(


However, I’m encountering an error with the Batch Permutation op:
RuntimeError: [enforce fail at] X.dim32(0) > 0. 0 vs 0

Does anyone have an idea what could be causing this?

1 Like

I’m trying to do the same thing, and getting the same error. Did you ever fix this?

EDIT: The solution is to use a larger image input size, I suspect because otherwise the successive pooling operations will reduce it to have a negative size or something. Also it must be a multiple of 32.

Did anyone find the solution for this? I am still facing it.