有点想说在前面的,我原来是用vs2015的,后来在编译的时候提示我不支持c++14标准,这才用的vs2017.
暂时没有用复杂的模型,用的是官网的分类模型。
import torch
import torchvision
import torchvision.transforms as transforms
transform = transforms.Compose(
[transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])
trainset = torchvision.datasets.CIFAR10(root='./data', train=True,
download=True, transform=transform)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=4,
shuffle=True, num_workers=2)
testset = torchvision.datasets.CIFAR10(root='./data', train=False,
download=True, transform=transform)
testloader = torch.utils.data.DataLoader(testset, batch_size=4,
shuffle=False, num_workers=2)
classes = ('plane', 'car', 'bird', 'cat',
'deer', 'dog', 'frog', 'horse', 'ship', 'truck')
import matplotlib.pyplot as plt
import numpy as np
# functions to show an image
def imshow(img):
img = img / 2 + 0.5 # unnormalize
npimg = img.numpy()
plt.imshow(np.transpose(npimg, (1, 2, 0)))
plt.show()
# get some random training images
dataiter = iter(trainloader)
images, labels = dataiter.next()
# show images
imshow(torchvision.utils.make_grid(images))
# print labels
print(' '.join('%5s' % classes[labels[j]] for j in range(4)))
import torch.nn as nn
import torch.nn.functional as F
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = nn.Conv2d(3, 6, 5)
self.pool = nn.MaxPool2d(2, 2)
self.conv2 = nn.Conv2d(6, 16, 5)
self.fc1 = nn.Linear(16 * 5 * 5, 120)
self.fc2 = nn.Linear(120, 84)
self.fc3 = nn.Linear(84, 10)
def forward(self, x):
x = self.pool(F.relu(self.conv1(x)))
x = self.pool(F.relu(self.conv2(x)))
x = x.view(-1, 16 * 5 * 5)
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x)
return x
net = Net()
import torch.optim as optim
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9)
for epoch in range(2): # loop over the dataset multiple times
running_loss = 0.0 #损失函数
for i, data in enumerate(trainloader, 0):
# enumerate() 函数用于将一个可遍历的数据对象(如列表、元组或字符串)组合
# 成一个索引序列,同时列出数据和数据下标,一般用在for循环当中
# get the inputs; data is a list of [inputs, labels]
inputs, labels = data
# zero the parameter gradients
optimizer.zero_grad()
# forward + backward + optimize
outputs = net(inputs.cuda())
loss = criterion(outputs, labels.cuda())
loss.backward()
optimizer.step()
# print statistics
running_loss += loss.item()
if i % 100 == 99: # print every 2000 mini-batches
print('[%d, %5d] loss: %.3f' %
(epoch + 1, i + 1, running_loss / 100))
running_loss = 0.0
print('Finished Training')
PATH = './cifar_net.pth'
torch.save(net.state_dict(), PATH)
##################################
#上面就训练出模型了,下面的是导出
net = Net()
example = torch.rand(1, 3, 32, 32)#输入tensor的大小
net.load_state_dict(torch.load("cifar_net.pth"))
#net=net.cuda()
net.eval()
traced_script_module = torch.jit.trace(net, example)
output = traced_script_module(torch.ones(1, 3, 32,32))#输入tensor的大小
traced_script_module.save("RRDB_ESRGAN_x4_000.pt")
#outputs = net(images[0:1].cuda
这样我们就得到了c++能用的模型。
我选的是debug版的,也可以选release版的,只要在后面改一个地方就行了
创建一个文件夹,里面的结构是这样,如果非必要建议不要有中文路径,放入CMakeLists.txt和main.cpp
------创建的空文件夹
---------CMakeLists.txt
---------main.cpp
CMakeLists.txt内容如下
# 设置 cmake 版本限制
cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
# 项目名称
project(libtorch-app)
# 设置 libtorch-win-shared-with-deps-latest 目录,主要让 find_package 可以找到 Torch 的 TorchConfig.cmake 配置文件以及其他相关 Config.cmake 配置文件
set(CMAKE_PREFIX_PATH "D:/libtorch;C:/opencv/build/x64/vc15/lib") #这个是我的libtorch和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(libtorch-app main.cpp)
target_link_libraries(libtorch-app "${TORCH_LIBRARIES}")
set_property(TARGET libtorch-app PROPERTY CXX_STANDARD 11)
main.cpp 内容如下
#include // One-stop header.
#include
#include
int main() {
// Deserialize the ScriptModule from a file using torch::jit::load().
torch::Device device = torch::kCUDA;//cpu的话改成torch::kCPU
//if (torch::cuda::is_available()) {
// std::cout << "CUDA is available! Training on GPU." << std::endl;
// device = torch::kCUDA;
//}
torch::jit::script::Module module = torch::jit::load("RRDB_ESRGAN_x4_000.pt");
module.to(device);
std::vector inputs;
inputs.push_back(torch::ones({ 1, 3, 32, 32 },device));
// Execute the model and turn its output into a tensor.
torch::Tensor output = module.forward(std::move(inputs)).toTensor();
std::cout << output.slice(/*dim=*/1, /*start=*/0, /*end=*/5) << '\n';
while (1);
}
在cmd中cd到目录下
使用cmake命令
cmake -DCMAKE_BUILD_TYPE=Debug -G"Visual Studio 15 Win64"
如果您用的是release版本
cmake -DCMAKE_BUILD_TYPE=Release -G"Visual Studio 15 Win64"
出现下面的结果就是成功了
CMake Warning:
No source or binary directory provided. Both will be assumed to be the
same as the current working directory, but note that this warning will
become a fatal error in future CMake releases.
-- The C compiler identification is MSVC 19.16.27042.0
-- The CXX compiler identification is MSVC 19.16.27042.0
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/
2017/Professional/VC/Tools/MSVC/14.16.27023/bin/Hostx86/x64/cl.exe
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/
2017/Professional/VC/Tools/MSVC/14.16.27023/bin/Hostx86/x64/cl.exe -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studi
o/2017/Professional/VC/Tools/MSVC/14.16.27023/bin/Hostx86/x64/cl.exe
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studi
o/2017/Professional/VC/Tools/MSVC/14.16.27023/bin/Hostx86/x64/cl.exe -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Looking for pthread.h
-- Looking for pthread.h - not found
-- Found Threads: TRUE
-- Found CUDA: C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v10.2 (found v
ersion "10.2")
-- Caffe2: CUDA detected: 10.2
-- Caffe2: CUDA nvcc is: C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v10.
2/bin/nvcc.exe
-- Caffe2: CUDA toolkit directory: C:/Program Files/NVIDIA GPU Computing Toolkit
/CUDA/v10.2
-- Caffe2: Header version is: 10.2
-- Found CUDNN: C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v10.2/lib/x64
/cudnn.lib
-- Found cuDNN: v7.6.5 (include: C:/Program Files/NVIDIA GPU Computing Toolkit/
CUDA/v10.2/include, library: C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/
v10.2/lib/x64/cudnn.lib)
-- Autodetected CUDA architecture(s): 5.0
-- Added CUDA NVCC flags for: -gencode;arch=compute_50,code=sm_50
CMake Warning (dev) at D:/libtorch/share/cmake/Torch/TorchConfig.cmake:116 (if):
Policy CMP0054 is not set: Only interpret if() arguments as variables or
keywords when unquoted. Run "cmake --help-policy CMP0054" for policy
details. Use the cmake_policy command to set the policy and suppress this
warning.
Quoted variables like "MSVC" will no longer be dereferenced when the policy
is set to NEW. Since the policy is not set the OLD behavior will be used.
Call Stack (most recent call first):
CMakeLists.txt:11 (find_package)
This warning is for project developers. Use -Wno-dev to suppress it.
-- Found torch: D:/libtorch/lib/torch.lib
-- Found OpenCV: C:/opencv/build (found version "3.4.0")
-- Pytorch status:
-- libraries: torch;torch_library;D:/libtorch/lib/c10.lib;C:/Program Files/N
VIDIA Corporation/NvToolsExt/lib/x64/nvToolsExt64_1.lib;C:/Program Files/NVIDIA
GPU Computing Toolkit/CUDA/v10.2/lib/x64/cudart_static.lib;D:/libtorch/lib/caffe
2_nvrtc.lib;D:/libtorch/lib/c10_cuda.lib
-- OpenCV library status:
-- version: 3.4.0
-- libraries: opencv_calib3d;opencv_core;opencv_dnn;opencv_features2d;opencv
_flann;opencv_highgui;opencv_imgcodecs;opencv_imgproc;opencv_ml;opencv_objdetect
;opencv_photo;opencv_shape;opencv_stitching;opencv_superres;opencv_video;opencv_
videoio;opencv_videostab;opencv_world
-- include path: C:/opencv/build/include;C:/opencv/build/include/opencv
-- Configuring done
-- Generating done
-- Build files have been written to: D:/torchgpu
接着要做的就是把libtorch里lib文件夹下的dll全部拷贝到这个文件夹下
注意设置好启动项,右键libtorch-app这个工程,点击设置为启动项
cpu的话就下cpu的libtorch就好了,要小很多。