对Alexnet模型进行迁移学习查看:MATLAB对Alexnet模型进行迁移学习
这里我贴出的代码是对训练好的Alexnet模型进行测试,绘制混淆矩阵,保存分类后的图片。
我训练的模型是对尿沉渣显微图像进行分类,主要是对BYST,GRAN,HYAL,MUCS,RBC,WBC,WBCC这几种细胞进行分类。
clc;close all;clear;
tic;
%% 加载模型
load('-mat','E:\MATLAB_Code\AlexnetTrain');
%% 加载测试集
Location = 'E:\image_test\test_02';
imds = imageDatastore(Location,'includeSubfolders',true,'LabelSource','foldernames');
inputSize = AlexnetTransfer.Layers(1).InputSize;
imdstest = augmentedImageDatastore(inputSize(1:2),imds);
% tic;
[YPred,scores] = classify(AlexnetTransfer,imdstest);
%使用训练好的模型对测试集进行分类
% disp(['分类所用时间为:',num2str(toc),'秒']);
%% 显示分类结果,绘制混淆矩阵
byst = 'BYST';
BYST = numel(YPred,YPred == byst);
disp(['BYST = ',num2str(BYST)]);
gran = 'GRAN';
GRAN = numel(YPred,YPred == gran);
disp(['GRAN = ',num2str(GRAN)]);
hyal = 'HYAL';
HYAL = numel(YPred,YPred == hyal);
disp(['HYAL = ',num2str(HYAL)]);
mucs = 'MUCS';
MUCS = numel(YPred,YPred == mucs);
disp(['MUCS = ',num2str(MUCS)]);
rbc = 'RBC';
RBC = numel(YPred,YPred == rbc);
disp(['RBC = ',num2str(RBC)]);
wbc = 'WBC';
WBC = numel(YPred,YPred == wbc);
disp(['WBC = ',num2str(WBC)]);
wbcc = 'WBCC';
WBCC = numel(YPred,YPred == wbcc);
disp(['WBCC = ',num2str(WBCC)]);
sum = numel(YPred);
disp(['sum = ',num2str(sum)]);
%求出每个标签对应的分类数量
% numel(A) 返回数组A的数目
% numel(A,x) 返回数组A在x的条件下的数目
%% 计算精确度
YTest = imds.Labels;
accuracy = mean(YPred == YTest);
disp(['accuracy = ',num2str(accuracy)]);
% disp(x) 变量x的值
% num2str(x) 将数值数值转换为表示数字的字符数组
toc;
%% 绘制混淆矩阵
predictLabel = YPred;%通过训练好的模型分类后的标签
actualLabel = YTest;%原始的标签
plotconfusion(actualLabel,predictLabel,'Alexnet');%绘制混淆矩阵
%plotconfusion(targets,outputs);绘制混淆矩阵,使用target(true)和output(predict)标签,将标签指定为分类向量或1到N的形式
%% 随机显示测试分类后的图片
idx = randperm(numel(imds.Files),9);
figure
for i = 1:9
subplot(3,3,i);
I = readimage(imds,idx(i));
imshow(I);
label = YPred(idx(i));
title(string(label) + ',' + num2str(100*max(scores(idx(i),:)),3) + '%');
end
%% 将分类好的图片进行保存
x = [1:numel(imds.Files)];
Location_BYST = 'E:\image_classification\Alexnet\BYST';%写入的位置
Location_GRAN = 'E:\image_classification\Alexnet\GRAN';
Location_HYAL = 'E:\image_classification\Alexnet\HYAL';
Location_MUCS = 'E:\image_classification\Alexnet\MUCS';
Location_RBC = 'E:\image_classification\Alexnet\RBC';
Location_WBC = 'E:\image_classification\Alexnet\WBC';
Location_WBCC = 'E:\image_classification\Alexnet\WBCC';
writePostfix = '.bmp';%写入的后缀
for i = 1:numel(x)
I = readimage(imds,x(i));
Label = YPred(x(i));
Name = YTest(x(i));
switch Label
case 'BYST'
saveName = sprintf('%s%s%s_%d',Location_BYST,'\',Name,i,writePostfix); %这里的Name为图片的原始标签,这样就可以查看分类错误的图片。
imwrite(I,saveName); %若只是用来分类,则将Name改为Label。
case 'GRAN'
saveName = sprintf('%s%s%s_%d',Location_GRAN,'\',Name,i,writePostfix);
imwrite(I,saveName);
case 'HYAL'
saveName = sprintf('%s%s%s_%d',Location_HYAL,'\',Name,i,writePostfix);
imwrite(I,saveName);
case 'MUCS'
saveName = sprintf('%s%s%s_%d',Location_MUCS,'\',Name,i,writePostfix);
imwrite(I,saveName);
case 'RBC'
saveName = sprintf('%s%s%s_%d',Location_RBC,'\',Name,i,writePostfix);
imwrite(I,saveName);
case 'WBC'
saveName = sprintf('%s%s%s_%d',Location_WBC,'\',Name,i,writePostfix);
imwrite(I,saveName);
case 'WBCC'
saveName = sprintf('%s%s%s_%d',Location_WBCC,'\',Name,i,writePostfix);
imwrite(I,saveName);
end
end