clear;
trainPath = fullfile(pwd,‘image‘);
trainData = imageDatastore(trainPath,...
‘IncludeSubfolders‘,true,‘LabelSource‘,‘foldernames‘);
[trainingImages,testImages] = splitEachLabel(trainData,0.7,‘randomized‘);
numTrainImages = numel(trainingImages.Labels);
%加载预训练模型
net = alexnet;
%指定用来提取特征的层
layer = ‘fc7‘;
%提取指定层训练数据特征
trainingFeatures = activations(net,trainingImages,layer);
%提取指定层测试数据特征
testFeatures = activations(net,testImages,layer);
%获取训练数据标签
trainingLabels = trainingImages.Labels;
%获取测试数据标签
testLabels = testImages.Labels;
save(‘alexnetFeature.mat‘,‘trainingFeatures‘,‘trainingLabels‘,‘trainingFeatures‘,‘testLabels‘);
相关内容可参看Matlab图像识别/检索系列(7)-10行代码完成深度学习网络之取中间层数据作为特征。
原文地址:http://blog.51cto.com/8764888/2086352