我正在使用tensorflow对象检测api创建用于微调任务的数据集 .
我的目录结构是:
培养/
- imgs /
---- img1.jpg
- 安/
---- img1.csv
csv,每个图像一个, label, x, y, w, h
我用这个脚本来保存tfrecord:
import tensorflow as tf
from os import listdir
import os
from os.path import isfile, join
import csv
import json
from object_detection.utils import dataset_util
flags = tf.app.flags
flags.DEFINE_string('output_path', '', 'Path to output TFRecord')
FLAGS = flags.FLAGS
LABEL_DICT = {}
counter = 0
def create_tf_example(example):
# TODO(user): Populate the following variables from your example.
height = 404 # Image height
width = 720 # Image width
filename = example['path'].encode('utf-8').strip() # Filename of the image. Empty if image is not from file
with tf.gfile.GFile(example['path'], 'rb') as fid:
encoded_image_data = fid.read()
image_format = 'jpeg'.encode('utf-8').strip() # b'jpeg' or b'png'
xmins = [] # List of normalized left x coordinates in bounding box (1 per box)
xmaxs = [] # List of normalized right x coordinates in bounding box
# (1 per box)
ymins = [] # List of normalized top y coordinates in bounding box (1 per box)
ymaxs = [] # List of normalized bottom y coordinates in bounding box
# (1 per box)
classes_text = [] # List of string class name of bounding box (1 per box)
classes = [] # List of integer class id of bounding box (1 per box)
for box in example['boxes']:
#if box['occluded'] is False:
#print("adding box")
xmins.append(float(int(box['x']) / width))
xmaxs.append(float(int(box['w']) + int(box['x']) / width))
ymins.append(float(int(box['y']) / height))
ymaxs.append(float(int(box['h']) + int(box['y']) / height))
classes_text.append(box['label'].encode('utf-8'))
classes.append(int(LABEL_DICT[box['label']]))
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_image_data),
'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_example
def ex_info(img_path, ann_path):
boxes = []
head = ['label','x','y','w','h']
with open(ann_path, 'r') as csvfile:
annreader = csv.DictReader(csvfile, fieldnames=head)
for box in annreader:
boxes.append(box)
LABEL_DICT[box['label']] = LABEL_DICT.get(box['label'], len(LABEL_DICT) + 1)
ex = {
"path" : img_path,
"boxes" : boxes
}
return ex
def main(_):
writer = tf.python_io.TFRecordWriter(FLAGS.output_path)
# TODO(user): Write code to read in your dataset to examples variable
dataset_dir = "train"
ann_dir = join(dataset_dir, "ann")
imgs_dir = join(dataset_dir, "imgs")
labelDest = "tfTrain/data/labels_map.pbtxt"
imgs = [join(imgs_dir, f) for f in listdir(imgs_dir) if isfile(join(imgs_dir, f))]
anns = [join(ann_dir, os.path.basename(im).replace("jpg","csv")) for im in imgs]
for img,ann in zip(imgs,anns):
example = ex_info(img,ann)
#tf_example = create_tf_example(example)
#writer.write(tf_example.SerializeToString())
with open(labelDest, 'w', encoding='utf-8') as outL:
for name,key in LABEL_DICT.items():
outL.write("item { \n id: " + str(key) + "\n name: '" + name + "'\n}\n")
writer.close()
if __name__ == '__main__':
tf.app.run()
但是当我运行火车脚本时,我收到了这个错误
python train.py --logtostderr --train_dir = . / models / train --pipeline_config_path = faster_rcnn_resnet101_coc o.config警告:tensorflow:来自models / research / object_detection / trainer.py:257:create_global_step(来自tensorflow.contrib.framewo) rk.python.ops.variables)已弃用,将在以后的版本中删除 . 更新说明:请切换到tf.train.create_global_step Traceback(最近一次调用最后一次):文件“models / research / object_detection / utils / label_map_util.py”,第135行,在load_labelmap中text_format.Merge(label_map_string,label_map)文件“ /home/user/anaconda3/envs/tf/lib/python3.6/site-packages/google/protobuf/text_format.py“,第525行,在Merge descriptor_pool = descriptor_pool中)文件”/ home / user / anaconda3 / envs /在MergeLines中返回解析器.MergeLines(行,消息)文件“/ home / user / anaconda3 / envs / tf / lib /” python3.6 / site-packages / google / protobuf / text_format.py“,第612行,MergeLines中的self._ParseOrMerge(行,消息)文件”/home/user/anaconda3/envs/tf/lib/python3.6/site -packages / google / protobuf / text_format.py“,第627行,在_ParseOrMerge self._MergeField(tokenizer,message)文件”/home/user/anaconda3/envs/tf/lib/python3.6/site-packages/google/ protobuf / text_format.py“,第727行,在_MergeField合并中(到kenizer,message,field)在_MergeMessageField self._MergeField(tokenizer,sub_message)中输入文件“/home/user/anaconda3/envs/tf/lib/python3.6/site-packages/google/protobuf/text_format.py”,第815行)文件“/home/user/anaconda3/envs/tf/lib/python3.6/site-packages/google/protobuf/text_format.py”,第695行,在_MergeField(message_descriptor.full_name,name)中)google.protobuf . text_format.ParseError:23:20:消息类型“object_detection.protos.StringIntLabelMapItem”没有名为“s”的字段 . 在处理上述异常期间,发生了另一个异常:Traceback(最近一次调用last):
在<module>中的文件“train.py”,第184行
tf.app.run()
文件“/home/user/anaconda3/envs/tf/lib/python3.6/site-packages/tensorflow/python/platform/app.py”,
第126行,在run _sys.exit(main(argv))文件“train.py”,第180行,在主graph_hook_fn = graph_rewriter_fn中)文件“models / research / object_detection / trainer.py”,第264行,在train train_config中 . prefetch_queue_capacity,data_augmentation_options)文件“models / research / object_detection / trainer.py”,第59行,在create_input_queue tensor_dict = create_tensor_dict_fn()文件“train.py”,第121行,在get_next dataset_builder.build(config)中 . )get_next()文件“models / research / object_detection / builders / dataset_builder.py”,第155行,在构建label_map_proto_file = label_map_proto_file)文件“models / research / object_detection / data_decoders / tf_example_decoder.py”,第245行,在init use_display_name中)文件“models / research / object_detection / utils / label_map_util.py“,第152行,在get_label_map_dict中label_map = load_labelmap(label_map_path)文件”models / research / object_detection / utils / label_map_util.py“,第137行,在load_labelmap中label_map.ParseFromString(label_map_string)TypeError:一个类似字节的对象请问,不是'str'我不明白是什么问题 . 在tfrecord?在labels.pbtxt中?还是在配置文件中?
1 回答
好的,我刚刚解决了调试张量流问题 . 虽然采用utf-8格式,但我的标签很难被张量流读取,因为有一些奇怪的字符,如&ùà . 从csv中删除让火车开始