DATASetDir = fullfile('C:\Users\USER\Downloads\NEW 3D U NET 128X128\vgg');
IMAGEDir = fullfile(DATASetDir,'ImagesTr');
volReader = @(x) matRead(x);
volds = imageDatastore(IMAGEDir, ...
'FileExtensions','.mat','ReadFcn',volReader);
matFileDir = fullfile('C:\Users\USER\Downloads\NEW 3D U NET 128X128\LabelsTr');
classNames = ["background", "tumor"];
pxds = pixelLabelDatastore(matFileDir,classNames,pixelLabelID, ...
'FileExtensions','.mat','ReadFcn',@matRead);
patchSize = [128 128 64];
patchds = randomPatchExtractionDatastore(volds,pxds,patchSize, ...
'PatchesPerImage',patchPerImage);
patchds.MiniBatchSize = miniBatchSize;
dsTrain = transform(patchds,@augment3dPatch);
volLocVal = fullfile('C:\Users\USER\Downloads\NEW 3D U NET 128X128\imagesVal');
voldsVal = imageDatastore(volLocVal, ...
'FileExtensions','.mat','ReadFcn',volReader);
lblLocVal = fullfile('C:\Users\USER\Downloads\NEW 3D U NET 128X128\labelsVal');
pxdsVal = pixelLabelDatastore(lblLocVal,classNames,pixelLabelID, ...
'FileExtensions','.mat','ReadFcn',volReader);
dsVal = randomPatchExtractionDatastore(voldsVal,pxdsVal,patchSize, ...
'PatchesPerImage',patchPerImage);
dsVal.MiniBatchSize = miniBatchSize;
image3dInputLayer([128 128 64],"Name","image3dinput")
convolution3dLayer([3 3 3],32,"Name","conv3d_1","Padding","same")
batchNormalizationLayer("Name","batchnorm_1")
reluLayer("Name","relu_1")
convolution3dLayer([3 3 3],32,"Name","conv3d_2","Padding","same")
batchNormalizationLayer("Name","batchnorm_2")
reluLayer("Name","relu_2")
maxPooling3dLayer([5 5 5],"Name","maxpool3d_1","Padding","same")
convolution3dLayer([3 3 3],32,"Name","conv3d_3","Padding","same")
batchNormalizationLayer("Name","batchnorm_3")
reluLayer("Name","relu_3")
convolution3dLayer([3 3 3],32,"Name","conv3d_4","Padding","same")
batchNormalizationLayer("Name","batchnorm_4")
reluLayer("Name","relu_4")
maxPooling3dLayer([5 5 5],"Name","maxpool3d_2","Padding","same")
convolution3dLayer([3 3 3],32,"Name","conv3d_5","Padding","same")
batchNormalizationLayer("Name","batchnorm_5")
reluLayer("Name","relu_5")
convolution3dLayer([3 3 3],32,"Name","conv3d_6","Padding","same")
batchNormalizationLayer("Name","batchnorm_6")
reluLayer("Name","relu_6")
convolution3dLayer([3 3 3],32,"Name","conv3d_7","Padding","same")
batchNormalizationLayer("Name","batchnorm_7")
reluLayer("Name","relu_7")
convolution3dLayer([3 3 3],32,"Name","conv3d_8","Padding","same")
batchNormalizationLayer("Name","batchnorm_8")
reluLayer("Name","relu_8")
maxPooling3dLayer([5 5 5],"Name","maxpool3d_3","Padding","same")
convolution3dLayer([3 3 3],32,"Name","conv3d_9","Padding","same")
batchNormalizationLayer("Name","batchnorm_9")
reluLayer("Name","relu_9")
convolution3dLayer([3 3 3],32,"Name","conv3d_10","Padding","same")
batchNormalizationLayer("Name","batchnorm_10")
reluLayer("Name","relu_10")
convolution3dLayer([3 3 3],32,"Name","conv3d_11","Padding","same")
batchNormalizationLayer("Name","batchnorm_11")
reluLayer("Name","relu_11")
convolution3dLayer([3 3 3],32,"Name","conv3d_12","Padding","same")
batchNormalizationLayer("Name","batchnorm_12")
reluLayer("Name","relu_12")
maxPooling3dLayer([5 5 5],"Name","maxpool3d_4","Padding","same")
convolution3dLayer([3 3 3],32,"Name","conv3d_13","Padding","same")
batchNormalizationLayer("Name","batchnorm_13")
reluLayer("Name","relu_13")
convolution3dLayer([3 3 3],32,"Name","conv3d_14","Padding","same")
batchNormalizationLayer("Name","batchnorm_14")
reluLayer("Name","relu_14")
convolution3dLayer([3 3 3],32,"Name","conv3d_15","Padding","same")
batchNormalizationLayer("Name","batchnorm_15")
reluLayer("Name","relu_15")
convolution3dLayer([3 3 3],32,"Name","conv3d_16","Padding","same")
batchNormalizationLayer("Name","batchnorm_16")
reluLayer("Name","relu_16")
maxPooling3dLayer([5 5 5],"Name","maxpool3d_5","Padding","same")
fullyConnectedLayer(2,"Name","fc_1")
reluLayer("Name","relu_17")
dropoutLayer(0.5,"Name","dropout_1")
fullyConnectedLayer(2,"Name","fc_2")
reluLayer("Name","relu_18")
dropoutLayer(0.5,"Name","dropout_2")
fullyConnectedLayer(2,"Name","fc_3")
softmaxLayer("Name","softmax")
pixelClassificationLayer("Name","pixel-class")];
plot(layerGraph(layers));
options = trainingOptions('adam', ...
'MaxEpochs',maxEpochs, ...
'InitialLearnRate',1e-3, ...
'LearnRateSchedule','piecewise', ...
'LearnRateDropPeriod',5, ...
'LearnRateDropFactor',0.97, ...
'ValidationData',dsVal, ...
'ValidationFrequency',200, ...
'Plots','training-progress', ...
'MiniBatchSize',miniBatchSize);
modelDateTime = datestr(now,'dd-mmm-yyyy-HH-MM-SS');
[net,info] = trainNetwork(dsTrain,layers,options);
save(['trained3DUNet-' modelDateTime '-Epoch-' num2str(maxEpochs) '.mat'],'net');
load('trained3DVNet-07-Jun-2022-13-45-30-Epoch-250.mat');