python 中文识别训练库_crnn实现水平和垂直方向中文文字识别, 提供在3w多个中文字符训练的水平识别和垂直识别的预训练模型...

crnn.pytorch

​ 本工程使用随机生成的水平和垂直图像训练crnn模型做文字识别;一共使用10多种不同字体;共包括数字、字符、简体和繁体中文字30656个,详见all_words.txt。

预测

直接预测

a) 执行如下命令预测单个图像

# 水平方向

python demo.py --weight-path /path/to/chk.pth --image-path /path/to/image --direction horizontal

# 垂直方向

python demo.py --weight-path /path/to/chk.pth --image-path /path/to/image --direction vertical

b) 执行如下命令预测图像目录

# 水平方向

python demo.py --weight-path /path/to/chk.pth --image-dir /path/to/image/dir --direction horizontal

# 垂直方向

python demo.py --weight-path /path/to/chk.pth --image-dir /path/to/image/dir --direction vertical

使用restful服务预测

a) 启动restful服务

python rest.py -l /path/to/crnn.horizontal.061.pth -v /path/to/crnn.vertical.090.pth -d cuda

b) 使用如下代码预测,参考rest_test.py

import codecs

import cv2

import requests

img = cv2.imread('./images/horizontal-002.jpg', 0)

h, w = img.shape

data = {'img': codecs.encode(img.tostring(), 'base64'), # 转为字节,并编码

'shape': [h, w]}

r = requests.post("http://localhost:5000/crnn", data=data)

print(r.json()['text'])

结果如下:

厘鳃 銎 萛闿 檭車 垰銰 陀 婬2 蠶

模型效果

​ 以下图像均来为生成器随机生成的,也可以试用自己的图像测试

水平方向

图像

识别结果

鎏贬冱剽粥碍辆置钷固闻塔ど船

厘鳃銎萛闿檭車垰銰陀婬2蠶

磨丢河窜蹬奶鼋

添肃琉恪范粼兢俺斋┟傺怃梗纱脉陷荼荡荫驿

荼反霎吕娟斑恃畀貅引铥哳断替碱嘏

汨鑅譜軥嶰細挓

讵居世鄄钷橄鸠乩嗓犷魄芈丝

憎豼蕖蚷願巇廾尖瞚寣眗媝页锧荰瞿睔

休衷餐郄俐徂煅黢让咣

桃顸噢伯臣

垂直方向

Image1

Image2

Image3

Image4

Image5

Image6

Image7

Image8

Image9

Image10

从左到右识别结果

蟒销咔侉糌圻

醵姹探里坌葺神赵漓

紊趼掰膊縉氺月

皱坊凋庳剜蓍赚拾赣缮

ⅲ樱妖遐灌纽枰孽笸逼⊙斟喧湄汇

铳颢汜橇忝稿┗淌㎞琉炭盛㈨事

ゆ囚具憎鉴蔟馍络ら裕翱偬

绸唿綜袼殊潸名廪收鈁跃唤蛴腕

斥嗡门彳鹪Ⅴ戝物据趱欹

覃追煮茫舔酾桎藏瘪挚檎笏嵊疙鹦

训练

a) 单机多卡

export CUDA_DEVICE_ORDER="PCI_BUS_ID"

export CUDA_VISIBLE_DEVICES=1,2,3,4

python -m torch.distributed.launch --nproc_per_node 4 train.py --device cuda --direction vertical

b) 多机多卡

# 第一台主机

export NCCL_SOCKET_IFNAME=eth0

export NCCL_IB_DISABLE=1

export CUDA_DEVICE_ORDER="PCI_BUS_ID"

export CUDA_VISIBLE_DEVICES=1,2,3

python -m torch.distributed.launch --nproc_per_node 3 --nnodes=2 --node_rank=0 \

--master_port=6066 --master_addr="192.168.0.1" \

train.py --device cuda --direction vertical

# 第二台主机

export NCCL_SOCKET_IFNAME=eth0

export NCCL_IB_DISABLE=1

export CUDA_DEVICE_ORDER="PCI_BUS_ID"

export CUDA_VISIBLE_DEVICES=1,2,3

python -m torch.distributed.launch --nproc_per_node 3 --nnodes=2 --node_rank=1 \

--master_port=6066 --master_addr="192.168.0.1" \

train.py --device cuda --direction vertical

存在问题:多机训练比单机要慢很多,目前尚未解决.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值