繁体   English   中英

在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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM