2024-6-12-IXI(npy存储)应用SR的数据处理代码解读

 数据集:https://drive.google.com/drive/folders/1i2nj-xnv0zBRC-jOtu079Owav12WIpDEicon-default.png?t=N7T8https://drive.google.com/drive/folders/1i2nj-xnv0zBRC-jOtu079Owav12WIpDE

 

import numpy as np
from torch.utils.data import DataLoader, Dataset
import torch
from skimage.measure import shannon_entropy
hr_data_dir: Ji/Data/IXI_npy/4Scale/train/IXI_training_hr_t2_scale_by_4_imgs.npy
lr_data_dir: Ji/Data/IXI_npy/4Scale/train/IXI_training_lr_t2_scale_by_4_imgs.npy
other_data_dir: Ji/Data/IXI_npy/4Scale/train/IXI_training_hr_t1_scale_by_4_imgs.npy
brain_dataset = load_superres_data(
        args.hr_data_dir,
        args.lr_data_dir,
        args.other_data_dir
    )
def load_superres_data(hr_data_dir, lr_data_dir, other_data_dir):
    # Load the super-resolution data using the specified directories
    return load_data(
        hr_data_dir=hr_data_dir,
        lr_data_dir=lr_data_dir,
        other_data_dir=other_data_dir)
def load_data(
        *,
        hr_data_dir,
        lr_data_dir,
        other_data_dir,
        deterministic=False
):
    
    return BraTSMRI(hr_data_dir, lr_data_dir, other_data_dir)
class BraTSMRI(Dataset):
    def __init__(self, hr_data_name, lr_data_name, other_data_name):
#使用 numpy 的 load 函数载入这些文件,并采用内存映射模式 mmap_mode='r'(只读)。这些数据都被
#切片以获取中间的20层(从第40层到第60层)。
        self.hr_data, self.lr_data, self.other_data = np.load(hr_data_name, mmap_mode="r")[:, 40:60 ], \
                                                      np.load(lr_data_name, mmap_mode="r")[:, 40:60], \
                                                      np.load(other_data_name, mmap_mode="r")[:, 40:60]

        num_subject, num_slice, h, w = self.hr_data.shape
        self.hr_data = self.hr_data.reshape(num_subject * num_slice, h, w)
        self.lr_data = self.lr_data.reshape(num_subject * num_slice, h, w)
        self.other_data = self.other_data.reshape(num_subject * num_slice, h, w)

        data_dict = {}
#data_dict:创建一个空字典用于根据熵值存储对应的索引。
#遍历 self.hr_data 中的每个图像,计算其 Shannon 熵(信息熵),并将其四舍五入。
#如果熵值已存在于 data_dict 中,则将当前图像的索引追加到该熵值的列表中;
#如果不存在,则在字典中新建该熵值的条目并添加索引。
        for s in range(len(self.hr_data)):
            entropy = np.round(shannon_entropy(self.hr_data[s]))
            if entropy in data_dict:
                data_dict[entropy].append(s)
            else:
                data_dict[entropy] = [s]

        self.hr_data = torch.from_numpy(self.hr_data).float()
        self.lr_data = torch.from_numpy(self.lr_data).float()
        self.other_data = torch.from_numpy(self.other_data).float()
# 使用 torch.unsqueeze在每个数据的第一个维度(通道维度)添加一维对于后续的神经网络处理很重要。
        self.hr_data = torch.unsqueeze(self.hr_data, 1)
        self.lr_data = torch.unsqueeze(self.lr_data, 1)
        self.other_data = torch.unsqueeze(self.other_data, 1)
        self.data_dict = data_dict
        print(self.hr_data.shape, self.lr_data.shape, self.other_data.shape)

    def __len__(self):
#这里是 self.hr_data 的第一个维度的大小,即所有病例的所有层的数量。
        return self.hr_data.shape[0]

    def __getitem__(self, index):
        return self.hr_data[index], self.lr_data[index], self.other_data[index]
    def get_dataloader(self):
        loader = self.load_dataloader()
        for hr_data, lr_data, other_data in loader:
            model_kwargs = {"low_res": lr_data, "other": other_data}
            yield hr_data, model_kwargs
    def load_dataloader(self, deterministic=False):
        if deterministic:
            loader = DataLoader(
                self.data, batch_size=self.batch_size, shuffle=False, num_workers=8, drop_last=True, pin_memory=True,
            )
        else:
            loader = DataLoader(
                self.data, batch_size=self.batch_size, shuffle=True, num_workers=8, drop_last=True, pin_memory=True
            )

        while True:
            yield from loader

 【MICCAI 2023, Early accept】DisC-Diff: Disentangled Conditional Diffusion Model for Multi-Contrast MRI Super-Resolution

GitHub - Yebulabula/DisC-Diff: 【MICCAI 2023, Early accept】DisC-Diff: Disentangled Conditional Diffusion Model for Multi-Contrast MRI Super-Resolution

  • 15
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值