1. 程式人生 > >利用tensorflow object detection訓練ssd_mobilenets

利用tensorflow object detection訓練ssd_mobilenets

已經安裝好了object detection這個api,先利用該api對ssd_mobilenets進行訓練

1.資料夾構造

為不影響tensorflow的原始碼,我在我的主資料夾下新建了名為ssd_mobilenets的資料夾,裡面放置了名為QRCodeData的資料夾,QRCodeData中有image和xml兩個資料夾,其中image包含這圖片資料,xml包含著xml資料。在根目錄新建models資料夾,用於存放config和預訓練模型。繼續再根目錄新建data資料夾,用於存放.record資料。

2.資料準備

tensorflow有自己的格式要求,pascal的voc格式不能直接使用,但是之前說過了voc格式的資料即使各個框架不適用,但是也會給轉化的指令碼。為了進行轉化,除了影象資料和xml檔案,還需要準備一下幾個檔案

(1)準備.pbtxt檔案

.pbtxt用於表示label和id的對映關係,我的.pbtxt檔案命名pascal_label_map.pbtxt,其中內容如下:

item {
  id: 1
  name: 'QR_code'
}

如有多類則在後面不斷增加id

item {
  id: 2
  name: XXX
}

(2)準備.txt檔案

.txt檔案用於表示影象檔名中目標與id之間的對映關係。用指令碼提取出所有資料集的名稱,每個影象檔名佔一行:

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Wed Jul 12 17:50:26 2017

@author: seven
"""
import os from os import listdir, getcwd from os.path import join if __name__ == '__main__': source_folder='/home/seven/darknet/infrared/plate/image/'#地址是所有圖片的儲存地點 dest='/home/seven/darknet/infrared/train.txt' #儲存train.txt的地址 file_list=os.listdir(source_folder) #賦值圖片所在資料夾的檔案列表 train_file=open(dest,'a'
) #開啟檔案 for file_obj in file_list: #訪問檔案列表中的每一個檔案 file_path=os.path.join(source_folder,file_obj) file_name,file_extend=os.path.splitext(file_obj) #file_name 儲存檔案的名字,file_extend儲存副檔名 file_num=int(file_name) train_file.write(file_name+'\n') train_file.close()#關閉檔案

將source_folder改為自己影象所在路徑,dest為輸出txt的路徑。得到的.txt每一行儲存著image中每一個影象的名稱,由於我只檢測一類,所以直接在每一行加個1。我的.txt檔案內容如下:

QR_code_2017711_122 1
QR_code_2017711_422 1
QR_code_2017711_506 1
QR_code_2017711_538 1
QR_code_2017711_474 1
QR_code_2017711_310 1
QR_code_2017711_499 1
QR_code_2017711_081 1
QR_code_2017711_269 1
QR_code_2017711_472 1
QR_code_2017711_211 1
QR_code_2017711_060 1
QR_code_2017711_071 1
QR_code_2017711_204 1
QR_code_2017711_276 1
QR_code_2017711_206 1
QR_code_2017711_214 1
QR_code_2017711_124 1
QR_code_2017711_331 1
QR_code_2017711_180 1

(3)生成.record檔案

官方提供了指令碼,我將指令碼進行了修改,不需要再加其他引數,直接在程式碼中修改路徑後,在ssd_mobilenets根目錄儲存為a.py,終端進入ssd_mobilenets ,輸入python a.py即可。
指令碼中,data_dir為資料路徑,其下有著image和xml兩個資料夾, output_path為輸出.record檔案的路徑, label_map_path為生成的.pbtxt路徑,examples_path為生成的txt檔案路徑。annotations_dir為xml檔案路徑。
(生成QR_code_val.record用於模型評估,生成QR_code_train.record用於訓練,每次該指令碼只能生成一個record,所以需要val.record可直接用此指令碼,需要train.record需要將指令碼中的val改為train)

# Copyright 2017 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# 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 implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================

r"""Convert raw PASCAL dataset to TFRecord for object_detection.

Example usage:
    ./create_pascal_tf_record --data_dir=/home/user/VOCdevkit \
        --year=VOC2012 \
        --output_path=/home/user/pascal.record
"""
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





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

  Notice that this function normalizes the bounding box coordinates provided
  by the raw data.

  Args:
    data: dict holding PASCAL XML fields for a single image (obtained by
      running dataset_util.recursive_parse_xml_to_dict)
    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  (default: False).
    image_subdirectory: String specifying subdirectory within the
      PASCAL dataset directory holding the actual image data.

  Returns:
    example: The converted tf.Example.

  Raises:
    ValueError: if the image pointed to by data['filename'] is not a valid JPEG
  """
  img_path = os.path.join(image_subdirectory, data['filename']+'.jpg')
  full_path = os.path.join(dataset_directory, img_path)
  with tf.gfile.GFile(full_path, 'rb') as fid:
    encoded_jpg = fid.read()
  encoded_jpg_io = io.BytesIO(encoded_jpg)
  image = PIL.Image.open(encoded_jpg_io)
  if image.format != 'JPEG':
    raise ValueError('Image format not JPEG')
  key = hashlib.sha256(encoded_jpg).hexdigest()

  width = int(data['size']['width'])
  height = int(data['size']['height'])

  xmin = []
  ymin = []
  xmax = []
  ymax = []
  classes = []
  classes_text = []
  truncated = []
  poses = []
  difficult_obj = []
  for obj in data['object']:
    difficult = bool(int(obj['difficult']))


    xmin.append(float(obj['bndbox']['xmin']) / width)
    ymin.append(float(obj['bndbox']['ymin']) / height)
    xmax.append(float(obj['bndbox']['xmax']) / width)
    ymax.append(float(obj['bndbox']['ymax']) / height)
    classes_text.append(obj['name'].encode('utf8'))
    classes.append(label_map_dict[obj['name']])
    truncated.append(int(obj['truncated']))
    poses.append(obj['pose'].encode('utf8'))

  example = tf.train.Example(features=tf.train.Features(feature={
      'image/height': dataset_util.int64_feature(height),
      'image/width': dataset_util.int64_feature(width),
      'image/filename': dataset_util.bytes_feature(
          data['filename'].encode('utf8')),
      'image/source_id': dataset_util.bytes_feature(
          data['filename'].encode('utf8')),
      'image/key/sha256': dataset_util.bytes_feature(key.encode('utf8')),
      'image/encoded': dataset_util.bytes_feature(encoded_jpg),
      'image/format': dataset_util.bytes_feature('jpeg'.encode('utf8')),
      'image/object/bbox/xmin': dataset_util.float_list_feature(xmin),
      'image/object/bbox/xmax': dataset_util.float_list_feature(xmax),
      'image/object/bbox/ymin': dataset_util.float_list_feature(ymin),
      'image/object/bbox/ymax': dataset_util.float_list_feature(ymax),
      'image/object/class/text': dataset_util.bytes_list_feature(classes_text),
      'image/object/class/label': dataset_util.int64_list_feature(classes),
      '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),
  }))
  return example


def main(_):
  data_dir = '/home/seven/ssd_mobilenets/QRCodeData'
  output_path = '/home/seven/ssd_mobilenets/data/QR_code_val.record'
  writer = tf.python_io.TFRecordWriter(output_path)
  label_map_path = '/home/seven/ssd_mobilenets/data/pascal_label_map.pbtxt'
  label_map_dict = label_map_util.get_label_map_dict(label_map_path)


  examples_path = '/home/seven/ssd_mobilenets/data/val.txt'
  annotations_dir = '/home/seven/ssd_mobilenets/QRCodeData/xml'
  examples_list = dataset_util.read_examples_list(examples_path)
  for idx, example in enumerate(examples_list):
    if idx % 100 == 0:
      logging.info('On image %d of %d', idx, len(examples_list))
    path = os.path.join(annotations_dir, example + '.xml')
    with tf.gfile.GFile(path, 'r') as fid:
      xml_str = fid.read()
    xml = etree.fromstring(xml_str)
    data = dataset_util.recursive_parse_xml_to_dict(xml)['annotation']

    tf_example = dict_to_tf_example(data, data_dir, label_map_dict)
    writer.write(tf_example.SerializeToString())

  writer.close()


if __name__ == '__main__':
  tf.app.run()

這樣就生成了需要的.record檔案!

3.下載預訓練模型

點選下載模型:預訓練模型
下載後解壓到ssd_mobilenets/models

4.修改配置檔案

將檔案models/object_detection/samples/configs/ssd_mobilenet_v1_pets.config複製到ssd_mobilenets/models並開啟做如下修改:
(1) num_classes:修改為自己的classes num
(2)將所有PATH_TO_BE_CONFIGURED的地方修改為自己之前設定的路徑(總共五處)
我的修改後如下:

# SSD with Mobilenet v1, configured for Oxford-IIIT Pets Dataset.
# 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 configured.

model {
  ssd {
    num_classes: 1
    box_coder {
      faster_rcnn_box_coder {
        y_scale: 10.0
        x_scale: 10.0
        height_scale: 5.0
        width_scale: 5.0
      }
   (中間省略)
  fine_tune_checkpoint: "/home/seven/ssd_mobilenets/result/1-50000/model.ckpt-50000"
  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 dataset. This
  # effectively bypasses the learning rate schedule (the learning rate will
  # never decay). Remove the below line to train indefinitely.
  num_steps: 600000
  data_augmentation_options {
    random_horizontal_flip {
    }
  }
  data_augmentation_options {
    ssd_random_crop {
    }
  }
}

train_input_reader: {
  tf_record_input_reader {
    input_path: "/home/seven/ssd_mobilenets/data/QR_code_train.record"
  }
  label_map_path: "/home/seven/ssd_mobilenets/data/pascal_label_map.pbtxt"
}

eval_config: {
  num_examples: 100
  # Note: The below line limits the evaluation process to 10 evaluations.
  # Remove the below line to evaluate indefinitely.
  max_evals: 10
}

eval_input_reader: {
  tf_record_input_reader {
    input_path: "/home/seven/ssd_mobilenets/data/QR_code_val.record"
  }
  label_map_path: "/home/seven/ssd_mobilenets/data/pascal_label_map.pbtxt"
  shuffle: false
  num_readers: 1
}

5.進行訓練

終端進入models/object_detection路徑,輸入

python train.py --train_dir='/home/seven/ssd_mobilenets/result' --pipeline_config_path='/home/seven/ssd_mobilenets/models/ssd_mobilenet_v1_pets.config

其中train_dir為訓練模型的輸出路徑,pipeline_config_path為config配置檔案路徑。生成的模型將再train_dir中。

6.tensorboard視覺化

終端輸入

tensorboard --logdir='/home/seven/ssd_mobilenets/result

即可根據提示在瀏覽器中開啟網址觀看訓練資料(我的Firefox開啟失敗,用谷歌瀏覽器正常開啟),其中logdir為生成模型的輸出路徑。