Train traindata to OCR fraction with tesseract4.1
在最新的Tesseract4 Train的说明文档中已明确明说,tesstrain.sh已被弃用,因此本文中将用Train Tesseract LSTM with make提及的方式来训练分数。
环境说明
1. win10 x64
2. 源码编译的Tesseract4.1发布版
训练步骤
1. 修改Makefile文件
1 2 |
Name of the model to be built. Default: $(MODEL_NAME) MODEL_NAME = fraction |
2. 准备训练文件
这里我们暂且用[0]来替代图中的方框,上图对应的.gt.txt对应的内容为
1 |
1/2,0,0,1/4,3/4,0,0 |
3. .numbers/.punc/.wordlist/Latin.unicharset/radical-stroke
根据Train Tesseract LSTM with make的说明准备相应的.numbers/.punc/.wordlist/Latin.unicharset/radical-stroke,这些文件存放的路径为:tesstrain-win-master\data。
.numbers file: Optional file for number patterns DAWG.
不同语言该文件中的内容不同。eng.numbers文件中存放与数字相关的单位或运算符,例如年,月份,天,上午,下午,时,分,秒等等。chi_sim.numbers只有一个字符 [ . ]。因为本次训练的traineddata只需要识别分数,因此本次训练用的fraction.numbers 文件中仅有[ / ]。
.punc file Optional file for punctuation DAWG.
语境中可能会用的到标点符号。本次训练用的fraction.func仅有标点[ , ]。
.wordlist file Optional file for dictionary DAWG.
待训练文件中用到的单词列表。本次训练用的fraction.wordlist文件为空。
4. 开始训练
以管理员身份运行命令提示符,进入tesstrain-win-master所在路径,运行如下命令:
1 |
make training --trace |
最终训练结果如下:
1 2 3 4 5 6 7 8 |
Finished! Error rate = 0.077 Makefile:277: update target 'data/fraction.traineddata' due to: data/fraction/checkpoints/fraction_checkpoint lstmtraining --stop_training --continue_from data/fraction/checkpoints/fraction_checkpoint --traineddata data/fraction/fraction.traineddata --model_output data/fraction.traineddata Loaded file data/fraction/checkpoints/fraction_checkpoint, unpacking... |
5. 验证训练效果
1 2 3 4 |
E:\TempTest>tesseract test_fraction1.png stdout -l fraction 1/2,0,0,1/4,3/4,0,0, 0,0,0,0,0,1/8,3/8 5/8,7/8,0,0 |
Tesseract5训练模块测试说明
tesstrain(20220420版本)中的makefile与tesstrain-win的makefile相比,有以下两点差异:
1. PSM的默认值由6更改为13,相关说明请参考GT4HistOCR 和 LSTMF file are not getting generated for some part of dataset
2. tesstrain(20220420版本)中的makefile寻找.gt.txt文件的代码为:
1 |
$(ALL_LSTMF): $(patsubst %.gt.txt,%.lstmf,$(shell find -L $(GROUND_TRUTH_DIR) -name '.gt.txt'))@mkdir -p $(OUTPUT_DIR)find -L $(GROUND_TRUTH_DIR) -name '.lstmf' | python3 shuffle.py $(RANDOM_SEED) > "$@" |
1 |
$(ALL_LSTMF): $(patsubst %.gt.txt,%.lstmf,$(wildcard $(GROUND_TRUTH_DIR)/.gt.txt))@mkdir -p $(OUTPUT_DIR)find $(GROUND_TRUTH_DIR) -name '.lstmf' -exec echo {} ; | sort -R -o "$@" |
此处tesstrain(20220420版本)使用了[ shell find -L],因为cygwin中的find命令与windows下的find命令重名,要在cygwin中使用find命令,需要用/usr/bin/find,因此将tesstrain(20220420版本)makefile中[shell find -L]均修改为[shell /usr/bin/find -L],此语句才能正常执行。
除了上述两点差异,要正常执行tesstrain(20220420版本)中makefile,还需要做如下修改:
1. 将tesstrain(20220420版本)中makefile中的python3更改为python.
假定tesstrain下载并解压后在本地电脑中的名称为tesstrain-main,以管理员身份运行命令提示符,进入tesstrain-main所在路径,运行如下命令:
1 |
make training --trace |
在执行到lstmtraining命令时,会出现如下错误:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
lstmtraining --debug_interval 0 --traineddata data/foo/foo.traineddata --learning_rate 0.02 --net_spec "[1,36,0,1 Ct3,3,16 Mp3,3 Lfys48 Lfx96 Lrx96 Lfx192 O1chead -n1 data/foo/unicharset]" --model_output data/foo/checkpoints/foo --train_listfile data/foo/list.train --eval_listfile data/foo/list.eval --max_iterations 10000 --target_error_rate 0.01 Warning: given outputs 77 not equal to unicharset of 76. Num outputs,weights in Series: 1,36,0,1:1, 0 Num outputs,weights in Series: C3,3:9, 0 Ft16:16, 160 Total weights = 160 [C3,3Ft16]:16, 160 Mp3,3:16, 0 TxyLfys48:48, 12480 Lfx96:96, 55680 RxLrx96:96, 74112 Lfx192:192, 221952 Fc76:76, 14668 Total weights = 379052 Built network:[1,36,0,1[C3,3Ft16]Mp3,3TxyLfys48Lfx96RxLrx96Lfx192Fc76] from request [1,36,0,1 Ct3,3,16 Mp3,3 Lfys48 Lfx96 Lrx96 Lfx192 O1c77] Training parameters: Debug interval = 0, weights = 0.1, learning rate = 0.02, momentum=0.5 null char=75 Deserialize header failed: data/foo-ground-truth/frapan_bittersuess_1891_0103_007.lstmf Deserialize header failed: data/foo-ground-truth/clauren_liebe_1827_0105_016.lstmf Deserialize header failed: data/foo-ground-truth/hoffmann_elixiere01_1815_0173_012.lstmf Deserialize header failed: data/foo-ground-truth/andreas_fenitschka_1898_0066_007.lstmf Deserialize header failed: data/foo-ground-truth/lenau_gedichte_1832_0225_006.lstmf Deserialize header failed: data/foo-ground-truth/poersch_gewerkschaftsbewegung_1897_0032_045.lstmf Deserialize header failed: data/foo-ground-truth/saar_novellen_1877_0283_020.lstmf Deserialize header failed: data/foo-ground-truth/fiedler_kuenstlerische_1887_0135_015.lstmf Deserialize header failed: data/foo-ground-truth/raschdorff_hochbau_1880_0025_016.lstmf Load of page 0 failed! Load of images failed!! |
在前述两个makefile的差异点中,关于[shell find]和[wildcard]的差异部分,如果直接将[shell find]相关代码修改为tesseract4.1版本中使用的[wildcard]相应代码,则训练过程不会出现all-lstmf, list-eval, list.train换行符的问题,可以正常训练完成,但是其最后结果的错误率也在99.5%以上,且经过验证,设定PSM=6或PSM=13并不影响结果,目前原因暂不明确,Github上也未见相关说明或者issue。
感谢阅读,欢迎交流!