前一阵子利用pytorch写了一个CNN,训练完成了,也保存成了pt模型文件,但是现在C语言的一个大工程需要利用这个模型的结果,所以就开始研究了各种可能的方案,最后选择了我觉得相对来说比较简单的那种,也就是配置VS的各种变异环境然后引入Python.h库进行各种调用~
环境:Visual Studio 2015、Python 3.7.3、Pytorch 1.2.0
debug过程非常坎坷,网上照着学了不少,但是总有很多坑,有可能是版本不匹配的问题什么的
这里记录一下最终成功的版本~
python代码:split.py
import torch
import numpy as np
import gc
def split(patch, status, qp):
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
model = torch.jit.load('model/split_model_moredata.pt').to(device)
index_1 = 2 if status=='luma' else 3
print("hello I'm from python")
assert (patch.shape[0] == 65)
assert (patch.shape[1] == 65)
print("hello I'm from python")
input = np.full((1, index_1, 65, 65), 0.5)
input[0, 0: index_1-1, :, :] = patch
input[0, index_1-1, 0, 0] = qp
input = torch.from_numpy(input).type(torch<