MATLAB与深度学习(二)— 训练神经网络(图像分类识别)

上一篇,我们介绍了与深度学习相关的MATLAB工具包。这一篇,我们将介绍如何训练神经网络和相关的基础知识。本文借鉴和引用了网上许多前辈的经验和代码,如有冒犯,请及时与我联系。

1. 下载数据集
以CIFAR-10数据集为例
1.1 方式一,在matlab上下载

%% Download the CIFAR-10 dataset
if ~exist('cifar-10-batches-mat','dir')cifar10Dataset = 'cifar-10-matlab';disp('Downloading 174MB CIFAR-10 dataset...');   websave([cifar10Dataset,'.tar.gz'],...['https://www.cs.toronto.edu/~kriz/',cifar10Dataset,'.tar.gz']);gunzip([cifar10Dataset,'.tar.gz'])delete([cifar10Dataset,'.tar.gz'])untar([cifar10Dataset,'.tar'])delete([cifar10Dataset,'.tar'])
end

1.2 方式二,直接下载
我运行方式一的代码会报错,所以直接通过下面网址下载后。然后把数据集放在MATLAB的工作路径上。
https://www.cs.toronto.edu/~kriz/cifar-10-matlab.tar.gz;链接.

2. 利用CIFAR10数据集 — 迁移学习训练现有网络
以Alexnet为例:图像分类识别

convnet = alexnet;
convnet.Layers % Take a look at the layers


常用网络层
卷积层将输入图像放进一组卷积滤波器,每个滤波器激活图像中的某些特征。

ReLU 层通过将负值映射到零和保持正数值,实现更快、更高效的训练。

池化层通过执行非线性下采样,减少网络需要学习的参数个数,从而简化输出。

全连接层将网络 2D 空间特征“扁平化”为 1D 矢量,为分类目的而表示图像级特征。

Softmax 层为数据集中的每个类别提供概率。

下载完成后,转换数据格式,不然后面会报错:

 saveCIFAR10AsFolderOfImages('cifar-10-batches-mat', pwd, true);

数据设置,CIFAR-10数据集可以选择的对象是10种

rootFolder = 'cifar10Train';
categories = {'Deer','Dog','Frog','Cat'};
imds = imageDatastore(fullfile(rootFolder, categories), 'LabelSource', 'foldernames');
imds.ReadFcn = @readFunctionTrain;% Change the number 50 to as many training images as you would like to use
% how does increasing the number of images change the
% accuracy of the classifier?
[trainingSet, ~] = splitEachLabel(imds, 50, 'randomize');

通过激活来提取数据集的特征

featureLayer = 'fc7';
trainingFeatures = activations(convnet, trainingSet, featureLayer);

训练SVM分类器

temp(:,:)=trainingFeatures ;
temp=temp';
classifier = fitcnb(temp, trainingSet.Labels);

测试

rootFolder = 'cifar10Test';
testSet = imageDatastore(fullfile(rootFolder, categories), 'LabelSource', 'foldernames');
testSet.ReadFcn = @readFunctionTrain;
testFeatures = activations(convnet, testSet, featureLayer);
temp1(:,:)=testFeatures ;
temp=temp1';
predictedLabels = predict(classifier, temp);
confMat = confusionmat(testSet.Labels, predictedLabels);
confMat = confMat./sum(confMat,2);
mean(diag(confMat))

测试结果:整体精度为0.6447。

3. 利用CIFAR10数据集训练自主创建的神经网络模型
引用其他博主的代码作为示例:图像分类识别
https://blog.csdn.net/caokaifa/article/details/81163973;链接.

clear all; close all;clc;
%加载显示数据
[trainingImages,trainingLabels,testImages,testLabels] = helperCIFAR10Data.load('cifar10Data');
figure
thumbnails = trainingImages(:,:,:,1:100);
montage(thumbnails)%% 训练
%数据集有10类
numImageCategories = 10;
categories(trainingLabels)%建立输入层32x32x3 CIFAR-10 images
[height, width, numChannels, ~] = size(trainingImages);
imageSize = [height width numChannels];
inputLayer = imageInputLayer(imageSize);%建立网络中间层
filterSize = [5 5];
numFilters = 32;middleLayers = [% The first convolutional layer has a bank of 32 5x5x3 filters. A
% symmetric padding of 2 pixels is added to ensure that image borders
% are included in the processing. This is important to avoid
% information at the borders being washed away too early in the
% network.
convolution2dLayer(filterSize, numFilters, 'Padding', 2)  %(n+2p-f)/s+1% Note that the third dimension of the filter can be omitted because it
% is automatically deduced based on the connectivity of the network. In
% this case because this layer follows the image layer, the third
% dimension must be 3 to match the number of channels in the input
% image.% Next add the ReLU layer:
reluLayer()% Follow it with a max pooling layer that has a 3x3 spatial pooling area
% and a stride of 2 pixels. This down-samples the data dimensions from
% 32x32 to 15x15.
maxPooling2dLayer(3, 'Stride', 2)% Repeat the 3 core layers to complete the middle of the network.
convolution2dLayer(filterSize, numFilters, 'Padding', 2)
reluLayer()
maxPooling2dLayer(3, 'Stride',2)convolution2dLayer(filterSize, 2 * numFilters, 'Padding', 2)
reluLayer()
maxPooling2dLayer(3, 'Stride',2)
]%定义输出层
finalLayers = [% Add a fully connected layer with 64 output neurons. The output size of
% this layer will be an array with a length of 64.
fullyConnectedLayer(64)% Add an ReLU non-linearity.
reluLayer% Add the last fully connected layer. At this point, the network must
% produce 10 signals that can be used to measure whether the input image
% belongs to one category or another. This measurement is made using the
% subsequent loss layers.
fullyConnectedLayer(numImageCategories)% Add the softmax loss layer and classification layer. The final layers use
% the output of the fully connected layer to compute the categorical
% probability distribution over the image classes. During the training
% process, all the network weights are tuned to minimize the loss over this
% categorical distribution.
softmaxLayer
classificationLayer
]%三层合并
layers = [inputLayermiddleLayersfinalLayers
]%定义层权值
layers(2).Weights = 0.0001 * randn([filterSize numChannels numFilters]);%设置网络参数值
opts = trainingOptions('sgdm', ...'Momentum', 0.9, ...'InitialLearnRate', 0.001, ...'LearnRateSchedule', 'piecewise', ...'LearnRateDropFactor', 0.1, ...'LearnRateDropPeriod', 8, ...'L2Regularization', 0.004, ...'MaxEpochs', 40, ...'MiniBatchSize', 128, ...'Verbose', true,...'Plots','training-progress');
%  参数解释:
%  sgdm就是stochastic gradient descent with momentum(动量的随机梯度下降法),
%  Momentum是动量参数为0.9,InitialLearnRate初始学习速率0.001,L2Regularization=0.004
%  是L2正则化系数,LearnRateDropFactor=0.1、LearnRateDropPeriod=8是每8个epoces使得学习
%  速率乘以一个0.1的比例因子,MaxEpochs=?40最大训练为40个epoces,MiniBatchSize=128为Batch
%  为128,Verbose?=true就是把信息打印到命令窗口% 开始训练
% doTraining为false,直接导入已经训练好的模型,
% doTraining为True,可以自己改模型训练
doTraining = true;if doTraining    % Train a network.cifar10Net = trainNetwork(trainingImages, trainingLabels, layers, opts);
else% Load pre-trained detector for the example.load('rcnnStopSigns.mat','cifar10Net')
end%显示训练权值
w = cifar10Net.Layers(2).Weights;% rescale the weights to the range [0, 1] for better visualization
w = rescale(w);figure
montage(w)% 在测试集上训练网络
YTest = classify(cifar10Net, testImages);% 计算准确度
accuracy = sum(YTest == testLabels)/numel(testLabels)

调用函数helperCIFAR10Data代码如下


% This is helper class to download and import the CIFAR-10 dataset. The
% dataset is downloaded from:
%
% ?https://www.cs.toronto.edu/~kriz/cifar-10-matlab.tar.gz
%
% References
% ----------
% Krizhevsky, Alex, and Geoffrey Hinton. "Learning multiple layers of
% features from tiny images." (2009).
classdef helperCIFAR10Datamethods(Static)%------------------------------------------------------------------
function download(url, destination)
if nargin == 1
url = 'https://www.cs.toronto.edu/~kriz/cifar-10-matlab.tar.gz';
endunpackedData = fullfile(destination, 'cifar-10-batches-mat');
if ~exist(unpackedData, 'dir')
fprintf('Downloading CIFAR-10 dataset...');
untar(url, destination);
fprintf('done.\n\n');
end
end%------------------------------------------------------------------
% Return CIFAR-10 Training and Test data.
function [XTrain, TTrain, XTest, TTest] = load(dataLocation) location = fullfile(dataLocation, 'cifar-10-batches-mat');[XTrain1, TTrain1] = loadBatchAsFourDimensionalArray(location, 'data_batch_1.mat');
[XTrain2, TTrain2] = loadBatchAsFourDimensionalArray(location, 'data_batch_2.mat');
[XTrain3, TTrain3] = loadBatchAsFourDimensionalArray(location, 'data_batch_3.mat');
[XTrain4, TTrain4] = loadBatchAsFourDimensionalArray(location, 'data_batch_4.mat');
[XTrain5, TTrain5] = loadBatchAsFourDimensionalArray(location, 'data_batch_5.mat');XTrain = cat(4, XTrain1, XTrain2, XTrain3, XTrain4, XTrain5);
TTrain = [TTrain1; TTrain2; TTrain3; TTrain4; TTrain5];[XTest, TTest] = loadBatchAsFourDimensionalArray(location, 'test_batch.mat');end
end
endfunction [XBatch, TBatch] = loadBatchAsFourDimensionalArray(location, batchFileName)
load(fullfile(location,batchFileName));
XBatch = data';
XBatch = reshape(XBatch, 32,32,3,[]);
XBatch = permute(XBatch, [2 1 3 4]);
TBatch = convertLabelsToCategorical(location, labels);
endfunction categoricalLabels = convertLabelsToCategorical(location, integerLabels)
load(fullfile(location,'batches.meta.mat'));
categoricalLabels = categorical(integerLabels, 0:9, label_names);
end

测试结果:整体精度为0.7445。

再来一个例子:图像分类识别

%Load training data
% Please note: these are 4 of the 10 categories available
% Feel free to choose which ever you like best!
categories = {'Deer','Dog','Frog','Cat'};rootFolder = 'cifar10Train';
imds = imageDatastore(fullfile(rootFolder, categories), ...'LabelSource', 'foldernames');%Define Layers
varSize = 32;
conv1 = convolution2dLayer(5,varSize,'Padding',2,'BiasLearnRateFactor',2);
conv1.Weights = gpuArray(single(randn([5 5 3 varSize])*0.0001));
fc1 = fullyConnectedLayer(64,'BiasLearnRateFactor',2);
fc1.Weights = gpuArray(single(randn([64 576])*0.1));
fc2 = fullyConnectedLayer(4,'BiasLearnRateFactor',2);
fc2.Weights = gpuArray(single(randn([4 64])*0.1));layers = [imageInputLayer([varSize varSize 3]);conv1;maxPooling2dLayer(3,'Stride',2);reluLayer();convolution2dLayer(5,32,'Padding',2,'BiasLearnRateFactor',2);reluLayer();averagePooling2dLayer(3,'Stride',2);convolution2dLayer(5,64,'Padding',2,'BiasLearnRateFactor',2);reluLayer();averagePooling2dLayer(3,'Stride',2);fc1;reluLayer();fc2;softmaxLayer()classificationLayer()];%Define training options
opts = trainingOptions('sgdm', ...'InitialLearnRate', 0.001, ...'LearnRateSchedule', 'piecewise', ...'LearnRateDropFactor', 0.1, ...'LearnRateDropPeriod', 8, ...'L2Regularization', 0.004, ...'MaxEpochs', 10, ...'MiniBatchSize', 100, ...'Verbose', true);%tain
[net, info] = trainNetwork(imds, layers, opts);%test
rootFolder = 'cifar10Test';
imds_test = imageDatastore(fullfile(rootFolder, categories), ...'LabelSource', 'foldernames');labels = classify(net, imds_test);ii = randi(4000);
im = imread(imds_test.Files{ii});
imshow(im);
if labels(ii) == imds_test.Labels(ii)colorText = 'g';
elsecolorText = 'r';
end
title(char(labels(ii)),'Color',colorText);% This could take a while if you are not using a GPU
confMat = confusionmat(imds_test.Labels, labels);
confMat = confMat./sum(confMat,2);
mean(diag(confMat))

测试结果:整体精度0.7570。

MATLAB与深度学习(二)— 训练神经网络(图像分类识别)相关推荐

  1. 【毕业设计_课程设计】基于深度学习网络模型训练的车型识别系统

    文章目录 0 项目说明 1 简介 2 模型训练精度 3 扫一扫识别功能 4 技术栈 5 模型训练 6 最后 0 项目说明 基于深度学习网络模型训练的车型识别系统 提示:适合用于课程设计或毕业设计,工作 ...

  2. caffe图像分类教程_跟我上手深度学习: 五分钟尝试第一个深度学习(Caffe)训练和图像分类(详细图文步骤)...

    申请深度学习的开发环境 Supervessel超能云(www.ptopenlab.com)上可以免费申请深度学习的开发环境.用户可以免费注册一个用户帐号,无须任何信用卡信息,就可以申请. 申请了用户帐 ...

  3. 【MATLAB、深度学习】AlexNet及VGG神经网络在MATLAB上的应用

    基于MATLAB2019a.---和同学一起完成老师给的作业. 在MATLAB中这两种框架的神经网络是已经经过训练的网络,官方的数据是使用了百万张训练了大约100多个类别的物品,也就是大概可以识别一千 ...

  4. 深度学习经典网络解析图像分类篇(二):AlexNet

    深度学习经典网络解析图像分类篇(二):AlexNet 1.背景介绍 2.ImageNet 3.AlexNet 3.1AlexNet简介 3.2AlexNet网络架构 3.2.1第一层(CONV1) 3 ...

  5. 【深度学习】LSTM神经网络解决COVID-19预测问题(二)

    [深度学习]LSTM神经网络解决COVID-19预测问题(二) 文章目录 1 概述 2 模型求解和检验 3 模型代码 4 模型评价与推广 5 参考 1 概述 建立一个普适性较高的模型来有效预测疫情的达 ...

  6. 深度学习之卷积神经网络(Convolutional Neural Networks, CNN)(二)

    前面我们说了CNN的一般层次结构, 每个层的作用及其参数的优缺点等内容.深度学习之卷积神经网络(Convolutional Neural Networks, CNN)_fenglepeng的博客-CS ...

  7. 【零基础深度学习教程第二课:深度学习进阶之神经网络的训练】

    深度学习进阶之神经网络的训练 神经网络训练优化 一.数据集 1.1 数据集分类 1.2 数据集的划分 1.3 同源数据集的重要性 1.4 无测试集的情况 二.偏差与方差 2.1 概念定义 2.1.1 ...

  8. CV之YOLOv3:深度学习之计算机视觉神经网络Yolov3-5clessses训练自己的数据集全程记录(第二次)

    YOLOv3:深度学习之计算机视觉神经网络Yolov3-5clessses训练自己的数据集全程记录(第二次) 目录 训练记录 训练记录

  9. CV之YOLOv3:深度学习之计算机视觉神经网络Yolov3-5clessses训练自己的数据集全程记录

    CV之YOLOv3:深度学习之计算机视觉神经网络Yolov3-5clessses训练自己的数据集全程记录 目录 视频请观看 训练输出记录 视频请观看 深度学习之计算机视觉神经网络训练Yolov3-5c ...

最新文章

  1. 2022-2028年中国反射偏光膜行业市场研究及前瞻分析报告
  2. eyoucms range 范围判断标签
  3. 乌当区利用大数据织密环境监测保护网
  4. CodeForces 689B Mike and Shortcuts (bfs or 最短路)
  5. java输出不同颜色_Java设计模式-策略模式、状态模式
  6. 交叉编译出现skipping incompatible_交叉编译bluez-5.50
  7. case when怎么取别名_《小鞋子》:没有伞的孩子在雨中奔跑,没有鞋的孩子应该怎么做?...
  8. UVA10602 Editor Nottoobad【贪心】
  9. [leetcode]5178. 四因数
  10. 路径读取os.path.abspath、os.path.dirname、os.path.basename、os.path.split
  11. 「leetcode」501. 二叉搜索树中的众数【暴力统计】【中序遍历】详解
  12. jQuery中绑定事件的命名空间详解
  13. 挤房工具发布--支持最新版浩方和VS平台
  14. Javashop B2B2C 系统之社区团购商城
  15. Qt怎么实现将bmp图片转换成Ascii_你保存的word和pdf文档图片为什么变模糊了?
  16. JDBC中connection.isClosed 和 connection.isValid的区别
  17. LimeSDR有用的网页链接
  18. 响应式Web设计帮助移动终端访问网站
  19. 麻省理工学院公开课:计算机科学及编程导论 课堂笔记
  20. As a developer —— dotNet —— 01VB是什么

热门文章

  1. 天天团购注册页面空白问题
  2. 11g导出报错:EXP-00106: Invalid Database Link Passwords
  3. C++11 - std::shared_ptr初始化的几种方式
  4. 安卓盘点机PDA的WIFI连接
  5. 数据分组技术GroupBy
  6. SEO:必须登录才能查看文章或者文章列表,实则也是阻挡蜘蛛
  7. 匕首线切割图纸下载_干净匕首
  8. 就业指导:电脑工程师的需求分析和就业指导
  9. python调用打印机打印图片_pyqt5 调用打印机 打印远程图片
  10. 联想天逸笔记本关闭Fn功能,直接使用F1~12键