【发布时间】:2016-08-02 07:43:14
【问题描述】:
我有一个 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)?
【问题讨论】:
-
您需要在所有库组件上调用
luaL_dofile之类的东西。 -
我了解如何为我创建的脚本(如 data_augmentation)执行此操作。但是对于'nn'、'optim'、'image'和'torch',我应该写什么呢?我假设我必须用我的编译命令给出包的路径,对吧?提前谢谢你。
-
我认为您需要在执行此脚本之前加载
package库。所以创建新的 Lua stet,至少打开base和package库。最好打开所有标准库。 -
寻找答案,我意识到我必须使用:luaL_requiref(L,"nn",luaopen_nn,1);在 luaL_openlibs(); 之后。问题是我应该在编译命令中包含包“nn”的路径。我正在使用 -l:nn.so.1 但它不起作用。有什么帮助吗?