Clear Filters
Clear Filters

VGG19 GOT ERROR

7 views (last 30 days)
mohd akmal masud
mohd akmal masud on 24 Jul 2022
Answered: Himanshu on 27 Sep 2023
Dear all,
Need your help.
I want to run VGG19. But I got an error. (all data in ezyzip folder as attached, and matlab.m is workspace)
Below is my coding
clc
clear all
close all
%testDataimages
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);
% labelReader = @(x) matread(x);
matFileDir = fullfile('C:\Users\USER\Downloads\NEW 3D U NET 128X128\LabelsTr');
classNames = ["background", "tumor"];
pixelLabelID = [0 1];
% pxds = (LabelDirr,classNames,pixelLabelID, ...
% 'FileExtensions','.mat','ReadFcn',labelReader);
pxds = pixelLabelDatastore(matFileDir,classNames,pixelLabelID, ...
'FileExtensions','.mat','ReadFcn',@matRead);
volume = preview(volds);
label = preview(pxds);
patchSize = [128 128 64];
patchPerImage = 16;
miniBatchSize = 2;
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;
layers = [
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));
maxEpochs = 150;
options = trainingOptions('adam', ...
'MaxEpochs',maxEpochs, ...
'InitialLearnRate',1e-3, ...
'LearnRateSchedule','piecewise', ...
'LearnRateDropPeriod',5, ...
'LearnRateDropFactor',0.97, ...
'ValidationData',dsVal, ...
'ValidationFrequency',200, ...
'Plots','training-progress', ...
'Verbose',false, ...
'MiniBatchSize',miniBatchSize);
doTraining = true;
if doTraining
modelDateTime = datestr(now,'dd-mmm-yyyy-HH-MM-SS');
[net,info] = trainNetwork(dsTrain,layers,options);
save(['trained3DUNet-' modelDateTime '-Epoch-' num2str(maxEpochs) '.mat'],'net');
else
load('trained3DVNet-07-Jun-2022-13-45-30-Epoch-250.mat');
end
Error using trainNetwork
Invalid training data. The output size ([1 1 1 2]) of the last layer does not match the response size ([128 128 64 2]).

Answers (1)

Himanshu
Himanshu on 27 Sep 2023
Hello,
I understand that you are facing an error indicating that the output size of your last layer in the network does not match the size of your response data. Your network is outputting a 4D tensor of size [1 1 1 2], but your response data is a 4D tensor of size [128 128 64 2].
The error occurred because you are using a fully connected layer 'fullyConnectedLayer(2,"Name","fc_3")' followed by a softmax layer and a pixel classification layer. The fully connected layer will collapse the spatial dimensions of your input, resulting in an output tensor that doesn't have the expected spatial dimensions.
You can use a convolutional layer with the same number of filters as your number of classes, which would maintain the spatial dimensions of your data.
convolution3dLayer([1 1 1],2,"Name","conv3d_19")
You can refer to the below documentation to understand more about designing network architectures in MATLAB.

Community Treasure Hunt

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

Start Hunting!