代码参考:https://github.com/sinclairjang/3D-MRI-brain-tumor-segmentation-using-autoencoder-regularization
数据集来源:BraTs 2018
参考论文:https://arxiv.org/abs/1810.11654
3D-MRI-brain-tumor-segmentation-using-autoencoder-regularization
参考作者数据集预处理方式,后续模型将持续更新…
t1 = glob.glob(r'/home/locco/brats17/MICCAI_BraTS_2019_Data_Training/MICCAI_BraTS_2019_Data_Training/*/*/*t1.nii.gz')
t2 = glob.glob(r'/home/locco/brats17/MICCAI_BraTS_2019_Data_Training/MICCAI_BraTS_2019_Data_Training/*/*/*t2.nii.gz')
flair = glob.glob(r'/home/locco/brats17/MICCAI_BraTS_2019_Data_Training/MICCAI_BraTS_2019_Data_Training/*/*/*flair.nii.gz')
t1ce = glob.glob(r'/home/locco/brats17/MICCAI_BraTS_2019_Data_Training/MICCAI_BraTS_2019_Data_Training/*/*/*t1ce.nii.gz')
seg = glob.glob(r'/home/locco/brats17/MICCAI_BraTS_2019_Data_Training/MICCAI_BraTS_2019_Data_Training/*/*/*seg.nii.gz')
分别生成t1、t2、flair、t1ce、seg对应文件目录
利用SimpleITK读取图像
def read_img(img_path):
return sitk.GetArrayFromImage(sitk.ReadImage(img_path))
读取图像,并可视化(可视化的是三维图像中的一个切片图像)
import matplotlib.pyplot as plt
img = (read_img(t1[0])[100]).astype(np.uint8)
plt.imshow(img)
def resize(img, shape, mode='constant', orig_shape=(155, 240, 240)):
"""
Wrapper for scipy.ndimage.zoom suited for MRI images.
"""
assert len(shape) == 3, "Can not have more than 3 dimensions"
factors = (
shape[0]/orig_shape[0],
shape[1]/orig_shape[1],
shape[2]/orig_shape[2]
)
# Resize to the given shape
return zoom(img, factors, mode=mode)
def preprocess(img, out_shape=None):
"""
Preprocess the image.
Just an example, you can add more preprocessing steps if you wish to.
"""
if out_shape is not None:
img = resize(img, out_shape, mode='constant')
# Normalize the image
mean = img.mean()
std = img.std()
return (img - mean) / std
def preprocess_label(img, out_shape=None, mode='nearest'):
"""
Separates out the 3 labels from the segmentation provided, namely:
GD-enhancing tumor (ET — label 4), the peritumoral edema (ED — label 2))
and the necrotic and non-enhancing tumor core (NCR/NET — label 1)
"""
ncr = img == 1 # Necrotic and Non-Enhancing Tumor (NCR/NET)
ed = img == 2 # Peritumoral Edema (ED)
et = img == 4 # GD-enhancing Tumor (ET)
if out_shape is not None:
ncr = resize(ncr, out_shape, mode=mode)
ed = resize(ed, out_shape, mode=mode)
et = resize(et, out_shape, mode=mode)
return np.array([ncr, ed, et], dtype=np.uint8)
input_shape = (4, 80, 96, 64)
output_channels = 3
data = np.empty((len(data_paths),) + input_shape, dtype=np.float32)
labels = np.empty((len(data_paths), output_channels) + input_shape[1:], dtype=np.uint8)
import math
# Parameters for the progress bar
total = len(data_paths)
step = 25 / total
for i, imgs in enumerate(data_paths):
try:
data[i] = np.array([preprocess(read_img(imgs[m]), input_shape[1:]) for m in ['t1', 't2', 't1ce', 'flair']], dtype=np.float32)
labels[i] = preprocess_label(read_img(imgs['seg']), input_shape[1:])[None, ...]
# Print the progress bar
print('\r' + f'Progress: '
f"[{'=' * int((i+1) * step) + ' ' * (24 - int((i+1) * step))}]"
f"({math.ceil((i+1) * 100 / (total))} %)",
end='')
except Exception as e:
print(f'Something went wrong with {imgs["t1"]}, skipping...\n Exception:\n{str(e)}')
continue
本文仅po出BraTs数据集加载与预处理部分,刚好本人最近再看医学图像分割方面的论文。由于数据集为3D MRI数据集,处理时还需要大家了解一定的医学图象知识。
作者原文中采用了一种改良的U-Net网络进行训练,后续将更新模型部分详解,欢迎交流。