1. 程式人生 > >深度學習 2. MatConvNet(CNN)的配置和相關實驗結果,CNN學習使用(本人project作業)

深度學習 2. MatConvNet(CNN)的配置和相關實驗結果,CNN學習使用(本人project作業)

作者( [email protected] )資訊。

(如有需要可以郵件聯絡我)

上學期神經網最後的project有關CNN的部分我們學習使用了MatConvNet。

從名字我們就知道,Matlab Convolution Neural Network。所以就不做過多介紹了。

正文:

MatConvNet 本身是用Matlab去編譯執行C++檔案,所以我們需要搭建相關連線。

這裡我用到的是Matlab2016a和VS2015兩個平臺去實現的。這裡我極力推薦VS2015版本和Matlab2013以後的版本

因為我當時嘗試了VS2017不可以,VS2010可以但是必須要按照網上的相關步驟去一步一步的按照順序的安裝SDK,相當的麻煩。所以我強烈推薦大家用VS2015.因為只需要在安裝的時候選上開發者工具包並一起安裝就可以很簡單的進行二者的搭建。

配置方法:

1. 新增MatConvNet內的相關資料夾至Matlab路徑中。

2. 輸入:mex -setup cpp 等待系統配置。

3. 配置成功之後,輸入:vl_compilenn進行搭建相關檔案。

如下圖:


第二步,我們就可以利用MatConvNet自帶的資料集進行訓練了,不過我們當時並沒有使用自帶的cnn_mnist_init.m檔案來做訓練,我的請看下面程式碼。

下面是我的CNN的程式設計:

function cnn_mnist_NNclass(varargin)

warning off

% CNN_MNIST  Demonstrated MatConNet on MNIST

% run( fullfile(fileparts(mfilename('fullpath')), '../matlab/vl_setupnn.m') ) ;
run('C:\Users\matconvnet-1.0-beta23\matconvnet-1.0-beta23\matlab/vl_setupnn.m') ;

opts.dataDir = 'data/mnist' ;
opts.expDir = 'data/mnist-baseline' ;
opts.imdbPath = fullfile(opts.expDir, 'imdb.mat');
opts.train.batchSize = 100 ;
opts.train.numEpochs = 100 ;
opts.train.continue = true ;
% opts.train.useGpu = [] ;
opts.train.gpus = [];
opts.train.learningRate = 0.001 ;
opts.train.expDir = opts.expDir ;
opts = vl_argparse(opts, varargin);
opts.train.subsetSize = 1e4;    % statsogk

% --------------------------------------------------------------------
%                                                         Prepare data
% --------------------------------------------------------------------

if exist(opts.imdbPath)
  imdb = load(opts.imdbPath) ;
else
  imdb = getMnistImdb(opts) ;
  mkdir(opts.expDir) ;
 
  save(opts.imdbPath, '-struct', 'imdb') ;
end

% Use a subset of the images for faster training. 
if opts.train.subsetSize > 0
    imdb = getSubset(imdb,opts);
end

% Define a network similar to LeNet
f=1/100 ;
net.layers = {} ;
net.layers{end+1} = struct('type', 'conv', ...
                           'filters', f*randn(5,5,1,20, 'single'), ...
                           'biases', zeros(1, 20, 'single'), ...
                           'stride', 1, ...
                           'pad', 0) ;
% net.layers{end+1} = struct('type', 'relu') ;
net.layers{end+1} = struct('type', 'pool', ...
                           'method', 'max', ...
                           'pool', [2 2], ...
                           'stride', 2, ...
                           'pad', 0) ;
net.layers{end+1} = struct('type', 'conv', ...
                           'filters', f*randn(5,5,20,50, 'single'),...
                           'biases', zeros(1,50,'single'), ...
                           'stride', 1, ...
                           'pad', 0) ;
% net.layers{end+1} = struct('type', 'relu') ;
net.layers{end+1} = struct('type', 'pool', ...
                           'method', 'max', ...
                           'pool', [2 2], ...
                           'stride', 2, ...
                           'pad', 0) ;
net.layers{end+1} = struct('type', 'conv', ...
                           'filters', f*randn(4,4,50,500, 'single'),...
                           'biases', zeros(1,500,'single'), ...
                           'stride', 1, ...
                           'pad', 0) ;
net.layers{end+1} = struct('type', 'relu') ;

net.layers{end+1} = struct('type', 'conv', ...
                           'filters', f*randn(1,1,500,10, 'single'),...
                           'biases', zeros(1,10,'single'), ...
                           'stride', 1, ...
                           'pad', 0) ;
net.layers{end+1} = struct('type', 'softmaxloss') ;

% --------------------------------------------------------------------
%                                                                Train
% --------------------------------------------------------------------

% Take the mean out and make GPU if needed
imdb.images.data = bsxfun(@minus, imdb.images.data, mean(imdb.images.data,4)) ;
% if opts.train.useGpu
if opts.train.gpus
    imdb.images.data = gpuArray(imdb.images.data) ;
end

[ net, info ] = cnn_train(net, imdb, @getBatch, opts.train, 'val', find(imdb.images.set == 3)) ;

% --------------------------------------------------------------------
function [im, labels] = getBatch(imdb, batch)
% --------------------------------------------------------------------
im = imdb.images.data(:,:,:,batch) ;
labels = imdb.images.labels(1,batch) ;

% --------------------------------------------------------------------
function imdb = getMnistImdb(opts)
% --------------------------------------------------------------------
files = {'train-images-idx3-ubyte', ...
         'train-labels-idx1-ubyte', ...
         't10k-images-idx3-ubyte', ...
         't10k-labels-idx1-ubyte'} ;

mkdir(opts.dataDir) ;
for i=1:4
  if ~exist(fullfile(opts.dataDir, files{i}), 'file')
    url = sprintf('http://yann.lecun.com/exdb/mnist/%s.gz',files{i}) ;
    fprintf('downloading %s\n', url) ;
    gunzip(url, opts.dataDir) ;
  end
end

f=fopen(fullfile(opts.dataDir, 'train-images-idx3-ubyte'),'r') ;
x1=fread(f,inf,'uint8');
fclose(f) ;
x1=permute(reshape(x1(17:end),28,28,60e3),[2 1 3]) ;

f=fopen(fullfile(opts.dataDir, 't10k-images-idx3-ubyte'),'r') ;
x2=fread(f,inf,'uint8');
fclose(f) ;
x2=permute(reshape(x2(17:end),28,28,10e3),[2 1 3]) ;

f=fopen(fullfile(opts.dataDir, 'train-labels-idx1-ubyte'),'r') ;
y1=fread(f,inf,'uint8');
fclose(f) ;
y1=double(y1(9:end)')+1 ;

f=fopen(fullfile(opts.dataDir, 't10k-labels-idx1-ubyte'),'r') ;
y2=fread(f,inf,'uint8');
fclose(f) ;
y2=double(y2(9:end)')+1 ;

imdb.images.data = single(reshape(cat(3, x1, x2),28,28,1,[])) ;
imdb.images.labels = cat(2, y1, y2) ;
imdb.images.set = [ones(1,numel(y1)) 3*ones(1,numel(y2))] ;
imdb.meta.sets = {'train', 'val', 'test'} ;
imdb.meta.classes = arrayfun(@(x)sprintf('%d',x),0:9,'uniformoutput',false) ;

% ------------------------------------------------------------------------------
function imdb = getSubset(imdb,opts)
% ------------------------------------------------------------------------------
assert(opts.train.subsetSize <= nnz(imdb.images.set == 1),...
        'Subset size is bigger than the total train set size')
inds = find(imdb.images.set == 1);   % indices  must be from the train set
inds = randsample(inds, length(inds)-opts.train.subsetSize );
imdb.images.labels(inds) = [];
imdb.images.set(inds) = [];
imdb.images.data(:,:,:,inds) = [];

下面是我的執行結果一共為四個(我一共構造了3個網路外加原始網路一共是4個):

1. 原始CNN(LeNets)的訓練結果:



2.減去兩層(layer)之後的訓練結果:



3.增加兩層之後的訓練結果:



4.在原始網路中新增Dropout layer之後的訓練結果:



實驗結果討論:(在我的project裡面我已經寫了相關的討論(英文),大家可以百度翻譯一下看看,我這裡就不再寫了,直接複製貼上了。)

1. Thediscussion about the Top1 err and Top5 err.

First,you make a prediction using the CNN and obtain the predicted class multinomialdistribution (∑pclass=1∑pclass=1).

Now, inthe case of top-1 score, you check if the top class (the one having the highestprobability) is the same as the target label.

In thecase of top-5 score, you check if the target label is one of your top 5predictions (the 5 ones with the highest probabilities).

In bothcases, the top score is computed as the times a predicted label matched thetarget label, divided by the number of data-points evaluated.

Finally,when 5-CNNs are used, you first average their predictions and follow the sameprocedure for calculating the top-1 and top-5 scores.

2. Thediscussion about the differences between reduced and increased 

Original LeNets

Reduced

Increased

Dropout

Object

0.082

0.071

0.062

0.066

Top1 err

0.021

0.019

0.018

0.018

Top5 err

0.000

0.000

0.000

0.000

Generallyif we use more layers, it becomes more complex and more prone to overfitting.To avoid that kind of problem when increasing the layers, I add relu layers.And the result shows that it performs than the original one. But if I addlayers consisting of additional conv-pool , then it would cause overfitting anderror will go up.

Alsowhen decreasing two layers , I got less error than the original one because itreduces the complexity. More complexity means more extract details from thefeature. Through erasing some layers, network becomes nice circumstance andperforms better than the original one.

3. Thediscussion about Dropout Layer.

A dropout layerrandomly sets a layer's input elements to zero with a given probability.

This corresponds to temporarily droppinga randomly chosen unit and all of its connections from the network duringtraining. So, for each new input element, the software randomly selects asubset of neurons, hence forms a different layer architecture. Thesearchitectures use common weights, but because the learning does not depend onspecific neurons and connections, the dropout layer might help preventoverfitting

4. DROPOUT FOR REGULARIZATION

Hinton,Srivastava, Krizhevsky, Sutskever, & Salakhutdinov (2012) introduced thedropout regularization algorithm. Although dropout works in a different waythan L1 and L2, it accomplishes the same goal—the prevention of overfitting.However, the algorithm goes about the task by actually removing neurons and connections—atleast temporarily. Unlike L1 and L2, no weight penalty is added. Dropout doesnot directly seek to train small weights. Most neural network frameworksimplement dropout as a separate layer. Dropout layers function as a regular,densely connected neural network layer. The only difference is that the dropoutlayers will periodically drop some of their neurons during training. You canuse dropout layers on regular feedforward neural networks. Figure 6 showsdropout in action.

作者( [email protected] )資訊。


相關推薦

深度學習 2. MatConvNetCNN配置相關實驗結果,CNN學習使用本人project作業

作者( [email protected] )資訊。 (如有需要可以郵件聯絡我) 上學期神經網最後的project有關CNN的部分我們學習使用了MatConvNet。 從名字我們就知道,Matlab Convolution Neural Network。所以

Linux學習之路:第二章配置網路IP,實現遠端連線

備註:屬於個人分享,文章如有問題請留言,謝謝! 第二章配置網路IP,實現遠端連線 1、輸入使用者和密碼 輸入密碼的時候是不會顯示的 如何檢視Linux系統是32位還是64位,X86是32位,X86_64是64位                  命令: unam

機器學習篇:Python環境配置相關模組的安裝Python3.7 ,Numpy,Matplotlib

Python大環境搭建 記住這個路徑。(我們不需要自定義安裝,而且3.7版本是整合pip以及其他的東西,很方便),下一步點選Install Now Python環境變數配置 、 找到系統變數的Path 複製貼上安裝路徑,確定。 Python

mongoDB學習之路,安裝、配置、啟動、命令、應用

mongoDB初學 mongoDB學習了一段時間,今天整理一下,以便自己回顧,加深印象,同時讓更多mongo初學者有個好的資料。真好 在學習mongoDB之前,我們先了解什麼是mongoDB,以及相關概念 MongoDB 是一個基於分散式檔案儲存的資料庫。由 C++

mongoDB學習之路,安裝、配置、啟動、命令、應用-

上篇說了java連線mongo,並進行增刪改查 這篇說一下spring整合mongo github上也有小demo,很簡單,適合初學者,地址:點選跳轉 1、首先建立maven專案,新增依賴 <!-- mongo驅動 --> <dependen

配置檔案ehcache.xml詳解2配置相關

<diskStore>是用來配置ehcache的磁碟儲存的,磁碟儲存可以儲存記憶體中驅除過來的元素,也可以在系統重啟的時候將記憶體中的快取資訊儲存起來,供系統重新啟動後使用。 一、eh

spring-boot-starter-actuator健康監控配置使用

frame maven git 追蹤 包括 屬性 per dump zookeepe 在生產環境中,需要實時或定期監控服務的可用性。Spring Boot的actuator(健康監控)功能提供了很多監控所需的接口,可以對應用系統進行配置查看、相關功能統計等。 集成:

配置mac環境下的JAVA_HOME 與 配置maven Mac上jdk的配置 (四)在terminal中執行.class檔案

(一)mac環境下,echo $JAVA_HOME 一般輸出為空,但有時候某些構件會需要有javahome的配置,這時就需要把Java home配置好。 步驟: 1, 命令列輸入: /usr/libexec/java_home 我的環境輸出是 /Library/Java/JavaVi

Eclisests配置啟動、關閉tomcat服務

  (一) 第一步:首先啟動eclise(sts),然後配置tomcat,如下圖:            單擊“Window”選單,選擇下方的“Preferences”,

spring事務管理原始碼分析配置事務增強代理的生成流程

在本篇文章中,將會介紹如何在spring中進行事務管理,之後對其內部原理進行分析。主要涉及 @EnableTransactionManagement註解為我們做了什麼? 為什麼標註了@Transactional註解的方法就可以具有事務的特性,保持了資料的ACID特性?spring到底是如何具有這樣

深入淺出Mybatis原始碼系列---配置詳解之properties與environmentsmybatis原始碼篇

上篇文章《深入淺出Mybatis原始碼系列(二)---配置簡介(mybatis原始碼篇)》我們通過對mybatis原始碼的簡單分析,可看出,在mybatis配置檔案中,在configuration根節點下面,可配置properties、typeAliases、plugins、

spring-boot actuator監控配置使用

在生產環境中,需要實時或定期監控服務的可用性。spring-boot 的actuator(監控)功能提供了很多監控所需的介面。簡單的配置和使用如下: 1、引入依賴: <dependency> <groupId>org.springframe

【pandas】[2] DataFrame 基礎,建立DataFrame增刪改查基本操作1

作者:lianghc 地址:http://blog.csdn.net/zutsoft         DataFrame 是pandas最常用的資料結構,類似於資料庫中的表,不過DataFrame不僅僅限制於2維,可以建立多維資料表。DataFrame既有行索引,也有列

Oracle PUPPRODUCT_USER_PROFILE配置使用

--禁用HR使用者的DROP命令 [email protected]> insert into product_user_profile values('SQL*Plus', 'HR', 'DROP', NULL, NULL, 'DISABLED', NULL, NULL); 已建立 1 行

PyTorch學習7—儲存載入訓練結果

本篇部落格主要介紹如何在PyTorch中儲存和載入模型訓練的結果。 對訓練結果進行儲存,有兩種方式,一種是儲存整個網路,另一種是儲存訓練好的引數,相對而言,第二種方式具有更高的效率。 下面是示例程式碼: import torch from torch.autograd

【翻譯】Sklearn與TensorFlow機器學習實用指南 ——第12章 裝置伺服器上的分散式TensorFlow

在第 11 章,我們討論了幾種可以明顯加速訓練的技術:更好的權重初始化,批量標準化,複雜的優化器等等。 但是,即使採用了所有這些技術,在具有單個 CPU 的單臺機器上訓練大型神經網路可能需要幾天甚至幾周的時間。在本章中,我們將看到如何使用 TensorFlow 在多個裝置(C

hive對接hbase-配置使用例子

1.配置 配置hive-site.xml,除了增加hive-hbase-handler-xxx.jar之外,在需要進行一些mapreduce計算的時候計算節點還需要hbase的jar來訪問hbase,還要增加其它hbase訪問的jar。 <proper

在Android用ZXing.jar識別二維碼的精簡版簡化了配置代碼

好的 war per extend 輸入 進行 all lease ica ??????? 近期公司做了一款OTP令牌激活的產品,因為之前激活手機令牌須要輸入非常多

深入淺出Mybatis系列---配置詳解之properties與environmentsmybatis原始碼篇

上篇文章《深入淺出Mybatis系列(二)---配置簡介(mybatis原始碼篇)》我們通過對mybatis原始碼的簡單分析,可看

每篇半小時1天入門MongoDB——2.MongoDB環境變量配置Shell操作

.get same 修復 nss its keys 電腦 sts lis 上一篇:每篇半小時1天入門MongoDB——1.MongoDB介紹和安裝 配置環境變量 Win10系統為例 右鍵單擊“此電腦”—&md