目标检测_1
Keywords :Ubuntu,VOC,Object_detection
1,环境搭建
使用anaconda3, 先安装
换源,若不然会非常慢 方式参考 https://www.cnblogs.com/Dean0731/p/11560371.html
export PATH="/usr/local/anaconda3/bin:$PATH"
新建虚拟环境 本例python=3.5
进入环境本例安装的tensorflow=1.14.0
安装其他依赖包,pillow,lxml 等,亦可以等待报错时安装相应模块
本例环境 windwos:python3.6+tensorflow-1.12
windwos:python3.6+tensorflow-gpu-1.12+cuda8.0
# 注意选择cuda版本 有最小计算能力 ,需要小于你显卡的计算能力,若不匹配的话 可能会忽略显卡的加速作用,而且很卡
# 本例使用GT820m 2.1计算能力 cuda9.0 计算能力最小为3.7
# 显卡计算能力查询 https://developer.nvidia.com/cuda-gpus
# tensorflow---》cuda--》cudnn https://tensorflow.google.cn/install/source
# python----决定tensorflow的版本
# 先根据显卡选cuda
linux:python3.5+tensorflow-1.14
2,下载数据
- models:https://github.com/tensorflow/models.git 使用git下载 或直接下载zip
- VOC2012:链接: https://pan.baidu.com/s/12IP4iyL9hN5Dohzkm8wi7A 提取码: q31q
也可到官网下载,比较慢 visual object classes 2012 一共20类别,一共5个文件夹- JPEGImages存放图片;
- Annotation存放对图片的标注信息(XML文件),即外包边框bounding box的位置信息;
- SegmentationClass和SegmentationObject存放了图片的分割前景。
- ImageSets是对图片集合的描述,分别对应了不同的竞赛任务,例如Layout表示图片中人体部位的数据,Main表示每个图片包含的分类信息(20个类别),Segmentation表示用于分割的数据,2007没有action,2012有了action数据,表示图片中人的动作。
注:train 训练集,trainval 训练集中的测试集,val 测试集
3,models 部署
# 生成objection——detection/protos 下的py文件 models/research 目录下
linux protoc object_detection/protos/*.proto --python_out=.
windows Get-ChildItem object_detection/protos/*.proto | Resolve-Path -Relative | %{ protoc $_ --python_out=. }
# 运行完成后,可以检查object_detection/protos/文件夹,如果每个proto文件都成了对应的以py为后缀的python源码,就说明编译成功了
# 添加环境
# 需要当前虚拟环境中进入在这2目录中(research,research/slim)python setup.py install 把object_detection等模块装入
# 若还出现错误变量(报当前线程错误等)可试试添加环境变量:
# export PYTHONPATH=$PYTHONPATH:/home/dzf/models/research:/home/dzf/models/research/slim
# PYTHONPATH:上边两个目录
4,生成数据
-
准备:VOC2012 解压后的数据 VOCdevkit
- models/research/objection_detection/create_pascal_tf_record.py # 将数据转换为record 的文件
- export_inference_graph.py # 后边用到,将模型转换为应用
- pascal_label_map.pbtxt # 标签
修改create_pascal_tf_record.py 164行
改为 data_dir, year, 'ImageSets', 'Main',FLAGS.set + '.txt' 对应文件目录
-
生成数据
# 可能需要pillow,lxml等包 # data_dir voc目录 即为解压后文件夹 # year 接收VOC2007 VOC2012 # set 表示使用train训练 # output_path 生成的文件路径 # 可能会报错 data/pascal_label_map.pbtxt 找不到 移到上级目录运行即可 python create_pascal_tf_record.py --set=train或val 表示时生成训练数据还是测试数据 --data_dir=.../VOCdevkit --year=VOC2012 默认 VOC2007 --output_path=..../训练文件.record(pascal_train.record) pascal_train.record # 生成的训练数据 pascal_val.record # 生成的测试数据
5,模型训练
-
models:在objection_detection/sample/config/xxxxxx.config # models配置文件
包含训练次数,分类数,测试集,测试集标签,训练集,训练集标签的位置
重要:接其官方的训练结果 fine_tune_checkpoint: "下载的他人models解压后的文件夹/model.ckpt"
下载地址:https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/detection_model_zoo.md
注意下载的models要与你选择的model.config一致 本例为 faster_rcnn_inception_resnet_v2_atrous_coco 加压后有文件夹faster_rcnn_inception_resnet_v2_atrous_coco_2018_1_29 -
修改配置文件
# 指定接着训练的模型,若要重新开始注释本行,下行改为false即可 fine_tune_checkpoint: "D:\\\\faster_rcnn_inception_resnet_v2_atrous_coco_2018_1_29\\model.ckpt" from_detection_checkpoint: true # 训练集,训练集标签,测试集,测试集标签 input_path: "D:\\\pascal_train.record" label_map_path: "D:\\data\\pascal_label_map.pbtxt" input_path: "D:\\models\\pascal_val.record" label_map_path: "D:\\models\\pascal_label_map.pbtxt"
-
# 开始训练:cpu训练较慢,可在配置文件修改次数,若出现内存溢出,也可修改config中的图片大小600x1024,可改小一点 python legacy/train.py --train_dir=/home/dzf/train # model # 输出目录 --pipeline_config_path=/home/dzf/dataset/models/faster_rcnn_inception_resnet_v2_atrous_coco.config # 配置文件 # 新版的models 是用的是object_detection/model_main.py --model_dir=..... # 旧版的models 使用的是object_detection/train.py --train_dir=... # 注意形参名称改变了,本例使用的是新版的models但使用的是legacy/train.py
6,导出模型为pb文件
python export_inference_graph.py
--input_type=image_tensor
--pipeline_config_path=/...../models/faster_rcnn_inception_resnet_v2_atrous_coco.config
--trained_checkpoint_prefix=/...../train/model.ckpt-10
--output_directory=/..../train
注:上编的路径尽量使用绝对路径,不要使用相对路径和~符号 可能报错
生成frozen_inference_graph.pb文件 及其他文件
7,使用pd文件检测图片
import cv2
import os
import pathlib
import numpy as np
import os
import six.moves.urllib as urllib
import sys
import tarfile
import tensorflow as tf
import zipfile
from collections import defaultdict
from io import StringIO
from matplotlib import pyplot as plt
from PIL import Image
from IPython.display import display
from object_detection.utils import ops as utils_ops
from object_detection.utils import label_map_util
from object_detection.utils import visualization_utils as vis_util
# patch tf1 into `utils.ops`
utils_ops.tf = tf.compat.v1
# Patch the location of gfile
tf.gfile = tf.io.gfile
def load_model(model_name):
# here
# base_url = 'http://download.tensorflow.org/models/object_detection/'
# model_file = model_name + '.tar.gz'
# model_dir = tf.keras.utils.get_file(
# fname=model_name,
# origin=base_url + model_file,
# untar=True)
#model_dir = pathlib.Path("ssd_mobilenet_v1_coco_2017_11_17")/"saved_model"
#model_dir = pathlib.Path("legacy/pb/pb56339")/"saved_model"
model_dir = pathlib.Path("pb/pb1218")/"saved_model"
model = tf.saved_model.load(str(model_dir))
model = model.signatures['serving_default']
return model
# List of the strings that is used to add correct label for each box.
PATH_TO_LABELS = 'data/pascal_label_map.pbtxt'
category_index = label_map_util.create_category_index_from_labelmap(PATH_TO_LABELS, use_display_name=True)
# If you want to test the code with your images, just add path to the images to the TEST_IMAGE_PATHS.
PATH_TO_TEST_IMAGES_DIR = pathlib.Path('test_images2')
TEST_IMAGE_PATHS = sorted(list(PATH_TO_TEST_IMAGES_DIR.glob("*.jpg")))
TEST_IMAGE_PATHS
model_name = 'ssd_mobilenet_v1_coco_2017_11_17'
detection_model = load_model(model_name)
def run_inference_for_single_image(model, image):
image = np.asarray(image)
# The input needs to be a tensor, convert it using `tf.convert_to_tensor`.
input_tensor = tf.convert_to_tensor(image)
# The model expects a batch of images, so add an axis with `tf.newaxis`.
input_tensor = input_tensor[tf.newaxis,...]
# Run inference
output_dict = model(input_tensor)
# All outputs are batches tensors.
# Convert to numpy arrays, and take index [0] to remove the batch dimension.
# We're only interested in the first num_detections.
num_detections = int(output_dict.pop('num_detections'))
output_dict = {key:value[0, :num_detections].numpy()
for key,value in output_dict.items()}
output_dict['num_detections'] = num_detections
# detection_classes should be ints.
output_dict['detection_classes'] = output_dict['detection_classes'].astype(np.int64)
# Handle models with masks:
if 'detection_masks' in output_dict:
# Reframe the the bbox mask to the image size.
detection_masks_reframed = utils_ops.reframe_box_masks_to_image_masks(
output_dict['detection_masks'], output_dict['detection_boxes'],
image.shape[0], image.shape[1])
detection_masks_reframed = tf.cast(detection_masks_reframed > 0.5,
tf.uint8)
output_dict['detection_masks_reframed'] = detection_masks_reframed.numpy()
print(output_dict['detection_boxes'])
print(output_dict['detection_classes'])
print(output_dict['detection_scores'])
return output_dict
def show_inference(model, image_path):
# the array based representation of the image will be used later in order to prepare the
# result image with boxes and labels on it.
image_np = np.array(Image.open(image_path))
# Actual detection.
output_dict = run_inference_for_single_image(model, image_np)
# Visualization of the results of a detection.
vis_util.visualize_boxes_and_labels_on_image_array(
image_np,
output_dict['detection_boxes'],
output_dict['detection_classes'],
output_dict['detection_scores'],
category_index,
instance_masks=output_dict.get('detection_masks_reframed', None),
use_normalized_coordinates=True,
line_thickness=8)
#display(Image.fromarray(image_np))
cv2.namedWindow("detection", cv2.WINDOW_NORMAL)
cv2.imshow("detection", image_np)
cv2.waitKey(0)
for image_path in TEST_IMAGE_PATHS:
show_inference(detection_model, image_path)
# 本例中在原始模型训练的基础上的训练一定次数 生成model.ckpt 之后转为pb文件 进行目标检测 没有检测框
# 若使用原始模型的pb文件 faster_rcnn_inception_resnet_v2_atrous_coco_2018_1_29/frozen_interence_inception.pb 可以显示检测框,至于什么原因还没有找到
# 对于上面所述的现象,我重新搭建了一次环境,上面的train,export 等过程,我都是将py文件复制都单独文件夹 进行操作,
# 本次搜有的操作都位于models中,将数据放入object_detection中等,可解决上述问题。但还是会出现某些图片不能检测的问题,也可能是由于训练测试过少的原因。
# 使用model_main.py 预测时可能效果较好
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,如有问题, 可评论咨询.