Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
O
opencv
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
opencv
Commits
622bd422
Commit
622bd422
authored
Aug 14, 2011
by
Alexander Mordvintsev
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
work on MLP sample in letter_recog.py (in progress...)
parent
638f3d31
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
41 additions
and
16 deletions
+41
-16
letter_recog.py
samples/python2/letter_recog.py
+41
-16
No files found.
samples/python2/letter_recog.py
View file @
622bd422
...
@@ -7,12 +7,28 @@ def load_base(fn):
...
@@ -7,12 +7,28 @@ def load_base(fn):
return
samples
,
responses
return
samples
,
responses
class
LetterStatModel
(
object
):
class
LetterStatModel
(
object
):
class_n
=
26
train_ratio
=
0.5
train_ratio
=
0.5
def
load
(
self
,
fn
):
def
load
(
self
,
fn
):
self
.
model
.
load
(
fn
)
self
.
model
.
load
(
fn
)
def
save
(
self
,
fn
):
def
save
(
self
,
fn
):
self
.
model
.
save
(
fn
)
self
.
model
.
save
(
fn
)
def
unroll_samples
(
self
,
samples
):
sample_n
,
var_n
=
samples
.
shape
new_samples
=
np
.
zeros
((
sample_n
*
self
.
class_n
,
var_n
+
1
),
np
.
float32
)
new_samples
[:,:
-
1
]
=
np
.
repeat
(
samples
,
self
.
class_n
,
axis
=
0
)
new_samples
[:,
-
1
]
=
np
.
tile
(
np
.
arange
(
self
.
class_n
),
sample_n
)
return
new_samples
def
unroll_responses
(
self
,
responses
):
sample_n
=
len
(
responses
)
new_responses
=
np
.
zeros
(
sample_n
*
self
.
class_n
,
np
.
int32
)
resp_idx
=
np
.
int32
(
responses
+
np
.
arange
(
sample_n
)
*
self
.
class_n
)
new_responses
[
resp_idx
]
=
1
return
new_responses
class
RTrees
(
LetterStatModel
):
class
RTrees
(
LetterStatModel
):
def
__init__
(
self
):
def
__init__
(
self
):
self
.
model
=
cv2
.
RTrees
()
self
.
model
=
cv2
.
RTrees
()
...
@@ -43,7 +59,6 @@ class KNearest(LetterStatModel):
...
@@ -43,7 +59,6 @@ class KNearest(LetterStatModel):
class
Boost
(
LetterStatModel
):
class
Boost
(
LetterStatModel
):
def
__init__
(
self
):
def
__init__
(
self
):
self
.
model
=
cv2
.
Boost
()
self
.
model
=
cv2
.
Boost
()
self
.
class_n
=
26
def
train
(
self
,
samples
,
responses
):
def
train
(
self
,
samples
,
responses
):
sample_n
,
var_n
=
samples
.
shape
sample_n
,
var_n
=
samples
.
shape
...
@@ -60,20 +75,6 @@ class Boost(LetterStatModel):
...
@@ -60,20 +75,6 @@ class Boost(LetterStatModel):
pred
=
pred
.
reshape
(
-
1
,
self
.
class_n
)
.
argmax
(
1
)
pred
=
pred
.
reshape
(
-
1
,
self
.
class_n
)
.
argmax
(
1
)
return
pred
return
pred
def
unroll_samples
(
self
,
samples
):
sample_n
,
var_n
=
samples
.
shape
new_samples
=
np
.
zeros
((
sample_n
*
self
.
class_n
,
var_n
+
1
),
np
.
float32
)
new_samples
[:,:
-
1
]
=
np
.
repeat
(
samples
,
self
.
class_n
,
axis
=
0
)
new_samples
[:,
-
1
]
=
np
.
tile
(
np
.
arange
(
self
.
class_n
),
sample_n
)
return
new_samples
def
unroll_responses
(
self
,
responses
):
sample_n
=
len
(
responses
)
new_responses
=
np
.
zeros
(
sample_n
*
self
.
class_n
,
np
.
int32
)
resp_idx
=
np
.
int32
(
responses
+
np
.
arange
(
sample_n
)
*
self
.
class_n
)
new_responses
[
resp_idx
]
=
1
return
new_responses
class
SVM
(
LetterStatModel
):
class
SVM
(
LetterStatModel
):
train_ratio
=
0.1
train_ratio
=
0.1
...
@@ -89,12 +90,36 @@ class SVM(LetterStatModel):
...
@@ -89,12 +90,36 @@ class SVM(LetterStatModel):
def
predict
(
self
,
samples
):
def
predict
(
self
,
samples
):
return
np
.
float32
(
[
self
.
model
.
predict
(
s
)
for
s
in
samples
]
)
return
np
.
float32
(
[
self
.
model
.
predict
(
s
)
for
s
in
samples
]
)
class
MLP
(
LetterStatModel
):
def
__init__
(
self
):
self
.
model
=
cv2
.
ANN_MLP
()
def
train
(
self
,
samples
,
responses
):
sample_n
,
var_n
=
samples
.
shape
new_responses
=
self
.
unroll_responses
(
responses
)
.
reshape
(
-
1
,
self
.
class_n
)
layer_sizes
=
np
.
int32
([
var_n
,
100
,
100
,
self
.
class_n
])
self
.
model
.
create
(
layer_sizes
)
# CvANN_MLP_TrainParams::BACKPROP,0.001
params
=
dict
(
term_crit
=
(
cv2
.
TERM_CRITERIA_COUNT
,
300
,
0.01
),
train_method
=
cv2
.
ANN_MLP_TRAIN_PARAMS_BACKPROP
,
bp_dw_scale
=
0.001
,
bp_moment_scale
=
0.0
)
self
.
model
.
train
(
samples
,
np
.
float32
(
new_responses
),
None
,
params
=
params
)
def
predict
(
self
,
samples
):
pass
#return np.float32( [self.model.predict(s) for s in samples] )
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
import
getopt
import
getopt
import
sys
import
sys
models
=
[
RTrees
,
KNearest
,
Boost
,
SVM
]
# MLP,
NBayes
models
=
[
RTrees
,
KNearest
,
Boost
,
SVM
,
MLP
]
#
NBayes
models
=
dict
(
[(
cls
.
__name__
.
lower
(),
cls
)
for
cls
in
models
]
)
models
=
dict
(
[(
cls
.
__name__
.
lower
(),
cls
)
for
cls
in
models
]
)
print
'USAGE: letter_recog.py [--model <model>] [--data <data fn>] [--load <model fn>] [--save <model fn>]'
print
'USAGE: letter_recog.py [--model <model>] [--data <data fn>] [--load <model fn>] [--save <model fn>]'
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment