Tensorflow Object Detection API 使用步骤

本文详细介绍使用TensorFlow Object Detection API进行目标检测的全过程,包括环境搭建、库安装、数据预处理、模型训练及预测。文章涵盖关键步骤如库升级、配置文件调整、训练与评估流程,以及常见错误排查。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

需要安装的库有:
pandas
针对PIL 缺失, 需要装的包,
conda install Pillow

matplotlib

装了matplotlib,可能因为一会儿用 pip装包,一会儿conda,导致 numpy被降级
出现

module compiled against API version 0xc but this version of numpy is 0xb

这样的错误

又使用

pip install numpy --upgrade

进行升级!

pycocotools
注意: 这个make 用的python 环境 要和 后面执行训练用的 python 版本一致,否则
No module named ‘pycocotools._mask’
也就是说,后面如果是用 conda环境中 tensorflow 和 python,
这里也要 切换到 同样的环境中执行!!!

git clone https://github.com/cocodataset/cocoapi.git
cd cocoapi/PythonAPI
make
cp -r pycocotools <path_to_tensorflow>/models/research/

The Tensorflow Object Detection API uses Protobufs to configure model and training parameters. Before the framework can be used, the Protobuf libraries must be compiled. This should be done by running the following command from the tensorflow/models/research/ directory:

# From tensorflow/models/research/
protoc object_detection/protos/*.proto --python_out=.

环境变量设置,这个是必要的
不然出现

ImportError: No module named 'nets'

类似这样的问题

# From tensorflow/models/research/
export PYTHONPATH=$PYTHONPATH:`pwd`:`pwd`/slim

训练步骤:
1 将标注数据转为一定的CSV格式,

2 执行 python 文件, 分别生成 train.record 和 test.record
用的代码来自:https://blog.youkuaiyun.com/RobinTomps/article/details/78115628
注意,这个python 代码中 函数 class_text_to_int 中 要修改 关于 类别名称的代码,。还有就是 图片文件扩展名 要根据实际情况进行 修改

3 建一个 label_map.pbtxt 文件, 这个文件是 类别名称 和 ID 的对应关系。id序号注意与前面创建CSV文件时保持一致,从1开始

4 拷贝一个 ssd_mobilenet_v1_coco.config 或者其他模型 配置文件, 修改里面的东西,主要有以下 几项:
num_classes: 1
train_input_reader 中 input_path 和 label_map_path
eval_input_reader中 input_path 和 label_map_path

如果使用预训练模型,那么from_detection_checkpoint 为true,
并且设置fine_tune_checkpoint 的路径

5 执行 model_main.py 开始训练

# From the tensorflow/models/research/ directory
python object_detection/model_main.py --pipeline_config_path=/home/bst-cd/panghaiyan/DataSet/demo_data/trainsets/faster_rcnn_resnet50_coco.config --model_dir=object_detection/training --num_train_steps=50000 --num_eval_steps=2000 --alsologtostderr

预测的步骤:
1 先要 导出模型, 这个 使用 脚本完成,注意命令行参数 涉及到的路径
2 修改 导出模型的路径PATH_TO_FROZEN_GRAPH
3 修改 标签路径
4 PATH_TO_TEST_IMAGES_DIR,这是测试图片路径,注意是否需要修改图像扩展名
5 cv2.imwrite(‘test_dataset/’ 这是结果保存路径
6 data.to_csv(‘detectRes/test_dataset2.csv’, 检测结果保存

遇到的问题,训练了一些次数以后,报错
ValueError: Tensor conversion requested dtype string for Tensor with dtype float32: ‘Tensor(“arg0:0”, shape=(), dtype=float32, device=/device:CPU:0)’

这是 eval 相关配置不对导致的,比如 eval_input_reader 中的record 文件路径没写对!

TensorFlow Object Detection API 是一个开源项目,它提供了一系列基于 TensorFlow 的工具和库,用于实现目标检测任务。对于 macOS 系统,我们可以通过以下步骤使用 TensorFlow Object Detection API: 1. 安装 TensorFlow:在 macOS 上安装 TensorFlow使用 TensorFlow Object Detection API 的前提。你可以通过 pip 命令进行安装,例如在终端中执行 `pip install tensorflow`。 2. 下载 TensorFlow Object Detection API:打开终端并导航到适合你的工作目录中,然后使用 git 命令来克隆 TensorFlow Object Detection API 的 GitHub 仓库,例如执行 `git clone https://github.com/tensorflow/models.git`。 3. 安装依赖项:进入克隆的模型目录中,找到 research 文件夹并进入。然后运行 `pip install -r object_detection/requirements.txt` 命令来安装所需的依赖项。 4. 下载预训练模型:在 TensorFlow Object Detection API 中,我们可以使用预训练的模型来进行目标检测。你可以从 TensorFlow Model Zoo 中下载适合你任务的模型,并将其解压到你的工作目录中。 5. 运行实例代码:在 research/object_detection 目录中,你可以找到一些示例代码,用于训练、评估和使用目标检测模型。可以通过阅读这些示例代码并根据自己的需求进行修改。例如,你可以使用 `python object_detection/builders/model_builder_tf2_test.py` 命令来运行一个模型的测试。 以上是在 macOS 上使用 TensorFlow Object Detection API 的基本步骤,你可以根据你的具体需求进行更多的深入研究和调整。希望这些信息能帮助到你!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值