serizba / cppflow

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

how to resize a cppflow tensor #167

Closed clelouch closed 2 years ago

clelouch commented 2 years ago

Thanks for your great work. I implement a model, which takes a 128 * 128 image as input. However, I do not know how to resize the image. Could you please help me?

serizba commented 2 years ago

Hi!

If you already have the image as a cppflow::tensor you can resize it with cppflow::resize_bicubic (defined here).

You can check the TensorFlow raw operations. Most of them should work in cppflow. The file with all of implemented functions is here.

msj121 commented 2 years ago

Very rusty with c++, I have a 224x224 that I am trying to resize to 128x128. Currently I have:


auto input = cppflow::decode_jpeg(cppflow::read_file(std::string(filename)));
auto resized = cppflow::resize_bicubic(input,tensor({128,128}),true);
resized = cppflow::cast(resized, TF_UINT8, TF_FLOAT);
resized = input / 255.f;
resized = cppflow::expand_dims(input, 0);

However, I get the error: libc++abi.dylib: terminating with uncaught exception of type std::runtime_error: input must be 4-dimensional[224,224,3]

Not sure how to turn this into a 4 dimensional array. It expects: ''images: 4-D with shape [batch, height, width, channels].''

Any help much appreciated.

dskkato commented 2 years ago

How about to call expand_dims before the resize operation?

auto input = cppflow::decode_jpeg(cppflow::read_file(std::string(filename)));
input = cppflow::expand_dims(input, 0);
auto resized = cppflow::resize_bicubic(input, tensor({128,128}),true);
resized = cppflow::cast(resized, TF_UINT8, TF_FLOAT);
resized = input / 255.f;
msj121 commented 2 years ago

Awesome that compiled and ran. Now I need to figure out how to save the image to see what it looks like to make sure the resize works. Thanks!

dskkato commented 2 years ago

You can dump the resized image with the following ops: EncdePng and WriteFile.

Before using EncdePng, you might need to cast and Squueze the resized tensor to fit the above ops.

serizba commented 2 years ago

This looks fixed.