当前位置:网站首页>libtorch示例

libtorch示例

2022-08-09 07:50:00 HySmiley

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:
原网站

版权声明
本文为[HySmiley]所创,转载请带上原文链接,感谢
https://blog.csdn.net/m0_37264397/article/details/120319508