{"id":13543539,"url":"https://github.com/da03/Attention-OCR","last_synced_at":"2025-04-02T13:30:33.596Z","repository":{"id":50281221,"uuid":"60739376","full_name":"da03/Attention-OCR","owner":"da03","description":"Visual Attention based OCR","archived":false,"fork":false,"pushed_at":"2018-11-08T03:45:21.000Z","size":55,"stargazers_count":1117,"open_issues_count":69,"forks_count":361,"subscribers_count":57,"default_branch":"master","last_synced_at":"2025-03-27T07:09:40.157Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/da03.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"License.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-06-09T00:13:54.000Z","updated_at":"2025-03-17T21:21:08.000Z","dependencies_parsed_at":"2022-09-24T10:26:09.204Z","dependency_job_id":null,"html_url":"https://github.com/da03/Attention-OCR","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/da03%2FAttention-OCR","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/da03%2FAttention-OCR/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/da03%2FAttention-OCR/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/da03%2FAttention-OCR/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/da03","download_url":"https://codeload.github.com/da03/Attention-OCR/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246823523,"owners_count":20839740,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-01T11:00:32.713Z","updated_at":"2025-04-02T13:30:33.248Z","avatar_url":"https://github.com/da03.png","language":"Python","funding_links":[],"categories":["Optical Character Recognition Engines and Frameworks","Python","Librarys"],"sub_categories":["CTPN [paper:2016](https://arxiv.org/pdf/1609.03605.pdf)"],"readme":"# Attention-OCR\nAuthours: [Qi Guo](http://qiguo.ml) and [Yuntian Deng](https://github.com/da03)\n\nVisual Attention based OCR. The model first runs a sliding CNN on the image (images are resized to height 32 while preserving aspect ratio). Then an LSTM is stacked on top of the CNN. Finally, an attention model is used as a decoder for producing the final outputs.\n\n![example image 0](http://cs.cmu.edu/~yuntiand/OCR-2.jpg)\n\n# Prerequsites\nMost of our code is written based on Tensorflow, but we also use Keras for the convolution part of our model. Besides, we use python package distance to calculate edit distance for evaluation. (However, that is not mandatory, if distance is not installed, we will do exact match).\n\n### Tensorflow: [Installation Instructions](https://www.tensorflow.org/get_started/os_setup#download-and-setup) (tested on 0.12.1)\n\n### Distance (Optional):\n\n```\nwget http://www.cs.cmu.edu/~yuntiand/Distance-0.1.3.tar.gz\n```\n\n```\ntar zxf Distance-0.1.3.tar.gz\n```\n\n```\ncd distance; sudo python setup.py install\n```\n\n# Usage:\n\nNote: We assume that the working directory is `Attention-OCR`.\n\n## Train\n\n### Data Preparation\nWe need a file (specified by parameter `data-path`) containing the path of images and the corresponding characters, e.g.:\n\n```\npath/to/image1 abc\npath/to/image2 def\n```\n\nAnd we also need to specify a `data-base-dir` parameter such that we read the images from path `data-base-dir/path/to/image`. If `data-path` contains absolute path of images, then `data-base-dir` needs to be set to `/`.\n\n### A Toy Example\n\nFor a toy example, we have prepared a training dataset of the specified format, which is a subset of [Synth 90k](http://www.robots.ox.ac.uk/~vgg/data/text/)\n\n```\nwget http://www.cs.cmu.edu/~yuntiand/sample.tgz\n```\n\n```\ntar zxf sample.tgz\n```\n\n```\npython src/launcher.py --phase=train --data-path=sample/sample.txt --data-base-dir=sample --log-path=log.txt --no-load-model\n```\n\nAfter a while, you will see something like the following output in `log.txt`:\n\n```\n...\n2016-06-08 20:47:22,335 root  INFO     Created model with fresh parameters.\n2016-06-08 20:47:52,852 root  INFO     current_step: 0\n2016-06-08 20:48:01,253 root  INFO     step_time: 8.400597, step perplexity: 38.998714\n2016-06-08 20:48:01,385 root  INFO     current_step: 1\n2016-06-08 20:48:07,166 root  INFO     step_time: 5.781749, step perplexity: 38.998445\n2016-06-08 20:48:07,337 root  INFO     current_step: 2\n2016-06-08 20:48:12,322 root  INFO     step_time: 4.984972, step perplexity: 39.006730\n2016-06-08 20:48:12,347 root  INFO     current_step: 3\n2016-06-08 20:48:16,821 root  INFO     step_time: 4.473902, step perplexity: 39.000267\n2016-06-08 20:48:16,859 root  INFO     current_step: 4\n2016-06-08 20:48:21,452 root  INFO     step_time: 4.593249, step perplexity: 39.009864\n2016-06-08 20:48:21,530 root  INFO     current_step: 5\n2016-06-08 20:48:25,878 root  INFO     step_time: 4.348195, step perplexity: 38.987707\n2016-06-08 20:48:26,016 root  INFO     current_step: 6\n2016-06-08 20:48:30,851 root  INFO     step_time: 4.835423, step perplexity: 39.022887\n```\n\nNote that it takes quite a long time to reach convergence, since we are training the CNN and attention model simultaneously.\n\n## Test and visualize attention results\n\nThe test data format shall be the same as training data format. We have also prepared a test dataset of the specified format, which includes ICDAR03, ICDAR13, IIIT5k and SVT.\n\n```\nwget http://www.cs.cmu.edu/~yuntiand/evaluation_data.tgz\n```\n\n```\ntar zxf evaluation_data.tgz\n```\n\nWe also provide a trained model on Synth 90K:\n\n```\nwget http://www.cs.cmu.edu/~yuntiand/model.tgz\n```\n\n```\ntar zxf model.tgz\n```\n\n```\npython src/launcher.py --phase=test --visualize --data-path=evaluation_data/svt/test.txt --data-base-dir=evaluation_data/svt --log-path=log.txt --load-model --model-dir=model --output-dir=results\n```\n\nAfter a while, you will see something like the following output in `log.txt`:\n\n```\n2016-06-08 22:36:31,638 root  INFO     Reading model parameters from model/translate.ckpt-47200\n2016-06-08 22:36:40,529 root  INFO     Compare word based on edit distance.\n2016-06-08 22:36:41,652 root  INFO     step_time: 1.119277, step perplexity: 1.056626\n2016-06-08 22:36:41,660 root  INFO     1.000000 out of 1 correct\n2016-06-08 22:36:42,358 root  INFO     step_time: 0.696687, step perplexity: 2.003350\n2016-06-08 22:36:42,363 root  INFO     1.666667 out of 2 correct\n2016-06-08 22:36:42,831 root  INFO     step_time: 0.466550, step perplexity: 1.501963\n2016-06-08 22:36:42,835 root  INFO     2.466667 out of 3 correct\n2016-06-08 22:36:43,402 root  INFO     step_time: 0.562091, step perplexity: 1.269991\n2016-06-08 22:36:43,418 root  INFO     3.366667 out of 4 correct\n2016-06-08 22:36:43,897 root  INFO     step_time: 0.477545, step perplexity: 1.072437\n2016-06-08 22:36:43,905 root  INFO     4.366667 out of 5 correct\n2016-06-08 22:36:44,107 root  INFO     step_time: 0.195361, step perplexity: 2.071796\n2016-06-08 22:36:44,127 root  INFO     5.144444 out of 6 correct\n\n```\n\nExample output images in `results/correct` (the output directory is set via parameter `output-dir` and the default is `results`): (Look closer to see it clearly.)\n\nFormat: Image `index` (`predicted`/`ground truth`) `Image file`\n\nImage 0 (j/j): ![example image 0](http://cs.cmu.edu/~yuntiand/2evaluation_data_icdar13_images_word_370.png/image_0.jpg)\n\nImage 1 (u/u): ![example image 1](http://cs.cmu.edu/~yuntiand/2evaluation_data_icdar13_images_word_370.png/image_1.jpg)\n\nImage 2 (n/n): ![example image 2](http://cs.cmu.edu/~yuntiand/2evaluation_data_icdar13_images_word_370.png/image_2.jpg)\n\nImage 3 (g/g): ![example image 3](http://cs.cmu.edu/~yuntiand/2evaluation_data_icdar13_images_word_370.png/image_3.jpg)\n\nImage 4 (l/l): ![example image 4](http://cs.cmu.edu/~yuntiand/2evaluation_data_icdar13_images_word_370.png/image_4.jpg)\n\nImage 5 (e/e): ![example image 5](http://cs.cmu.edu/~yuntiand/2evaluation_data_icdar13_images_word_370.png/image_5.jpg)\n\n\n# Parameters:\n\n- Control\n    * `phase`: Determine whether to train or test.\n    * `visualize`: Valid if `phase` is set to test. Output the attention maps on the original image.\n    * `load-model`: Load model from `model-dir` or not.\n\n- Input and output\n    * `data-base-dir`: The base directory of the image path in `data-path`. If the image path in `data-path` is absolute path, set it to `/`.\n    * `data-path`: The path containing data file names and labels. Format per line: `image_path characters`.\n    * `model-dir`: The directory for saving and loading model parameters (structure is not stored).\n    * `log-path`: The path to put log.\n    * `output-dir`: The path to put visualization results if `visualize` is set to True.\n    * `steps-per-checkpoint`: Checkpointing (print perplexity, save model) per how many steps\n\n- Optimization\n    * `num-epoch`: The number of whole data passes.\n    * `batch-size`: Batch size. Only valid if `phase` is set to train.\n    * `initial-learning-rate`: Initial learning rate, note the we use AdaDelta, so the initial value doe not matter much.\n\n- Network\n    * `target-embedding-size`: Embedding dimension for each target.\n    * `attn-use-lstm`: Whether or not use LSTM attention decoder cell.\n    * `attn-num-hidden`: Number of hidden units in attention decoder cell.\n    * `attn-num-layers`: Number of layers in attention decoder cell. (Encoder number of hidden units will be `attn-num-hidden`*`attn-num-layers`).\n    * `target-vocab-size`: Target vocabulary size. Default is = 26+10+3 # 0: PADDING, 1: GO, 2: EOS, \u003e2: 0-9, a-z\n\n\n# References\n\n[Convert a formula to its LaTex source](https://github.com/harvardnlp/im2markup)\n\n[What You Get Is What You See: A Visual Markup Decompiler](https://arxiv.org/pdf/1609.04938.pdf)\n\n[Torch attention OCR](https://github.com/da03/torch-Attention-OCR)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fda03%2FAttention-OCR","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fda03%2FAttention-OCR","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fda03%2FAttention-OCR/lists"}