如何快速转换返回的Python在Lua的numpy的阵列到一个Lua火炬张量? [英] How to quickly convert a returned Python-in-Lua numpy array into a Lua Torch Tensor?

查看:525
本文介绍了如何快速转换返回的Python在Lua的numpy的阵列到一个Lua火炬张量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个返回多维数组numpy的Python函数。我想打电话从Lua这个Python的功能和数据进入一个Lua火炬张量尽可能快的。我有一个很慢有效的解决方案,并正在寻找一种方式,是显著快(10FPS以上顺序)。我不知道这是可能的。

我相信这将是利用他人考虑了Facebook的日益普及,支持火炬,并在Python提供广泛的易于使用的图像处理工具,其中Lua的缺乏。

我为了从Lua调用一个Python函数现在用疯子-蟒Bastibe叉。从这个previous <一个援助href=\"http://stackoverflow.com/questions/20400390/how-to-call-a-python-function-from-lua\">question这<一个href=\"https://labix.org/lunatic-python#head-d0597456187b5ea4006f9427346633f765432975\">documentation,我想出了一些code,它的工作原理,但过于缓慢。我使用的Lua 5.1和Python 2.7.6,如果需要可以更新这些。

Lua的code:test_lua.lua

 要求'火炬'打印(package.loadlib(libpython2.7.so,*))
要求(LUA-蟒蛇)的getImage = python.importtest_python.getImagePB = python.builtins()功能getImageTensor(pythonImageHandle,宽度,高度)
    imageTensor = torch.Tensor(3,高度,宽度)
    image_0 = python.asindx(pythonImageHandle(高度,宽度))
    对于i = 0,身高1做
        IMAGE_1 = python.asindx(image_0 [I])
        对于j = 0,宽度1做
            IMAGE_2 = python.asindx(IMAGE_1 [J]。)
            对于k = 0,2 DO
                 - 张量指数从1开始
                 - 用户蟒蛇内置到INT函数返回整数
                imageTensor [K + 1]第[i + 1] [J + 1] = pb.int(IMAGE_2 [K])/ 255
            结束
        结束
    结束
    返回imageTensor
结束
A = getImageTensor(的getImage,600,400)

Python的code:test_python.py

 进口numpy的
进口操作系统,SYS
进口图片清晰度的getImage(宽度,高度):
    返回numpy.asarray(Image.open(image.jpg的))


解决方案

尝试 lutorpy ,它有一个在python LUA引擎,并能够分享火炬numpy的内存,因此它的速度非常快,这里是code为您的情况:

 进口numpy的
进口图片
进口lutorpy作为LUA的getImage = numpy.asarray(Image.open(image.jpg文件))
A = torch.fromNumpyArray(的getImage)#现在你可以使用你作为火炬手张量图像
#例如:使用SpatialConvolution从神经网络来处理图像
要求(NN)
N = nn.SpatialConvolution(1,16,12,12)
RES = n._forward(一)
打印(res._size())#转换回numpy的数组
输出= res.asNumpyArray()

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

这篇关于如何快速转换返回的Python在Lua的numpy的阵列到一个Lua火炬张量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆