{"notes":[{"id":"text-recognition/safl","category":"text-recognition","legacyPath":"text-recognition/2201-safl","legacySlug":"2201-safl","legacyPaths":[],"title":"SAFL","subtitle":"來點 Focal Loss 吧！","author":"Z. Yuan","paperDate":"2022-01-01","publishedOn":"2024-11-06","paperTitle":"SAFL: A Self-Attention Scene Text Recognizer with Focal Loss","paperUrl":"https://arxiv.org/abs/2201.00132","bibliography":{"title":"SAFL: A Self-Attention Scene Text Recognizer with Focal Loss","authors":["Bao Hieu Tran","Thanh Le-Cong","Huu Manh Nguyen","Duc Anh Le","Thanh Hung Nguyen","Phi Le Nguyen"],"url":"https://arxiv.org/abs/2201.00132","pdfUrl":"https://arxiv.org/pdf/2201.00132","codeUrl":null,"preprint":{"id":"2201.00132","submittedOn":"2022-01-01","url":"https://arxiv.org/abs/2201.00132"},"publication":{"venue":"ICMLA","year":2020,"kind":"conference","url":"https://doi.org/10.1109/ICMLA51294.2020.00223"},"citation":"@misc{textrecognitionsafl2022,\n  title = {{SAFL: A Self-Attention Scene Text Recognizer with Focal Loss}},\n  author = {Bao Hieu Tran and Thanh Le-Cong and Huu Manh Nguyen and Duc Anh Le and Thanh Hung Nguyen and Phi Le Nguyen},\n  year = {2022},\n  eprint = {2201.00132},\n  archivePrefix = {arXiv},\n  url = {https://arxiv.org/abs/2201.00132}\n}\n","citationPath":"/content/papers/citations/text-recognition/safl.ffb8d1295322.bib"},"thumbnail":{"src":"/content/papers/assets/8569c1a98ddccbd2efcd61736984fa56625057d777ef2bc05568618df21334f3.jpg","width":1470,"height":568},"locale":"zh-hant","path":"/content/papers/zh-hant/text-recognition/safl.d0051bded24c.md","headings":[{"id":"定義問題","text":"定義問題","depth":2},{"id":"解決問題","text":"解決問題","depth":2},{"id":"模型架構","text":"模型架構","depth":3},{"id":"focal-loss","text":"Focal Loss","depth":3},{"id":"討論","text":"討論","depth":2},{"id":"focal-loss-效果好嗎","text":"Focal Loss 效果好嗎？","depth":3},{"id":"結論","text":"結論","depth":2}],"anchorAliases":{},"guide":null,"relatedReading":[{"id":"object-detection/retinanet","relation":"background","topic":"從物件偵測到文字辨識的 Focal Loss"}]},{"id":"object-detection/retinanet","category":"object-detection","legacyPath":"object-detection/1708-retinanet","legacySlug":"1708-retinanet","legacyPaths":[],"title":"RetinaNet","subtitle":"Focal Loss 才是本體","author":"Z. Yuan","paperDate":"2017-08-07","publishedOn":"2024-08-08","paperTitle":"Focal Loss for Dense Object Detection","paperUrl":"https://arxiv.org/abs/1708.02002","bibliography":{"title":"Focal Loss for Dense Object Detection","authors":["Tsung-Yi Lin","Priya Goyal","Ross Girshick","Kaiming He","Piotr Dollár"],"url":"https://arxiv.org/abs/1708.02002","pdfUrl":"https://arxiv.org/pdf/1708.02002","codeUrl":null,"preprint":{"id":"1708.02002","submittedOn":"2017-08-07","url":"https://arxiv.org/abs/1708.02002"},"publication":{"venue":"ICCV","year":2017,"kind":"conference","url":"https://openaccess.thecvf.com/content_iccv_2017/html/Lin_Focal_Loss_for_ICCV_2017_paper.html"},"citation":"@misc{objectdetectionretinanet2017,\n  title = {{Focal Loss for Dense Object Detection}},\n  author = {Tsung-Yi Lin and Priya Goyal and Ross Girshick and Kaiming He and Piotr Dollár},\n  year = {2017},\n  eprint = {1708.02002},\n  archivePrefix = {arXiv},\n  url = {https://arxiv.org/abs/1708.02002}\n}\n","citationPath":"/content/papers/citations/object-detection/retinanet.7782ba4afe61.bib"},"thumbnail":{"src":"/content/papers/assets/72e05351a2bee439214afa9c111f8ec8ca466561a1fbd6be2b98cbd43305ad47.jpg","width":1746,"height":492},"locale":"zh-hant","path":"/content/papers/zh-hant/object-detection/retinanet.7e4aa15e2ead.md","headings":[{"id":"定義問題","text":"定義問題","depth":2},{"id":"解決問題","text":"解決問題","depth":2},{"id":"focal-loss","text":"Focal Loss","depth":3},{"id":"模型架構","text":"模型架構","depth":3},{"id":"訓練參數設定","text":"訓練參數設定","depth":3},{"id":"討論","text":"討論","depth":2},{"id":"在-coco-數據集上的實驗結果","text":"在 COCO 數據集上的實驗結果","depth":3},{"id":"消融實驗","text":"消融實驗","depth":3},{"id":"結論","text":"結論","depth":2}],"anchorAliases":{},"guide":null,"relatedReading":[{"id":"object-detection/fcos","relation":"next","topic":"密集偵測與正負樣本指派"},{"id":"retail-product/sku-110k","relation":"comparison","topic":"密集商品的偵測問題"},{"id":"text-recognition/safl","relation":"next","topic":"從物件偵測到文字辨識的 Focal Loss"}]}],"text":"最近愈來愈多 Transformer 的架構出現在 STR 的領域。\n\n做研究總是得找點樂子，這次作者把 Focal Loss 拿來試試看，看看能不能提升模型的效果。\n\n## 定義問題\n\n最常見的訓練方式大致上分兩種。\n\n第一種是 CTC Loss，這種方式是直接將模型的輸出與標籤進行比對，並計算兩者之間的差異。它可以讓我們不需要對標籤進行對齊，直接給定輸出序列，讓模型自己去學習如何對齊，最後得到結果。\n\n第二種是 CrossEntropy Loss，要使用這種損失函數，通常是使用一個解碼器的架構來對齊輸出與標籤。解碼器的選擇可以是一個 LSTM，或是一個 Transformer。\n\n既然有了 CrossEntropy Loss，嘗試其他的損失函數就是一個很自然的想法。我們可以看一下隔壁的物件偵測領域，Focus Loss 好像蠻流行的，不妨就拿來試試看吧！\n\n於是作者提出了 SAFL，一個使用 Focal Loss 作為損失函數的 STR 模型。\n\n## 解決問題\n\n### 模型架構\n\n\n\n![SAFL](/content/papers/assets/8569c1a98ddccbd2efcd61736984fa56625057d777ef2bc05568618df21334f3.jpg)\n\n\n\n這次的架構很簡單，大部分的內容我們之前都看過了。\n\n首先是一個使用 TPS 變換的校正網路，這個部分請參考：\n\n- [**[16.03] RARE: 全自動校正**](/blog/papers/text-recognition/rare)。\n\n接著由 CNN 提取特徵，再搭配 Transformer 編碼器與解碼器辨識字元，這個部分可以參考：\n\n- [**[19.10] SATRN: Transformer 抵達戰場**](/blog/papers/text-recognition/satrn)。\n\n作者在最後把 CrossEntropy Loss 換成了 Focal Loss。\n\n為什麼要換成 Focal Loss 呢？\n\n### Focal Loss\n\n\n\n![data distribution](/content/papers/assets/3f3a72e12a8615a3d86500354d219db30bf8ee29a537ab37d77dff648e9b12f2.jpg)\n\n\n\n作者針對訓練資料的字元分佈進行了分析，發現有些字元出現的頻率非常高，而有些字元出現的頻率非常低。作者認為，這樣的分佈可能讓模型偏向容易辨識的高頻樣本，而較少學好低頻樣本。\n\n而 Focal Loss 則是一種可以減緩這個問題的損失函數，其公式如下：\n\n$$\n\\text{FL}(p_t) = -\\alpha_t(1 - p_t)^\\gamma \\log(p_t)\n$$\n\n其中 $p_t$ 是模型對正確字元的預測機率，$\\alpha_t$ 和 $\\gamma$ 是可調參數。當 $p_t$ 越接近 1 時，$(1-p_t)^\\gamma$ 越小，使模型更重視難以預測的字元；這可能有助於低頻樣本，其權重依預測難度調整。\n\n這篇論文我們之前也讀過了，可以參考這篇：\n\n- [**[17.08] RetinaNet: Focal Loss 才是本體**](/blog/papers/object-detection/retinanet)。\n\n## 討論\n\n### Focal Loss 效果好嗎？\n\n\n\n![results](/content/papers/assets/ea14538beb80737051a6bddcad3f797fc86df8cf44fbc2432f8f64b776c8406e.jpg)\n\n\n\n如上表，使用 Focal Loss 的模型在所有數據集上都優於使用 CrossEntropy Loss 的模型。\n\n依表中的平均值，Focal Loss 將準確率從 86.9% 提高到 88.2%，相差 1.3 個百分點；論文正文所寫的 2.3% 與表格不符。差距最大的是 CUTE，從 80.6% 到 85.4%，相差 4.8 個百分點。\n\n## 結論\n\n在這篇論文中，作者利用自注意機制和 Focal Loss 的場景文本辨識深度學習模型。\n\n實驗結果表明，SAFL 在常規數據集和不規則數據集上均取得了最高的平均準確率。\n\n\n> [!TIP]\n> \n> 如果你也在做 STR 的研究，不妨試試看 Focal Loss，或許會有意想不到的效果！\n","imageSizes":{"/content/papers/assets/8569c1a98ddccbd2efcd61736984fa56625057d777ef2bc05568618df21334f3.jpg":{"width":1470,"height":568},"/content/papers/assets/3f3a72e12a8615a3d86500354d219db30bf8ee29a537ab37d77dff648e9b12f2.jpg":{"width":1224,"height":536},"/content/papers/assets/ea14538beb80737051a6bddcad3f797fc86df8cf44fbc2432f8f64b776c8406e.jpg":{"width":1368,"height":656}}}
