技术标签: 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)
文章浏览阅读1.6k次。安装配置gi、安装数据库软件、dbca建库见下:http://blog.csdn.net/kadwf123/article/details/784299611、检查集群节点及状态:[root@rac2 ~]# olsnodes -srac1 Activerac2 Activerac3 Activerac4 Active[root@rac2 ~]_12c查看crs状态
文章浏览阅读1.3w次,点赞45次,收藏99次。我个人用的是anaconda3的一个python集成环境,自带jupyter notebook,但在我打开jupyter notebook界面后,却找不到对应的虚拟环境,原来是jupyter notebook只是通用于下载anaconda时自带的环境,其他环境要想使用必须手动下载一些库:1.首先进入到自己创建的虚拟环境(pytorch是虚拟环境的名字)activate pytorch2.在该环境下下载这个库conda install ipykernelconda install nb__jupyter没有pytorch环境
文章浏览阅读5.2k次,点赞19次,收藏28次。选择scoop纯属意外,也是无奈,因为电脑用户被锁了管理员权限,所有exe安装程序都无法安装,只可以用绿色软件,最后被我发现scoop,省去了到处下载XXX绿色版的烦恼,当然scoop里需要管理员权限的软件也跟我无缘了(譬如everything)。推荐添加dorado这个bucket镜像,里面很多中文软件,但是部分国外的软件下载地址在github,可能无法下载。以上两个是官方bucket的国内镜像,所有软件建议优先从这里下载。上面可以看到很多bucket以及软件数。如果官网登陆不了可以试一下以下方式。_scoop-cn
文章浏览阅读4.5k次,点赞2次,收藏3次。首先要有一个color-picker组件 <el-color-picker v-model="headcolor"></el-color-picker>在data里面data() { return {headcolor: ’ #278add ’ //这里可以选择一个默认的颜色} }然后在你想要改变颜色的地方用v-bind绑定就好了,例如:这里的:sty..._vue el-color-picker
文章浏览阅读640次。基于芯片日益增长的问题,所以内核开发者们引入了新的方法,就是在内核中只保留函数,而数据则不包含,由用户(应用程序员)自己把数据按照规定的格式编写,并放在约定的地方,为了不占用过多的内存,还要求数据以根精简的方式编写。boot启动时,传参给内核,告诉内核设备树文件和kernel的位置,内核启动时根据地址去找到设备树文件,再利用专用的编译器去反编译dtb文件,将dtb还原成数据结构,以供驱动的函数去调用。firmware是三星的一个固件的设备信息,因为找不到固件,所以内核启动不成功。_exynos 4412 刷机
文章浏览阅读2w次,点赞24次,收藏42次。Linux系统配置jdkLinux学习教程,Linux入门教程(超详细)_linux配置jdk
文章浏览阅读3.3k次,点赞5次,收藏19次。xlabel('\delta');ylabel('AUC');具体符号的对照表参照下图:_matlab微米怎么输入
文章浏览阅读119次。顺序读写指的是按照文件中数据的顺序进行读取或写入。对于文本文件,可以使用fgets、fputs、fscanf、fprintf等函数进行顺序读写。在C语言中,对文件的操作通常涉及文件的打开、读写以及关闭。文件的打开使用fopen函数,而关闭则使用fclose函数。在C语言中,可以使用fread和fwrite函数进行二进制读写。 Biaoge 于2024-03-09 23:51发布 阅读量:7 ️文章类型:【 C语言程序设计 】在C语言中,用于打开文件的函数是____,用于关闭文件的函数是____。
文章浏览阅读3.4k次,点赞2次,收藏13次。跟随鼠标移动的粒子以grid(SOP)为partical(SOP)的资源模板,调整后连接【Geo组合+point spirit(MAT)】,在连接【feedback组合】适当调整。影响粒子动态的节点【metaball(SOP)+force(SOP)】添加mouse in(CHOP)鼠标位置到metaball的坐标,实现鼠标影响。..._touchdesigner怎么让一个模型跟着鼠标移动
文章浏览阅读178次。项目运行环境配置:Jdk1.8 + Tomcat7.0 + Mysql + HBuilderX(Webstorm也行)+ Eclispe(IntelliJ IDEA,Eclispe,MyEclispe,Sts都支持)。项目技术:Springboot + mybatis + Maven +mysql5.7或8.0+html+css+js等等组成,B/S模式 + Maven管理等等。环境需要1.运行环境:最好是java jdk 1.8,我们在这个平台上运行的。其他版本理论上也可以。_基于java技术的停车场管理系统实现与设计
文章浏览阅读3.5k次。前言对于MediaPlayer播放器的源码分析内容相对来说比较多,会从Java-&amp;gt;Jni-&amp;gt;C/C++慢慢分析,后面会慢慢更新。另外,博客只作为自己学习记录的一种方式,对于其他的不过多的评论。MediaPlayerDemopublic class MainActivity extends AppCompatActivity implements SurfaceHolder.Cal..._android多媒体播放源码分析 时序图
文章浏览阅读2.4k次,点赞41次,收藏13次。java 数据结构与算法 ——快速排序法_快速排序法