Calculate TP,TN,FN,FP F1 score for above code

4 views (last 30 days)
Hello Everyone, the below is code snipped, however my f1 scores exceed 1, please help me
clc;
warning off
imds=imageDatastore('Train',...
'IncludeSubFolders',true,'LabelSource','foldername');
[imdsTrain,imdsValidation]=splitEachLabel(imds,0.8,'randomized');
g=alexnet;
labels='foldername';
layers=g.Layers;
inputSize = g.Layers(1).InputSize;
layers(23)=fullyConnectedLayer(4);
layers(25)=classificationLayer;
layer='fc8';
allImages=imageDatastore('Train','IncludeSubfolders',true, 'LabelSource','foldernames');
I=read(allImages);
augimdsTrain = augmentedImageDatastore(inputSize(1:2),imdsTrain);
augimdsTest = augmentedImageDatastore(inputSize(1:2),imdsValidation);
opts=trainingOptions('sgdm','InitialLearnRate',0.001,'MaxEpochs',12,'MiniBatchSize',64,'Plots','training-progress','ValidationData',augimdsTest);
myNet1=trainNetwork(allImages,layers,opts);
featuresTrain=activations(g,imdsTrain,layer,'OutputAs','rows');
featuresTest=activations(g,imdsValidation,layer,'OutputAs','rows');
svmtrain=fitcecoc(featuresTrain,imdsTrain.Labels);
YPred=predict(svmtrain,featuresTest);
plotconfusion(imdsValidation.Labels,YPred);
[cm,order] = confusionmat(imdsValidation.Labels,YPred);
YAct=imdsValidation.Labels;
cmt=cm';
diagonal=diag(cmt);
sum_of_rows=sum(cmt,1);
precision=diagonal./sum_of_columns;
overall_precision=mean(precision);
sum_of_columns=sum(cmt,2);
recall=diagonal./sum_of_rows;
overall_recall=mean(recall);
f1_score=2*((overall_precision*overall_recall)/(overall_precision+overall_recall));
save myNet1;
  1 Comment
Victor Silva
Victor Silva on 4 Jan 2023
sum_of_rows=sum(cmt,1);
precision=diagonal./sum_of_columns; is this code ok?

Sign in to comment.

Answers (0)

Categories

Find more on Statistics and Machine Learning Toolbox in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!