matlab 关于利用深度学习进行图像识别
2018-03-20 15:01
639 查看
深度学习进行图像识别现在主要 是利用CNN来进行操作,其中图像预处理涉及到灰度处理,零均值,图像分割,图像增强等等,比较多。
最近在用matlab进行图像识别这一方面的实验,在matlab官网上出了很多这样的例子,提出里两种方法:training from scratch和transfer learning。
我比较倾向于transfer learning,主需要少量的训练数据就可以进行对已经训练好的网络进行参数的微调和修正,并应用于新问题,而且收到的效果还是不错的。
附上视频教程:https://cn.mathworks.com/videos/object-recognition-deep-learning-and-machine-learning-for-computer-vision-121144.html?s_iid=doc_rw_VP_footer
在transfer learning 中也有两种方法,一种是利用提取到的特征直接对classifier进行训练,可以是SVM等等,这种我已经实现了,另外一种就是trainNetwork了,我在其中的训练过程中,matlab一直提示没有与‘single’对于的‘apply’函数,版本是2016b,并使用了gpu加速,一直怀疑是我的matlab问题?因为我是用的破解版,希望和大家交流,来一起解决这个问题
附上一份代码,希望对大家有帮助
% Copyright 2017 The MathWorks, Inc.
%% Deep Learning: Transfer Learning in 10 Lines of MATLAB Code
% Transfer learning is a very practical way to use deep learning by
% modifying an existing deep network(usually trained by an expert) to work
% with your data.
%% Problem statement
% The problem we tried to solve with transfer learning is to distinguish
% between 5 categories of food - cupcakes, burgers, apple pie, hot dogs and
% ice cream. To get started you need two things:
%
% # Training images of the different object classes
% # A pre-trained deep neural network (AlexNet)
% You can substitute these categories for any of your own based on what
% image data you have avaliable.
%% Load Training Images
% In order for imageDataStore to parse the folder names as category labels,
% you would have to store image categories in corresponding sub-folders.
rootFolder = fullfile('C:\Users\lzp\AppData\Local\Temp\caltech101\101_ObjectCategories');
categories = {'ASKsegement', 'FSKsegement', 'PSKsegement'};
%%
% Create an |ImageDatastore| to help you manage the data. Because
% |ImageDatastore| operates on image file locations, images are not loaded
% into memory until read, making it efficient for use with large image
% collections.
allImages = imageDatastore(fullfile(rootFolder, categories), 'LabelSource', 'foldernames');
%% Split data into training and test sets
[trainingImages, testImages] = splitEachLabel(allImages, 0.8, 'randomize');
%% Load Pre-trained Network (AlexNet)
% AlexNet is a pre-trained network trained on 1000 object categories.
% AlexNet is avaliable as a support package on FileExchange.
cnnMatFile =fullfile('C:\Users\lzp\AppData\Local\Temp','imagenet-caffe-alex25.mat');
convnet=helperImportMatConvNet(cnnMatFile);
%% Review Network Architecture
convnet.Layers
%% Modify Pre-trained Network
% AlexNet was trained to recognize 1000 classes, we need to modify it to
% recognize just 5 classes.
layers = convnet.Layers(1:end-3);
layers(23) = fullyConnectedLayer(3); % change this based on # of classes
layers(24) = softmaxLayer();
layers(25) = classificationLayer;
%% Perform Transfer Learning
% For transfer learning we want to change the weights of the network ever so slightly. How
% much a network is changed during training is controlled by the learning
% rates.
opts = trainingOptions('sgdm', 'InitialLearnRate', 0.001,...
'MaxEpochs', 20, 'MiniBatchSize', 64);
%% Set custom read function
% One of the great things about imageDataStore it lets you specify a
% "custom" read function, in this case it is simply resizing the input
% images to 227x227 pixels which is what AlexNet expects. You can do this by
% specifying a function handle of a function with code to read and
% pre-process the image.
trainingImages.ReadFcn = @readFunctionTrain;
trainingLabels = categorical(trainingImages.Labels);
% trainingFiles = cell(trainingSet.Files);
trainingImages = readall(trainingImages);
trainingImages = cat(4, trainingImages{:});
% trainingfiles =imageDatastore(trainingImages );
%% Train the Network
% This process usually takes about 5-20 minutes on a desktop GPU.
myNet = trainNetwork(trainingImages,trainingLabels,layers, opts);
%% Test Network Performance
% Now let's the test the performance of our new "snack recognizer" on the test set.
testImages.ReadFcn = @readFunctionTrain;
predictedLabels = classify(myNet, testImages);
accuracy = mean(predictedLabels == testImages.Labels)
最近在用matlab进行图像识别这一方面的实验,在matlab官网上出了很多这样的例子,提出里两种方法:training from scratch和transfer learning。
我比较倾向于transfer learning,主需要少量的训练数据就可以进行对已经训练好的网络进行参数的微调和修正,并应用于新问题,而且收到的效果还是不错的。
附上视频教程:https://cn.mathworks.com/videos/object-recognition-deep-learning-and-machine-learning-for-computer-vision-121144.html?s_iid=doc_rw_VP_footer
在transfer learning 中也有两种方法,一种是利用提取到的特征直接对classifier进行训练,可以是SVM等等,这种我已经实现了,另外一种就是trainNetwork了,我在其中的训练过程中,matlab一直提示没有与‘single’对于的‘apply’函数,版本是2016b,并使用了gpu加速,一直怀疑是我的matlab问题?因为我是用的破解版,希望和大家交流,来一起解决这个问题
附上一份代码,希望对大家有帮助
% Copyright 2017 The MathWorks, Inc.
%% Deep Learning: Transfer Learning in 10 Lines of MATLAB Code
% Transfer learning is a very practical way to use deep learning by
% modifying an existing deep network(usually trained by an expert) to work
% with your data.
%% Problem statement
% The problem we tried to solve with transfer learning is to distinguish
% between 5 categories of food - cupcakes, burgers, apple pie, hot dogs and
% ice cream. To get started you need two things:
%
% # Training images of the different object classes
% # A pre-trained deep neural network (AlexNet)
% You can substitute these categories for any of your own based on what
% image data you have avaliable.
%% Load Training Images
% In order for imageDataStore to parse the folder names as category labels,
% you would have to store image categories in corresponding sub-folders.
rootFolder = fullfile('C:\Users\lzp\AppData\Local\Temp\caltech101\101_ObjectCategories');
categories = {'ASKsegement', 'FSKsegement', 'PSKsegement'};
%%
% Create an |ImageDatastore| to help you manage the data. Because
% |ImageDatastore| operates on image file locations, images are not loaded
% into memory until read, making it efficient for use with large image
% collections.
allImages = imageDatastore(fullfile(rootFolder, categories), 'LabelSource', 'foldernames');
%% Split data into training and test sets
[trainingImages, testImages] = splitEachLabel(allImages, 0.8, 'randomize');
%% Load Pre-trained Network (AlexNet)
% AlexNet is a pre-trained network trained on 1000 object categories.
% AlexNet is avaliable as a support package on FileExchange.
cnnMatFile =fullfile('C:\Users\lzp\AppData\Local\Temp','imagenet-caffe-alex25.mat');
convnet=helperImportMatConvNet(cnnMatFile);
%% Review Network Architecture
convnet.Layers
%% Modify Pre-trained Network
% AlexNet was trained to recognize 1000 classes, we need to modify it to
% recognize just 5 classes.
layers = convnet.Layers(1:end-3);
layers(23) = fullyConnectedLayer(3); % change this based on # of classes
layers(24) = softmaxLayer();
layers(25) = classificationLayer;
%% Perform Transfer Learning
% For transfer learning we want to change the weights of the network ever so slightly. How
% much a network is changed during training is controlled by the learning
% rates.
opts = trainingOptions('sgdm', 'InitialLearnRate', 0.001,...
'MaxEpochs', 20, 'MiniBatchSize', 64);
%% Set custom read function
% One of the great things about imageDataStore it lets you specify a
% "custom" read function, in this case it is simply resizing the input
% images to 227x227 pixels which is what AlexNet expects. You can do this by
% specifying a function handle of a function with code to read and
% pre-process the image.
trainingImages.ReadFcn = @readFunctionTrain;
trainingLabels = categorical(trainingImages.Labels);
% trainingFiles = cell(trainingSet.Files);
trainingImages = readall(trainingImages);
trainingImages = cat(4, trainingImages{:});
% trainingfiles =imageDatastore(trainingImages );
%% Train the Network
% This process usually takes about 5-20 minutes on a desktop GPU.
myNet = trainNetwork(trainingImages,trainingLabels,layers, opts);
%% Test Network Performance
% Now let's the test the performance of our new "snack recognizer" on the test set.
testImages.ReadFcn = @readFunctionTrain;
predictedLabels = classify(myNet, testImages);
accuracy = mean(predictedLabels == testImages.Labels)
相关文章推荐
- 利用Matlab自带的深度学习工具进行车辆区域检测与车型识别【福利-内附源码与数据库】(一)
- 利用Matlab自带的深度学习工具进行车辆区域检测与车型识别【Github更新!!!】(三)
- 利用Matlab自带的深度学习工具进行车辆区域检测与车型识别【福利-内附源码与数据库】(二)
- 利用Matlab自带的深度学习工具进行车辆区域检测与车型识别【福利-内附源码与数据库】(一)
- 深度学习与人脸识别系列(5)__利用训练好的vgg模型进行人脸识别(利用摄像头)
- 深度学习与人脸识别系列(6)__利用训练好的vgg模型进行人脸识别(利用摄像头)
- Matlab图像识别/检索系列(7)-10行代码完成深度学习网络之取中间层数据作为特征
- Matlab图像识别/检索系列(11)—开源介绍之深度学习工具MatConvNet toolbox
- Matlab图像识别/检索系列(5)—10行代码完成深度学习网络之CNN/Autoencoder
- Matlab图像识别/检索系列(6)-10行代码完成深度学习网络之基于CNN的图像分类
- CNTK API文档翻译(24)——使用深度迁移学习进行图像识别
- 深度学习-CAFFE利用CIFAR10网络模型训练自己的图像数据获得模型-4应用生成模型进行预测
- Matlab图像识别/检索系列(6)-10行代码完成深度学习网络之基于CNN的图像分类
- 16 利用scipy.misc等库对jpg以及png等图像数据预处理(用于深度学习喂数据)
- 随时更新———个人喜欢的关于模式识别、机器学习、推荐系统、图像特征、深度学习、数值计算、目标跟踪等方面个人主页及博客
- Matlab图像处理学习笔记(三):基于匹配的目标识别
- 个人喜欢的关于模式识别、机器学习、推荐系统、图像特征、深度学习、数值计算、目标跟踪等方面个人主页及博客
- caffe学习(二):利用mnist数据集训练并进行手写数字识别(windows)
- kinect 学习笔记二(深度图像的利用--抠取用户躯体)
- DCMTK学习之利用RGB进行图像的输出时,图像反向的问题