Semantic Segmentation Response size and output size mismatch
Show older comments
Hello everyone,
I am trying to do a segmentation of a mushroom dataset. But, after I ran my code, I got an error.
Invalid training data. The output size ([32 32 2]) of the last layer does not match the response size ([168 300 2]).
Can someone help me?
%% Cereate a Semantic Segmentation Network
inputSize = [32 32 3];%Here, an image size of [32 32 3] is used for the network to process 64x64 RGB images.
imgLayer = imageInputLayer(inputSize);
%Create Downsampling Network
filterSize = 3;
numFilters = 32;
conv = convolution2dLayer(filterSize,numFilters,'Padding',1);
relu = reluLayer();
poolSize = 2;
maxPoolDownsample2x = maxPooling2dLayer(poolSize,'Stride',2);
downsamplingLayers = [
conv
relu
maxPoolDownsample2x
conv
relu
maxPoolDownsample2x
];
%Create Upsampling Network
filterSize = 4;
transposedConvUpsample2x = transposedConv2dLayer(4,numFilters,'Stride',2,'Cropping',1);
upsamplingLayers = [
transposedConvUpsample2x
relu
transposedConvUpsample2x
relu
];
%Create A Pixel Classification Layer
numClasses = 3;
conv1x1 = convolution2dLayer(1,numClasses);
finalLayers = [
conv1x1
softmaxLayer()
pixelClassificationLayer()
];
%Stack All Layers
net = [
imgLayer
downsamplingLayers
upsamplingLayers
finalLayers
];
%Train A Semantic Segmentation Network
dataDir = 'C:\Users\cleme\OneDrive\Bureau\tp_matlab\mushroom';
dataDir1 = 'C:\Users\cleme\OneDrive\Bureau\tp_matlab\mushroom\images';
imDir = fullfile(dataDir1,'*.jpg');
pxDir = fullfile(dataDir,'PixelLabelData');
classNames = ["cap" "stalk"];
pixelLabelID = [1 2];
pxds = pixelLabelDatastore(pxDir,classNames,pixelLabelID);
imds = imageDatastore(imDir);
% resize image
imds.ReadFcn = @customreader;
reset(imds);
I = read(imds);
C = read(pxds);
%Visualize training images and ground truth pixel labels.
I = imresize(I,5);
L = imresize(uint8(C{1}),5);
imshowpair(I,L,'montage')
%Create a semantic segmentation network. This network uses a simple semantic segmentation network based on a downsampling and upsampling design.
numFilters = 64;
filterSize = 3;
numClasses = 2;
layers = [
imageInputLayer([32 32 1])
convolution2dLayer(filterSize,numFilters,'Padding',1)
reluLayer()
maxPooling2dLayer(2,'Stride',2)
convolution2dLayer(filterSize,numFilters,'Padding',1)
reluLayer()
transposedConv2dLayer(4,numFilters,'Stride',2,'Cropping',1);
convolution2dLayer(1,numClasses);
softmaxLayer()
pixelClassificationLayer()
];
opts = trainingOptions('sgdm', ...
'InitialLearnRate',1e-3, ...
'MaxEpochs',100, ...
'MiniBatchSize',64);
%Create a pixel label image datastore that contains training data.
trainingData = pixelLabelImageDatastore(imds,pxds);
net = trainNetwork(trainingData,layers,opts);
%% Function
function data = customreader(filename)
onState = warning('off', 'backtrace');
c = onCleanup(@() warning(onState));
data = imread(filename);
data = data(:,:,min(1:3, end));
data = imresize(data, [32 32]);
data = rgb2gray(data);
end
1 Comment
Clémence Labarre
on 18 Jun 2020
Answers (0)
Categories
Find more on Deep Learning Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!