tensorflow-object-detection

15 篇文章 0 订阅
3 篇文章 0 订阅

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这两个文件就可以使用了,其余都是训练时使用的文件不需要考被到树莓派上

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

_无往而不胜_

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

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

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

打赏作者

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

抵扣说明:

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

余额充值