Download DiffAtlas/Dataset/MMWHS_Dataset.py from kanydao/backup: direct link, hf CLI and curl.
- Browser
- Download file 11.5 kB
-
https://huggingface.co/datasets/kanydao/backup/resolve/main/DiffAtlas/Dataset/MMWHS_Dataset.py
- Command line
-
hf download hf://datasets/kanydao/backup/DiffAtlas/Dataset/MMWHS_Dataset.py
-
curl -L -o MMWHS_Dataset.py https://huggingface.co/datasets/kanydao/backup/resolve/main/DiffAtlas/Dataset/MMWHS_Dataset.py
11.5 kB
| # from concurrent.futures import ThreadPoolExecutor | |
| # import sys | |
| # import os | |
| # import numpy as np | |
| # import torch | |
| # from torch.utils.data.dataset import Dataset | |
| # import glob | |
| # import cv2 | |
| # import torchio as tio | |
| # import matplotlib.pyplot as plt | |
| # from torch.utils.data import DataLoader | |
| # from sdf import compute_sdf | |
| # PREPROCESSING_TRANSORMS_CT = tio.Compose([ | |
| # tio.Clamp(out_min=-250, out_max=800), | |
| # tio.RescaleIntensity(in_min_max=(-250, 800), | |
| # out_min_max=(-1.0, 1.0)), | |
| # tio.CropOrPad(target_shape=(64, 64, 64)) | |
| # ]) | |
| # PREPROCESSING_TRANSORMS_MRI = tio.Compose([ | |
| # tio.Clamp(out_min=0, out_max=1000), | |
| # tio.RescaleIntensity(in_min_max=(0, 1000), | |
| # out_min_max=(-1.0, 1.0)), | |
| # tio.CropOrPad(target_shape=(64, 64, 64)) | |
| # ]) | |
| # PREPROCESSING_MASK_TRANSORMS = tio.Compose([ | |
| # tio.CropOrPad(target_shape=(64, 64, 64)) | |
| # ]) | |
| # class MMWHS_Dataset(Dataset): | |
| # def __init__(self, root_dir='', data_type='', mode = ''): | |
| # self.root_dir = root_dir | |
| # self.data_type = data_type | |
| # self.mode = mode | |
| # self.file_names = self.get_file_names() | |
| # self.preprocessing_img_ct = PREPROCESSING_TRANSORMS_CT | |
| # self.preprocessing_img_mri = PREPROCESSING_TRANSORMS_MRI | |
| # self.preprocessing_mask = PREPROCESSING_MASK_TRANSORMS | |
| # @staticmethod | |
| # def create_mask(shape): | |
| # return torch.zeros(shape, dtype=torch.uint8) | |
| # @staticmethod | |
| # def project_to_2d(mask): | |
| # projection = torch.max(mask, dim=0)[0] | |
| # return projection.numpy() | |
| # @staticmethod | |
| # def min_enclosing_circle(projection): | |
| # points = np.column_stack(np.where(projection > 0)) | |
| # points = points.astype(np.float32) | |
| # print(points.shape) | |
| # (x, y), radius = cv2.minEnclosingCircle(points.astype(np.float32)) | |
| # center = (int(x), int(y)) | |
| # radius = int(radius) | |
| # return center, radius | |
| # @staticmethod | |
| # def create_circle_mask_2d(shape, center, radius): | |
| # mask = np.zeros(shape, dtype=np.uint8) | |
| # cv2.circle(mask, center, radius, 1, thickness=-1) | |
| # return mask | |
| # @staticmethod | |
| # def apply_circle_mask_to_3d(mask, circle_mask_2d): | |
| # for i in range(mask.shape[0]): | |
| # mask[i] = torch.from_numpy(circle_mask_2d) | |
| # return mask | |
| # def train_transform(self, image, label, p): | |
| # TRAIN_TRANSFORMS = tio.Compose([ | |
| # tio.RandomFlip(axes=(1), flip_probability=p), | |
| # ]) | |
| # image = TRAIN_TRANSFORMS(image) | |
| # label = TRAIN_TRANSFORMS(label) | |
| # return image, label | |
| # def get_file_names(self): | |
| # all_img_names = glob.glob(os.path.join(self.root_dir, './*image.nii.gz'), recursive=True) | |
| # return all_img_names | |
| # def __len__(self): | |
| # return len(self.file_names) | |
| # def __getitem__(self, index): | |
| # img_path = self.file_names[index] | |
| # mask_path = img_path.replace("image.nii.gz", "label.nii.gz") | |
| # sdf_path = img_path.split() | |
| # img = tio.ScalarImage(img_path) | |
| # mask = tio.LabelMap(mask_path) | |
| # name = img_path.split('/')[-1] | |
| # name = name.split('.nii')[0] | |
| # if self.data_type.upper() == 'CT': | |
| # img = self.preprocessing_img_ct(img) | |
| # elif self.data_type.upper() == 'MRI': | |
| # img = self.preprocessing_img_mri(img) | |
| # else: | |
| # raise ValueError("Wrong Data Type!") | |
| # mask = self.preprocessing_mask(mask) | |
| # # p = np.random.choice([0, 1]) | |
| # # if self.mode == 'train': | |
| # # img, mask = self.train_transform(img, mask, p) | |
| # affine = img.affine | |
| # mask = mask.data | |
| # img = img.data | |
| # label_1 = (mask == 1).float() | |
| # label_1_sdf = compute_sdf(label_1) | |
| # label_2 = (mask == 2).float() | |
| # label_2_sdf = compute_sdf(label_2) | |
| # label_3 = (mask == 3).float() | |
| # label_3_sdf = compute_sdf(label_3) | |
| # label_4 = (mask == 4).float() | |
| # label_4_sdf = compute_sdf(label_4) | |
| # label_5 = (mask == 5).float() | |
| # label_5_sdf = compute_sdf(label_5) | |
| # label = torch.cat((label_1, label_2, label_3, label_4, label_5), dim=0) | |
| # label_sdf = torch.cat((torch.tensor(label_1_sdf), torch.tensor(label_2_sdf), torch.tensor(label_3_sdf), torch.tensor(label_4_sdf), torch.tensor(label_5_sdf)), dim=0) | |
| # sdf_to_save = tio.ScalarImage(tensor=label_sdf, channels_last=False, affine=affine) | |
| # os.makedirs(self.root_dir, exist_ok=True) | |
| # sdf_to_save.save(os.path.join(self.root_dir, f"{name.replace('image', 'sdf')}.nii.gz")) | |
| # return { | |
| # 'name': name, | |
| # 'img': img, | |
| # 'mask_sdf': label_sdf, | |
| # 'mask': label, | |
| # 'affine': affine | |
| # } | |
| # def get_MMWHS_dataloader(root_dir, data_type, mode, batch_size=1, drop_last=False): | |
| # dataset = MMWHS_Dataset(root_dir=root_dir, data_type=data_type, mode=mode) | |
| # if mode == 'train': | |
| # shuffle = True | |
| # return dataset | |
| # elif mode == 'test': | |
| # shuffle = False | |
| # else: | |
| # raise ValueError('No Such Mode') | |
| # loader = DataLoader( | |
| # dataset, batch_size=batch_size, shuffle=shuffle, num_workers=20, pin_memory=True, drop_last=drop_last | |
| # ) | |
| # return loader | |
| # def test_dataset(): | |
| # dl = get_MMWHS_dataloader(root_dir='/mnt/data2/data/store/MSN/data_v2/MMWHS-CT/MMWHS-CT-resize-try-tiny-train', data_type='CT', mode='test') | |
| # for batch in iter(dl): | |
| # print(batch['name']) | |
| # print(len(dl)) | |
| # if __name__ == '__main__': | |
| # test_dataset() | |
| from concurrent.futures import ThreadPoolExecutor | |
| import sys | |
| import os | |
| import numpy as np | |
| import torch | |
| from torch.utils.data.dataset import Dataset | |
| import glob | |
| import cv2 | |
| import torchio as tio | |
| import matplotlib.pyplot as plt | |
| from torch.utils.data import DataLoader | |
| from sdf import compute_sdf | |
| PREPROCESSING_TRANSORMS_CT = tio.Compose([ | |
| tio.Clamp(out_min=-250, out_max=800), | |
| tio.RescaleIntensity(in_min_max=(-250, 800), | |
| out_min_max=(-1.0, 1.0)), | |
| tio.CropOrPad(target_shape=(64, 64, 64)) | |
| ]) | |
| PREPROCESSING_TRANSORMS_MRI = tio.Compose([ | |
| tio.Clamp(out_min=0, out_max=1000), | |
| tio.RescaleIntensity(in_min_max=(0, 1000), | |
| out_min_max=(-1.0, 1.0)), | |
| tio.CropOrPad(target_shape=(64, 64, 64)) | |
| ]) | |
| PREPROCESSING_MASK_TRANSORMS = tio.Compose([ | |
| tio.CropOrPad(target_shape=(64, 64, 64)) | |
| ]) | |
| class MMWHS_Dataset(Dataset): | |
| def __init__(self, root_dir='', data_type='', mode = ''): | |
| self.root_dir = root_dir | |
| self.data_type = data_type | |
| self.mode = mode | |
| self.file_names = self.get_file_names() | |
| self.preprocessing_img_ct = PREPROCESSING_TRANSORMS_CT | |
| self.preprocessing_img_mri = PREPROCESSING_TRANSORMS_MRI | |
| self.preprocessing_mask = PREPROCESSING_MASK_TRANSORMS | |
| def create_mask(shape): | |
| return torch.zeros(shape, dtype=torch.uint8) | |
| def project_to_2d(mask): | |
| projection = torch.max(mask, dim=0)[0] | |
| return projection.numpy() | |
| def min_enclosing_circle(projection): | |
| points = np.column_stack(np.where(projection > 0)) | |
| points = points.astype(np.float32) | |
| print(points.shape) | |
| (x, y), radius = cv2.minEnclosingCircle(points.astype(np.float32)) | |
| center = (int(x), int(y)) | |
| radius = int(radius) | |
| return center, radius | |
| def create_circle_mask_2d(shape, center, radius): | |
| mask = np.zeros(shape, dtype=np.uint8) | |
| cv2.circle(mask, center, radius, 1, thickness=-1) | |
| return mask | |
| def apply_circle_mask_to_3d(mask, circle_mask_2d): | |
| for i in range(mask.shape[0]): | |
| mask[i] = torch.from_numpy(circle_mask_2d) | |
| return mask | |
| def train_transform(self, image, label, sdf, p): | |
| TRAIN_TRANSFORMS = tio.Compose([ | |
| tio.RandomFlip(axes=(1), flip_probability=p), | |
| ]) | |
| image = TRAIN_TRANSFORMS(image) | |
| label = TRAIN_TRANSFORMS(label) | |
| sdf = TRAIN_TRANSFORMS(sdf) | |
| return image, label, sdf | |
| def get_file_names(self): | |
| all_img_names = glob.glob(os.path.join(self.root_dir, './*image.nii.gz'), recursive=True) | |
| return all_img_names | |
| def __len__(self): | |
| return len(self.file_names) | |
| def __getitem__(self, index): | |
| img_path = self.file_names[index] | |
| mask_path = img_path.replace("image.nii.gz", "label.nii.gz") | |
| sdf_path = img_path.replace("image.nii.gz", "sdf.nii.gz") | |
| img = tio.ScalarImage(img_path) | |
| mask = tio.LabelMap(mask_path) | |
| sdf = tio.ScalarImage(sdf_path) | |
| name = img_path.split('/')[-1] | |
| name = name.split('.nii')[0] | |
| if self.data_type.upper() == 'CT': | |
| img = self.preprocessing_img_ct(img) | |
| elif self.data_type.upper() == 'MRI': | |
| img = self.preprocessing_img_mri(img) | |
| else: | |
| raise ValueError("Wrong Data Type!") | |
| mask = self.preprocessing_mask(mask) | |
| p = np.random.choice([0, 1]) | |
| if self.mode == 'train': | |
| img, mask, sdf = self.train_transform(img, mask, sdf, p) | |
| affine = img.affine | |
| mask = mask.data | |
| img = img.data | |
| sdf = sdf.data | |
| label_1 = (mask == 1).float() | |
| # label_1_sdf = compute_sdf(label_1) | |
| label_2 = (mask == 2).float() | |
| # label_2_sdf = compute_sdf(label_2) | |
| label_3 = (mask == 3).float() | |
| # label_3_sdf = compute_sdf(label_3) | |
| label_4 = (mask == 4).float() | |
| # label_4_sdf = compute_sdf(label_4) | |
| label_5 = (mask == 5).float() | |
| # label_5_sdf = compute_sdf(label_5) | |
| label = torch.cat((label_1, label_2, label_3, label_4, label_5), dim=0) | |
| # label_sdf = torch.cat((torch.tensor(label_1_sdf), torch.tensor(label_2_sdf), torch.tensor(label_3_sdf), torch.tensor(label_4_sdf), torch.tensor(label_5_sdf)), dim=0) | |
| label_sdf = sdf | |
| # sdf_to_save = tio.LabelMap(tensor=label_sdf, channels_last=False, affine=affine) | |
| # os.makedirs(self.root_dir, exist_ok=True) | |
| # sdf_to_save.save(os.path.join(self.root_dir, f"{name.replace('image', 'sdf')}.nii.gz")) | |
| return { | |
| 'name': name, | |
| 'img': img, | |
| 'mask_sdf': label_sdf, | |
| 'mask': label, | |
| 'affine': affine | |
| } | |
| def get_MMWHS_dataloader(root_dir, data_type, mode, batch_size=1, drop_last=False): | |
| dataset = MMWHS_Dataset(root_dir=root_dir, data_type=data_type, mode=mode) | |
| if mode == 'train': | |
| shuffle = True | |
| return dataset | |
| elif mode == 'test': | |
| shuffle = False | |
| else: | |
| raise ValueError('No Such Mode') | |
| loader = DataLoader( | |
| dataset, batch_size=batch_size, shuffle=shuffle, num_workers=20, pin_memory=True, drop_last=drop_last | |
| ) | |
| return loader | |
| # def test_dataset(): | |
| # dl = get_MMWHS_dataloader(root_dir='/mnt/data2/data/store/MSN/data/MMWHS-MRI/mr_train_clean_resize_to_64_train_10percent', data_type='MRI', mode='test') | |
| # for batch in iter(dl): | |
| # print(batch['name']) | |
| # print(len(dl)) | |
| # if __name__ == '__main__': | |
| # test_dataset() | |