简体   繁体   English

为什么在 MONAI 转换中使用 SimpleITK 库时 Python 停止在 GPU 上工作?

[英]Why Python stops to work on GPU when using SimpleITK library in MONAI transforms?

I'm using Python 3.9 with Spyder 5.2.2 (Anaconda) for a U-Net segmentation task with MONAI.我将 Python 3.9 与 Spyder 5.2.2 (Anaconda) 一起用于 MONAI 的 U-Net 分割任务。 After importing all the images in a dictionary, I create these lines to define pre-process steps:将所有图像导入字典后,我创建了这些行来定义预处理步骤:


import SimpleITK as sitk
from monai.inferers import SimpleInferer
from monai.transforms import (
    AsDiscrete,
    DataStatsd,
    AddChanneld,
    Compose,
    Activations,
    LoadImaged,
    Resized,
    RandFlipd,
    ScaleIntensityRanged,
    DataStats,
    AsChannelFirstd,
    AsDiscreted,
    ToTensord,
    EnsureTyped,
    RepeatChanneld,
    EnsureType
)

from monai.transforms import Transform


monai_load = [
    LoadImaged(keys=["image","segmentation"],image_only=False,reader=PILReader()),
    EnsureTyped(keys=["image", "segmentation"], data_type="numpy"),
    AddChanneld(keys=["segmentation","image"]),   
    RepeatChanneld(keys=["image"],repeats=3),
    AsChannelFirstd(keys=["image"], channel_dim = 0), 
    ]


monai_transforms =[
    AsDiscreted(keys=["segmentation"],threshold=0.5),
    ToTensord(keys=["image","segmentation"]),
    ]

class N4ITKTransform(Transform):

    def __call__(self,image):
        filtered = []
        for channel in image["image"]:
            inputImage = sitk.GetImageFromArray(channel)
            inputImage = sitk.Cast(inputImage, sitk.sitkFloat32)
            corrector = sitk.N4BiasFieldCorrectionImageFilter()
            outputImage = corrector.Execute(inputImage)
            filtered.append(sitk.GetArrayFromImage(outputImage))
        image["image"] = np.stack(filtered)
        
        return image

train_transforms = Compose(monai_load + [N4ITKTransform()] + monai_transforms)

When i recall these transforms with Compose and apply them to the train images, python does not work on GPU despite当我用 Compose 回忆起这些变换并将它们应用于火车图像时,python 对 GPU 不起作用,尽管

torch.cuda.is_available()

return True.返回真。

These are the lines where I apply the transforms:这些是我应用转换的行:

train_ds = IterableDataset(data = train_data, transform = train_transforms)
train_loader = DataLoader(dataset = train_ds, batch_size = batch_size, num_workers = 0, pin_memory = True)

When I define the U-Net model, I send it to 'cuda'.当我定义 U-Net model 时,我将它发送到“cuda”。

The problem is in the SimpleITK transform.问题出在 SimpleITK 转换中。 If I don't use them, Python works on GPU as usual.如果我不使用它们,Python 会像往常一样在 GPU 上工作。

Thank you in advance for getting back to me.预先感谢您回复我。

Federico费德里科

The answer is simple: SimpleITK uses CPU for processing.答案很简单:SimpleITK 使用 CPU 进行处理。

I am not sure whether it is possible to get it to use some of the GPU-accelerated filters from ITK (its base library).我不确定是否可以让它使用 ITK(其基础库)中的一些 GPU 加速过滤器。 If you use ITK Python, you have the possibility to use GPU-filters.如果您使用 ITK Python,则可以使用 GPU 过滤器。 But only a few filters have GPU implementations.但只有少数过滤器有 GPU 个实现。 N4BiasFieldCorrection does NOT have a GPU implementation. N4BiasFieldCorrection没有 GPU 实现。 So if you want to use this filter, it needs to be done on the CPU.所以如果要使用这个过滤器,需要在CPU上完成。

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

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