天天看點

windows下vs2015+cmake+libtorch +opencv(CPU下測試)生成項目項目中調試

生成項目

首先保證安裝成功:

步驟參考

https://blog.csdn.net/qq_35608277/article/details/89817027

Cmakelist加上Opencv:

cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(with_opencv)

find_package(Torch REQUIRED)
find_package(OpenCV REQUIRED)

message(STATUS "Pytorch status:")
message(STATUS "    libraries: ${TORCH_LIBRARIES}")
message(STATUS "OpenCV library status:")
message(STATUS "    version: ${OpenCV_VERSION}")
message(STATUS "    libraries: ${OpenCV_LIBS}")
message(STATUS "    include path: ${OpenCV_INCLUDE_DIRS}")

add_executable(test_opencv use_opencv.cpp)
target_link_libraries(test_opencv "${TORCH_LIBRARIES}" "${OpenCV_LIBS}")
set_property(TARGET test_opencv PROPERTY CXX_STANDARD 11)

           

use_opencv.cpp

#include <torch/script.h> // One-stop header.
#include <iostream>
#include <memory>
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

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

	string model = "model.pt";
	// Deserialize the ScriptModule from a file using torch::jit::load().
	shared_ptr<torch::jit::script::Module> module = torch::jit::load(model);
	assert(module != nullptr);
	cout << "ok";
	
	waitKey(3000);
	return 0;
}

           

cmd:

改成libtorch和cv自己路徑

cmake -DCMAKE_PREFIX_PATH=I:\opencv3_3\opencv\build\x64\vc14\lib;I:\libtorch -DCMAKE_BUILD_TYPE=Release -G "Visual Studio 14 Win64" ..
           

項目中調試

#include <torch/script.h>
#include <iostream>
#include <memory>
#include <string>
#include<ctime>
#include <opencv2/opencv.hpp>
clock_t start, end;
using namespace std;
using namespace cv;

int main(int argc, const char* argv[]) {
	//load model
	string model = "model.pt";
	// Deserialize the ScriptModule from a file using torch::jit::load().
	shared_ptr<torch::jit::script::Module> module = torch::jit::load(model);
	assert(module != nullptr);
	cout << "ok";	

	//load image
	start = clock();
	Mat img=imread("2.jpg");
	resize(img, img, Size(224, 224), (0, 0), (0, 0), INTER_LINEAR);
	img.convertTo(img, CV_32F, 1.0 / 255.0);
	//Mat to Torch   opencv H x W x C  torch 1 x C x H x W,
	torch::TensorOptions option(torch::kFloat32);
	at::Tensor img_tensor = torch::from_blob(img.data, {img.rows,img.cols ,img.channels() }, option);
	img_tensor = img_tensor.permute({2,0,1});
	img_tensor = img_tensor.reshape({ 1,img.channels(),img.rows,img.cols });
	//prediction
	at::Tensor result = module->forward({ img_tensor }).toTensor();
	//result = result.squeeze(0);
	//result= result.permute({ 1,2,0 });
	Mat dehazed = Mat::ones(img.rows, img.cols, CV_32FC3);
	auto foo_a = result.accessor<float, 4>();
	for (int i = 0; i < img.rows; i++)
	{
		for (int j = 0; j <img.cols; j++)
		{
			dehazed.at<float>(i, (3 * j + 0)) = foo_a[0][0][i][j];
			dehazed.at<float>(i, (3 * j + 1)) = foo_a[0][1][i][j];
			dehazed.at<float>(i, (3 * j + 2)) = foo_a[0][2][i][j];
		}

	}
	
	//Mat prediction(Size(img.cols, img.rows), CV_32FC3, result.data_ptr());
	clock_t end = clock();
	double endtime = (double)(end - start) / CLOCKS_PER_SEC;
	cout << "Total time:" << endtime << "s" << endl;
	imshow("resized image", dehazed);
	waitKey(3000);
	return 0;
}

           

為了看元素變化,生成的2* 4 *3的Mat

/*
		
		Mat ab = Mat::zeros(2, 4, CV_32FC3);
		for (int i = 0; i <2; i++)
		{
		for (int j = 0; j < 4; j++)
			{
				ab.at<float>(i, (3 * j + 0)) = i*(3*4)+1+j*3;
				ab.at<float>(i, (3 * j + 1)) = i * (3 * 4) + 2 + j * 3;
				ab.at<float>(i, (3 * j + 2)) = i * (3 * 4) + 3 + j * 3;
			}
		}
		cout << ab << endl;
		torch::TensorOptions option(torch::kFloat32);
		torch::Tensor f = torch::from_blob(ab.data, { 2,4,3 }, option);
		cout <<"read(2,4,3):f"<< f<<endl;
		
		torch::Tensor f_re = f.permute({2,0,1});
		cout << "reshape(3,2,4):f_re" << f_re << endl;
		torch::Tensor f_add = f_re.reshape({1,3,2,4});
		cout << "reshape(1,3,2,4):f_add" << f_add << endl;
		at::Tensor result = module->forward({ f_add }).toTensor();
		cout <<"final"<< result;
		auto foo_a = result.accessor<float, 4>();
		Mat add = Mat::zeros(2, 4, CV_32FC3);
		for (int i = 0; i < 2; i++)
		{
			for (int j = 0; j < 4; j++)
			{
				add.at<float>(i, (3 * j + 0)) = foo_a[0][0][i][j];
				add.at<float>(i, (3 * j + 1)) = foo_a[0][1][i][j];
				add.at<float>(i, (3 * j + 2)) = foo_a[0][2][i][j];
			}
		}
		cout << add << endl;

	*/
           

ref libtorch read Mat data

https://pytorch.org/cppdocs/notes/tensor_basics.html#using-externally-created-data