File size: 7,306 Bytes
28e6f98 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 | from __future__ import print_function, division
import numpy as np
import pandas as pd
from glob import glob
import random
from skimage import transform
from PIL import Image
import os
import torch
from torch.utils.data import Dataset
class Hybrid(Dataset):
def __init__(self, base_dir=None, split='train', MRIDOWN='4X', SNR=15, transform=None):
super().__init__()
self._base_dir = base_dir
self._MRIDOWN = MRIDOWN
self.im_ids = []
self.t2_images = []
self.t1_undermri_images, self.t2_undermri_images = [], []
self.splits_path = "/home/xiaohan/datasets/BRATS_dataset/BRATS_2020_images/cv_splits/"
if split=='train':
self.train_file = self.splits_path + 'train_data.csv'
train_images = pd.read_csv(self.train_file).iloc[:, -1].values.tolist()
self.t1_images = [image for image in train_images if image.split('_')[-1]=='t1.png']
elif split=='test':
self.test_file = self.splits_path + 'test_data.csv'
test_images = pd.read_csv(self.test_file).iloc[:, -1].values.tolist()
# test_images = os.listdir(self._base_dir)
self.t1_images = [image for image in test_images if image.split('_')[-1]=='t1.png']
for image_path in self.t1_images:
t2_path = image_path.replace('t1', 't2')
if SNR == 0:
# t1_under_path = image_path.replace('t1', 't1_' + self._MRIDOWN + '_undermri')
t1_under_path = image_path
t2_under_path = image_path.replace('t1', 't2_' + self._MRIDOWN + '_undermri')
else:
# t1_under_path = image_path.replace('t1', 't1_' + self._MRIDOWN + '_' + str(SNR) + 'dB_undermri')
t1_under_path = image_path.replace('t1', 't1_' + str(SNR) + 'dB')
if MRIDOWN == "False":
t2_under_path = image_path.replace('t1', 't2_' + str(SNR) + 'dB')
else:
t2_under_path = image_path.replace('t1', 't2_' + self._MRIDOWN + '_' + str(SNR) + 'dB_undermri')
# print("image paths:", image_path, t1_under_path, t2_path, t2_under_path)
self.t2_images.append(t2_path)
self.t1_undermri_images.append(t1_under_path)
self.t2_undermri_images.append(t2_under_path)
# print("t1 images:", self.t1_images)
# print("t2 images:", self.t2_images)
# print("t1_undermri_images:", self.t1_undermri_images)
# print("t2_undermri_images:", self.t2_undermri_images)
self.transform = transform
assert (len(self.t1_images) == len(self.t2_images))
assert (len(self.t1_images) == len(self.t1_undermri_images))
assert (len(self.t1_images) == len(self.t2_undermri_images))
# Display stats
print('Number of images in {}: {:d}'.format(split, len(self.t1_images)))
def __len__(self):
return len(self.t1_images)
def __getitem__(self, index):
### 两种settings.
### 1. T1 fully-sampled 不加noise, T2 down-sampled, 做MRI acceleration.
### 2. T1 fully-sampled 但是加noise, T2 down-sampled同时也加noise, 同时做MRI acceleration and enhancement.
### T1, T2两个模态的输入都是low-quality images.
sample = {'image_in': np.array(Image.open(self._base_dir + self.t1_undermri_images[index]))/255.0,
'image': np.array(Image.open(self._base_dir + self.t1_images[index]))/255.0,
'target_in': np.array(Image.open(self._base_dir + self.t2_undermri_images[index]))/255.0,
'target': np.array(Image.open(self._base_dir + self.t2_images[index]))/255.0}
# ### 2023/05/23, Xiaohan, 把T1模态的输入改成high-quality图像(和ground truth一致,看能否为T2提供更好的guidance)。
# sample = {'image_in': np.array(Image.open(self._base_dir + self.t1_images[index]))/255.0,
# 'image': np.array(Image.open(self._base_dir + self.t1_images[index]))/255.0,
# 'target_in': np.array(Image.open(self._base_dir + self.t2_undermri_images[index]))/255.0,
# 'target': np.array(Image.open(self._base_dir + self.t2_images[index]))/255.0}
if self.transform is not None:
sample = self.transform(sample)
return sample
class RandomPadCrop(object):
def __call__(self, sample):
new_w, new_h = 256, 256
crop_size = 240
pad_size = (256-240)//2
img_in = sample['image_in']
img = sample['image']
target_in = sample['target_in']
target = sample['target']
img_in = np.pad(img_in, pad_size, mode='reflect')
img = np.pad(img, pad_size, mode='reflect')
target_in = np.pad(target_in, pad_size, mode='reflect')
target = np.pad(target, pad_size, mode='reflect')
ww = random.randint(0, np.maximum(0, new_w - crop_size))
hh = random.randint(0, np.maximum(0, new_h - crop_size))
# print("img_in:", img_in.shape)
img_in = img_in[ww:ww+crop_size, hh:hh+crop_size]
img = img[ww:ww+crop_size, hh:hh+crop_size]
target_in = target_in[ww:ww+crop_size, hh:hh+crop_size]
target = target[ww:ww+crop_size, hh:hh+crop_size]
sample = {'image_in': img_in, 'image': img, 'target_in': target_in, 'target': target}
return sample
class RandomResizeCrop(object):
"""Convert ndarrays in sample to Tensors."""
def __call__(self, sample):
new_w, new_h = 270, 270
crop_size = 256
img_in = sample['image_in']
img = sample['image']
target_in = sample['target_in']
target = sample['target']
img_in = transform.resize(img_in, (new_h, new_w), order=3)
img = transform.resize(img, (new_h, new_w), order=3)
target_in = transform.resize(target_in, (new_h, new_w), order=3)
target = transform.resize(target, (new_h, new_w), order=3)
ww = random.randint(0, np.maximum(0, new_w - crop_size))
hh = random.randint(0, np.maximum(0, new_h - crop_size))
img_in = img_in[ww:ww+crop_size, hh:hh+crop_size]
img = img[ww:ww+crop_size, hh:hh+crop_size]
target_in = target_in[ww:ww+crop_size, hh:hh+crop_size]
target = target[ww:ww+crop_size, hh:hh+crop_size]
sample = {'image_in': img_in, 'image': img, 'target_in': target_in, 'target': target}
return sample
class ToTensor(object):
"""Convert ndarrays in sample to Tensors."""
def __call__(self, sample):
# swap color axis because
# numpy image: H x W x C
# torch image: C X H X W
img_in = sample['image_in'][:, :, None].transpose((2, 0, 1))
img = sample['image'][:, :, None].transpose((2, 0, 1))
target_in = sample['target_in'][:, :, None].transpose((2, 0, 1))
target = sample['target'][:, :, None].transpose((2, 0, 1))
img_in = torch.from_numpy(img_in).float()
img = torch.from_numpy(img).float()
target_in = torch.from_numpy(target_in).float()
target = torch.from_numpy(target).float()
return {'ct_in': img_in,
'ct': img,
'mri_in': target_in,
'mri': target}
|