directoryPath = 'training_images/resized';
if exist(directoryPath, 'dir')
[status, message, messageId] = rmdir(directoryPath, 's');
disp(['Directory "', directoryPath, '" has been deleted successfully.']);
disp(['Failed to delete "', directoryPath, '": ', message]);
disp(['Directory "', directoryPath, '" does not exist.']);
imds = imageDatastore( ...
classNames = ["Lesion","Background"];
pxds=pixelLabelDatastore( ...
pximds=pixelLabelImageDatastore(imds,pxds);
total_num_images=length(pximds.Images);
perm=randperm(total_num_images,4);
image = readimage(imds, perm(idx));
mask = readimage(pxds, perm(idx));
binaryMask = mask(:,:,1) == classNames(1);
visboundaries(binaryMask, 'Color', 'r');
[~, filename] = fileparts(imds.Files{perm(idx)});
title(sprintf('%s', filename), 'Interpreter', "none");
resizedImagesDir = 'training_images/images-segmantation/resized/images';
resizedLabelsDir = 'training_images/images-segmantation/resized/labels';
if ~exist(resizedImagesDir, 'dir'), mkdir(resizedImagesDir); end
if ~exist(resizedLabelsDir, 'dir'), mkdir(resizedLabelsDir); end
for i = 1:length(imds.Files)
img = imread(imds.Files{i});
resizedImg = imresize(img, 'OutputSize', imageSize(1:2));
[~, fileName, ext] = fileparts(imds.Files{i});
imwrite(resizedImg, fullfile(resizedImagesDir, [fileName, ext]));
for i = 1:length(pxds.Files)
label = imread(pxds.Files{i});
resizedLabel = imresize(label, 'OutputSize', imageSize(1:2), 'Method', 'nearest');
[~, fileName, ext] = fileparts(pxds.Files{i});
imwrite(resizedLabel, fullfile(resizedLabelsDir, [fileName, ext]));
imdsResized = imageDatastore(resizedImagesDir);
pximdsResz = pixelLabelDatastore(resizedLabelsDir, classNames, labelIDs);
numImages = numel(imdsResized.Files);
testIdx = randperm(numImages, 5);
trainValidIdx = setdiff(1:numImages, testIdx);
validIdx = trainValidIdx(randperm(length(trainValidIdx), 10));
trainIdx = trainValidIdx;
imdsTrain = subset(imdsResized, trainIdx);
imdsValid = subset(imdsResized, validIdx);
imdsTest = subset(imdsResized, testIdx);
pximdsTrain = subset(pximdsResz, trainIdx);
pximdsValid = subset(pximdsResz, validIdx);
pximdsTest = subset(pximdsResz, testIdx);
validDS = combine(imdsValid, pximdsValid);
validationData = {validDS};
trainDSConverted = transform(pximdsTrain, @(c) categoricalToNumeric(c{1}, classNames));
validDSConverted = transform(pximdsValid, @(c) categoricalToNumeric(c{1}, classNames));
imdsTrain = subset(imdsResized, trainIdx);
imdsValid = subset(imdsResized, validIdx);
imdsTest = subset(imdsResized, testIdx);
pxdsTrain = subset(pximdsResz, trainIdx);
pxdsValid = subset(pximdsResz, validIdx);
pxdsTest = subset(pximdsResz, testIdx);
pximdsTrain = pixelLabelImageDatastore(imdsTrain, pxdsTrain);
pximdsValid = pixelLabelImageDatastore(imdsValid, pxdsValid);
numClasses = numel(classNames);
lgraph = deeplabv3plusLayers(imageSize, numClasses, 'resnet50');
options = trainingOptions('sgdm', ...
'InitialLearnRate', 0.03, ...
'L2Regularization', 0.0005, ...
'VerboseFrequency', 20, ...
'LearnRateSchedule', 'piecewise', ...
'ExecutionEnvironment', 'cpu', ...
'Shuffle', 'every-epoch', ...
'ValidationData', validDSConverted, ...
'ValidationFrequency', 50, ...
'ValidationPatience', 4, ...
'Plots', 'training-progress', ...
'GradientThresholdMethod', 'l2norm', ...
'GradientThreshold', 0.05);
net = trainNetwork(trainDSConverted, lgraph, options);
[pxdspredicted]=semanticseg(pximdsTest,net,'WriteLocation',tempdir);
metrics=evaluateSemanticSegmentation(pxdspredicted,pximdsTest);
normConfMatData = metrics.NormalizedConfusionMatrix.Variables;
h=heatmap(classNames,classNames,100*normConfMatData);
h.XLabel='Predicted Class';
h.Title='Normalized Confusion Matrix (%)';
num_test_images=length(pximdsTest.Images);
perm=randperm(num_test_images,2);
[~,filename]=fileparts(pximdsTest.Images{idx});
I = imread(pximdsTest.Images{perm(idx)});
I = imresize(I,[imageSize(1) imageSize(2)],'bilinear');
actual_mask=imread(pximdsTest.PixelLabelData{perm(idx)});
actual_mask=imresize(actual_mask,[imageSize(1) imageSize(2)],'bilinear');
visboundaries(actual_mask,'Color','r');
predicted_image=(uint8(readimage(pxdspredicted,perm(idx))));
predicted_results=uint8(~(predicted_image-1));
visboundaries(predicted_results,'Color','g');
title(sprintf('%s Red- Actual, Green - Predicted',filename),'Interpreter',"none");
imwrite(mat2gray(predicted_results),sprintf('%s.png',filename));