Convert from Crop to Slice

Hi,
since MXNet’s Crop is deprecated, and thus I cannot convert a model from MXNet to ONNX, I’m struggling to change Crop with slice_axis where axis=1 (this is supported - I can export it from MXNet to ONNX).
I’ve added the visual part of my graph

Here’s a part of the code of Crop:

F2 = 128
_bwm = 1.0
conv4_128 = conv_act_layer(relu4_3, 'ssh_m1_red_conv',
    F2, kernel=(1, 1), pad=(0, 0), stride=(1, 1), act_type='relu', bias_wd_mult=_bwm)
conv5_128 = conv_act_layer(relu5_3, 'ssh_m2_red_conv',
    F2, kernel=(1, 1), pad=(0, 0), stride=(1, 1), act_type='relu', bias_wd_mult=_bwm)
conv5_128_up = mx.symbol.Deconvolution(data=conv5_128, num_filter=F2, kernel=(4,4),  stride=(2, 2), pad=(1,1),
    num_group = F2, no_bias = True, attr={'__lr_mult__': '0.0', '__wd_mult__': '0.0'},
    name='ssh_m2_red_upsampling')
#conv5_128_up = mx.symbol.Deconvolution(data=conv5_128, num_filter=F2, kernel=(2,2),  stride=(2, 2), pad=(0,0),
#    num_group = F2, no_bias = True, attr={'__lr_mult__': '0.0', '__wd_mult__': '0.0'},
#    name='ssh_m2_red_upsampling')
conv4_128 = mx.symbol.Crop(*[conv4_128, conv5_128_up])

and it looks like this in the .json file:

}, 
{
  "op": "Crop", 
  "name": "crop0", 
  "attrs": {"num_args": "2"}, 
  "inputs": [[117, 0, 0], [123, 0, 0]]
}, 

And I’m trying to delete the Crop operation and used slice instead, and this doesn’t work: (would love to get your help on that)

{
  "op": "slice_axis",
   "name": "slice",
   "attrs": {
     "axis": "1",
     "begin": "0",
     "end": "128"
   },
   "inputs": [[117, 0, 0], [123, 0, 0]]
},

Just thinking out loud.
The slice_axis operation should accept just one input. In your example there are two inputs one is [117,0,0] and the other is [123,0,0]. I don’t exactly know how the crop operation works and why it has two inputs, but I am pretty sure slice_axis should have a single input.
or maybe add
“attrs”:{“num_args”:“2”},
to the slice_axis operation

1 Like

Thank you very much [shababqcd], changing the “inputs” to just [123,0,0] made it compile.
Hopefully it’ll solve the problem now, thanks again!

After changing to
{
“op”: “slice_axis”,
“name”: “slice”,
“attrs”: {
“axis”: “1”,
“begin”: “0”,
“end”: “128”
},
“inputs”: [123, 0, 0]]
},
Getting this error:

onnx.onnx_cpp2py_export.checker.ValidationError: Node (slice_axis) has input size 1 not in range [min=3, max=5].

Also:

==> Context: Bad node spec: input: “ssh_m2_red_upsampling” output: “slice_axis” name: “slice_axis” op_type: “Slice” attribute { name: “axes” ints: 1 type: INTS } attribute { name: “ends” ints: 128 type: INTS } attribute { name: “starts” ints: 0 type: INTS }

What are you trying to achieve @AnaRhisT94 with this crop operation?

Here is an example of how you can use the slice operator to center crop a batch of 200x300 images to 100x100

a = mx.nd.ones((2,3,200,300))
a_center_cropped = mx.nd.slice(a, begin=(None, None, 50, 100), end=(None, None, 150, 200), step=(None, None, 1, 1))
a_center_cropped : 2x3x100x100 

Hi Thomas,
thanks for your response.
Please note that slice and slice_like aren’t supported for conversion from MXNET to ONNX.
also, this is somehow solved for now (just using certain height/width images unfortunately for slice_axis).

I have the same problems now, have you solved it yet?

@TensorFly Use “slice_axis” with axis=1 instead. It may cause problems in the future and let you just input only certain input images height,width. but this is the only choice you’ve got, for now.