Libtorch常用代码
torch: : DeviceType device = torch: : kCUDA;
module = torch: : jit: : load( "traced_model.pt" ) ;
torch: : jit: : script: : Module module;
try {
// Deserialize the ScriptModule from a file using torch: : jit: : load( ) .
module = torch: : jit: : load( "traced_model.pt" ) ;
} catch ( const c10: : Error & e) {
std: : cerr << "error loading the model\n" ;
}
module. to( device) ;
cv: : cvtColor( image, input , CV_BGR2RGB) ;
tensor_image = tensor_image. to( torch: : kCUDA) ;
torch: : Tensor result = module. forward( { tensor_image } ) . toTuple( ) - > elements( ) [ 0 ] . toTensor( ) ;
torch: : Tensor pred = result. argmax( 1 ) ;
pred = pred. squeeze( ) ;
pred = pred. to( torch: : kU8) ;
pred = pred. to( torch: : kCPU) ;
std: : cout << torch: : cuda: : cudnn_is_available( ) << endl; // 输出为1 ,成功
std: : cout << torch: : cuda: : is_available( ) << endl; // 输出为1 ,成功
using namespace std;
QtGuiApplication2: : QtGuiApplication2( QWidget * parent)
: QMainWindow( parent)
{
ui. setupUi( this) ;
std: : cout << torch: : cuda: : cudnn_is_available( ) << endl; // 输出为1 ,成功
std: : cout << torch: : cuda: : is_available( ) << endl; // 输出为1 ,成功
torch: : jit: : script: : Module module = torch: : jit: : load( "F:/pytorch-gpu.pt" , torch: : kCUDA) ;
module. to( at: : kCUDA) ;
torch: : NoGradGuard on_grad;
std: : vector< torch: : jit: : IValue> inputs;
inputs. push_back( torch: : ones( { 1 , 3 , 512 , 512 } , torch: : kCUDA) ) ;
at: : Tensor output = module. forward( inputs) . toTensor( ) ;
std: : cout << output << std: : endl;
}
PyTorch生成模型
from torchvision. models import resnet34
import torch. nn. functional as F
import torch. nn as nn
import torch
import cv2
#读取一张图片,并转换成[1,3,224,224]的float张量并归一化
image = cv2. imread ( "flower.jpg" )
image = cv2. resize( image, ( 224, 224) )
input_tensor = torch. tensor ( image) . permute ( 2 , 0 , 1 ) . unsqueeze ( 0 ) . float ( ) / 225.0
#定义并加载resnet34模型在imagenet预训练的权重
model = resnet34 ( pretrained= True)
model. eval ( )
#查看模型预测该付图的结果
output = model ( input_tensor)
output = F. softmax( output, 1)
print ( "模型预测结果为第{}类,置信度为{}" . format ( torch. argmax ( output) , output. max ( ) ) )
#生成pt模型,按照官网来即可
model= model. to ( torch. device ( "cpu" ) )
model. eval ( )
var = torch. ones ( ( 1 , 3 , 224 , 224 ) )
traced_script_module = torch. jit. trace( model, var )
traced_script_module. save ( "resnet34.pt" )
LibTorch常用代码模板
#include<opencv2/opencv.hpp>
#include <torch/torch.h>
#include <torch/script.h>
using namespace std ;
int main ( )
{
auto device = torch:: Device ( torch :: kCUDA, 0 ) ;
auto image = cv:: imread ( "E:\\WorkPlace\\PycharmProjects\\ModelTrainingSystem\\flower.png" ) ;
cv:: resize ( image, image, cv :: Size ( 224 , 224 ) ) ;
auto input_tensor = torch:: from_blob ( image. data, { image. rows, image. cols, 3 } , torch :: kByte) . permute ( { 2 , 0 , 1 } ) . unsqueeze ( 0 ) . to ( torch :: kFloat32) / 225.0 ;
auto model = torch:: jit:: load ( "E:\\WorkPlace\\PycharmProjects\\ModelTrainingSystem\\resnet34.pt" ) ;
model. eval ( ) ;
auto output = model. forward ( { input_tensor } ) . toTensor ( ) ;
output = torch:: softmax ( output, 1 ) ;
std:: cout << "模型预测结果为第" << torch:: argmax ( output) << "类,置信度为" << output. max ( ) << std:: endl;
return 0 ;
}
张量的常见操作
auto t = torch:: zeros ( { 3 , 4 } ) ;
t = torch:: ones ( { 3 , 4 } ) ;
t = torch:: tensor ( { 33 , 22 , 11 } ) ;