1. 程式人生 > >matlab 關於利用深度學習進行影象識別

matlab 關於利用深度學習進行影象識別

深度學習進行影象識別現在主要 是利用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)