基于图像识别的智慧零售商品识别
近几年,随着“新零售”概念的火热,越来越多的品牌商及商超零售企业开始引进人工智能技术,探索商品管理、成本控制、用户体验等多维度的数字化转型,转型中所涉及的货架陈列分析、智能结算、智能库存管理、智能货柜、电商平台等以图搜图的场景背后的核心技术都离不开商品识别算法。

1. 项目介绍
近几年,随着“新零售”概念的火热,越来越多的品牌商及商超零售企业开始引进人工智能技术,探索商品管理、成本控制、用户体验等多维度的数字化转型,转型中所涉及的货架陈列分析、智能结算、智能库存管理、智能货柜、电商平台等以图搜图的场景背后的核心技术都离不开商品识别算法。
痛点问题
免费影视、动漫、音乐、游戏、小说资源长期稳定更新! 👉 点此立即查看 👈
结算效率要求极高:在零售场景中,若顾客购买的商品较多,采用传统的条形码结算,效率较低,顾客购物体验较差;不同商品相似度极高:比如同一种饮料的不同口味,就很可能拥有非常类似的包装。而且即便对于同一件商品,在不同情况下所获得的商品图像都往往存在相当大的差异;品类更新极快:商超零售场景下,新品通常以小时级别速度更新迭代,每增加新产品时若仅靠单一模型均需重新训练模型,模型训练成本及时间成本极大。解决方案
PaddleClas团队开源的图像识别PP-ShiTu技术方案,主要由主体检测、特征学习和向量检索三个模块组成,是一个实用的轻量级通用图像识别系统。基于此技术方案,商超零售企业可实现大量商品的一键式智能化识别,大大提高识别效率,节省人工及时间成本。
此外,当新品迭代更新时,PP-shitu无需重新训练模型,能够做到“即增即用”,完美解决上述痛点问题,大大提高了人工智能在商超零售行业的应用落地可能性。
PP-shitu技术方案可具体应用于例如:商品结算、库存管理等关于商品识别的商超细分场景。
模型工具简介
飞桨图像识别套件PaddleClas是飞桨为工业界和学术界所准备的一个图像识别任务的工具集,助力使用者训练出更好的视觉模型和应用落地。
而PP-ShiTu是一个实用的轻量级通用图像识别系统,主要由主体检测、特征学习和向量检索三个模块组成。该系统从骨干网络选择和调整、损失函数的选择、数据增强、学习率变换策略、正则化参数选择、预训练模型使用以及模型裁剪量化8个方面,采用多种策略,对各个模块的模型进行优化,最终得到在CPU上仅0.2s即可完成10w+库的图像识别的系统。
2. 安装说明
环境要求
Python >= 3.6
PaddlePaddle >= 2.1
Linux 环境最佳
安装PaddleClas
In [ ]# 克隆 PaddleClas# 由于本项目已克隆并存储,故不重复克隆## github仓库#!git clone https://github.com/PaddlePaddle/PaddleClas.git -b release/2.3 ## gitee仓库(推荐)#!git clone https://gitee.com/paddlepaddle/PaddleClas.git -b release/2.3登录后复制In [ ]
# 安装 Python 依赖库 # PaddleClas 的 Python 依赖库在 `requirements.txt` 中给出,可通过如下命令安装:%cd /home/aistudio/PaddleClas/!pip install --upgrade -r requirements.txt -i https://mirror.baidu.com/pypi/simple登录后复制
3. 数据准备
数据集介绍
目前开源的商品识别方向的数据集Products-10K Large Scale Product Recognition Dataset :数据集中的所有图片均来自京东商城。数据集中一共包含 1 万个经常购买的 SKU。所有 SKU 组织成一个层次结构。总共有近 19 万张图片。在实际应用场景中,图像量的分布是不均衡的。所有图像都由生产专家团队手工检查/标记。RP2K: A Large-Scale Retail Product Dataset for Fine-Grained Image Classification :收集了超过 500,000 张货架上零售产品的图像,属于 2000 种不同的产品。所有图片均在实体零售店人工拍摄,自然采光,符合实际应用场景。本项目以实际应用场景为依托,以数据质量为主要衡量标准,主体基于上述两大开源商品识别方向数据集,从中以样本均衡、图像质量、实际应用等为思考维度选取了部分SKU,并结合图片爬虫技术等数据搜索方式,开源了一份更符合本项目实际应用背景和技术需求的demo数据集。此数据集总计覆盖商品357类,涵盖包括厨房用品、日用品、饮料等生活日常购买商品,商品类别细粒度较高,涉及诸如同一品牌的不同规格商品、同一品类的不同品牌商品等实际场景下的数据可能性,能够模拟实际购物场景下的购买需求。商品部分
东古酱油一品鲜
东古黄豆酱750G
东鹏特饮罐装
中华(硬)
中华(软)
ru酸菌600亿_2
ru酸菌600亿_3
ru酸菌600亿原味
ru酸菌600亿芒果
ru酸菌600亿芦荟
...
数据集格式
训练集合(train dataset):用来训练模型,使模型能够学习该集合的图像特征。底库数据集合(gallery dataset):用来提供图像检索任务中的底库数据,该集合可与训练集或测试集相同,也可以不同,当与训练集相同时,测试集的类别体系应与训练集的类别体系相同。测试数据集合(query dataset):用来测试模型的好坏,通常要对测试集的每一张测试图片进行特征提取,之后和底库数据的特征进行距离匹配,得到识别结果,后根据识别结果计算整个测试集的指标。 训练集、底库数据集和测试数据集均使用 txt 文件指定,训练数据集 train_list.txt文件内容格式如下所示:# 采用"空格"作为分隔符号...train/10/1283.webp 10 624train/10/1284.webp 10 625train/10/1285.webp 10 626train/10/1286.webp 10 627...登录后复制
验证数据集(本数据集中既是 gallery dataset,也是 query dataset)test_list.txt 文件内容格式如下所示:
...test/103/743.webp 103 743test/103/744.webp 103 744test/103/745.webp 103 745test/103/746.webp 103 746...登录后复制
注:
每行数据使用“空格”分割,三列数据的含义分别是训练数据的路径、训练数据的label信息、训练数据的unique id;本数据集中由于 gallery dataset 和 query dataset 相同,为了去掉检索得到的第一个数据(检索图片本身无须评估),每个数据需要对应一个 unique id(每张图片的 id 不同即可,可以用行号来表示 unique id),用于后续评测 mAP、recall@1 等指标。yaml 配置文件的数据集选用 VeriWild。根据以上描述对数据集进行处理,并将数据集修改为如下目录格式:
├── classlabel.txt# 数据label和对应商品名称├── gallery_label.txt# 图片地址及对应的商品名称├── gallery# 底库图片├── test# 测试集图片├── test_list.txt# 测试集图片地址及对应的商品名称├── train# 训练集图片└── train_list.txt# 训练集图片地址及对应的商品名称登录后复制In [ ]
# 只想体验项目部署的,可解压此数据集,直接进入【8. 图像识别系统】%cd /home/aistudio/# !unzip -qd dataset dataset.zip## 想体验模型训练及评估等全流程的,解压data目录下数据集文件,本数据集较大,第一次下载需要耐心等待~!unzip -qd dataset /home/aistudio/data/data108651/dataset.zip登录后复制
4. 模型选择
PP-ShiTu是一个实用的轻量级通用图像识别系统,主要由主体检测、特征学习和向量检索三个模块组成。该系统从骨干网络选择和调整、损失函数的选择、数据增强、学习率变换策略、正则化参数选择、预训练模型使用以及模型裁剪量化8个方面,采用多种策略,对各个模块的模型进行优化,最终得到在CPU上仅0.2s即可完成10w+库的图像识别的系统。
主体检测
主体检测技术是目前应用非常广泛的一种检测技术,它指的是检测出图片中一个或者多个主体的坐标位置,然后将图像中的对应区域裁剪下来,进行识别,从而完成整个识别过程。主体检测是识别任务的前序步骤,可以有效提升识别精度。
考虑到商品识别实际应用场景中,需要快速准确地获得识别结果,故本项目选取适用于 CPU 或者移动端场景的轻量级主体检测模型PicoDet作为本项目主体检测部分的模型。此模型融合了ATSS、Generalized Focal Loss、余弦学习率策略、Cycle-EMA、轻量级检测 head等一系列优化算法,基于COCO train2017数据集进行大规模预训练,最终inference模型大小(MB)仅30.1MB,mAP可达40.1%,在cpu下单张图片预测耗时仅29.8ms,完美符合本项目实际落地需求,故在本项目中不对主体检测部分做适应性训练。
特征提取
特征提取是图像识别中的关键一环,它的作用是将输入的图片转化为固定维度的特征向量,用于后续的向量检索。好的特征需要具备相似度保持性,即在特征空间中,相似度高的图片对其特征相似度要比较高(距离比较近),相似度低的图片对,其特征相似度要比较小(距离比较远)。Deep Metric Learning用以研究如何通过深度学习的方法获得具有强表征能力的特征。
考虑到本项目的真实落地的场景中,推理速度及预测准确率是考量模型好坏的重要指标,所以本项目采用 PP_LCNet_x2_5 作为骨干网络, Neck 部分选用 Linear Layer, Head 部分选用 ArcMargin,Loss 部分选用 CELoss,并结合度量学习arcmargin算法,对高相似物体的区分效果远超单一模型,能更好地适应 Intel CPU,不仅准确率超越大模型ResNet50,预测速度还能快上3倍。
向量检索
向量检索技术在图像识别、图像检索中应用比较广泛。其主要目标是,对于给定的查询向量,在已经建立好的向量库中,与库中所有的待查询向量,进行特征向量的相似度或距离计算,得到相似度排序。在图像识别系统中,本项目使用 Faiss 对此部分进行支持。在此过程中,本项目选取 HNSW32 为检索算法,使得检索精度、检索速度能够取得较好的平衡,更为贴切本项目实际应用场景的使用需求。
5. 模型训练
本项目在主体检测部分直接采用PaddleDetection提供给得预训练模型,故这里主要介绍特征提取部分的模型训练。
修改配置文件
首先,在启动模型训练之前,需要在配置文件中修改数据配置相关的内容, 主要包括数据集的地址以及类别数量。对应到配置文件中的位置如下所示:
Head: name: ArcMargin embedding_size: 512 class_num: 358 #此处表示类别数登录后复制
Train: dataset: name: ImageNetDataset image_root: /home/aistudio/dataset/ #此处表示train数据所在的目录 cls_label_path: /home/aistudio/dataset/train_list.txt #此处表示train数据集label文件的地址登录后复制
Query: dataset: name: VeriWild image_root: /home/aistudio/dataset/#此处表示query数据集所在的目录 cls_label_path: /home/aistudio/dataset/test_list.txt #此处表示query数据集label文件的地址登录后复制
Gallery: dataset: name: VeriWild image_root: /home/aistudio/dataset/#此处表示gallery数据集所在的目录 cls_label_path: /home/aistudio/dataset/test_list.txt #此处表示gallery数据集label文件的地址登录后复制
模型训练
单机单卡训练In [ ]%cd /home/aistudio/PaddleClas!python tools/train.py \ -c ./ppcls/configs/GeneralRecognition/GeneralRecognition_PPLCNet_x2_5.yaml \ -o Arch.Backbone.pretrained=True \ -o Global.device=gpu \登录后复制
6.模型评估
单卡评估In [ ]%cd /home/aistudio/PaddleClas!python tools/eval.py \ -c ./ppcls/configs/GeneralRecognition/GeneralRecognition_PPLCNet_x2_5.yaml \ -o Global.pretrained_model="/home/aistudio/PaddleClas/output/RecModel/best_model"登录后复制
由评估log可见,recall1为0.98232,能够符合实际产业场景应用需求。
7. 模型推理
推理过程包括两个步骤: 1)导出推理模型, 2)获取特征向量登录后复制
导出推理模型
PaddlePaddle框架保存的权重文件分为两种:支持前向推理和反向梯度的训练模型 和 只支持前向推理的推理模型。二者的区别是推理模型针对推理速度和显存做了优化,裁剪了一些只在训练过程中才需要的tensor,降低显存占用,并进行了一些类似层融合,kernel选择的速度优化。因此可执行如下命令导出推理模型。
In [ ]%cd /home/aistudio/PaddleClas!python tools/export_model.py \ -c ./ppcls/configs/GeneralRecognition/GeneralRecognition_PPLCNet_x2_5.yaml \ -o Global.pretrained_model="output/RecModel/best_model"登录后复制
生成的推理模型位于 inference 目录,里面包含三个文件,分别为 inference.pdmodel、inference.pdiparams、inference.pdiparams.info。 其中: inference.pdmodel 用来存储推理模型的结构, inference.pdiparams 和 inference.pdiparams.info 用来存储推理模型相关的参数信息。
获取特征向量
In [ ]%cd /home/aistudio/PaddleClas/deploy!python python/predict_rec.py \ -c configs/inference_rec.yaml \ -o Global.rec_inference_model_dir="../inference" \ -o Global.infer_imgs="/home/aistudio/dataset/test_image/sijibao.webp"登录后复制
8.图像识别系统
这里串联主体检测、特征提取、向量检索,从而构成一整套图像识别系统:
若商品为原索引库里已有的商品:建立索引库修改configs/build_general.yaml文件内容:
Global: rec_inference_model_dir: "/home/aistudio/PaddleClas/inference"登录后复制
IndexProcess: index_method: "HNSW32" # supported: HNSW32, IVF, Flat image_root: "/home/aistudio/dataset/" index_dir: "/home/aistudio/dataset/index" data_file: "/home/aistudio/dataset/gallery_label.txt"登录后复制
执行如下代码:
In [ ]# 建立索引库%cd /home/aistudio/PaddleClas/deploy!python3 python/build_gallery.py \ -c configs/build_general.yaml \ -o IndexProcess.data_file="/home/aistudio/dataset/gallery_label.txt" \ -o IndexProcess.index_dir="/home/aistudio/dataset/index"登录后复制识别图片
运行如下命令,下载通用检测inference模型并解压:In [ ]
%cd /home/aistudio/PaddleClas/deploy/%mkdir models%cd models# 下载通用检测 inference 模型并解压!wget https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/rec/models/inference/picodet_PPLCNet_x2_5_mainbody_lite_v1.0_infer.tar && tar -xf picodet_PPLCNet_x2_5_mainbody_lite_v1.0_infer.tar登录后复制
修改推理文件configs/inference_general.yaml内容:
Global: infer_imgs: "/home/aistudio/dataset/test_image/sijibao.webp" det_inference_model_dir: "./models/picodet_PPLCNet_x2_5_mainbody_lite_v1.0_infer" rec_inference_model_dir: "/home/aistudio/PaddleClas/inference" rec_nms_thresold: 0.05登录后复制
IndexProcess: index_dir: "/home/aistudio/dataset/index" return_k: 5 score_thres: 0.5登录后复制
运行下面的命令,对图像 /home/aistudio/dataset/test_image/sijibao.webp 进行识别与检索:
In [4]#基于索引库的图像识别%cd /home/aistudio/PaddleClas/deploy!python python/predict_system.py \ -c configs/inference_general.yaml \ -o Global.infer_imgs="/home/aistudio/dataset/test_image/sijibao.webp" \ -o IndexProcess.index_dir="/home/aistudio/dataset/index"登录后复制
/home/aistudio/PaddleClas/deploy/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/vision/transforms/functional_pil.py:36: DeprecationWarning: NEAREST is deprecated and will be removed in Pillow 10 (2024-07-01). Use Resampling.NEAREST or Dither.NONE instead. 'nearest': Image.NEAREST,/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/vision/transforms/functional_pil.py:37: DeprecationWarning: BILINEAR is deprecated and will be removed in Pillow 10 (2024-07-01). Use Resampling.BILINEAR instead. 'bilinear': Image.BILINEAR,/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/vision/transforms/functional_pil.py:38: DeprecationWarning: BICUBIC is deprecated and will be removed in Pillow 10 (2024-07-01). Use Resampling.BICUBIC instead. 'bicubic': Image.BICUBIC,/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/vision/transforms/functional_pil.py:39: DeprecationWarning: BOX is deprecated and will be removed in Pillow 10 (2024-07-01). Use Resampling.BOX instead. 'box': Image.BOX,/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/vision/transforms/functional_pil.py:40: DeprecationWarning: LANCZOS is deprecated and will be removed in Pillow 10 (2024-07-01). Use Resampling.LANCZOS instead. 'lanczos': Image.LANCZOS,/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/vision/transforms/functional_pil.py:41: DeprecationWarning: HAMMING is deprecated and will be removed in Pillow 10 (2024-07-01). Use Resampling.HAMMING instead. 'hamming': Image.HAMMING2024-04-07 16:40:27 INFO: ============================================================= PaddleClas is powered by PaddlePaddle ! =============================================================== ==== For more info please go to the following website. ==== ==== https://github.com/PaddlePaddle/PaddleClas =============================================================2024-04-07 16:40:27 INFO: DetPostProcess : 2024-04-07 16:40:27 INFO: DetPreProcess : 2024-04-07 16:40:27 INFO: transform_ops : 2024-04-07 16:40:27 INFO: DetResize : 2024-04-07 16:40:27 INFO: interp : 22024-04-07 16:40:27 INFO: keep_ratio : False2024-04-07 16:40:27 INFO: target_size : [640, 640]2024-04-07 16:40:27 INFO: DetNormalizeImage : 2024-04-07 16:40:27 INFO: is_scale : True2024-04-07 16:40:27 INFO: mean : [0.485, 0.456, 0.406]2024-04-07 16:40:27 INFO: std : [0.229, 0.224, 0.225]2024-04-07 16:40:27 INFO: DetPermute : 2024-04-07 16:40:27 INFO: Global : 2024-04-07 16:40:27 INFO: batch_size : 12024-04-07 16:40:27 INFO: cpu_num_threads : 102024-04-07 16:40:27 INFO: det_inference_model_dir : ./models/picodet_PPLCNet_x2_5_mainbody_lite_v1.0_infer2024-04-07 16:40:27 INFO: enable_benchmark : True2024-04-07 16:40:27 INFO: enable_mkldnn : True2024-04-07 16:40:27 INFO: enable_profile : False2024-04-07 16:40:27 INFO: gpu_mem : 80002024-04-07 16:40:27 INFO: image_shape : [3, 640, 640]2024-04-07 16:40:27 INFO: infer_imgs : /home/aistudio/dataset/test_image/sijibao.webp2024-04-07 16:40:27 INFO: ir_optim : True2024-04-07 16:40:27 INFO: labe_list : ['foreground']2024-04-07 16:40:27 INFO: max_det_results : 52024-04-07 16:40:27 INFO: rec_inference_model_dir : /home/aistudio/PaddleClas/inference2024-04-07 16:40:27 INFO: rec_nms_thresold : 0.052024-04-07 16:40:27 INFO: threshold : 0.22024-04-07 16:40:27 INFO: use_fp16 : False2024-04-07 16:40:27 INFO: use_gpu : True2024-04-07 16:40:27 INFO: use_tensorrt : False2024-04-07 16:40:27 INFO: IndexProcess : 2024-04-07 16:40:27 INFO: batch_size : 322024-04-07 16:40:27 INFO: data_file : /home/aistudio/dataset/gallery_label.txt2024-04-07 16:40:27 INFO: delimiter : 2024-04-07 16:40:27 INFO: dist_type : IP2024-04-07 16:40:27 INFO: embedding_size : 5122024-04-07 16:40:27 INFO: image_root : /home/aistudio/dataset/2024-04-07 16:40:27 INFO: index_dir : /home/aistudio/dataset/index2024-04-07 16:40:27 INFO: index_method : HNSW322024-04-07 16:40:27 INFO: index_operation : new2024-04-07 16:40:27 INFO: return_k : 52024-04-07 16:40:27 INFO: score_thres : 0.52024-04-07 16:40:27 INFO: RecPostProcess : None2024-04-07 16:40:27 INFO: RecPreProcess : 2024-04-07 16:40:27 INFO: transform_ops : 2024-04-07 16:40:27 INFO: ResizeImage : 2024-04-07 16:40:27 INFO: size : 2242024-04-07 16:40:27 INFO: NormalizeImage : 2024-04-07 16:40:27 INFO: mean : [0.485, 0.456, 0.406]2024-04-07 16:40:27 INFO: order : 2024-04-07 16:40:27 INFO: scale : 0.003921572024-04-07 16:40:27 INFO: std : [0.229, 0.224, 0.225]2024-04-07 16:40:27 INFO: ToCHWImage : None/home/aistudio/PaddleClas/deploy/python/preprocess.py:65: DeprecationWarning: NEAREST is deprecated and will be removed in Pillow 10 (2024-07-01). Use Resampling.NEAREST or Dither.NONE instead. 'nearest': Image.NEAREST,/home/aistudio/PaddleClas/deploy/python/preprocess.py:66: DeprecationWarning: BILINEAR is deprecated and will be removed in Pillow 10 (2024-07-01). Use Resampling.BILINEAR instead. 'bilinear': Image.BILINEAR,/home/aistudio/PaddleClas/deploy/python/preprocess.py:67: DeprecationWarning: BICUBIC is deprecated and will be removed in Pillow 10 (2024-07-01). Use Resampling.BICUBIC instead. 'bicubic': Image.BICUBIC,/home/aistudio/PaddleClas/deploy/python/preprocess.py:68: DeprecationWarning: BOX is deprecated and will be removed in Pillow 10 (2024-07-01). Use Resampling.BOX instead. 'box': Image.BOX,/home/aistudio/PaddleClas/deploy/python/preprocess.py:69: DeprecationWarning: LANCZOS is deprecated and will be removed in Pillow 10 (2024-07-01). Use Resampling.LANCZOS instead. 'lanczos': Image.LANCZOS,/home/aistudio/PaddleClas/deploy/python/preprocess.py:70: DeprecationWarning: HAMMING is deprecated and will be removed in Pillow 10 (2024-07-01). Use Resampling.HAMMING instead. 'hamming': Image.HAMMINGInference: 34.667015075683594 ms per batch image[{'bbox': [131, 114, 356, 355], 'rec_docs': '四季宝花生酱', 'rec_scores': 0.85414684}]登录后复制其中 bbox 表示检测出的主体所在位置,rec_docs 表示索引库中与检测框最为相似的类别,rec_scores 表示对应的置信度。
检测的可视化结果也保存在 output 文件夹下,对于本张图像,识别结果可视化如下所示:
若商品为原索引库里没有的商品:对图像 /home/aistudio/dataset/test_image/recognition_2.webp 进行识别, 待检索图像如下所示。

运行如下识别命令:
In [5]%cd /home/aistudio/PaddleClas/deploy!python python/predict_system.py \ -c configs/inference_general.yaml \ -o Global.infer_imgs="/home/aistudio/dataset/test_image/recognition_2.webp"登录后复制
/home/aistudio/PaddleClas/deploy/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/vision/transforms/functional_pil.py:36: DeprecationWarning: NEAREST is deprecated and will be removed in Pillow 10 (2024-07-01). Use Resampling.NEAREST or Dither.NONE instead. 'nearest': Image.NEAREST,/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/vision/transforms/functional_pil.py:37: DeprecationWarning: BILINEAR is deprecated and will be removed in Pillow 10 (2024-07-01). Use Resampling.BILINEAR instead. 'bilinear': Image.BILINEAR,/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/vision/transforms/functional_pil.py:38: DeprecationWarning: BICUBIC is deprecated and will be removed in Pillow 10 (2024-07-01). Use Resampling.BICUBIC instead. 'bicubic': Image.BICUBIC,/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/vision/transforms/functional_pil.py:39: DeprecationWarning: BOX is deprecated and will be removed in Pillow 10 (2024-07-01). Use Resampling.BOX instead. 'box': Image.BOX,/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/vision/transforms/functional_pil.py:40: DeprecationWarning: LANCZOS is deprecated and will be removed in Pillow 10 (2024-07-01). Use Resampling.LANCZOS instead. 'lanczos': Image.LANCZOS,/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/vision/transforms/functional_pil.py:41: DeprecationWarning: HAMMING is deprecated and will be removed in Pillow 10 (2024-07-01). Use Resampling.HAMMING instead. 'hamming': Image.HAMMING2024-04-07 16:41:11 INFO: ============================================================= PaddleClas is powered by PaddlePaddle ! =============================================================== ==== For more info please go to the following website. ==== ==== https://github.com/PaddlePaddle/PaddleClas =============================================================2024-04-07 16:41:11 INFO: DetPostProcess : 2024-04-07 16:41:11 INFO: DetPreProcess : 2024-04-07 16:41:11 INFO: transform_ops : 2024-04-07 16:41:11 INFO: DetResize : 2024-04-07 16:41:11 INFO: interp : 22024-04-07 16:41:11 INFO: keep_ratio : False2024-04-07 16:41:11 INFO: target_size : [640, 640]2024-04-07 16:41:11 INFO: DetNormalizeImage : 2024-04-07 16:41:11 INFO: is_scale : True2024-04-07 16:41:11 INFO: mean : [0.485, 0.456, 0.406]2024-04-07 16:41:11 INFO: std : [0.229, 0.224, 0.225]2024-04-07 16:41:11 INFO: DetPermute : 2024-04-07 16:41:11 INFO: Global : 2024-04-07 16:41:11 INFO: batch_size : 12024-04-07 16:41:11 INFO: cpu_num_threads : 102024-04-07 16:41:11 INFO: det_inference_model_dir : ./models/picodet_PPLCNet_x2_5_mainbody_lite_v1.0_infer2024-04-07 16:41:11 INFO: enable_benchmark : True2024-04-07 16:41:11 INFO: enable_mkldnn : True2024-04-07 16:41:11 INFO: enable_profile : False2024-04-07 16:41:11 INFO: gpu_mem : 80002024-04-07 16:41:11 INFO: image_shape : [3, 640, 640]2024-04-07 16:41:11 INFO: infer_imgs : /home/aistudio/dataset/test_image/recognition_2.webp2024-04-07 16:41:11 INFO: ir_optim : True2024-04-07 16:41:11 INFO: labe_list : ['foreground']2024-04-07 16:41:11 INFO: max_det_results : 52024-04-07 16:41:11 INFO: rec_inference_model_dir : /home/aistudio/PaddleClas/inference2024-04-07 16:41:11 INFO: rec_nms_thresold : 0.052024-04-07 16:41:11 INFO: threshold : 0.22024-04-07 16:41:11 INFO: use_fp16 : False2024-04-07 16:41:11 INFO: use_gpu : True2024-04-07 16:41:11 INFO: use_tensorrt : False2024-04-07 16:41:11 INFO: IndexProcess : 2024-04-07 16:41:11 INFO: batch_size : 322024-04-07 16:41:11 INFO: data_file : /home/aistudio/dataset/gallery_label.txt2024-04-07 16:41:11 INFO: delimiter : 2024-04-07 16:41:11 INFO: dist_type : IP2024-04-07 16:41:11 INFO: embedding_size : 5122024-04-07 16:41:11 INFO: image_root : /home/aistudio/dataset/2024-04-07 16:41:11 INFO: index_dir : /home/aistudio/dataset/index2024-04-07 16:41:11 INFO: index_method : HNSW322024-04-07 16:41:11 INFO: index_operation : new2024-04-07 16:41:11 INFO: return_k : 52024-04-07 16:41:11 INFO: score_thres : 0.52024-04-07 16:41:11 INFO: RecPostProcess : None2024-04-07 16:41:11 INFO: RecPreProcess : 2024-04-07 16:41:11 INFO: transform_ops : 2024-04-07 16:41:11 INFO: ResizeImage : 2024-04-07 16:41:11 INFO: size : 2242024-04-07 16:41:11 INFO: NormalizeImage : 2024-04-07 16:41:11 INFO: mean : [0.485, 0.456, 0.406]2024-04-07 16:41:11 INFO: order : 2024-04-07 16:41:11 INFO: scale : 0.003921572024-04-07 16:41:11 INFO: std : [0.229, 0.224, 0.225]2024-04-07 16:41:11 INFO: ToCHWImage : None/home/aistudio/PaddleClas/deploy/python/preprocess.py:65: DeprecationWarning: NEAREST is deprecated and will be removed in Pillow 10 (2024-07-01). Use Resampling.NEAREST or Dither.NONE instead. 'nearest': Image.NEAREST,/home/aistudio/PaddleClas/deploy/python/preprocess.py:66: DeprecationWarning: BILINEAR is deprecated and will be removed in Pillow 10 (2024-07-01). Use Resampling.BILINEAR instead. 'bilinear': Image.BILINEAR,/home/aistudio/PaddleClas/deploy/python/preprocess.py:67: DeprecationWarning: BICUBIC is deprecated and will be removed in Pillow 10 (2024-07-01). Use Resampling.BICUBIC instead. 'bicubic': Image.BICUBIC,/home/aistudio/PaddleClas/deploy/python/preprocess.py:68: DeprecationWarning: BOX is deprecated and will be removed in Pillow 10 (2024-07-01). Use Resampling.BOX instead. 'box': Image.BOX,/home/aistudio/PaddleClas/deploy/python/preprocess.py:69: DeprecationWarning: LANCZOS is deprecated and will be removed in Pillow 10 (2024-07-01). Use Resampling.LANCZOS instead. 'lanczos': Image.LANCZOS,/home/aistudio/PaddleClas/deploy/python/preprocess.py:70: DeprecationWarning: HAMMING is deprecated and will be removed in Pillow 10 (2024-07-01). Use Resampling.HAMMING instead. 'hamming': Image.HAMMINGInference: 34.21902656555176 ms per batch image[]登录后复制
输出结果为空。
由于默认的索引库中不包含对应的索引信息,所以这里的识别结果有误,此时我们可以通过构建新的索引库的方式,完成未知类别的图像识别。
当索引库中的图像无法覆盖我们实际识别的场景时,即在预测未知类别的图像时,只需要将对应类别的相似图像添加到索引库中,从而完成对未知类别的图像识别,这一过程是不需要重新训练的。
准备新的数据与标签
首先需要将与待检索图像相似的图像列表拷贝到索引库原始图像的文件夹。这里将所有的底库图像数据都放在文件夹 /home/aistudio/dataset/gallery/ 中。
然后需要编辑记录了图像路径和标签信息的文本文件,这里 PaddleClas 将更正后的标签信息文件放在了 /home/aistudio/dataset/gallery_update.txt 文件中。可以与原来的 /home/aistudio/dataset/gallery_label.txt 标签文件进行对比,添加了小度充电宝和韩国进口火山泥的索引图像。
每一行的文本中,第一个字段表示图像的相对路径,第二个字段表示图像对应的标签信息,中间用 \t 键分隔开
建立新的索引库 使用下面的命令构建 index 索引,加速识别后的检索过程。In [ ]%cd /home/aistudio/PaddleClas/deploy/!python python/build_gallery.py \ -c configs/build_general.yaml \ -o IndexProcess.data_file="/home/aistudio/dataset/gallery_update.txt" \ -o IndexProcess.index_dir="/home/aistudio/dataset/index_update"登录后复制
最终新的索引信息保存在文件夹 /home/aistudio/dataset/index_update 中。
基于新的索引库的图像识别使用新的索引库,对上述图像进行识别,运行命令如下:
In [7]%cd /home/aistudio/PaddleClas/deploy/!python python/predict_system.py \ -c configs/inference_general.yaml \ -o Global.infer_imgs="/home/aistudio/dataset/test_image/recognition_2.webp" \ -o IndexProcess.index_dir="/home/aistudio/dataset/index_update"登录后复制
/home/aistudio/PaddleClas/deploy/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/vision/transforms/functional_pil.py:36: DeprecationWarning: NEAREST is deprecated and will be removed in Pillow 10 (2024-07-01). Use Resampling.NEAREST or Dither.NONE instead. 'nearest': Image.NEAREST,/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/vision/transforms/functional_pil.py:37: DeprecationWarning: BILINEAR is deprecated and will be removed in Pillow 10 (2024-07-01). Use Resampling.BILINEAR instead. 'bilinear': Image.BILINEAR,/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/vision/transforms/functional_pil.py:38: DeprecationWarning: BICUBIC is deprecated and will be removed in Pillow 10 (2024-07-01). Use Resampling.BICUBIC instead. 'bicubic': Image.BICUBIC,/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/vision/transforms/functional_pil.py:39: DeprecationWarning: BOX is deprecated and will be removed in Pillow 10 (2024-07-01). Use Resampling.BOX instead. 'box': Image.BOX,/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/vision/transforms/functional_pil.py:40: DeprecationWarning: LANCZOS is deprecated and will be removed in Pillow 10 (2024-07-01). Use Resampling.LANCZOS instead. 'lanczos': Image.LANCZOS,/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/vision/transforms/functional_pil.py:41: DeprecationWarning: HAMMING is deprecated and will be removed in Pillow 10 (2024-07-01). Use Resampling.HAMMING instead. 'hamming': Image.HAMMING2024-04-07 16:42:20 INFO: ============================================================= PaddleClas is powered by PaddlePaddle ! =============================================================== ==== For more info please go to the following website. ==== ==== https://github.com/PaddlePaddle/PaddleClas =============================================================2024-04-07 16:42:20 INFO: DetPostProcess : 2024-04-07 16:42:20 INFO: DetPreProcess : 2024-04-07 16:42:20 INFO: transform_ops : 2024-04-07 16:42:20 INFO: DetResize : 2024-04-07 16:42:20 INFO: interp : 22024-04-07 16:42:20 INFO: keep_ratio : False2024-04-07 16:42:20 INFO: target_size : [640, 640]2024-04-07 16:42:20 INFO: DetNormalizeImage : 2024-04-07 16:42:20 INFO: is_scale : True2024-04-07 16:42:20 INFO: mean : [0.485, 0.456, 0.406]2024-04-07 16:42:20 INFO: std : [0.229, 0.224, 0.225]2024-04-07 16:42:20 INFO: DetPermute : 2024-04-07 16:42:20 INFO: Global : 2024-04-07 16:42:20 INFO: batch_size : 12024-04-07 16:42:20 INFO: cpu_num_threads : 102024-04-07 16:42:20 INFO: det_inference_model_dir : ./models/picodet_PPLCNet_x2_5_mainbody_lite_v1.0_infer2024-04-07 16:42:20 INFO: enable_benchmark : True2024-04-07 16:42:20 INFO: enable_mkldnn : True2024-04-07 16:42:20 INFO: enable_profile : False2024-04-07 16:42:20 INFO: gpu_mem : 80002024-04-07 16:42:20 INFO: image_shape : [3, 640, 640]2024-04-07 16:42:20 INFO: infer_imgs : /home/aistudio/dataset/test_image/recognition_2.webp2024-04-07 16:42:20 INFO: ir_optim : True2024-04-07 16:42:20 INFO: labe_list : ['foreground']2024-04-07 16:42:20 INFO: max_det_results : 52024-04-07 16:42:20 INFO: rec_inference_model_dir : /home/aistudio/PaddleClas/inference2024-04-07 16:42:20 INFO: rec_nms_thresold : 0.052024-04-07 16:42:20 INFO: threshold : 0.22024-04-07 16:42:20 INFO: use_fp16 : False2024-04-07 16:42:20 INFO: use_gpu : True2024-04-07 16:42:20 INFO: use_tensorrt : False2024-04-07 16:42:20 INFO: IndexProcess : 2024-04-07 16:42:20 INFO: batch_size : 322024-04-07 16:42:20 INFO: data_file : /home/aistudio/dataset/gallery_label.txt2024-04-07 16:42:20 INFO: delimiter : 2024-04-07 16:42:20 INFO: dist_type : IP2024-04-07 16:42:20 INFO: embedding_size : 5122024-04-07 16:42:20 INFO: image_root : /home/aistudio/dataset/2024-04-07 16:42:20 INFO: index_dir : /home/aistudio/dataset/index_update2024-04-07 16:42:20 INFO: index_method : HNSW322024-04-07 16:42:20 INFO: index_operation : new2024-04-07 16:42:20 INFO: return_k : 52024-04-07 16:42:20 INFO: score_thres : 0.52024-04-07 16:42:20 INFO: RecPostProcess : None2024-04-07 16:42:20 INFO: RecPreProcess : 2024-04-07 16:42:20 INFO: transform_ops : 2024-04-07 16:42:20 INFO: ResizeImage : 2024-04-07 16:42:20 INFO: size : 2242024-04-07 16:42:20 INFO: NormalizeImage : 2024-04-07 16:42:20 INFO: mean : [0.485, 0.456, 0.406]2024-04-07 16:42:20 INFO: order : 2024-04-07 16:42:20 INFO: scale : 0.003921572024-04-07 16:42:20 INFO: std : [0.229, 0.224, 0.225]2024-04-07 16:42:20 INFO: ToCHWImage : None/home/aistudio/PaddleClas/deploy/python/preprocess.py:65: DeprecationWarning: NEAREST is deprecated and will be removed in Pillow 10 (2024-07-01). Use Resampling.NEAREST or Dither.NONE instead. 'nearest': Image.NEAREST,/home/aistudio/PaddleClas/deploy/python/preprocess.py:66: DeprecationWarning: BILINEAR is deprecated and will be removed in Pillow 10 (2024-07-01). Use Resampling.BILINEAR instead. 'bilinear': Image.BILINEAR,/home/aistudio/PaddleClas/deploy/python/preprocess.py:67: DeprecationWarning: BICUBIC is deprecated and will be removed in Pillow 10 (2024-07-01). Use Resampling.BICUBIC instead. 'bicubic': Image.BICUBIC,/home/aistudio/PaddleClas/deploy/python/preprocess.py:68: DeprecationWarning: BOX is deprecated and will be removed in Pillow 10 (2024-07-01). Use Resampling.BOX instead. 'box': Image.BOX,/home/aistudio/PaddleClas/deploy/python/preprocess.py:69: DeprecationWarning: LANCZOS is deprecated and will be removed in Pillow 10 (2024-07-01). Use Resampling.LANCZOS instead. 'lanczos': Image.LANCZOS,/home/aistudio/PaddleClas/deploy/python/preprocess.py:70: DeprecationWarning: HAMMING is deprecated and will be removed in Pillow 10 (2024-07-01). Use Resampling.HAMMING instead. 'hamming': Image.HAMMINGInference: 36.23652458190918 ms per batch image[{'bbox': [2152, 179, 2916, 1709], 'rec_docs': '韩国进口火山泥', 'rec_scores': 0.5329636}, {'bbox': [572, 361, 1245, 1653], 'rec_docs': '小度充电宝', 'rec_scores': 0.5052846}]登录后复制由测试效果图可知,模型对于未参与训练的商品及多个商品均有较好的识别效果:

9. 模型优化思路
检测模型调优PP-ShiTu中检测模型采用的 PicoDet算法,在使用最新模型后,如果不满足精度需求,则可以参考此部分文档,进行模型调优
对模型进行训练的话,需要自行准备数据,并对数据进行标注,建议一个类别至少准备200张标注图像,并将标注图像及groudtruth文件转成coco文件格式,以方便使用PaddleDetection进行训练的时候,请加载主体检测的预训练权重。
识别模型调优在使用最新模型后,如果不满足精度需求,则可以参考此部分文档,进行模型调优
因为要对模型进行训练,所以参照数据准备部分描述收集自己的数据集。值得注意的是,此部分需要准备大量的数据,以保证识别模型效果。
数据增强:根据实际情况选择不同数据增强方法。如:实际应用中数据遮挡比较严重,建议添加RandomErasing增强方法。换不同的backbone,一般来说,越大的模型,特征提取能力更强。选择不同的Metric Learning方法。不同的Metric Learning方法,对不同的数据集效果可能不太一样,建议尝试其他Loss采用蒸馏方法,对小模型进行模型能力提升增补数据集。针对错误样本,添加badcase数据模型训练完成后,参照测试代码进行检索库更新。同时,对整个pipeline进行测试,如果精度不达预期,则重复此步骤。
10. 模型服务化部署
使用 PaddleServing 做服务化部署时,需要将保存的 inference 模型转换为 Serving 模型。
模型转换
将 inference 模型转换为 Serving 模型:In [ ]%cd /home/aistudio/PaddleClas/deploy/# 安装相关包!pip3 install paddle-serving-client==0.7.0 -i https://mirror.baidu.com/pypi/simple!pip3 install paddle-serving-app==0.7.0 -i https://mirror.baidu.com/pypi/simple!pip3 install faiss-cpu==1.7.1post2 -i https://mirror.baidu.com/pypi/simple!pip3 install paddle-serving-server-gpu==0.7.0.post102 -i https://mirror.baidu.com/pypi/simple!pip3 install paddle-serving-server==0.7.0 # CPU登录后复制In [ ]
# 执行模型转换命令!python3 -m paddle_serving_client.convert --dirname /home/aistudio/PaddleClas/inference/ \ --model_filename inference.pdmodel \ --params_filename inference.pdiparams \ --serving_server ./inference_PPLCNet_serving/ \ --serving_client ./inference_PPLCNet_client登录后复制
识别推理模型转换完成后,会在当前文件夹多出 inference_PPLCNet_serving/ 和 inference_PPLCNet_client/ 的文件夹。分别修改 inference_PPLCNet_serving/ 和 inference_PPLCNet_client/ 目录下的 serving_server_conf.prototxt 中的 alias 名字: 将 fetch_var 中的 alias_name 改为 features。 修改后的 serving_server_conf.prototxt 内容如下:
feed_var { name: "x" alias_name: "x" is_lod_tensor: false feed_type: 1 shape: 3 shape: 224 shape: 224}fetch_var { name: "save_infer_model/scale_0.tmp_1" alias_name: "features" is_lod_tensor: false fetch_type: 1 shape: 512}登录后复制转换通用检测 inference 模型为 Serving 模型:In [ ]# 转换通用检测模型%cd /home/aistudio/PaddleClas/deploy/!python -m paddle_serving_client.convert --dirname ./models/picodet_PPLCNet_x2_5_mainbody_lite_v1.0_infer/ \ --model_filename inference.pdmodel \ --params_filename inference.pdiparams \ --serving_server ./picodet_PPLCNet_x2_5_mainbody_lite_v1.0_serving/ \ --serving_client ./picodet_PPLCNet_x2_5_mainbody_lite_v1.0_client/登录后复制
检测 inference 模型转换完成后,会在当前文件夹多出 picodet_PPLCNet_x2_5_mainbody_lite_v1.0_serving/ 和 picodet_PPLCNet_x2_5_mainbody_lite_v1.0_client/ 的文件夹。
注意: 此处不需要修改 picodet_PPLCNet_x2_5_mainbody_lite_v1.0_serving/ 目录下的 serving_server_conf.prototxt 中的 alias 名字。
服务部署和请求
注意: 识别服务涉及到多个模型,出于性能考虑采用 PipeLine 部署方式。
进入到工作目录In [10]%cd /home/aistudio/PaddleClas/deploy/paddleserving/recognition登录后复制
/home/aistudio/PaddleClas/deploy/paddleserving/recognition登录后复制
paddleserving 目录包含启动 pipeline 服务和发送预测请求的代码,包括:
__init__.pyconfig.yml # 启动服务的配置文件pipeline_http_client.py # http方式发送pipeline预测请求的脚本pipeline_rpc_client.py # rpc方式发送pipeline预测请求的脚本recognition_web_service.py # 启动pipeline服务端的脚本登录后复制修改配置文件
PaddleServing Python Pipeline提供了用户友好的多模型组合服务编程框架,适用于多模型组合应用的场景。 其配置文件为YAML格式,一般默认为config.yaml。所以在启动服务前需要修改config.yaml配置文件。 这里主要修改识别模型和检测模型的路径以及相关的计算设备:
op: rec: ··· #uci模型路径 model_config: /home/aistudio/PaddleClas/deploy/inference_PPLCNet_serving #计算硬件类型: 空缺时由devices决定(CPU/GPU),0=cpu, 1=gpu, 2=tensorRT, 3=arm cpu, 4=kunlun xpu device_type: 1 #计算硬件ID,当devices为""或不写时为CPU预测;当devices为"0", "0,1,2"时为GPU预测,表示使用的GPU卡 devices: "0" # "0,1" #client类型,包括brpc, grpc和local_predictor.local_predictor不启动Serving服务,进程内预测 client_type: local_predictor #Fetch结果列表,以client_config中fetch_var的alias_name为准 fetch_list: ["features"] det: concurrency: 1 local_service_conf: client_type: local_predictor device_type: 1 devices: '0' fetch_list: - save_infer_model/scale_0.tmp_1 model_config: /home/aistudio/PaddleClas/deploy/picodet_PPLCNet_x2_5_mainbody_lite_v1.0_serving登录后复制修改启动服务脚本
这里主要修改服务所需的检索库地址:
··· index_dir = "/home/aistudio/dataset/index_update" assert os.path.exists(os.path.join( index_dir, "vector.index")), "vector.index not found ..." assert os.path.exists(os.path.join( index_dir, "id_map.pkl")), "id_map.pkl not found ... " ···登录后复制启动服务In [ ]
# 启动服务,运行日志保存在 log.txt# 需要移步至终端执行此命令!python3 recognition_web_service.py &>log.txt &登录后复制发送请求In [12]
!python3 pipeline_http_client.py登录后复制
{'err_no': 0, 'err_msg': '', 'key': ['result'], 'value': ["[{'bbox': [132, 118, 355, 357], 'rec_docs': '四季宝花生酱', 'rec_scores': 0.85405}]"], 'tensors': []}Used Time: 0.16031885147094727s登录后复制关闭服务需要特别说明的是,PP-ShiTu在采用PaddleServing Pipeline部署时,每当检索库更新后,需要重启服务才能基于新的检索库进行匹配识别,这里可以采用如下命令关闭服务。In [ ]
# 此步需要移步至终端执行!python3 -m paddle_serving_server.serve stop登录后复制
游乐网为非赢利性网站,所展示的游戏/软件/文章内容均来自于互联网或第三方用户上传分享,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系youleyoucom@outlook.com。
同类文章
全网开骂!Claude订阅「封杀」OpenClaw,想用龙虾?得加钱!
机器之心编辑部Anthropic「封杀」OpenClaw 了?刚刚,不少 OpenClaw 用户收到一封来自 Anthropic 的邮件。邮件宣布,从太平洋时间 4 月 4 日中午 12 点起,Cl
机器人版GPT-3来了:任务成功率99%,「涌现」过后能临场发挥
编辑 | Sia这家原本低调的硅谷机器人 AI 公司,可能很难再低调下去了。今天,正试图把大模型那一套,完全搬进机器人世界的 Generalist。首次对外披露其最新一代基础模型 GEN-1。效果相
突发!Claude封杀OpenClaw
智东西作者 许丽思编辑 漠影智东西4月4日报道,刚刚,Anthropic宣布,从美国东部时间4月4日下午3点(北京时间4月5日凌晨3点)起,Claude禁用OpenClaw等第三方工具,用户只能通过
科技史上最贵的饭局:扎克伯格痛失DeepMind始末
编辑|杨文又到了八卦时间。最近,著名记者、作家塞巴斯蒂安・马拉比出了本新书,名叫《无限机器:德米斯・哈萨比斯、DeepMind 与超级智能的探索》。书中,他披露了一段扎克伯格与 DeepMind 失
Karpathy知识库「LLM Wiki」火爆了,全网围观讨论
机器之心编辑部还记得前几天,AI 领域知名学者 Andrej Karpathy 做客一档节目时,半开玩笑地提到:token 用不完会让人焦虑,就像患上了某种「AI 精神病」。这句话当时听起来有点夸张
- 日榜
- 周榜
- 月榜
1
2
3
4
5
6
7
8
9
10
相关攻略
2015-03-10 11:25
2015-03-10 11:05
2021-08-04 13:30
2015-03-10 11:22
2015-03-10 12:39
2022-05-16 18:57
2025-05-23 13:43
2025-05-23 14:01
热门教程
- 游戏攻略
- 安卓教程
- 苹果教程
- 电脑教程
热门话题

