MNIST手写数字识别【Matlab神经网络工具箱】

MNIST手写数字识别

Matlab代码:

%Neural Networks Codes will be run on this part
tic
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clc
clear all
pic1=imread('8.png');
pic1=rgb2gray(pic1);
% pic=255-pic;
[a,b]=size(pic1);
for i=1:1:a
    for j=1:1:b   
        if  pic1(i,j)==0    
            up=i;           
            break       
        end
    end
end
for i=a:-1:1
    for j=1:1:b       
        if  pic1(i,j)==0            
            down=i;
            break
        end        
    end
end
for j=1:1:b
    for i=1:1:a       
        if  pic1(i,j)==0            
            left=j;
            break
        end        
    end
end
for j=b:-1:1
    for i=1:1:a       
        if  pic1(i,j)==0            
            right=j;
            break
        end        
    end
end
pic=pic1(down:up,right:left);
imshow(pic)
pic=imresize(pic,[28 28]);
% size(pic);
pic1=1-double(reshape(pic,784,1))/255;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%加载待识别图像
image = loadMNISTImages('train-images'); % 加载样本图像共60000,size(image)=784*60000
label = loadMNISTLabels('train-labels');%加载样本图像对应标签
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
label1=zeros(60000,10);
j=0;
for i=1:1:60000  
    j=label(i)+1;
    label1(i,j)=1;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%=训练样本
PR=minmax(image);
bpnet=newff(PR,[30 10],{'tansig', 'tansig'}, 'traingd', 'learngdm');
net.epoch=100;
net.trainParam.epochs=10;%允许最大训练步数
net.trainParam.goal=0.001; %训练目标最小误差0.001
net.trainParam.show=1; %每间隔100步显示一次训练结果
net.trainParam.lr=0.01; %学习速率
bpnet=train(bpnet,image,label1');
bpnet=sim(bpnet,pic1);
shuzi=find(bpnet==max(bpnet))-1
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
toc

利用自己在写字板上写的字测试:

MNIST手写数字识别【Matlab神经网络工具箱】_第1张图片

如果不会下载MNIST 数据,可以访问:我的GitHub:

https://github.com/WeisongZhao/MNIST_Recognization

里面有不使用MATLAB工具箱的源代码

你可能感兴趣的:(计算机视觉,机器学习,Matlab)