Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

useing mnncv image process get wrong result #3076

Open
jfkkf123 opened this issue Nov 13, 2024 · 8 comments
Open

useing mnncv image process get wrong result #3076

jfkkf123 opened this issue Nov 13, 2024 · 8 comments
Labels
question Further information is requested

Comments

@jfkkf123
Copy link

平台(如果交叉编译请再附上交叉编译目标平台):

Platform(Include target platform as well if cross-compiling):

  • x84_64 ubuntu20.04
  • raspberry 3b+

mnn版本:

mnn Version:

2.9.5

I use pytorch->onnx->mnn to get .mnn model. I use two different ways to preprocess image,but get different result:

  • same init
    ultraface_interpreter_ = std::shared_ptr<MNN::Interpreter>(MNN::Interpreter::createFromFile(mnn_path.c_str()));
    MNN::ScheduleConfig config;
    config.numThread = 4;
    MNN::BackendConfig backendConfig;
    backendConfig.precision = (MNN::BackendConfig::PrecisionMode) 1;
    config.backendConfig = &backendConfig;
    ultraface_session_ = ultraface_interpreter_->createSession(config);
    input_tensor_ = ultraface_interpreter_->getSessionInput(ultraface_session_, nullptr);
  • opencv pre-process
   cv::Mat raw_image=cv::imread("./image.jpg");
    cv::Mat image;
    cv::resize(raw_image, image, cv::Size(320, 240));

    cv::Mat rgb_img;
    cv::cvtColor(image, rgb_img, cv::COLOR_BGR2RGB);
    rgb_img.convertTo(rgb_img,CV_32FC3,1.0);
    rgb_img=(rgb_img-127.0)/128.0;
    cv::Mat nchwMat;
    hwc_to_chw(rgb_img, nchwMat);

    MNN::Tensor *inhostTensor=MNN::Tensor::create<float>(std::vector<int>{1,3,240,320},nullptr,MNN::Tensor::CAFFE);
    std::memcpy(inhostTensor->host<float>(),nchwMat.data,nchwMat.total()*sizeof(float));
    input_tensor_->copyFromHostTensor(inhostTensor);

    ultraface_interpreter_->resizeTensor(input_tensor_, {1, 3, 240, 320});
    ultraface_interpreter_->resizeSession(ultraface_session_);
   // run net
    ultraface_interpreter_->runSession(ultraface_session_);
  • mnn cv pre-process
   // opencv function
   cv::Mat raw_image=cv::imread("./image.jpg");
    cv::Mat image;
    cv::resize(raw_image, image, cv::Size(320, 240));

   //mnn cv function
   const float mean_vals[3] = {127, 127, 127};
    const float norm_vals[3] = {1.0 / 128, 1.0 / 128, 1.0 / 128};
    std::shared_ptr<MNN::CV::ImageProcess> pretreat(
             MNN::CV::ImageProcess::create(MNN::CV::BGR, MNN::CV::RGB, mean_vals, 3,norm_vals, 3));
     pretreat->convert(image.data, 320, 240, image.step[0], input_tensor_);  // or pretreat->convert(image.data, 320, 240, 0, input_tensor_);
    ultraface_interpreter_->runSession(ultraface_session_);
  • get output data
   string scores = "scores";
    string boxes = "boxes";
    MNN::Tensor *tensor_scores = ultraface_interpreter_->getSessionOutput(ultraface_session_, scores.c_str());
    MNN::Tensor *tensor_boxes = ultraface_interpreter_->getSessionOutput(ultraface_session_, boxes.c_str());

     MNN::Tensor tensor_scores_host(tensor_scores, tensor_scores->getDimensionType());
     tensor_scores->copyToHostTensor(&tensor_scores_host);
    MNN::Tensor tensor_boxes_host(tensor_boxes, tensor_boxes->getDimensionType());
    tensor_boxes->copyToHostTensor(&tensor_boxes_host);

using opencv pre-process, I can get the same result as onnx model, but mnn cv can not.

PS: an old version mnn can get same result, but I do not know the version number. ( only have .so)

Uploading slim-Epoch-500_320_8c_1.onnx.tar.gz…

@jxt1234 jxt1234 added the question Further information is requested label Nov 13, 2024
@jxt1234
Copy link
Collaborator

jxt1234 commented Nov 13, 2024

It appears in x86_64 ? If close MNN_USE_SSE, what's the result?

@jfkkf123
Copy link
Author

It appears in x86_64 ? If close MNN_USE_SSE, what's the result?

yes. It appears in x86_64. I close MNN_USE_SSE to build a new libmnn.so. It also get the wrong result.
I think there is something deference between old version and 2.9.5 in imagepro cess.
When I use old version libmnn. It tell me Imageprocess don't support caffe dimension type,please create tensor with type TensorFlow or caffe_c4. So I convert onnx model to an nc4hw4.mnn model, old version mnn give me right result. But I use nc4hw4.mnn in 2.9.5 version mnn, it gives wrong result.

@jxt1234
Copy link
Collaborator

jxt1234 commented Nov 14, 2024

The onnx file is not upload successfully. Could you upload it again?

@jfkkf123
Copy link
Author

@jxt1234
Copy link
Collaborator

jxt1234 commented Nov 14, 2024

Could you upload correct input and output txt ?

@jfkkf123
Copy link
Author

Could you upload correct input and output txt ?

which part data do you need? before OpenCV pre-process or after pre-process?or others.
I think correct input data can't help you find out reason. I give you the all code and test image file?

@jxt1234
Copy link
Collaborator

jxt1234 commented Nov 14, 2024

Could you upload correct input and output txt ?

which part data do you need? before OpenCV pre-process or after pre-process?or others. I think correct input data can't help you find out reason. I give you the all code and test image file?

OK. I use a random picture can't reproduce the error. It seem the same as the expect value.

@jfkkf123
Copy link
Author

test.tar.gz

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants