在c ++中调用lua函数(带有require'nn')

时间:2016-04-11 22:50:09

标签: c++ lua torch

我有Lua功能:

YuvImage

我想在C ++中调用此函数,但我收到错误:

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 ++可以识别所有包或其他Lua脚本(如data_augmentation)?

1 个答案:

答案 0 :(得分:0)

我在我的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