MXPredReshape always fails (return -1) - MXNet 1.3.0 c++

Hi friends,

I am having some problem with MXPredReshape(). I tried to reshape the input to a larger size but the function always returns -1. The code is given below. I am not sure what went wrong since there is not error hints. Any suggestion is highly appreciated. Thanks.

– J

    // create Predictor
    mx_uint num_input_nodes = 1;  // 1 for feedforward
    const char* input_key[1] = {"data"};
    const char** input_keys = input_key;
    const mx_uint input_shape_indptr[2] = { 0, 4 };
    const mx_uint input_shape_data[4] = { batch_size, input_depth, input_height, input_width };
    MXPredCreatePartialOut( (const char*)model_buf.GetBuffer(),
                            (const char*)param_buf.GetBuffer(),
                            static_cast<size_t >(param_buf.GetLength()),
                            device_type,
                            device_id,
                            num_input_nodes,
                            input_keys,
                            input_shape_indptr,
                            input_shape_data,
                            0,
                            NULL, 
                            &pred_hnd);

    if (!pred_hnd) {
        LOG(ERROR) << "Fail to create MXNet predictor";
        return false;
    }
    
    PredictorHandle reshaped_pred_hnd = NULL;
    const mx_uint new_input_shape_data[4] = { batch_size, input_depth, 512, 512 };
    int ret = MXPredReshape(num_input_nodes,
                            input_keys,
                            input_shape_indptr,
                            new_input_shape_data,
                            pred_hnd,
                            &reshaped_pred_hnd);

Looking at the code snippet you pasted,
Specifically : const mx_uint input_shape_data[4] = { batch_size, input_depth, input_height, input_width };

What are the values of input_height and input_width ?

Because you are reshaping it to :
const mx_uint new_input_shape_data[4] = { batch_size, input_depth, 512, 512 };

So for the reshape to work, the product of :
batch_size * input_depth * input_height * input_width should be equal to batch_size * input_depth * 512 * 512

Hi @piyushghai,

Thanks for your feedback. Does MXPredReshape really require this?

My purpose is to forward pass on a larger image, just like that in fully convolutional networks. I have made the python version work like expected using mxnet.module. reshape. Now I am porting that into c++.

MXNet already has what I need in python. I am just trying to find the c++ APIs.

Is MXPredReshape not the c++ counterpart for mxnet.module. reshape?

– J

Hi
The mxnet.module.reshape() internally uses MXPredReshape() function. The MXPredReshape() is a c_predict API.

I am suspecting that the MXPredReshape() is internally throwing dmlc::Error exception and hence returning -1.
Can you try calling MXGetLastError() and print its output immediately after you invoke MXPredReshape()?

The output error message might give us the clue.

Thanks,
-Amol

@leleamol Thanks a lot for your suggestion.

Here is the output of MXGetLastError()

MXGetLastError: [15:40:03] src/c_api/c_predict_api.cc:310: Check failed: newShape.Size() == arr.shape().Size() (205520896 vs. 26214400) arg fc1_weight shape has been changed, only allow to change the shape of input data.

I don’t quite understand how to interpret this. Any suggestions? Thanks!

– J

I believe below is the part of code that gave the error message:

  ret->ctx = p->ctx;
  for (size_t i=0; i < arg_names.size(); ++i) {
    TShape newShape = arg_shapes[i];
    NDArray &arr = p->arg_arrays[i];
    if (new_shape.count(arg_names[i]) != 0) {
      ret->arg_arrays[i].ReshapeAndAlloc(newShape);
    } else {
       CHECK_EQ(newShape.Size(), arr.shape().Size())
        << "arg " << arg_names[i]
        << " shape has been changed, only allow to change the shape of input data.";
    }
  }