一、模型训练
sample_in = []
sample_out = []
offset = 5
path='D:\demo3\l\';
Files = dir(fullfile(path,'*.png'));
LengthFiles = length(Files);
a_s_out = [1 0 0];
for i=1:LengthFiles
Img = imread([path,Files(i).name]);
Img_grey = rgb2gray(Img);
a_sample = [];
for i = 1:offset:size(Img_grey,1)
for j = 1:offset:size(Img_grey,2)
a_sample(end+1) = Img_grey(i,j);
end
end
sample_in = [sample_in;a_sample];
sample_out = [sample_out;a_s_out];
%plot(a_sample);
%imagesc(Img2);
end
size(sample_in)
path='D:\demo3\r\';
Files = dir(fullfile(path,'*.png'));
LengthFiles = length(Files);
a_s_out = [0 0 1];
for i=1:LengthFiles
Img = imread([path,Files(i).name]);
Img_grey = rgb2gray(Img);
a_sample = [];
for i = 1:offset:size(Img_grey,1)
for j = 1:offset:size(Img_grey,2)
a_sample(end+1) = Img_grey(i,j);
end
end
sample_in = [sample_in;a_sample];
sample_out = [sample_out;a_s_out];
%plot(a_sample);
%imagesc(Img2);
end
path='D:\demo3\m\';
Files = dir(fullfile(path,'*.png'));
LengthFiles = length(Files);
a_s_out = [0 1 0];
for i=1:LengthFiles
Img = imread([path,Files(i).name]);
Img_grey = rgb2gray(Img);
a_sample = [];
for i = 1:offset:size(Img_grey,1)
for j = 1:offset:size(Img_grey,2)
a_sample(end+1) = Img_grey(i,j);
end
end
sample_in = [sample_in;a_sample];
sample_out = [sample_out;a_s_out];
%plot(a_sample);
%imagesc(Img2);
end
p = sample_in';
t = sample_out';%[1 0;0 1]';
% % %创建BP网络
net = newff(minmax(p),[100 3],{'logsig' 'logsig'},'traingda');
% %
% % %初始化BP网络,包括:权值和偏置的初值
net = init(net);
% %
% % %设置训练参数和训练BP网络
net.trainParam.epochs = 200;
net.trainParam.goal = 0.001;
net.trainParam.show = 10;
% % %训练BP神经网络
net = train(net,p,t);
%保存网络
save('jtt_net.mat','net');
%自洽验证,
BPoutput=sim(net,p);
str = 'BPoutput size is';
size(BPoutput)
figure;
imagesc(BPoutput);
order = [];
for j = 1:size(BPoutput,2)%very sample
big_order = 1;
big_val = BPoutput(1,j);
for i = 2: size(BPoutput,1)
if big_val < BPoutput(i,j)
big_order = i;
end
end
order(end+1) = big_order;
end
figure;plot(order,'*');
二、模型使用
load('jtt_net.mat');
sample_in = []
sample_out = []
offset = 5
path='D:\demo3\before\';
Files = dir(fullfile(path,'*.png'));
LengthFiles = length(Files);
for i=1:LengthFiles
Img = imread([path,Files(i).name]);
Img_grey = rgb2gray(Img);
a_sample = [];
for i = 1:offset:size(Img_grey,1)
for j = 1:offset:size(Img_grey,2)
a_sample(end+1) = Img_grey(i,j);
end
end
sample_in = [sample_in;a_sample];
end
p = sample_in';
size(p)
size(t)
BPoutput=sim(net,p);
str = 'BPoutput size is';
size(BPoutput)
figure;
imagesc(BPoutput);
order = [];
for j = 1:size(BPoutput,2)%very sample
big_order = 1;
big_val = BPoutput(1,j);
for i = 2: size(BPoutput,1)
if big_val < BPoutput(i,j)
big_order = i;
end
end
order(end+1) = big_order;
end
figure;plot(order,'*');
%文件转移
for i=1:LengthFiles
if order(i) == 1
copyfile([path,Files(i).name],[path,'\left\',Files(i).name]);
elseif (order(i)==2)
copyfile([path,Files(i).name],[path,'\middle\',Files(i).name]);
elseif (order(i)==3)
copyfile([path,Files(i).name],[path,'\right\',Files(i).name]);
end;
end;