20+ lines can recognize handwritten characters for AI code

Just starting out? Need help? Post your questions and find answers here.
MiLoo
User
User
Posts: 47
Joined: Fri Jan 28, 2011 12:26 pm

20+ lines can recognize handwritten characters for AI code

Post by MiLoo »

神经网络模型部分只有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: Select all

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: Select all

; **************************************************
; *****   手写数字智能识别简例--单层神经网络   *****
; *****           迷路仟 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++
Poplar
User
User
Posts: 16
Joined: Sun Apr 30, 2017 12:27 pm

Re: 20+ lines can recognize handwritten characters for AI co

Post by Poplar »

Very good!Where is your training picture?
MiLoo
User
User
Posts: 47
Joined: Fri Jan 28, 2011 12:26 pm

Re: 20+ lines can recognize handwritten characters for AI co

Post by MiLoo »

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++
User avatar
djes
Addict
Addict
Posts: 1806
Joined: Sat Feb 19, 2005 2:46 pm
Location: Pas-de-Calais, France

Re: 20+ lines can recognize handwritten characters for AI co

Post by djes »

Great ! Thanks for sharing :)
User avatar
Fig
Enthusiast
Enthusiast
Posts: 351
Joined: Thu Apr 30, 2009 5:23 pm
Location: Côtes d'Azur, France

Re: 20+ lines can recognize handwritten characters for AI co

Post by Fig »

Good job ! :shock:
There are 2 methods to program bugless.
But only the third works fine.

Win10, Pb x64 5.71 LTS
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5350
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: 20+ lines can recognize handwritten characters for AI co

Post by Kwai chang caine »

Works fine :shock:
Thanks for sharing 8)
ImageThe happiness is a road...
Not a destination
MiLoo
User
User
Posts: 47
Joined: Fri Jan 28, 2011 12:26 pm

Re: 20+ lines can recognize handwritten characters for AI co

Post by MiLoo »

上述代码的神经元模型讲解!
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++
walbus
Addict
Addict
Posts: 929
Joined: Sat Mar 02, 2013 9:17 am

Re: 20+ lines can recognize handwritten characters for AI co

Post by walbus »

Very cool !
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5350
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: 20+ lines can recognize handwritten characters for AI co

Post by Kwai chang caine »

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
MiLoo
User
User
Posts: 47
Joined: Fri Jan 28, 2011 12:26 pm

Re: 20+ lines can recognize handwritten characters for AI co

Post by MiLoo »

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++
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5350
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: 20+ lines can recognize handwritten characters for AI co

Post by Kwai chang caine »

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
MiLoo
User
User
Posts: 47
Joined: Fri Jan 28, 2011 12:26 pm

Re: 20+ lines can recognize handwritten characters for AI co

Post by MiLoo »

Thank you for your praise
I came to the ancient oriental country - China
I will PureBasic called B++
User avatar
Olliv
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Sep 22, 2009 10:41 pm

Re: 20+ lines can recognize handwritten characters for AI co

Post by Olliv »

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

Re: 20+ lines can recognize handwritten characters for AI co

Post by MiLoo »

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++
Post Reply