CNN 脑肿瘤诊断方案MATLAB 实现
一、CNN 在脑肿瘤诊断中的两种典型用法(MATLAB )
| 任务 | 输入 | 输出 | 常用 CNN |
|---|---|---|---|
| 分类 | 2D MRI 切片 | 肿瘤类型 / 是否患病 | ResNet / AlexNet / GoogLeNet |
| 分割 | 2D / 3D MRI | 肿瘤区域掩码 | U‑Net / SegNet |
MATLAB 官方强烈推荐:分类用 ResNet,分割用 U‑Net
二、脑肿瘤分类(ResNet18,最稳妥方案)
1、数据集准备(标准结构)
dataset/
├── glioma/
├── meningioma/
├── pituitary/
└── normal/
每张图片为 jpg / png / bmp
2、数据加载与增强(关键)
dataDir = "dataset";
imds = imageDatastore( ...
dataDir, ...
"IncludeSubfolders", true, ...
"LabelSource", "foldernames");
% 数据增强(医学影像必备)
aug = imageDataAugmenter( ...
"RandRotation",[-15 15], ...
"RandXReflection",true, ...
"RandYReflection",true);
auimds = augmentedImageDatastore([224 224], imds, ...
"DataAugmentation", aug);
3、构建 CNN(迁移学习)
net = resnet18;
lgraph = layerGraph(net);
% 替换全连接层
newFc = fullyConnectedLayer( ...
4, ...
"Name","fc_new", ...
"WeightLearnRateFactor",10, ...
"BiasLearnRateFactor",10);
lgraph = replaceLayer(lgraph,"fc1000",newFc);
lgraph = replaceLayer(lgraph,"prob", ...
softmaxLayer("Name","softmax"));
lgraph = replaceLayer(lgraph,"ClassificationLayer_predictions", ...
classificationLayer("Name","output"));
4、 训练 CNN
options = trainingOptions("adam", ...
"InitialLearnRate",1e-4, ...
"MaxEpochs",15, ...
"MiniBatchSize",16, ...
"Shuffle","every-epoch", ...
"ValidationData",auimds, ...
"ValidationFrequency",30, ...
"Plots","training-progress", ...
"Verbose",false);
[netTrained, info] = trainNetwork(auimds, lgraph, options);
5、 测试与评估
testImds = imageDatastore( ...
"test_dataset", ...
"IncludeSubfolders",true, ...
"LabelSource","foldernames");
YPred = classify(netTrained, testImds);
YTrue = testImds.Labels;
accuracy = mean(YPred == YTrue)
confusionchart(YTrue, YPred)
典型结果:
4 类脑肿瘤准确率 94%–97%(BraTS / Figshare 数据)
三、脑肿瘤分割(U‑Net,临床级方案)
1、数据格式(分割)
| 文件 | 内容 |
|---|---|
images/ |
MRI 切片 |
masks/ |
肿瘤掩码(0/1/2/3) |
2、构建 U‑Net
imageSize = [256 256 1];
encoderDepth = 3;
numClasses = 4;
unet = unetLayers(imageSize, numClasses, ...
"EncoderDepth", encoderDepth);
3、训练 U‑Net
options = trainingOptions("adam", ...
"InitialLearnRate",1e-3, ...
"MaxEpochs",20, ...
"MiniBatchSize",8, ...
"Shuffle","every-epoch", ...
"Plots","training-progress");
[netSeg, info] = trainNetwork(dsTrain, unet, options);
4、分割结果可视化
[YPred, scores] = semanticseg(testImg, netSeg);
figure
montage({testImg, YPred})
title("MRI 与 肿瘤分割结果")
参考代码 CNN卷积神经网络在脑肿瘤诊断中的应用 www.youwenfan.com/contentcsu/63411.html
四、评价指标
分类
- Accuracy
- Precision / Recall
- F1‑score
- Confusion Matrix
分割
- Dice Coefficient
- IoU
- Sensitivity / Specificity
dice = diceOverlap(YTrue, YPred);
五、工程级建议
1. 2D vs 3D
| 场景 | 建议 |
|---|---|
| 毕业设计 | 2D CNN |
| 论文 / 项目 | 3D CNN(volumetric) |
| 实时系统 | 2D + 滑动窗口 |
2. 多模态 MRI
同时输入:
- T1
- T2
- FLAIR
通道数 = 3(像 RGB)