当前位置:网站首页>Analysis of 43 cases of MATLAB neural network: Chapter 19 handwritten font recognition based on SVM

Analysis of 43 cases of MATLAB neural network: Chapter 19 handwritten font recognition based on SVM

2022-06-21 22:42:00 mozun2020

《MATLAB neural network 43 A case study 》: The first 19 Chapter be based on SVM Handwritten font recognition

1. Preface

《MATLAB neural network 43 A case study 》 yes MATLAB Technology Forum (www.matlabsky.com) planning , Led by teacher wangxiaochuan ,2013 Beijing University of Aeronautics and Astronautics Press MATLAB A book for tools MATLAB Example teaching books , Is in 《MATLAB neural network 30 A case study 》 On the basis of modification 、 Complementary , Adhering to “ Theoretical explanation — case analysis — Application extension ” This feature , Help readers to be more intuitive 、 Learn neural networks vividly .

《MATLAB neural network 43 A case study 》 share 43 Chapter , The content covers common neural networks (BP、RBF、SOM、Hopfield、Elman、LVQ、Kohonen、GRNN、NARX etc. ) And related intelligent algorithms (SVM、 Decision tree 、 Random forests 、 Extreme learning machine, etc ). meanwhile , Some chapters also cover common optimization algorithms ( Genetic algorithm (ga) 、 Ant colony algorithm, etc ) And neural network . Besides ,《MATLAB neural network 43 A case study 》 It also introduces MATLAB R2012b New functions and features of neural network toolbox in , Such as neural network parallel computing 、 Custom neural networks 、 Efficient programming of neural network, etc .

In recent years, with the rise of artificial intelligence research , The related direction of neural network has also ushered in another upsurge of research , Because of its outstanding performance in the field of signal processing , The neural network method is also being applied to various applications in the direction of speech and image , This paper combines the cases in the book , It is simulated and realized , It's a relearning , I hope I can review the old and know the new , Strengthen and improve my understanding and practice of the application of neural network in various fields . I just started this book on catching more fish , Let's start the simulation example , Mainly to introduce the source code application examples in each chapter , This paper is mainly based on MATLAB2015b(32 position ) Platform simulation implementation , This is Chapter 19 of the book based on SVM An example of handwriting font recognition , Don't talk much , Start !

2. MATLAB Simulation example

open MATLAB, Click on “ Home page ”, Click on “ open ”, Find the sample file
 Insert picture description here
Choose Chapter_CharacterRecognitionUsingLibsvm.m, Click on “ open ”

Chapter_CharacterRecognitionUsingLibsvm.m Source code is as follows :

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function Chapter_CharacterRecognitionUsingLibsvm
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% function : be based on SVM Handwritten font recognition 
% Environmental Science :Win7,Matlab2015b
%Modi: C.S
% Time :2022-06-17
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%% Matlab neural network 43 A case study 

%  be based on SVM Handwritten font recognition 
% by  Li Yang (faruto)
% http://www.matlabsky.com
% Email:[email protected]163.com
% http://weibo.com/faruto 
% http://blog.sina.com.cn/faruto
% 2013.01.01
%% A Little Clean Work
close all;
clear;
clc;
format compact;
%%  Load training data 
tic
%  utilize uigetfile Function to select training samples interactively 
[FileName,PathName,FilterIndex] = uigetfile( ...
    {
    '*.jpg';'*.bmp'},' Please import training pictures ','*.jpg','MultiSelect','on');
if ~FilterIndex
    return;
end
num_train = length(FileName);
TrainData = zeros(num_train,16*16);
TrainLabel = zeros(num_train,1);
for k = 1:num_train
    pic = imread([PathName,FileName{
    k}]);
    pic = pic_preprocess(pic);
    
    %  Pull the normalized image into a vector by column and transpose , Generate 50*256 Training sample matrix of 
    TrainData(k,:) = double(pic(:)');
    %  The sample label is the number corresponding to the sample 
    TrainLabel(k) = str2double(FileName{
    k}(4));
end
%%  Establish support vector machine 
% [bestCVaccuracy,bestc,bestg] = ...
%     SVMcgForClass(TrainLabel,TrainData,-8,8,-8,8,10,0.8,0.8,4.5)

%  Set up GA Related parameters 
    ga_option.maxgen = 100;
    ga_option.sizepop = 20;
    ga_option.cbound = [0,100];
    ga_option.gbound = [0,100];
    ga_option.v = 10;
    ga_option.ggap = 0.9;
    [bestCVaccuracy,bestc,bestg] = ...
    gaSVMcgForClass(TrainLabel,TrainData,ga_option)

%  Training 
cmd = ['-c ',num2str(bestc),' -g ',num2str(bestg)];
model = svmtrain(TrainLabel, TrainData, cmd);
%  View recognition on the training set 
preTrainLabel = svmpredict(TrainLabel, TrainData, model);
%%  Load test samples 
[FileName,PathName,FilterIndex] = uigetfile( ...
    {
    '*.jpg';'*.bmp'},' Please import test pictures ','*.bmp','MultiSelect','on');
if ~FilterIndex
    return;
end
num_train = length(FileName);
TestData = zeros(num_train,16*16);
TestLabel = zeros(num_train,1);
for k = 1:num_train
    pic = imread([PathName,FileName{
    k}]);
    pic = pic_preprocess(pic);
    
    TestData(k,:) = double(pic(:)');
    TestLabel(k) = str2double(FileName{
    k}(4));
end
%%  Classify the test samples 
preTestLabel = svmpredict(TestLabel, TestData, model);
assignin('base','TestLabel',TestLabel);
assignin('base','preTestLabel',preTestLabel);
TestLabel'
preTestLabel'
%% sub function of pre-processing pic
function pic_preprocess = pic_preprocess(pic)
%  Image preprocessing subfunction 
%  Image inversion processing 
pic = 255-pic;
%  Set threshold , Convert an inverse color image into a binary image 
pic = im2bw(pic,0.4);
%  Find the row mark of all pixels on the number y And column labels x
[y,x] = find(pic == 1);
%  Intercept the smallest area containing the complete number 
pic_preprocess = pic(min(y):max(y), min(x):max(x));
%  Convert the intercepted minimum area image containing complete numbers into 16*16 Standardized image 
pic_preprocess = imresize(pic_preprocess,[16,16]);

toc


Add completed , Click on “ function ”, Start emulating , The following prompt appears in the pop-up box : Please import training pictures
 Insert picture description here
choice “ Handwritten numeral training sample pictures ” Folder , Select all pictures , Click on “ open ”
 Insert picture description here
Waiting to run , The following results appear
 Insert picture description here
The following pop-up prompt appears : Please import test pictures
 Insert picture description here
choice “ Handwritten numeral test sample pictures ” Folder , Select all pictures , Click on “ open ”
 Insert picture description here
The output simulation results are as follows :

Accuracy = 93.3333% (28/30) (classification)
ans =
  1  to  19  Column 
     0     0     0     1     1     1     2     2     2     3     3     3     4     4     4     5     5     5     6
  20  to  30  Column 
     6     6     7     7     7     8     8     8     9     9     9
ans =
  1  to  19  Column 
     0     0     0     1     1     7     2     2     2     3     3     3     4     4     4     5     5     5     6
  20  to  30  Column 
     6     6     7     7     7     8     8     8     7     9     9

3. Summary

Simple handwritten numeral recognition application , First, train , Then carry out prediction, classification and identification , Get the results of classification , The whole process has been fully implemented , If you choose a single picture by yourself, you will report an error , So when you do the simulation , Remember to select all pictures . Interested in the content of this chapter or want to fully learn and understand , It is suggested to study the contents of Chapter 19 in the book . Some of these knowledge points will be supplemented on the basis of their own understanding in the later stage , Welcome to study and exchange together .

原网站

版权声明
本文为[mozun2020]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/172/202206212049266830.html