prabhuomkar / pytorch-cpp

C++ Implementation of PyTorch Tutorials for Everyone
MIT License
1.96k stars 260 forks source link

Integration of torch::tensor with Siv3D Image / texture #72

Closed ghost closed 3 years ago

ghost commented 4 years ago

Is your feature request related to a problem? Please describe. I am the author of Siv3DTorch (https://github.com/QuantScientist/Siv3DTorch) which integrates OpenSiv3d with Libtorch C++. At the moment Siv3D does not support CMake and therefore all integration efforts are on VC 19. One main burning issue that I have is reading and writing Images/video frames from and to Siv3D without using OpenCV.

Describe the solution you'd like I would love to have an Image conversation method between the two frameworks, either using stb_image or libpng (used by Siv3D).

Additional context The whole scenario is described here: https://github.com/Siv3D/OpenSiv3D/issues/534

The source code is here: https://github.com/QuantScientist/Siv3DTorch/blob/master/src/loadmodel003.cpp

Many thanks for your help,

ghost commented 4 years ago

I also understand that torchvision has integration with libpng: https://github.com/pytorch/vision

But I could not find any example.

mfl28 commented 4 years ago

Hi! so as you describe here, you are able to read images from disk to torch::tensors using stb_image but you are missing the conversion torch::tensor -> s3d::Image?

If that is the case, have you tried the available constructors of the Image class in OpenSiv3D? I could find constructors taking s3d::FilePaths and s3d::IReaders (e.g.: s3d::BinaryReader seems to be relevant for you) here. So it might be worth trying to read torch::tensor's data_ptr() using s3d::BinaryReader and passing that to the s3d::Image constructor.

ghost commented 4 years ago

@mfl28 Thank you so much for your reply. I ended up writing a small lib: https://github.com/QuantScientist/PngTorch

I am going to test it with Siv3D, it works great independently.