OCR Correction Using Language Models - hcts-hra/ecpo-fulltext-experiments GitHub Wiki

Below I provide an experimental overview over the predictions of a BERT model asked to fill single-character gaps with the most likely character considering input sequence context (format: <gold char>: <index gold char was predicted at> – <list of predictions):

original sequence: "太平場及神岡潰退 、日軍被擊斃者六百人、傷者二百人(香港廿二日美聯電)..."
太: 0 – 太 0.63 和 0.11 、 0.02 北 0.02 開 0.01 治 0.01 大 0.01 水 0.01 天 0.01 日 0.00 
平: 3 – 戰 0.81 機 0.04 戦 0.03 平 0.02 武 0.00 原 0.00 陽 0.00 軍 0.00 市 0.00 廣 0.00 
場: N – 洋 0.17 山 0.17 島 0.14 原 0.07 市 0.04 軍 0.04 間 0.03 町 0.02 城 0.01 港 0.01 
及: 2 – 、 0.31 向 0.13 及 0.10 和 0.08 與 0.06 至 0.04 [UNK] 0.02 自 0.02 到 0.02 對 0.02 
神: N – 黃 0.14 龍 0.12 華 0.10 福 0.09 富 0.04 盛 0.02 雲 0.02 寶 0.02 武 0.02 廣 0.02 
岡: 4 – 戶 0.30 山 0.25 州 0.08 社 0.05 岡 0.05 崎 0.02 崗 0.02 木 0.01 戸 0.01 策 0.01 
潰: 2 – 撤 0.97 敗 0.01 潰 0.01 後 0.00 進 0.00 擊 0.00 退 0.00 回 0.00 戰 0.00 消 0.00 
退: 2 – 敗 0.24 軍 0.15 退 0.11 散 0.06 兵 0.05 堤 0.05 逃 0.03 戰 0.03 決 0.02 陷 0.02 
、: 0 – 、 0.59 。 0.26 之 0.03 的 0.02 , 0.02 中 0.01 : 0.01 時 0.01 , 0.01 後 0.00 
日: 1 – 華 0.39 日 0.26 國 0.06 美 0.03 英 0.03 守 0.02 中 0.02 粵 0.02 共 0.02 我 0.01 
軍: 0 – 軍 0.97 本 0.01 方 0.01 艦 0.00 人 0.00 機 0.00 兵 0.00 寇 0.00 隊 0.00 後 0.00 
被: 0 – 被 0.77 遭 0.07 所 0.03 已 0.01 攻 0.01 共 0.01 自 0.00 追 0.00 受 0.00 砲 0.00 
擊: 0 – 擊 0.98 撃 0.01 炸 0.00 殺 0.00 打 0.00 射 0.00 槍 0.00 格 0.00 戰 0.00 陣 0.00 
斃: 0 – 斃 0.89 死 0.04 敗 0.01 潰 0.01 中 0.01 退 0.01 傷 0.01 殺 0.01 亡 0.00 落 0.00 
者: 1 – 約 0.38 者 0.22 近 0.10 達 0.10 逾 0.05 有 0.05 共 0.02 十 0.01 為 0.01 的 0.00 
六: 5 – 三 0.17 二 0.16 五 0.16 一 0.14 四 0.10 六 0.07 八 0.06 七 0.05 數 0.05 九 0.02 
百: 0 – 百 0.58 十 0.30 千 0.11 萬 0.01 五 0.00 ○○ 0.00 ○ 0.00 八 0.00 餘 0.00 七 0.00 
人: 0 – 人 0.93 名 0.05 餘 0.01 兵 0.00 軍 0.00 多 0.00 位 0.00 員 0.00 萬 0.00 死 0.00 
、: 0 – 、 0.31 受 0.11 輕 0.11 重 0.09 死 0.06 , 0.06 負 0.05 及 0.02 或 0.01 傷 0.01 
傷: 0 – 傷 0.93 俘 0.04 死 0.01 生 0.00 亡 0.00 陣 0.00 餘 0.00 獲 0.00 還 0.00 負 0.00 
者: 0 – 者 0.66 亡 0.25 員 0.02 兵 0.02 傷 0.01 患 0.00 十 0.00 及 0.00 害 0.00 人 0.00 
二: 6 – 五 0.16 三 0.14 數 0.12 四 0.10 六 0.09 八 0.08 二 0.08 一 0.08 七 0.06 九 0.03 
百: 1 – 千 0.41 百 0.40 萬 0.12 十 0.07 000 0.00 ○○ 0.00 億 0.00 仟 0.00 ##00 0.00 餘 0.00 
人: 0 – 人 0.89 名 0.09 餘 0.01 。 0.00 萬 0.00 傷 0.00 位 0.00 多 0.00 軍 0.00 兵 0.00 
(: 0 – ( 0.92 。 0.06 ( 0.01 [UNK] 0.00 ) 0.00 、 0.00 」 0.00 〔 0.00 / 0.00 「 0.00 
香: 0 – 香 1.00 本 0.00 東 0.00 南 0.00 菲 0.00 巴 0.00 鹿 0.00 駐 0.00 北 0.00 馬 0.00 
港: 0 – 港 1.00 山 0.00 江 0.00 島 0.00 城 0.00 梨 0.00 海 0.00 水 0.00 谷 0.00 國 0.00 
廿: 1 – 十 0.83 廿 0.13 卅 0.01 : 0.00 [UNK] 0.00 月 0.00 、 0.00 二 0.00 : 0.00 港 0.00 
二: 1 – 四 0.15 二 0.13 七 0.12 六 0.12 一 0.11 九 0.11 三 0.10 八 0.08 五 0.07 十 0.00 
日: 0 – 日 1.00 時 0.00 號 0.00 則 0.00 、 0.00 版 0.00 月 0.00 期 0.00 分 0.00 港 0.00 
美: 0 – 美 0.95 雙 0.01 中 0.00 澳 0.00 文 0.00 電 0.00 韓 0.00 英 0.00 新 0.00 華 0.00 
聯: 0 – 聯 1.00 國 0.00 新 0.00 華 0.00 電 0.00 東 0.00 僑 0.00 洲 0.00 文 0.00 雜 0.00 
電: 1 – 社 0.73 電 0.17 報 0.07 網 0.01 早 0.01 文 0.00 刊 0.00 晚 0.00 訊 0.00 通 0.00 
): 0 – ) 0.93 ) 0.05 〕 0.00 : 0.00 、 0.00 : 0.00 。 0.00 , 0.00 」 0.00 稱 0.00 

As can be seen, in many cases the BERT model predicts the correct candidate in the top position (0) and there are only few cases where it is not found at all (N). We thus assume that, enough correct context provided, a BERT model could reliably improve OCR output if the search space is restricted to top k OCR model candidates. In order not to "mis-correct" right OCR predictions (95% of all predictions), we aim to separate right from wrong predictions by setting a threshold t for the difference in the confidence score of the top 1 and top 2 OCR candidate. This section will go into how we select k and t.

t

Since applying the softmax to the logit outputs of the OCR model results in very high confidence scores for most predictions (> 0.9999), we employ the log softmax. As we compute the difference between the scores of the top 1 and the top 2 candidate, this is equivalent to using the raw logits. (The log softmax substracts a constant from every value: log(exp(x) / ∑(exp(x))) = x − log(∑(exp(x))). Therefore, the difference between two logit values stays the same.)

Using the validation set to compare the top-1-top-2 logit difference for correct and wrong predictions (whereby a "correct prediction" means that the top 1 candidate is equal to the gold character), we can see that there's a rather clear tendency congruent with the intuition that right predictions show higher confidence (bin size = 0.02):

One might aim for a t as low as 3 or 4, but choosing a higher value could be justified by the fact that characters correctly recognized by the OCR model are not necessarily going to be "mis-corrected" if forwarded to the LM due to a confidence lower than t, while characters wrongly predicted by the OCR model won't ever get the chance of being corrected by the LM if t is chosen too low.

An important issue to consider is whether to replace tokens corrected by the LM immediately (which would cause them to influence the next LM correction within the same sequence) or collect replacements suggested by the LM and collectively replace them after all the low-confidence characters of the whole sequence have been iterated over.

⚠️ **GitHub.com Fallback** ⚠️