Я пытаюсь тренировать модель ssd с моим собственным набором данных. Я создаю файл записи с этой измененной программой:
# Copyright 2017 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
r"""Convert raw PASCAL dataset to TFRecord for object_detection.
Example usage:
python object_detection/dataset_tools/create_pascal_tf_record.py \
--data_dir=/home/user/VOCdevkit \
--year=VOC2012 \
--output_path=/home/user/pascal.record
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import hashlib
import io
import logging
import os
from lxml import etree
import PIL.Image
import tensorflow as tf
from object_detection.utils import dataset_util
from object_detection.utils import label_map_util
flags = tf.app.flags
flags.DEFINE_string('data_dir', '', 'Root directory to raw PASCAL VOC dataset.')
flags.DEFINE_string('set', 'train', 'Convert training set, validation set or '
'merged set.')
flags.DEFINE_string('annotations_dir', 'Annotations',
'(Relative) path to annotations directory.')
flags.DEFINE_string('year', 'VOC2007', 'Desired challenge year.')
flags.DEFINE_string('output_path', '', 'Path to output TFRecord')
flags.DEFINE_string('label_map_path', 'data/pascal_label_map.pbtxt',
'Path to label map proto')
flags.DEFINE_boolean('ignore_difficult_instances', False, 'Whether to ignore '
'difficult instances')
FLAGS = flags.FLAGS
SETS = ['train', 'val', 'trainval', 'test']
YEARS = ['VOC2007', 'VOC2012', 'merged']
def dict_to_tf_example(data,
dataset_directory,
label_map_dict,
ignore_difficult_instances=False,
image_subdirectory='JPEGImages'):
"""Convert XML derived dict to tf.Example proto.
Notice that this function normalizes the bounding box coordinates provided
by the raw data.
Args:
data: dict holding PASCAL XML fields for a single image (obtained by
running dataset_util.recursive_parse_xml_to_dict)
dataset_directory: Path to root directory holding PASCAL dataset
label_map_dict: A map from string label names to integers ids.
ignore_difficult_instances: Whether to skip difficult instances in the
dataset (default: False).
image_subdirectory: String specifying subdirectory within the
PASCAL dataset directory holding the actual image data.
Returns:
example: The converted tf.Example.
Raises:
ValueError: if the image pointed to by data['filename'] is not a valid JPEG
"""
print(" ---- {0} \n {1} \n {2}".format(data['folder'], image_subdirectory, data['filename']))
img_path = os.path.join(data['folder'], image_subdirectory, data['filename'])
full_path = os.path.join(dataset_directory, img_path)
with tf.gfile.GFile(full_path, 'rb') as fid:
encoded_jpg = fid.read()
encoded_jpg_io = io.BytesIO(encoded_jpg)
image = PIL.Image.open(encoded_jpg_io)
if image.format != 'JPEG':
raise ValueError('Image format not JPEG')
key = hashlib.sha256(encoded_jpg).hexdigest()
width = int(data['size']['width'])
height = int(data['size']['height'])
xmin = []
ymin = []
xmax = []
ymax = []
classes = []
classes_text = []
truncated = []
poses = []
difficult_obj = []
image_format = b'jpg'
if 'object' in data:
for obj in data['object']:
difficult = bool(int(obj['difficult']))
if ignore_difficult_instances and difficult:
continue
difficult_obj.append(int(difficult))
xmin.append(float(obj['bndbox']['xmin']) / width)
ymin.append(float(obj['bndbox']['ymin']) / height)
xmax.append(float(obj['bndbox']['xmax']) / width)
ymax.append(float(obj['bndbox']['ymax']) / height)
classes_text.append(obj['name'].encode('utf8'))
classes.append(label_map_dict[obj['name']])
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(data['filename'].encode('utf8')),
'image/source_id': dataset_util.bytes_feature(data['filename'].encode('utf8')),
'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(xmin),
'image/object/bbox/xmax': dataset_util.float_list_feature(xmax),
'image/object/bbox/ymin': dataset_util.float_list_feature(ymin),
'image/object/bbox/ymax': dataset_util.float_list_feature(ymax),
'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 main(_):
if FLAGS.set not in SETS:
raise ValueError('set must be in : {}'.format(SETS))
if FLAGS.year not in YEARS:
raise ValueError('year must be in : {}'.format(YEARS))
data_dir = FLAGS.data_dir
years = ['VOC2007', 'VOC2012']
if FLAGS.year != 'merged':
years = [FLAGS.year]
writer = tf.python_io.TFRecordWriter(FLAGS.output_path)
label_map_dict = label_map_util.get_label_map_dict(FLAGS.label_map_path)
examples_path = os.path.join(data_dir, 'ImageSets', 'Main',
'default.txt')
annotations_dir = os.path.join(data_dir, FLAGS.annotations_dir)
examples_list = dataset_util.read_examples_list(examples_path)
for idx, example in enumerate(examples_list):
if idx % 100 == 0:
logging.info('On image %d of %d', idx, len(examples_list))
path = os.path.join(annotations_dir, example + '.xml')
if not os.path.exists(path) :
continue
with tf.gfile.GFile(path, 'r') as fid:
xml_str = fid.read()
xml = etree.fromstring(xml_str)
data = dataset_util.recursive_parse_xml_to_dict(xml)['annotation']
tf_example = dict_to_tf_example(data, FLAGS.data_dir, label_map_dict,
FLAGS.ignore_difficult_instances)
writer.write(tf_example.SerializeToString())
writer.close()
if __name__ == '__main__':
tf.app.run()
Когда я запускаю модель со своей сгенерированной tfrecord. Я получаю эту ошибку:
/home/xavier/dev/models/research/object_detection/utils/label_map_util.py:144: RuntimeWarning: Unexpected end-group tag: Not all data was converted
label_map.ParseFromString(label_map_string)
Traceback (most recent call last):
File "object_detection/model_main.py", line 109, in <module>
tf.app.run()
File "/home/xavier/.local/lib/python2.7/site-packages/tensorflow/python/platform/app.py", line 40, in run
_run(main=main, argv=argv, flags_parser=_parse_flags_tolerate_undef)
File "/home/xavier/.local/lib/python2.7/site-packages/absl/app.py", line 299, in run
_run_main(main, args)
File "/home/xavier/.local/lib/python2.7/site-packages/absl/app.py", line 250, in _run_main
sys.exit(main(argv))
File "object_detection/model_main.py", line 105, in main
tf.estimator.train_and_evaluate(estimator, train_spec, eval_specs[0])
File "/home/xavier/.local/lib/python2.7/site-packages/tensorflow_estimator/python/estimator/training.py", line 473, in train_and_evaluate
return executor.run()
File "/home/xavier/.local/lib/python2.7/site-packages/tensorflow_estimator/python/estimator/training.py", line 613, in run
return self.run_local()
File "/home/xavier/.local/lib/python2.7/site-packages/tensorflow_estimator/python/estimator/training.py", line 714, in run_local
saving_listeners=saving_listeners)
File "/home/xavier/.local/lib/python2.7/site-packages/tensorflow_estimator/python/estimator/estimator.py", line 367, in train
loss = self._train_model(input_fn, hooks, saving_listeners)
File "/home/xavier/.local/lib/python2.7/site-packages/tensorflow_estimator/python/estimator/estimator.py", line 1158, in _train_model
return self._train_model_default(input_fn, hooks, saving_listeners)
File "/home/xavier/.local/lib/python2.7/site-packages/tensorflow_estimator/python/estimator/estimator.py", line 1192, in _train_model_default
saving_listeners)
File "/home/xavier/.local/lib/python2.7/site-packages/tensorflow_estimator/python/estimator/estimator.py", line 1485, in _train_with_estimator_spec
any_step_done = True
File "/home/xavier/.local/lib/python2.7/site-packages/tensorflow/python/training/monitored_session.py", line 854, in __exit__
self._close_internal(exception_type)
File "/home/xavier/.local/lib/python2.7/site-packages/tensorflow/python/training/monitored_session.py", line 887, in _close_internal
h.end(self._coordinated_creator.tf_sess)
File "/home/xavier/.local/lib/python2.7/site-packages/tensorflow/python/training/basic_session_run_hooks.py", line 600, in end
self._save(session, last_step)
File "/home/xavier/.local/lib/python2.7/site-packages/tensorflow/python/training/basic_session_run_hooks.py", line 619, in _save
if l.after_save(session, step):
File "/home/xavier/.local/lib/python2.7/site-packages/tensorflow_estimator/python/estimator/training.py", line 519, in after_save
self._evaluate(global_step_value) # updates self.eval_result
File "/home/xavier/.local/lib/python2.7/site-packages/tensorflow_estimator/python/estimator/training.py", line 539, in _evaluate
self._evaluator.evaluate_and_export())
File "/home/xavier/.local/lib/python2.7/site-packages/tensorflow_estimator/python/estimator/training.py", line 920, in evaluate_and_export
hooks=self._eval_spec.hooks)
File "/home/xavier/.local/lib/python2.7/site-packages/tensorflow_estimator/python/estimator/estimator.py", line 477, in evaluate
name=name)
File "/home/xavier/.local/lib/python2.7/site-packages/tensorflow_estimator/python/estimator/estimator.py", line 519, in _actual_eval
return _evaluate()
File "/home/xavier/.local/lib/python2.7/site-packages/tensorflow_estimator/python/estimator/estimator.py", line 501, in _evaluate
self._evaluate_build_graph(input_fn, hooks, checkpoint_path))
File "/home/xavier/.local/lib/python2.7/site-packages/tensorflow_estimator/python/estimator/estimator.py", line 1501, in _evaluate_build_graph
self._call_model_fn_eval(input_fn, self.config))
File "/home/xavier/.local/lib/python2.7/site-packages/tensorflow_estimator/python/estimator/estimator.py", line 1534, in _call_model_fn_eval
input_fn, ModeKeys.EVAL)
File "/home/xavier/.local/lib/python2.7/site-packages/tensorflow_estimator/python/estimator/estimator.py", line 1022, in _get_features_and_labels_from_input_fn
self._call_input_fn(input_fn, mode))
File "/home/xavier/.local/lib/python2.7/site-packages/tensorflow_estimator/python/estimator/estimator.py", line 1113, in _call_input_fn
return input_fn(**kwargs)
File "/home/xavier/dev/models/research/object_detection/inputs.py", line 625, in _eval_input_fn
params=params)
File "/home/xavier/dev/models/research/object_detection/inputs.py", line 725, in eval_input
transform_input_data_fn=transform_and_pad_input_data_fn)
File "/home/xavier/dev/models/research/object_detection/builders/dataset_builder.py", line 155, in build
dataset = data_map_fn(process_fn, num_parallel_calls=num_parallel_calls)
File "/home/xavier/.local/lib/python2.7/site-packages/tensorflow/python/util/deprecation.py", line 324, in new_func
return func(*args, **kwargs)
File "/home/xavier/.local/lib/python2.7/site-packages/tensorflow/python/data/ops/dataset_ops.py", line 1813, in map_with_legacy_function
use_legacy_function=True))
File "/home/xavier/.local/lib/python2.7/site-packages/tensorflow/python/data/ops/dataset_ops.py", line 3228, in __init__
use_legacy_function=use_legacy_function)
File "/home/xavier/.local/lib/python2.7/site-packages/tensorflow/python/data/ops/dataset_ops.py", line 2526, in __init__
self._function.add_to_graph(ops.get_default_graph())
File "/home/xavier/.local/lib/python2.7/site-packages/tensorflow/python/framework/function.py", line 516, in add_to_graph
self._create_definition_if_needed()
File "/home/xavier/.local/lib/python2.7/site-packages/tensorflow/python/framework/function.py", line 348, in _create_definition_if_needed
self._create_definition_if_needed_impl()
File "/home/xavier/.local/lib/python2.7/site-packages/tensorflow/python/framework/function.py", line 379, in _create_definition_if_needed_impl
capture_resource_var_by_value=self._capture_resource_var_by_value)
File "/home/xavier/.local/lib/python2.7/site-packages/tensorflow/python/framework/function.py", line 915, in func_graph_from_py_func
outputs = func(*func_graph.inputs)
File "/home/xavier/.local/lib/python2.7/site-packages/tensorflow/python/data/ops/dataset_ops.py", line 2518, in wrapper_fn
ret = _wrapper_helper(*args)
File "/home/xavier/.local/lib/python2.7/site-packages/tensorflow/python/data/ops/dataset_ops.py", line 2489, in _wrapper_helper
ret = func(*nested_args)
File "/home/xavier/dev/models/research/object_detection/builders/dataset_builder.py", line 134, in process_fn
processed_tensors = decoder.decode(value)
File "/home/xavier/dev/models/research/object_detection/data_decoders/tf_example_decoder.py", line 388, in decode
tensors = decoder.decode(serialized_example, items=keys)
File "/home/xavier/.local/lib/python2.7/site-packages/tensorflow/contrib/slim/python/slim/data/tfexample_decoder.py", line 526, in decode
outputs.append(handler.tensors_to_item(keys_to_tensors))
File "/home/xavier/dev/models/research/object_detection/data_decoders/tf_example_decoder.py", line 129, in tensors_to_item
item = self._handler.tensors_to_item(keys_to_tensors)
File "/home/xavier/dev/models/research/object_detection/data_decoders/tf_example_decoder.py", line 98, in tensors_to_item
return tf.maximum(self._name_to_id_table.lookup(unmapped_tensor),
File "/home/xavier/.local/lib/python2.7/site-packages/tensorflow/python/ops/lookup_ops.py", line 217, in lookup
(self._key_dtype, keys.dtype))
TypeError: Signature mismatch. Keys must be dtype <dtype: 'float32'>, got <dtype: 'string'>.
Я почти уверен, что она исходит из моего файла tfrecord. Как я могу проверить надежность моей записи? В чем мои ошибки?
Я также пытаюсь изменить tf_example_decoder.py, чтобы найти, где это может произойти. Я пытаюсь локально с tfrecord найти там: https://github.com/datitran/raccoon_dataset/tree/master/data