标签:图像识别 patternnet 深度网络 Matlab
除了softmax层构建的深度网络,Matlab还有一个简单的构建数据分类的函数,那就是patternnet,其用法类似。可以直接对图像特征数据处理,也可以对图像集处理。代码如下:%exam1.m 用训练图像特征构建深度网络并计算测试图像得分
clear;
load(‘JDPig_mlhmslbp_spyr.mat‘);
m = numel(classe_name);
n = length(y);
label = []
for i=1:n
label(:,i) = zeros(m,1);
label(y(i),i) = 1;
end
testImg = load(‘JDTest_mlhmslbp_spyr.mat‘);
if ~exist(‘patternnet.mat‘)
net = patternnet(1000);
net = train(net,X,label);
fprintf(‘saving net\n‘);
save(‘patternnet.mat‘,‘net‘);
else
load(‘patternnet.mat‘,‘net‘);
fprintf(‘loading net\n‘);
end
if ~exist(‘resPatternnet.mat‘)
view(net);
scores = net(testImg.X);
save(‘resPatternnet.mat‘,‘scores‘);
else
load(‘resPatternnet.mat‘,‘scores‘);
end
fprintf(‘Testing images!\n‘);
load(‘testName.mat‘,‘imgName‘);
for i=1:length(scores)
for j=1:m
indImg((i-1)*m+j) = imgName(i);
plabel((i-1)*m+j) = j;
prob((i-1)*m+j) = scores(j,i);
end
end
T = table(indImg‘,plabel‘,prob‘);
writetable(T,‘resPatternnet.csv‘);
fprintf(‘Image recognition finished!\n‘);
clear;
% 不用特征文件,对图像文件夹进行处理
trainFolder = fullfile(pwd, ‘image‘);
testFolder = fullfile(pwd, ‘query‘);
testSet = imageSet(testFolder); %query image
trainImds = imageDatastore(trainFolder,‘IncludeSubfolders‘,true,‘FileExtensions‘,‘.jpg‘,‘LabelSource‘,‘foldernames‘);
testImds = imageDatastore(testFolder,‘IncludeSubfolders‘,false);
setNum = numel(qImageSets);
y = trainImds.Labels;
n = length(y);
label = []
for i=1:n
label(:,i) = zeros(m,1);
label(y(i),i) = 1;
end
for i=1:
if ~exist(‘patternnet1024.mat‘)
net = patternnet(1024);
net = train(net,X,label);
fprintf(‘saving net\n‘);
save(‘patternnet1024.mat‘,‘net‘);
else
load(‘patternnet1024.mat‘,‘net‘);
fprintf(‘loading net\n‘);
end
if ~exist(‘resPatternnet1024.mat‘)
view(net);
scores = net(testImg.X);
save(‘resPatternnet1024.mat‘,‘scores‘);
else
load(‘resPatternnet1024.mat‘,‘scores‘);
end
fprintf(‘Testing images!\n‘);
load(‘testName.mat‘,‘imgName‘);
for i=1:length(scores)
for j=1:m
indImg((i-1)*m+j) = imgName(i);
plabel((i-1)*m+j) = j;
prob((i-1)*m+j) = scores(j,i);
end
end
T = table(indImg‘,plabel‘,prob‘);
writetable(T,‘resPatternnet2000.csv‘);
fprintf(‘Image recognition finished!\n‘);
代码功能与上节京东金融大数据竞赛猪脸识别(6)- 识别方法之三类似,可参看其注释。
标签:图像识别 patternnet 深度网络 Matlab
原文地址:http://blog.51cto.com/8764888/2086338