-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from alan-turing-institute/14-ocr-reverted
14 ocr reverted
- Loading branch information
Showing
5 changed files
with
156 additions
and
24 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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
""" | ||
OCR error computation for eval. | ||
""" | ||
|
||
from typing import Any | ||
|
||
from torchmetrics.text import CharErrorRate | ||
|
||
|
||
def ocr_error(ocr_output: dict[Any, Any]) -> float: | ||
""" | ||
Compute the character error rate for the predicted ocr character. | ||
NB: - this puts all strings into lower case for comparisons. | ||
- ideal error rate is 0, worst case is 1. | ||
Args: | ||
ocr_output: output from the ocr model, with structure, | ||
{ | ||
'full_output: [ | ||
{ | ||
'generated_text': gen text from the ocr model (str) | ||
'target': target text (str) | ||
'entropies': entropies for UQ (torch.Tensor) | ||
} | ||
] | ||
'outpu': pieced back together full string (str) | ||
} | ||
Returns: | ||
Character error rate across entire output of OCR (float) | ||
""" | ||
preds = [itm["generated_text"].lower() for itm in ocr_output["full_output"]] | ||
targs = [itm["target"].lower() for itm in ocr_output["full_output"]] | ||
cer = CharErrorRate() | ||
return cer(preds, targs).item() |
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