{"notes":[{"id":"text-recognition/safl","category":"text-recognition","legacyPath":"text-recognition/2201-safl","legacySlug":"2201-safl","legacyPaths":[],"title":"SAFL","subtitle":"Focal Loss Can Help!","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":"en","path":"/content/papers/en/text-recognition/safl.66cada9be8ec.md","headings":[{"id":"problem-definition","text":"Problem Definition","depth":2},{"id":"solution","text":"Solution","depth":2},{"id":"model-architecture","text":"Model Architecture","depth":3},{"id":"why-focal-loss","text":"Why Focal Loss?","depth":3},{"id":"discussion","text":"Discussion","depth":2},{"id":"does-focal-loss-improve-performance","text":"Does Focal Loss Improve Performance?","depth":3},{"id":"conclusion","text":"Conclusion","depth":2}],"anchorAliases":{},"guide":null,"relatedReading":[{"id":"object-detection/retinanet","relation":"background","topic":"Focal Loss from detection to text recognition"}]},{"id":"object-detection/retinanet","category":"object-detection","legacyPath":"object-detection/1708-retinanet","legacySlug":"1708-retinanet","legacyPaths":[],"title":"RetinaNet","subtitle":"Focal Loss is the Key","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":"en","path":"/content/papers/en/object-detection/retinanet.5feba1136b30.md","headings":[{"id":"problem-definition","text":"Problem Definition","depth":2},{"id":"solution","text":"Solution","depth":2},{"id":"focal-loss","text":"Focal Loss","depth":3},{"id":"model-architecture","text":"Model Architecture","depth":3},{"id":"training-parameters","text":"Training Parameters","depth":3},{"id":"discussion","text":"Discussion","depth":2},{"id":"experimental-results-on-the-coco-dataset","text":"Experimental Results on the COCO Dataset","depth":3},{"id":"ablation-studies","text":"Ablation Studies","depth":3},{"id":"conclusion","text":"Conclusion","depth":2}],"anchorAliases":{"make-cross-entropy-respond-to-difficulty":"focal-loss","putting-retinanet-together":"model-architecture","experimental-results-and-discussion":"discussion","compare-losses-on-the-same-model-first":"ablation-studies","read-accuracy-together-with-its-conditions":"experimental-results-on-the-coco-dataset"},"guide":null,"relatedReading":[{"id":"object-detection/fcos","relation":"next","topic":"Dense detection and sample assignment"},{"id":"retail-product/sku-110k","relation":"comparison","topic":"Detecting densely packed retail products"},{"id":"text-recognition/safl","relation":"next","topic":"Focal Loss from detection to text recognition"}]}],"text":"Transformers are increasingly common in Scene Text Recognition (STR) research.\n\nResearch should have a little fun, too: this time, the authors tried Focal Loss to see whether it could improve the model.\n\n## Problem Definition\n\nThere are two commonly used methods for STR training.\n\nCTC loss sums the probabilities of all alignments that collapse to the target string. Training requires the transcription, but no explicit correspondence between each feature position and each character.\n\nThe second method is CrossEntropy Loss. Models using this method typically require a decoder to align output and labels, often implemented with an LSTM or Transformer decoder.\n\nSince we already have CrossEntropy Loss, exploring alternative loss functions is a natural next step. In object detection, Focal Loss has become popular, so why not give it a try here as well?\n\nThus, the authors proposed SAFL, an STR model that incorporates Focal Loss as the loss function.\n\n## Solution\n\n### Model Architecture\n\n\n\n![SAFL](/content/papers/assets/8569c1a98ddccbd2efcd61736984fa56625057d777ef2bc05568618df21334f3.jpg)\n\n\n\n> [Original paper, Figure 1](https://arxiv.org/html/2201.00132v1): rectification, feature extraction, and recognition.\n\nThe architecture here is straightforward, largely comprising components we’ve seen in previous work.\n\nFirst comes a rectification network using a Thin Plate Spline (TPS) transformation. We covered this in:\n\n- [**[16.03] RARE: Automatic Alignment**](/blog/papers/text-recognition/rare).\n\nNext, a CNN extracts features, and a Transformer encoder and decoder recognize the characters. We covered this setup in:\n\n- [**[19.10] SATRN: Transformers Enter the Scene**](/blog/papers/text-recognition/satrn).\n\nFinally, the authors replaced CrossEntropy Loss with Focal Loss.\n\nWhy make that replacement?\n\n### Why Focal Loss?\n\n\n\n![data distribution](/content/papers/assets/3f3a72e12a8615a3d86500354d219db30bf8ee29a537ab37d77dff648e9b12f2.jpg)\n\n\n\n> [Original paper, Figure 2](https://arxiv.org/html/2201.00132v1): character frequencies in the training lexicon. Frequency and prediction difficulty are different quantities.\n\nThe chart shows an uneven **character** distribution in the training lexicon: some characters appear often, others rarely. The authors suggest that this could favor easy, frequent examples at the expense of harder, less frequent ones. Focal Loss downweights easy predictions; prediction difficulty and character frequency are separate properties.\n\nFocal Loss may help mitigate this issue. Its formula is as follows:\n\n$$\n\\text{FL}(p_t) = -\\alpha_t(1 - p_t)^\\gamma \\log(p_t)\n$$\n\nHere $p_t$ is the probability assigned to the correct character, while $\\alpha_t$ and $\\gamma$ are tunable parameters. As $p_t$ approaches 1, $(1-p_t)^\\gamma$ shrinks, so confident correct predictions contribute less to the update. This may help with less frequent examples, but it is not direct frequency-based weighting.\n\nWe previously explored this concept, which you can review here:\n\n- [**[17.08] RetinaNet: Focal Loss in Object Detection**](/blog/papers/object-detection/retinanet).\n\n## Discussion\n\n### Does Focal Loss Improve Performance?\n\n\n\n![results](/content/papers/assets/ea14538beb80737051a6bddcad3f797fc86df8cf44fbc2432f8f64b776c8406e.jpg)\n\n\n\n> [Original paper, Table II](https://arxiv.org/html/2201.00132v1): negative log-likelihood and Focal Loss for the same recognizer; the last row averages seven datasets.\n\nIn this table, the Focal Loss variant outperforms the negative log-likelihood (cross-entropy) variant on all seven datasets.\n\nThe table's mean accuracy rises from **86.9% to 88.2%**, a **1.3 percentage-point** difference. On CUTE it rises from **80.6% to 85.4%**, a **4.8-point** difference. The paper's prose says 2.3% for the mean, which conflicts with Table II; the calculation here follows the table. This ablation does not promise the same gain for every STR model.\n\n## Conclusion\n\nIn this study, the authors developed SAFL, a scene text recognition deep learning model that combines self-attention mechanisms with Focal Loss.\n\nThe experimental results show that SAFL achieves the highest average accuracy among the compared methods on the reported regular and irregular benchmarks.\n\n\n> [!TIP]\n> \n> If you're working on STR research, consider trying Focal Loss—it might just bring unexpected improvements!\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}}}
