“ 一个人如果不能学会遗忘,那将是很痛苦的事,别再自寻烦恼,快把痛苦的事给忘了吧!”

为了能够使用Object Detection API~

需要将数据集格式转化为.TFRecord再进行训练~

至于,

如何使用Tensorflow官方的Object Detection API

包括下载、依赖(protobuf等)安装、跑demo、训练自己的数据过程~

推荐一篇博文:  1.https://blog.csdn.net/rookie_wei/article/details/81143814

2.https://blog.csdn.net/rookie_wei/article/details/81210499

3.https://blog.csdn.net/rookie_wei/article/details/81275663

整个过程比较详细,可以参考~

本篇主要介绍如何将已标注好的数据集转化成Tensorflow通用的.TFRecord格式~

注意:本程序是我自己检测的6类object,根据情况修改!

#-*- coding=utf-8 -*-
# File Name: Create_TFRecord.py
# Author: HZ
# Created Time: 2018-06-06
import os
import sys
import randomimport numpy as np
import tensorflow as tfimport xml.etree.ElementTree as ET #操作xml文件#我的标签定义有6类,根据自己的图片而定
VOC_LABELS = {'none': (0, 'Background'),'person': (1, 'Person'),'car': (2, 'Car'),'bus': (3, 'Bus'),'truck': (4, 'Truck'),'cyclist': (5, 'cyclist')
}# 图片和标签存放的文件夹.
DIRECTORY_ANNOTATIONS = 'Annotations/'
DIRECTORY_IMAGES = 'JPEGImages/'# 随机种子.
RANDOM_SEED = 4242  #生成整数型,浮点型和字符串型的属性
def int64_feature(value):if not isinstance(value, list):value = [value]return tf.train.Feature(int64_list=tf.train.Int64List(value=value))def float_feature(value):if not isinstance(value, list):value = [value]return tf.train.Feature(float_list=tf.train.FloatList(value=value))def bytes_feature(value):if not isinstance(value, list):value = [value]return tf.train.Feature(bytes_list=tf.train.BytesList(value=value))#图片处理
def _process_image(directory, name):# Read the image file.filename = directory + DIRECTORY_IMAGES + name + '.jpg'image_data = tf.gfile.FastGFile(filename, 'rb').read()# Read the XML annotation file.filename = os.path.join(directory, DIRECTORY_ANNOTATIONS, name + '.xml')tree = ET.parse(filename)root = tree.getroot()# Image shape.size = root.find('size')shape = [int(size.find('height').text),int(size.find('width').text),int(size.find('depth').text)]# Find annotations.bboxes = []labels = []labels_text = []difficult = []truncated = []for obj in root.findall('object'):label = obj.find('name').textlabels.append(int(VOC_LABELS[label][0]))labels_text.append(label.encode('ascii')) #变为ascii格式if obj.find('difficult'):difficult.append(int(obj.find('difficult').text))else:difficult.append(0)if obj.find('truncated'):truncated.append(int(obj.find('truncated').text))else:truncated.append(0)bbox = obj.find('bndbox')a=float(bbox.find('ymin').text) / shape[0]b=float(bbox.find('xmin').text) / shape[1]a1=float(bbox.find('ymax').text) / shape[0]b1=float(bbox.find('xmax').text) / shape[1]a_e=a1-ab_e=b1-bif abs(a_e)<1 and abs(b_e)<1:bboxes.append((a,b,a1,b1))return image_data, shape, bboxes, labels, labels_text, difficult, truncated#转化样例
def _convert_to_example(image_data, labels, labels_text, bboxes, shape,difficult, truncated):xmin = []ymin = []xmax = []ymax = []for b in bboxes:assert len(b) == 4# pylint: disable=expression-not-assigned[l.append(point) for l, point in zip([ymin, xmin, ymax, xmax], b)]# pylint: enable=expression-not-assignedimage_format = b'JPEG'example = tf.train.Example(features=tf.train.Features(feature={'image/height': int64_feature(shape[0]),'image/width': int64_feature(shape[1]),'image/channels': int64_feature(shape[2]),'image/shape': int64_feature(shape),'image/object/bbox/xmin': float_feature(xmin),'image/object/bbox/xmax': float_feature(xmax),'image/object/bbox/ymin': float_feature(ymin),'image/object/bbox/ymax': float_feature(ymax),'image/object/bbox/label': int64_feature(labels),'image/object/bbox/label_text': bytes_feature(labels_text),'image/object/bbox/difficult': int64_feature(difficult),'image/object/bbox/truncated': int64_feature(truncated),'image/format': bytes_feature(image_format),'image/encoded': bytes_feature(image_data)}))return example
#增加到tfrecord
def _add_to_tfrecord(dataset_dir, name, tfrecord_writer):image_data, shape, bboxes, labels, labels_text, difficult, truncated = \_process_image(dataset_dir, name)example = _convert_to_example(image_data, labels, labels_text,bboxes, shape, difficult, truncated)tfrecord_writer.write(example.SerializeToString())
#name为转化文件的前缀
def _get_output_filename(output_dir, name, idx):return '%s/%s_%03d.tfrecord' % (output_dir, name, idx)def run(dataset_dir, output_dir, name='voc_train', shuffling=False):if not tf.gfile.Exists(dataset_dir):tf.gfile.MakeDirs(dataset_dir)  path = os.path.join(dataset_dir, DIRECTORY_ANNOTATIONS)filenames = sorted(os.listdir(path)) #排序# shuffling == Ture时,打乱顺序if shuffling:random.seed(RANDOM_SEED)random.shuffle(filenames)i = 0fidx = 0while i < len(filenames):# Open new TFRecord file.tf_filename = _get_output_filename(output_dir, name, fidx)with tf.python_io.TFRecordWriter(tf_filename) as tfrecord_writer:while i < len(filenames):sys.stdout.write(' Converting image %d/%d \n' % (i+1, len(filenames)))#终端打印,类似printsys.stdout.flush() #缓冲filename = filenames[i]img_name = filename[:-4]_add_to_tfrecord(dataset_dir, img_name, tfrecord_writer)i += 1fidx += 1print('\nFinished converting the Pascal VOC dataset!')#原数据集路径,输出路径以及输出文件名
dataset_dir="./VOC2007/"
output_dir="./TFRecords"
name="voc_train"
def main(_):run(dataset_dir, output_dir,name)if __name__ == '__main__':tf.app.run()

在获得训练好的模型,进行检测时的demo.py如下: (较好)

#encoding:utf-8
import tensorflow as tf
import numpy as npimport os
from matplotlib import pyplot as plt
from PIL import Image
from object_detection.utils import label_map_util
from object_detection.utils import visualization_utils as vis_utils#下载下来的模型的目录
MODEL_DIR = 'object_detection/ssd_mobilenet_v1_coco_2018_01_28'
#下载下来的模型的文件
MODEL_CHECK_FILE = os.path.join(MODEL_DIR, 'frozen_inference_graph.pb')
#数据集对于的label
MODEL_LABEL_MAP = os.path.join('object_detection/data', 'mscoco_label_map.pbtxt')
#数据集分类数量,可以打开mscoco_label_map.pbtxt文件看看
MODEL_NUM_CLASSES = 90#这里是获取实例图片文件名,将其放到数组中
PATH_TO_TEST_IMAGES_DIR = 'object_detection/test_images'
TEST_IMAGES_PATHS = [os.path.join(PATH_TO_TEST_IMAGES_DIR, 'image{}.jpg'.format(i)) for i in range(1, 6)]#输出图像大小,单位是in
IMAGE_SIZE = (12, 8)tf.reset_default_graph()#将模型读取到默认的图中
with tf.gfile.GFile(MODEL_CHECK_FILE, 'rb') as fd:_graph = tf.GraphDef()_graph.ParseFromString(fd.read())tf.import_graph_def(_graph, name='')#加载COCO数据标签,将mscoco_label_map.pbtxt的内容转换成
# {1: {'id': 1, 'name': u'person'}...90: {'id': 90, 'name': u'toothbrush'}}格式
label_map = label_map_util.load_labelmap(MODEL_LABEL_MAP)
categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=MODEL_NUM_CLASSES)
category_index = label_map_util.create_category_index(categories)#将图片转化成numpy数组形式
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_graph = tf.get_default_graph()
with tf.Session(graph=detection_graph) as sess:for image_path in TEST_IMAGES_PATHS:print(image_path)#读取图片image = Image.open(image_path)#将图片数据转成数组image_np = load_image_into_numpy_array(image)#增加一个维度image_np_expanded = np.expand_dims(image_np, axis=0)#下面都是获取模型中的变量,直接使用就好了image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')#存放所有检测框boxes = detection_graph.get_tensor_by_name('detection_boxes:0')#每个检测结果的可信度scores = detection_graph.get_tensor_by_name('detection_scores:0')#每个框对应的类别classes = detection_graph.get_tensor_by_name('detection_classes:0')#检测框的个数num_detections = detection_graph.get_tensor_by_name('num_detections:0')#开始计算(boxes, scores, classes, num_detections) = sess.run([boxes, scores, classes, num_detections],feed_dict={image_tensor : image_np_expanded})#打印识别结果print(num_detections)print(boxes)print(classes)print(scores)#得到可视化结果vis_utils.visualize_boxes_and_labels_on_image_array(image_np,np.squeeze(boxes),np.squeeze(classes).astype(np.int32),np.squeeze(scores),category_index,use_normalized_coordinates=True,line_thickness=8)#显示plt.figure(figsize=IMAGE_SIZE)plt.imshow(image_np)plt.show()

恩,复习+巩固!

sweet~

将自己手动标注的数据集(PascalVOC格式)转化为.TFRecord格式相关推荐

  1. TensorFlow学习笔记之 bmp格式、txt格式数据转换成tfrecord 格式

    目录 一.前言 二.bmp 格式数据转换成 tfrecord 格式的代码 三.txt 格式数据转换成 tfrecord 格式的代码 一.前言 之前我们讲过了关于 tfrecord 格式的相关内容,在这 ...

  2. 求助,在使用TensorFlow进行花朵图片分类时,想把数据集的图片转化为tfrecord文件,但是程序运行后没有反应,不知道问题出在哪里

    求助,在使用TensorFlow进行花朵图片分类时,想把数据集的图片转化为tfrecord文件,但是程序运行后没有反应,一直显示服务空闲,不知道问题出在哪里,一下是我的程序,希望可以指点我一下哪里出了 ...

  3. 如何将caj格式转化为word格式

    如何将caj格式转化为word格式 caj文件的识别: 1)局部文字识别:直接使用caj浏览器的ocr  2)全文件识别:打印到Microsoft Office document Image Writ ...

  4. 批量将OFD格式转化为PDF格式文件(来源于chatgpt)

    批量将OFD格式转化为PDF格式文件 一.pyofd库 预先下载pip install pyofd. import os from pyofd import OFDFile from pyofd.re ...

  5. 目标检测,将voc格式转化为coco格式详细过程

    在目标检测方法研究中,数据集的格式至关重要,为了减小模型的训练时长,需要现在小数据集进行模型研究,从而需要将VOC数据集转化为coco格式.下面一步一步来看: 1. 下载VOC数据集 Pascal V ...

  6. 将搜狗词库.scel格式转化为.txt格式

    [2020年5月28日更新:有一说一,这篇文章是我2017年底在新浪工作时处理家居.房产频道相关业务时的实践,代码是后来从自己代码库直接粘贴的,当然转码部分的代码是借鉴的,当时也是查阅了几种方法,一一 ...

  7. DGN格式转化为shp格式 【转】

    其实本来,我就是需要把一个autocad的dwg/dgn格式的东西导入到google earth里面:但是首先我对dwg/dgn格式的东西根本就不熟:其次我拿到的dwg/dgn格式文件是用的HK80 ...

  8. DGN格式转化为shp格式

    其实本来,我就是需要把一个autocad的dwg/dgn格式的东西导入到google earth里面:但是首先我对dwg/dgn格式的东西根本就不熟:其次我拿到的dwg/dgn格式文件是用的HK80 ...

  9. 数据集json格式转化为xml格式、txt格式

    (仅用于自己学习时记录) 在使用YOLO V5训练Tsinghua-Tencent 100k时发现的问题.官方的数据集为json格式,而YOLO需要txt格式.在网上查阅了相关资料,并记录. Tsin ...

最新文章

  1. Lidar与IMU标定代码实战:lidar_align
  2. txt或者csv数据文件的格式是有要求的,如下shell代码中说明。
  3. DeepLab:语义图像分割
  4. WINDOWS 几种坐标系
  5. 小技巧:with用法 pycharm控制台输出带颜色的文字 打印进度条的
  6. Android重点笔记,安卓listview 懒加载的实现笔记
  7. 遥感影像滤波处理软件 — timesat3.2
  8. IDEA项目找不到浏览器报错的情况
  9. [转载] python[1]-print中的sep、end参数
  10. 吴恩达机器学习神经网络作业(python实现)
  11. 推荐两个Magento做的中文网站 GAP和佰草集
  12. PHP面向对象分析设计的61条军规
  13. Atitit.log日志技术的最佳实践attilax总结
  14. 江苏省2008年普通高校自主招生入选考生名单公示 - 北大清华版
  15. cyclone小知识(二)——cyclone加载扫描工程的数据
  16. 该怎么选择便宜和贵的SSL证书
  17. 1月全球CTF比赛时间汇总来了!
  18. CloudFlare Workers 设置使用自定义域名
  19. JavaScript计算两个日期之间相差的天数
  20. 【广东工业大学】谭子真丨个人作品展示

热门文章

  1. 医嘱卡片打印功能修改
  2. Python获取股票数据并绘制相应K线图,看这个就够了!
  3. BERT(二)——BERT 缺陷
  4. 汽车电子革命的四大核心技术趋势
  5. input失去焦点验证格式_在文本框的onblur事件里写验证信息
  6. 三国杀的联想-前端页面进度条的生成
  7. android 设置键盘按钮事件,Android 改变软键盘的回车键按钮格式,然后监听回车键的点击事件...
  8. vivo云测平台上线Android 13开发者预览版通知
  9. [Python Study Notes]进程信息(丁丁软件监控进程,http-post)
  10. SpringBoot33-springboot开发部署与测试-spring boot测试