libtorch示例
1、IDE
VS2019+libtorch1.7cpu+opencv3.4
2、图片转tensor测试
#include<torch/script.h>
#include<iostream>
#include<opencv2/opencv.hpp>
using namespace std;
int main()
{
cv::Mat img = cv::imread("D:/data/dog_cat/test/cat/cat.8154.jpg");
/*torch::Tensor ten = torch::rand({ 2,3 });
cout << ten << endl;*/
torch::Tensor timg = torch::from_blob(img.data, { 1,img.rows,img.cols,3 }, torch::kByte);
timg = timg.permute({ 0,3,1,2 });
timg = timg.toType(torch::kFloat);
std::cout << timg.sizes() << std::endl;
system("pause");
return 0;
}
3、图像识别
#include <torch/script.h>
#include <opencv2/opencv.hpp>
#include <iostream>
#include <memory>
#include<torch/torch.h>
#include<string>
#include<time.h>
using namespace std;
//https://pytorch.org/tutorials/advanced/cpp_export.html
string image_path = "D:
还没有评论,来说两句吧...