1.下载alexnet工具箱
在命令栏输入alexnet,若不报错说明已经安装,若出现错误按照提示去下载工具箱;
下载地址:https://ww2.mathworks.cn/matlabcentral/fileexchange/59133-deep-learning-toolbox-model-for-alexnet-network
(1)需要先注册(十分简单),登陆,下载;
(2)下载完成之后,windows 是无法运行该文件的;
(3)需要打开 matlab,进入到该文件所在的路径,双击运行;(注:需要较久的时间下载安装 alexnet)
2.demo
clear
camera = webcam;
nnet = alexnet;
while true
picture = camera.snapshot;
picture = imresize(picture, [227, 227]);
label = classify(nnet, picture);
image(picture);
title(char(label));
end
3.网络结构
>> net = alexnet;
>> net.Layers
ans =
25x1 Layer array with layers:
1 'data' Image Input 227x227x3 images with 'zerocenter' normalization
2 'conv1' Convolution 96 11x11x3 convolutions with stride [4 4] and padding [0 0 0 0]
3 'relu1' ReLU ReLU
4 'norm1' Cross Channel Normalization cross channel normalization with 5 channels per element
5 'pool1' Max Pooling 3x3 max pooling with stride [2 2] and padding [0 0 0 0]
6 'conv2' Convolution 256 5x5x48 convolutions with stride [1 1] and padding [2 2 2 2]
7 'relu2' ReLU ReLU
8 'norm2' Cross Channel Normalization cross channel normalization with 5 channels per element
9 'pool2' Max Pooling 3x3 max pooling with stride [2 2] and padding [0 0 0 0]
10 'conv3' Convolution 384 3x3x256 convolutions with stride [1 1] and padding [1 1 1 1]
11 'relu3' ReLU ReLU
12 'conv4' Convolution 384 3x3x192 convolutions with stride [1 1] and padding [1 1 1 1]
13 'relu4' ReLU ReLU
14 'conv5' Convolution 256 3x3x192 convolutions with stride [1 1] and padding [1 1 1 1]
15 'relu5' ReLU ReLU
16 'pool5' Max Pooling 3x3 max pooling with stride [2 2] and padding [0 0 0 0]
17 'fc6' Fully Connected 4096 fully connected layer
18 'relu6' ReLU ReLU
19 'drop6' Dropout 50% dropout
20 'fc7' Fully Connected 4096 fully connected layer
21 'relu7' ReLU ReLU
22 'drop7' Dropout 50% dropout
23 'fc8' Fully Connected 1000 fully connected layer
24 'prob' Softmax softmax
25 'output' Classification Output crossentropyex with 'tench' and 999 other classes
4.特征提取
%图片集有 55 个训练图像和 20 个验证图像,将数据拆分,其中 70% 用作训练数据,30% 用作测试数据。
>> unzip('MerchData.zip');
>> imds = imageDatastore('MerchData', ...
'IncludeSubfolders',true, ...
'LabelSource','foldernames');
>> [imdsTrain,imdsTest] = splitEachLabel(imds,0.7,'randomized');
%显示一些实例图像
>> numTrainImages = numel(imdsTrain.Labels);
>>> idx = randperm(numTrainImages,16);
>> figure
>> for i = 1:16
subplot(4,4,i)
I = readimage(imdsTrain,idx(i));
imshow(I)
end
>> inputSize = net.Layers(1).InputSize
inputSize =
227 227 3
%输入图像尺寸要求为227 X 227 X 3,调整输入图像尺寸,并进行图像增强处理
>> augimdsTrain = augmentedImageDatastore(inputSize(1:2),imdsTrain);
>> augimdsTest = augmentedImageDatastore(inputSize(1:2),imdsTest);
%提取第七层全连接层的特征
>> net = alexnet;
>> layer = 'fc7';
>> featuresTrain = activations(net,augimdsTrain,layer,'OutputAs','rows');
featuresTest = activations(net,augimdsTest,layer,'OutputAs','rows');
5.对测试图像进行分类
%从训练数据和测试数据中提取类标签。
>> YTrain = imdsTrain.Labels;
>> YTest = imdsTest.Labels;
%使用从训练图像中提取的特征作为预测变量,拟合图像分类器
>> classifier = fitcecoc(featuresTrain,YTrain);
%对测试图像进行分类
>> YPred = predict(classifier,featuresTest);
%显示四个示例测试图像及预测的标签
>> idx = [1 5 10 15];
>> figure
>> for i = 1:numel(idx)
subplot(2,2,i)
I = readimage(imdsTest,idx(i));
label = YPred(idx(i));
imshow(I)
title(char(label))
end
>> accuracy = mean(YPred == YTest)
accuracy =
1