我正在使用动手DL教程(http://www.cvc.uab.es/~gros/index.php/hands-on-deep-learning-with-matconvnet/)来了解卷积神经网络(CNN)的工作原理。
首先,我编译MatConvnet
并使用网络结构运行AlexNet
,如下所示:
net = dagnn.DagNN() ;
% special padding for CIFAR-10
net.addLayer('conv1', dagnn.Conv('size', [11 11 3 96], 'hasBias', true, 'stride', [4, 4], 'pad', [20 20 20 20]), {'input'}, {'conv1'}, {'conv1f' 'conv1b'});
net.addLayer('relu1', dagnn.ReLU(), {'conv1'}, {'relu1'}, {});
net.addLayer('lrn1', dagnn.LRN('param', [5 1 2.0000e-05 0.7500]), {'relu1'}, {'lrn1'}, {});
net.addLayer('pool1', dagnn.Pooling('method', 'max', 'poolSize', [3, 3], 'stride', [2 2], 'pad', [0 0 0 0]), {'lrn1'}, {'pool1'}, {});
net.addLayer('conv2', dagnn.Conv('size', [5 5 48 256], 'hasBias', true, 'stride', [1, 1], 'pad', [2 2 2 2]), {'pool1'}, {'conv2'}, {'conv2f' 'conv2b'});
net.addLayer('relu2', dagnn.ReLU(), {'conv2'}, {'relu2'}, {});
net.addLayer('lrn2', dagnn.LRN('param', [5 1 2.0000e-05 0.7500]), {'relu2'}, {'lrn2'}, {});
net.addLayer('pool2', dagnn.Pooling('method', 'max', 'poolSize', [3, 3], 'stride', [2 2], 'pad', [0 0 0 0]), {'lrn2'}, {'pool2'}, {});
net.addLayer('conv3', dagnn.Conv('size', [3 3 256 384], 'hasBias', true, 'stride', [1, 1], 'pad', [1 1 1 1]), {'pool2'}, {'conv3'}, {'conv3f' 'conv3b'});
net.addLayer('relu3', dagnn.ReLU(), {'conv3'}, {'relu3'}, {});
net.addLayer('conv4', dagnn.Conv('size', [3 3 192 384], 'hasBias', true, 'stride', [1, 1], 'pad', [1 1 1 1]), {'relu3'}, {'conv4'}, {'conv4f' 'conv4b'});
net.addLayer('relu4', dagnn.ReLU(), {'conv4'}, {'relu4'}, {});
net.addLayer('conv5', dagnn.Conv('size', [3 3 192 256], 'hasBias', true, 'stride', [1, 1], 'pad', [1 1 1 1]), {'relu4'}, {'conv5'}, {'conv5f' 'conv5b'});
net.addLayer('relu5', dagnn.ReLU(), {'conv5'}, {'relu5'}, {});
net.addLayer('pool5', dagnn.Pooling('method', 'max', 'poolSize', [3 3], 'stride', [2 2], 'pad', [0 0 0 0]), {'relu5'}, {'pool5'}, {});
net.addLayer('fc6', dagnn.Conv('size', [1 1 256 4096], 'hasBias', true, 'stride', [1, 1], 'pad', [0 0 0 0]), {'pool5'}, {'fc6'}, {'conv6f' 'conv6b'});
net.addLayer('relu6', dagnn.ReLU(), {'fc6'}, {'relu6'}, {});
net.addLayer('fc7', dagnn.Conv('size', [1 1 4096 4096], 'hasBias', true, 'stride', [1, 1], 'pad', [0 0 0 0]), {'relu6'}, {'fc7'}, {'conv7f' 'conv7b'});
net.addLayer('relu7', dagnn.ReLU(), {'fc7'}, {'relu7'}, {});
net.addLayer('classifier', dagnn.Conv('size', [1 1 4096 10], 'hasBias', true, 'stride', [1, 1], 'pad', [0 0 0 0]), {'relu7'}, {'classifier'}, {'conv8f' 'conv8b'});
net.addLayer('prob', dagnn.SoftMax(), {'classifier'}, {'prob'}, {});
net.addLayer('objective', dagnn.Loss('loss', 'log'), {'prob', 'label'}, {'objective'}, {});
net.addLayer('error', dagnn.Loss('loss', 'classerror'), {'prob','label'}, 'error') ;
我加载数据集(imdb_cifar10.mat
)并训练网络:
imdb_cifar10 = load('../data/imdb_cifar10.mat');
[net_alexnet, info] = alexnet_train(imdb_cifar10, 'results/cifar_10_experiment_1');
在10个时期之后,我收到了10 net-epoch-x.mat
个文件。那么,我想加载其中一个文件来测试图像,但没有成功:
net = load('results/cifar_10_experiment_1 /net-epoch-10.mat');
net = dagnn.DagNN.loadobj(net.net);
net.meta.classes.description = imdb_cifar10 .meta.classes;
im = imread('../data/dog.jpg');
inference_classification(im, alexNet);
其中:
function inference_classification(im, net)
im_ = single(im) ; % note: 0-255 range
im_ = imresize(im_, net.meta.normalization.imageSize(1:2));
im_ = im_ - net.meta.normalization.averageImage;
% run the CNN
net.eval({'input', im_});
% obtain the CNN otuput
scores = net.vars(net.getVarIndex('prob')).value;
scores = squeeze(gather(scores));
% show the classification results
[bestScore, best] = max(scores);
figure(1) ; clf ; imagesc(im);
title(sprintf('%s (%d), score %.3f', net.meta.classes.description{best}, best, bestScore));
end
Matlab显示我在im_ = imresize(im_, net.meta.normalization.imageSize(1:2));
我尝试使用MatconvNet的某些不同版本运行代码(例如,matconvnet-1.0-beta16和matconvnet-1.0-beta23),但结果是相同的。能不能给我一些解决这个问题的建议。
非常感谢您的时间。
致以最诚挚的问候,
An Nhien