1.安装tensorflow(version>=1.4.0)

2.部署tensorflow models
  - 在这里下载
  - 解压并安装
    - 解压后重命名为models复制到tensorflow/目录下
    - 在linux下
      - 进入tensorflow/models/research/目录,运行protoc object_detection/protos/*.proto --python_out=.
      - 在~/.bashrc file.中添加slim和models/research路径
      export PYTHONPATH=$PYTHONPATH:/path/to/slim:/path/to/research
    - 在windows下
      - 下载protoc-3.3.0-win32.zip(version==3.3,已知3.5版本会报错) 
      - 解压后将protoc.exe放入C:\Windows下
      - 在tensorflow/models/research/打开powershell,运行protoc object_detection/protos/*.proto --python_out=.

3.训练数据准备(标记分类的图片)
  - 安装labelImg 用来手动标注图片 ,图片需要是png或者jpg格式
  - 标注信息会被保存为xml文件,使用 这个脚本 将所有xml文件转换为一个csv文件(xml文件路径识别在29行,根据情况自己修改)
  - 把生成的csv文件分成训练集和测试集

4.生成TFRecord文件
  - 使用 这个脚本 将两个csv文件生成出两个TFRecord文件(训练自己的模型,必须使用TFRecord格式文件。图片路径识别在86行,根据情况自己修改)

5.创建label map文件
  id需要从1开始,class-N便是自己需要识别的物体类别名,文件后缀为.pbtxt
    item{
      id:1
      name: 'class-1'
      }
    item{
      id:2
      name: 'class-2'
      }

6.下载模型并配置文件
  - 下载一个模型(文件后缀.tar.gz)
  - 修改对应的训练pipline配置文件 
    - 查找文件中的PATH_TO_BE_CONFIGURED字段,并做相应修改
      - num_classes 改为你模型中包含类别的数量
      - fine_tune_checkpoint 解压.tar.gz文件后的路径 + /model.ckpt
      - from_detection_checkpoint:true
      - train_input_reader
        - input_path 由train.csv生成的record格式训练数据
        - label_map_path 第5步创建的pbtxt文件路径
      - eval_input_reader
        - input_path 由test.csv生成的record格式训练数据
        - label_map_path 第5步创建的pbtxt文件路径

7. 训练模型
  - 进入tensorflow/models/research/目录,运行
  python object_detection/train.py --logtostderr  --pipeline_config_path=${PATH_TO_YOUR_PIPELINE_CONFIG} //第六步中修改的pipline配置文件路径// --train_dir=${PATH_TO_TRAIN_DIR} //生成的模型保存路径//

8.导出模型
  - 在第7步中,--train_dir指向的路径中会生成一系列训练中自动保存的checkpoint,一个checkpoint由三个文件组成,后缀分别是.data-00000-of-00001 .index和.meta,任然在第7步的路径中,运行
    python object_detection/export_inference_graph.py \

--input_type image_tensor  \

--pipeline_config_path ${PIPELINE_CONFIG_PATH} //第六步中修改的pipline配置文件路径\--trained_checkpoint_prefix ${TRAIN_PATH} //上述的一个checkpoint,例如model.ckpt-112254 \ --output_directory ${OUTPUT_PATH} //输出模型文件的路径//

9.使用新模型识别图片
  调用predict.py

首先导入包

import time
import cv2
import numpy as np
import tensorflow as tf
import pandas as pd
import math
import osfrom object_detection.utils import label_map_util
from object_detection.utils import visualization_utils as vis_util

然后定义类和函数

class TOD(object):def __init__(self):self.PATH_TO_CKPT = r'D:/xiangchuang/new_train_model/result/frozen_inference_graph.pb'self.PATH_TO_LABELS = r'D:/xiangchuang/pig.pbtxt'self.NUM_CLASSES = 1self.detection_graph = self._load_model()self.category_index = self._load_label_map()def _load_model(self):global detection_graphdetection_graph = tf.Graph()with detection_graph.as_default():od_graph_def = tf.GraphDef()with tf.gfile.GFile(self.PATH_TO_CKPT, 'rb') as fid:serialized_graph = fid.read()od_graph_def.ParseFromString(serialized_graph)tf.import_graph_def(od_graph_def, name='')return detection_graphdef _load_label_map(self):label_map = label_map_util.load_labelmap(self.PATH_TO_LABELS)categories = label_map_util.convert_label_map_to_categories(label_map,max_num_classes=self.NUM_CLASSES,use_display_name=True)category_index = label_map_util.create_category_index(categories)return category_indexdef detect(self, image):image_np_expanded = np.expand_dims(image, axis=0)image_tensor = self.detection_graph.get_tensor_by_name('image_tensor:0')boxes = self.detection_graph.get_tensor_by_name('detection_boxes:0')scores = self.detection_graph.get_tensor_by_name('detection_scores:0')classes = self.detection_graph.get_tensor_by_name('detection_classes:0')num_detections = self.detection_graph.get_tensor_by_name('num_detections:0')# Actual detection.(boxes, scores, classes, num_detections) = sess.run([boxes, scores, classes, num_detections],feed_dict={image_tensor: image_np_expanded})# Visualization of the results of a detection.vis_util.visualize_boxes_and_labels_on_image_array(image,np.squeeze(boxes),np.squeeze(classes).astype(np.int32),np.squeeze(scores),self.category_index,use_normalized_coordinates=True,line_thickness=8)cv2.namedWindow("detection", cv2.WINDOW_NORMAL)cv2.imshow("detection", image)cv2.waitKey(1)

最后执行

if __name__ == '__main__':detector = TOD()with detection_graph.as_default():with tf.Session(graph=detection_graph) as sess:cap = cv2.VideoCapture(r'Your Vedio Path')n = 1success = Truewhile  (success) :success, frame = cap.read()t1=time.clock()print('正在预测第%d张' % n)n = n + 1if success == True:detector.detect(frame)t2=time.clock()t = t2-t1print('cost time %f s'%t)cv2.destroyAllWindows()

即可以实现基于视频的目标目标检测

参考文档

https://gist.github.com/douglasrizzo/c70e186678f126f1b9005ca83d8bd2ce
https://towardsdatascience.com/how-to-train-your-own-object-detector-with-tensorflows-object-detector-api-bec72ecfe1d9

使用object detection训练并识别自己的模型相关推荐

  1. win下使用TensorFlow object detection训练自己模型

    win下使用TensorFlow object detection训练自己模型 1. 环境 2.xml生成csv文件,再生成record文件 2.1 对训练文件和测试文件都使用以下两个文件分别生成自己 ...

  2. 人工智能python3+tensorflow人脸识别_机器学习tensorflow object detection 实现人脸识别...

    object detection是Tensorflow很常用的api,功能强大,很有想象空间,人脸识别,花草识别,物品识别等.下面是我做实验的全过程,使用自己收集的胡歌图片,实现人脸识别,找出胡歌. ...

  3. object detection训练自己数据

    1.用labelImg标自己数据集. 并将图片存放在JPEGImages中,xml存放在Annotations中 2.分离训练和测试数据 import os import randomtrainval ...

  4. Object Detection+目标检测概述及其常见模型总结

    概述 普通的深度学习监督算法主要是用来做分类,如图1(1)所示,分类的目标是要识别出图中所示是一只猫.而在ILSVRC(ImageNet Large Scale Visual Recognition ...

  5. 使用tensorflow object detection api训练自己的数据集

    简介 使用tensorflow object detection训练自己的数据集时,可能会出现 AttributeError: module 'tensorflow.contrib.data' has ...

  6. Object Detection in 20 Years A Survey 论文阅读笔记

    文章链接:https://arxiv.org/pdf/1905.05055.pdf 1.Introduction 作为计算机视觉的基本问题之一,目标检测构成了许多其他计算机视觉任务的基础,例如实例分割 ...

  7. TensorFlow 2 Object Detection API 教程: 安装

    TensorFlow 2 Object Detection API 教程: 安装 本教程针对的是TensorFlow 2.4,它是TensorFlow 2.x的最新稳定版本. 这是一个循序渐进的教程/ ...

  8. Implicit 3D Orientation Learning for 6D Object Detection from RGB Images

    从RGB图像中进行6D目标检测的隐式三维方向学习 论文是我自己翻译的,限于本人的水平,不到之处请多包涵 摘要:我们提出了一种基于RGB的实时管道[1],用于物体检测和6D姿态估计.我们新颖的3D方向估 ...

  9. TPH-YOLOv5: Improved YOLOv5 Based on Transformer Prediction Head for Object Detection on Drone-captu

    TPH-YOLOv5 文章目录 TPH-YOLOv5 参考 Introduciton Structure CSPDarknet53 Transformer CBAM Ms-testing and mo ...

最新文章

  1. 高斯回归过程应用例子
  2. CLion上用platformIO开发esp32,CLion开发esp32详细配置流程
  3. Mesos在传统金融企业的实践——平安科技陈秋浩实录分享
  4. 每天一香蕉,长成螺纹钢
  5. python线程join
  6. sql server2008给数据表,字段,添加修改注释
  7. LeetCode 447. 回旋镖的数量(哈希map+组合数)
  8. 《面向对象程序设计》课程作业(七)
  9. 导致jquery.min.map 404 (Not Found)错误的原因
  10. mac建立sftp连接_【5分钟玩转Lighthouse】Win10远程连接同步代码
  11. SpringBoot入门教程(七)整合themeleaf+bootstrap
  12. [雪峰磁针石博客]数据仓库快速入门教程1简介
  13. 数据结构与与算法之插入排序
  14. java 双倍长密钥3des_用Java实现的单倍长密钥DES、双倍长密钥3DES和Mac计算
  15. 祝广大运维人:2020新年快乐!
  16. Ubuntu 16.04安装Docker
  17. CEBX格式的文档如何转换为PDF格式文档、DOCX文档?
  18. springMVC注解的意思
  19. The import com. cannot be resolved
  20. 【leetcode刷题】[简单]441. 排列硬币(arranging coins)-java

热门文章

  1. Java常见内存溢出异常分析
  2. SIFT特征提取-应用篇
  3. 跟我一起写 Makefile(八)
  4. 斯坦福大学机器学习第八课“神经网络的表示(Neural Networks: Representation)”
  5. 用Python实现一个1加到n求和功能的函数
  6. 从hello server开始,到hello client结束
  7. java websocket 客户端
  8. Kali Linux
  9. 基于Docker + Consul + Nginx + Consul-template的服务负载均衡实现
  10. GDI绘制时钟效果,与系统时间保持同步,基于Winform