惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Schneier on Security
A
Arctic Wolf
S
Security Affairs
O
OpenAI News
SecWiki News
SecWiki News
TaoSecurity Blog
TaoSecurity Blog
H
Heimdal Security Blog
T
Threat Research - Cisco Blogs
Hacker News: Ask HN
Hacker News: Ask HN
N
News | PayPal Newsroom
Google Online Security Blog
Google Online Security Blog
C
Cisco Blogs
The Hacker News
The Hacker News
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
C
CXSECURITY Database RSS Feed - CXSecurity.com
P
Privacy International News Feed
V
Vulnerabilities – Threatpost
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
T
Tenable Blog
T
The Exploit Database - CXSecurity.com
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Spread Privacy
Spread Privacy
人人都是产品经理
人人都是产品经理
www.infosecurity-magazine.com
www.infosecurity-magazine.com
V2EX - 技术
V2EX - 技术
L
LINUX DO - 最新话题
The GitHub Blog
The GitHub Blog
博客园 - 三生石上(FineUI控件)
T
The Blog of Author Tim Ferriss
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
Visual Studio Blog
The Cloudflare Blog
N
News and Events Feed by Topic
量子位
Google DeepMind News
Google DeepMind News
Application and Cybersecurity Blog
Application and Cybersecurity Blog
L
LINUX DO - 热门话题
P
Palo Alto Networks Blog
Stack Overflow Blog
Stack Overflow Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Attack and Defense Labs
Attack and Defense Labs
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Hacker News - Newest:
Hacker News - Newest: "LLM"
Apple Machine Learning Research
Apple Machine Learning Research
The Register - Security
The Register - Security
Microsoft Security Blog
Microsoft Security Blog
Know Your Adversary
Know Your Adversary
Webroot Blog
Webroot Blog

RealCat

📝笔记:图像匹配挑战赛回顾(CVPR 2023) | RealCat 📝笔记:Stable Diffusion QR-Code | RealCat 📝笔记:Python zip() | RealCat 📝笔记:图像匹配挑战赛回顾(CVPR 2022) | RealCat 📝笔记:5秒钟训练NeRF,NVIDIA Instant NeRF 测试 | RealCat 📝笔记:Visualization Localization Revisited(under construction...) | RealCat 📝笔记:使用vlfeat的Matlab接口简单实现BOW以及VLAD | RealCat 📝笔记:一些关于KD-Tree的知识点 | RealCat 📝笔记:简明矩阵求导术之分子布局与分母布局 | RealCat 📝笔记:使用Clockwise/Spiral Rule技巧轻松读懂变量/函数声明 | RealCat 🔨工具:优雅地下载Youtube视频 | RealCat 🔨工具:每日自动获取arXiv论文摘要 | RealCat 🎃资料: 从Eigen向量化谈内存对齐 | RealCat 📝笔记:图像匹配挑战赛总结 (SuperPoint + SuperGlue 缝缝补补还能再战一年) | RealCat 📝笔记:ICCV 2021最佳学生论文 | COLMAP 优化建图组件 Pixel-Perfect SFM | RealCat 📝笔记:CVPR 2021 | PixLoc: 端到端场景无关视觉定位算法(SuperGlue一作出品) | RealCat 📝笔记:港大MARS实验室 R3LIVE (R2LIVE升级) 鲁棒实时RGB雷达视觉惯导紧耦合状态估计 | RealCat 🔨工具:bash常用命令 | RealCat 📝笔记:VSLAM基础知识导图 | RealCat 📝笔记:Patch-NetVLAD论文阅读 | RealCat 📝笔记:光场相机能否用于SLAM? | RealCat 📝笔记:读写文本常用操作 | RealCat 📝笔记:CVPR 2020 视觉定位挑战赛冠军方案 | RealCat 📝笔记:三维重建系列 COLMAP: Structure-from-Motion Revisited | RealCat 🐈芒果驾到 | RealCat 📝笔记:GMS一种基于运动统计的快速鲁棒特征匹配过滤算法 | RealCat 🌡️秋天到了,还是很热 | RealCat 📝笔记:AdaLAM: Revisiting Handcrafted Outlier Detection 超强外点滤除算法 | RealCat 🔨工具:使用vercel加速Hexo静态博客访问 | RealCat 📝笔记:ORB-SLAM3论文阅读 | RealCat 🔨工具:解决Github挂图及龟速访问 | RealCat 📝笔记:图解卡尔曼滤波 | RealCat 🔨工具:国内加速访问arxiv | RealCat 📝笔记:CVPR2020图像匹配挑战赛,新数据集+新评测方法,SOTA正瑟瑟发抖! | RealCat 📝笔记:SuperGlue:Learning Feature Matching with Graph Neural Networks论文阅读 | RealCat 📝笔记:SLAM常见问题(四):求解ICP,利用SVD分解得到旋转矩阵 | RealCat 📝笔记:SLAM常见问题(五):Singular Value Decomposition(SVD)分解 | RealCat 📝笔记:SLAM常见问题(三):PNP | RealCat 📝笔记:SLAM常见问题(二):重定位Relocalisation | RealCat 📝笔记:SLAM常见问题(一):SearchByBoW | RealCat 📝笔记:2019年浙大CADCG暑假SLAM培训部分课件 | RealCat 🔨工具:Filebrowser:一款轻量级个人网盘 | RealCat 📝笔记:SuperPoint: Self-Supervised Interest Point Detection and Description 自监督深度学习特征点 | RealCat Black Hole | RealCat 🔥Awesome CV Works | RealCat 🔨工具:开启SSR模式 | RealCat 虚实:「未麻的部屋」 | RealCat 笔记:李群与李代数求导 | RealCat 资料:ORB SLAM2 阅读报告 | RealCat 资料:SLAM草稿 | RealCat 资料:Line Segments Detection | RealCat 资料:Eigen与Matlab语句之对应关系 | RealCat 笔记:清华-谷歌人工智能研讨会(Tsinghua-Google AI Symposium) | RealCat Think Different | RealCat Light Field Depth Estimation | RealCat CV Related References | RealCat 立体视觉综述:Stereo Vision Overview | RealCat Lytro的光场AR之路:从巅峰到死亡 | RealCat Stephen Hawking | RealCat 常用的生产力工具 | RealCat 深度学习在深度(视差)估计中的应用(2) | RealCat 深度学习在深度(视差)估计中的应用(1) | RealCat CNN框架(CNN Architectures) | RealCat 理解LSTM网络【译】 | RealCat 降维之PCA主成分分析原理 | RealCat 📝笔记:SIFT和SURF特性提取总结 | RealCat 统计学习方法总结 | RealCat Ubuntu上使用Git以及GitHub | RealCat 机器学习修炼手册 | RealCat 初试HCI光场数据集 | RealCat 实习季到了,大家又浮躁了起来 | RealCat 日本与美国之行 | RealCat Light Field 光场以及MATLAB光场工具包(LightField ToolBox)的使用说明 | RealCat MATLAB:多个不同维度的箱线图画在一起 | RealCat AR形势与应用 | RealCat Hexo+Github+jsDelivr+Vercel建站备忘录 | RealCat Markdown 学习 | RealCat 复试那些事儿 | RealCat
Matlab Deep Learning学习笔记 | RealCat
2017-11-18 · via RealCat

最近对深度学习尤其着迷,是时候用万能的Matlab去践行我的DL学习之路了。之所以用Matlab,是因为Matlab真的太强大了!自从大学开始我就一直用这个神奇的软件,算是最熟悉的编程工具。加上最近mathworks公司一大波大佬的不懈努力,在今年下半年发行的R2017b版本中又加入了诸多新颖的特性,尤其在DL方面,可以发现:仅仅几条简单的代码,就能够实现复杂的功能。基于以上,我在本文列举了几个在Matlab上学习Deep Learning的例子:1. 手写字符识别;2. 搭建网络对CIFAR10分类;3.搭建一个Resnet。务必保证主机已经安装Matlab 2017a及以上。

手写字符识别

利用CNN做数字分类实验。

接下来的实验会阐明如何进行: - 加载图像数据 - 设计网络结构 - 设置网络训练参数 - 训练网络 - 预测新数据的类别

加载图像数据

digitDatasetPath = fullfile(matlabroot,'toolbox','nnet','nndemos',...
    'nndatasets','DigitDataset');
% imageDatastore函数 能够通过文件夹名自动地把数据存储成ImageDatastore 对象
digitData = imageDatastore(digitDatasetPath,...
    'IncludeSubfolders',true,'LabelSource','foldernames');

% Display some of the images in the datastore.
figure;
perm = randperm(10000,25);
for i = 1:25
    subplot(5,5,i);
    imshow(digitData.Files{perm(i)});
end

以下是手写字符的部分数据:

创建训练集与验证集

trainNumFiles = 750;
[trainDigitData,valDigitData] = splitEachLabel(digitData,750,'randomize');
% 每类有1000个,选择其中的750类作为训练集,剩下的作为验证集;此处750可以换成一个比例:75%

注意Matlab里面支持的的类型,包括:CLICK THIS LINK。如下所示:

Epoch

Iteration

Layer Type

Function

Image input layer

imageInputLayer

Sequence input layer

sequenceInputLayer

2-D convolutional layer

convolution2dLayer

2-D transposed convolutional layer

transposedConv2dLayer

Fully connected layer

fullyConnectedLayer

Long short-term memory (LSTM) layer

LSTMLayer

Rectified linear unit (ReLU) layer

reluLayer

Leaky rectified linear unit (ReLU) layer

leakyReluLayer

Clipped rectified linear unit (ReLU) layer

clippedReluLayer

Batch normalization layer

batchNormalizationLayer

Channel-wise local response normalization (LRN) layer

crossChannelNormalizationLayer

Dropout layer

dropoutLayer

Addition layer

additionLayer

Depth concatenation layer

depthConcatenationLayer

Average pooling layer

averagePooling2dLayer

Max pooling layer

maxPooling2dLayer

Max unpooling layer

maxUnpooling2dLayer

Softmax layer

softmaxLayer

Classification layer

classificationLayer

Regression layer

regressionLayer

创建自己的网络结构

%% Define Network Architecture
% Define the convolutional neural network architecture.
layers =
    [
    imageInputLayer([28 28 1])
    convolution2dLayer(3,16,'Padding',1)
    batchNormalizationLayer()
    reluLayer()
    maxPooling2dLayer(2,'Stride',2)

    convolution2dLayer(3,32,'Padding',1)
    batchNormalizationLayer()
    reluLayer()

    maxPooling2dLayer(2,'Stride',2)

    convolution2dLayer(3,64,'Padding',1)
    batchNormalizationLayer()
    reluLayer()

    fullyConnectedLayer(10)
    softmaxLayer()
    classificationLayer()
    ];

以下就是该网络结构及参数设置:

Image Input             28x28x1 images with 'zerocenter' normalization
Convolution             16 3x3 convolutions with stride [1  1] and padding [1  1  1  1]
Batch Normalization     Batch normalization
ReLU                    ReLU
Max Pooling             2x2 max pooling with stride [2  2] and padding [0  0  0  0]
Convolution             32 3x3 convolutions with stride [1  1] and padding [1  1  1  1]
Batch Normalization     Batch normalization
ReLU                    ReLU
Max Pooling             2x2 max pooling with stride [2  2] and padding [0  0  0  0]
Convolution             64 3x3 convolutions with stride [1  1] and padding [1  1  1  1]
Batch Normalization     Batch normalization
ReLU                    ReLU
Fully Connected         10 fully connected layer
Softmax                 softmax
Classification Output   crossentropyex

网络训练参数设计

 options = trainingOptions('sgdm',...
'MaxEpochs',3, ...                 % 训练最大轮回
'ValidationData',valDigitData,...  % 验证集
'ValidationFrequency',30,...
'Verbose',false,...
'Plots','training-progress');

开始训练

net = trainNetwork(trainDigitData,layers,options);

测试新的数据

predictedLabels = classify(net,valDigitData);
valLabels = valDigitData.Labels;
accuracy = sum(predictedLabels == valLabels)/numel(valLabels)

查看某层参数

例如查看第2层的weight参数,输入以下命令:

montage(imresize(mat2gray(net.Layers(2).Weights),[128 128]));
set(gcf,'color',[1 1 1]);
frame=getframe(gcf); % get the frame
image=frame.cdata;
[image,map]     =  rgb2ind(image,256);
imwrite(image,map,'weight-layer2.png');

图像如下所示:

再看一下第10层的参数:

[~,~,iter,~]=size(net.Layers(10).Weights);
name='weight.gif';
dt=0.4;
for i=1:iter
	montage(imresize(mat2gray(net.Layers(10).Weights(:,:,i,:)),[128 128]));
    set(gcf,'color',[1 1 1]); %变白
	title(['Layer(10), Channel: ',num2str(i)]);
	axis normal
	truesize
	%Creat GIF
	frame(i)=getframe(gcf); % get the frame
	image=frame(i).cdata;
	[image,map]     =  rgb2ind(image,256);
	if i==1
		 imwrite(image,map,name,'gif');
	else
		 imwrite(image,map,name,'gif','WriteMode','append','DelayTime',dt);
    end
end

搭建网络对CIFAR10分类

CIFAR10和CIFAR100是80 million tiny images的子集,是由Geoffrey Hinton的弟子们Alex Krizhevsky和Vinod Nair共同采集。

CIFAR10

CIFAR10由60000张32*32的彩色图像组成,一种分成10类,平均每类图像6000张。共有50000张训练图像,10000张测试图像。这个数据集被分成了5个分支,其中每个分支10000张。测试集包含每类中随机选择的1000张图像。训练集就是剩下的那些图像。

对于每个分支的数据的大小是:10000*3072;其中3072=32*32*3。数据以行优先的顺序存储,所以前1024个数据是r通道的数据,接下来的1024个数据是g通道的数据,最后1024个数据是b通道的。 假如原始的数据是data,我们想要将其重新排列成我们需要的数据。首先对其进行转置,然后再用reshape函数对图像重组(可选:最后将图像前两维互换(转置),之所以这么做,可以更好的可视化)。

XBatch = data';
XBatch = reshape(XBatch, 32,32,3,[]);
XBatch = permute(XBatch, [2 1 3 4]);

以下是cifar10的部分数据。 共有10类,包括:airplane,automobile,bird,cat,deer,dog,frog,horse,ship,truck。

Just run it

接下来我们就开始运行以下代码,来训练我们的网络。闲话少说,我把代码放在了Github,欢迎

1   'imageinput'    Image Input         	28x28x1 images with 'zerocenter' normalization
2   'conv_1'        Convolution         	16 3x3x1 convolutions with stride [1  1] and padding [1  1  1  1]
3   'batchnorm_1'   Batch Normalization 	Batch normalization with 16 channels
4   'relu_1'        ReLU               		ReLU
5   'maxpool_1'     Max Pooling         	2x2 max pooling with stride [2  2] and padding [0 0  0  0]
6   'conv_2'        Convolution         	32 3x3x16 convolutions with stride [1  1] and padding [1  1  1  1]
7   'batchnorm_2'   Batch Normalization 	Batch normalization with 32 channels
8   'relu_2'        ReLU                	ReLU
9   'maxpool_2'     Max Pooling         	2x2 max pooling with stride [2  2] and padding [0  0  0 0]
10   'conv_3'       Convolution         	64 3x3x32 convolutions with stride [1  1] and padding [1 1  1  1]
11   'batchnorm_3'  Batch Normalization 	Batch normalization with 64 channels
12   'relu_3'       ReLU			ReLU
13   'fc'           Fully Connected		10 fully connected layer
14   'softmax'      Softmax			softmax
15   'classoutput'  Classification Output	crossentropyex with '0', '1', and 8 other classes

以下是训练过程输出:

Epoch

Iteration

Time Elapsed (seconds)

Mini-batch Loss

Mini-batch Accuracy

Base Learning Rate

1

1

0.06

2.3026

8.59%

0.0020

1

50

1.27

2.3026

14.06%

0.0020

1

100

2.52

2.3024

7.81%

0.0020

1

150

3.73

2.2999

20.31%

0.0020

1

200

5.01

2.2740

15.63%

0.0020

1

250

6.28

2.1194

21.09%

0.0020

1

300

7.58

1.9100

23.44%

0.0020

1

350

8.86

1.8892

28.13%

0.0020

2

400

10.08

1.7490

29.69%

0.0020

2

450

11.32

1.8377

31.25%

0.0020

2

500

12.57

1.6073

39.84%

0.0020

20

7650

407.74

0.2858

93.75%

2.00e-05

20

7700

409.06

0.3127

89.84%

2.00e-05

20

7750

410.38

0.3254

87.50%

2.00e-05

20

7800

411.64

0.2456

92.19%

2.00e-05

最后测试我们的模型的性能,accuracy=76%左右。但是训练时,我们的batch-accuracy已经达到了90%以上,说明我们的模型过拟合了。显然这不是我们想要的结果,进一步的调参将会在此补充。

可视化某层的参数

% Extract the first convolutional layer weights
w = cifar10Net.Layers(2).Weights;

% rescale and resize the weights for better visualization
w = mat2gray(w);
w = imresize(w, [100 100]);

figure
montage(w)
name='cifar10-weight-layer2';
set(gcf,'color',[1 1 1]);
frame=getframe(gcf); % get the frame
image=frame.cdata;
[image,map]     =  rgb2ind(image,256);
imwrite(image,map,[name,'.png']);

搭建一个Resnet

接下来,为了验证下这个DL工具包的强大之处,我打算纯手工建一个Resnet。为方便起见,我搭了一个Resnet34(更深的网络敬请期待吧)。这里是它的prototxt,我们可以用网络可视化工具进行查看resnet34的结构。以下是Resnet34的一部分(太长了没有截下全部视图)。

定义每一层与连接层

以从pool1到res2a为例子建立网络。

layers_example=[
    % pool1 - res2a
    maxPooling2dLayer(3, 'Stride', 2,'Name','pool1');
    %  branch2a
    convolution2dLayer(3,64,'Stride', 1,'Padding', 1,'Name','res2a_branch2a')
    batchNormalizationLayer('Name','bn2a_branch2a')
    reluLayer('Name','res2a_branch2a_relu')

    %  branch2b
    convolution2dLayer(3,64,'Stride', 1,'Padding', 1,'Name','res2a_branch2b')
    batchNormalizationLayer('Name','bn2a_branch2b')

    % add together
    additionLayer(2,'Name','res2a')
    reluLayer('Name','res2a_relu')
];

上述过程仅仅完成了网络的一个小分支,记下来要完成res2a_branch1这部分的连接。这时候要用到DAG一些方法。通过添加新层同时建立新的连接即可,方式如下。

lgraph = layerGraph(layers_example);
figure
plot(lgraph)

%% add some connections (shortcut)
layers_2a=[
    convolution2dLayer(1,64,'Stride', 1,'Padding', 0,'Name','res2a_branch1')
    batchNormalizationLayer('Name','bn2a_branch1')
];
lgraph = addLayers(lgraph,layers_2a);
lgraph = connectLayers(lgraph,'pool1','res2a_branch1');
lgraph = connectLayers(lgraph,'bn2a_branch1','res2a/in2');
% show net
plot(lgraph)

其他部分的构建同上,经过一系列重复的工作,我们可以构建出这个不太深的Resnet34,全部代码见我的Github

一些基本问题

  • 参数的基本格式
  • SGD是什么? 可以参见好友写的一篇博文
  • 什么是epoch? 模型训练的时候一般采用stochastic gradient descent(SGD),一次迭代选取一个batch进行update。一个epoch的意思就是迭代次数*batch的数目 和训练数据的个数一样,就是一个epoch。
  • 为什么要是用BN? Batch normalization layers normalize the activations and gradients propagating through a network, making network training an easier optimization problem. Use batch normalization layers between convolutional layers and nonlinearities, such as ReLU layers, to speed up network training and reduce the sensitivity to network initialization.
  • RELU的作用? Max-Pooling Layer Convolutional layers (with activation functions) are sometimes followed by a down-sampling operation that reduces the spatial size of the feature map and removes redundant spatial information. Down-sampling makes it possible to increase the number of filters in deeper convolutional layers without increasing the required amount of computation per layer. One way of down-sampling is using a max pooling. The max pooling layer returns the maximum values of rectangular regions of inputs.
  • Resnet中scale层是如何定义的?有什么用途?
  • Resnet中为何残差好学?
  • add more