# Week23: OCR algorithms 解密大平台 ###### tags: `技術研討` ## 1. 前言 資料集為原始人工貼標和上次 Text-Image-Augmentation 所產生的資料,總共 144,806 張。每位 Track 1 的成員依據這些資料進行訓練,可以使用預訓練模型若該 github 有提供 。每位參賽者都各自於 github 找尋一個模型,需要紀錄成效和說明使用方法。 ## 2. 各種演算法 ### 報告大綱 - 立晟:ViTSTR - 信賢:Paddle - 昊中:SATRN - 沛筠:MASTER - 昱睿:tensorflow official attention ### 模型成效 #### 測試集結果 | Model | 04.acct_id | 06.bank_acct | 05.bank_code | 03.card_id | 08.post_acct | 07.post_number | avg | | -------- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | |clova (TPS-ResNet-BiLSTM-Attn)| 0.9340 | 0.9417 | 0.9599 | 0.9451 | 0.9444 | 0.9611 | 0.9465 | | Paddle (3, 32, 100) | 0.9029 | 0.6875 | 0.9713 | 0.8946 | 0.9222 | 0.9500 | 0.8711 | | Paddle(3, 32, 256) | 0.9542 | 0.9527 | 0.9728 | 0.9473 | 0.9555 | 0.9777 | 0.9573 | | SATRN | N/A | N/A | N/A | N/A | N/A | N/A | N/A | |MASTER(160, 48, epoch=120)|0.9084|0.3542|0.9671|0.9034|0.8889|0.9222|0.7960| |MASTER(240, 48, epoch=99)|0.9560|0.8875|0.9785|0.9539|0.9556|0.9833|0.9465| | ViTSTR | 0.8810 | 0.8931 | 0.9613 | 0.8804 | 0.8611 | 0.8889 | 0.9002 | | tensorflow official attention |0.923|0.920|0.952|0.924|0.883|0.916|0.9267| #### 各模型指標 | Model | Accuracy | Inference(s) | Model Size (MB)| GPU memory usage(MiB) | Training Time | epoch / iteration | | -------- | -------- | -------- | -------- | -------- |-------- |-------- | | ViTSTR | 0.9002 | 0.0192 | 326 | 1451 ||| | SATRN(training) | 0.9402 |N/A| 1052 | 8601 |5 mins/200 iters|60000 iterations| |MASTER(160, 48)|0.7960|0.4154|619|15779|1~2 step / s|120 epochs| |MASTER(240, 48)|0.9465|0.4025|619|15779|1~2 step / s|99 epochs| | PaddleOCR (3, 32, 100)| 0.8711 | 0.0173 | 299 | 7763 | 5 s /iter|epoch:139 <br> iteration: 130000| | PaddleOCR (3, 32, 256)| 0.9573 | 0.02~0.03(GPU)<br>0.25 (CPU) | 299 | 1000左右 | 5 s /iter|| | attention_ocr | 0.9267 |0.835 <br/>(奇怪怎麼這麼久,可是我改 batch inference 也是差不多 0.88)|2|2469<br/>(我 session fconfig 設定 10%)|1 ~ 2 sec / step|118304<br/>(最後到 115076,但 118304 表現最好)| ## 3. 模型介紹 ### [ViTSTR](https://github.com/roatienza/deep-text-recognition-benchmark) #### 論文背景 - 大多數的 Scene text recognition (STR) 演算法著重於精進 Accuracy,相較之下 Speed (inference time) 和 computational efficiency (gpu memory usage, FLOPS) 比較不被注重 - 利用 vision transformers (ViT) 的優勢進而提升 Speed 和 computational efficiency #### 模型架構 - 常見的做法 ![](https://i.imgur.com/HYCOmNJ.png) 1. 先把影像中的文字拉平 2. 用 Backbone 萃取影像中的特徵 3. 套用 Sequence Model 對序列做處理 4. 預測最後的字串 - ViTSTR 做法 ![](https://i.imgur.com/BTmrlRK.png) ![](https://i.imgur.com/RTmyXW4.png) 1. 先將要預測的圖檔 resize (本篇設定是 224*224) 2. 切成數小塊 patches (本篇設定 16*16) ![](https://i.imgur.com/8EtB6qm.png) 3. 每個 patch 乘以一個權重去做線性轉換,得到 patch embedding 4. 加入位置訊息 (position embedding) 5. 丟進 transformer encoder (本篇設定是 12 個 block) ![](https://i.imgur.com/Ca6Rr9V.png) 5-1. Layer Nomorlization (LN) v.s. Batch Normalization (BN) - Layer Normalization:針對同一個樣本去做 Normalization - Batch Normalization:針對同一個 channel 去做 Normalization ![](https://i.imgur.com/HpRDxef.png) 5-2. Sequence Model 用 Batch Normalization 會遇到的問題 ![](https://i.imgur.com/P62G9xr.png) 5-3. Multi-head Self-Attention (MSA) - [Vision Transformer 超詳細解讀](https://zhuanlan.zhihu.com/p/340149804) 6. 預測最後的字串 #### 使用方式 [roatienza/deep-text-recognition-benchmark](https://github.com/roatienza/deep-text-recognition-benchmark) #### 成效數字 (ViTSTR-Base) - accuracy: 0.9002 - inference time: 0.0192 秒 / 張 - model size: 326 M - GPU memory usage: 1451 MiB(load_model + inference 1 張) --- ### [Paddle OCR](https://github.com/PaddlePaddle/PaddleOCR) #### 前言 - 要記得 character dict 要換或是看一下! - 資料沒有換順序導致 eval 很爛(但後來發現應該是backbone的問題),我一開始沒有隨機分配 train和 eval - 記得安裝套件,若是要 gpu 要再安裝 paddlepaddle-gpu (requirement沒寫R) - 本人嘗試多次 mobileNet 都沒有 train 起來,eval 都只有 0.7 ,真令人難過 - inference 的部分不是很方便(或是我沒有找到),目前只能吃單一資料夾路徑,不能餵多個路徑檔 (因此結果我還要自己爬回來QAQ) #### 說明 * **演算法的理論基礎** - 論文連結:[PP-OCR: A Practical Ultra Lightweight OCR System](https://arxiv.org/pdf/2009.09941.pdf) - 作者宣傳一下自己的成果: ![](https://i.imgur.com/G6UAnDu.png) - Framework: 整篇 paddle OCR 的架構包含三個 - text detection - detection boxes rectify 檢測框變形,主要通過幾何變換實現,因為檢測框就是四個點構成的。但是突變後的圖片可能是顛倒的,即水平方向,垂直方向,此時需要做進一步的處理。 的文本定向分類器,來進行文本定向的變形。為了保證準確率和模型大小,使用了4種策略,使用了600k圖訓練 - text recognition 本次競賽僅採用 text recognition ![](https://i.imgur.com/uMVKTJ9.png) - Recognition: CRNN + CTC 論文說為了增強效果,他們用了一些撇步 - light backbone 採用輕量級 MobileNetV3 當作骨架 作者對近期出現的輕量級網絡結構進行了比較實驗,發現在文本檢測問題中用MobileNetv3做骨幹網的效果最好,MobileNetV3_large_x0.5取得了最好的結果。 ![](https://i.imgur.com/q9kNHKs.png) - data augmentation - 採用 BDA(Base Data Augmentation),其實就是我們說的旋轉、高斯模糊、透視變形 (Perspective distortion)等等 - 採用 [TLA](https://arxiv.org/pdf/2003.06606.pdf) ![](https://i.imgur.com/4KwJRd6.png) - cosine learning rate decay 在訓練的初期,要讓模型更快達到收斂,因此我們可以選擇比較大的 learning rate;在訓練中後期,要達到最適化的模型,可以用相對比較小的 learning rate。 作者發現用餘弦學習率衰減方法比階梯式衰減得到的檢測精度更高。 ![](https://i.imgur.com/SrCeLWm.png) [Tensorflow 中 learning rate decay 的奇技淫巧](https://blog.csdn.net/zaf0516/article/details/90720759) - feature map resolution Paddle OCR 有開放多語言的訓練,而針對中文文本來說有很多中文字, MobileNetV3 的 stride 不適合太大。因此作者有將 stride 從 (2, 1) 調整為 (1, 1). ![](https://i.imgur.com/wWg4BrP.png) - regularization parameters 使用 L2_decay正則化,作者發現這部分對識別精度有重要影響。 - learning rate warm-up 在最一開始的時候,過大的 learning rate 會導致數值不穩定,推薦使用比較小的 learning rate。 當訓練較穩定之後,可以再重設定一開始的 learning rate。 但我覺得這邊講得跟 cosine learning rate decay 有點小矛盾,個人猜測這邊是說最最最一開始吧~ 原文參考如下: >The paper (He et al. 2019a) shows that using learning rate warm-up operation can help to improve the accuracy in the image classification. At the beginning of the training process, using a too large learning rate may result in numerical instability, a small learning rate is recommended to be used. When the training process is stable, the initial learning rate is to be used. For text detection, the experiments show that this strategy also is effective [神经网络中 warmup 策略为什么有效;有什么理论解释么?](https://www.zhihu.com/question/338066667/answer/771252708) ![](https://i.imgur.com/tmvxcHa.jpg) 從圖二(b,c)可以,加了熱身增加了模型最後幾層的相似性,這防熱身可以避免FC層的不穩定的變化。從圖一中可以看到,有熱身之後,模型能夠學得更穩定。 - light head 檢測部分是FPN結構,定義更小的inner_channels,設置最終的序列特徵維度 48。 (但他沒有說清楚中文有6000字怎麼做到的@@) >A full connection layer is used to encode the sequence features to the predicted characters in the ordinary. The dimension of the sequence features have an impact on the model size of a text recognizer, especially for Chinese recognition whose characters are more than 6 thousands. Meanwhile, it is not that the higher of the dimension, the stronger of the ability of the sequence features representation. In PP-OCR, the dimension of the sequence features is set to 48 empirically. - pre-trained model 提供預訓練模型 - PACT quantization 對於設計好的模型,依然很有必要進行剪枝,作者使用了FPGM (He et al. 2019b),去掉不重要的網絡部分。 ![](https://i.imgur.com/UuVK9lY.png) [闲话模型压缩之量化(Quantization)篇](https://jinzhuojun.blog.csdn.net/article/details/106955059) [模型精度不降反升!飞桨是这样改进PACT量化算法的!](https://zhuanlan.zhihu.com/p/314346120) - Backbone: 我們採用 MobileNetV3 作為與文本識別器相同的主幹文本檢測。 ![](https://i.imgur.com/ybcuE7X.png) - Data Augmentation: 但本次競賽要在規定給的訓練資料 training,因此不採用。 可以參考 https://github.com/PaddlePaddle/PaddleOCR/blob/develop/ppocr/data/rec/img_tools.py - Experiment ![](https://i.imgur.com/0AHIZXi.png) * 使用方式 * 資料預處理 將昊中給的 pickle 檔案隨機打散排列,前 120000 張為 training data、剩下是 testing data 24806 張,總共 144806 張 * 訓練 ``` python = python3 tools/train.py -c configs/det/det_mv3_db.yml -o Global.checkpoints=./your/trained/model ``` * 預測 - 單張 ``` python = # Predict English results python3 tools/infer_rec.py -c configs/rec/ch_ppocr_v2.0/rec_chinese_lite_train_v2.0.yml -o Global.pretrained_model={path/to/weights}/best_accuracy Global.load_static_weights=false Global.infer_img=doc/imgs_words/en/word_1.jpg ``` - 資料夾 在 yaml 檔的 infer_img 寫資料夾路徑 * 超參數的設定(batch_size, epoch, iteration) ![](https://i.imgur.com/FWSp5oL.png) ![](https://i.imgur.com/cyobwK5.png) ![](https://i.imgur.com/9VjN6B4.png) ![](https://i.imgur.com/utlgjr7.png) * [yaml 參數參考](https://github.com/PaddlePaddle/PaddleOCR/blob/release/2.2/doc/doc_en/config_en.md) * dataset 與 label 設定方式 by default, the image path and image label are split with \t, if you use other methods to split, it will cause training error ![](https://i.imgur.com/eBJt8pJ.png) * dict 設定方式 在 yaml 檔 Global 的設定 character_dict_path * evaluation (accuracy, normalized edit-distance) * 繼續訓練的方式 ``` python = python3 tools/train.py -c configs/det/det_mv3_db.yml -o Global.checkpoints=./your/trained/model ``` * 成效數字 - accuracy: 0.8711 - inference time: 0.0173 秒 / 張 (922張花費16秒) - model size: 299 M - GPU memory usage: 7763 MiB * 如果訓練不好,要找出訓練不好的原因跟現象,例如: - [CRNN识别训练的问题](https://github.com/PaddlePaddle/PaddleOCR/issues/3788) - [训练icdar2015识别,acc一直为0,loss也没有怎么变化,请问可能是什么原因](https://github.com/PaddlePaddle/PaddleOCR/issues/3602) 鄉民回覆:我的也是MobileNet large训练不了,但是small是可以训练的(我一直訓練large啊怎麼不早說!) - [学习率衰减可以每隔一定迭代数,或者epoches,手动设定吗?](https://github.com/PaddlePaddle/PaddleOCR/issues/3153) - [Training a model error: File "/content/PaddleOCR/ppocr/data/simple_dataset.py", line 128 in __getitem__ #3675](https://github.com/PaddlePaddle/PaddleOCR/issues/3845) - [识别训练 crnn+ctc 测试集准确率提升不了](https://github.com/PaddlePaddle/PaddleOCR/issues/1163) - 最後發現 backbone 的 MobileNetV3 train 不起來@@改成 ResNet * 參考資訊 https://blog.csdn.net/bevison/article/details/108806944 --- ### MASTER #### 演算法的理論基礎 論文:[MASTER: Multi-Aspect Non-local Network for Scene Text Recognition](https://arxiv.org/pdf/1910.02562.pdf) 模型利用 CNN + Transformer 的架構識別文字。 為改進 RNN-based 的短處,作者在 backbone (ResNet31) 的 block 後增加 **Multi-Aspect Global Context Attention (GCAttention)**,使模型學習更多樣的 2D attention。 - 源自 GCNet [[paper]](https://arxiv.org/pdf/1904.11492.pdf) 的 Global Context Block 可分為三個部分: - context modeling:feature map 先經 1 x 1 Conv 和 softmax 得到 attention 值,再進行 global attention pooling,算出各 pixel 與其他 pixels 的關聯性。 - transform:獲取 channel 間的依賴性。 - addition:feature fusion(broadcast element-wise)。 - 本文則令 h 為 Multi-Aspect GCAttention 數量,將 channel 分成 C/h 進行運算,或能視為一個 multi-head self-attention module。 ![](https://github.com/wenwenyu/MASTER-pytorch/raw/main/assets/overall.png) ![](https://d3i71xaburhd42.cloudfront.net/5e0f0cd537d592d249556842ebc5f1cd3ee6a4ef/6-TableI-1.png =60%x) <!-- https://zhuanlan.zhihu.com/p/102990363 --> <!-- https://zhuanlan.zhihu.com/p/65776424 --> #### 使用方式 ##### 訓練 1. 將 dataset 轉為 lmdb 格式([參考連結](https://github.com/chibohe/text_recognition_toolbox/blob/main/tools/create_lmdb_dataset.py))。 2. 修改 ./configs/master.yaml 檔內參數。 Train : Eval = 9 : 1(共 144806 張) Epoch:120 Batch size:200 Case_sensitive:True 3. 依照指示安裝套件。 ```python= pip install -r requirements.txt ``` 4. 開始訓練。 ```python= # training from scratch python train.py -c [your_config].yaml # resume training from last checkpoint python train.py -c [your_config].yaml -r # finetune with some checkpoint python train.py -c [your_config].yaml -f [checkpoint] ``` ##### 需要特別注意的地方 - 固定圖片大小 - width=160, height=48 - 可於 ./src/dataset/lmdb_data_generator.py 內確認實際讀取情況。 - dict 和長度分別對應 ./src/dataset/utils.py 中的 keys 及 max_length。 ```python= # line 10 (default) keys = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' # line 121 LabelTransformer = strLabelConverterForTransformer(keys, max_length=50) ``` - 將 ./src/model/metrics.py 內的 case_sensitive 默認值改成 True。 - ./src/tools/train_net.py 細節調整: - shuffle ```python= train_ds, eval_datasets = get_dataset(config) ``` - 依個人需求紀錄訓練過程的變化: - 現有的 log 以 step 為最小紀錄單位且僅有 loss 值。 - epoch 與 step 存有計算落差,或需連帶更動 yaml 檔的 eval interval_iter。 - 註解掉 train.py 裡的 prepare_outputs(config),以免額外複製一組檔案。 ##### 修改 eval_iiit5k.py → test.py 1. 微調 case_insensitive 成 case_sensitive。 2. 令 ./src/tools/eval_net.py 第 38 行為可直接傳入 checkpoint 所在資料夾的形式。 ```python= status = checkpoint.restore(tf.train.lastest_checkpoint(ckpt)) ``` 3. 按需求存下各張圖片的預測成效。 #### Performance - (width, height, epoch) = (160, 48, 120) 結果:0.7960 |tag|accuracy| |-|-| |acct_id|0.9084| |bank_acct|0.3542| |bank_code|0.9671| |card_id|0.9034| |post_acct|0.8889| |post_number|0.9222| - (width, height, epoch) = (240, 48, 99) 整體結果:0.9465 |tag|accuracy| |-|-| |acct_id|0.9560| |bank_acct|0.8875| |bank_code|0.9785| |card_id|0.9539| |post_acct|0.9556| |post_number|0.9833| #### 討論:為什麼 bank_acct 的成效特別差? -bank_acct 的長度介於 11~14 個字元,是所有類別中最長。 -若使用基於 attention 的模型處理長文本,需要 pretrain 來提升效果。 -再調整模型超參數(batch_size 和 learning rate 等,或不以 standard setting 訓練)。 -相關提問: [请问这个模型对长文本的识别和CRNN比较,哪个效果好?](https://github.com/jiangxiluning/MASTER-TF/issues/2) [The training phase converges quickly (acc>0.95), but the validate result is very bad (acc<0.3)](https://github.com/jiangxiluning/MASTER-TF/issues/15) **→已證實加長 image width 有助於改善結果,但 bank_acct 依然是 accuracy 最低的類別。** <!-- https://zhuanlan.zhihu.com/p/54743941 https://zhuanlan.zhihu.com/p/48508221 https://zhuanlan.zhihu.com/p/330483336 https://zhuanlan.zhihu.com/p/266069794 https://codingnote.cc/zh-tw/p/154455/ --> #### 參考資料 [論文解讀](https://blog.csdn.net/u011622208/article/details/102588704) [slide](https://github.com/wenwenyu/MASTER-pytorch/blob/main/assets/MASTER.pdf) [MASTER-TF(本次使用的原始碼)](https://github.com/jiangxiluning/MASTER-TF) [MASTER-pytorch](https://github.com/wenwenyu/MASTER-pytorch) --- ### SATRN #### 演算法的理論基礎: - [On Recognizing Texts of Arbitrary Shapes with 2D Self-Attention](https://arxiv.org/pdf/1910.04396.pdf) - GitHub: [text_recognition_toolbox](https://github.com/chibohe/text_recognition_toolbox) #### 論文簡介: - 目的: - 解決 STR 模型對於嚴重彎曲或旋轉的文字影像辨識效果不佳的問題。 ![](https://i.imgur.com/U3afWWn.jpg) - 模型基礎架構: Self-attention mechanism + Transformer - 用 attention 機制來描述文字圖像中字符的2D空間依賴性。 - 把在 NLP 領域效果很好的 Transformer 套用到 CV 領域中。 ![](https://i.imgur.com/mTLgnLp.jpg) #### 程式碼使用方式 - 模型資料集準備 - 目前只支持lmdb格式的數據檔,可以根據github作者提供的程式進行轉換 - 準備影像資料集以及label.txt檔,label.txt的格式內容如下: ```python= img_1.jpg 0123456789 img_2.jpg 9876543210 ``` - lmdb格式數據集轉換 ```python= python3 tools/create_lmdb_dataset.py --inputPath {影像資料集路徑} --gtFile {label.txt路徑} --outputPath {lmdb格式數據集保存路徑} ``` - 模型訓練 - 超參數設定:configs/SATRN.yml - batch_size: 10 - num_iters: 60000 - base_lr: 0.0005 - character_type: en ![](https://i.imgur.com/qVaJL6a.jpg) - 需要特別注意的地方: - configs/SATRN.yml 中的超參數 **character_dict_path** 應為字符的.txt檔,未提供的狀況下可以到 ./utils.py 的 def get_characters(dict_path) ```python= ... character_str = "這裡填上字符" ... return character_str ``` - 訓練指令: ```python= python train.py -c configs/SATRN.yml ``` - 模型接續訓練設定:configs/SATRN.yml - resumed_optimizer: True - pretrain_weights: 模型檔路徑 - 模型預測 - pretrain_weights: 模型檔路徑 ```python= python predict.py -c configs/SATRN.yml ``` :::info 作者現僅實現到 CRNN, FAN, DAN 三種模型的加載,所以 SATRN 模型的預測跟接續訓練還未完成。 ::: #### 模型訓練結果 - 每 5000 iteration 進行 evaluation - 模型訓練時GPU memory usage: 8601 MB - eval_acc: 0.9402 ![](https://i.imgur.com/VLd09g7.jpg) - eval_loss: ![](https://i.imgur.com/hjbNh2R.jpg) - norm_edit_dist: ![](https://i.imgur.com/8btS7em.jpg) - eval_sample: ![](https://i.imgur.com/FnkB22p.jpg) --- ### attention_ocr * 程式碼來源:[github](https://github.com/tensorflow/models/tree/master/research/attention_ocr) * 理論來源: [Attention-based Extraction of Structured Information from Street View Imagery](https://arxiv.org/pdf/1704.03549.pdf) #### **1. 演算法的理論基礎** CNN -> RNN -> Attention decoder * $f = \{ f_{i, j, c} \}$: 經過 CNN 之後的 feature map。其中 $i, \,j, \,c$ 分別為長、寬、深度 (channel) * $\alpha_t = \{ \alpha_{t, j, c} \}$: 表示第 t 個時間的 attention mask。言下之意是,頂多看到 t 時間以前的,不會看到 t 時間之後的向量 * 沒有切圖,整個 feature map 加上 attention mask 去做預測,所以有時候 saliency map 會跳來跳去,有時候左邊有時候右邊 ![](https://i.imgur.com/ycxZeJQ.png) #### 2. 使用方式 * 資料準備 1. 將影像檔改寫為 **tfrecord**,並且先手動拆分成 train, validation 兩部分。執行以下的程式碼 (詳細可以見: [github](https://gist.github.com/gvanhorn38/ac19b85a4f7b5fb9e82e04f4ac6d5566)) ```python= def _float_feature(value): return tf.train.Feature(float_list=tf.train.FloatList(value=value)) def _int64_feature(value): return tf.train.Feature(int64_list=tf.train.Int64List(value=value)) def _bytes_feature(value): return tf.train.Feature(bytes_list=tf.train.BytesList(value=value)) char_ids_padded, char_ids_unpadded = encode_utf8_string( text='abc', charset={'a':0, 'b':1, 'c':2}, length=5, null_char_id=3 ) """ char_ids_padded = [0,1,2,3,3] char_ids_unpadded = [0,1,2] """ # 接著要去產生 example 這個 tensorflow 物件,然後寫成 tfrecord tfrecord_writer = tf.python_io.TFRecordWriter(path) example = tf.train.Example(features=tf.train.Features( feature={ 'image/format': _bytes_feature("PNG"), 'image/encoded': _bytes_feature(img.tostring()), 'image/class': _int64_feature(char_ids_padded), 'image/unpadded_class': _int64_feature(char_ids_unpadded), 'height': _int64_feature(img.shape[0]), 'width': _int64_feature(img.shape[1]), 'orig_width': _int64_feature(img.shape[1]/num_of_views), 'image/text': _bytes_feature(text) } )) tfrecord_writer.write(example.SerializeToString) tfrecord_writer.close() ``` 2. 將新產生的資料以及該資料的 config 設定寫成新的 apa_config.py,接著在 `datasets/__init__.py` 裡面加入新的資料變成新的屬性 ```python= from datasets import fsns from datasets import fsns_test from datasets import apa_data __all__ = [fsns, fsns_test, apa_data] ``` * 超參數的設定 * learning_rate: 0.5 -> 0.8 * 我的 loss 在 10.8 ~ 11 之間卡了大約 24 HR,覺得前進不了,所以在 step 10.5 萬的時候改成 0.8 看看 * 改成 0.8 之後發現 loss 卻穩定提升到 11.5 ~ 11.8 左右,推測因為 learning_rate 變大導致跳出 loss 曲線 * 接著改成 0.3 繼續訓練,訓練到 118304,就有了 92.4% 的成效 * batch_size: **512** * 這部分我覺得有點特別,因為以往的演算法都不用這麼大,通常都 64 就很多了 * 不過 [name=Ahare] 是設定 1500,但因為我只有 1 顆 GPU,所以我就改成 750,但還是會 memory out,所以後來就只有設定 512 * image_shape: (40, 300) * null_code: 空值在 character dictonary 的第幾個字元 (以下的例子就是要設定在 133) ```python 0 # (這裡有個半形的空白) 133 <nul> 1 l 2 ’ 3 é 4 t ``` * 繼續訓練的方式: 這版 attention_ocr 跟 之前 CRNN + CTC 用 tensorflow 寫的那版很像,只要 log 指向訓練時候存取 checkpoint 的路徑,就會自動去找 checkpoint,所以可以無痛繼續訓練。也就是原封不動執行 ```shell= # 訓練部分的 log 與 eval 的 log 分在兩個不同的資料夾 CUDA_VISIBLE_DEVICES=0\ TF_FORCE_GPU_ALLOW_GROWTH=true\ python train.py\ --datast_name=apa_config\ --train_log_dir=log/train\ --eval_log_dir=log/eval ``` * loss 變化: 基本上 loss 在 10.8 ~ 11 之間就不太下降了 ![](https://i.imgur.com/hXva6uI.jpg) ![](https://i.imgur.com/la1GcHl.jpg) * validation sequence accuracy:validation 表現看起來很不錯,都接近 99% ;但是到 testing accuracy 就沒有想像中這麼好 ![](https://i.imgur.com/KyVJHms.jpg) #### 3. performance * 使用第 99456 checkpoint * 整體 accuracy: 86.31% * 分 tag 的 accuracy |tag|mean|sum|count| |-|-|-|-| |acct_id|85.1%|465|546| |bank_acct|85.8%|618|720| |bank_code|94.8%|663|699| |card_id|83.4%|760|911| |post_acct|77.2%|139|180| |post_number|82.2%|148|180| * 使用第 118304 checkpoint * 整體 accuracy: 92.67% * 分 tag 的 accuracy |tag|mean|sum|count| |-|-|-|-| |acct_id|92.3%|504|546| |bank_acct|92.0%|663|720| |bank_code|95.2%|666|699| |card_id|92.4%|842|911| |post_acct|88.3%|159|180| |post_number|91.6%|165|180| * 使用第 155076 checkpoint * 沒有比較好 :cry: * 整體 accuracy: 90% #### 4. 覺得問題點在哪裡 * batch_size 看能不能大一點 * 怎麼設定最適當的 learning rate: 問題起源是 loss 一直降不下來 ## 草稿部分 信賢: - 延續上次的主題,我們可以找不同的OCR方法 - 延續上次的主題,實驗家精神把這次比賽的人有用到的方法都試過一遍! - 嘗試 - 找新的paper報告,跟以前一樣 [PP-OCR: A Practical Ultra Lightweight OCR System](https://arxiv.org/pdf/2009.09941.pdf) https://github.com/PaddlePaddle/PaddleOCR - 找一些現成的OCR套件或是別人的github,試試效果如何 - https://github.com/GlassyWing/text-detection-ocr - https://github.com/open-mmlab/mmocr 昊中: - https://github.com/JaidedAI/EasyOCR - https://github.com/jiangxiluning/FOTS.PyTorch - https://github.com/kba/awesome-ocr - https://github.com/zacharywhitley/awesome-ocr 昱睿: - [paddle](https://github.com/PaddlePaddle/PaddleOCR) - 存摺影像辨識曾經用過的 model ,但後來使用 attention - [tensorflow official attention](https://github.com/tensorflow/models/tree/master/research/attention_ocr) - 匯款單 OCR 使用的 model <!-- 內容太多先刪掉啦 --> - 信賢:paddle: - 昊中:SATRN - 沛筠:MASTER: 大約 79% - 立晟:ViTSTR: 85% - 昱睿:tensorflow official attention: None - augmentation: - [Text-Image-Augmentation](https://github.com/RubanSeven/Text-Image-Augmentation-python) - [imgaug](https://imgaug.readthedocs.io/en/latest/source/examples_basics.html) - 訓練到 loss 收斂為準