I have a Python function that returns a multi-dimensional numpy array. I want to call this Python function from Lua and get the data into a Lua Torch Tensor as quickly as possible. I have a solution that works quite slowly and am looking for a way that is significantly faster (order of 10fps or more). I'm not sure if this is possible.
I believe this will be of use to others considering the growing popularity of Facebook backed Torch and the extensive easy-to-use image-processing tools available in Python of which Lua lacks.
I am using the Bastibe fork of lunatic-python in order to call a Python function from Lua. With aid from this previous question and this documentation, I have come up with some code that works, but is far too slow. I am using Lua 5.1 and Python 2.7.6 and can update these 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"))
No way to call python functions with keyword arguments from lua. One idea is to emulate this in lua by passing a table but I never got around to implementing that. Unlike lupa, lunatic-python only has one global lua state and one python VM context. So you can't create multiple VM runtimes using lunatic-python.
To convert a Numpy array to a PyTorch tensor - we have two distinct approaches we could take: using the from_numpy () function, or by simply supplying the Numpy array to the torch.Tensor () constructor or by using the tensor () function: So, what's the difference? The from_numpy () and tensor () functions are dtype -aware!
Use Tensor.cpu () to copy the tensor to host memory first. np_c = tensor.detach ().cpu ().numpy () # array ( [1., 2., 3., 4., 5.], dtype=float32) Note: It's highly advised to call detach () before cpu (), to prune away the gradients before transferring to the CPU.
TensorFlow is an open-source library for AI/ML. It primarily focuses on training and analysis of Deep Neural Networks. Let’s see how we convert Tensors from TensorFlow into arrays. Tensors can be converted into regular arrays with the help of the .eval () function.
Try lutorpy, it has a lua engine in python and be able to share numpy memory with torch, so it's 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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With