# PureBasic Forum

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

 All times are UTC + 1 hour

 Page 1 of 1 [ 14 posts ]
 Print view Previous topic | Next topic
Author Message
 Post subject: 20+ lines can recognize handwritten characters for AI codePosted: Sun Apr 30, 2017 8:36 am
 User

Joined: Fri Jan 28, 2011 12:26 pm
Posts: 29

The neural network model section only has 20+ lines of code!

But it can identify the handwriting 0-9 of these ten figures!

Handbook for handwriting used for training:

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) ;为每个数字设置一个权限矩阵

; 样本图片采样
;20x10数字阵,像素大小28x28
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
;==============
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_TrainSampleImage(10)
MAI_IdentifySample()

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

Top

 Post subject: Re: 20+ lines can recognize handwritten characters for AI coPosted: Sun Apr 30, 2017 12:48 pm
 User

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

Top

 Post subject: Re: 20+ lines can recognize handwritten characters for AI coPosted: Sun Apr 30, 2017 1:53 pm
 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

 Post subject: Re: 20+ lines can recognize handwritten characters for AI coPosted: Sun Apr 30, 2017 2:52 pm

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

Top

 Post subject: Re: 20+ lines can recognize handwritten characters for AI coPosted: Sun Apr 30, 2017 4:15 pm
 Enthusiast

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

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

Win10, Pb x86 5.70 LTS

Top

 Post subject: Re: 20+ lines can recognize handwritten characters for AI coPosted: Mon May 01, 2017 8:02 pm

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

_________________
Not a destination

Top

 Post subject: Re: 20+ lines can recognize handwritten characters for AI coPosted: Tue May 02, 2017 1:39 am
 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

 Post subject: Re: 20+ lines can recognize handwritten characters for AI coPosted: Tue May 02, 2017 8:16 am

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

_________________
http://www.nachtoptik.de

Top

 Post subject: Re: 20+ lines can recognize handwritten characters for AI coPosted: Tue May 02, 2017 9:59 am

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"

_________________
Not a destination

Top

 Post subject: Re: 20+ lines can recognize handwritten characters for AI coPosted: Tue May 02, 2017 2:25 pm
 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"

My English teacher is: Google translation

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

Top

 Post subject: Re: 20+ lines can recognize handwritten characters for AI coPosted: Tue May 02, 2017 2:48 pm

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
Google is when even strong, because Chinese is really a hard language to translate
Also strong that your splendid code for recognize number in a picture

_________________
Not a destination

Top

 Post subject: Re: 20+ lines can recognize handwritten characters for AI coPosted: Tue May 02, 2017 5:04 pm
 User

Joined: Fri Jan 28, 2011 12:26 pm
Posts: 29

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

Top

 Post subject: Re: 20+ lines can recognize handwritten characters for AI coPosted: Sat May 06, 2017 6:36 pm
 Enthusiast

Joined: Tue Sep 22, 2009 10:41 pm
Posts: 421
It is a good start of characters recognizing.

Top

 Post subject: Re: 20+ lines can recognize handwritten characters for AI coPosted: Sun May 07, 2017 6:17 pm
 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

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

Top

 Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending
 Page 1 of 1 [ 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 forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forum

Search for:
 Jump to:  Select a forum ------------------ PureBasic    Coding Questions    Game Programming    3D Programming    Assembly Programming    The PureBasic Editor    The PureBasic Form Designer    General Discussion    Feature Requests and Wishlists    Tricks 'n' Tips Bug Reports    Bugs - Windows    Bugs - Linux    Bugs - Mac OSX    Bugs - Documentation OS Specific    AmigaOS    Linux    Windows    Mac OSX Miscellaneous    Announcement    Off Topic Showcase    Applications - Feedback and Discussion    PureFORM & JaPBe    TailBite