MATLAB对神经网络预测结果计算其各类评价指标及PR曲线和ROC曲线等的...

MATLAB对神经⽹络预测结果计算其各类评价指标及PR曲线和ROC曲线等的绘
MATLAB对神经⽹络预测结果计算其各类评价指标及PR曲线和ROC曲线等的绘制、阈值⾃动选取问题的解决
基于深度学习的图像处理预测结果,我们需要通过对预测结果计算各类评价指标以来评价模型好坏,通常的评价指标有precision(精确率)、accuracy(准确率)、recall(召回率)、F1指数,PR曲线、ROC曲线、AUC值,本⽂将给出matlab对预测结果的上⾯这些指标计算的实现,同时运⽤不同的阈值选取⽅法来对其结果进⾏对⽐,选取最适合的阈值选取⽅法。
⾸先是数据读取及各类指标的计算代码:
%unet预测结果的读取
list=dir(['C:\Users\26594\Desktop\桌⾯乱七⼋糟东西\⼤创申请\test\unet_valipredictout(3000)\valipredict3000\','*.png']);
k=length(list);
pre=zeros(400,400,k);
for i=1:1:k
pre(:,:,i)=imread(['C:\Users\26594\Desktop\桌⾯乱七⼋糟东西\⼤创申请\test\unet_valipredictout(3000)\valipredict3000\',list(i).name]);
end
precision=zeros(1,256);
recall=zeros(1,256);
FPR=zeros(1,256);
pre1=zeros(400,400,k);
%lebals
list=dir(['C:\Users\26594\Desktop\桌⾯乱七⼋糟东西\⼤创申请\test\0729test\labels\','*.png']);
k=length(list);
lab=zeros(400,400,k);
for i=1:1:k
lab(:,:,i)=imread(['C:\Users\26594\Desktop\桌⾯乱七⼋糟东西\⼤创申请\test\0729test\labels\',list(i).name]);
end
for j=0:1:255
%mid=zeros(400,400);
for i=1:1:k
pre1(:,:,i)=(pre(:,:,i)>=j);
pre1(:,:,i)=uint8(pre1(:,:,i));
end
% pre(:,:,1)=imread('predict\predict_cdeep\Segmented_0003.png');
% lab(:,:,1)=imread('predict\labels\cell_5_mask.tif');
% pre(:,:,2)=imread('predict\predict_cdeep\Segmented_0001.png');
% lab(:,:,2)=imread('predict\labels\cell_15_mask.tif');
% pre(:,:,3)=imread('predict\predict_cdeep\Segmented_0002.png');
% lab(:,:,3)=imread('predict\labels\cell_26_mask.tif');
% pre(:,:,1)=im2bw(pre(:,:,1),0.196);
% pre(:,:,1)=uint8(pre(:,:,1));
% pre(:,:,2)=im2bw(pre(:,:,2),0.196);
% pre(:,:,2)=uint8(pre(:,:,2));
% pre(:,:,3)=im2bw(pre(:,:,3),0.196);
% pre(:,:,3)=uint8(pre(:,:,3));
lab_1=0;
lab_0=0;
pre_1=0;
pre_0=0;
lab_1pre_1=0;%lab和pre都为1;
lab_1pre_0=0;%lab为1,pre为0;
lab_0pre_1=0;%lab为0,pre为1;
lab_0pre_0=0;%lab为0,pre为0;
[m,n]=size(lab(:,:,1));
q=k;
for k=1:1:q
for i=1:1:m
for f=1:1:n
if lab(i,f,k)==1&&pre1(i,f,k)==1
lab_1=lab_1+1;
pre_1=pre_1+1;
lab_1pre_1=lab_1pre_1+1;
end
if lab(i,f,k)==1&&pre1(i,f,k)==0
lab_1=lab_1+1;
pre_0=pre_0+1;
lab_1pre_0=lab_1pre_0+1;
end
if lab(i,f,k)==0&&pre1(i,f,k)==1
if lab(i,f,k)==0&&pre1(i,f,k)==1
lab_0=lab_0+1;
pre_1=pre_1+1;
lab_0pre_1=lab_0pre_1+1;
end
if lab(i,f,k)==0&&pre1(i,f,k)==0
lab_0=lab_0+1;
pre_0=pre_0+1;
lab_0pre_0=lab_0pre_0+1;
end
end
end
end
% zhen_yang=lab_1pre_1/lab_1*100;
jia_yang=lab_0pre_1/lab_0*100;
% zhen_yin=lab_0pre_0/lab_0*100;
jia_yin=lab_1pre_0/lab_1*100;
%zhunquedu=lab_1pre_1/pre_1*100;
accuracy=(lab_1pre_1+lab_0pre_0)/(lab_0+lab_1)*100;
FPR(j+1)=jia_yang;
precision(j+1)=lab_1pre_1/pre_1*100;
recall(j+1)=lab_1pre_1/lab_1*100;
F1value=2*precision(j+1)*recall(j+1)/(precision(j+1)+recall(j+1));
ji=recall(j+1)/(recall(j+1)+jia_yang+jia_yin)*100;
end
%计算PR曲线⾯积(AUC值)
AUC_PR=-trapz(recall,precision)/10000;%因为这⾥recall和FPR值是从⼤到⼩变化,
AUC_ROC=-trapz(FPR,recall)/10000;%所以积分出来结果是负值
% disp(['recall:',num2str(recall),'%']);
% disp(['precision:',num2str(precision),'%']);
% disp(['accuracy:',num2str(accuracy),'%']);
% %disp(['召回率为:',num2str(recall),'%']);
% disp(['F1值为:',num2str(F1value)]);
% disp(['雅克卡指数为:',num2str(ji),'%']);
% disp(['AUC_PR:',num2str(AUC_PR)]);
% disp(['AUC_ROC:',num2str(AUC_ROC)]);
通过上⾯这段程序后,会得到从0到255阈值下计算出来的precision和recall等值。根据这些值,再运⾏下⾯这段程序即可作出其各类图,并且可以查看具体阈值下各评价指标的⼤⼩:
%此程序需要先通过前⾯程序运⾏后,再运⾏此程序,其中本程序前⾯部分是作图代码,后⾯是
%计算取不同阈值所得到的各类评价指标只⼤⼩。
%作图程序代码
figure(4)
plot(FPR/100,recall/100);
title('ROC曲线');
figure(3)
plot(recall/100,precision/100);
title('PR曲线');
% figure(1)
% plot(precision);
% figure(2)
% plot(recall);
figure(1)
plot(precision/100);
hold on
plot(recall/100);
title('精确率,召回率-阈值曲线');
legend('precision','recall');
figure(2)
plot(diff(precision/100),'color','r');
hold on
plot(-diff(recall/100),'color','g');
title('精确率,召回率导数-阈值曲线');
legend('d(precision)','d(recall)');
figure(5)
plot((1-FPR/100));
hold on
plot(recall/100);
title('召回率,假阳率-阈值曲线');
legend('FPR','recall');
figure(6)
plot(-diff(FPR/100),'color','r');
hold on
plot(-diff(recall/100),'color','g');
title('召回率,假阳率导数-阈值曲线');
legend('d(FPR)','d(recall)');
% %unet
% list=dir(['C:\Users\26594\Desktop\桌⾯乱七⼋糟东西\⼤创申请\test\unet_valipredictout(3000)\valipredict3000\','*.png']);
% k=length(list);
% pre=zeros(400,400,k);
% for i=1:1:k
%    pre(:,:,i)=imread(['C:\Users\26594\Desktop\桌⾯乱七⼋糟东西\⼤创申请\test\unet_valipredictout(3000)\valipredict3000\',list(i).name]); % end
%
% pre1=zeros(400,400,k);
% list=dir(['C:\Users\26594\Desktop\桌⾯乱七⼋糟东西\⼤创申请\test\0729test\labels\','*.png']);
% k=length(list);
% lab=zeros(400,400,k);
% for i=1:1:k
%    lab(:,:,i)=imread(['C:\Users\26594\Desktop\桌⾯乱七⼋糟东西\⼤创申请\test\0729test\labels\',list(i).name]);
% end
% j=150;%通过此项来设定阈值⼤⼩
% for i=1:1:k
%    pre1(:,:,i)=(pre(:,:,i)>=j);
%    pre1(:,:,i)=uint8(pre1(:,:,i));
% end
% % pre(:,:,1)=imread('predict\predict_cdeep\Segmented_0003.png');
% % lab(:,:,1)=imread('predict\labels\cell_5_mask.tif');
% % pre(:,:,2)=imread('predict\predict_cdeep\Segmented_0001.png');
% % lab(:,:,2)=imread('predict\labels\cell_15_mask.tif');
% % pre(:,:,3)=imread('predict\predict_cdeep\Segmented_0002.png');
% % lab(:,:,3)=imread('predict\labels\cell_26_mask.tif');
% % pre(:,:,1)=im2bw(pre(:,:,1),0.196);
% % pre(:,:,1)=uint8(pre(:,:,1));
% % pre(:,:,2)=im2bw(pre(:,:,2),0.196);
% % pre(:,:,2)=uint8(pre(:,:,2));
% % pre(:,:,3)=im2bw(pre(:,:,3),0.196);
% % pre(:,:,3)=uint8(pre(:,:,3));
% lab_1=0;
% lab_0=0;
神经网络预测% pre_1=0;
% pre_0=0;
% lab_1pre_1=0;%lab和pre都为1;
% lab_1pre_0=0;%lab为1,pre为0;
% lab_0pre_1=0;%lab为0,pre为1;
% lab_0pre_0=0;%lab为0,pre为0;
% [m,n]=size(lab(:,:,1));
% q=k;
% for k=1:1:q
% for i=1:1:m
%    for f=1:1:n
%        if lab(i,f,k)==1&&pre1(i,f,k)==1
%            lab_1=lab_1+1;
%            pre_1=pre_1+1;
%            lab_1pre_1=lab_1pre_1+1;
%        end
%        if lab(i,f,k)==1&&pre1(i,f,k)==0
%            lab_1=lab_1+1;
%            pre_0=pre_0+1;
%            lab_1pre_0=lab_1pre_0+1;
%            end
%        if lab(i,f,k)==0&&pre1(i,f,k)==1
%            lab_0=lab_0+1;
%            pre_1=pre_1+1;
%            lab_0pre_1=lab_0pre_1+1;
%        end
%        end
%        if lab(i,f,k)==0&&pre1(i,f,k)==0
%            lab_0=lab_0+1;
%            pre_0=pre_0+1;
%            lab_0pre_0=lab_0pre_0+1;
%        end
%    end
% end
% end
% jia_yang=lab_0pre_1/lab_0*100;
% jia_yin=lab_1pre_0/lab_1*100;
% accuracy=(lab_1pre_1+lab_0pre_0)/(lab_0+lab_1)*100; % precision=lab_1pre_1/pre_1*100;
% recall=lab_1pre_1/lab_1*100;
% F1value=2*precision*recall/(precision+recall);
% ji=recall/(recall+jia_yang+jia_yin)*100;
%
%
% disp(['recall:',num2str(recall),'%']);
% disp(['precision:',num2str(precision),'%']);
% disp(['accuracy:',num2str(accuracy),'%']);
% disp(['F1值为:',num2str(F1value)]);
% disp(['雅克卡指数为:',num2str(ji),'%']);
% disp(['AUC_PR:',num2str(AUC_PR)]);
% disp(['AUC_ROC:',num2str(AUC_ROC)]);
所以可以得到PR、ROC等曲线如下
的点,也就是ROC曲线与y=1-x交点阈值⼤⼩。计算出其不同阈值下评价指标⼤⼩如下:
⾄于到底选哪种⽅法得到合适阈值,就需要根据具体情况了。

本文发布于:2024-09-21 08:31:20,感谢您对本站的认可!

本文链接:https://www.17tex.com/tex/4/361518.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:阈值   结果   评价   指标
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议