文章目录
1. json转record文件
1.1 整体目录结构
整体目录结构如下(之后需要往该文件夹中逐渐增加一些文件):
1.1.1 datasets
文件夹datasets中包含train(训练集)和val(验证集),images中是jpg格式的原图,json是通过labelme打标得到的json文件。不会制作数据集的可以参考制作数据集
1.1.2 Abyssinian_label_map.pbtxt
name是类名,根据自己的情况修改,如果有更多的类往下增加即可。
item {
id: 1
name: '0.0'
}
item {
id: 2
name: '0.4'
}
item {
id: 3
name: '0.8'
}
item {
id: 4
name: '1.2'
}
item {
id: 5
name: '1.6'
}
item {
id: 6
name: 'point'
}
1.1.3 read_pbtxt_file.py
# -*- coding: utf-8 -*-
import tensorflow as tf
from google.protobuf import text_format
import string_int_label_map_pb2
def load_pbtxt_file(path):
"""Read .pbtxt file.
Args:
path: Path to StringIntLabelMap proto text file (.pbtxt file).
Returns:
A StringIntLabelMapProto.
Raises:
ValueError: If path is not exist.
"""
if not tf.gfile.Exists(path):
raise ValueError('`path` is not exist.')
with tf.gfile.GFile(path, 'r') as fid:
pbtxt_string = fid.read()
pbtxt = string_int_label_map_pb2.StringIntLabelMap()
try:
text_format.Merge(pbtxt_string, pbtxt)
except text_format.ParseError:
pbtxt.ParseFromString(pbtxt_string)
return pbtxt
def get_label_map_dict(path):
"""Reads a .pbtxt file and returns a dictionary.
Args:
path: Path to StringIntLabelMap proto text file.
Returns:
A dictionary mapping class names to indices.
"""
pbtxt = load_pbtxt_file(path)
result_dict = {}
for item in pbtxt.item:
result_dict[item.name] = item.id
return result_dict
1.1.4 create_tf_record.py
# -*- coding: utf-8 -*-
import cv2
import glob
import hashlib
import io
import json
import numpy as np
import os
import PIL.Image
import tensorflow as tf
import read_pbtxt_file
flags = tf.app.flags
flags.DEFINE_string('images_dir', None, 'Path to images directory.')
flags.DEFINE_string('annotations_json_dir', 'datasets/annotations',
'Path to annotations directory.')
flags.DEFINE_string('label_map_path', None, 'Path to label map proto.')
flags.DEFINE_string('output_path', None, 'Path to the output tfrecord.')
FLAGS = flags.FLAGS
def int64_feature(value):
return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))
def int64_list_feature(value):
return tf.train.Feature(int64_list=tf.train.Int64List(value=value))
def bytes_feature(value):
return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))
def bytes_list_feature(value):
return tf.train.Feature(bytes_list=tf.train.BytesList(value=value))
def float_list_feature(value):
return tf.train.Feature(float_list=tf.train.FloatList(value=value))
def create_tf_example(annotation_dict, label_map_dict=None):
"""Converts image and annotations to a tf.Example proto.
Args:
annotation_dict: A dictionary containing the following keys:
['height', 'width', 'filename', 'sha256_key', 'encoded_jpg',
'format', 'xmins', 'xmaxs', 'ymins', 'ymaxs', 'masks',
'class_names'].
label_map_dict: A dictionary maping class_names to indices.
Returns:
example: The converted tf.Example.
Raises:
ValueError: If label_map_dict is None or is not containing a class_name.
"""
if annotation_dict is None:
return