将.nrrd文件读入Lua

我有一个 .nrrd 文件,可以将其读入 python 并保存为 np 数组。我想在 lua/torch 中使用生成的数组,应该如何操作?或者是否有一种直接将 .nrrd 文件读入 lua 的方法?谢谢。

点赞
用户4509141
用户4509141

就像评论中所提到的,npy4th应该可以满足你的需求。不过,我也找到了一个看起来更简单的 Python 库。

你可以用许多方式将结果导出为 Lua,比如将其输出到文本文件中。我放了一些链接,可能有所帮助。据我所见,使用 Python 相当简单,只需 import nrrdframes, options = nrrd.read("test.nrrd")

Pynrrd 的 GitHub 文档

2016-02-09 22:06:08
用户6262499
用户6262499

现在您也可以尝试lutorpy,实质上,您可以在python中使用torch和任何lua库。

转换将使用torch.fromNumpyArray(arr)完成,您将获得torch张量。还有另一个函数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张量之间使用共享内存瞬间完成,无需磁盘保存甚至无需内存复制

2016-05-04 09:04:07