版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_41569159/article/details/89048904
在刚开始使用MATLAB进行深度学习时,使用MATLAB自带的深度学习工具箱进行迁移学习,十分方便。对只是使用这个工具的人来说非常便利。我这里采用的是MATLAB关于Alexnet模型的帮助文档,来进行迁移学习。详细可以在命令行中输入 help alexnet查看帮助
使用训练好的Alexnet模型进行分类查看:https://blog.csdn.net/qq_41569159/article/details/89511493
%% 加载数据集
unzip('MerchData.zip')
%unzip 提取zip文件的内容
%Location = 'E:\Image_Set';%使用自己的数据集进行训练
imds = imageDatastore('MerchData', ...%若使用自己的数据集,将其改为Location
'IncludeSubfolders',true, ...
'LabelSource','foldernames');
%imageDatastore 图像数据的数据存储 /...续行
% imds = imageDatastore(location,Name,Value)
% location-要包括在数据存储中的文件或文件夹
% Name-相对组参数
% 'IncludeSubfolders'-子文件夹包含标记
% ture or false | 0 or 1 (逗号隔开)
% ————指定ture可包含每个文件夹中的所有文件和子文件夹,指定false则仅包含每个文件夹中的文件
% 'FileExtensions'-图像文件扩展名
% 'AlternateFileSystemRoots'-备用文件系统根路径
% 'LabelSource'-提供标签数据的源
% 指定为以逗号分隔的对组,如果指定了'none',则Labels属性为空。如果指定了'foldernames',将根据文件夹名称分配标签并
% 存储在Labels属性中
[imdsTrain,imdsValidation] = splitEachLabel(imds,0.7,'randomized');%随机拆分imds中的文件,前70%放入imdsTrain中,剩余30%翻入imdsValidation中
% splitEachLabel-按比例拆分ImageDatastor标签
% [imds1,imds2] = splitEachLabel(imds,p)
% 可将imds中的图像文件拆分为两个新的数据存储,imds1和imds2.新的数据存储imds1包含每个标签前百分之p个文件,imds2中包含剩余的文件
numTrainImages = numel(imdsTrain.Labels);%numel数组中的元素数目
%随机显示数据集中的图像
% idx = randperm(numTrainImages,16);
% % randperm——随机置换
% % p = randperm(n,k)返回行向量,其中包含1到n自检随机选择的k个唯一整数
% figure
% for i = 1:16
% subplot(4,4,i)
% I = readimage(imdsTrain,idx(i));
% % img = readimage(imds,i)——从数据存储imds中读取地i个图像文件并返回图像数据img
% imshow(I)
% end
%% 加载预训练网络
Alexnet_Train = alexnet;
%net.Layers %展示这个网络架构,这个网络有5个卷积层和3个全连接层
inputSize = Alexnet_Train.Layers(1).InputSize;
% 第一层是图像输入层,要求输入图像的尺寸为227*227*3 这里的3是颜色通道的数字
%% 替换最后三层
layersTransfer = Alexnet_Train.Layers(1:end-3);
% Extract all layers, exceptthe last three ,from the pretrained network
% 预处理网络的最后三层被配置为1000个类。这三层必须针对新的分类问题进行调整
numClasses = numel(categories(imdsTrain.Labels));%数据集中类的数目
layers = [
layersTransfer
fullyConnectedLayer(numClasses,'WeightLearnRateFactor',20,'BiasLearnRateFactor',20)
softmaxLayer
classificationLayer];
% Transfer the layers to the new classification task by replacing the last three layers with a fully connected layer,
%a softmax layer, and a classification output layer. Specify the options of the new fully connected layer according
%to the new data. Set the fully connected layer to have the same size as the number of classes in the new data.
%To learn faster in the new layers than in the transferred layers, increase the WeightLearnRateFactor and
%BiasLearnRateFactor values of the fully connected layer.
% 通过将最后三个图层替换为完全连接图层,softmax图层和分类输出图层,将图层转移到新的分类任务。根据新的数据指定新的完全连接层的
% 选项。将完全连接层设置为与新数据中的类数大小相同。要在新层中比传输层更快的学习,增加完全连接层的WeightLearnRateFactor 和
% BiasLearnRateFactor的值
%% 训练网络
%用于数据增强,增加数据量
% The network requires input images of size 227-by-227-by-3, but the images in the image datastores have different sizes.
% Use an augmented image datastore to automatically resize the training images. Specify additional augmentation operations
% to perform on the training images: randomly flip the training images along the vertical axis, and randomly translate them up
% to 30 pixels horizontally and vertically. Data augmentation helps prevent the network from overfitting and memorizing the exact details of the training images.
% 这个网络要求的尺寸是227*227*3,但是在图像存储中的图像有不同的尺寸,使用增强数据存储自动调整训练图像大小。在训练图像中指定
% 额外的增强操作:沿着垂直轴随机翻转训练图像,水平和垂直随机移动30个像素单位
pixelRange = [-30 30];
imageAugmenter = imageDataAugmenter( ...
'RandXReflection',true, ...
'RandXTranslation',pixelRange, ...
'RandYTranslation',pixelRange);
% imageDataAugmenter——配置图像数据扩充
% aug = imageDataAugmenter(Name,Value)
% RandXReflection——左右方向的随机反射,当为真(ture/1)时每个图像都以50%的概率随机反射,当为假(falsw/0)时,不会反射图像
% RandYReflection——上下方向的随机反射,当为真(ture/1)时每个图像都以50%的概率随机反射,当为假(falsw/0)时,不会反射图像
% RandRotation——随机旋转
% 'RandRotation',[num1 num2]第二个元素必须大于等于第一个元素,角度从指定间隔内的连续均匀分布中随机选取
% RandScale——均匀标度范围(应用于输入图像的均匀(各向同性)缩放范围)
% 'RandScale',[num1 num2]第二个元素必须大于等于第一个元素,比例因子从指定间隔内的连续均匀分布中随机选取
% RandXScale——均匀标度范围(应用于输入图像的水平缩放范围)
% 'RandXScale',[num1 num2]第二个元素必须大于等于第一个元素,比例因子从指定间隔内的连续均匀分布中随机选取
% RandYScale——均匀标度范围(应用于输入图像的垂直缩放范围)
% 'RandYScale',[num1 num2]第二个元素必须大于等于第一个元素,比例因子从指定间隔内的连续均匀分布中随机选取
% RandXShear——水平剪切范围
% 'RandXShear',[num1 num2]第二个元素必须大于等于第一个元素,水平剪切角从指定间隔内的连续均匀分布中随机选取
% RandYShear——垂直剪切范围
% 'RandYShear',[num1 num2]第二个元素必须大于等于第一个元素,垂直剪切角从指定间隔内的连续均匀分布中随机选取
% RandXTranslation——水平转换范围
% 'RandXTranslation',[num1 num2]第二个元素必须大于等于第一个元素,水平平移距离从指定间隔内的连续均匀分布中随机选取
% RandYTranslation——垂直转换范围
% 'RandYTranslation',[num1 num2]第二个元素必须大于等于第一个元素,垂直平移距离从指定间隔内的连续均匀分布中随机选取
augimdsTrain = augmentedImageDatastore(inputSize(1:2),imdsTrain, ...
'DataAugmentation',imageAugmenter);
% augmentedImageDatastore——将批处理转化为增强图像数据
% DataAugmentation——图像数据扩充
% To automatically resize the validation images without performing further data augmentation, use an augmented image datastore
%without specifying any additional preprocessing operations.
% 自动调整验证图像大小而不进行其他数据增强。使用扩充图像数据存储而不指定其他预处理操作。
augimdsValidation = augmentedImageDatastore(inputSize(1:2),imdsValidation);
% Specify the training options. For transfer learning, keep the features from the early layers of the pretrained network (the transferred layer weights).
%To slow down learning in the transferred layers, set the initial learning rate to a small value. In the previous step, you increased the learning rate factors
%for the fully connected layer to speed up learning in the new final layers. This combination of learning rate settings results in fast learning only in the new
%layers and slower learning in the other layers. When performing transfer learning, you do not need to train for as many epochs. An epoch is a full training cycle
%on the entire training data set. Specify the mini-batch size and validation data. The software validates the network every ValidationFrequency iterations during training.
% 指定训练选项,对于传输学习保留来自预训练网络早期层中的特征。为了延缓在传输层中的学习,将初始学习速率设置为一个小值。在前一步中
% 增加了完全连接层的学习因子去加速新的最终层中的学习。这种学习速率的结合会导致新层中学习速度加快,其他层中学习速度变慢。在进行转换学习时
% 你不需要这么多的时间段进行训练。一个时间段是整个训练数据集上的完全训练循环。指定最小批量和验证数据。软件在训练期间每次验证迭代都会验证网络
options = trainingOptions('sgdm', ...
'MiniBatchSize',10, ...
'MaxEpochs',6, ...
'InitialLearnRate',1e-4, ...
'ValidationData',augimdsValidation, ...
'ValidationFrequency',3, ...
'ValidationPatience',Inf, ...
'Verbose',true,...
'Plots','training-progress');
% trainingOptions——深度学习神经网络训练选项
% 语法——options = trainingOptions(solverName,Name,Value)
% solverName——训练网络检算器
% 'sgdm'——使用随机梯度下降动量(SGDM)优化器。
% 'rmsprop'——使用RMSProp优化器
% 'adam'——使用adam优化器
% 绘图和显示
% 'plots'——在网络训练时显示的图
% 'none'or'default' 在训练时期不显示图
% 'taining-progress' 显示训练过程。这个图显示了最小批量损失和准确性,验证损失和准确性,和训练过程中的额外信息。对训练过程的更多信息,查看Monitor Deep Learning Training Progress.
% Verbose——显示训练过程信息指示器
% MaxEpochs——最大训练周期数
% 用于训练的最大周期数, 'MaxEpochs',正整数
% MiniBatchSize——最小批量的大小
% 用于每个训练迭代的小批量大小 'MiniBatchSize',正整数 小批量是训练集的一个子集,用于评估损失函数的梯度并更新权重
% ValidationData——训练期间用于验证的数据
% ValidationFrequency——网络验证频率
% 以迭代次数表示网络验证频率 'ValidationFrequency',正整数
% ValidationPatience——停止验证的patience
% 'ValidationPatience',inf or 正整数 若使用默认inf训练将在最大的epoch数后停止
% InitialLearnRate——初始学习率
% 'InitialLearnRate',正数 'sgdm'解算器的默认值为0.01,'rmsprop' 'adam' 解算器的默认值为0.001.
% 如果学习率太低,那么培训需要很长时间,如果学习率太高那么训练可能达不到最佳结果或出现分歧
Train = trainNetwork(augimdsTrain,layers,options);
% trainNetwork——训练神经网络进行深度学习
%% 验证训练好的模型
[YPred,scores] = classify(Train,augimdsValidation);
% classify——使用经过训练的神经网络对数据进行分类
idx = randperm(numel(imdsValidation.Files),6);
% randperm——随机置换
% p = randperm (n,k) 返回行向量,其中包含1到n之间随机选择k个唯一整数
%随机显示使用训练好的模型进行分类的图片及其标签和概率
figure
for i = 1:6
subplot(2,3,i)
I = readimage(imdsValidation,idx(i));
imshow(I)
label = YPred(idx(i));
title(string(label) + "," + num2str(100*max(scores(idx(i),:)),3) + "%");
end
YValidation = imdsValidation.Labels;
accuracy = mean(YPred == YValidation);
disp(['accuracy:',num2str(accuracy)]); % 输出预测精度结果
%% 保存训练好的模型
save Alexnet_01 Train;