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 等, 亦可以等待报错时安装相应模块
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 目录下
- protoc object_detection/protos/*.proto --python_out=.
- # 运行完成后, 可以检查 object_detection/protos / 文件夹, 如果每个 proto 文件都成了对应的以 py 为后缀的 python 源码, 就说明编译成功了
- # 添加环境变量: export PYTHONPATH=$PYTHONPATH:/home/dzf/models/research:/home/dzf/models/research/slim
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"
下载地址:
注意下载的 models 要与你选择的 model.config 一致 本例为 加压后有文件夹 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 numpy as np
- import tensorflow as tf
- from 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 = '/home/dzf/train/frozen_inference_graph.pb'
- self.PATH_TO_CKPT = '/home/dzf/dataset/models/faster_rcnn_inception_resnet_v2_atrous_coco_2018_01_28/frozen_inference_graph.pb'
- #self.PATH_TO_CKPT = '/home/dzf/train_model/frozen_inference_graph.pb'
- self.PATH_TO_LABELS = '/home/dzf/dataset/tools/pascal_label_map.pbtxt'
- self.NUM_CLASSES = 20
- self.detection_graph = self._load_model()
- self.category_index = self._load_label_map()
- def _load_model(self):
- detection_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_graph
- def _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_index
- def detect(self, image):
- with self.detection_graph.as_default():
- with tf.Session(graph=self.detection_graph) as sess:
- # Expand dimensions since the model expects images to have shape: [1, None, None, 3]
- 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(0)
- if __name__ == '__main__':
- #image = cv2.imread('/home/dzf/dataset/VOCdevkit/VOC2012/JPEGImages/2007_000027.jpg')
- #image = cv2.imread('/home/dzf/Downloads/aeroplane.jpeg')
- image = cv2.imread('/home/dzf/Downloads/people.jpg')
- detecotr = TOD()
- detecotr.detect(image)
- # 本例中在原始模型训练的基础上的训练一定次数 生成 model.ckpt 之后转为 pb 文件 进行目标检测 没有检测框
- # 若使用原始模型的 pb 文件 faster_rcnn_inception_resnet_v2_atrous_coco_2018_1_29/frozen_interence_inception.pb 可以显示检测框, 至于什么原因还没有找到
来源: http://www.bubuko.com/infodetail-3302511.html