在 c++ 中调用 lua 函数(带有 require 'nn')
Call lua function (with require 'nn') in c++
我有一个Lua函数:
require 'nn'
require 'image'
require 'torch'
require './lib/data_augmentation'
function predict (x) do
model = torch.load("trained.t7")
img = image.load(x)
img_tensor = torch.DoubleTensor(2, 3, 32, 32)
img_tensor[1]:copy(img)
x = data_augmentation(img_tensor[1])
preprocessing(x,params)
preds = torch.Tensor(4):zero()
step = 64
for j = 1, x:size(1), step do
batch = torch.Tensor(step, x:size(2), x:size(3), x:size(4)):zero()
n = step
if j + n > x:size(1) then
n = 1 + n - ((j + n) - x:size(1))
end
batch:narrow(1, 1, n):copy(x:narrow(1, j, n))
z = model:forward(batch):float()
for k = 1, n do
preds = preds + z[k]
end
end
preds:div(x:size(1))
confidences, indices = torch.sort(preds,true)
return indices[1]
end
end
我想用C++调用这个函数,但我得到了一个错误:
PANIC: unprotected error in call to Lua API (attempt to call a nil value)
因为需要"…"
我应该怎么做,这样C++就可以识别所有的包或其他Lua脚本(比如data_augmentation)?
我在mac上遇到了同样的问题,并用解决了它
source ~/.profile
然后运行lua脚本。
完整说明可在此处找到:torch.ch
# On Linux with bash
source ~/.bashrc
# On Linux with zsh
source ~/.zshrc
# On OSX or in Linux with none of the above.
source ~/.profile
相关文章:
- OpenCV 3.4.1 error readNetFromTensorflow 无法在 cv::d nn::ReadProtoFromBinaryFile 中打开 .pb
- CMake 用于设定C++标准的设施 "at least C++NN"
- 训练 NN 以计算 atan2(y, x)
- 如何解决 ReLU 不是构建时火炬::nn 错误的成员?
- 重写require后调用"lua_getfield()"时崩溃
- 使用全局变量与局部变量和require
- 通过require在不同的Lua状态之间共享全局变量
- 如何在 JXCore C++应用程序中使用"require"
- 当使用 require 表达式指定和不使用 require 表达式指定 require 子句时,C++ gcc HEAD
- C++调用lua_dostring来加载具有"require('cjson')"的Lua Scrip引发错误:cjson.so:未定义的符号:lua_getfield
- 静态断言失败,出现"Windows headers require the default packing option..."
- 支持向量机 (SVM) 与 K-NN 测试效率
- 如何从 k-d 树实现 K-NN 分类
- 开放CV C 人识别K-NN
- 在 c++ 中调用 lua 函数(带有 require 'nn')
- 使用require(.)或dofile(.)在Lua中加载.dll
- node.js:抑制require()对解析错误的回显到stderr
- 如何更新返回值"res"?(ConcativeMat Con NN)