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

P粉084495128
发布: 2025-07-21 11:33:15
原创
836人浏览过
本文介绍利用MNIST数据集构建多数字识别模型的过程。先通过预处理MNIST数据,拼接生成含多个数字的训练集和测试集;接着安装PaddleOCR及依赖,下载预训练模型;然后训练模型并导出;最后采样测试图片,用导出的模型进行识别测试。

☞☞☞AI 智能聊天, 问答助手, AI 智能搜索, 免费无限量使用 DeepSeek R1 模型☜☜☜

paddleocr:基于 mnist 数据集的手写多数字识别 - php中文网

怪兽AI数字人
怪兽AI数字人

数字人短视频创作,数字人直播,实时驱动数字人

怪兽AI数字人 44
查看详情 怪兽AI数字人

引入

  • 传统的基于 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.jpg' % (num, _), img)
        datas_train_list.append('train/%03d_%04d.jpg\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.jpg' % (num, _), img)
        datas_test_list.append('test/%03d_%04d.jpg\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 数据集的手写多数字识别 - php中文网        

安装 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.jpg' % 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"
登录后复制
   

以上就是PaddleOCR:基于 MNIST 数据集的手写多数字识别的详细内容,更多请关注php中文网其它相关文章!

相关标签:
最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号