Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
SimpleHTR
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Fabian Mersch
SimpleHTR
Commits
d807bc44
Commit
d807bc44
authored
3 years ago
by
Harald Scheidl
Browse files
Options
Downloads
Patches
Plain Diff
in validation mode use charset from trained model and not from dataset, see issue #127
parent
7f26b321
Branches
pre-training
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
model/.gitignore
+3
-2
3 additions, 2 deletions
model/.gitignore
src/main.py
+33
-21
33 additions, 21 deletions
src/main.py
with
36 additions
and
23 deletions
model/.gitignore
+
3
−
2
View file @
d807bc44
# Ignore everything in this directory
*
# Except this file
# Except this file
and wordCharList.txt
!.gitignore
wordCharList.txt
\ No newline at end of file
This diff is collapsed.
Click to expand it.
src/main.py
+
33
−
21
View file @
d807bc44
...
...
@@ -36,6 +36,11 @@ def write_summary(char_error_rates: List[float], word_accuracies: List[float]) -
json
.
dump
({
'
charErrorRates
'
:
char_error_rates
,
'
wordAccuracies
'
:
word_accuracies
},
f
)
def
char_list_from_file
()
->
List
[
str
]:
with
open
(
FilePaths
.
fn_char_list
)
as
f
:
return
list
(
f
.
read
())
def
train
(
model
:
Model
,
loader
:
DataLoaderIAM
,
line_mode
:
bool
,
...
...
@@ -45,7 +50,7 @@ def train(model: Model,
summary_char_error_rates
=
[]
summary_word_accuracies
=
[]
preprocessor
=
Preprocessor
(
get_img_size
(
line_mode
),
data_augmentation
=
True
,
line_mode
=
line_mode
)
best_char_error_rate
=
float
(
'
inf
'
)
# best val
d
iation character error rate
best_char_error_rate
=
float
(
'
inf
'
)
# best vali
d
ation character error rate
no_improvement_since
=
0
# number of epochs no improvement of character error rate occurred
# stop training after this number of epochs without improvement
while
True
:
...
...
@@ -133,8 +138,8 @@ def infer(model: Model, fn_img: Path) -> None:
print
(
f
'
Probability:
{
probability
[
0
]
}
'
)
def
main
()
:
"""
Main function
.
"""
def
parse_args
()
->
argparse
.
Namespace
:
"""
Parses arguments from the command line
.
"""
parser
=
argparse
.
ArgumentParser
()
parser
.
add_argument
(
'
--mode
'
,
choices
=
[
'
train
'
,
'
validate
'
,
'
infer
'
],
default
=
'
infer
'
)
...
...
@@ -146,41 +151,48 @@ def main():
parser
.
add_argument
(
'
--img_file
'
,
help
=
'
Image used for inference.
'
,
type
=
Path
,
default
=
'
../data/word.png
'
)
parser
.
add_argument
(
'
--early_stopping
'
,
help
=
'
Early stopping epochs.
'
,
type
=
int
,
default
=
25
)
parser
.
add_argument
(
'
--dump
'
,
help
=
'
Dump output of NN to CSV file(s).
'
,
action
=
'
store_true
'
)
args
=
parser
.
parse_args
()
# set chosen CTC decoder
return
parser
.
parse_args
()
def
main
():
"""
Main function.
"""
# parse arguments and set CTC decoder
args
=
parse_args
()
decoder_mapping
=
{
'
bestpath
'
:
DecoderType
.
BestPath
,
'
beamsearch
'
:
DecoderType
.
BeamSearch
,
'
wordbeamsearch
'
:
DecoderType
.
WordBeamSearch
}
decoder_type
=
decoder_mapping
[
args
.
decoder
]
# train or validate on IAM dataset
if
args
.
mode
in
[
'
train
'
,
'
validate
'
]:
# load training data, create TF model
# train the model
if
args
.
mode
==
'
train
'
:
loader
=
DataLoaderIAM
(
args
.
data_dir
,
args
.
batch_size
,
fast
=
args
.
fast
)
char_list
=
loader
.
char_list
# when in line mode, take care to have a whitespace in the char list
char_list
=
loader
.
char_list
if
args
.
line_mode
and
'
'
not
in
char_list
:
char_list
=
[
'
'
]
+
char_list
# save characters of model for inference mode
open
(
FilePaths
.
fn_char_list
,
'
w
'
).
write
(
''
.
join
(
char_list
))
# save characters and words
with
open
(
FilePaths
.
fn_char_list
,
'
w
'
)
as
f
:
f
.
write
(
''
.
join
(
char_list
))
# save words contained in dataset into file
open
(
FilePaths
.
fn_corpus
,
'
w
'
)
.
write
(
'
'
.
join
(
loader
.
train_words
+
loader
.
validation_words
))
with
open
(
FilePaths
.
fn_corpus
,
'
w
'
)
as
f
:
f
.
write
(
'
'
.
join
(
loader
.
train_words
+
loader
.
validation_words
))
# execute training or validation
if
args
.
mode
==
'
train
'
:
model
=
Model
(
char_list
,
decoder_type
)
train
(
model
,
loader
,
line_mode
=
args
.
line_mode
,
early_stopping
=
args
.
early_stopping
)
# evaluate it on the validation set
elif
args
.
mode
==
'
validate
'
:
model
=
Model
(
char_list
,
decoder_type
,
must_restore
=
True
)
loader
=
DataLoaderIAM
(
args
.
data_dir
,
args
.
batch_size
,
fast
=
args
.
fast
)
model
=
Model
(
char_list_from_file
(),
decoder_type
,
must_restore
=
True
)
validate
(
model
,
loader
,
args
.
line_mode
)
# infer text on test image
elif
args
.
mode
==
'
infer
'
:
model
=
Model
(
list
(
open
(
FilePaths
.
fn_char_list
).
read
()
),
decoder_type
,
must_restore
=
True
,
dump
=
args
.
dump
)
model
=
Model
(
char_list_from_file
(
),
decoder_type
,
must_restore
=
True
,
dump
=
args
.
dump
)
infer
(
model
,
args
.
img_file
)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment