Both reading and writing PNG’s, finished testing it fully yesterday. There was a problem with saving tensors that returned from models. Now it’s done, works for RGB and RGBA.

  
      github.com
  
  
    QuantScientist/PngTorch/blob/master/src/example001.cpp 2
//#include <torch/torch.h>
#include <torch/script.h> // One-stop header.
#include <iostream>
#include <iostream>
#include <chrono>
#include <iostream>
#include <typeinfo>
#include <thread>
#include <future>
#include "../include/utils/vision_utils.hpp"
using namespace std;
using namespace std::chrono;

int main(int argc, char *argv[]) {
    auto VU= VisionUtils();
    torch::Device device(torch::kCUDA);
//    torch::Device device(torch::kCPU);
    // Input PNG-image
    png::image<png::rgb_pixel> imageI("windmill.png");
    // Convert png::image into torch::Tensor


  This file has been truncated. show original

  
  
    
    
  
  



  
      github.com
  
  
    QuantScientist/PngTorch/blob/master/src/example002.cpp 1
#include <iostream>
#include <torch/torch.h>
#include <png++/png.hpp>
#include <utils/vision_utils.hpp>


int main(int argc, char *argv[]){

    auto VU= VisionUtils();
    torch::Device device(torch::kCUDA);
//    const std::string modelName = "mosaic_cpp.pt";
//    const std::string modelName = "erfnet_fs.pt";
    const std::string modelName = "candy_cpp.pt";
    const std::string content_image_path = "amber.png";
    auto module = torch::jit::load(modelName, device);

    //RGB
    png::image<png::rgb_pixel> imageI(content_image_path);
    torch::Tensor tensor = VU.pngToTorch(imageI, device);
    std::cout << "C:" << tensor.size(0) << " H:" << tensor.size(1) << " W:" << tensor.size(2) << std::endl;


  This file has been truncated. show original

  
  
    
    
  
  


Using in in my other project:(https://github.com/QuantScientist/Siv3DTorch 3)

Best,