上一篇文章讲了如果把pytorch模型转化成torchscripts模型,这篇介绍如何在libtorch中调用
先贴代码,细节等后面再补充
#include <torch/script.h> // One-stop header.
#include <iostream>
#include <memory>
#include <stdlib.h>
#include <string>
#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
int main()
{
// 加载模型
std::shared_ptr<torch::jit::script::Module> module = torch::jit::load("D:\\scripts\\simple_facenet\\log\\torch_script_eval.pt");
assert(module != nullptr);
// 转化为GPU模型
module->to(at::kCUDA);
std::cout << "ok\n" << std::endl;
std::vector<torch::jit::IValue> inputs_1,inputs_2;
// 加载图片,这里加载了两张一样的图片
cv::Mat image_1,image_2;
image_1 = cv::imread("D:\\datasets\\lfw_yolo_182\\Aaron_Eckhart\\Aaron_Eckhart_0001.jpg");
image_2 = cv::imread("D:\\datasets\\lfw_yolo_182\\Aaron_Eckhart\\Aaron_Eckhart_0001.jpg");
// 数据转换
// OPENCV 默认格式为BGR,而我训练模型用的是RGB格式,所以需要转换
cv::cvtColor(image_1, image_1, CV_BGR2RGB);
cv::cvtColor(image_2, image_2, CV_BGR2RGB);
cv::Mat img_float_1,img_float_2;
image_1.convertTo(img_float_1, CV_32F, 1.0 / 255);
image_2.convertTo(img_float_2, CV_32F, 1.0 / 255);
cv::resize(img_float_1, img_float_1, cv::Size(182, 182));
cv::resize(img_float_2, img_float_2, cv::Size(182, 182));
auto img_tensor_1 = torch::CPU(torch::kFloat32).tensorFromBlob(img_float_1.data, { 1,182,182,3 });
auto img_tensor_2 = torch::CPU(torch::kFloat32).tensorFromBlob(img_float_2.data, { 1,182,182,3 });
img_tensor_1 = img_tensor_1.permute({ 0,3,1,2 }); // ת��nchw
img_tensor_2 = img_tensor_2.permute({ 0,3,1,2 }); // ת��nchw
auto img_var_1 = torch::autograd::make_variable(img_tensor_1, false);
auto img_var_2 = torch::autograd::make_variable(img_tensor_2, false);
inputs_1.push_back(img_var_1.to(at::kCUDA));
inputs_2.push_back(img_var_2.to(at::kCUDA));
std::cout << "image ready" << std::endl;
// forward
auto output_1 = module->forward(inputs_1).toTensor();
auto output_2 = module->forward(inputs_2).toTensor();
vector<float> feature1(128),feature2(128);
for (int i = 0;i<128;i++)
{
// 转化成Float
feature1[i] = output_1[0][i].item().toFloat();
feature2[i] = output_2[0][i].item().toFloat();
}
/* 计算vector之间的距离
... ...
*/
getchar();
return 0;
}
这里为了方便部署,把最后的结果转化为vector了,对libtorch的API还没有摸得很清楚,不知道是否还有跟高效率的转化方式。
有问题的话,可以私信或者加好友交流。
参考:
https://github.com/Jack-An/TorchDemo/blob/master/main.cpp
https://oldpan.me/archives/pytorch-c-libtorch-inference