Matlab+UNet+LIDC 32行代码训练自己的肺结节数据集

版本:Matlab2021b,需要安装deeplearningToolBox

数据集是LIDC-IDRI,我已经事先切割成64*64大小的PNG了,数据集下载链接:lidc数据集增强,便于使用-互联网文档类资源-CSDN文库

话不多说直接上matlab代码

%% 数据集导入
imageDir='G:\MyLIDC\output';
trainImgLoc=fullfile(imageDir,'train','Image');
labelImgLoc = fullfile(imageDir,'train','Mask');
imds = datastore(trainImgLoc, "IncludeSubfolders",true,"Type","image",'ReadFcn',@myReader);
classNames = ["background","nodule"];
pixelLabelID = [0 255];
pxds = pixelLabelDatastore(labelImgLoc,classNames,pixelLabelID, ...
    "IncludeSubfolders",true,'ReadFcn',@myReader);
dsTrain = combine(imds,pxds);
%% 网络参数配置
lgraph=myUnet();
initialLearningRate = 5e-4;
maxEpochs = 1;
minibatchSize = 20;
l2reg = 0.0001;

options = trainingOptions('adam',...
    'InitialLearnRate',initialLearningRate, ...
    'L2Regularization',l2reg,...
    'MaxEpochs',maxEpochs,...
    'MiniBatchSize',minibatchSize,...
    'LearnRateSchedule','piecewise',...
    'Shuffle','every-epoch',...
    'GradientThresholdMethod','l2norm',...
    'GradientThreshold',0.05, ...
    'Plots','training-progress', ...
    'VerboseFrequency',20);

[unet,info] = trainNetwork(dsTrain,lgraph_1,options);
modelDateTime = string(datetime('now','Format',"yyyy-MM-dd-HH-mm-ss"));
save(strcat("Lidc-Unet-",modelDateTime,"-Epoch-",num2str(maxEpochs),".mat"),'unet');

训练一个epoch已经效果还ok了Matlab+UNet+LIDC 32行代码训练自己的肺结节数据集_第1张图片

 再看一下模型预测图片的代码

%% 查看模型预测结果
testimg=imread('G:\MyLIDC\output\test\Image\LIDC-IDRI-0875\0875_NI001_slice004.png');
testmask=imread('G:\MyLIDC\output\test\Mask\LIDC-IDRI-0875\0875_MA001_slice004.png');
testimg=rgb2gray(testimg);
testmask=rgb2gray(testmask);
categoricalMask=categorical(testmask,pixelLabelID,classNames);
categoricalImage=semanticseg(testimg, unet);
predict=uint8(categoricalImage=='nodule')*255;
img_predicmask = labeloverlay(testimg,categoricalImage,'Transparency',0.8);
img_gtmask = labeloverlay(testimg,categoricalMask,'Transparency',0.8);
figure
subplot(2,3,1)
imshow(testimg)
title("原始测试图像")
subplot(2,3,2)
imshow(predict)
title("测试图像预测输出")
subplot(2,3,3)
imshow(img_predicmask)
title("测试图像预测覆盖")
subplot(2,3,5)
imshow(testmask)
title("测试图像GroundTruth")
subplot(2,3,6)
imshow(img_gtmask)
title("测试图像GroundTruth覆盖")

效果图如下

Matlab+UNet+LIDC 32行代码训练自己的肺结节数据集_第2张图片

 

你可能感兴趣的:(自我学习归纳,matlab,深度学习)