serizba / cppflow

Run TensorFlow models in C++ without installation and without Bazel
https://serizba.github.io/cppflow/
MIT License
774 stars 177 forks source link

How to threshold a tensor to generate a binary image #231

Closed bmiftah closed 1 year ago

bmiftah commented 1 year ago

Hi , I am working on segmenation problem. The model was trained and succecssfully loaded to C++ using the method suggested. I was tring to threshold my model output to generate a binary image . I want to threshold the pixles to certain value and make all pixles either 1 or 0 (binary white and black image) based on the thresholding value. here is what I tried based on my search : -

cppflow::model model("model"); auto output_tensor = model_abdomen(input); output_tensor = tf.where(output_tensor >0.5, 1, 0) -- this is in fact from tenforflow APIs I don't know if this is supported in cppflow.

model is the name of my model, it returns predicted tensor-output Cany anyone help me to share how to use "output_tensor = tf.where(output_tensor >0.5, 1, 0)" from cppflow Library ,I try it this way ;-

auto output_tensor = cppflow::where(output_tensor >0.5, 1, 0)

but it gave me the following error note: template argument deduction/substitution failed: AC.cpp:44:56: note: ‘cppflow::tensor’ is not derived from ‘const std::multiset<_Key, _Compare, _Allocator>’ 44 | auto output_tensor = cppflow::where(output_tensor >0.5, 1, 0)

one more thing ... Is there a function to change tensorf to image in c++ using the cppflow interface ...

Thank you so much in advance

serizba commented 1 year ago

Hi @bmiftah

For such operation you can do a comparison and a cast:

auto binary_tensor = cppflow::cast(cppflow::greater(output_tensor, 0.5f), TF_BOOL, TF_FLOAT);

First you perform a greater than operation that transforms the input tensor into a tensor of booleans, and then convert those booleans to floats (false will be 0s and true will be 1s)

bmiftah commented 1 year ago

Thank you so much @serizba . Casting and greater than funtion worked for me. I was aksing at the bottom of my previous question if there is cppflow routine to convert tensor to image , Or should I change the binary_tensor to c++ MAT/VECTOR and save it as image ? I will close this issue after this follow up. thanks

serizba commented 1 year ago

What do you mean to convert to an image? To a cv::Mat? Or to save it to disk?

bmiftah commented 1 year ago

Yes , that is what I meant, tensor to cv::Mat and later to save it as image with imwrite(). Thanks

serizba commented 1 year ago

For cv::Mat you will need to first get the tensor data with tensor::get_data() and then create a mat from there. There isn't yet a direct method to convert from one to the other.

bmiftah commented 1 year ago

Yes, I did _vector output = tensor.getdata(); which return vector data . I will proceed from here to Mat , may be with nested for-loop . Thank you @serizba