问题从Tensorflow创建我自己的对象检测教程数据集

时间:2018-05-31 08:25:37

标签: python tensorflow jupyter-notebook object-detection

我试图从tensorflow为对象检测教程构建自己的数据集,我的代码在这里:

import tensorflow as tf
import os
from PIL import Image
from resizeimage import resizeimage
from object_detection.utils import dataset_util

flags = tf.app.flags
flags.DEFINE_string('output_path', '', 'Path to output TFRecord')
FLAGS = flags.FLAGS

def create_tf_example(label_and_data_info):
    # TODO START: Populate the following variables from your example.
    height = 200 # Image height
    width = 200 # Image width
    filename = 'asc.0.jpg' # Filename of the image. Empty if image is not from file
    encoded_image_data = None # Encoded image bytes
    image_format = b'jpg' # 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 = ['Ascaris'] # List of string class name of bounding box (1 per box)
    classes = [1] # List of integer class id of bounding box (1 per box)
    # TODO END
    tf_label_and_data = 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_label_and_data

def main(_):
    writer = tf.python_io.TFRecordWriter(FLAGS.output_path)

    # TODO START: Write code to read in your dataset to examples variable
    file_loc = 'NematodeConvImagesResimensionadas/train_images'
    all_data_and_label_info = LOAD(file_loc)
    # TODO END

    for data_and_label_info in all_data_and_label_info:
        tf_example = create_tf_example(data_and_label_info)
        writer.write(tf_example.SerializeToString())

    writer.close()

if __name__ == '__main__':
    tf.app.run()

但是当我尝试运行脚本时,会出现以下错误:

NotFoundError                             Traceback (most recent call last)
<ipython-input-4-658e1dd88560> in <module>()
      14 
      15 if __name__ == '__main__':
 ---> 16     tf.app.run()
/usr/local/lib/python3.5/dist-packages/tensorflow/python/platform/app.py in run(main, argv)
      122   # Call the main function, passing through any arguments
      123   # to the final program.
 ---> 124   _sys.exit(main(argv))
      125 
      126 
<ipython-input-4-658e1dd88560> in main(_)
      1 def main(_):
----> 2     writer = tf.python_io.TFRecordWriter(FLAGS.output_path)
      3 
      4     # TODO START: Write code to read in your dataset to examples variable
      5     file_loc = 'NematodeConvImagesResimensionadas/train_images'
/usr/local/lib/python3.5/dist-packages/tensorflow/python/lib/io/tf_record.py in __init__(self, path, options)
      104     with errors.raise_exception_on_not_ok_status() as status:
      105       self._writer = pywrap_tensorflow.PyRecordWriter_New(
  --> 106           compat.as_bytes(path), compat.as_bytes(compression_type), status)
      107 
      108   def __enter__(self):
/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/errors_impl.py in __exit__(self, type_arg, value_arg, traceback_arg)
      471             None, None,
      472             compat.as_text(c_api.TF_Message(self.status.status)),
  --> 473             c_api.TF_GetCode(self.status.status))
      474     # Delete the underlying status object from memory otherwise it stays alive
      475     # as there is a reference to status from this from the traceback due to

NotFoundError: ; No such file or directory

我不知道如何解决这个问题,我试图改变图像的路径,但它似乎找不到目录。任何帮助都会非常感激。

1 个答案:

答案 0 :(得分:0)

flags.DEFINE_string中的第二个参数(&#39; output_path&#39;,&#39;&#39;,&#39;输出TFRecord的路径&#39;)应指向您的输出文件夹机。你把它留空了。