我使用tensorflow对象检测API中的代码生成了pascal voc 2007 tfrecords文件。我使用tf.contrib.data.DatasetAPI从tfrecords中读取数据。我在没有tf.contrib.data.DatasetAPI的情况下尝试了mehtod ,并且代码可以正常运行,但是更改为tf.contrib.data.DatasetAPI后无法正常工作。
没有tf.contrib.data.Dataset以下代码:
import tensorflow as tf
if __name__ == '__main__':
slim_example_decoder = tf.contrib.slim.tfexample_decoder
features = {"image/height": tf.FixedLenFeature((), tf.int64, default_value=1),
"image/width": tf.FixedLenFeature((), tf.int64, default_value=1),
"image/filename": tf.FixedLenFeature((), tf.string, default_value=""),
"image/source_id": tf.FixedLenFeature((), tf.string, default_value=""),
"image/key/sha256": tf.FixedLenFeature((), tf.string, default_value=""),
"image/encoded": tf.FixedLenFeature((), tf.string, default_value=""),
"image/format": tf.FixedLenFeature((), tf.string, default_value="jpeg"),
"image/object/bbox/xmin": tf.VarLenFeature(tf.float32),
"image/object/bbox/xmax": tf.VarLenFeature(tf.float32),
"image/object/bbox/ymin": tf.VarLenFeature(tf.float32),
"image/object/bbox/ymax": tf.VarLenFeature(tf.float32),
"image/object/class/text": tf.VarLenFeature(tf.string),
"image/object/class/label": tf.VarLenFeature(tf.int64),
"image/object/difficult": tf.VarLenFeature(tf.int64),
"image/object/truncated": tf.VarLenFeature(tf.int64),
"image/object/view": tf.VarLenFeature(tf.int64)}
items_to_handlers = {
'image': slim_example_decoder.Image(
image_key='image/encoded', …Run Code Online (Sandbox Code Playgroud) 我正在使用以下代码生成 tfrecords 文件。
def generate_tfrecords(data_path, labels, name):
"""Converts a dataset to tfrecords."""
filename = os.path.join(args.tfrecords_path, name + '.tfrecords')
writer = tf.python_io.TFRecordWriter(filename)
for index, data in enumerate(data_path):
with tf.gfile.GFile(data, 'rb') as fid:
encoded_jpg = fid.read()
print(len(encoded_jpg)) # 17904
encoded_jpg_io = io.BytesIO(encoded_jpg)
image = pil.open(encoded_jpg_io)
image = np.asarray(image)
print(image.shape) # 112*112*3
example = tf.train.Example(features=tf.train.Features(feature={
'height': _int64_feature(int(image.shape[0])),
'width': _int64_feature(int(image.shape[1])),
'depth': _int64_feature(int(3)),
'label': _int64_feature(int(labels[index])),
'image_raw': _bytes_feature(encoded_jpg)}))
writer.write(example.SerializeToString())
writer.close()
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,encoded_jpg有长度17904,图像有112*112*3不一致的形状。
当我使用以下代码解析 tfrecords 时:
def _parse_function(example_proto):
features = {'height': tf.FixedLenFeature((), …Run Code Online (Sandbox Code Playgroud)