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

引入
传统的基于 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
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"登录后复制
相关攻略
Pywinrm 通过Windows远程管理(WinRM)协议,让Python能够像操作本地一样执行远程Windows命令,真正打通了跨平台管理的最后一公里。 在混合IT环境中,Linux机器管理Wi
早些时候,聊过 Python 领域那场惊心动魄的供应链攻击。当时我就感叹,虽然我们 JavaScript 开发者对这类套路烂熟于心,但亲眼目睹这种规模的“投毒”还是头一次。 早些时候,聊过 Pyth
Toga 是 BeeWare 家族的核心成员,号称“写一次,跑遍所有平台”,而且用的是系统原生控件,不是那种一看就是网页套壳的界面 。 写了这么多年 Python,你是不是也想过:要是能一套代码跑
异常处理的核心:让错误在正确的地方被有效处理。正确的地方,就是别在底层就把异常吞了,也别在顶层还抛裸奔的 Exception。 异常处理写得好,半夜不用起来改 bug。1 你是不是也这么干过?tr
1 Skills机制概述 提起OpenClaw的Skills机制,不少人可能会把它想象成传统意义上的可执行插件。其实,它的内涵要更精妙一些。 简单说,Skills本质上是一套基于提示驱动的能力扩展机制。它并不是一个可以独立“跑”起来的程序模块,而是通过一份结构化描述文件(核心就是那个SKILL m
热门专题
热门推荐
加密货币行业翘首以盼的监管里程碑,终于有了实质性进展。美国证券交易委员会(SEC)主席保罗·阿特金斯(Paul Atkins)近日证实,那份允许加密项目在早期获得注册豁免权的“安全港”框架提案,已经正式送抵白宫,进入了最终审查阶段。 在范德堡大学与区块链协会联合举办的数字资产峰会上,阿特金斯透露了这
微策略Strategy报告:第一季录得144 6亿美元浮亏 再斥资约3 3亿美元买进4871枚比特币 市场震荡的威力有多大?看看Strategy的最新季报就明白了。根据其最新向美国证管会(SEC)提交的8-K报告,受市场剧烈波动影响,这家公司所持的比特币在第一季度录得了一笔惊人的数字——144 6亿
稳定币巨头Tether的动向,向来是加密世界的风向标。这不,它向Web3基础设施的版图扩张,又迈出了关键一步。公司执行长Paolo Ardoino在社交平台X上透露,其工程团队正在全力“烹制”一个新项目——去中心化搜索引擎 “Hypersearch”。这个消息一出,立刻引发了行业的广泛猜想。 采用D
基地位于Coinbase旗下以太坊Layer2网络Base的Seamless Protocol,日前正式宣告了服务的终结。这个曾经吸引了超过20万用户的原生DeFi借贷协议,在运营不到三年后,终究没能跑赢时间。它主打的核心产品是Integrated Leverage Markets(ILMs)——一
PAAL代币揭秘:深度解析Web3社区治理的核心钥匙 在去中心化自治组织的浪潮中,谁真正掌握了项目的话语权?PAAL代币提供了一套系统化的答案。它不仅是生态内流转的价值媒介,更是开启链上治理大门的核心凭证。通过持有并质押PAAL代币,用户能够对协议升级、资金分配乃至战略方向等关键事务投出决定性的一票





