技术标签: detection tensorflow deep-learning Deep Learning object
将object_detection_api安装好以后,我们可以使用其进行transfer learning从而实现对新检测应用的快速学习,当然也可以使用训练好的模型进行预测。以下分别讲如何使用现有模型进行预测以及在训练好的模型基础上使用新数据集学习新的模型。
官网提供了在linux下使用的说明,这里说明如何在windows下进行使用,笔者在win7下进行了测试并运行成功。
运行object_detection模型下的object_detection_tutorial.ipynb
可以使用ssd_mobilenet_v1模型进行预测。将该notebook中的MODEL_NAME = 'faster_rcnn_resnet101_coco_2018_01_28'
进行更改即可使用不同的模型进行预测。注意若当前文件夹下若存放相应模型文件,该代码会自动在当前路径下下载模型文件。
Running the Training Job
A local training job can be run with the following command:
# From the tensorflow/models/research/ directory
python object_detection/train.py \
--logtostderr \
--pipeline_config_path=${PATH_TO_YOUR_PIPELINE_CONFIG} \
--train_dir=${PATH_TO_TRAIN_DIR}
where ${PATH_TO_YOUR_PIPELINE_CONFIG} points to the pipeline config and ${PATH_TO_TRAIN_DIR} points to the directory in which training checkpoints and events will be written to. By default, the training job will run indefinitely until the user kills it.
按照官网说明,只需找到对应模型的pipeline写好即可。
在detection_model_zoo中包含了很多预训练模型。
为了使用这些模型,我们需要依次准备以下内容:准备数据,设置object detection pipeline。
数据准备可以参考原github中的说明。该网站中也提供了各种公开数据集的下载地址。这里使用的数据是2012 PASCAL VOC data set。准备数据的过程中的输入为原始数据的解压文件夹,输出为两个.tfrecord文件。使用的代码为create_pascal_tf_record.py
(其目录为object_detection/dataset_tools/create_pascal_tf_record.py)
运行命令如下:
python ./dataset_tools/create_pascal_tf_record.py \
--data_dir=./data/VOCdevkit \
--year=VOC2012 \
--set=train \
--label_map_path=./data/pascal_label_map.pbtxt \
--output_path=./data/pascal_train.record
对应的文件结构如下:
--object_detection
|-- dataset_tools
| |-- create_pascal_tf_record.py
|-- data
| |-- VOCdevkit
| |-- pascal_label_map.pbtxt
|
代码运行后会在object_detection/data
目录下生成pascal_train.record
和pascal_cal.record
两个文件。之后我们使用这两个文件作为数据输入即可。
在pre-trained模型下载下来后,其内会包含一个.config
的文件,我们将其复制一下,改名为faster_rcnn_resnet101_voc.config
,之后打开该文件,将PATH_TO_BE_CONFIGURED
的路径修改为自己存储的路径。如我将文件以如下结构存放:
--object_detection
|
|--demo_VOC2012
| |
| |--faster_rcnn_resnet101_voc.config
| |
| |--model.ckpt
| |
| |--pascal_train.record
| |--pascal_val.record
| |--pascal_label_map.pbtxt
可将其内代码改为fine_tune_checkpoint: "./demo_VOC2012/model.ckpt"
,input_path: "./demo_VOC2012/pascal_train.record"
, label_map_path: "./demo_VOC2012/pascal_label_map.pbtxt".
其他代码保持不变即可。
模型训练运行以下代码即可:
python train.py --logtostderr --pipeline_config_path=./demo_VOC2012/faster_rcnn_resnet101_voc.config --train_dir=./demo_VOC2012/train
由于object_detection_API官方教程在linux下运行,加之其仍在测试阶段,所以可能会有一些bug和错误出现,这里列举一些我遇到的问题和解决方法。
failed: tried to convert ‘t’ to a tensor. Argument must be a dense tensor range (0, 3) got shape [3], but wanted []
这个问题出在learning_schedule.py文件。需找到安装api时的目录下的learning_schedule.py,将
rate_index = tf.reduce_max(tf.where(tf.greater_equal(global_step, boundaries),
range(num_boundaries),
[0] * num_boundaries))
rate_index = tf.reduce_max(tf.where(tf.greater_equal(global_step, boundaries),
list(range(num_boundaries)),
[0] * num_boundaries))
方法来源:github_issue
DataLossError: Unable to open table file Input/output Error
这个问题是model restore的问题,由于新的模型存储从.ckpt变为存储为三个.ckpt文件,所以将fine_tune_checkpoints=设置为三个文件所在文件夹/model.ckpt即可。
No module named ‘pycocotools’
这个应该是由于cocoAPI没有安装的问题,但是官网仅仅提供了linux下安装的方法,win7下的安装目前我仍未成功。
参考github-issue和github-issue,我从philferriere-github中下载了cocoapi-master
,解压后分别运行
python setup.py build_ext --inplace
python setup.py build_ext install
(即makefile中的命令). 执行后没报错,且eval.py运行不再报上述问题了。
为了可以在api中使用自己生成的数据,与PASCAL VOC处理方法相似,我们需将数据文件夹转为tfrecord。相似地,我们采用相同的数据存放结构,我们将自己的数据以如下文件结构存放:
--data
|--label_map.pbtxt
|--VOC2007
| |--Annotations
| |--ImageSets
| | |--Main
| | | |--train.txt
| |--JPEGImages
--create_xx_tf_record.py
其中annotations
文件夹中存放标注文件(以xml格式文件存放),
JPEGImages
文件夹中存放原图(以jpg格式存放),而label_map.pbtxt
文件需自己来写,其内以字典形式存放不同的label类别(分类名称对应的整型分类),如:
item {
id: 1
name: cat
}
item {
id: 2
name: dog
}
这些文件都可以参考PASCAL VOC的示例来写。
将create_pascal_tf_record.py
(在object_detection/dataset_tool目录下)复制到与data
文件夹同目录下,这里我重命名为create_xx_tf_record.py
。我们需要对其相关参数进行修改使其与自己的数据相匹配。如下:
data_dir 设为 ./data
label_map_path 设为 ./data/label_map.pbtxt
output_path 设为 ./data/xx_train.tfrecord
# 注释下列几个不用的语句
# difficult_obj.append(int(difficult))
# truncated.append(int(obj['truncated']))
# poses.append(obj['pose'].encode('utf8'))
# 'image/object/difficult': dataset_util.int64_list_feature(difficult_obj),
# 'image/object/truncated': dataset_util.int64_list_feature(truncated),
# 'image/object/view': dataset_util.bytes_list_feature(poses),
# 若xml中未设置图像的width和height,则将
width = int(data['size']['width'])
height = int(data['size']['height'])
改为 width, height = image.size
# 修改main函数中image path
examples_path = os.path.join(data_dir, year, 'ImageSets', 'Main',
'train' + '.txt')
# 调试时一定要确保dict_to_tf_example()函数中full_path和主函数中的data都读取正确。
# 生成各文件时需保证.jpg和.xml一一对应,且和.pbtxt中的一致。
将以上改动设置好以后,运行create.py则可在output_path中生成tfrecord文件。
在create_xx_tf_record.py
中img_path
变量的设置中用到了xml
中的folder
标签值(data[‘folder’]),因此需将xml
中folder标签值改为image存放路径:VOC2007
。该问题使用以下代码运行即可。
from xml.etree.ElementTree import parse, Element
import os
filepath = './xml'
xml_files = os.listdir(filepath)
for xml_file in xml_files:
print("now {} is being processed".format(xml_file))
xml_path = os.path.join(filepath, xml_file)
doc = parse(xml_path)
root = doc.getroot()
# # Remove a few elements
# root.remove(root.find('sri'))
# root.remove(root.find('cr'))
# # Insert a new element after <nm>...</nm>
# root.getchildren().index(root.find('nm'))
#
# e = Element('spam')
# e.text = 'This is a test'
# root.insert(2, e)
folder = root.find('folder') # folder是<>中的值
folder.text = 'VOC2007' # text是<><>之间的值
# Write back to a file
# doc.write(xml_file, xml_declaration=True)
doc.write(xml_file)
同样的,需在预训练模型的config文件基础上进行修改。主要修改以下参数:
参考上篇总结,进行相似存放即可。训练时运行
python train.py --logtostderr --pipeline_config_path=./demo_XX/faster_rcnn_resnet101_xx.config --train_dir=./demo_XX/train
eval时运行
python eval.py --logtostderr --pipeline_config_path=./demo_VOC2012/faster_rcnn_resnet101_xx.config --checkpoint_dir=./demo_xx/train --eval_dir=./demo_xx/eval
即可。
预测使用的代码是从object_detection_tutorial复制改写得到的,改名为demo_faster_rcnn_resnet101_xx.py。需要修改其中以下内容:
PATH_TO_CKPT = "./demo_XX/output/frozen_inference_graph.pb" # .pb文件存储训练好的模型
PATH_TO_LABELS = './demo_XX/xx_label_map.pbtxt'
num_classes = 4
PATH_TO_TEST_IMAGE_DIR = 'test_images'
TEST_IMAGE_PATHS = [ os.path.join(PATH_TO_TEST_IMAGES_DIR, '00000{}.jpg'.format(i)) for i in range(1, 10) ]
其文件结构如下:
object detection
|
|--test_images
|--demo_XX
| |
| |--train
| |--output
| | |
| | |--frozen_inference_graph.pb
| |--xx_label_map.pbtxt
|--export_inference_graph.py
|--demo_faster_rcnn_resnet101_xx.py
执行时需先将训练好的模型转为.pb格式文件(过程后述),然后在test_images文件夹中存放测试图像,同时测试数据的名称要以列表形式存放在TEST_IMAGE_PATHS中。准备妥当以后,运行
python demo_faster_rcnn_resnet101_xx.py
即可进行预测。
同样在/object detection
文件夹中,export_inference_graph.py
可以完成这个功能。运行该脚本只需将原始ckpt.meta所在文件夹目录
, 输出目录
, 以及pipeline目录
设置好即可。如执行以下命令:
python export_inference_graph.py --input_type image_tensor --pipeline_config_path ./demo_XX/faster_rcnn_resnet101_xx.config --trained_checkpoint_prefix ./demo_XX/train/model.ckpt-83082 --output_directory ./demo_XX/output
生成数据
|--create_xx_tf_record.py
|
|--data
| |--VOC2007
| | |--Annotations
| | | |--xml文件
| | |--ImageSets
| | | |--Main
| | | | |--txt文件
| | |--JPEGImages
| | | |--jpg文件
完成训练
object_detection
|
|--demo_xx (存放某个应用/数据训练的结果)
| |
| |--train(训练model目录)
| |--faster_rcnn_resnet101_xx.config (预训练模型config,设置模型超参数)
| |--model.ckpt(三个)
| |--train.record
| |--val.record
| |--.pbtxt(数据label标注文件)
完成测试(或完成eval)
object detection
|
|--test_images (存放测试数据集图像)
|--demo_XX (存放某个应用/数据训练的结果)
| |
| |--train(训练好的model目录)
| |--output
| | |
| | |--frozen_inference_graph.pb(训练好转存为.pb文件)
| |--xx_label_map.pbtxt
|--export_inference_graph.py
|--demo_faster_rcnn_resnet101_xx.py
整个文件结构(注意生成数据可以不依赖object detection库,可以在单独的文件夹下执行)
object detection
|
|--dataset tools
| |--create_xx_tf_record.py
| |
| |--data
| | |--VOC2007
| | | |--Annotations
| | | | |--xml文件
| | | |--ImageSets
| | | | |--Main
| | | | | |--txt文件
| | | |--JPEGImages
| | | | |--jpg文件
|
|--demo_xx (存放某个应用/数据训练的结果)
| |
| |--train(训练model目录)
| |--output
| | |--frozen_inference_graph.pb(训练好转存为.pb文件)
| |--faster_rcnn_resnet101_xx.config (预训练模型config,设置模型超参数)
| |--model.ckpt(三个)
| |--train.record
| |--val.record
| |--.pbtxt(数据label标注文件)
|--test_images (存放测试数据集图像)
|--export_inference_graph.py(用于将.ckpt文件转化为.pb)
|--demo_faster_rcnn_resnet101_xx.py(用于预测test)
文章浏览阅读2w次,点赞7次,收藏51次。四个步骤1.创建C++ Win32项目动态库dll 2.在Win32项目动态库中添加 外部依赖项 lib头文件和lib库3.导出C接口4.c#调用c++动态库开始你的表演...①创建一个空白的解决方案,在解决方案中添加 Visual C++ , Win32 项目空白解决方案的创建:添加Visual C++ , Win32 项目这......_c#调用lib
文章浏览阅读4.6k次。苹方字体是苹果系统上的黑体,挺好看的。注重颜值的网站都会使用,例如知乎:font-family: -apple-system, BlinkMacSystemFont, Helvetica Neue, PingFang SC, Microsoft YaHei, Source Han Sans SC, Noto Sans CJK SC, W..._ubuntu pingfang
文章浏览阅读159次。表单表单概述表单标签表单域按钮控件demo表单标签表单标签基本语法结构<form action="处理数据程序的url地址“ method=”get|post“ name="表单名称”></form><!--action,当提交表单时,向何处发送表单中的数据,地址可以是相对地址也可以是绝对地址--><!--method将表单中的数据传送给服务器处理,get方式直接显示在url地址中,数据可以被缓存,且长度有限制;而post方式数据隐藏传输,_html表单的处理程序有那些
文章浏览阅读1.2k次。使用说明:开启Google的登陆二步验证(即Google Authenticator服务)后用户登陆时需要输入额外由手机客户端生成的一次性密码。实现Google Authenticator功能需要服务器端和客户端的支持。服务器端负责密钥的生成、验证一次性密码是否正确。客户端记录密钥后生成一次性密码。下载谷歌验证类库文件放到项目合适位置(我这边放在项目Vender下面)https://github.com/PHPGangsta/GoogleAuthenticatorPHP代码示例://引入谷_php otp 验证器
文章浏览阅读4.3k次,点赞5次,收藏11次。matplotlib.plot画图横坐标混乱及间隔处理_matplotlib更改横轴间距
文章浏览阅读2.2k次。①Storage driver 处理各镜像层及容器层的处理细节,实现了多层数据的堆叠,为用户 提供了多层数据合并后的统一视图②所有 Storage driver 都使用可堆叠图像层和写时复制(CoW)策略③docker info 命令可查看当系统上的 storage driver主要用于测试目的,不建议用于生成环境。_docker 保存容器
文章浏览阅读834次,点赞27次,收藏13次。网络拓扑结构是指计算机网络中各组件(如计算机、服务器、打印机、路由器、交换机等设备)及其连接线路在物理布局或逻辑构型上的排列形式。这种布局不仅描述了设备间的实际物理连接方式,也决定了数据在网络中流动的路径和方式。不同的网络拓扑结构影响着网络的性能、可靠性、可扩展性及管理维护的难易程度。_网络拓扑csdn
文章浏览阅读1.8k次,点赞5次,收藏8次。IOS系统Date的坑要创建一个指定时间的new Date对象时,通常的做法是:new Date("2020-09-21 11:11:00")这行代码在 PC 端和安卓端都是正常的,而在 iOS 端则会提示 Invalid Date 无效日期。在IOS年月日中间的横岗许换成斜杠,也就是new Date("2020/09/21 11:11:00")通常为了兼容IOS的这个坑,需要做一些额外的特殊处理,笔者在开发的时候经常会忘了兼容IOS系统。所以就想试着重写Date函数,一劳永逸,避免每次ne_date.prototype 将所有 ios
文章浏览阅读5.3k次。方法一:用PLSQL Developer工具。 1 在PLSQL Developer的sql window里输入select * from test for update; 2 按F8执行 3 打开锁, 再按一下加号. 鼠标点到第一列的列头,使全列成选中状态,然后粘贴,最后commit提交即可。(前提..._excel导入pl/sql
文章浏览阅读83次。Git常用命令速查手册1、初始化仓库git init2、将文件添加到仓库git add 文件名 # 将工作区的某个文件添加到暂存区 git add -u # 添加所有被tracked文件中被修改或删除的文件信息到暂存区,不处理untracked的文件git add -A # 添加所有被tracked文件中被修改或删除的文件信息到暂存区,包括untracked的文件...
文章浏览阅读202次。分享119个ASP.NET源码总有一个是你想要的_千博二手车源码v2023 build 1120
文章浏览阅读1.8k次。版权声明:转载请注明出处 http://blog.csdn.net/irean_lau。目录(?)[+]1、缺省构造函数。2、缺省拷贝构造函数。3、 缺省析构函数。4、缺省赋值运算符。5、缺省取址运算符。6、 缺省取址运算符 const。[cpp] view plain copy_空类默认产生哪些类成员函数