Matlab自带神经网络工具箱在2017b版本中才出现,而在2014年9月,VLFeat就推出了深度神经网络工具箱MatConvNet。该工具箱功能全面,演示程序多,不但给出了深度网络各层(包括卷积层、池化层、激活层、Softmax层)和设置损失、dropout、归一化的实现函数,通过这些函数的组合实现自己的深度网络,还给出了简单卷积网络和DAG网络的封装,详细内容可参看帮助文档。同时,官网还提供了预训练模型,并给出了它们在物体检测、人脸识别、语义分割和ImageNet ILSVRC分类的应用,其中ImageNet ILSVRC分类使用的模型包括ResNet、GoogleNet、VGG-VD、VGG-S/M/F、Caffe reference model和Alexnet,对学习和研究人员有很大参考价值。
除该工具箱给的演示程序外,牛津大学VGG也有相关演示程序,可进入Oxford Vision Geometry Group页面中的 practical,进入该页面后请下载大小为约800兆的文件,该文件中包含了编译过的脚本文件和数据,演示程序可直接运行。除了该演示外,Oxford VGG还提供了许多与图像检索、识别的项目,详细内容请查看software和projects,这些内容是该项目组多年成果,对相关领域研究有很大帮助。
下面简单介绍一个来自practical-CNN的示例exercise1.m。
%exercise1.m
setup ;
x = imread('peppers.png') ;
% 将图像数据转换为单精度
x = im2single(x) ;
% 显示输入图像
figure(1) ; clf ; imagesc(x) ;
% 初始化卷基层参数
w = randn(5,5,3,10,'single') ;
%对输入图像进行卷积运算
y = vl_nnconv(x, w, []) ;
figure(2) ; clf ; vl_imarraysc(y) ; colormap gray ;
% 对输入图像进行步长为16的卷积,达到下采样效果
y_ds = vl_nnconv(x, w, [], 'stride', 16) ;
figure(3) ; clf ; vl_imarraysc(y_ds) ; colormap gray ;
%对输入图像进行有填充的卷积
y_pad = vl_nnconv(x, w, [], 'pad', 4) ;
figure(4) ; clf ; vl_imarraysc(y_pad) ; colormap gray ;
% 手动设置卷积核系数
w = [0 1 0 ;
1 -4 1 ;
0 1 0 ] ;
w = single(repmat(w, [1, 1, 3])) ;
y_lap = vl_nnconv(x, w, []) ;
figure(5) ; clf ; colormap gray ;
subplot(1,2,1) ; imagesc(y_lap) ; title('filter output') ;
subplot(1,2,2) ; imagesc(-abs(y_lap)) ; title('- abs(filter output)') ;
w = single(repmat([1 0 -1], [1, 1, 3])) ;
w = cat(4, w, -w) ;
y = vl_nnconv(x, w, []) ;
%对卷积输出进行激活运算
z = vl_nnrelu(y) ;
figure(6) ; clf ; colormap gray ;
subplot(1,2,1) ; vl_imarraysc(y) ;
subplot(1,2,2) ; vl_imarraysc(z) ;
%对卷积输出进行池化运算
y = vl_nnpool(x, 15) ;
figure(7) ; clf ; imagesc(y) ;
rho = 5 ;
kappa = 0 ;
alpha = 1 ;
beta = 0.5 ;
%对卷积输出进行归一化运算
y_nrm = vl_nnnormalize(x, [rho kappa alpha beta]) ;
figure(8) ; clf ; imagesc(y_nrm) ;
下面是来自practical-category-recognition-cnn-2017a的示例exercise2.m。
%exercise2.m
setup ;
%加载预训练模型,需要先下载该模型文件
encoding = 'vggm128-conv4' ;
% 是否使用图像增强,即增加图像数量
augmentation = false ;
% 计算图像正例特征
encoder = loadEncoder(encoding) ;
pos.names = getImageSet('data/myImages', augmentation) ;
if numel(pos.names) == 0, error('Please add some images to data/myImages before running this exercise') ; end
pos.descriptors = encodeImage(encoder, pos.names, ['data/cache_' encoding]) ;
% 加入噪声图像
neg = load(sprintf('data/background_train_%s.mat',encoding)) ;
names = {pos.names{:}, neg.names{:}};
descriptors = [pos.descriptors, neg.descriptors] ;
labels = [ones(1,numel(pos.names)), - ones(1,numel(neg.names))] ;
clear pos neg ;
%加载测试图像
pos = load(sprintf('data/horse_val_%s.mat',encoding)) ;
neg = load(sprintf('data/background_val_%s.mat',encoding)) ;
testNames = {pos.names{:}, neg.names{:}};
testDescriptors = [pos.descriptors, neg.descriptors] ;
testLabels = [ones(1,numel(pos.names)), - ones(1,numel(neg.names))] ;
clear pos neg ;
fprintf('Number of training images: %d positive, %d negative\n', ...
sum(labels > 0), sum(labels < 0)) ;
fprintf('Number of testing images: %d positive, %d negative\n', ...
sum(testLabels > 0), sum(testLabels < 0)) ;
% 对特征进行L2归一化
descriptors = bsxfun(@times, descriptors, 1./sqrt(sum(descriptors.^2,1))) ;
testDescriptors = bsxfun(@times, testDescriptors, 1./sqrt(sum(testDescriptors.^2,1))) ;
%训练线性SVM模型
C = 10 ;
[w, bias] = trainLinearSVM(descriptors, labels, C) ;
% 计算训练数据得分
scores = w' * descriptors + bias ;
% 计算测试数据得分
testScores = w' * testDescriptors + bias ;
figure(3) ; clf ; set(3,'name','Ranked test images (subset)') ;
displayRankedImageList(testNames, testScores) ;
% 显示precision-recall曲线
figure(4) ; clf ; set(4,'name','Precision-recall on test data') ;
vl_pr(testLabels, testScores) ;
% 显示平均准确率
[drop,drop,info] = vl_pr(testLabels, testScores) ;
fprintf('Test AP: %.2f\n', info.auc) ;
[drop,perm] = sort(testScores,'descend') ;
fprintf('Correctly retrieved in the top 36: %d\n', sum(testLabels(perm(1:36)) > 0)) ;
工具箱的主要函数有:
Building blocks
- vl_nnbnorm Batch normalization.
- vl_nnbilinearsampler Bilinear Sampler.
- vl_nnconv Linear convolution by a filter.
- vl_nnconcat Concatenation.
- vl_nnconvt Convolution transpose.
- vl_nncrop Cropping.
- vl_nndropout Dropout.
- vl_nnloss Classification log-loss.
- vl_nnnoffset Norm-dependent offset.
- vl_nnnormalize Local Response Normalization (LRN).
- vl_nnpdist Pairwise distances.
- vl_nnpool Max and sum pooling.
- vl_nnrelu Rectified Linear Unit.
- vl_nnroipool Region of interest pooling.
- vl_nnsigmoid Sigmoid.
- vl_nnsoftmax Channel soft-max.
- vl_nnsoftmaxloss Deprecated
- vl_nnspnorm Spatial normalization.
SimpleCNN wrapper- vl_simplenn A lightweight wrapper for CNNs with a linear topology.
- vl_simplenn_tidy Upgrade or otherwise fix a CNN.
- vi_simplenn_display Print information about the CNN architecture.
- vl_simplenn_move Move the CNN between CPU and GPU.
DagNN wrapper- DagNN An object-oriented wrapper for CNN with complex topologies
Other functions- vl_argparse A helper function to parse optional arguments.
- vl_compilenn Compile the MEX fields in the toolbox.
- vl_contrib Download, compile, and setup third-party modules.
- vl_rootnn Return the path to the MatConvNet toolbox installation.
- vl_setpunn Setup MatConvNet for use in MATLAB.
- vl_imreadjpeg Quickly load a batch of JPEG images.
- vl_taccum Accumulate tensors operating in-place when possible.
- vl_tmove Exchange tensors between MATLAB processes and GPUs.
- vl_tshow Show a tensor on screen.