How to find the sensitivity and specificity from the confusion matrix?

6 次查看(过去 30 天)
  2 个评论
Win Sheng Liew
Win Sheng Liew 2018-9-28
global net imageSize featureLayer classifier
f = waitbar(0,'Please wait...');
rootFolder = fullfile('c:\', 'Users', 'Ws Liew', 'Documents', 'MATLAB', 'Dataset'); categories = {'normal', 'abnormal'}; imds = imageDatastore(fullfile(rootFolder, categories), 'LabelSource','foldernames');
tb1 = countEachLabel(imds)
minSetCount = 0.6;
imds = splitEachLabel(imds, minSetCount, 'randomize');
normal = find(imds.Labels == 'normal', 1); abnormal = find(imds.Labels == 'abnormal', 1);
str = {'Select a CNN Architectures: '}; list = {'GoogLeNet','AlexNet','ResNet','VGG-16','VGG-19'}; [indx,tf] = listdlg('PromptString', str, 'ListSize', [200 130], 'SelectionMode', 'single', 'ListString', list);
waitbar(.25,f,'Loading Pre-trained Network');
if indx == 1 net = googlenet(); featureLayer = 'loss3-classifier';
elseif indx == 2 net = alexnet(); featureLayer = 'fc8';
elseif indx == 3 net = resnet50(); featureLayer = 'fc1000';
elseif indx == 4 net = vgg16(); featureLayer = 'fc8';
else indx == 5 net = vgg19(); featureLayer = 'fc8';
end
net.Layers
net.Layers(1)
net.Layers(end)
numel(net.Layers(end).ClassNames)
waitbar(.4,f,'Training and Testing Image Datasets');
[trainingSet, testSet] = splitEachLabel(imds, 0.3, 'randomize');
imageSize = net.Layers(1).InputSize; augmentedTrainingSet = augmentedImageDatastore(imageSize, trainingSet, 'ColorPreprocessing', 'gray2rgb'); augmentedTestSet = augmentedImageDatastore(imageSize, testSet, 'ColorPreprocessing', 'gray2rgb');
w1 = net.Layers(2).Weights;
w1 = mat2gray(w1); w1 = imresize(w1,5);
trainingFeatures = activations(net, augmentedTrainingSet, featureLayer, 'MiniBatchSize', 32, 'OutputAs', 'columns');
trainingLabels = trainingSet.Labels;
waitbar(.65,f,'SVM Classifier Training');
classifier = fitcecoc(trainingFeatures, trainingLabels, 'Learners', 'Linear', 'Coding', 'onevsall', 'ObservationsIn', 'columns');
testFeatures = activations(net, augmentedTestSet, featureLayer, 'MiniBatchSize', 32, 'OutputAs', 'columns');
predictedLabels = predict(classifier, testFeatures, 'ObservationsIn', 'columns');
testLabels = testSet.Labels;
confMat = confusionmat(testLabels, predictedLabels);
confMat = bsxfun(@rdivide,confMat,sum(confMat,2))

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Dimensionality Reduction and Feature Extraction 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by