共计 2246 个字符,预计需要花费 6 分钟才能阅读完成。
BRATS2020 数据集概述
BRATS2020 是脑肿瘤分割领域的权威数据集,包含多模态 MRI 扫描(T1、T1c、T2、FLAIR)和专家标注的肿瘤子区域(坏死 / 水肿、增强肿瘤、肿瘤核心)。每个病例包含 155 张轴向切片,图像尺寸为 240×240 像素,特别适合研究 3D 上下文信息在医学影像分析中的应用价值。

数据加载与预处理
1. NIfTI 格式解析
推荐使用 nibabel 库加载 NIfTI 文件,它能自动处理头部信息并返回 numpy 数组:
import nibabel as nib
def load_nii(filepath):
"""加载 NIfTI 文件并返回数据和 affine 矩阵"""
img = nib.load(filepath)
return img.get_fdata(), img.affine
处理缺失模态时建议添加有效性检查:
modalities = ['t1', 't1ce', 't2', 'flair']
for mod in modalities:
if not os.path.exists(f'{case_id}_{mod}.nii.gz'):
print(f'Warning: Missing {mod} for case {case_id}')
# 可用零矩阵或相邻切片插值替代
2. 标准化与裁剪
医学影像的强度值差异较大,需做 Z -score 标准化:
def normalize(img):
"""针对非零区域做强度归一化"""
mask = img > 0
mean = img[mask].mean()
std = img[mask].std()
return (img - mean) / (std + 1e-8)
脑部区域裁剪可减少计算量:
def crop_brain_region(vol, margin=5):
"""根据非零区域自动裁剪,保留 margin 像素边界"""
coords = np.where(vol > 0)
min_z, max_z = np.min(coords[0]), np.max(coords[0])
min_y, max_y = np.min(coords[1]), np.max(coords[1])
min_x, max_x = np.min(coords[2]), np.max(coords[2])
return vol[min_z-margin:max_z+margin,
min_y-margin:max_y+margin,
min_x-margin:max_x+margin]
3. 数据增强
3D 数据增强需要保持空间一致性:
import torchio as tio
transforms = tio.Compose([tio.RandomFlip(axes=(0,1,2), p=0.5),
tio.RandomAffine(scales=(0.9, 1.1),
degrees=10,
translation=5),
tio.RandomNoise(std=0.01)
])
3D U-Net 模型实现
基础结构
import torch.nn as nn
class DoubleConv(nn.Module):
"""(卷积 → BN → ReLU) × 2"""
def __init__(self, in_ch, out_ch):
super().__init__()
self.net = nn.Sequential(nn.Conv3d(in_ch, out_ch, kernel_size=3, padding=1),
nn.BatchNorm3d(out_ch),
nn.ReLU(inplace=True),
nn.Conv3d(out_ch, out_ch, kernel_size=3, padding=1),
nn.BatchNorm3d(out_ch),
nn.ReLU(inplace=True)
)
def forward(self, x):
return self.net(x)
多模态处理
将 4 模态数据堆叠为 4 通道输入:
# 假设输入形状为 [batch, modalities, D, H, W]
x = torch.cat([t1, t1ce, t2, flair], dim=1)
显存优化技巧
使用 patch-based 训练减少显存占用:
def extract_patches(volume, patch_size=64):
"""将大体积图像拆分为重叠的小 patch"""
return volume.unfold(1, patch_size, patch_size//2)
.unfold(2, patch_size, patch_size//2)
.unfold(3, patch_size, patch_size//2)
避坑指南
- 患者级数据划分:必须确保同一患者的扫描不会同时出现在训练集和验证集
- 测试集标签不可见:官方测试集标签需通过在线平台提交预测结果获取
- 模态顺序一致性:不同病例的模态存储顺序可能不同,需通过文件名明确指定
- 标签重映射:原始标签为[0,1,2,4],需转换为连续整数[0,1,2,3]
延伸思考
- 如何处理肿瘤区域和非肿瘤区域的极端类别不平衡?
- 能否利用未标注病例(BRATS2020 包含 369 例无标签数据)进行半监督学习?
- 不同模态之间是否存在最优融合策略(如早期 / 晚期融合)?
参考文献
- BraTS 2020 Challenge 官方说明
- Bakas S, et al. “Advancing The Cancer Genome Atlas glioma MRI collections with expert segmentation labels and radiomic features” Nature Scientific Data, 2017
正文完
