京东金融大数据竞赛猪脸识别(9)- 识别方法之五

这里给出使用深度网络中间层输出结果作为图像特征,并构建分类模型和对训练数据进行识别的代码。相关内容可参看Matlab图像识别/检索系列(7)-10行代码完成深度学习网络之取中间层数据作为特征。代码如下:

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%。这段代码不是比赛用的完整代码,不过用法类似。

转载于:https://blog.51cto.com/8764888/2086360

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值