tensorflow-object-detection

本文介绍了如何在Windows环境下使用TensorFlow Object Detection API进行目标检测模型的编译、COCO API的安装、数据标注、预训练模型微调及个性化训练过程。详细步骤包括模型构建、数据准备、模型训练和部署到树莓派的应用演示。

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

https://pan.baidu.com/s/1gu4-kfZHD4libN2PJ9_nTg
1234


https://tensorflow-object-detection-api-tutorial.readthedocs.io/en/latest/

集成了市面上比较流行的目标侦测框架
使用:

在c盘新建tensorflow文件夹,解压到该目录下,解压出来3个文件
models 是tensorflow提供的里面有社区的模型,打开readme看
scripts : 是老师提供的用来生成训练数据的脚本
workspace:主要在training——demo中做代码的编写

看版本:
conda activate tfenv
python -V
3.7.9

import tensorflow as tf
tf.__version__
tensorflow 版本 2.3.1

1.第一步编译谷歌给的模型
cd models
cd research
protoc object_detection\protos\*.proto --python_out=.
pip install cython   #c语言解析器,把Python翻译成C语言高效的去执行

#第3方的打标签的工具集cocoapi, 安装时间在1小时左右 github换成gitee 速度就很快
https://tensorflow-object-detection-api-tutorial.readthedocs.io/en/latest/install.html#coco-api-installation

windows:
    pip install git+https://github.com/philferriere/cocoapi.git#subdirectory=PythonAPI
linux:
    git clone https://github.com/cocodataset/cocoapi.git
    cd cocoapi/PythonAPI
    make
    cp -r pycocotools <PATH_TO_TF>/TensorFlow/models/research/
windows下 安装 cocoapi 必须安装visual studio 在环境变量里
https://go.microsoft.com/fwlink/?Linkld=691126 下载微软vcode编译器 visual studio

拷贝一个编译的脚本过来
在当前目录中 research
copy object_detection/packages/tf2/setup.py .      #windows下的命令 或手动复制
cp object_detection/packages/tf2/setup.py .
python -m pip install .
验证有没有安装完成
python object_detection/builders/model_builder_tf2_test.py


开始使用框架
1.
把图片数据放入:tensorflow/workspace/training_demo/images下
2.开始标注操作
 进tensorflow/workspace/training_demo/annotations 中有label_map.pbtxt文件,指定分类的内容用的

3.去git下载 labelimg 图像分类器
https://github.com/tzutalin/labelImg.git

Windows + Anaconda 安装
解压labelImg放进tensorflow与其他三个放一起
进入labelImg-master目录
切换conda环境
conda activate tfenv
conda install pyqt=5
conda install -c anaconda lxml
pyrcc5 -o libs/resources.py resources.qrc
python labelImg.py   #打开标注软件
python labelImg.py [IMAGE_PATH] [PRE-DEFINED CLASS FILE] #这句不需要执行
 1.open Dir 选择tensorflow/workspace/training_demo/images下的需要训练的图片
   1.1 选择create rectbox 框出图片中的数据 ,在弹出款中填写框中的物体名字,不支持中文
       1.1.2把以前的数据名字清空比如dog之类的,labelImg放进tensorflow/data/predefined_classes.txt中的数据清空    
   
   1.2 ctrl+s保存0.xml
 2.tensorflow/scripts/preprocessing/generate_tfrecord.py 数据转换二进制格式 train.record 是一个二进制文件,看不懂的
  2.1 conda activate tfenv
  2.2  train
  python generate_tfrecord.py -x /home/star/Downloads/1/tensorflow/workspace/training_demo/images/train -1 /home/star/Downloads/1/tensorflow/workspace/training_demo/annotations/label_map.pbtxt -o /home/star/Downloads/1/tensorflow/workspace/training_demo/annotations/train.record
  2.3      test
  python generate_tfrecord.py -x /home/star/Downloads/1/tensorflow/workspace/training_demo/images/test -1 /home/star/Downloads/1/tensorflow/workspace/training_demo/annotations/label_map.pbtxt -o /home/star/Downloads/1/tensorflow/workspace/training_demo/annotations/test.record

3.预训练的模型 /home/star/Downloads/1/tensorflow/workspace/training_demo/pre-trained-models ,空文件夹
              /home/star/Downloads/1/tensorflow/models/community/readme
pwd/home/star/Downloads/1/tensorflow/models/research/object_detection/g3doc 帮助文档  ,tf2_detection_zoo.md 目标侦测的动物园
 [SSD MobileNet V2 FPNLite 640x640](http://download.tensorflow.org/models/object_detection/tf2/20200711/  ssd_mobilenet_v2_fpnlite_640x640_coco17_tpu-8.tar.gz) #最适合移动端跑的一个目标侦测的一个算法
把它下载到tensorflow/workspace/training_demo/pre-trained-models 文件夹中
4.对预训练的模型的基础上做扩展,进行新一轮的训练
 把预训练的模型下载到tensorflow/workspace/training_demo/pre-trained-models 文件夹中    ,解压出3个文件                                               
 4.1把解压出来的训练的管道文件pipeline.config拷贝到我们要训练的文件夹中: /home/star/Downloads/1/tensorflow/workspace/training_demo/models 中新建一个文件夹 my_ssd_mobilenet_v2_fpnlite,放在此文件夹下

4.2 pipeline.config改第3行 num_classes:90 训练的模型总共有多少个种类?比如识别斑马线 停车 和直行 共3个种类,这里改成3,num_classes:3

4.3 pipeline.config改第165行  fine_tune_checkpoint: "PATH_TO_BE_CONFIGURED" 加载模型微调的文件,刚刚下载的目录中 fine_tune_checkpoint: "pre-trained-models/ssd_mobilenet_v2_fpnlite_640x640_coco17_tpu-8/checkpoint/ckpt-0"

4.4 pipeline.config改第171行 默认fine_tune_checkpoint_type: "classification"分类任务,改成fine_tune_checkpoint_type: "detection" 识别的任务不止要知道它的类别,还要知道物体的位置

4.5 pipeline.config改第175行   label_map_path: "PATH_TO_BE_CONFIGURED" 改成  label_map_path: "annotations/label_map.pbtxt"

4.6 pipeline.config改第177行 input_path: "PATH_TO_BE_CONFIGURED" 训练集的数据路径 input_path: "annotations/train.record"


4.7 pipeline.config改第185行   label_map_path: "PATH_TO_BE_CONFIGURED" 改成  label_map_path: "annotations/label_map.pbtxt"

4.8 pipeline.config改第189行 input_path: "PATH_TO_BE_CONFIGURED" 测试集的数据路径 input_path: "annotations/test.record"

5.就可以开始训练了
cd tensorflow/workspace/training_demo下
conda activate tfenv
python model_main_tf2.py --model_dir=models\my_ssd_mobilenet_v2_fpnlite --pipeline_config_path=models\my_ssd_mobilenet_v2_fpnlite\pipeline.config
训练时间大概 5到13个小时,训练结果在models\my_ssd_mobilenet_v2_fpnlite\目录下;

6.把文件导出来,转成我们可以理解和操作的模型
cd tensorflow/workspace/training_demo下
.\exporter_main_v2.py --input_type image_tensor --pipeline_config_path .\models\my_ssd_mobilenet_v2_fpnlite\pipeline.config --trained_checkpoint_dir .\models\my_ssd_mobilenet_v2_fpnlite\ --output_directory .\exported-models\my_mobilenet_model
6.1 在/tensorflow/workspace/training_demo/exported-models下会多出来一个文件夹my_mobilenet_model,中有3个文件 两文件夹checkpoint ,saved_model,pipeline.config,其中saved_model就是训练导出来的模型文件,导出模型的时间大概几分钟

7.数据预测:
TF-image-od.py 预测图片  #23行指定预测的图片 default='images/test/i-1e092ec6eabf47f9b85795a9e069181b.jpg')  #default='images/test/7.jpg'
python TF-image-od.py
TF-video-od.py 预测视频流  #46行 PATH_TO_SAVED_MODEL = PATH_TO_MODEL_DIR + "/saved_model" 加载目录模型,#89行利用opencv打开摄像头 video = cv2.VideoCapture(VIDEO_PATHS) , video = cv2.VideoCapture(1)

出现 error:(-215:Assertion failed)需要重新插拔摄像头,奥比中光的摄头在windows下不稳定
1秒7到8帧

8.部署树莓派
1.环境安装 需要交叉编译的文件,注意arm平台
2.拷贝exported-models文件夹,及TF-image-od.py ,TF-video-od.py这两个文件就可以使用了,其余都是训练时使用的文件不需要考被到树莓派上

### TensorFlow Object Detection API 的安装方法 #### 准备工作 为了顺利安装 TensorFlow Object Detection API,需先确认环境配置满足需求。通常推荐使用虚拟环境来隔离依赖项[^1]。 #### 安装步骤 以下是基于 Ubuntu 18.04 平台的安装指南: 1. **克隆 TensorFlow Models 存储库** 需要从 GitHub 上获取官方存储库中的 `object_detection` 文件夹。 ```bash git clone https://github.com/tensorflow/models.git cd models/research/ ``` 2. **编译 Protobufs** 使用 Protocol Buffers 编译器 (`protoc`) 将 `.proto` 文件转换为 Python 可读文件。 ```bash protoc object_detection/protos/*.proto --python_out=. ``` 如果未安装 `protoc`,可以通过以下命令安装: ```bash sudo apt-get install protobuf-compiler python-pil python-lxml python-tk ``` 3. **设置路径变量** 修改当前用户的环境变量以便加载自定义模块。 ```bash export PYTHONPATH=$PYTHONPATH:`pwd`:`pwd`/slim echo $PYTHONPATH ``` 4. **运行单元测试** 测试安装是否成功完成。 ```bash python object_detection/builders/model_builder_test.py ``` 若无错误提示,则说明安装正常[^4]。 5. **验证依赖版本** 确认所使用的 TensorFlow 版本兼容目标硬件架构(CPU 或 GPU)。对于较新版本模型可能仅支持特定范围内的 TF 版本[^2]。 6. **额外组件准备** 根据实际项目需求下载预训练模型权重并放置到指定位置;同时调整配置参数适配数据集结构[^3]。 ```python import tensorflow as tf from object_detection.utils import config_util from object_detection.protos import pipeline_pb2 ``` 以上即完成了基本框架搭建过程,请依据具体应用场景进一步定制化开发流程。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_无往而不胜_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值