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

Weird output of resize_bilinear #429

Closed
kyuusaku opened this issue Jun 8, 2018 · 1 comment
Closed

Weird output of resize_bilinear #429

kyuusaku opened this issue Jun 8, 2018 · 1 comment
Labels

Comments

@kyuusaku
Copy link
Contributor

kyuusaku commented Jun 8, 2018

The function resize_bilinear_image in Mat.cpp seems not accurate.

The unit test reveals unexpected negative outputs for positive inputs.

Here is a simple example can be used to reproduce the weird output.

#include <iostream>
#include <fstream>
#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include "net.h"

void ncnn_blob_to_txt(const ncnn::Mat& m, const std::string blob_name)
{
    std::ofstream outfile("log/" + blob_name + ".txt");
    if (!outfile.is_open())
    {
        std::cout << "file open fail" << std::endl;
        exit(1);
    }
    
    std::cout << "dims:" << m.dims << " w:" << m.w << " h:" << m.h << " c:" << m.c << std::endl;
    outfile << "dims:" << m.dims << " w:" << m.w << " h:" << m.h << " c:" << m.c << "\r\n" << " \r\n";

    int size = m.w * m.h;
    for (int c = 0; c < m.c; c++)
    {
        const float* ptr = m.channel(c);
        for (int h = 0; h < m.h; h++)
        {
            for (int w = 0; w < m.w; w++)
            {
                outfile << *ptr << " ";
                ptr++;
            }
            outfile << "\r\n";
        }
        outfile << " \r\n";
    }
    outfile.close();
}

void cv_blob_to_txt(const cv::Mat& m, const std::string blob_name)
{
    std::ofstream outfile("log/" + blob_name + ".txt");
    if (!outfile.is_open())
    {
        std::cout << "file open fail" << std::endl;
        exit(1);
    }
    
    std::cout << "dims:" << m.dims << " w:" << m.cols << " h:" << m.rows << std::endl;
    outfile << "dims:" << m.dims << " w:" << m.cols << " h:" << m.rows << "\r\n" << " \r\n";

    const float* ptr = (const float*)m.data;
    for (int h = 0; h < m.rows; h++)
    {
        for (int w = 0; w < m.cols; w++)
        {
            outfile << *(ptr + h*m.cols + w) << " ";
        }
        outfile << "\r\n";
    }
    outfile.close();
}

void ncnn_from_float(ncnn::Mat& m, float* input)
{
    float* ptr = m;

    int size = m.w * m.h;

    printf("%d %d %d \n", m.w, m.h, m.c);

    for (int h = 0; h < m.h; h++)
    {
        for (int w = 0; w < m.w; w++)
        {
            *(ptr + h*m.w + w) = *(input + h*m.w + w);
        }
    }
}

void cv_from_float(cv::Mat& m, float* input)
{
    float* ptr = (float*)m.data;

    printf("%d %d \n", m.cols, m.rows);

    for (int h = 0; h < m.rows; h++)
    {
        for (int w = 0; w < m.cols; w++)
        {
            *(ptr + h*m.cols + w) = *(input + h*m.cols + w);
        }
    }
}

int test_resize_bilinear(int argc, char** argv) {
    float input[12][12] = {
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4.81992e-14, 0}, 
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3.63482e-15, 4.02549e-13}, 
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
    };


    ncnn::Mat in(12, 12, 1);
    ncnn_from_float(in, &input[0][0]);

    ncnn::Mat out;
    ncnn::resize_bilinear(in, out, 48, 48);

    ncnn_blob_to_txt(in, "test_resize_bilinear_input");
    ncnn_blob_to_txt(out, "test_resize_bilinear_output");

    cv::Mat cv_in(12, 12, CV_32FC1);
    cv_from_float(cv_in, &input[0][0]);
    cv::Mat cv_out;
    cv::resize(cv_in, cv_out, cv::Size(48, 48));
    cv_blob_to_txt(cv_out, "test_resize_bilinear_opencv_output");

    return 0;
}


int main(int argc, char* argv[]) {
    test_resize_bilinear(argc, argv);
}

The weird output is

dims:3 w:48 h:48 c:1
 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -2.25934e-15 -6.77801e-15 -1.12967e-14 -1.58154e-14 -1.58154e-14 -1.12967e-14 -6.77801e-15 -2.25934e-15 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -7.53113e-16 -2.25934e-15 -3.76556e-15 -5.27179e-15 -5.27179e-15 -3.76556e-15 -2.25934e-15 -7.53113e-16 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 7.53113e-16 2.25934e-15 3.76556e-15 5.27179e-15 5.27179e-15 3.76556e-15 2.25934e-15 7.53113e-16 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2.25934e-15 6.77801e-15 1.12967e-14 1.58154e-14 1.58154e-14 1.12967e-14 6.77801e-15 2.25934e-15 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3.76556e-15 1.12967e-14 1.88278e-14 2.63589e-14 2.63589e-14 1.88278e-14 1.12967e-14 3.76556e-15 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 5.27179e-15 1.58154e-14 2.63589e-14 3.69025e-14 3.69025e-14 2.63589e-14 1.58154e-14 5.27179e-15 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 5.27179e-15 1.58154e-14 2.63589e-14 3.69025e-14 3.69025e-14 2.63589e-14 1.58154e-14 5.27179e-15 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3.76556e-15 1.12967e-14 1.88278e-14 2.63589e-14 2.63589e-14 1.88278e-14 1.12967e-14 3.76556e-15 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2.25934e-15 6.77801e-15 1.12967e-14 1.58154e-14 1.58154e-14 1.12967e-14 6.77801e-15 2.25934e-15 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 7.53113e-16 2.25934e-15 3.76556e-15 5.27179e-15 5.27179e-15 3.76556e-15 2.25934e-15 7.53113e-16 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0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0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 5.67941e-17 1.70382e-16 2.8397e-16 3.97558e-16 6.68739e-15 1.91535e-14 3.16195e-14 4.40856e-14 5.03186e-14 5.03186e-14 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1.70382e-16 5.11147e-16 8.51911e-16 1.19268e-15 2.00622e-14 5.74604e-14 9.48586e-14 1.32257e-13 1.50956e-13 1.50956e-13 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2.8397e-16 8.51911e-16 1.41985e-15 1.98779e-15 3.34369e-14 9.57673e-14 1.58098e-13 2.20428e-13 2.51593e-13 2.51593e-13 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3.97558e-16 1.19268e-15 1.98779e-15 2.78291e-15 4.68117e-14 1.34074e-13 2.21337e-13 3.08599e-13 3.5223e-13 3.5223e-13 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3.97558e-16 1.19268e-15 1.98779e-15 2.78291e-15 4.68117e-14 1.34074e-13 2.21337e-13 3.08599e-13 3.5223e-13 3.5223e-13 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2.8397e-16 8.51911e-16 1.41985e-15 1.98779e-15 3.34369e-14 9.57673e-14 1.58098e-13 2.20428e-13 2.51593e-13 2.51593e-13 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1.70382e-16 5.11147e-16 8.51911e-16 1.19268e-15 2.00622e-14 5.74604e-14 9.48586e-14 1.32257e-13 1.50956e-13 1.50956e-13 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 5.67941e-17 1.70382e-16 2.8397e-16 3.97558e-16 6.68739e-15 1.91535e-14 3.16195e-14 4.40856e-14 5.03186e-14 5.03186e-14 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 

It is worth noting that. There are also other cases can generate such weird outputs.

@nihui please help.

@nihui nihui added the bug label Jun 25, 2018
@nihui
Copy link
Member

nihui commented Jun 25, 2018

confirmed

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

No branches or pull requests

2 participants