PaddleOCR:基于 MNIST 数据集的手写多数字识别
本文介绍利用MNIST数据集构建多数字识别模型的过程。先通过预处理MNIST数据,拼接生成含多个数字的训练集和测试集;接着安装PaddleOCR及依赖,下载预训练模型;然后训练模型
本文介绍利用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"登录后复制
你是一名 AI 行业编辑,请围绕下面这条热点输出一份资讯解读:
热点:PaddleOCR:基于 MNIST 数据集的手写多数字识别要求:
1. 先用一句话解释这条热点在讲什么
2. 再总结它为什么重要
3. 说明会影响哪些 AI 产品或内容方向
4. 最后给出 3 个适合资讯站使用的标题
游乐网为非赢利性网站,所展示的游戏/软件/文章内容均来自于互联网或第三方用户上传分享,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系youleyoucom@outlook.com。
相关热点京东近日发布了国内首个专门为智能体自主支付设计的协议——A2P2协议。该协议将AI支付自主化程度系统性地划分为L0至L5六个等级,重点聚焦L3和L4级别,让智能体能在用户设定的规则边界内自主完成支付。为确保安全,协议首创了ARI机制,在支付时实时核验用户、智能体身份及运行环境,并采用资金账户隔离设计
VGN推出新款霓虹75Air磁轴键盘,单模有线版售价259元,三模无线版售价339元。该键盘采用75%配列,搭载天霸Air轴,主打电竞级性能,支持8kHz回报率、128kHz扫描率以及0 1ms低延迟,并具备0 005mm精度的RapidTrigger快速触发功能。外观上配备雾透键帽与霓虹灯带,
爱国者御风F90机箱上市,采用十字散热风道与MESH网孔面板,最多可装10个风扇,支持顶部和前部360水冷。兼容ATX主板,CPU散热器限高170mm,显卡限长395mm,提供2个3 5英寸和2个2 5英寸硬盘位,有黑、白两色可选。
北京越野BJ81“战术方盒子”硬派SUV的申报信息于近日公布。新车采用标志性方盒子造型,提供6座布局,车身尺寸为4850×2050×1975mm,轴距2810mm。本次申报新增了运动版BJ81VJ,升级22英寸轮圈、305 45宽胎等运动套件。动力方面搭载1 5T增程系统,电池来自宁德时代。新车预售
- 日榜
- 周榜
- 月榜
热点快看
