大家好,又见面了,我是你们的朋友全栈君。
除了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)- 识别方法之三类似,可参看其注释。
转载于:https://blog.51cto.com/8764888/2086338
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/151911.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...