[06669b]: / SemanticSegmentation.m

Download this file

157 lines (119 with data), 4.0 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
%% This code train and test semantic segmentation of Capsule endoscopy images
% required MATLAB 2018
% Developer: Tonmoy Ghosh (tghosh@crimson.ua.edu)
% Load Images
% Use |imageDatastore| to load images. The |imageDatastore| enables you
% to efficiently load a large collection of images on disk.
%%
clear; clc; close all;
%imgDir = fullfile('bleeding images')
imgDir = '/Users/tonmoyghosh/OneDrive - The University of Alabama/Paper with Code/Semantic Segmentation Based Bleeding Zone Detection/Dataset/bleeding';
imds = imageDatastore(imgDir);
%%
% Display one of the images.
I = readimage(imds, 1);
I = histeq(I);
figure
imshow(I)
%% Load Pixel-Labeled Images
classes = [
"Bleeding"
"Non_Bleeding"
"Background"
];
labelIDs = PixelLabelIDs();
%%
% Use the classes and label IDs to create the |pixelLabelDatastore|:
%labelDir = fullfile('labels');
labelDir = '/Users/tonmoyghosh/OneDrive - The University of Alabama/Paper with Code/Semantic Segmentation Based Bleeding Zone Detection/Dataset/labels';
pxds = pixelLabelDatastore(labelDir,classes,labelIDs);
% Read and display one of the pixel-labeled images by overlaying it on top
% of an image.
C = readimage(pxds, 1);
cmap = CEColorMap;
B = labeloverlay(I,C,'ColorMap',cmap);
figure
imshow(B)
pixelLabelColorbar(cmap,classes);
%%
%analize the data statistics
tbl = countEachLabel(pxds)
%Visualize the pixel counts by class.
frequency = tbl.PixelCount/sum(tbl.PixelCount);
figure
bar(1:numel(classes),frequency)
xticks(1:numel(classes))
xticklabels(tbl.Name)
xtickangle(45)
ylabel('Frequency')
%%
%Resize CamVid Data
imageFolder = fullfile('imagesReszed',filesep);
imds = resizeCEImages(imds,imageFolder);
labelFolder = fullfile('labelsResized',filesep);
pxds = resizeCEPixelLabels(pxds,labelFolder);
%%
%Prepare Training and Test Sets
[imdsTrain, imdsTest, pxdsTrain, pxdsTest] = partitionCEData(imds,pxds);
numTrainingImages = numel(imdsTrain.Files)
numTestingImages = numel(imdsTest.Files)
%Create the network
imageSize = [256 256 3];
numClasses = numel(classes);
%lgraph = segnetLayers(imageSize,numClasses,'vgg16');
%%
%Balance Classes Using Class Weighting
% imageFreq = tbl.PixelCount ./ tbl.ImagePixelCount;
% classWeights = median(imageFreq) ./ imageFreq
%
% pxLayer = pixelClassificationLayer('Name','labels','ClassNames', tbl.Name, 'ClassWeights', classWeights)
%
%
% lgraph = removeLayers(lgraph, 'pixelLabels');
% lgraph = addLayers(lgraph, pxLayer);
% lgraph = connectLayers(lgraph, 'softmax' ,'labels');
% load saved network architecture
load lgraph
%Select Training Options
options = trainingOptions('sgdm', ...
'Momentum', 0.9, ...
'InitialLearnRate', 1e-3, ...
'L2Regularization', 0.0005, ...
'MaxEpochs', 100, ...
'MiniBatchSize', 3, ...
'Shuffle', 'every-epoch', ...
'VerboseFrequency', 2);
%%
%Data Augmentation
augmenter = imageDataAugmenter('RandXReflection',true,...
'RandXTranslation', [-10 10], 'RandYTranslation',[-10 10]);
%Start Training
datasource = pixelLabelImageSource(imdsTrain,pxdsTrain, ...
'DataAugmentation',augmenter);
doTraining = false;
if doTraining
[net, info] = trainNetwork(datasource,lgraph,options);
else
data = load('CEtrainedSegNet.mat');
net = data.net;
end
%%
%Test Network on One Image
tic
I = read(imdsTest);
C = semanticseg(I, net);
%Display the results.
B = labeloverlay(I, C, 'Colormap', cmap, 'Transparency',0.4);
figure
imshow(B)
pixelLabelColorbar(cmap, classes);
expectedResult = read(pxdsTest);
actual = uint8(C);
expected = uint8(expectedResult{1});
imshowpair(actual, expected)
iou = jaccard(C, expectedResult{1});
table(classes,iou)
%Evaluate Trained Network
pxdsResults = semanticseg(imdsTest,net,'WriteLocation',tempdir,'Verbose',false);
metrics = evaluateSemanticSegmentation(pxdsResults,pxdsTest,'Verbose',false);
toc