Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
C

crnn-pytorch

> 编程语言
Open source

Pytorch implementation of CRNN (CNN + RNN + CTCLoss) for all language OCR.

394 stars0 likes0 views
WebsiteGitHub

About

Pytorch implementation of CRNN (CNN + RNN + CTCLoss) for all language OCR.

Convolutional Recurrent Neural Network + CTCLoss

I think i have fixed the ctcloss nan problem!

Now!

Please pull the latest code from master.

Please update the pytorch to >= v1.2.0

Enjoy it!

PS: Once there is ctclossnan, please

  1. Change the batchSize to smaller (eg: 8, 16, 32)
  2. Change the lr to smaller (eg: 0.00001, 0.0001)
  3. Contact me by emailing to [email protected]

Dependence

  • CentOS7
  • Python3.6.5
  • torch==1.2.0
  • torchvision==0.4.0
  • Tesla P40 - Nvidia

Run demo

  • Download a pretrained model from Baidu Cloud (extraction code: si32)

  • People who cannot access Baidu can download a copy from Google Drive

  • Run demo

    bash
    python demo.py -m path/to/model -i data/demo.jpg

    Expected output

    bash
    -妳----真---的的---可---------以 => 妳真的可以

Feature

  • Variable length

    It support variable length.

  • Chinese support

    I change it to binary mode when reading the key and value, so you can use it to do Chinese OCR.

  • Change CTCLoss from warp-ctc to torch.nn.CTCLoss

    As we know, warp-ctc need to compile and it seems that it only support PyTorch 0.4. But PyTorch support CTCLoss itself, so i change the loss function to torch.nn.CTCLoss .

  • Solved PyTorch CTCLoss become nan after several epoch

    Just don't know why, but when i train the net, the loss always become nan after several epoch.

    I add a param dealwith_lossnan to params.py . If set it to True , the net will autocheck and replace all nan/inf in gradients to zero.

  • DataParallel

    I add a param multi_gpu to params.py . If you want to use multi gpu to train your net, please set it to True and set the param ngpu to a proper number.

Train your data

Prepare data

Folder mode

  1. Put your images in a folder and organize your images in the following format:

    label_number.jpg

    For example

    • English
    bash
    hi_0.jpg hello_1.jpg English_2.jpg English_3.jpg E n g l i s h_4.jpg...
    • Chinese
    bash
    一身转战_0.jpg 三千里_1.jpg 一剑曾当百万师_2.jpg 一剑曾当百万师_3.jpg 一 剑 曾 当 百 万 师_3.jpg ...

    So you can see, the number is used to distinguish the same label.

  2. Run the create_dataset.py in tool folder by

    bash
    python tool/create_dataset.py --out lmdb/data/output/path --folder path/to/folder
  3. Use the same step to create train and val data.

  4. The advantage of the folder mode is that it's convenient! But due to some illegal character can't be in the path

    So the disadvantage of the folder mode is that it's labels are limited.

File mode

  1. Your data file should like

    bash
    absolute/path/to/image/一身转战_0.jpg
    一身转战
    absolute/path/to/image/三千里_1.jpg
    三千里
    absolute/path/to/image/一剑曾当百万师_2.jpg
    一剑曾当百万师
    absolute/path/to/image/3.jpg
    一剑曾当百万师
    absolute/path/to/image/一 剑 曾 当 百 万 师_4.jpg
    一 剑 曾 当 百 万 师
    absolute/path/to/image/xxx.jpg
    label of xxx.jpg
    .
    .
    .

    DO REMEMBER:

    1. It must be the absolute path to image.
    2. The first line can't be empty.
    3. There are no blank line between two data.
  2. Run the create_dataset.py in tool folder by

    bash
    python tool/create_dataset.py --out lmdb/data/output/path --file path/to/file
  3. Use the same step to create train and val data.

Change parameters and alphabets

Parameters and alphabets can't always be the same in different situation.

  • Change parameters

    Your can see the params.py in detail.

  • Change alphabets

    Please put all the alphabets appeared in your labels to alphabets.py , or the program will throw error during training process.

Train

Run train.py by

bash
python train.py --trainroot path/to/train/dataset --valroot path/to/val/dataset

Reference

meijieru/crnn.pytorch

Sierkinhane/crnn_chinese_characters_rec

Issues· 22 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Pythoncnnctc-lossocrrnn

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言