【发布时间】:2016-04-30 03:23:37
【问题描述】:
我有一个 .nrrd 文件,我可以将其读入 python 并保存为 np 数组。我想在 lua/torch 中使用生成的数组,我该怎么做?或者有没有办法将 .nrrd 文件直接读取到 lua 中?谢谢。
【问题讨论】:
-
刚刚找到了一个解决方案,使用了一个名为:npy4th 的包。
标签: numpy image-processing lua torch
我有一个 .nrrd 文件,我可以将其读入 python 并保存为 np 数组。我想在 lua/torch 中使用生成的数组,我该怎么做?或者有没有办法将 .nrrd 文件直接读取到 lua 中?谢谢。
【问题讨论】:
标签: numpy image-processing lua torch
与 cmets 一样,npy4th 应该可以满足您的需求。无论如何,我还找到了一个看起来更容易的python库。
您可以通过多种方式将结果导出到 Lua,例如输出到文本文件。我删除了一些有用的 URL。正如我所见,在 Python 中使用它非常简单,只需使用 import nrrd 和 frames, options = nrrd.read("test.nrrd")。
【讨论】:
现在你也可以试试lutorpy,本质上,你可以在python中使用torch和任何lua库。
转换将使用torch.fromNumpyArray(arr) 完成,您将获得一个火炬张量。还有另一个函数tensor.asNumpyArray(),可以帮助您转换回 numpy 数组。
import lutorpy as lua
import numpy as np
xn = np.random.randn(100)
## convert the numpy array into torch tensor
tensor_xn = torch.fromNumpyArray(xn)
# you can use torch tensor as well
t = torch.DoubleTensor(10,3)
print(t._size()) # the corresponding lua version is t:size()
## convert torch tensor to numpy array
arr = t.asNumpyArray()
print(arr.shape)
大多数情况下,转换是通过 numpy 数组和 torch 张量之间的共享内存立即完成的,无需节省磁盘,甚至 无需内存复制。
【讨论】: