It is currently Sun Aug 18, 2019 4:21 am

All times are UTC + 1 hour




Post new topic Reply to topic  [ 14 posts ] 
Author Message
 Post subject: 20+ lines can recognize handwritten characters for AI code
PostPosted: Sun Apr 30, 2017 8:36 am 
Offline
User
User

Joined: Fri Jan 28, 2011 12:26 pm
Posts: 29
神经网络模型部分只有20+行代码!
The neural network model section only has 20+ lines of code!
但却可以识别手写体的0-9这十个数字!
But it can identify the handwriting 0-9 of these ten figures!


训练所用的手写体样本:
Handbook for handwriting used for training:
Image

Code:
For Idx = 0 To #MAI_Sample-1
   ForEach _DimSample(Idx)\ListSample()
      For r = 0 To #MAI_Sample-1
         ;对样本像素量化值进行卷积
         Convolue.f = _DimWeight(r, 0) * -1
         For c = 1 To #MAI_Factor
            Convolue + _DimWeight(r, c) * (_DimSample(Idx)\ListSample()\Pixels[c-1])
         Next
         ;计算出可能性
         OutResult.f = 1/(1+Exp(-Convolue))
         ;对预期值进行比对,
         RFeedback.f = Exp(-Convolue)/Sqr(1+Exp(-Convolue))
         If Idx = r And OutResult < #MAI_Judged
            AntiConvolue.f = (0.90 - OutResult) * RFeedback
         ElseIf Idx <> r And OutResult >= #MAI_Judged
            AntiConvolue.f = (0.10 - OutResult) * RFeedback
         Else
            Continue
         EndIf
         ;如果达不到预期效果,对权限矩阵进行微调,以达成在下一次计算时,更加接近预期值!
         _DimWeight(r, 0) - 0.01 * AntiConvolue
         For c = 1 To #MAI_Factor
            _DimWeight(r, c) + 0.01 * AntiConvolue * _DimSample(Idx)\ListSample()\Pixels[c-1]
         Next
      Next
   Next
Next


Only training once the effect:
;只训练一次的效果 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
;没有一个识别成功
; 第 1次训练
; : 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ,
; 0: -, -, -, -, -, -, -, -, -, 51.58%,
; 1: -, -, -, -, -, -, -, -, -, 51.35%,
; 2: -, -, -, -, -, -, -, -, -, 50.23%,
; 3: -, -, -, -, -, -, -, -, -, 51.78%,
; 4: -, -, -, -, -, -, -, -, -, 53.33%,
; 5: -, -, -, -, -, -, -, -, -, 51.70%,
; 6: -, -, -, -, -, -, -, -, -, 51.07%,
; 7: -, -, -, -, -, -, -, -, -, 53.54%,
; 8: -, -, -, -, -, -, -, -, -, 53.39%,
; 9: -, -, -, -, -, -, -, -, -, 52.98%,



Training 50 times the effect:
;训练50次的效果 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
;有明显的进步了,样本识别方面,0,2,3,4,5,6,7已经能够成功识别
; : 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ,
; 0: 51.25%, -, -, -, -, -, -, -, -, -,
; 1: -, -, -, -, -, -, -, -, 50.54%, -,
; 2: -, -, 52.02%, -, -, -, -, -, 53.32%, -,
; 3: -, -, -, 59.98%, -, -, -, -, -, 60.29%,
; 4: -, -, -, -, 55.66%, -, -, -, -, -,
; 5: -, -, -, -, -, 56.79%, -, -, -, -,
; 6: -, -, -, -, -, -, 53.67%, -, -, -,
; 7: -, -, -, -, -, -, -, 53.67%, -, 54.85%,
; 8: -, -, -, -, -, -, -, -, 62.38%, -,
; 9: -, -, -, -, -, -, -, -, -, 69.44%,



Training 500 times the effect:
;训练500次的效果 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
;样本识别方面,已经没问题了,目标识别,还有几个不理想,这个跟训练样品数量有限有很大的关系,
; : 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ,
; 0: 58.03%, -, -, -, -, -, -, -, -, -,
; 1: -, 52.46%, -, -, -, -, -, -, -, -,
; 2: -, -, 65.46%, -, -, -, -, -, -, -,
; 3: -, -, -, 58.69%, -, -, -, -, -, -,
; 4: -, -, -, -, 57.11%, -, -, -, -, -,
; 5: -, -, -, -, -, 57.51%, -, -, -, -,
; 6: -, -, -, -, -, -, 57.30%, -, -, -,
; 7: -, -, -, -, -, -, -, 55.08%, -, -,
; 8: -, -, -, -, -, -, -, -, 66.43%, -,
; 9: -, -, -, -, -, -, -, -, -, 58.93%,


完整的代码:
Complete code:
Code:
; **************************************************
; *****   手写数字智能识别简例--单层神经网络   *****
; *****           迷路仟 QQ:714095563          *****
; *****               2017.04.27               *****
; **************************************************

; 说明:
; 1.本实例是用于识别手写体数字的神经网络代码,旨在教学与案例,
;   对神经网络作一个简约的编码. 本实例采样数量有限,所以,在识别手写体数字,并非万能,
;   如果有兴趣的同行,可以给程序喂食更多的手写体样本,以达到更加智能的效果!
; 2.本实例采用的是单层神经网络算法,在识别方面,不如多层神经网络的效果更好!
; 3.本实例没有对样本有识别目标做滤波处理,故本实例没有什么实战意义,
;   只用于阐述神经的基本结构!


#MAI_LightR = 299
#MAI_LightG = 587
#MAI_LightB = 114
#MAI_Total  = #MAI_LightR + #MAI_LightG + #MAI_LightB
#MAI_Factor = 28*28
#MAI_Sample = 10
#MAI_Judged = 0.5

; 字符转换结构
Structure __FactorInfo
   Pixels.f[28*28]
EndStructure

; 字符表
Structure __SampleInfo
   List ListSample.__FactorInfo()
EndStructure


UsePNGImageDecoder()
Global Dim _DimSample.__SampleInfo(#MAI_Sample-1)
Global NewList _ListIdentify.__FactorInfo()
Global Dim _DimWeight.f(#MAI_Sample-1, #MAI_Factor) ;为每个数字设置一个权限矩阵

; 样本图片采样
Procedure MAI_LoadSampleImage(ImageName$)
   ;20x10数字阵,像素大小28x28
   ImageID = LoadImage(#PB_Any, ImageName$)
   If ImageID
      CreateImage(0, ImageWidth(ImageID)+20, ImageHeight(ImageID)+20) ;为采样进行偏移处理做装备
      If StartDrawing(ImageOutput(0))
         Box(0, 0, 1000,1000, 0)
         DrawImage(ImageID(ImageID), 10, 10)
         For r = 0 To 9
            For c = 0 To 19
               For k = 1 To 5
                  i = Random(5,0)-3 ;这里对图片采样进行偏移采样,以获取更多可能性
                  j = Random(5,0)-3
                  ;==============
                  AddElement(_DimSample(r)\ListSample())
                  Idx = 0
                  For y = 0 To 27
                     For x = 0 To 27
                        Color = Point(10+c*28+i+x, 10+r*28+j+y)
                        ;通过灰度值,将样本像素化为1或0
                        If ((Red(Color)*#MAI_LightR+Green(Color)*#MAI_LightG+Blue(Color)*#MAI_LightB)/#MAI_Total) & $FF >= 128
                           _DimSample(r)\ListSample()\Pixels[Idx] = 1
                        Else
                           _DimSample(r)\ListSample()\Pixels[Idx] = 0
                        EndIf
                        Idx+1
                     Next
                  Next
               Next
               ;==============
            Next
         Next
         StopDrawing()
      EndIf
      FreeImage(ImageID)
   EndIf
EndProcedure

;-
; 训练样本
Procedure MAI_TrainSampleImage(CountTrain)
   Debug "初始化"
   For r = 0 To #MAI_Sample-1
      For c = 0 To #MAI_Factor
         _DimWeight(r,c) = Random(2,1)/#MAI_Factor/#MAI_Sample
      Next
   Next
   For k = 1 To CountTrain
      Debug "第"+RSet(Str(k), 4, " ")+"次训练"
      For Idx = 0 To #MAI_Sample-1
         ForEach _DimSample(Idx)\ListSample()
            For r = 0 To #MAI_Sample-1
               ;对样本像素量化值进行卷积
               Convolue.f = _DimWeight(r, 0) * -1
               For c = 1 To #MAI_Factor
                  Convolue + _DimWeight(r, c) * (_DimSample(Idx)\ListSample()\Pixels[c-1])
               Next
               ;计算出可能性
               OutResult.f = 1/(1+Exp(-Convolue))
               ;对预期值进行比对,
               RFeedback.f = Exp(-Convolue)/Sqr(1+Exp(-Convolue))
               If Idx = r And OutResult < #MAI_Judged
                  AntiConvolue.f = (0.90 - OutResult) * RFeedback
               ElseIf Idx <> r And OutResult >= #MAI_Judged
                  AntiConvolue.f = (0.10 - OutResult) * RFeedback
               Else
                  Continue
               EndIf
               ;如果达不到预期效果,对权限矩阵进行微调,以达成在下一次计算时,更加接近预期值!
               _DimWeight(r, 0) - 0.01 * AntiConvolue
               For c = 1 To #MAI_Factor
                  _DimWeight(r, c) + 0.01 * AntiConvolue * _DimSample(Idx)\ListSample()\Pixels[c-1]
               Next
            Next
         Next
      Next
   Next
EndProcedure

;-
; 随机识别样本,以判断学习进度
Procedure MAI_IdentifySample()
   Text$ = " : "
   For Idx = 0 To #MAI_Sample-1
      Text$ + RSet(Str(Idx)+"  ", 6, " ") +", "
   Next
   Debug Text$
   For Idx = 0 To #MAI_Sample-1
      Index = Random(19,0)
      SelectElement(_DimSample(Idx)\ListSample(), Index)
      *pFactor.__FactorInfo = _DimSample(Idx)\ListSample()
      Text$ = Str(Idx)+": "
      For r = 0 To #MAI_Sample-1
         Convolue.f = _DimWeight(r, 0) * -1
         For c = 1 To #MAI_Factor
            Convolue + _DimWeight(r, c) * (*pFactor\Pixels[c-1])
         Next
         OutResult.f = 1/(1+Exp(-Convolue))
         If OutResult >= #MAI_Judged
            Text$ + RSet(StrF(OutResult*100, 2)+"%", 6, " ") +", "
         Else
            Text$ + RSet("-", 6, " ") +", "
         EndIf
      Next
      Debug Text$
   Next
   Debug "====="
EndProcedure



MAI_LoadSampleImage("训练样本_数字.png")
MAI_TrainSampleImage(10)
MAI_IdentifySample()

_________________
I came to the ancient oriental country - China
I will PureBasic called B++


Top
 Profile  
Reply with quote  
 Post subject: Re: 20+ lines can recognize handwritten characters for AI co
PostPosted: Sun Apr 30, 2017 12:48 pm 
Offline
User
User

Joined: Sun Apr 30, 2017 12:27 pm
Posts: 16
Very good!Where is your training picture?


Top
 Profile  
Reply with quote  
 Post subject: Re: 20+ lines can recognize handwritten characters for AI co
PostPosted: Sun Apr 30, 2017 1:53 pm 
Offline
User
User

Joined: Fri Jan 28, 2011 12:26 pm
Posts: 29
Poplar wrote:
Very good!Where is your training picture?


http://blog.sina.com.cn/s/blog_4e4522ce0102wrsr.html

_________________
I came to the ancient oriental country - China
I will PureBasic called B++


Top
 Profile  
Reply with quote  
 Post subject: Re: 20+ lines can recognize handwritten characters for AI co
PostPosted: Sun Apr 30, 2017 2:52 pm 
Offline
Addict
Addict
User avatar

Joined: Sat Feb 19, 2005 2:46 pm
Posts: 1792
Location: Pas-de-Calais, France
Great ! Thanks for sharing :)


Top
 Profile  
Reply with quote  
 Post subject: Re: 20+ lines can recognize handwritten characters for AI co
PostPosted: Sun Apr 30, 2017 4:15 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Thu Apr 30, 2009 5:23 pm
Posts: 300
Location: Côtes d'Azur, France
Good job ! :shock:

_________________
There are 2 methods to program bugless.
But only the third works fine.

Win10, Pb x86 5.70 LTS


Top
 Profile  
Reply with quote  
 Post subject: Re: 20+ lines can recognize handwritten characters for AI co
PostPosted: Mon May 01, 2017 8:02 pm 
Offline
Addict
Addict
User avatar

Joined: Sun Nov 05, 2006 11:42 pm
Posts: 4486
Location: Lyon - France
Works fine :shock:
Thanks for sharing 8)

_________________
ImageThe happiness is a road...
Not a destination


Top
 Profile  
Reply with quote  
 Post subject: Re: 20+ lines can recognize handwritten characters for AI co
PostPosted: Tue May 02, 2017 1:39 am 
Offline
User
User

Joined: Fri Jan 28, 2011 12:26 pm
Posts: 29
上述代码的神经元模型讲解!
The above code of the neuron model is explained!

http://blog.sina.com.cn/s/blog_4e4522ce0102wrtm.html

_________________
I came to the ancient oriental country - China
I will PureBasic called B++


Top
 Profile  
Reply with quote  
 Post subject: Re: 20+ lines can recognize handwritten characters for AI co
PostPosted: Tue May 02, 2017 8:16 am 
Offline
Addict
Addict

Joined: Sat Mar 02, 2013 9:17 am
Posts: 931
Very cool !

_________________
http://www.nachtoptik.de


Top
 Profile  
Reply with quote  
 Post subject: Re: 20+ lines can recognize handwritten characters for AI co
PostPosted: Tue May 02, 2017 9:59 am 
Offline
Addict
Addict
User avatar

Joined: Sun Nov 05, 2006 11:42 pm
Posts: 4486
Location: Lyon - France
Miloo wrote:
The above code of the neuron model is explained!
Like say a french expression : "It's chinese for us" :wink: :lol:

_________________
ImageThe happiness is a road...
Not a destination


Top
 Profile  
Reply with quote  
 Post subject: Re: 20+ lines can recognize handwritten characters for AI co
PostPosted: Tue May 02, 2017 2:25 pm 
Offline
User
User

Joined: Fri Jan 28, 2011 12:26 pm
Posts: 29
Kwai chang caine wrote:
Miloo wrote:
The above code of the neuron model is explained!
Like say a french expression : "It's chinese for us" :wink: :lol:

My English teacher is: Google translation :D :D :D

_________________
I came to the ancient oriental country - China
I will PureBasic called B++


Top
 Profile  
Reply with quote  
 Post subject: Re: 20+ lines can recognize handwritten characters for AI co
PostPosted: Tue May 02, 2017 2:48 pm 
Offline
Addict
Addict
User avatar

Joined: Sun Nov 05, 2006 11:42 pm
Posts: 4486
Location: Lyon - France
You know, numerous members of this forum have GOOGLE like friend too :wink:
Google is when even strong, because Chinese is really a hard language to translate :shock:
Also strong that your splendid code for recognize number in a picture 8)

_________________
ImageThe happiness is a road...
Not a destination


Top
 Profile  
Reply with quote  
 Post subject: Re: 20+ lines can recognize handwritten characters for AI co
PostPosted: Tue May 02, 2017 5:04 pm 
Offline
User
User

Joined: Fri Jan 28, 2011 12:26 pm
Posts: 29
Thank you for your praise

_________________
I came to the ancient oriental country - China
I will PureBasic called B++


Top
 Profile  
Reply with quote  
 Post subject: Re: 20+ lines can recognize handwritten characters for AI co
PostPosted: Sat May 06, 2017 6:36 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Tue Sep 22, 2009 10:41 pm
Posts: 421
It is a good start of characters recognizing.
这是人物识别的一个很好的开始。


Top
 Profile  
Reply with quote  
 Post subject: Re: 20+ lines can recognize handwritten characters for AI co
PostPosted: Sun May 07, 2017 6:17 pm 
Offline
User
User

Joined: Fri Jan 28, 2011 12:26 pm
Posts: 29
Olliv wrote:
It is a good start of characters recognizing.
这是人物识别的一个很好的开始。

A few days later, will release more heavy AI source code
过几天,还会发布更重磅的AI源代码。

_________________
I came to the ancient oriental country - China
I will PureBasic called B++


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 14 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: CDXbow and 11 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye