clear
trainPath = fullfile(pwd,‘image‘);
trainData = imageDatastore(trainPath,...
‘IncludeSubfolders‘,true,‘LabelSource‘,‘foldernames‘);
%对训练数据集进行划分
[trainingImages,testImages] = splitEachLabel(trainData,0.7,‘randomized‘);
numTrainImages = numel(trainingImages.Labels);
%设定要加载的预训练模型
% net1 = googlenet;
% net2 = vgg16;
% net = vgg19;
net = alexnet;
layer = ‘fc7‘;
%提取第7层网络输出数据
trainingFeatures = activations(net,trainingImages,layer);
testFeatures = activations(net,testImages,layer);
trainingLabels = trainingImages.Labels;
testLabels = testImages.Labels;
%训练多分类模型
classifier = fitcecoc(trainingFeatures,trainingLabels, ‘FitPosterior‘,1);
%预测数据的类别
predictedLabels = predict(classifier,testFeatures);
[label,NegLoss,PBScore,Posterior] = predict(classifier,testFeatures);
idx = [1 5 10 15];
figure
for i = 1:numel(idx)
subplot(2,2,i)
I = readimage(testImages,idx(i));
label = predictedLabels(idx(i));
imshow(I);
title(char(label));
end
%计算准确率
accuracy = mean(predictedLabels == testLabels);
该方法在csv文件上传后得分在1左右,排名约在前8%。这段代码不是比赛用的完整代码,不过用法类似。
原文地址:http://blog.51cto.com/8764888/2086360