Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
D
DAN
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Package registry
Container Registry
Operate
Terraform modules
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Automatic Text Recognition
DAN
Commits
e9aa1084
Commit
e9aa1084
authored
1 year ago
by
Solene Tarride
Browse files
Options
Downloads
Patches
Plain Diff
Add CTC frame between each frames
parent
16d83adb
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
dan/ocr/decoder.py
+15
-13
15 additions, 13 deletions
dan/ocr/decoder.py
with
15 additions
and
13 deletions
dan/ocr/decoder.py
+
15
−
13
View file @
e9aa1084
...
...
@@ -516,17 +516,19 @@ class CTCLanguageDecoder:
# column with 1 probability on CTC token
ctc_probs
=
(
torch
.
ones
((
batch_size
,
1
,
n_tokens
),
dtype
=
torch
.
float32
)
*
0.1
/
n_tokens
torch
.
ones
((
batch_size
,
1
,
n_tokens
),
dtype
=
torch
.
float32
)
*
0.01
/
(
n_tokens
-
1
)
)
ctc_probs
[:,
:,
self
.
ctc_id
]
=
0.9
ctc_probs
[:,
:,
self
.
ctc_id
]
=
0.9
9
ctc_probs
=
ctc_probs
.
log
()
for
i
in
range
(
n_frames
-
1
):
batch_features
=
torch
.
cat
(
[
batch_features
[:,
2
*
i
+
1
:,
:],
ctc_probs
,
batch_features
[:,
:
2
*
i
+
1
,
:],
ctc_probs
,
batch_features
[:,
2
*
i
+
1
:,
:],
],
dim
=
1
,
)
...
...
@@ -534,14 +536,15 @@ class CTCLanguageDecoder:
def
post_process
(
self
,
hypotheses
):
"""
Post-process hypotheses to output JSON
Post-process hypotheses to output JSON
. Exports only the best hypothesis for each image.
"""
out
=
{}
#
Export only the best hypothesis
#
Replace <space> by an actual space and format string
out
[
"
text
"
]
=
[
""
.
join
(
hypothesis
[
0
].
words
).
replace
(
self
.
space_token
,
"
"
)
for
hypothesis
in
hypotheses
]
# Normalize confidence score
out
[
"
confidence
"
]
=
[
np
.
exp
(
hypothesis
[
0
].
score
/
hypothesis
[
0
].
timesteps
[
-
1
].
item
())
for
hypothesis
in
hypotheses
...
...
@@ -552,13 +555,12 @@ class CTCLanguageDecoder:
"""
Decode a feature vector using n-gram language modelling.
Args:
features (
Any
): feature vector of size (
n_frame,
batch_size, n_tokens).
Can be either a torch.tensor or a torch.nn.utils.rnn.PackedSequence
features (
torch.tensor
): feature vector of size (batch_size, n_tokens
, n_frame
).
batch_sizes (Union[List, torch.tensor]): actual length of predictions
Returns:
out (Dict[str, List]): a dictionary containing the hypothesis (the list of decoded tokens).
There is no character-based probability.
out (Dict[List]): a dictionary containing the hypotheses.
"""
# Reshape from (
n_frame,
batch_size, n_tokens) to (batch_size, n_frame, n_tokens)
# Reshape from (batch_size, n_tokens
, n_frames
) to (batch_size, n_frame
s
, n_tokens)
batch_features
=
batch_features
.
permute
((
0
,
2
,
1
))
# Apply temperature scaling
...
...
@@ -566,8 +568,8 @@ class CTCLanguageDecoder:
# Apply log softmax
batch_features
=
torch
.
nn
.
functional
.
log_softmax
(
batch_features
,
dim
=-
1
)
#
batch_features = self.add_ctc_frames(batch_features)
#
batch_sizes =
batch_features.shape[0]
batch_features
=
self
.
add_ctc_frames
(
batch_features
)
batch_sizes
*
=
2
# No GPU support for torchaudio's ctc_decoder
device
=
torch
.
device
(
"
cpu
"
)
...
...
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