Matlab DNN多层感知机进行图像分类——附源码分享

提示:麻烦点赞,拒绝白嫖

文章目录

  • 前言
  • 一、数据来源
  • 二、训练+预测_一步到位源码
    • 1.DNN.m
  • 总结


前言

Python不香吗?非得用matlab来搞机器学习的东西?不是不是,matlab也有集成了许多机器学习算法,当然,都是一些非常基础的机器学习算法。深度学习还是得向python看齐。今天试用了一下matlab自带的DNN模型,封装在newff函数里,寥寥几行代码,非常简洁。


提示:以下是本篇文章正文内容,下面案例可供参考

一、数据来源

打开画图软件自己画了10张图片,5张为A,5张为B,如下:

Matlab DNN多层感知机进行图像分类——附源码分享_第1张图片

将它们再次一一截取,得到10张图片作为训练集。随便找了一张,加入随机噪声得到测试集,以下为分别添加10%噪声,20%噪声,30%噪声。

Matlab DNN多层感知机进行图像分类——附源码分享_第2张图片

二、训练+预测_一步到位源码

1.DNN.m

clear;clc;
traindata = [];

A = imread('a1.png');
A = imresize(A,[50 50],'bilinear');
n=graythresh(A);
I = double(~im2bw(A,n));
I = reshape(I,1,2500);
traindata = [traindata;I];

A = imread('a2.png');
A = imresize(A,[50 50],'bilinear');
n=graythresh(A);
I = double(~im2bw(A,n));
I = reshape(I,1,2500);
traindata = [traindata;I];

A = imread('a3.png');
A = imresize(A,[50 50],'bilinear');
n=graythresh(A);
I = double(~im2bw(A,n));
I = reshape(I,1,2500);
traindata = [traindata;I];

A = imread('a4.png');
A = imresize(A,[50 50],'bilinear');
n=graythresh(A);
I = double(~im2bw(A,n));
I = reshape(I,1,2500);
traindata = [traindata;I];

A = imread('a5.png');
A = imresize(A,[50 50],'bilinear');
n=graythresh(A);
I = double(~im2bw(A,n));
I = reshape(I,1,2500);
traindata = [traindata;I];

A = imread('b1.png');
A = imresize(A,[50 50],'bilinear');
n=graythresh(A);
I = double(~im2bw(A,n));
I = reshape(I,1,2500);
traindata = [traindata;I];

A = imread('b2.png');
A = imresize(A,[50 50],'bilinear');
n=graythresh(A);
I = double(~im2bw(A,n));
I = reshape(I,1,2500);
traindata = [traindata;I];

A = imread('b3.png');
A = imresize(A,[50 50],'bilinear');
n=graythresh(A);
I = double(~im2bw(A,n));
I = reshape(I,1,2500);
traindata = [traindata;I];

A = imread('b4.png');
A = imresize(A,[50 50],'bilinear');
n=graythresh(A);
I = double(~im2bw(A,n));
I = reshape(I,1,2500);
traindata = [traindata;I];

A = imread('b5.png');
A = imresize(A,[50 50],'bilinear');
n=graythresh(A);
I = double(~im2bw(A,n));
I = reshape(I,1,2500);
traindata = [traindata;I];

class = [1;1;1;1;1;2;2;2;2;2];
c = [1;1;1;1;1;2;2;2;2;2];

R1  = randsrc(10,2500,[0,1;0.9,0.1]);
test1 = traindata - R1;
test1(test1<0) = 1;

R2  = randsrc(10,2500,[0,1;0.8,0.2]);
test2 = traindata - R2;
test2(test2<0) = 1;

R3  = randsrc(10,2500,[0,1;0.7,0.3]);
test3 = traindata - R3;
test3(test3<0) = 1;

%特征值归一化
% [input,minI,maxI] = premnmx(traindata)  ;
input =  traindata;
%构造输出矩阵
s = length( class) ;
output = zeros( s , 2  ) ;
for i = 1 : s 
   output( i , class( i )  ) = 1 ;
end
%创建神经网络
net = newff( minmax(input') ,output', [500 100 10] , { 'logsig' 'purelin' } , 'traingdx' ) ; 

%设置训练參数
net.trainparam.show = 50 ;% 显示中间结果的周期
net.trainparam.epochs = 500 ;%最
net.trainParam.lr = 0.01 ;%学习速率(Learning 大迭代次数(学习次数)
net.trainparam.goal = 0.01 ;%神经网络训练的目标误差rate)
%开始训练
%其中input为训练集的输入信号,对应output为训练集的输出结果
net = train( net, input' , output' ) ;
%================================训练完成====================================%
%=============================接下来进行测试=================================%
 %测试数据归一化
% testInput = tramnmx ( test1' , minI, maxI ) ;
%[testInput,minI,maxI] = premnmx( [t1 , t2 , t3 , t4 ]');

% 测试1
Y = sim( net , test1' );
[s1 , s2] = size( Y ) ;
hitNum = 0 ;
for i = 1 : s2
    [m , Index] = max( Y( : ,  i ) ) ;
    if( Index  == c(i)   ) 
        hitNum = hitNum + 1 ; 
    end
end
sprintf('Test1 Accuracy is %3.3f%%',100 * hitNum / s2 )

% 测试2
% Y = sim( net , test2' );
% [s1 , s2] = size( Y ) ;
% hitNum = 0 ;
% for i = 1 : s2
%     [m , Index] = max( Y( : ,  i ) ) ;
%     if( Index  == c(i)   ) 
%         hitNum = hitNum + 1 ; 
%     end
% end
% sprintf('Test2 Accuracy is %3.3f%%',100 * hitNum / s2 )
% 
% % 测试3
% Y = sim( net , test3' );
% [s1 , s2] = size( Y ) ;
% hitNum = 0 ;
% for i = 1 : s2
%     [m , Index] = max( Y( : ,  i ) ) ;
%     if( Index  == c(i)   ) 
%         hitNum = hitNum + 1 ; 
%     end
% end
% sprintf('Test3 Accuracy is %3.3f%%',100 * hitNum / s2 )

总结

拿最基础的DNN搞这个图片分类很难搞,而且matlab只适合机器学习,深度学习建议转python。

你可能感兴趣的:(Prediction,matlab,matlab,dnn,python)