简体   繁体   中英

How to quickly convert a returned Python-in-Lua numpy array into a Lua Torch Tensor?

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"))

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()

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