deepnet = alexnet %利用库内pretrained alexnet
classify(deepnet,img) %使用alexnet来分类当前的图片
1.做出预测
deepnet = alexnet;
pred= classify(deepnet ,img);
2.CNN架构
deepnet = alexnet;
ly=deepnet.Layers
inlayer=ly(1)
insz=inlayer.InputSize
outlayer=ly(end)
categorynames=outlayer.Classes
3.检查预测结果
img = imread('file01.jpg');
imshow(img)
net = alexnet;
categorynames = net.Layers(end).ClassNames;
%Classify an image
[pred,scores] = classify(net,img);
%Display scores
bar(scores)
%Threshold scores
highscores = scores > 0.01;
%Display thresholded scores
bar(scores(highscores))%提取出大于阈值的分数并显示
%Add tick labels
xticklabels(categorynames(highscores))
thresh = median(scores) + std(scores);%中值+ 标准差
highscores = scores > thresh;
xticks(1:length(scores(highscores)))%划出有多少个刻度值
xticklabels(categorynames(highscores))%标出该刻度值的标签
xtickangle(60)%标签显示角度
1.图像数据库存储
ls *.jpg
net = alexnet;
%Create datastore
imds=imageDatastore("file*.jpg");
%Extract file names
fname=imds.Files
%Read an image
img=readimage(imds,7)
%Classify images
[preds,scors]=classify(net,imds)
%max(scors,[],2)
[C,labels]=max(scores,[],2) %C为预测的最大值,labels 为预测的种类标签 长度= 文件数*1
bar(C)
2.准备要作为输入的图像
ls *.jpg
net = alexnet
%Create datastore
imds=imageDatastore('*.jpg')
%Create augmentedImageDatastore
auds = augmentedImageDatastore([227 227],imds)
%Classify datastore
preds=classify(net,auds)
3.处理数据存储中的图像
ls *.jpg
net = alexnet
imds=imageDatastore('*.jpg')
% Display images in imds
montage(imds)
% Create augmentedImageDatastore
auds = augmentedImageDatastore([227 227],imds,'ColorPreprocessing','gray2rgb')
% Classify datastore
preds=classify(net,auds)
4.使用子文件夹创建数据存储
net = alexnet;
% Create datastore
flwrds = imageDatastore('Flowers','IncludeSubfolders',true)
% Classify images
preds=classify(net,flwrds)
1.什么是迁移学习
2.迁移学习所需的组件
load pathToImages
flwrds = imageDatastore(pathToImages,'IncludeSubfolders',true);
flowernames = flwrds.Labels %获取标签
%Create datastore with labels
flwrds = imageDatastore(pathToImages,...
'IncludeSubfolders',true,...
'LabelSource','foldernames')
% Extract new labels
flowernames=flwrds.Labels
3.准备训练数据
load pathToImages
flwrds = imageDatastore(pathToImages,'IncludeSubfolders',true,'LabelSource','foldernames')
% Split datastore
[flwrTrain,flwrTest] = splitEachLabel(flwrds,0.6)
%Split datastore randomly
[flwrTrain,flwrTest] = splitEachLabel(flwrds,0.8,'randomized')
%Split datastore by number of images
[flwrTrain,flwrTest] = splitEachLabel(flwrds,50)
4.修改网络层次
anet = alexnet;
layers = anet.Layers
%Create new layer
fc = fullyConnectedLayer(12)
%Replace 23rd layer
layers(23) = fc
%Replace last layer
layers(end) = classificationLayer
%You can use the classificationLayer function to create a new output layer for an image %classification network.
%例中我们要识别12种花,我们要将第23层有1000个单元的神经层替换成12个单元的全连接层。再将最后一
%层1000个类别的输出层换成我们的类别输出层
5.设置训练选项
%Set default options
opts = trainingOptions('sgdm')
%Set initial learning rate
opts = trainingOptions('sgdm','InitialLearnRate',0.001)
6.训练网络
7.评估性能
8.迁移学习小结
Get training images
flowerds = imageDatastore('Flowers','IncludeSubfolders',true,'LabelSource','foldernames');
Split into training and testing sets
[trainImgs,testImgs] = splitEachLabel(flowerds,0.6);
Determine the number of flower species
numClasses = numel(categories(flowerds.Labels));
Create a network by modifying AlexNet
Get the layers from AlexNet
net = alexnet;
layers = net.Layers;
Modify the classification and output layers
layers(end-2) = fullyConnectedLayer(numClasses);
layers(end) = classificationLayer;
Set training algorithm options
Lower the learning rate for transfer learning
options = trainingOptions('sgdm','InitialLearnRate', 0.001);
Perform training
[flowernet,info] = trainNetwork(trainImgs, layers, options);
Use the trained network to classify test images
testpreds = classify(flowernet,testImgs);
Evaluate the results
Calculate the accuracy
nnz(testpreds == testImgs.Labels)/numel(testpreds)
Visualize the confusion matrix
confusionchart(testImgs.Labels,testpreds);