-
Notifications
You must be signed in to change notification settings - Fork 9.9k
Fix for LSTM Diplopia issue #3476
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
woodjohndavid
wants to merge
9
commits into
tesseract-ocr:main
Choose a base branch
from
woodjohndavid:JDWDIPLOPIA
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
3b7842b
recodebeam changes
woodjohndavid b296681
Update recodebeam.cpp
woodjohndavid d9244fc
diplopia interim solution
woodjohndavid ae85bc4
diplopia interim solution final
woodjohndavid a8af23f
Style Changes Per stweil comments
woodjohndavid bf6d32e
Apply suggestions from code review
stweil 5b445e1
Apply suggestions from code review
stweil f8f7a3f
Apply suggestions from code review
stweil 4035a7f
Merge branch 'main' into JDWDIPLOPIA
woodjohndavid File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -40,6 +40,11 @@ const int RecodeBeamSearch::kBeamWidths[RecodedCharID::kMaxCodeLen + 1] = { | |||||||||||
|
||||||||||||
static const char *kNodeContNames[] = {"Anything", "OnlyDup", "NoDup"}; | ||||||||||||
|
||||||||||||
// the minimum diplopia key is the minimum score (key) from | ||||||||||||
// the network output to qualify as a likely 'real' character | ||||||||||||
// for the purposes of identifying possible diplopia | ||||||||||||
static const float kMinDiplopiaKey = 0.25; | ||||||||||||
stweil marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||
|
||||||||||||
// Prints debug details of the node. | ||||||||||||
void RecodeNode::Print(int null_char, const UNICHARSET &unicharset, int depth) const { | ||||||||||||
if (code == null_char) { | ||||||||||||
|
@@ -65,6 +70,9 @@ RecodeBeamSearch::RecodeBeamSearch(const UnicharCompress &recoder, int null_char | |||||||||||
, beam_size_(0) | ||||||||||||
, top_code_(-1) | ||||||||||||
, second_code_(-1) | ||||||||||||
, in_possible_diplopia_(false) | ||||||||||||
, first_diplopia_code_(-1) | ||||||||||||
, second_diplopia_code_(-1) | ||||||||||||
stweil marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||
, dict_(dict) | ||||||||||||
, space_delimited_(true) | ||||||||||||
, is_simple_text_(simple_text) | ||||||||||||
|
@@ -182,7 +190,7 @@ RecodeBeamSearch::combineSegmentedTimesteps( | |||||||||||
|
||||||||||||
void RecodeBeamSearch::calculateCharBoundaries(std::vector<int> *starts, std::vector<int> *ends, | ||||||||||||
std::vector<int> *char_bounds_, int maxWidth) { | ||||||||||||
char_bounds_->push_back(0); | ||||||||||||
char_bounds_->push_back((*starts)[0]); | ||||||||||||
stweil marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||
for (int i = 0; i < ends->size(); ++i) { | ||||||||||||
int middle = ((*starts)[i + 1] - (*ends)[i]) / 2; | ||||||||||||
char_bounds_->push_back((*ends)[i] + middle); | ||||||||||||
|
@@ -567,8 +575,8 @@ void RecodeBeamSearch::ExtractPathAsUnicharIds(const std::vector<const RecodeNod | |||||||||||
} | ||||||||||||
rating -= cert; | ||||||||||||
} | ||||||||||||
starts.push_back(t); | ||||||||||||
if (t < width) { | ||||||||||||
starts.push_back(t); | ||||||||||||
int unichar_id = best_nodes[t]->unichar_id; | ||||||||||||
if (unichar_id == UNICHAR_SPACE && !certs->empty() && best_nodes[t]->permuter != NO_PERM) { | ||||||||||||
// All the rating and certainty go on the previous character except | ||||||||||||
|
@@ -582,16 +590,18 @@ void RecodeBeamSearch::ExtractPathAsUnicharIds(const std::vector<const RecodeNod | |||||||||||
} | ||||||||||||
unichar_ids->push_back(unichar_id); | ||||||||||||
xcoords->push_back(t); | ||||||||||||
do { | ||||||||||||
double cert = best_nodes[t++]->certainty; | ||||||||||||
t++; | ||||||||||||
while (t < width && best_nodes[t]->duplicate) { | ||||||||||||
double cert = best_nodes[t]->certainty; | ||||||||||||
// Special-case NO-PERM space to forget the certainty of the previous | ||||||||||||
// nulls. See long comment in ContinueContext. | ||||||||||||
if (cert < certainty || | ||||||||||||
(unichar_id == UNICHAR_SPACE && best_nodes[t - 1]->permuter == NO_PERM)) { | ||||||||||||
certainty = cert; | ||||||||||||
} | ||||||||||||
rating -= cert; | ||||||||||||
} while (t < width && best_nodes[t]->duplicate); | ||||||||||||
t++; | ||||||||||||
} | ||||||||||||
ends.push_back(t); | ||||||||||||
certs->push_back(certainty); | ||||||||||||
ratings->push_back(rating); | ||||||||||||
|
@@ -657,20 +667,46 @@ void RecodeBeamSearch::ComputeTopN(const float *outputs, int num_outputs, int to | |||||||||||
} | ||||||||||||
} | ||||||||||||
} | ||||||||||||
float top_key = 0.0F; | ||||||||||||
stweil marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||
float second_key = 0.0F; | ||||||||||||
stweil marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||
bool found_first_code = false; | ||||||||||||
bool found_second_code = false; | ||||||||||||
while (!top_heap_.empty()) { | ||||||||||||
TopPair entry; | ||||||||||||
top_heap_.Pop(&entry); | ||||||||||||
if (in_possible_diplopia_ && entry.data() == first_diplopia_code_) | ||||||||||||
stweil marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||
found_first_code = true; | ||||||||||||
if (in_possible_diplopia_ && entry.data() == second_diplopia_code_) | ||||||||||||
found_second_code = true; | ||||||||||||
if (top_heap_.size() > 1) { | ||||||||||||
top_n_flags_[entry.data()] = TN_TOPN; | ||||||||||||
} else { | ||||||||||||
top_n_flags_[entry.data()] = TN_TOP2; | ||||||||||||
if (top_heap_.empty()) { | ||||||||||||
top_code_ = entry.data(); | ||||||||||||
top_key = entry.key(); | ||||||||||||
} else { | ||||||||||||
second_code_ = entry.data(); | ||||||||||||
second_key = entry.key(); | ||||||||||||
} | ||||||||||||
} | ||||||||||||
} | ||||||||||||
// need to identify if we are in a potential diplopia situation | ||||||||||||
// or if we already are, then determine if it is ended | ||||||||||||
stweil marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||
if (in_possible_diplopia_) { | ||||||||||||
if (!found_first_code && !found_second_code){ | ||||||||||||
stweil marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||
in_possible_diplopia_ = false; | ||||||||||||
first_diplopia_code_ = -1; | ||||||||||||
second_diplopia_code_ = -1; | ||||||||||||
} | ||||||||||||
} | ||||||||||||
if (!in_possible_diplopia_) { | ||||||||||||
if (top_code_ != null_char_ && second_code_ != null_char_ && top_key > kMinDiplopiaKey && second_key > kMinDiplopiaKey){ | ||||||||||||
stweil marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||
in_possible_diplopia_ = true; | ||||||||||||
first_diplopia_code_ = top_code_; | ||||||||||||
second_diplopia_code_ = second_code_; | ||||||||||||
} | ||||||||||||
} | ||||||||||||
top_n_flags_[null_char_] = TN_TOP2; | ||||||||||||
} | ||||||||||||
|
||||||||||||
|
@@ -1138,6 +1174,10 @@ void RecodeBeamSearch::PushHeapIfBetter(int max_size, int code, int unichar_id, | |||||||||||
if (UpdateHeapIfMatched(&node, heap)) { | ||||||||||||
return; | ||||||||||||
} | ||||||||||||
// check to see if node is possible diplopia | ||||||||||||
stweil marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||
if (!AddToHeapIsAllowed(&node)) { | ||||||||||||
return; | ||||||||||||
} | ||||||||||||
RecodePair entry(score, node); | ||||||||||||
heap->Push(&entry); | ||||||||||||
ASSERT_HOST(entry.data().dawgs == nullptr); | ||||||||||||
|
@@ -1189,6 +1229,20 @@ bool RecodeBeamSearch::UpdateHeapIfMatched(RecodeNode *new_node, RecodeHeap *hea | |||||||||||
return false; | ||||||||||||
} | ||||||||||||
|
||||||||||||
// Determines if node can be added to heap based on possible diplopia status | ||||||||||||
stweil marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||
bool RecodeBeamSearch::AddToHeapIsAllowed(RecodeNode *new_node) { | ||||||||||||
if (!in_possible_diplopia_) | ||||||||||||
return true; | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
const RecodeNode *prev_node = new_node->prev; | ||||||||||||
if (prev_node != nullptr && prev_node->code == first_diplopia_code_ && new_node->code == second_diplopia_code_) { | ||||||||||||
return false; | ||||||||||||
} | ||||||||||||
if (prev_node != nullptr && prev_node->code == second_diplopia_code_ && new_node->code == first_diplopia_code_) { | ||||||||||||
return false; | ||||||||||||
} | ||||||||||||
return true; | ||||||||||||
} | ||||||||||||
|
||||||||||||
// Computes and returns the code-hash for the given code and prev. | ||||||||||||
uint64_t RecodeBeamSearch::ComputeCodeHash(int code, bool dup, const RecodeNode *prev) const { | ||||||||||||
uint64_t hash = prev == nullptr ? 0 : prev->code_hash; | ||||||||||||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.