tensorflow-object-detection-cpp icon indicating copy to clipboard operation
tensorflow-object-detection-cpp copied to clipboard

Why convert to float and then back to uint8?

Open oliverdain opened this issue 6 years ago • 6 comments

First: thanks for putting together this example, very helpful.

I was looking at the code in readTensorFromMat. It first does this:

    float *p = outTensor.flat<float>().data();
    Mat fakeMat(mat.rows, mat.cols, CV_32FC3, p);
    mat.convertTo(fakeMat, CV_32FC3);

which takes your Mat and converts it to a matrix of floats. But then you construct a TensorFlow graph to convert a tensor of float to a tensor of uint8:

    auto input_tensor = Placeholder(root.WithOpName("input"), tensorflow::DT_FLOAT);
    vector<pair<string, tensorflow::Tensor>> inputs = {{"input", outTensor}};
    auto uint8Caster = Cast(root.WithOpName("uint8_Cast"), outTensor, tensorflow::DT_UINT8);
    /// etc...

I'm wondering why you didn't just do:

    float *p = outTensor.flat<uint8_t>().data();
    Mat fakeMat(mat.rows, mat.cols, CV_8UC3, p);
    mat.convertTo(fakeMat, CV_8UC3);
    return outTensor;

Wouldn't that do the same thing with less computation and code?

oliverdain avatar Jun 12 '18 21:06 oliverdain

You're probably right, PR would be cool

lysukhin avatar Nov 10 '19 20:11 lysukhin

@oliverdain

Holy crap you are a life saver. I'll see if I can put a PR in for this change. Took the tensor creation time from ~110ms to .05 ms.

HOYS avatar Dec 02 '19 19:12 HOYS

Hi, I just wanted to know if anyone tested the idea from @oliverdain because I'm getting an error :

error: cannot convert ‘Eigen::TensorMap<Eigen::Tensor<unsigned char, 1, 1, long int>, 16, Eigen::MakePointer>::PointerType {aka unsigned char*}’ to ‘float*’ in initialization

I changed the definition of the function which look like this now :

Tensor readTensorFromMat(const Mat &mat,Tensor &outTensor) { float *p = outTensor.flat<uint8_t>().data(); Mat fakeMat(mat.rows, mat.cols, CV_8UC3, p); mat.convertTo(fakeMat, CV_8UC3); return outTensor; }

Is there anything wrong here ? Thanks !

Kmarconi avatar Jun 08 '20 07:06 Kmarconi

Solved ! Got from 15 FPS to 30 FPS with this trick, thanks !

Kmarconi avatar Jun 08 '20 09:06 Kmarconi

@Kmarconi How you solved the problem?

fiona-aa avatar Sep 01 '20 12:09 fiona-aa

By using directly these lines in my main code

float *p = outTensor.flat<uint8_t>().data(); Mat fakeMat(mat.rows, mat.cols, CV_8UC3, p); mat.convertTo(fakeMat, CV_8UC3);

Kmarconi avatar Sep 02 '20 07:09 Kmarconi