-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Integrated accumulated Symbol Choice in the Choice Iterator and made …
…the api lstm_choice_mode independent Signed-off-by: Noah Metzger <[email protected]>
- 5.5.0
- 5.4.1
- 5.4.0
- 5.4.0-rc2
- 5.4.0-rc1
- 5.3.4
- 5.3.3
- 5.3.2
- 5.3.1
- 5.3.0
- 5.3.0-rc1
- 5.2.0
- 5.1.0
- 5.0.1
- 5.0.0
- 5.0.0-rc3
- 5.0.0-rc2
- 5.0.0-rc1
- 5.0.0-beta-20210916
- 5.0.0-beta-20210815
- 5.0.0-alpha-20210401
- 5.0.0-alpha-20201231
- 5.0.0-alpha-20201224
- 5.0.0-alpha
- 4.1.3
- 4.1.2
- 4.1.1
- 4.1.1-rc2
- 4.1.1-rc1
- 4.1.0
- 4.1.0-rc4
- 4.1.0-rc3
- 4.1.0-rc2
1 parent
bc2b919
commit 5b3e2fe
Showing
10 changed files
with
174 additions
and
91 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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 |
---|---|---|
|
@@ -358,7 +358,17 @@ bool LTRResultIterator::SymbolIsDropcap() const { | |
ChoiceIterator::ChoiceIterator(const LTRResultIterator& result_it) { | ||
ASSERT_HOST(result_it.it_->word() != nullptr); | ||
word_res_ = result_it.it_->word(); | ||
oemLSTM_ = word_res_->tesseract->AnyLSTMLang(); | ||
oemLegacy_ = word_res_->tesseract->AnyTessLang(); | ||
BLOB_CHOICE_LIST* choices = nullptr; | ||
tstep_index_ = &result_it.blob_index_; | ||
if (oemLSTM_ && !oemLegacy_ && &word_res_->accumulated_timesteps != nullptr) { | ||
if (word_res_->leadingSpace) | ||
LSTM_choices_ = &word_res_->accumulated_timesteps[(*tstep_index_) + 1]; | ||
else | ||
LSTM_choices_ = &word_res_->accumulated_timesteps[*tstep_index_]; | ||
filterSpaces(); | ||
} | ||
if (word_res_->ratings != nullptr) | ||
choices = word_res_->GetBlobChoices(result_it.blob_index_); | ||
if (choices != nullptr && !choices->empty()) { | ||
|
@@ -367,49 +377,93 @@ ChoiceIterator::ChoiceIterator(const LTRResultIterator& result_it) { | |
} else { | ||
choice_it_ = nullptr; | ||
} | ||
if (&word_res_->symbol_steps != nullptr && !word_res_->symbol_steps.empty()) { | ||
symbol_step_it_ = word_res_->symbol_steps.begin(); | ||
if (LSTM_choices_ != nullptr && !LSTM_choices_->empty()) { | ||
LSTM_mode_ = true; | ||
LSTM_choice_it_ = LSTM_choices_->begin(); | ||
} | ||
} | ||
|
||
ChoiceIterator::~ChoiceIterator() { delete choice_it_; } | ||
|
||
// Moves to the next choice for the symbol and returns false if there | ||
// are none left. | ||
bool ChoiceIterator::Next() { | ||
if (choice_it_ == nullptr) return false; | ||
if (&word_res_->symbol_steps != nullptr) { | ||
if (symbol_step_it_ == word_res_->symbol_steps.end()) { | ||
symbol_step_it_ = word_res_->symbol_steps.begin(); | ||
if (LSTM_mode_) { | ||
if (LSTM_choice_it_ != LSTM_choices_->end() && | ||
next(LSTM_choice_it_) == LSTM_choices_->end()) { | ||
return false; | ||
} else { | ||
symbol_step_it_++; | ||
} | ||
++LSTM_choice_it_; | ||
return true; | ||
} | ||
} else { | ||
if (choice_it_ == nullptr) return false; | ||
choice_it_->forward(); | ||
return !choice_it_->cycled_list(); | ||
} | ||
choice_it_->forward(); | ||
return !choice_it_->cycled_list(); | ||
} | ||
|
||
// Returns the null terminated UTF-8 encoded text string for the current | ||
// choice. Do NOT use delete [] to free after use. | ||
const char* ChoiceIterator::GetUTF8Text() const { | ||
if (choice_it_ == nullptr) return nullptr; | ||
UNICHAR_ID id = choice_it_->data()->unichar_id(); | ||
return word_res_->uch_set->id_to_unichar_ext(id); | ||
if (LSTM_mode_) { | ||
std::pair<const char*, float> choice = *LSTM_choice_it_; | ||
return choice.first; | ||
} else { | ||
if (choice_it_ == nullptr) return nullptr; | ||
UNICHAR_ID id = choice_it_->data()->unichar_id(); | ||
return word_res_->uch_set->id_to_unichar_ext(id); | ||
} | ||
} | ||
|
||
// Returns the confidence of the current choice. | ||
// The number should be interpreted as a percent probability. (0.0f-100.0f) | ||
// Returns the confidence of the current choice depending on the used language | ||
// data. If only LSTM traineddata is used the value range is 0.0f - 1.0f. All | ||
// choices for one symbol should roughly add up to 1.0f. | ||
// If only traineddata of the legacy engine is used, the number should be | ||
// interpreted as a percent probability. (0.0f-100.0f) In this case probabilities | ||
// won't add up to 100. Each one stands on its own. | ||
float ChoiceIterator::Confidence() const { | ||
if (choice_it_ == nullptr) return 0.0f; | ||
float confidence = 100 + 5 * choice_it_->data()->certainty(); | ||
if (confidence < 0.0f) confidence = 0.0f; | ||
if (confidence > 100.0f) confidence = 100.0f; | ||
return confidence; | ||
if (LSTM_mode_) { | ||
std::pair<const char*, float> choice = *LSTM_choice_it_; | ||
return choice.second; | ||
} else { | ||
if (choice_it_ == nullptr) return 0.0f; | ||
float confidence = 100 + 5 * choice_it_->data()->certainty(); | ||
if (confidence < 0.0f) confidence = 0.0f; | ||
if (confidence > 100.0f) confidence = 100.0f; | ||
return confidence; | ||
} | ||
} | ||
|
||
// Returns the set of timesteps which belong to the current symbol | ||
std::vector<std::vector<std::pair<const char*, float>>>* | ||
ChoiceIterator::Timesteps() const { | ||
if (&word_res_->symbol_steps == nullptr) return nullptr; | ||
return &*symbol_step_it_; | ||
if (&word_res_->symbol_steps == nullptr || !LSTM_mode_) return nullptr; | ||
This comment has been minimized.
Sorry, something went wrong.
zdenop
Contributor
|
||
if (word_res_->leadingSpace) { | ||
return &word_res_->symbol_steps[*(tstep_index_) + 1]; | ||
} else { | ||
return &word_res_->symbol_steps[*tstep_index_]; | ||
} | ||
} | ||
|
||
void ChoiceIterator::filterSpaces() { | ||
if (LSTM_choices_->empty()) return; | ||
std::vector<std::pair<const char*, float>>::iterator it = | ||
LSTM_choices_->begin(); | ||
bool found_space = false; | ||
float sum = 0; | ||
for (it; it != LSTM_choices_->end();) { | ||
This comment has been minimized.
Sorry, something went wrong.
zdenop
Contributor
|
||
if (!strcmp(it->first, " ")) { | ||
it = LSTM_choices_->erase(it); | ||
found_space = true; | ||
} else { | ||
sum += it->second; | ||
++it; | ||
} | ||
} | ||
if (found_space) { | ||
for (it = LSTM_choices_->begin(); it != LSTM_choices_->end(); ++it) { | ||
it->second /= sum; | ||
} | ||
} | ||
} | ||
} // namespace tesseract. |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
amitdoNov 8, 2019
•
edited
Loading
CollaboratorThis comment has been minimized.
Sorry, something went wrong.
stweilNov 8, 2019
Member@noahmetzger, would the following code be correct?
This comment has been minimized.
Sorry, something went wrong.
noahmetzgerNov 8, 2019
Author ContributorI think this is for the case when the list is empty. If i remember right, when you use next(LSTM_choice_it) and the list is already at the end you ll get a nullpointer. So you have to make sure it first checks that before using the next method. With || it would use it even when the list is already at the end
This comment has been minimized.
Sorry, something went wrong.
amitdoNov 8, 2019
Collaboratorhttps://stackoverflow.com/a/7925696
This comment has been minimized.
Sorry, something went wrong.
noahmetzgerNov 8, 2019
•
edited
Loading
Author Contributormy bad, you are right
This comment has been minimized.
Sorry, something went wrong.
amitdoNov 20, 2019
Collaborator@stweil, a reminder...