C++ inference semantic segmentation


#1

Hi everyone!

i’m faced with a problem while develop c++ app for NN inference using c++ API. In examples i’m found code https://github.com/apache/incubator-mxnet/blob/master/example/image-classification/predict-cpp/image-classification-predict.cc which is used as a base for inference.

For debuging app deeplab model used from

The problem is empty output array data. No error while forward or any step.

Where is a mistake?
How to make it work?

output log:
[15:13:07] /home/user/incubator-mxnet/src/nnvm/legacy_json_util.cc:209: Loading symbol saved by previous version v1.2.0. Attempting to upgrade…
[15:13:07] /home/user/incubator-mxnet/src/nnvm/legacy_json_util.cc:217: Symbol successfully upgraded!
MXPREDCreate… Done
Assert… Done
[15:14:14] /home/user/incubator-mxnet/src/operator/nn/./cudnn/./cudnn_algoreg-inl.h:107: Running performance tests to find the best convolution algorithm, this can take a while… (setting env variable MXNET_CUDNN_AUTOTUNE_DEFAULT to 0 to disable)
[15:14:36] /home/user/incubator-mxnet/src/operator/nn/./cudnn/./cudnn_algoreg-inl.h:107: Running performance tests to find the best convolution algorithm, this can take a while… (setting env variable MXNET_CUDNN_AUTOTUNE_DEFAULT to 0 to disable)

Inference code:

std::string json_file = "deepLab-symbol.json";
    std::string param_file = "deepLab-0000.params";

    BufferFile json_data(json_file);
    BufferFile param_data(param_file);

    // Parameters
    int dev_type = 2;  // 1: cpu, 2: gpu
    int dev_id = 0;  // arbitrary.
    mx_uint num_input_nodes = 1;  // 1 for feedforward
    const char* input_key[1] = {"data"};
    const char** input_keys = input_key;

    // Image size and channels
    int width = 2048;
    int height = 1024;
    int channels = 3;

    const mx_uint input_shape_indptr[2] = { 0, 4 };
    const mx_uint input_shape_data[4] = { 1,
                                        static_cast<mx_uint>(channels),
                                        static_cast<mx_uint>(height),
                                        static_cast<mx_uint>(width)};
    PredictorHandle pred_hnd = 0;

    // if (json_data.GetLength() == 0 ||
    //     param_data.GetLength() == 0) {
    //     return -1;
    // }

    // Create Predictor
    cout << "MXPREDCreate... " << endl;
    MXPredCreate((const char*)json_data.GetBuffer(),
                 (const char*)param_data.GetBuffer(),
                 static_cast<size_t>(param_data.GetLength()),
                 dev_type,
                 dev_id,
                 num_input_nodes,
                 input_keys,
                 input_shape_indptr,
                 input_shape_data,
                 &pred_hnd);
    cout << "MXPREDCreate... Done" << endl;
    assert(pred_hnd);
    cout << "Assert... Done" << endl;
    int image_size = width * height * channels;

    // Read Mean Data
    const mx_float* nd_data = NULL;
    NDListHandle nd_hnd = 0;
		//BufferFile nd_buf; //nd_file);

  //  if (nd_buf.GetLength() > 0) {
  //      mx_uint nd_index = 0;
  //      mx_uint nd_len;
  //      const mx_uint* nd_shape = 0;
  //      const char* nd_key = 0;
  //      mx_uint nd_ndim = 0;

  //      MXNDListCreate((const char*)nd_buf.GetBuffer(),
  //                 nd_buf.GetLength(),
  //                 &nd_hnd, &nd_len);

  //      MXNDListGet(nd_hnd, nd_index, &nd_key, &nd_data, &nd_shape, &nd_ndim);
  //  }

    // Read Image Data
    std::vector<mx_float> image_data = std::vector<mx_float>(image_size);

    GetImageFile(test_file, image_data.data(),
                 channels, cv::Size(width, height), nd_data);

    // Set Input Image
    MXPredSetInput(pred_hnd, "data", image_data.data(), image_size);

    // Do Predict Forward
    MXPredForward(pred_hnd);

    mx_uint output_index = 0;

    mx_uint *shape = 0;
    mx_uint shape_len;

    // Get Output Result
    MXPredGetOutputShape(pred_hnd, output_index, &shape, &shape_len);

    size_t size = 1;
    for (mx_uint i = 0; i < shape_len; ++i) size *= shape[i];

    //std::vector<float> data(size);
		float * data = new float[size];
    MXPredGetOutput(pred_hnd, output_index, data, size);

    // Release NDList
    if (nd_hnd)
      MXNDListFree(nd_hnd);

    // Release Predictor
    MXPredFree(pred_hnd);