libtorch示例

电玩女神 2022-09-11 10:24 470阅读 0赞

1、IDE

VS2019+libtorch1.7cpu+opencv3.4

2、图片转tensor测试

  1. #include<torch/script.h>
  2. #include<iostream>
  3. #include<opencv2/opencv.hpp>
  4. using namespace std;
  5. int main()
  6. {
  7. cv::Mat img = cv::imread("D:/data/dog_cat/test/cat/cat.8154.jpg");
  8. /*torch::Tensor ten = torch::rand({ 2,3 });
  9. cout << ten << endl;*/
  10. torch::Tensor timg = torch::from_blob(img.data, { 1,img.rows,img.cols,3 }, torch::kByte);
  11. timg = timg.permute({ 0,3,1,2 });
  12. timg = timg.toType(torch::kFloat);
  13. std::cout << timg.sizes() << std::endl;
  14. system("pause");
  15. return 0;
  16. }

3、图像识别

  1. #include <torch/script.h>
  2. #include <opencv2/opencv.hpp>
  3. #include <iostream>
  4. #include <memory>
  5. #include<torch/torch.h>
  6. #include<string>
  7. #include<time.h>
  8. using namespace std;
  9. //https://pytorch.org/tutorials/advanced/cpp_export.html
  10. string image_path = "D:

发表评论

表情:
评论列表 (有 0 条评论,470人围观)

还没有评论,来说两句吧...

相关阅读