Tensorflow object detection 训练并使用自己的模型

第一次训练记录一下:

1.使用 LabelImg 这款小软件,将训练测试图片进行人工标注,标注完成后保存为同名的xml文件。

2.(1)对于Tensorflow,需要输入专门的 TFRecords Format 格式。

调用写好的xml_to_csv.py将xml文件转换为csv文件,
在Spyder中打开xml_to_csv.py(D:\install package\TensorFlow)文件,更改os.chdir(19行,改为自己的测试集训练集路径) 、path(20行 改为自己的测试集训练集路径) 、 xml_df.to_csv(46行 名字改为自己的.csv文件)
将生成的.csv文件放入data中。

第二步从.csv表格中创建TFRecord
调用generate_tfrecord.py,将csv文件转换为record文件。
打开generate_tfrecord.py,(1)更改os.chdir(C:\ProgramData\Anaconda3\envs\tensorflow\Lib\site-packages\tensorflow\models\research\object_detection\)

(2)更改# TO-DO replace this with label map,更改为自己的分类。
(3)更改main()函数中的 path = os.path.join(os.getcwd(), ‘image2\train’),将加粗部分改为自己的图片路径image2\train和image2\test。
打开anaconda prompt,定位到objection目录下,执行

python generate_TFRecord.py --csv_input=data/dog_train.csv  --output_path=data/dog_train.record
 python generate_TFRecord.py --csv_input=data/dog_test.csv  --output_path=data/dog_test.record

对于训练集与测试集分别运行上述代码即可,得到train.record与test.record文件

在对应目录(/data)下,创建一个 dog.pbtxt的文本文件(可以复制一个其他名字的文件,然后用文本编辑软件打开修改),写入自己的标签,我的例子中是一个,id序号注意与前面创建CSV文件时保持一致。

item {
 id: 1
name: 'dog'
}

3.Object Detection github链接: 模型.
将ssd_mobilenet_v1_coco.config 放在training 文件夹下,记事本打开,改下述代码:

(1)、搜索其中的 PATH_TO_BE_CONFIGURED ,将对应的路径改为自己的路径,注意不要把test跟train弄反了;
在这里插入图片描述
(2)、将 num_classes 按照实际情况更改

(3)、batch_size 原本是24,我在运行的时候出现显存不足的问题,故改为1。

4.开始训练

切换到object_detection目录,输入:

python train.py --logtostderr --train_dir=training/ --pipeline_config_path=training/ssd_mobilenet_v1_coco.config

–train_dir=
后面加的是输出check_point的文件夹路径
–pipeline_config_path=
加的是配置文件的路径

如果没有train.py,在object_detection\legacy文件夹下找到train.py,并放入object_detection文件夹

训练时出现错误:InvalidArgumentError: Unsuccessful TensorSliceReader constructor: Failed to get matching files on PATH_TO_BE_CONFIGURED/model.ckpt: Not found: FindFirstFile failed for: PATH_TO_BE_CONFIGURED : ϵͳ\udcd5Ҳ\udcbb\udcb5\udcbdָ\udcb6\udca8\udcb5\udcc4·\udcbe\udcb6\udca1\udca3
; No such process

解决方法:将ssd_mobilenet_v1_coco.config中的两行删除
在这里插入图片描述
解决上述问题重新开始训练。

训练结束后,在training文件夹中会出现checkpoint和一些model.ckpt文件。

5
在 models\research\object_detection 文件夹下找到 export_inference_graph.py 文件,要运行这个文件,还需要传入config以及checkpoint的相关参数。

训练结束后,打开Anaconda Prompt 定位到 models\research\object_detection 文件夹下,

运行下述代码:

python export_inference_graph.py --input_type image_tensor --pipeline_config_path training/ssd_mobilenet_v1_coco.config --trained_checkpoint_prefix training/model.ckpt-10000 --output_directory dog_detection

–trained_checkpoint_prefix training/model.ckpt-10000 这个checkpoint(.ckpt-后面的数字)可以在training文件夹下找到你自己训练的模型的情况,填上对应的数字(如果有多个,选最大的)。

-output_directory dog_detection改成自己的名字

运行完后,可以在 dog_detection文件夹下发现若干文件,有saved_model、checkpoint、frozen_inference_graph.pb等。 .pb结尾的就是最重要的frozen model了

Tensorflow还提供功能强大的Tensorboard来可视化训练过程。

Anaconda Prompt 定位到 models\research\object_detection 文件夹下,运行

tensorboard --logdir='training'或者tensorboard --logdir=training

6.测试模型
打开jupyter notebook ,为了方便我直接copy了一个object_detection_tutorial.ipynb,在上面修改代码。

#!/usr/bin/env python
# coding: utf-8

# # Object Detection Demo
# Welcome to the object detection inference walkthrough!  This notebook will walk you step by step through the process of using a pre-trained model to detect objects in an image. Make sure to follow the [installation instructions](https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/installation.md) before you start.

# # Imports

# In[1]:


import numpy as np
import os
import six.moves.urllib as urllib
import sys
import tarfile
import tensorflow as tf
import zipfile


from distutils.version import StrictVersion
from collections import defaultdict
from io import StringIO
from matplotlib import pyplot as plt
from PIL import Image

# This is needed since the notebook is stored in the object_detection folder.
sys.path.append("..")
from object_detection.utils import ops as utils_ops

if StrictVersion(tf.__version__) < StrictVersion('1.9.0'):
  raise ImportError('Please upgrade your TensorFlow installation to v1.9.* or later!')


# ## Env setup

# In[2]:


# This is needed to display the images.
get_ipython().run_line_magic('matplotlib', 'inline')


# ## Object detection imports
# Here are the imports from the object detection module.

# In[3]:


from utils import label_map_util

from utils import visualization_utils as vis_util


# # Model preparation 

# ## Variables
# 
# Any model exported using the `export_inference_graph.py` tool can be loaded here simply by changing `PATH_TO_FROZEN_GRAPH` to point to a new .pb file.  
# 
# By default we use an "SSD with Mobilenet" model here. See the [detection model zoo](https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/detection_model_zoo.md) for a list of other models that can be run out-of-the-box with varying speeds and accuracies.

# In[4]:


# What model to download.
MODEL_NAME = 'dog_detection'


# Path to frozen detection graph. This is the actual model that is used for the object detection.
PATH_TO_FROZEN_GRAPH = MODEL_NAME + '/frozen_inference_graph.pb'

# List of the strings that is used to add correct label for each box.
PATH_TO_LABELS = os.path.join('data', 'dog.pbtxt')


NUM_CLASSES = 1

# ## Download Model

# In[ ]:





# ## Load a (frozen) Tensorflow model into memory.

# In[5]:


detection_graph = tf.Graph()
with detection_graph.as_default():
  od_graph_def = tf.GraphDef()
  with tf.gfile.GFile(PATH_TO_FROZEN_GRAPH, 'rb') as fid:
    serialized_graph = fid.read()
    od_graph_def.ParseFromString(serialized_graph)
    tf.import_graph_def(od_graph_def, name='')


# ## Loading label map
# Label maps map indices to category names, so that when our convolution network predicts `5`, we know that this corresponds to `airplane`.  Here we use internal utility functions, but anything that returns a dictionary mapping integers to appropriate string labels would be fine

# In[6]:


category_index = label_map_util.create_category_index_from_labelmap(PATH_TO_LABELS, use_display_name=True)


# ## Helper code

# In[7]:


def load_image_into_numpy_array(image):
  (im_width, im_height) = image.size
  return np.array(image.getdata()).reshape(
      (im_height, im_width, 3)).astype(np.uint8)


# # Detection

# In[8]:


# For the sake of simplicity we will use only 2 images:
# image1.jpg
# image2.jpg
# If you want to test the code with your images, just add path to the images to the TEST_IMAGE_PATHS.
PATH_TO_TEST_IMAGES_DIR = 'test_images'
TEST_IMAGE_PATHS = [ os.path.join(PATH_TO_TEST_IMAGES_DIR, 'image{}.jpg'.format(i)) for i in range(1, 4) ]

# Size, in inches, of the output images.
IMAGE_SIZE = (12, 8)


# In[9]:


def run_inference_for_single_image(image, graph):
  with graph.as_default():
    with tf.Session() as sess:
      # Get handles to input and output tensors
      ops = tf.get_default_graph().get_operations()
      all_tensor_names = {output.name for op in ops for output in op.outputs}
      tensor_dict = {}
      for key in [
          'num_detections', 'detection_boxes', 'detection_scores',
          'detection_classes', 'detection_masks'
      ]:
        tensor_name = key + ':0'
        if tensor_name in all_tensor_names:
          tensor_dict[key] = tf.get_default_graph().get_tensor_by_name(
              tensor_name)
      if 'detection_masks' in tensor_dict:
        # The following processing is only for single image
        detection_boxes = tf.squeeze(tensor_dict['detection_boxes'], [0])
        detection_masks = tf.squeeze(tensor_dict['detection_masks'], [0])
        # Reframe is required to translate mask from box coordinates to image coordinates and fit the image size.
        real_num_detection = tf.cast(tensor_dict['num_detections'][0], tf.int32)
        detection_boxes = tf.slice(detection_boxes, [0, 0], [real_num_detection, -1])
        detection_masks = tf.slice(detection_masks, [0, 0, 0], [real_num_detection, -1, -1])
        detection_masks_reframed = utils_ops.reframe_box_masks_to_image_masks(
            detection_masks, detection_boxes, image.shape[0], image.shape[1])
        detection_masks_reframed = tf.cast(
            tf.greater(detection_masks_reframed, 0.5), tf.uint8)
        # Follow the convention by adding back the batch dimension
        tensor_dict['detection_masks'] = tf.expand_dims(
            detection_masks_reframed, 0)
      image_tensor = tf.get_default_graph().get_tensor_by_name('image_tensor:0')

      # Run inference
      output_dict = sess.run(tensor_dict,
                             feed_dict={image_tensor: np.expand_dims(image, 0)})

      # all outputs are float32 numpy arrays, so convert types as appropriate
      output_dict['num_detections'] = int(output_dict['num_detections'][0])
      output_dict['detection_classes'] = output_dict[
          'detection_classes'][0].astype(np.uint8)
      output_dict['detection_boxes'] = output_dict['detection_boxes'][0]
      output_dict['detection_scores'] = output_dict['detection_scores'][0]
      if 'detection_masks' in output_dict:
        output_dict['detection_masks'] = output_dict['detection_masks'][0]
  return output_dict


# In[12]:


for image_path in TEST_IMAGE_PATHS:
  image = Image.open(image_path)
  # the array based representation of the image will be used later in order to prepare the
  # result image with boxes and labels on it.
  image_np = load_image_into_numpy_array(image)
  # Expand dimensions since the model expects images to have shape: [1, None, None, 3]
  image_np_expanded = np.expand_dims(image_np, axis=0)
  # Actual detection.
  output_dict = run_inference_for_single_image(image_np, detection_graph)
  # Visualization of the results of a detection.
  vis_util.visualize_boxes_and_labels_on_image_array(
      image_np,
      output_dict['detection_boxes'],
      output_dict['detection_classes'],
      output_dict['detection_scores'],
      category_index,
      instance_masks=output_dict.get('detection_masks'),
      use_normalized_coordinates=True,
      line_thickness=8)
  plt.figure(figsize=IMAGE_SIZE)
  plt.imshow(image_np)
 

运行结束,输出图片,查看训练效果。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值