用TensorFlow训练一个物体检测器(手把手教学版)

TunSoFrices表现人家十足的的情人检测API。,笔者可以运用很API来锻炼笔者本人的创纪录的集以成特殊的目的布道所。。

作者的软硬件境遇词的搭配

CPU: i7-6800k (无价钱),主流CPU可以)
OS: Ubuntu Linux 16.04 LTS 优良独角兽版
内存:16G
硬质地层:ssd
显卡:NVIDIA GEFORCE GTX 1080TI 11G 唤回越大越好。,CUDA的小瘤数越多越好。,我在锻炼。,运用GPU,它将比运用CPU快10-20倍。

一位异国顺序员分享了他看法心爱浣熊DET的经历。

地址(必要爬过墙):

原作者吉图布:   (作者的创纪录的集可以在这时下载)

本文简洁的绍介了探险家的锻炼议事顺序。,早已太粗糙了。,初学者很难并驾齐驱。。初学者,我检修了特点的走。,如今让笔者一齐研究。!

笔者为什么要为了做?

手边的面不独心爱。,与到国外民主党员圆元是很遍及的事。。爱上了它,就在浣熊旁边的。,作者选择了它作为探险家的检测情人。。相机可以镶嵌在屋子四周达到结尾的后。,反省浣熊条件擅入你的屋子。,你会即时晓得人家不测的做客串条件有ARRI。。它眼神像浣熊。!

一、使成为创纪录的集

机具研究必要创纪录的作为原料。,因而笔者率先要做的是达到人家锻炼创纪录的集。,同时笔者必要应用契合Tensorflow的创纪录的体式来拘押健康这些创纪录的及其称制表。

1。在出口创纪录的先发制人,必要思索几件事。:最初的,你必要必然的浣熊的多色相片。;其二,你必要图片中浣熊的同等的(Xmin),ymin,xmax,YMAX)决定方向和混合物浣熊。。为了检测人家情人,笔者的布道所十足的复杂。,你只必要界限人家类。;

2。笔者在哪里可以找到创纪录的?互联网建立工作关系是最大的资源。。包罗次要搜调擎在内的图像搜索和图像网站。,找寻不寻常的的范围、位姿、光照相片。作者搜索了二百只浣熊来锻炼他的探险家。,早已业务是可以的。,竟,Imagenet上有浣熊创纪录的集。,人家有赞美的伴星可以下载。,流露IMANET必要爬过墙。,另外,检验码是不可见的。);

目的干脆面君:动动手,用TensorFlow API锻炼出本人的目的检测以前的

三。在笔者有创纪录的继后,笔者必要对它们终止制表。。混合物很复杂。,都是浣熊,但笔者必要人工操作决定方向浣熊在每人家图片。。人家好转的的制表器是LabelImg。, 使用着的图像制表,请商量我的另一篇文字《手把手教你图片打标》

4、到眼前为止,笔者受胎必然的浣熊的图片也这些图片的标注证明(annotation)(出路你比得上懒,您可以从作者的GITHUB下载这些图片和正文证明。, 这时我将下载原始作者的正文证明和图片。

(单击下载) ZIP可以下载整体物品。

5、笔者突然了笔者下载的ZIP包。,列举如下,正文编目录是正文证明。,图像编目录是图像证明。,正文证明正中鹄的证明名植物的节是对应证明的证明名。,必要特殊阐明的是,raccoon很创纪录的集外面的标注证明xml里的filename是png后缀,竟,笔者下载的图像是JPG体式。,据估计,原作者早已更衣了体式。,终于,在后头的文本中,笔者必要稍许地处置一下很证明名。。笔者不必要等等文档来突然。,整个迅速离开,嗣后,笔者将逐渐地使安定这些证明。。

6。终极的,将图片和图像称制表交换成TFRead体式。,它可以分为锻炼集和检验集来启动下人家。!用LabelImg is PASCAL制表的证明。 VOC体式,TySoFrof为Pascal求婚本子。 VOC体式标注证明和响应的图片交换为TFRecord,不外,笔者必要运用很本子。,笔者必要先达到TySoFr流境遇并镶嵌TySoFrase. object detection api ,使用着的这两个切断,请商量我的级数文字,Ubuntu。 16.04下搭建TensorFlow运转境遇(用Anaconda)》《Ubuntu 16.04下镶嵌TensorFlow Object Detection API(情人检测API),继后,授给物您早已依据这两篇文字建筑物了境遇。。

二、将图片和正文交换成TFREST体式

在~/tensorflow/models/research/object_detection/dataset_tools编目录下(~表现以后用户主编目录,查找同样的证明,这是TaSoFLASH将求婚的PASCAL。 VOC体式交换成TFREST体式本子,治理以下本子,稿件它。,早已很比赛是给Pascal的。 编制了VOC创纪录的集的编目录构造。,因而笔者必要汇编和修正它。:

(根) forest@forest-MS-7A20:~/tensorflow/models/research$ cp object_detection/dataset_tools/ object_detection/dataset_tools/
(根) forest@forest-MS-7A20:~/tensorflow/models/research$ gedit object_detection/dataset_tools/

有过于详细的修正。,这时缺少解说。,上面求婚完全地的证明指定遗传密码。。

# -*- 编码:UTF-8 -*-

# Copyright 2017 The TensorFlow 作者。 All Rights 保存的。
#
# Licensed under the Apache License, Version 2.0 (the 担保
# you may not use this file except in compliance with the 担保。
# You may obtain a copy of the License at
#
#     
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 暗指的。
# See the License for the specific language governing permissions and
# limitations under the 担保。
# ==============================================================================

交换 raw PASCAL dataset to TFRecord for object_detection.

Example usage:
    python object_detection/dataset_tools/ \
	--data_dir=/home/forest/dataset/raccoon_dataset-master/images \
	--set=/home/forest/dataset/raccoon_dataset-master/ \
	--output_path=/home/forest/dataset/raccoon_dataset-master/ \
	--label_map_path=/home/forest/dataset/raccoon_dataset-master/ \
	--annotations_dir=/home/forest/dataset/raccoon_dataset-master/annotations
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import hashlib
import io
import logging
import os

from lxml import etree
import PIL.Image
import tensorflow as tf

from object_detection.utils import dataset_util
from object_detection.utils import label_map_util


flags = tf.app.flags
(''data_dir'', '''', 根 directory to raw PASCAL VOC data集中。
(设置), 训练, 交换 training set, validation set or ''
                    合 集中。
(''annotations_dir'', 正文,
                    (对立) path to annotations directory.'')
(年), ''VOC2007'', 预料的 challenge year.'')
(''output_path'', '''', 道路 to output TFRecord'')
(''label_map_path'', ''data/'',
                    道路 to label map 原
(''ignore_difficult_instances'', False, 条件 to ignore ''
                     英〉硬海滩 围住)
FLAGS = flags.FLAGS

SETS = [训练, 瓦尔, ''trainval'', 与试验有关的
YEARS = [''VOC2007'', ''VOC2012'', 合'']


def dict_to_tf_example(data,
                       dataset_directory,
                       label_map_dict,
                       ignore_difficult_instances=False,
                       image_subdirectory=''JPEGImages''):
  """Convert XML derived dict to tf.Example 原句

  Notice that this function normalizes the bounding box coordinates provided
  by the raw 创纪录的。

  Args:
    data: dict holding PASCAL XML fields for a single image (达到预期的目的) by
      running )
    dataset_directory: Path to root directory holding PASCAL dataset
    label_map_dict: A map from string label names to integers ids.
    ignore_difficult_instances: Whether to skip difficult instances in the
      dataset  (默许) 假)
    image_subdirectory: String specifying subdirectory within the
      PASCAL dataset directory holding the actual image 创纪录的。

  Returns:
    example: The converted tf.Example.

  Raises:
    ValueError: if the image pointed to by 创纪录的[证明名] is not a valid JPEG
  """
  # 上面这句里的replace执意指画reccoon的标注证明里的filename称制表后缀不义的行为而特殊添加的
  img_path = (dataset_directory, 创纪录的[证明名].replace(''.png'',JPG)交换(P.NPNG),''.jpg''))
  full_path = img_path
  with tf.gfile.GFile(full_path, RB as fid:
    encoded_jpg = ()
  encoded_jpg_io = (encoded_jpg)
  image = (encoded_jpg_io)
  if image.format != JPEG
    raise ValueError(图像) format not JPEG’
  key = (encoded_jpg).hexdigest()

  width = 创纪录的[ [体积] ] [宽度]
  height = 创纪录的[ [体积] ] [ [高的] ]

  xmin = []
  ymin = []
  xmax = []
  ymax = []
  classes = []
  classes_text = []
  truncated = []
  poses = []
  difficult_obj = []
  for obj in 创纪录的[情人]
    difficult = bool(int(obj[英〉硬海滩'']))
    if ignore_difficult_instances and difficult:
      continue

    difficult_obj.append(int(difficult))

    (float(obj[''bndbox''][''xmin'']) / 宽度)
    (漂(Obj[BNDBOX ] ] [ YMIN ] / 高的)
    (float(obj[''bndbox''][''xmax'']) / 宽度)
    (float(obj[''bndbox''][''ymax'']) / 高的)
    (Obj[决定])编码(UTF8)
    (LabelsiMaPixDICT[OB] [姓名]
    (int)(Obj[[大厦] ]
    (Obje[姿态])编码(UTF8)

  example = (features=(feature={
      ''image/height'': (高的),
      ''image/width'': (宽度),
      ''image/filename'': (
          创纪录的[证明名].encode(''utf8'')),
      ''image/source_id'': (
          创纪录的[证明名].encode(''utf8'')),
      ''image/key/sha256'': ((''utf8'')),
      ''image/encoded'': (encoded_jpg),
      ''image/format'': (JPEG)编码(UTF8),
      ''image/object/bbox/xmin'': (xmin),
      ''image/object/bbox/xmax'': (xmax),
      ''image/object/bbox/ymin'': (俞明),
      ''image/object/bbox/ymax'': (ymax),
      ''image/object/class/text'': (classes_text),
      ''image/object/class/label'': (类),
      ''image/object/difficult'': (difficult_obj),
      ''image/object/truncated'': (大厦),
      ''image/object/view'': (姿态),
  }))
  return example


def main(_):
  #if FLAGS.set not in SETS:
  #  raise 值不义的行为集 must be in : 体式(集中)
  #if  not in YEARS:
  #  raise 价钱年 must be in : 体式(年)

  data_dir = FLAGS.data_dir
  years = [''VOC2007'', ''VOC2012'']
  if  != 合'':
    years = []

  writer = ()

  label_map_dict = ()

  for year in years:
    (读数) from PASCAL %s 创纪录的集。'', 年)
    examples_path = FLAGS.set
    #                             ''aeroplane_'' + FLAGS.set + 'txt’)
    annotations_dir = FLAGS.annotations_dir
    examples_list = dataset_util.read_examples_list(examples_path)
    for idx, example in enumerate(examples_list):
      if idx % 100 == 0:
        (对) image %d of %d'', idx, len(examples_list))
      path = (annotations_dir, example + .xml
      with tf.gfile.GFile(path, ''r'') as fid:
        xml_str = ()
      xml = (xml_str)
      data = (XML)[正文]

      tf_example = dict_to_tf_example(data, FLAGS.data_dir, label_map_dict,
                                      FLAGS.ignore_difficult_instances)
      (())

  ()


if __name__ == ''__main__'':
  ()

边框资格笔者界限笔者的类别ID私下的相干。,通常拘押健康在PBTXT体式中。,笔者在~/dataset/raccoon_dataset-master/编目录下新建人家名为的文本证明,使满意列举如下:

item {
  id: 1
  name: 浣熊
}

鉴于笔者只人家类别。,因而笔者只必要在这时界限1个物品。,出路您有多个类别,您必要多个物品。,注意到,ID从1开端,决定的值与正文证明正中鹄的类名同样的。,也执意说,当你制表图像时,你执意mark raccoon。,在这时,笔者一定写浣熊。,我不见得写浣熊。

笔者必要两个文本证明。,通知交换本子。,关系代词图片证明用于锻炼集?,关系代词图片证明用于检验集?。在~/dataset/raccoon_dataset-master/编目录下新建人家名为的文本证明,使满意(此列表由Python本子制造)。,先前160件为锻炼集。,最后的40个检验集列举如下。:

raccoon-194
raccoon-101
raccoon-4
raccoon-105
raccoon-97
raccoon-113
raccoon-48
raccoon-108
raccoon-156
raccoon-55
raccoon-161
raccoon-2
raccoon-59
raccoon-18
raccoon-41
raccoon-126
raccoon-79
raccoon-23
raccoon-92
raccoon-58
raccoon-153
raccoon-165
raccoon-182
raccoon-122
raccoon-95
raccoon-86
raccoon-6
raccoon-99
raccoon-46
raccoon-130
raccoon-167
raccoon-186
raccoon-183
raccoon-177
raccoon-170
raccoon-179
raccoon-73
raccoon-82
raccoon-5
raccoon-10
raccoon-94
raccoon-30
raccoon-67
raccoon-85
raccoon-87
raccoon-112
raccoon-174
raccoon-196
raccoon-134
raccoon-133
raccoon-148
raccoon-13
raccoon-89
raccoon-16
raccoon-128
raccoon-150
raccoon-106
raccoon-120
raccoon-144
raccoon-137
raccoon-12
raccoon-140
raccoon-21
raccoon-141
raccoon-107
raccoon-47
raccoon-29
raccoon-115
raccoon-25
raccoon-124
raccoon-3
raccoon-1
raccoon-131
raccoon-50
raccoon-103
raccoon-80
raccoon-71
raccoon-42
raccoon-142
raccoon-176
raccoon-149
raccoon-33
raccoon-132
raccoon-118
raccoon-26
raccoon-65
raccoon-43
raccoon-200
raccoon-135
raccoon-154
raccoon-93
raccoon-22
raccoon-36
raccoon-69
raccoon-96
raccoon-114
raccoon-181
raccoon-84
raccoon-193
raccoon-68
raccoon-192
raccoon-70
raccoon-74
raccoon-88
raccoon-8
raccoon-34
raccoon-139
raccoon-91
raccoon-109
raccoon-27
raccoon-54
raccoon-52
raccoon-185
raccoon-136
raccoon-61
raccoon-127
raccoon-98
raccoon-155
raccoon-75
raccoon-28
raccoon-173
raccoon-57
raccoon-56
raccoon-187
raccoon-160
raccoon-35
raccoon-151
raccoon-175
raccoon-129
raccoon-20
raccoon-116
raccoon-66
raccoon-37
raccoon-180
raccoon-143
raccoon-40
raccoon-76
raccoon-111
raccoon-64
raccoon-24
raccoon-60
raccoon-168
raccoon-17
raccoon-32
raccoon-147
raccoon-117
raccoon-172
raccoon-189
raccoon-45
raccoon-81
raccoon-7
raccoon-63
raccoon-110
raccoon-198
raccoon-100
raccoon-39
raccoon-9
raccoon-19
raccoon-195
raccoon-197

在~/dataset/raccoon_dataset-master/编目录下新建人家名为的文本证明,使满意列举如下:

raccoon-138
raccoon-90
raccoon-44
raccoon-152
raccoon-162
raccoon-190
raccoon-191
raccoon-51
raccoon-62
raccoon-102
raccoon-119
raccoon-178
raccoon-49
raccoon-184
raccoon-72
raccoon-157
raccoon-14
raccoon-163
raccoon-53
raccoon-188
raccoon-104
raccoon-169
raccoon-146
raccoon-164
raccoon-31
raccoon-166
raccoon-171
raccoon-78
raccoon-77
raccoon-145
raccoon-199
raccoon-123
raccoon-11
raccoon-83
raccoon-158
raccoon-125
raccoon-15
raccoon-159
raccoon-38
raccoon-121

可以运用上面的Python本子制造此列表

import os
import random

pt="/home/forest/dataset/raccoon_dataset-master/images/"
image_name=(pt)
for temp in image_name:
    if temp.endswith(".jpg"):
        print temp.replace(''.jpg'','''')

运转笔者写的本子。,制造TFREST证明

#from ~/tensorflow/models/research
python object_detection/dataset_tools/ \
	--data_dir=/home/forest/dataset/raccoon_dataset-master/images \
	--set=/home/forest/dataset/raccoon_dataset-master/ \
	--output_path=/home/forest/dataset/raccoon_dataset-master/ \
	--label_map_path=/home/forest/dataset/raccoon_dataset-master/ \
	--annotations_dir=/home/forest/dataset/raccoon_dataset-master/annotations

出路出口 if not xml,成完成描画,~/dataset/raccoon_dataset-master/ 执意笔者必要锻炼集证明

隐情治理以下本子,制造检验集TFREST证明

python object_detection/dataset_tools/ \
	--data_dir=/home/forest/dataset/raccoon_dataset-master/images \
	--set=/home/forest/dataset/raccoon_dataset-master/ \
	--output_path=/home/forest/dataset/raccoon_dataset-master/ \
	--label_map_path=/home/forest/dataset/raccoon_dataset-master/ \
	--annotations_dir=/home/forest/dataset/raccoon_dataset-master/annotations

出路出口 if not xml,成完成描画,~/dataset/raccoon_dataset-master/ 执意笔者必要检验集证明

三、下载前训练类型

四、修正训练词的搭配证明

 稿件object_detection/samples/configs下的 到 ~/dataset/raccoon_dataset-master/下,重命名为,并终止以下修正:

# SSD with Mobilenet v1 configuration for MSCOCO 创纪录的集。
# Users should configure the fine_tune_checkpoint field in the train config as
# well as the label_map_path and input_path fields in the train_input_reader and
# eval_input_reader. Search for "PATH_TO_BE_CONFIGURED" to find the fields that
# should be 词的搭配好的。


model {
  ssd {
    num_classes: 1
    box_coder {
      faster_rcnn_box_coder {
        y_scale: 
        x_scale: 
        height_scale: 
        width_scale: 
      }
    }
。。。


train_config: {
  batch_size: 24
  optimizer {
    rms_prop_optimizer: {
      learning_rate: {
        exponential_decay_learning_rate {
          initial_learning_rate: 
          decay_steps: 800720
          decay_factor: 
        }
      }
      momentum_optimizer_value: 
      decay: 
      epsilon: 
    }
  }
  fine_tune_checkpoint: "/home/forest/tensorflow/models/research/object_detection/ssd_mobilenet_v1_coco_2017_11_17/"
  from_detection_checkpoint: true
  # Note: The below line limits the training process to 200K steps, which we
  # empirically found to be sufficient enough to train the pets 创纪录的集。 This
  # effectively bypasses the learning rate schedule (the learning rate will
  # never 衰落) Remove the below line to train 不定期地
  num_steps: 200000
  data_augmentation_options {
    random_horizontal_flip {
    }
  }
  data_augmentation_options {
    ssd_random_crop {
    }
  }
}


train_input_reader: {
  tf_record_input_reader {
    input_path: "/home/forest/dataset/raccoon_dataset-master/"
  }
  label_map_path: "/home/forest/dataset/raccoon_dataset-master/"
}


eval_config: {
  num_examples: 40
  # Note: The below line limits the evaluation process to 10 评价。
  # Remove the below line to evaluate 不定期地
  metrics_set:"pascal_voc_metrics"
  max_evals: 10
}


eval_input_reader: {
  tf_record_input_reader {
    input_path: "/home/forest/dataset/raccoon_dataset-master/"
  }
  label_map_path: "/home/forest/dataset/raccoon_dataset-master/"
  shuffle: false
  num_readers: 1
}

在~/dataset/raccoon_dataset-master/编目录下新建人家train编目录,用于拘押健康训练的反省点证明。

五、开端锻炼

在目的检测道路下,治理以下命令,开端锻炼,:

# 从 ~/tensorflow/models/research/object_detection 在编目录中运转以下命令

python –logtostderr \–pipeline_config_path=/home/forest/dataset/raccoon_dataset-master/ \–train_dir=/home/forest/dataset/raccoon_dataset-master/train

出路GPU,证明的五十分之一的五行是本质的的。,调配上面这句:

#GPU必要累积而成上面这句,0表现第1块GPU装备

[”CUDA_VISIBLE_DEVICES”] = “0”

您可以在端子中任性时期按下CTRL C。,训练止付,这么笔者其时完毕锻炼呢?请看上面的使满意。。

六、用检验集评价训练出路(可选)

在~/dataset/raccoon_dataset-master/编目录下新建人家eval编目录,拘押健康EVE证明。孤独端子,治理以下命令

(根) forest@forest-MS-7A20:~/tensorflow/models/research/object_detection$ python eval.py \
	--logtostderr \
	--pipeline_config_path=/home/forest/dataset/raccoon_dataset-master/ \
	--checkpoint_dir=/home/forest/dataset/raccoon_dataset-master/train \
	--eval_dir=/home/forest/dataset/raccoon_dataset-master/eval

拘押端子运转。,锻炼完毕时,Ctrl+C端元

七、用张肌板检查训练列队行进

为了更手边的 TensorFlow 顺序担心、调试与使尽可能有效,谷歌放开了一组决定。 TensorBoard 的目测器。你可以用 TensorBoard 揭露给你看。 TensorFlow 图像,绘制图像制造的定量调图和附加创纪录的。TensorBoard 通道读数 TensorFlow 事情证明运转。TensorFlow 事情证明包罗您将在那里。 TensorFlow 运转中关涉的次要创纪录的。

开拓新安置楼,运转以下教:

(根) forest@forest-MS-7A20:~$ source activate root
(根) forest@forest-MS-7A20:~$ tensorboard --logdir=/home/forest/dataset/raccoon_dataset-master/

在浏览程序中翻开浏览程序正中鹄的HTTP地址。,你可以警告TooBooad的次要接口。,这是可以警告的。,在20k step先发制人,loss空投很快,继后就动摇很小了,通道一夜的锻炼,眼前切80k step了, mAP破旧的正确无误的率为67%(鉴于演讲的锻炼完继后改造的eval,这是同上垂线。,在训练上,盖尔老是开着的。,这应该是人家环形。,笔者险乎可以终止锻炼。。在锻炼的多么端子里按CTRL+C终止锻炼吧。

若你在train后,就开端跑了eval的话,你还可以像上面平均拖地而行滑动条来检查跟随时期研制,认识出路的用眼的变更。

八、将反省点证明导出为上冻的以前的证明

 TensorFlow建立工作关系中拿有雅量的的必要锻炼的变量,当锻炼完毕时,这些变量的值就决定了,笔者可以用上面的方式将锻炼的反省点证明里的变量交换为永恒值,导出成用于推断的以前的证明。注意到75895 切断依据你本人的最后的人家反省点的编号来修补。

(根) forest@forest-MS-7A20:~/tensorflow/models-master/research/object_detection$ python export_inference_graph.py \
--pipeline_config_path=/home/forest/dataset/raccoon_dataset-master/ \
--trained_checkpoint_prefix=/home/forest/dataset/raccoon_dataset-master/train/-75895 \
--output_directory=/home/forest/dataset/raccoon_dataset-master/train

/home/forest/dataset/raccoon_dataset-master/train下的 证明执意笔者珍贵的,终极要的以前的证明。

九、用以前的终止浣熊的认识

翻开《Ubuntu 16.04下镶嵌TensorFlow Object Detection API(情人检测API)里最后的一步提到的DEMO,做列举如下修正:

…..

单步运转每党

终极出路列举如下

This entry was posted in 澳门葡京官网. Bookmark the <a href="https://www.ezbukz.com/ampjgw/262.html" title="Permalink to 用TensorFlow训练一个物体检测器(手把手教学版)" rel="bookmark">permalink</a>.

发表评论

电子邮件地址不会被公开。 必填项已用*标注