gpt4 book ai didi

lua - 在 Lua 中将二进制数据转换为 Torch 张量

转载 作者:行者123 更新时间:2023-12-04 14:31:22 25 4
gpt4 key购买 nike

我有使用 luasocket 从 url 下载图像的 Lua 代码:

local http = require('socket.http')
local image = require('image')

image_url = 'https://www.somedomain.com/someimage.jpg'
local body, code = http.request(image_url) -- body has jpg binary data
if not body then error(code) end -- check for errors

为了将此图像读入 Torch 张量,我将其保存在 jpg 文件中并使用 image.load 读取它:

-- open a file in binary mode to store the image
local f = assert(io.open('./temp.jpg', 'wb'))
f:write(body)
f:close()

tensor = image.load('temp.jpg')

有没有一种方法可以直接将二进制 jpg 数据转换为 torch 张量,而无需从硬盘驱动器进行写入和读取?像这样的东西:

tensor = CovertBinaryDataToTorchTensor(body)

谢谢!

最佳答案

参见 image.decompressJPG .

您只需先将 body 字符串打包到 ByteTensor 中。这可以通过用 storage 构造这个张量来完成。可以用 string(str) 设置他的内容.

关于lua - 在 Lua 中将二进制数据转换为 Torch 张量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35641536/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com