How to quickly convert a numpy-in-Lua numpy returned array to a Lua torch tensor?

I have a Python function that returns a multidimensional numpy array. I want to call this Python function from Lua and get the data in the Lua torch tensor as quickly as possible. I have a solution that works quite slowly, and I'm looking for a way that is significantly faster (about 10 frames per second or more). I am not sure if this is possible.

I believe this will be useful to others, given the growing popularity of the torch supported by Facebook, and the extensive easy-to-use image processing tools available in Python, which Lua lacks.

I am using a Bastibe fork for crazy python to call a Python function from Lua. With the help of this previous question and this documentation, I came up with some code that works, but is too slow. I use Lua 5.1 and Python 2.7.6 and can update them if necessary.

Lua Code: "test_lua.lua"

require 'torch' print(package.loadlib("libpython2.7.so", "*")) require("lua-python") getImage = python.import "test_python".getImage pb = python.builtins() function getImageTensor(pythonImageHandle,width,height) imageTensor = torch.Tensor(3,height,width) image_0 = python.asindx(pythonImageHandle(height,width)) for i=0,height-1 do image_1 = python.asindx(image_0[i]) for j=0,width-1 do image_2 = python.asindx(image_1[j]) for k=0,2 do -- Tensor indices begin at 1 -- User python inbuilt to-int function to return integer imageTensor[k+1][i+1][j+1] = pb.int(image_2[k])/255 end end end return imageTensor end a = getImageTensor(getImage,600,400) 

Python code: "test_python.py"

 import numpy import os,sys import Image def getImage(width, height): return numpy.asarray(Image.open("image.jpg")) 
+6
source share
1 answer

Try lutorpy , it has a lua-engine in python and it will be able to exchange numpy memory with a torch, so it is very fast, here is the code for your case:

 import numpy import Image import lutorpy as lua getImage = numpy.asarray(Image.open("image.jpg")) a = torch.fromNumpyArray(getImage) # now you can use your image as torch Tensor # for example: use SpatialConvolution from nn to process the image require("nn") n = nn.SpatialConvolution(1,16,12,12) res = n._forward(a) print(res._size()) # convert back to numpy array output = res.asNumpyArray() 
+3
source

All Articles