tensorflow-object-detection-cpp
tensorflow-object-detection-cpp copied to clipboard
Why convert to float and then back to uint8?
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?
You're probably right, PR would be cool
@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.
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 !
Solved ! Got from 15 FPS to 30 FPS with this trick, thanks !
@Kmarconi How you solved the problem?
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);