Error trainNetwork for U net Deep learning.

2 views (last 30 days)
mohd akmal masud
mohd akmal masud on 31 May 2022
Dear All,
I tried to train U-Net Network Deep Learning. data for IMAGEDir(256x256x24single.mat) and matFileDir(256x256x24singlecategorical.mat) as attached.
below is my coding
clc
clear all
close all
%testDataimages
DATASetDir = fullfile('C:\Users\Akmal\Desktop\Unet3D');
IMAGEDir = fullfile(DATASetDir,'256x256x24singlemat');
volReader = @(x) matRead(x);
volds = imageDatastore(IMAGEDir, ...
'FileExtensions','.mat','ReadFcn',volReader);
% labelReader = @(x) matread(x);
matFileDir = fullfile('C:\Users\Akmal\Desktop\Unet3D');
classNames = ["background", "foreground"];
pixelLabelID = [1 2];
% pxds = (LabelDirr,classNames,pixelLabelID, ...
% 'FileExtensions','.mat','ReadFcn',labelReader);
pxds = pixelLabelDatastore(matFileDir,classNames,pixelLabelID, ...
'FileExtensions','.mat','ReadFcn',@matRead);
volume = preview(volds);
label = preview(pxds);
ds = pixelLabelImageDatastore(volds,pxds);
tbl = countEachLabel(pxds)
totalNumberOfPixels = sum(tbl.PixelCount);
frequency = tbl.PixelCount / totalNumberOfPixels;
inverseFrequency = 1./frequency
layerf=pixelClassificationLayer("Name","Segmentation-Layer")
lgraph = layerGraph();
tempLayers = [
image3dInputLayer([128 128 128 3],"Name","ImageInputLayer")
convolution3dLayer([3 3 3],16,"Name","Encoder-Stage-1-Conv-1","Padding","same","WeightsInitializer","he")
batchNormalizationLayer("Name","Encoder-Stage-1-BN-1")
reluLayer("Name","Encoder-Stage-1-ReLU-1")
convolution3dLayer([3 3 3],32,"Name","Encoder-Stage-1-Conv-2","Padding","same","WeightsInitializer","he")
batchNormalizationLayer("Name","Encoder-Stage-1-BN-2")
reluLayer("Name","Encoder-Stage-1-ReLU-2")];
lgraph = addLayers(lgraph,tempLayers);
tempLayers = [
maxPooling3dLayer([2 2 2],"Name","Encoder-Stage-1-MaxPool","Stride",[2 2 2])
convolution3dLayer([3 3 3],32,"Name","Encoder-Stage-2-Conv-1","Padding","same","WeightsInitializer","he")
batchNormalizationLayer("Name","Encoder-Stage-2-BN-1")
reluLayer("Name","Encoder-Stage-2-ReLU-1")
convolution3dLayer([3 3 3],64,"Name","Encoder-Stage-2-Conv-2","Padding","same","WeightsInitializer","he")
batchNormalizationLayer("Name","Encoder-Stage-2-BN-2")
reluLayer("Name","Encoder-Stage-2-ReLU-2")];
lgraph = addLayers(lgraph,tempLayers);
tempLayers = [
maxPooling3dLayer([2 2 2],"Name","Encoder-Stage-2-MaxPool","Stride",[2 2 2])
convolution3dLayer([3 3 3],64,"Name","Bridge-Conv-1","Padding","same","WeightsInitializer","he")
batchNormalizationLayer("Name","Bridge-BN-1")
reluLayer("Name","Bridge-ReLU-1")
convolution3dLayer([3 3 3],128,"Name","Bridge-Conv-2","Padding","same","WeightsInitializer","he")
batchNormalizationLayer("Name","Bridge-BN-2")
reluLayer("Name","Bridge-ReLU-2")
transposedConv3dLayer([2 2 2],128,"Name","Decoder-Stage-1-UpConv","BiasLearnRateFactor",2,"Stride",[2 2 2],"WeightsInitializer","he")];
lgraph = addLayers(lgraph,tempLayers);
tempLayers = [
concatenationLayer(4,2,"Name","Decoder-Stage-1-Concatenation")
convolution3dLayer([3 3 3],64,"Name","Decoder-Stage-1-Conv-1","Padding","same","WeightsInitializer","he")
batchNormalizationLayer("Name","Decoder-Stage-1-BN-1")
reluLayer("Name","Decoder-Stage-1-ReLU-1")
convolution3dLayer([3 3 3],64,"Name","Decoder-Stage-1-Conv-2","Padding","same","WeightsInitializer","he")
batchNormalizationLayer("Name","Decoder-Stage-1-BN-2")
reluLayer("Name","Decoder-Stage-1-ReLU-2")
transposedConv3dLayer([2 2 2],64,"Name","Decoder-Stage-2-UpConv","BiasLearnRateFactor",2,"Stride",[2 2 2],"WeightsInitializer","he")];
lgraph = addLayers(lgraph,tempLayers);
tempLayers = [
concatenationLayer(4,2,"Name","Decoder-Stage-2-Concatenation")
convolution3dLayer([3 3 3],32,"Name","Decoder-Stage-2-Conv-1","Padding","same","WeightsInitializer","he")
batchNormalizationLayer("Name","Decoder-Stage-2-BN-1")
reluLayer("Name","Decoder-Stage-2-ReLU-1")
convolution3dLayer([3 3 3],32,"Name","Decoder-Stage-2-Conv-2","Padding","same","WeightsInitializer","he")
batchNormalizationLayer("Name","Decoder-Stage-2-BN-2")
reluLayer("Name","Decoder-Stage-2-ReLU-2")
convolution3dLayer([1 1 1],5,"Name","Final-ConvolutionLayer","Padding","same","WeightsInitializer","he")
softmaxLayer("Name","Softmax-Layer")
pixelClassificationLayer("Name","Segmentation-Layer")];
lgraph = addLayers(lgraph,tempLayers);
% clean up helper variable
clear tempLayers;
lgraph = connectLayers(lgraph,"Encoder-Stage-1-ReLU-2","Encoder-Stage-1-MaxPool");
lgraph = connectLayers(lgraph,"Encoder-Stage-1-ReLU-2","Decoder-Stage-2-Concatenation/in2");
lgraph = connectLayers(lgraph,"Encoder-Stage-2-ReLU-2","Encoder-Stage-2-MaxPool");
lgraph = connectLayers(lgraph,"Encoder-Stage-2-ReLU-2","Decoder-Stage-1-Concatenation/in2");
lgraph = connectLayers(lgraph,"Decoder-Stage-1-UpConv","Decoder-Stage-1-Concatenation/in1");
lgraph = connectLayers(lgraph,"Decoder-Stage-2-UpConv","Decoder-Stage-2-Concatenation/in1");
plot(lgraph);
imageSize = [64 64 64];
numClasses = 2;
encoderDepth = 3;
lgraph = unetLayers(imageSize,numClasses,'EncoderDepth',encoderDepth)
options1 = trainingOptions('adam', ...
'InitialLearnRate',1e-3, ...
'MaxEpochs',20, ...
'LearnRateDropFactor',5e-1, ...
'LearnRateDropPeriod',20, ...
'LearnRateSchedule','piecewise', ...
'MiniBatchSize',4,'Plots','training-progress');
net1 = trainNetwork(ds,lgraph,options1);
But I got Error like below. anyone can help me?
Error using trainNetwork (line 184)
Invalid training or validation response data. Categorical responses must either
be a vector or a single-channel 2-D or 3-D image.

Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by