简体   繁体   English

使用 SimpleITK 和 PyTorch 读取 dicom 文件的性能

[英]Performance in reading dicom files with SimpleITK and PyTorch

I want to directly load image from memory to python in pytorch tensor format.我想以 pytorch 张量格式直接将图像从 memory 加载到 python。 I modified GetArrayViewFromImage() function by replacing those lines:我通过替换这些行修改了 GetArrayViewFromImage() function:

image_memory_view = _GetMemoryViewFromImage(image)
array_view = numpy.asarray(image_memory_view).view(dtype = dtype)

by:经过:

image_memory_view = _GetMemoryViewFromImage(image)
array_view = torch.as_tensor(image_memory_view, dtype = dtype)

in practise it is so slow I replaced it with:在实践中它太慢了,我将其替换为:

image_memory_view = _GetMemoryViewFromImage(image)
array_view = numpy.asarray(image_memory_view).view(dtype = dtype)
array_view  = torch.as_tensor(array_view)

Now I have two questions:现在我有两个问题:

  1. it is much slower, and I don't really know why reading it with numpy and converting it is faster.它慢得多,我真的不知道为什么用 numpy 阅读它并转换它更快。
  2. even though I add the dtype argument and it returns a tensor with a correct dtype it reads it wrong (ex. -1000 in numpy is read as 252 no matter what torch.dtype I choose) which is not a problem when reading with numpy and converting, why is that happening?即使我添加了 dtype 参数并且它返回了一个具有正确 dtype 的张量,但它读错了(例如,numpy 中的 -1000 被读取为 252,无论我选择什么 torch.dtype),这在使用 numpy 和转换,为什么会这样?

While this does not directly answer your question, I strongly recommend using the torchio package, instead of dealing with these IO issues yourself (torchio uses SimpleITK under the hood).虽然这不能直接回答您的问题,但我强烈建议使用torchio package,而不是自己处理这些 IO 问题(torchio 在引擎盖下使用 SimpleITK)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM