您的位置:首页 > 编程语言 > MATLAB

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)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐