当前位置: 首页
AI
PaddleOCR:基于 MNIST 数据集的手写多数字识别

PaddleOCR:基于 MNIST 数据集的手写多数字识别

热心网友 时间:2025-07-21
转载
本文介绍利用MNIST数据集构建多数字识别模型的过程。先通过预处理MNIST数据,拼接生成含多个数字的训练集和测试集;接着安装PaddleOCR及依赖,下载预训练模型;然后训练模型并导出;最后采样测试图片,用导出的模型进行识别测试。

paddleocr:基于 mnist 数据集的手写多数字识别 - 游乐网

引入

传统的基于 MNIST 数据集的手写数字识别模型只能识别单个数字但实际使用环境中,多数字识别才是更加常见的情况本次就使用 MNIST 数据集,通过拼接数据的方式,实现多数字识别模型

构建数据集

拼接采样数据集In [ ]
%cd ~!mkdir dataset !mkdir dataset/train!mkdir dataset/testimport cv2import randomimport numpy as npfrom tqdm import tqdmfrom paddle.vision.datasets import MNIST# 加载数据集mnist_train = MNIST(mode='train', backend='cv2')mnist_test = MNIST(mode='test', backend='cv2')# 数据集预处理datas_train = {}for i in range(len(mnist_train)):    sample = mnist_train[i]    x, y = sample[0], sample[1]    _sum = np.sum(x, axis=0)    _where = np.where(_sum > 0)    x = 255 - x[:, _where[0][0]: _where[0][-1]+1]    if str(y[0]) in datas_train:        datas_train[str(y[0])].append(x)    else:        datas_train[str(y[0])] = [x]datas_test = {}for i in range(len(mnist_test)):    sample = mnist_test[i]    x, y = sample[0], sample[1]    _sum = np.sum(x, axis=0)    _where = np.where(_sum > 0)    x = 255 - x[:, _where[0][0]: _where[0][-1]+1]    if str(y[0]) in datas_test:        datas_test[str(y[0])].append(x)    else:        datas_test[str(y[0])] = [x]# 图片拼接采样datas_train_list = []for num in tqdm(range(0, 999)):    for _ in range(1000):        imgs = [255 - np.zeros((28, np.random.randint(10)))]        for word in str(num):            index = np.random.randint(0, len(datas_train[word]))            imgs.append(datas_train[word][index])            imgs.append(255 - np.zeros((28, np.random.randint(10))))        img = np.concatenate(imgs, 1)        cv2.imwrite('dataset/train/%03d_%04d.webp' % (num, _), img)        datas_train_list.append('train/%03d_%04d.webp\t%d\n' % (num, _, num))datas_test_list = []for num in tqdm(range(0, 999)):    for _ in range(50):        imgs = [255 - np.zeros((28, np.random.randint(10)))]        for word in str(num):            index = np.random.randint(0, len(datas_test[word]))            imgs.append(datas_test[word][index])            imgs.append(255 - np.zeros((28, np.random.randint(10))))        img = np.concatenate(imgs, 1)        cv2.imwrite('dataset/test/%03d_%04d.webp' % (num, _), img)        datas_test_list.append('test/%03d_%04d.webp\t%d\n' % (num, _, num))# 数据列表生成with open('dataset/train.txt', 'w') as f:    for line in datas_train_list:        f.write(line)with open('dataset/test.txt', 'w') as f:    for line in datas_test_list:        f.write(line)
登录后复制    

数据样例展示

PaddleOCR:基于 MNIST 数据集的手写多数字识别 - 游乐网        

安装 PaddleOCR

In [ ]
!git clone https://gitee.com/PaddlePaddle/PaddleOCR -b release/2.1 --depth 1
登录后复制    

安装依赖环境

In [ ]
!pip install imgaug pyclipper lmdb Levenshtein
登录后复制    

下载预训练模型

In [ ]
%cd ~/PaddleOCR!wget -P ./pretrain_models/ https://paddleocr.bj.bcebos.com/dygraph_v2.0/ch/ch_ppocr_mobile_v2.0_rec_pre.tar!cd pretrain_models && tar -xf ch_ppocr_mobile_v2.0_rec_pre.tar && rm -rf ch_ppocr_mobile_v2.0_rec_pre.tar
登录后复制    

模型训练

In [8]
%cd ~/PaddleOCR!python tools/train.py -c ../multi_mnist.yml
登录后复制    

模型导出

In [34]
%cd ~/PaddleOCR!python3 tools/export_model.py \    -c ../multi_mnist.yml -o Global.pretrained_model=../output/multi_mnist/best_accuracy \    Global.load_static_weights=False \    Global.save_inference_dir=../inference/multi_mnist
登录后复制    

采样测试图片

In [45]
%cd ~/PaddleOCR!mkdir ~/test_imgsimport cv2import randomimport numpy as npfrom tqdm import tqdmfrom paddle.vision.datasets import MNIST# 加载数据集mnist_test = MNIST(mode='test', backend='cv2')# 数据集预处理datas_test = {}for i in range(len(mnist_test)):    sample = mnist_test[i]    x, y = sample[0], sample[1]    _sum = np.sum(x, axis=0)    _where = np.where(_sum > 0)    x = 255 - x[:, _where[0][0]: _where[0][-1]+1]    if str(y[0]) in datas_test:        datas_test[str(y[0])].append(x)    else:        datas_test[str(y[0])] = [x]# 图片拼接采样for num in range(0, 1000):    imgs = [255 - np.zeros((28, np.random.randint(10)))]    for word in str(num):        index = np.random.randint(0, len(datas_test[word]))        imgs.append(datas_test[word][index])        imgs.append(255 - np.zeros((28, np.random.randint(10))))    img = np.concatenate(imgs, 1)    cv2.imwrite('../test_imgs/%03d.webp' % num , img)
登录后复制    

模型测试

In [46]
%cd ~/PaddleOCR!python tools/infer/predict_rec.py \    --image_dir="../test_imgs" \    --rec_model_dir="../inference/multi_mnist/" \    --rec_image_shape="3, 28, 64" \    --rec_char_type="ch" \    --rec_char_dict_path="../label_list.txt"
登录后复制    
来源:https://www.php.cn/faq/1419608.html

游乐网为非赢利性网站,所展示的游戏/软件/文章内容均来自于互联网或第三方用户上传分享,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系youleyoucom@outlook.com。

同类文章
更多
逼AI当山顶洞人!Claude防话痨插件爆火,网友:受够了AI废话

逼AI当山顶洞人!Claude防话痨插件爆火,网友:受够了AI废话

新智元报道编辑:元宇【新智元导读】一个让AI像原始人一样说话的插件,在HN上一夜爆火,冲破2w星。它的核心只是一条简单粗暴的prompt:删掉冠词、客套和一切废话,号称能省下75%的输出token。

时间:2026-04-07 14:55
季度利润翻 8 倍,最赚钱的「卖铲人」财报背后,内存涨价狂潮如何收场?

季度利润翻 8 倍,最赚钱的「卖铲人」财报背后,内存涨价狂潮如何收场?

AI 时代最赚钱的公司,可能从来不是做 AI 的那个。作者|张勇毅编辑|靖宇淘金热里最稳赚的人,从来不是淘金的,是卖铲子的。这句老话在 2026 年的科技行业又应验了一次。只不过这次卖铲子的不是英伟

时间:2026-04-07 14:49
Claude Code Harness+龙虾科研团来了!金字塔分层架构+多智能体

Claude Code Harness+龙虾科研团来了!金字塔分层架构+多智能体

Claw AI Lab团队量子位 | 公众号 QbitAI你还在一个人做科研吗?科研最难的,从来不是问题本身,而是一个想法从文献到实验再到写作,只能靠自己一点点往前推。一个人方向偏了没人提醒,遇到歧

时间:2026-04-07 14:43
让离线强化学习从「局部描摹」变「全局布局」丨ICLR'26

让离线强化学习从「局部描摹」变「全局布局」丨ICLR'26

面对复杂连续任务的长程规划,现有的生成式离线强化学习方法往往会暴露短板。它们生成的轨迹经常陷入局部合理但全局偏航的窘境。它们太关注眼前的每一步,却忘了最终的目的地。针对这一痛点,厦门大学和香港科技大

时间:2026-04-07 14:37
美国犹他州启动新试点项目:AI为患者开具精神类药物处方

美国犹他州启动新试点项目:AI为患者开具精神类药物处方

IT之家 4 月 5 日消息,据外媒 PC Mag 当地时间 4 月 4 日报道,美国医疗机构 Legion Health 在犹他州获得监管批准,启动一项试点项目,允许 AI 系统为患者开具精神类药

时间:2026-04-07 14:30
热门专题
更多
刀塔传奇破解版无限钻石下载大全 刀塔传奇破解版无限钻石下载大全
洛克王国正式正版手游下载安装大全 洛克王国正式正版手游下载安装大全
思美人手游下载专区 思美人手游下载专区
好玩的阿拉德之怒游戏下载合集 好玩的阿拉德之怒游戏下载合集
不思议迷宫手游下载合集 不思议迷宫手游下载合集
百宝袋汉化组游戏最新合集 百宝袋汉化组游戏最新合集
jsk游戏合集30款游戏大全 jsk游戏合集30款游戏大全
宾果消消消原版下载大全 宾果消消消原版下载大全
热门教程
更多
  • 游戏攻略
  • 安卓教程
  • 苹果教程
  • 电脑教程