环境说明

  • 依赖环境安装eIQ官方指南:
name: eiq_auto
channels:
- conda-forge
- defaults
dependencies:
- numpy=1.18.1=py36h4f9e942_0
- onnx==1.6.0
- opencv==4.2.0
- pandas=0.24.2=py36he6710b0_0
- pillow=7.0.0=py36hb39fc2d_0
- protobuf=3.9.2=py36he6710b0_0
- pytest=5.3.0=py36_0
- python=3.6.10=h0371630_0
- tensorflow=1.14.0=mkl_py36h2526735_0
pip:
- onnxruntime==1.0.0

==================================================================================

1.安装tensorflow object detection API

sudo apt-get install protobuf-compiler python-pil python-lxml python-tk
pip install --user Cython
pip install --user contextlib2
pip install --user jupyter
pip install --user matplotlib
  • 下载models
git clone https://github.com/tensorflow/models.git
  • 安装cocoAPI
pip install --user pycocotools
  • 使用Protobuf Compilation
protoc object_detection/protos/*.proto --python_out=.
  • 添加到PYTHONPATH
# From tensorflow/models/research/
export PYTHONPATH=$PYTHONPATH:/mnt/d/0-WORK/models/models-master/research:/mnt/d/0-WORK/models/models-master/research/slim
source ~/.bashrc
  • 注意以上绝对路径填正确
  • 测试是否完成
python object_detection/builders/model_builder_tf1_test.py
  • 如果出现以下结果表示API已成功安装:

2.使用mobilenetV2_SSD进行训练和预测

官方使用的版本(ssd_mobilenet_v2_coco_2018_03_29)

  • 首先使用以下flowchart帮助理解transferLearning

  • step1:进入Model目录,执行如下命令:
cd models/research/
python setup.py build
python setup.py install
  • step2:配置model并进行训练,首先在object_detection/目录下创建目录ssd_model:

    将下载好的model解压后放在自定义路径下(如object_detection/ssd_model/),下载链接[http://download.tensorflow.org/models/object_detection/ssd_mobilenet_v2_coco_2018_03_29.tar.gz]

    mobileNetv2_SSD使用tfrecord格式的数据进行训练,数据集是使用labelImg工具进行标注的xml格式,需要完成xml转csv再转为record文件。数据集转换工具详见datitran:[https://github.com/datitran/raccoon_dataset]

    把制作好的数据集tfrecords放在路径下(制作步骤详见文末)。复制训练数据用到的文件,我们在这个基础上修改配置,训练我们的数据.coco数据集共有90个class。我们在APA数据标注中使用了3个class,因此打开配置文件ssd_mobilenet_v2_coco.config.需要修改的内容如下:
./object_detection/ssd_model/data
cp object_detection/data/mscoco_label_map.pbtxt object_detection/ssd_model/ cp object_detection/samples/configs/ssd_mobilenet_v2_coco.config object_detection/ssd_model/
# 修改ssd_mobilenet_v2_coco.config
num_classes: 3 # 自定义的class数目
num_steps: 200000 # 设置多少个step后停止,可以mark此行不使用,loss值没有持续下降,可以CTRL-C停止
batch_size: 8 # 根据算力设置
fine_tune_checkpoint:/mnt/.../ssd_model/mobilenet_v2_1.4_224/model.ckpt # 上述step中下载的 pre-trained model path,最后固定接上mode.ckpt
train_input_reader: {
tf_record_input_reader {
input_path: "/mnt/.../ssd_model/data/train.record"
# 之前dataset产生的TFRecord train.record路径 eval_input_reader: {
tf_record_input_reader {
input_path: "/mnt/.../ssd_model/data/test.record"
# 之前dataset产生的TFRecord test.record路径 label_map_path:"/mnt/.../ssd_model/data/mscoco_label_map.pbtxt" # 注意train和eval两处都需要更改.
  • step3:训练开始,新版的API中train.py在legacy目录下,先把它copy到research下。

    回到research目录下 执行
python train.py --logtostderr --train_dir=training/ --pipeline_config_path=ssd_model/data/ssd_mobilenet_v2_coco.config

训练过程如下 (没有GPU时间会比较长,可以在观察到loss不再下降的时候CTRL+C停止训练)






训练完成后,结果会在-–train_dir指定的path下:




  • step4:模型效果评估:

    我们使用tensorboard工具查看训练效果。首先browser打开tensorbord的address,即可看到training及validate的信息:

    执行以下命令(=后添加刚刚训练的路径)
tensorboard --logdir=/mnt/d/0-WORK/models/models-master/research/training

成功打开会出现以下地址:






将地址复制粘贴到浏览器中即可看到训练可视化结果:http://desktop-0vqus2j:6006/#scalars




  • step5: 使用eavl.py查看在验证集上的效果


  • step6:保存模型:
python object_detection/export_inference_graph.py --pipeline_config_path=/mnt/d/0-WORK/models/models-master/research/object_detection/ssd_model/data/ssd_mobilenet_v2_coco.config --trained_checkpoint_prefix=/mnt/d/0-WORK/models/models-master/research/training/model.ckpt-77133 --output_directory /mnt/d/0-WORK/models/models-master/research/training/

执行完毕后出现:



OK,得到pb模型啦。

Model_output
- saved_model
- saved_model.pb
- checkpoint
- frozen_inference_graph.pb # Main model
- model.ckpt.data-00000-of-00001
- model.ckpt.index
- model.ckpt.meta
- pipeline.config

保存前述data中的mscoco_label_map.pbtxt和本步骤中的frozen_inference_graph.pb,后续使用。


  • step7:使用训练好的模型进行预测:

    使用如下脚本进行单帧图片检测:
# test.py
import numpy as np
import tensorflow as tf
import cv2 as cv model_path = "/mnt/d/0-WORK/models/models-master/research/training/frozen_inference_graph.pb"
pbtxt_path = "/mnt/d/0-WORKmodels/models-master/research/object_detection/ssd_model/data/mscoco_label_map.pbtxt"
testimg = "/mnt/d/0-WORK/models/models-master/research/testing/114.jpg" # Read the graph.
#with tf.compat.v1.gfile.FastGFile(model_path, 'rb') as f:
# graph_def = tf.compat.v1.GraphDef()
# graph_def.ParseFromString(f.read()) with tf.gfile.FastGFile(model_path, 'rb') as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read()) with tf.Session() as sess:
# Restore session
sess.graph.as_default()
tf.import_graph_def(graph_def, name='') # Read and preprocess an image.
img = cv.imread(testimg)
rows = img.shape[0]
cols = img.shape[1]
inp = cv.resize(img, (450, 450))
inp = inp[:, :, [2, 1, 0]] # BGR2RGB # Run the model
out = sess.run([sess.graph.get_tensor_by_name('num_detections:0'),
sess.graph.get_tensor_by_name('detection_scores:0'),
sess.graph.get_tensor_by_name('detection_boxes:0'),
sess.graph.get_tensor_by_name('detection_classes:0')],
feed_dict={'image_tensor:0': inp.reshape(1, inp.shape[0], inp.shape[1], 3)}) # Visualize detected bounding boxes.
num_detections = int(out[0][0])
for i in range(num_detections):
classId = int(out[3][0][i]) score = float(out[1][0][i])
bbox = [float(v) for v in out[2][0][i]] if score > 0.5: x = bbox[1] * cols
y = bbox[0] * rows
right = bbox[3] * cols
bottom = bbox[2] * rows
cv.rectangle(img, (int(x), int(y)), (int(right), int(bottom)), (125, 255, 51), thickness=2)
print(classId, "-->", score, x, y) cv.imwrite('/mnt/d/0-WORK/models/models-master/research/testing/result_114.jpg', img)
cv.waitKey()

效果如下:




  • step7: 模型评测

附:voc转tfrecord

参考博文

使用TransferLearning实现环视图像的角点检测——Tensorflow+MobileNetv2_SSD的更多相关文章

  1. 使用TransferLearning实现环视图像的角点检测——yolo_v3_tiny

    本文选取yolov3-tiny,使用pretrained weights,实现环视图像中的角点等关键目标检测. 大神镇楼: https://pjreddie.com/darknet/yolo/ 配置参 ...

  2. 机器学习进阶-图像特征harris-角点检测 1.cv2.cornerHarris(进行角点检测)

    1.cv2.cornerHarris(gray, 2, 3, 0.04)  # 找出图像中的角点 参数说明:gray表示输入的灰度图,2表示进行角点移动的卷积框,3表示后续进行梯度计算的sobel算子 ...

  3. 图像金字塔、高斯金字塔、差分金字塔(DOG金字塔)、尺度空间、DoG (Difference of Gaussian)角点检测

    [图像金字塔] 图像金字塔是一种以多分辨率来解释图像的结构,通过对原始图像进行多尺度像素采样的方式,生成N个不同分辨率的图像.把具有最高级别分辨率的图像放在底部,以金字塔形状排列,往上是一系列像素(尺 ...

  4. OpenCV计算机视觉学习(13)——图像特征点检测(Harris角点检测,sift算法)

    如果需要处理的原图及代码,请移步小编的GitHub地址 传送门:请点击我 如果点击有误:https://github.com/LeBron-Jian/ComputerVisionPractice 前言 ...

  5. harris角点检测的学习

    Harris通过运用微分运算和自相关矩阵改进了Moravec角点检测算法.用微分算子重新定义灰度强度变化的公式,其灰度强度变化表示为: 式中的wu,v为高斯窗口在(u,v)处的系数.X,Y它们是像素点 ...

  6. Harris角点检测算法优化

    Harris角点检测算法优化 一.综述 用 Harris 算法进行检测,有三点不足:(1 )该算法不具有尺度不变性:(2 )该算法提取的角点是像素级的:(3 )该算法检测时间不是很令人满意. 基于以上 ...

  7. Harris角点检测

    代码示例一: #include<opencv2/opencv.hpp> using namespace cv; int main(){ Mat src = imread(); imshow ...

  8. opencv笔记6:角点检测

    time:2015年10月09日 星期五 23时11分58秒 # opencv笔记6:角点检测 update:从角点检测,学习图像的特征,这是后续图像跟踪.图像匹配的基础. 角点检测是什么鬼?前面一篇 ...

  9. Harris 角点检测

    一 .Motivation 对于做图像处理的人来说,Harris角点检测肯定听过,1988年发表的文章"A combined corner and edge detector"描述 ...

随机推荐

  1. Java实现字符串编辑距离

    1 问题描述 给定一个源串和目标串,能够进行如下操作: 在任意位置上插入一个字符: 替换掉任意字符: 删除任意字符. 写一个程序,实现返回最小操作次数,使得对源串进行上述这些操作后等于目标串. 2 解 ...

  2. Python学习之turtle绘图篇

    画一个红色的五角星 from turtle import * color('red','red') begin_fill() for i in range(5): fd(200) rt(144) en ...

  3. 通知!Symantec品牌证书已正式更名为Digicert

    尊敬的合作伙伴和客户: 您好! 2017年8月2日,CA认证机构Digicert宣布正式收购 Symantec 安全认证业务.为此,Digicert宣布从2020年4月30日起,停止使用与赛门铁克(S ...

  4. win32 socket http 操作

    纯wininet 操作http关键代码如下: HINTERNET hNet = ::InternetOpen(_T("Test"), INTERNET_OPEN_TYPE_DIRE ...

  5. 【JUC系列】01、之大话并发

    学习方法 学习技术的方法都很类似,大部分都有着类似的步骤: 场景 需求 解决方案 应用 原理 并发的目的 充分利用CPU 和 I/O资源 提高效率 并发的维度 分工 同步/协作 互斥 分工 线程池 f ...

  6. 1.Go 开始搞起

    link 1. IDE Go Land 服务器激活 2. 资源 中文网站 翻译组 翻译组wiki 待认领文章 入门指南 中文文档 fork 更新 github 中如何定期使用项目仓库内容更新自己 fo ...

  7. 说说硬件中核心板的作用和优缺点,基于i.MX8M Mini核心处理器平台

    核心板,顾名思义,即硬件构成中关键的器件和电路打包封装的一块电子主板,具有布线复杂.多层.高频信号干扰.器件密度高等特性,大多数核心板集成了处理器.内存.存储器.电源管理和引脚,通过引脚与配套基板连接 ...

  8. cb16a_c++_顺序容器的选用_排序_二分查找

    /*cb16a_c++_顺序容器的选用_排序_二分查找顺序容器: 1.vector的优点与缺点 vector优点:排序利用下标,快速排序,做二分查找非常快 2.list的优点与缺点 list优点:插入 ...

  9. DES 加密解密 文件工具类

    public class DESEncrypt { /** 加密工具 */ private Cipher encryptCipher = null; /** 解密工具 */ private Ciphe ...

  10. Linux下如何查看硬件信息?

    我们在 Linux 下进行开发时,有时也需要知道当前的硬件信息,比如:CPU几核?使用情况?内存大小及使用情况?USB设备是否被识别?等等类似此类问题.下面良许介绍一些常用的硬件查看命令. lshw ...