MATLAB对Squeezenet模型进行迁移学习

使用MATLAB自带的Squeezenet模型进行迁移学习,若没有安装Squeezenet模型支持工具,在命令窗口输入squeezenet,点击下载链接进行安装。
训练环境:Windows10系统,MATLAB20018b,CPU i3 3.7GHz,4GB内存。
使用squeezenet模型进行迁移学习的MATLAB代码如下:

%% 加载数据
clc;close all;clear;
% unzip('MerchData.zip');
Location = 'E:\Graduation_Project\RBC&WBC';
imds = imageDatastore(Location,...
                       'IncludeSubfolders',true,...
                       'LabelSource','foldernames');
[imdsTrain,imdsValidation] = splitEachLabel(imds,0.7);

%% 加载预训练网络
SqueezenetTrain = squeezenet;
% analyzeNetwork(SqueezenetTrain)%展示网络的层次结构和细节信息
% SqueezenetTrain.Layers(1)
inputSize = SqueezenetTrain.Layers(1).InputSize;

%% 替代最终层
if isa(SqueezenetTrain,'SeriesNetwork') 
  lgraph = layerGraph(SqueezenetTrain.Layers); 
else
  lgraph = layerGraph(SqueezenetTrain);
end 
[learnableLayer,classLayer] = findLayersToReplace(lgraph);
[learnableLayer,classLayer] 
numClasses = numel(categories(imdsTrain.Labels));

if isa(learnableLayer,'nnet.cnn.layer.FullyConnectedLayer')
    %isa(obj,'ClassName')确定类是否为指定输入的对象
    %如果obj是指定classCategory中任何类的实例则返回true否则返回false。
    newLearnableLayer = fullyConnectedLayer(numClasses, ...
        'Name','new_fc', ...
        'WeightLearnRateFactor',20, ...
        'BiasLearnRateFactor',20);
    
elseif isa(learnableLayer,'nnet.cnn.layer.Convolution2DLayer')
    newLearnableLayer = convolution2dLayer(1,numClasses, ...
        'Name','new_conv', ...
        'WeightLearnRateFactor',20, ...
        'BiasLearnRateFactor',20);
end

lgraph = replaceLayer(lgraph,learnableLayer.Name,newLearnableLayer);

newClassLayer = classificationLayer('Name','new_classoutput');
lgraph = replaceLayer(lgraph,classLayer.Name,newClassLayer);
% 
% figure('Units','normalized','Position',[0.3 0.3 0.4 0.4]);
% plot(lgraph)
% ylim([0,10])

%% 冻结初始层
layers = lgraph.Layers;
connections = lgraph.Connections;

layers(1:10) = freezeWeights(layers(1:10));
lgraph = createLgraphUsingConnections(layers,connections);

%% 训练网络
pixelRange = [-30 30];
scaleRange = [0.9 1.1];
imageAugmenter = imageDataAugmenter( ...
    'RandXReflection',true, ...
    'RandXTranslation',pixelRange, ...
    'RandYTranslation',pixelRange, ...
    'RandXScale',scaleRange, ...
    'RandYScale',scaleRange);
augimdsTrain = augmentedImageDatastore(inputSize(1:2),imdsTrain, ...
    'DataAugmentation',imageAugmenter);

augimdsValidation = augmentedImageDatastore(inputSize(1:2),imdsValidation);

options = trainingOptions('sgdm', ...
    'MiniBatchSize',10, ...
    'MaxEpochs',6, ...
    'InitialLearnRate',1e-4, ...
    'Shuffle','every-epoch', ...
    'ValidationData',augimdsValidation, ...
    'ValidationFrequency',60, ... 
    'Verbose',true, ...
    'Plots','training-progress');

SqueezenetTrain = trainNetwork(augimdsTrain,lgraph,options);
%% 验证分类图片
[YPred,probs] = classify(SqueezenetTrain,augimdsValidation);
accuracy = mean(YPred == imdsValidation.Labels)
idx = randperm(numel(imdsValidation.Files),4);

figure
for i = 1:4
    subplot(2,2,i)
    I = readimage(imdsValidation,idx(i));
    imshow(I)
    label = YPred(idx(i));
    title(string(label) + ", " + num2str(100*max(probs(idx(i),:)),3) + "%");
end

%% 保存训练好的模型
save Squeezenet_01 SqueezenetTrain;

函数findLayerToReplace

% findLayersToReplace(lgraph) finds the single classification layer and the
% preceding learnable (fully connected or convolutional) layer of the layer
% graph lgraph.
function [learnableLayer,classLayer] = findLayersToReplace(lgraph)

if ~isa(lgraph,'nnet.cnn.LayerGraph')
    error('Argument must be a LayerGraph object.')
end

% Get source, destination, and layer names.
src = string(lgraph.Connections.Source);
dst = string(lgraph.Connections.Destination);
layerNames = string({lgraph.Layers.Name}');

% Find the classification layer. The layer graph must have a single
% classification layer.
isClassificationLayer = arrayfun(@(l) ...
    (isa(l,'nnet.cnn.layer.ClassificationOutputLayer')|isa(l,'nnet.layer.ClassificationLayer')), ...
    lgraph.Layers);

if sum(isClassificationLayer) ~= 1
    error('Layer graph must have a single classification layer.')
end
classLayer = lgraph.Layers(isClassificationLayer);


% Traverse the layer graph in reverse starting from the classification
% layer. If the network branches, throw an error.
currentLayerIdx = find(isClassificationLayer);
while true
    
    if numel(currentLayerIdx) ~= 1
        error('Layer graph must have a single learnable layer preceding the classification layer.')
    end
    
    currentLayerType = class(lgraph.Layers(currentLayerIdx));
    isLearnableLayer = ismember(currentLayerType, ...
        ['nnet.cnn.layer.FullyConnectedLayer','nnet.cnn.layer.Convolution2DLayer']);
    
    if isLearnableLayer
        learnableLayer =  lgraph.Layers(currentLayerIdx);
        return
    end
    
    currentDstIdx = find(layerNames(currentLayerIdx) == dst);
    currentLayerIdx = find(src(currentDstIdx) == layerNames);
    
end

end

MATLAB对Squeezenet模型进行迁移学习_第1张图片
使用训练好的squeezenet模型进行图片分类测试,我训练的模型是对BYST、GRAN、HYAL、MUCS、RBC、WBC、WBCC等图像进行分类:

%% 加载模型
 clc;close all;clear;
load('-mat','E:\MATLAB_Code\Squeezenet_01');
%% 加载测试集
Location = 'E:\image_test\test_02';
imds = imageDatastore(Location,'includeSubfolders',true,'LabelSource','foldernames');
inputSize = SqueezenetTrain.Layers(1).InputSize; 
imdstest = augmentedImageDatastore(inputSize(1:2),imds);
tic;
[YPred,scores] = classify(SqueezenetTrain,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)  将数值数值转换为表示数字的字符数组


%% 绘制混淆矩阵
predictLabel = YPred;%通过训练好的模型分类后的标签
actualLabel = YTest;%原始的标签
plotconfusion(actualLabel,predictLabel,'Squeezenet');%绘制混淆矩阵
%    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 = numel(imds.Files);
% 图片保存位置
Location_BYST = 'E:\image_classification\Squeezenet\BYST';
Location_GRAN = 'E:\image_classification\Squeezenet\GRAN';
Location_HYAL = 'E:\image_classification\Squeezenet\HYAL';
Location_MUCS = 'E:\image_classification\Squeezenet\MUCS';
Location_RBC  = 'E:\image_classification\Squeezenet\RBC';
Location_WBC  = 'E:\image_classification\Squeezenet\WBC';
Location_WBCC = 'E:\image_classification\Squeezenet\WBCC';
writePostfix = '.bmp';%图片保存后缀
for i = 1:x
    I = readimage(imds,i);
    Label = YPred(i);
    Name = YTest(i);
   switch Label
       case 'BYST'
           saveName = sprintf('%s%s%s_%d',Location_BYST,'\',Name,i,writePostfix);
           imwrite(I,saveName);
       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

程序运行结果:
MATLAB对Squeezenet模型进行迁移学习_第2张图片
MATLAB对Squeezenet模型进行迁移学习_第3张图片
MATLAB对Squeezenet模型进行迁移学习_第4张图片

你可能感兴趣的:(MATLAB对Squeezenet模型进行迁移学习)