使用pix2pix-gan做医学图像合成的时候,如果把nii数据转成png格式会损失很多信息,以为png格式图像的灰度值有256阶,因此直接使用nii的医学图像做输入会更好一点。
但是Pythorch中的Dataloader是不能直接读取nii图像的,因此加一个CreateNiiDataset的类。
先来了解一下pytorch中读取数据的主要途径——Dataset类。在自己构建数据层时都要基于这个类,类似于C++中的虚基类。
自己构建的数据层包含三个部分
class Dataset(object):
"""An abstract class representing a Dataset.
All other datasets should subclass it. All subclasses should override
``__len__``, that provides the size of the dataset, and ``__getitem__``,
supporting integer indexing in range from 0 to len(self) exclusive.
"""
def __getitem__(self, index):
raise NotImplementedError
def __len__(self):
raise NotImplementedError
def __add__(self, other):
return ConcatDataset([self, other])
根据自己的需要编写CreateNiiDataset子类:
因为我是基于https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix
做pix2pix-gan的实验,数据包含两个部分mr 和 ct,不需要标签,因此上面的 def getitem(self, index):中不需要index这个参数了,类似地,根据需要,加入自己的参数,去掉不需要的参数。

在使用Pix2Pix-GAN进行医学图像合成时,为避免信息损失,直接使用nii格式的医学图像作为输入。由于PyTorch的DataLoader不支持直接读取nii图像,需要创建自定义的CreateNiiDataset类来扩展PyTorch的Dataset基类。该类主要包含数据读取、处理和转换。在getitem方法中,根据实验需求调整参数,并确保输出的数据字典中包含FloatTensor类型的图像数据和对应的路径信息。最后,在train.py的主函数中替换数据加载部分,即可实现nii数据的读取和处理。
最低0.47元/天 解锁文章
2万+

被折叠的 条评论
为什么被折叠?



