简体   繁体   中英

Call lua function (with require 'nn') in c++

I have a Lua function:

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

I want to call this function in C++, but I am getting an error:

PANIC: unprotected error in call to Lua API (attempt to call a nil value)

because of the require '...'

What should I do, so that C++ can recognize all the packages or other Lua scripts (like data_augmentation)?

I was having this same issue on my mac and solved it with

source ~/.profile

then running lua script.

Full instructions found here: 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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