1、用labelImg标自己数据集。

并将图片存放在JPEGImages中,xml存放在Annotations中

2、分离训练和测试数据

import os
import randomtrainval_percent = 0.66
train_percent = 0.5
xmlfilepath = 'Annotations'
txtsavepath = 'ImageSets\Main'
total_xml = os.listdir(xmlfilepath)
print(total_xml)
num=len(total_xml)
list=range(num)
tv=int(num*trainval_percent)
tr=int(tv*train_percent)
trainval= random.sample(list,tv)
train=random.sample(trainval,tr)ftrainval = open('ImageSets/Main/trainval.txt', 'w')
ftest = open('ImageSets/Main/test.txt', 'w')
ftrain = open('ImageSets/Main/train.txt', 'w')
fval = open('ImageSets/Main/val.txt', 'w')for i  in list:name=total_xml[i][:-4]+'\n'if i in trainval:ftrainval.write(name)if i in train:ftrain.write(name)else:fval.write(name)else:ftest.write(name)ftrainval.close()
ftrain.close()
fval.close()
ftest .close()

此时ImageSets/Main/目录下生成这四个文件

3、根据train.txt 和 test.txt中内容分别建立train和test文件(存放xml文件)

import os
import shutil
files = os.listdir('E:\gitcode\\tensorflow-model\\VOCPolice\\VOC2007\\Annotations')
file= open('E:\\gitcode\\tensorflow-model\\VOCPolice\\VOC2007\\ImageSets\\Main\\test.txt','r')
newpath='E:\\gitcode\\tensorflow-model\\VOCPolice\\VOC2007\\test'
num1=0
for line in file.readlines():num1=num1+1message=line.split('\n')num=message[0]xmlName=num+'.xml'for i in files:if i==xmlName:oldpath='E:\gitcode\\tensorflow-model\\VOCPolice\\VOC2007\\Annotations'+"\\"+ishutil.copy(oldpath,newpath)break
print(num1)

此时train和test文件中存放的就是train.txt  test.txt中所列的xml文件

4、将xml转换为tfrecord文件

方法一: xml转为csv,csv转为tfrecord

方法二:直接采用object_detection文件中的create_pascal_tf_record.py

本人采用方法二失败了。。方法一成功了。所以就列出来方法一的配置过程:

(1)将xml转为csv

'''
function:xml2csv
'''
import os
import glob
import pandas as pd
import xml.etree.ElementTree as ETdef xml_to_csv(path):xml_list = []for xml_file in glob.glob(path + '/*.xml'):tree = ET.parse(xml_file)root = tree.getroot()for member in root.findall('object'):value = (root.find('filename').text,int(root.find('size')[0].text),int(root.find('size')[1].text),member[0].text,int(member[4][0].text),int(member[4][1].text),int(member[4][2].text),int(member[4][3].text))xml_list.append(value)column_name = ['filename', 'width', 'height', 'class', 'xmin', 'ymin', 'xmax', 'ymax']xml_df = pd.DataFrame(xml_list, columns=column_name)return xml_dfdef main():for directory in ['train', 'test']:project_path = 'E:\\gitcode\\tensorflow-model\\VOCPolice\\VOC2007'image_path = os.path.join(project_path, directory)xml_df = xml_to_csv(image_path)xml_df.to_csv('E:/gitcode/tensorflow-model/VOCPolice/VOC2007/{}_labels.csv'.format(directory), index=None)print('Successfully converted xml to csv.')main()

运行完后VOC2007下有两个文件:train_labels.csv test_labels.csv。如下图所示:

这里运行的前提是之前产生的存放xml的train和test文件夹在project_path下。

(2)将csv文件转换为tfrecord文件

# generate_tfrecord.py# -*- coding: utf-8 -*-"""
Usage:# From tensorflow/models/# Create train data:python generate_tfrecord.py --csv_input=data/tv_vehicle_labels.csv  --output_path=train.record# Create test data:python generate_tfrecord.py --csv_input=data/test_labels.csv  --output_path=test.record
"""import os
import io
import pandas as pd
import tensorflow as tffrom PIL import Image
from object_detection.utils import dataset_util
from collections import namedtuple, OrderedDictos.chdir('E:/gitcode/tensorflow-model/chde222-models-master-MyData1230/models/research/object_detection')flags = tf.app.flags
flags.DEFINE_string('csv_input', 'E:/gitcode/tensorflow-model/VOCPolice/VOC2007/train_labels.csv', 'Path to the CSV input')
flags.DEFINE_string('output_path', 'train.record', 'Path to output TFRecord')
FLAGS = flags.FLAGS# TO-DO replace this with label map
def class_text_to_int(row_label):if row_label == 'police':     # 需改动return 1else:Nonedef split(df, group):data = namedtuple('data', ['filename', 'object'])gb = df.groupby(group)return [data(filename, gb.get_group(x)) for filename, x in zip(gb.groups.keys(), gb.groups)]def create_tf_example(group, path):with tf.gfile.GFile(os.path.join(path, '{}'.format(group.filename)), 'rb') as fid:encoded_jpg = fid.read()encoded_jpg_io = io.BytesIO(encoded_jpg)image = Image.open(encoded_jpg_io)width, height = image.sizefilename = group.filename.encode('utf8')image_format = b'jpg'xmins = []xmaxs = []ymins = []ymaxs = []classes_text = []classes = []for index, row in group.object.iterrows():xmins.append(row['xmin'] / width)xmaxs.append(row['xmax'] / width)ymins.append(row['ymin'] / height)ymaxs.append(row['ymax'] / height)classes_text.append(row['class'].encode('utf8'))classes.append(class_text_to_int(row['class']))tf_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(filename),'image/source_id': dataset_util.bytes_feature(filename),'image/encoded': dataset_util.bytes_feature(encoded_jpg),'image/format': dataset_util.bytes_feature(image_format),'image/object/bbox/xmin': dataset_util.float_list_feature(xmins),'image/object/bbox/xmax': dataset_util.float_list_feature(xmaxs),'image/object/bbox/ymin': dataset_util.float_list_feature(ymins),'image/object/bbox/ymax': dataset_util.float_list_feature(ymaxs),'image/object/class/text': dataset_util.bytes_list_feature(classes_text),'image/object/class/label': dataset_util.int64_list_feature(classes),}))return tf_exampledef main(_):writer = tf.python_io.TFRecordWriter(FLAGS.output_path)path = os.path.join(os.getcwd(), 'images/test')         #  需改动examples = pd.read_csv(FLAGS.csv_input)grouped = split(examples, 'filename')for group in grouped:tf_example = create_tf_example(group, path)writer.write(tf_example.SerializeToString())writer.close()output_path = os.path.join(os.getcwd(), FLAGS.output_path)print('Successfully created the TFRecords: {}'.format(output_path))if __name__ == '__main__':tf.app.run()

修改上述代码中flags.DEFINE_string的csv_input路径为上述步骤产生的train和test的csv路径。然后将output_path分别命名为train.record和test.record文件.运行两次,即可产生train.record 和test.record(注意你自己选择的output路径)

5、准备训练

(1)下载模型ssd_mobilenet_v1_coco_2017_11_17并解压在object_detection目录下

(2)修改pascal_label_map.pbtxt文件。也可新建自己的pbtxt文件。

因为我这里只有一类,所以修改如上。

(3)修改模型的配置文件

修改位置如下:

num_classes: 1  #你的类别数
batch_size: 2   #你的电脑能承受的数量
fine_tune_checkpoint: "E:/gitcode/tensorflow-model/chde222-models-master-MyData1230/models/research/object_detection/ssd_mobilenet_v1_coco_2017_11_17/model.ckpt"#改为你的模型所在的位置 我这里全部使用绝对路径
num_steps: 50000  #改为你所需要训练次数train_input_reader {label_map_path: "E:/gitcode/tensorflow-model/chde222-models-master-MyData1230/models/research/object_detection/data/pascal_label_map.pbtxt"tf_record_input_reader {input_path: "E:/gitcode/tensorflow-model/chde222-models-master-MyData1230/models/research/object_detection/train.record"}
}  #分别将label_map_path 和train input_path 改为你的文件所在的位置eval_input_reader {label_map_path: "E:/gitcode/tensorflow-model/chde222-models-master-MyData1230/models/research/object_detection/data/pascal_label_map.pbtxt"shuffle: falsenum_readers: 1tf_record_input_reader {input_path: "E:/gitcode/tensorflow-model/chde222-models-master-MyData1230/models/research/object_detection/test.record"}
}  #分别将label_map_path 和test input_path 改为你的文件所在的位置

6、开始训练

在object_detection/legacy/train.py 文件中

修改

flags.DEFINE_string中train_dir为你的结果存放path
flags.DEFINE_string中'pipeline_config_path为刚才下载模型的.config所在路径

然后运行即可。

7、查看训练曲线

在object_detection目录下:  logdir目录就是train_dir目录

tensorboard --logdir=E:/gitcode/tensorflow-model/chde222-models-master-MyData1230/models/research/ssdmodel1231

8、 导出模型

修改export_inference_graph.py 文件中

flags.DEFINE_string中pipeline_config_path为你的ssd模型.config所在路径
flags.DEFINE_string中trained_checkpoint_prefix为你训练结果的model.ckpt-训练次数
flags.DEFINE_string中output_directory为你的导出路径

然后运行即可保存

9、测试模型

在object_detection下新建myTest.py 文件

# 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.from distutils.version import StrictVersion
import numpy as np
import os
import six.moves.urllib as urllib
import sys
import tarfile
import tensorflow as tf
import zipfilefrom 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().magic(u'matplotlib inline')# ## Object detection imports
# Here are the imports from the object detection module.from object_detection.utils import label_map_utilfrom object_detection.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 = './Police_detection1231/'
# MODEL_FILE = MODEL_NAME + '.tar.gz'
# DOWNLOAD_BASE = 'http://download.tensorflow.org/models/object_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', 'pascal_label_map.pbtxt')NUM_CLASSES = 1# ## Download Model# opener = urllib.request.URLopener()
# opener.retrieve(DOWNLOAD_BASE + MODEL_FILE, MODEL_FILE)
'''
tar_file = tarfile.open(MODEL_FILE)
for file in tar_file.getmembers():file_name = os.path.basename(file.name)if 'frozen_inference_graph.pb' in file_name:tar_file.extract(file, os.getcwd())
'''# ## Load a (frozen) Tensorflow model into memory.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 finelabel_map = label_map_util.load_labelmap(PATH_TO_LABELS)
categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=NUM_CLASSES, use_display_name=True)
category_index = label_map_util.create_category_index(categories)# ## Helper code# In[8]:def load_image_into_numpy_array(image):(im_width, im_height) = image.sizereturn np.array(image.getdata()).reshape((im_height, im_width, 3)).astype(np.uint8)# # Detection# 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_police'
TEST_IMAGE_PATHS = [ os.path.join(PATH_TO_TEST_IMAGES_DIR, 'image{}.jpg'.format(i)) for i in range(1, 22) ]# Size, in inches, of the output images.
IMAGE_SIZE = (12, 8)# In[10]:config = tf.ConfigProto()
config.gpu_options.allow_growth = True
def run_inference_for_single_image(image, graph):with graph.as_default():with tf.Session(config=config) as sess:# Get handles to input and output tensorsops = 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 imagedetection_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 dimensiontensor_dict['detection_masks'] = tf.expand_dims(detection_masks_reframed, 0)image_tensor = tf.get_default_graph().get_tensor_by_name('image_tensor:0')# Run inferenceoutput_dict = sess.run(tensor_dict,feed_dict={image_tensor: np.expand_dims(image, 0)})# all outputs are float32 numpy arrays, so convert types as appropriateoutput_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[ ]: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.subplot(1, 2, 1)# plt.imshow(image)# plt.subplot(1, 2, 2)# plt.imshow(image_np)plt.savefig(image_path + '_labeled.jpg')# plt.show()# plt.legend()
print("finished")

修改MODEL_NAME为你导出模型的路径

修改 PATH_TO_LABELS为你的.pbtxt文件所在路径

修改NUM_CLASSES为你的类别数

修改PATH_TO_TEST_IMAGES_DIR为你的测试图片所在路径。同时命名形式为image1.jpg、image2.jpg。

在TEST_IMAGE_PATHS中range输入你的图片范围

运行即可。

10 评估模型

修改legacy/eval.py中以下参数。并运行如下

flags.DEFINE_string('checkpoint_dir', 'your trained model path','Directory containing checkpoints to evaluate, typically ''set to `train_dir` used in the training job.')
flags.DEFINE_string('eval_dir', 'your eval model save path', 'Directory to write eval summaries to.')
flags.DEFINE_string('pipeline_config_path', 'your .config path','Path to a pipeline_pb2.TrainEvalPipelineConfig config ''file. If provided, other configs are ignored')

大功告成

参考自

https://blog.csdn.net/Arvin_liang/article/details/84752427

https://chtseng.wordpress.com/2019/02/16/%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8google-object-detection-api%E8%A8%93%E7%B7%B4%E8%87%AA%E5%B7%B1%E7%9A%84%E6%A8%A1%E5%9E%8B/

https://www.cnblogs.com/zongfa/p/9663649.html

https://blog.csdn.net/linolzhang/article/details/87121875

https://blog.csdn.net/Arvin_liang/article/details/84752427

object detection训练自己数据相关推荐

  1. win下使用TensorFlow object detection训练自己模型

    win下使用TensorFlow object detection训练自己模型 1. 环境 2.xml生成csv文件,再生成record文件 2.1 对训练文件和测试文件都使用以下两个文件分别生成自己 ...

  2. 使用object detection训练并识别自己的模型

    1.安装tensorflow(version>=1.4.0) 2.部署tensorflow models - 在这里下载 - 解压并安装 - 解压后重命名为models复制到tensorflow ...

  3. 使用tensorflow object detection api训练自己的数据集

    简介 使用tensorflow object detection训练自己的数据集时,可能会出现 AttributeError: module 'tensorflow.contrib.data' has ...

  4. Object Detection in 20 Years A Survey 论文阅读笔记

    文章链接:https://arxiv.org/pdf/1905.05055.pdf 1.Introduction 作为计算机视觉的基本问题之一,目标检测构成了许多其他计算机视觉任务的基础,例如实例分割 ...

  5. Implicit 3D Orientation Learning for 6D Object Detection from RGB Images

    从RGB图像中进行6D目标检测的隐式三维方向学习 论文是我自己翻译的,限于本人的水平,不到之处请多包涵 摘要:我们提出了一种基于RGB的实时管道[1],用于物体检测和6D姿态估计.我们新颖的3D方向估 ...

  6. 如何在Windows系统上使用Object Detection API训练自己的数据?

    前言 之前写了一篇如何在windows系统上安装Tensorflow Object Detection API?(点击跳转) 然后就想着把数据集换成自己的数据集进行训练得到自己的目标检测模型.动手之前 ...

  7. ssd目标检测训练自己的数据_目标检测Tensorflow object detection API之训练自己的数据集...

    构建自己的模型之前,推荐先跑一下Tensorflow object detection API的demo JustDoIT:目标检测Tensorflow object detection API​zh ...

  8. 关于使用tensorflow object detection API训练自己的模型-补充部分(代码,数据标注工具,训练数据,测试数据)

    之前分享过关于tensorflow object detection API训练自己的模型的几篇博客,后面有人陆续碰到一些问题,问到了我解决方法.所以在这里补充点大家可能用到的东西.声明一下,本人专业 ...

  9. tensorflow使用object detection API训练自己的数据(个人总结)

    1.前期工作准备 1.首先从GitHub上下载models 网址:https://github.com/tensorflow/models,将object detection文件夹整个复制到pytho ...

最新文章

  1. 2.常用的实现多线程的两种方式
  2. TC工具后台模式_聊天能赚钱?来聊后台批量添加账号,伪装女性聊天赚钱内幕...
  3. Confluence 6 针对你的数据库类型确定校验 SQL
  4. python模拟行星运动_使用 Python 来简单的动态模拟一下太阳系的运转
  5. linux职业_对Linux的好奇心导致了意外的职业
  6. oracle查询失效包sql,sql – ORA-00904:子查询中的标识符无效
  7. iF.SVNAdmin
  8. python主题建模_在PYTHON中进行主题模型LDA分析
  9. hive内置函数_Hive Query生命周期 —— 钩子(Hook)函数篇
  10. python 几何计算_计算几何-凸包算法 Python实现与Matlab动画演示
  11. 好用的代理服务器工具_secscanauthcheck越权检查工具
  12. 如何用Directshow采集摄像头图像
  13. 苹果cmsv10黑色炫酷自适应在线视频网站简约模板源码
  14. 解决Cuda out of memory的一种思路
  15. DMA原理AHB-DMA控制器工作过程总结
  16. 大数据审计的发展_大数据时代对审计发展的影响
  17. android11obb,exagear安卓11数据包obb合集版
  18. 水库大坝安全监测监控系统平台axure分析+辽阳市水库大坝安全检测平台+志豪未来科技有限公司+陈志豪
  19. 课程设计:学生成绩管理系统
  20. STM32CubeMX学习--ThreadX_UART

热门文章

  1. 有没有词匹配算法_Google Ads 再次扩展了关键字变量匹配
  2. ios跨线程通知_一种基于Metal、Vulkan多线程渲染能力的渲染架构
  3. matlab如何读取csv,Matlab:如何读取CSV文件以及如何读取带有字符串数据项的CSV文件 | 学步园...
  4. Php jsondb,JsonDB-PHP
  5. Linux进阶之路————开机、重启和用户登录注销
  6. Eclipse使用————生成Get/Set、toString快捷键(不使用鼠标)
  7. 制图折断线_CAD制图初学入门之CAD标注时必须要区分的两个概念
  8. 如何用python做一个会聊天的女朋友_520来啦~教你用Python给自己造了一个女朋友!...
  9. java8 stream 做累加_《Java 8 in Action》Chapter 1:为什么要关心Java 8
  10. java 日期只计算年月日大小_Java 计算两个日期相差多少年月日