libtorch c++ windows7 vs2017 gpu配置的一次记录

\color{red}{1.python模型导出 }
有点想说在前面的,我原来是用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++能用的模型。

\color{red}{2.libtorch选择 }

选择libtorch.JPG

我选的是debug版的,也可以选release版的,只要在后面改一个地方就行了

\color{red}{3.生成sln }
创建一个文件夹,里面的结构是这样,如果非必要建议不要有中文路径,放入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 <torch/script.h> // One-stop header.
#include <iostream>
#include <memory>

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<torch::jit::IValue> 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全部拷贝到这个文件夹下

\color{red}{4.测试 }
注意设置好启动项,右键libtorch-app这个工程,点击设置为启动项

测试.JPG

\color{red}{5.cpu }
cpu的话就下cpu的libtorch就好了,要小很多。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 213,417评论 6 492
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,921评论 3 387
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 158,850评论 0 349
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,945评论 1 285
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,069评论 6 385
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,188评论 1 291
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,239评论 3 412
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,994评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,409评论 1 304
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,735评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,898评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,578评论 4 336
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,205评论 3 317
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,916评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,156评论 1 267
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,722评论 2 363
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,781评论 2 351