我对 Python 很陌生,并尝试启动脚本
convert_to_tfrecord.py
(神经网络;它应该使用一些库训练图像数据集......)
说明:
Now you’re ready to run the TFRecord script. Run the command below from the tensorflow/models/research directory, and pass it the following flags (run it twice: once for training data, once for test data):
python convert_labels_to_tfrecords.py \
--output_path=train.record \
--images_dir=path/to/your/training/images/ \
--labels_dir=path/to/training/label/xml/
为了适合我的 OS X,我通过 python3 运行了这个脚本...更改了脚本的名称...并设置了一个目录... 我位于我的脚本所在的目录;我的文件夹在哪里;我的图书馆在哪里。 所以就我而言:
python3 convert_to_tfrecord.py \
--output_path=train.record \
--images_dir=ENFj/ \
--labels_dir=ENFj/xml/
结果:
Oleksandrs-MacBook-Air:research jaskier$ python3 convert_to_tfrecord.py \
> --output_path=train.record \
Traceback (most recent call last):
File "convert_to_tfrecord.py", line 89, in <module>
tf.app.run()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/platform/app.py", line 126, in run
_sys.exit(main(argv))
File "convert_to_tfrecord.py", line 81, in main
for filename in os.listdir(FLAGS.images_dir):
FileNotFoundError: [Errno 2] No such file or directory: ''
Oleksandrs-MacBook-Air:research jaskier$ --images_dir=ENFj/ \
> --labels_dir=ENFj/xml/
“convert_to_tfrecord”代码:
import os
import io
import xml.etree.ElementTree as ET
import tensorflow as tf
from object_detection.utils import dataset_util
from PIL import Image
flags = tf.app.flags
flags.DEFINE_string('output_path', '', 'Path to output TFRecord')
flags.DEFINE_string('images_dir', '', 'Path to directory of images')
flags.DEFINE_string('labels_dir', '', 'Path to directory of labels')
FLAGS = flags.FLAGS
def create_tf_example(example):
image_path = os.getcwd() + '/' + FLAGS.images_dir + example
labels_path = os.getcwd() + '/' + FLAGS.labels_dir + os.path.splitext(example)[0] + '.xml'
# Read the image
img = Image.open(image_path)
width, height = img.size
img_bytes = io.BytesIO()
img.save(img_bytes, format=img.format)
height = height
width = width
encoded_image_data = img_bytes.getvalue()
image_format = img.format.encode('utf-8')
# Read the label XML
tree = ET.parse(labels_path)
root = tree.getroot()
xmins = xmaxs = ymins = ymaxs = list()
for coordinate in root.find('object').iter('bndbox'):
xmins = [int(coordinate.find('xmin').text)]
xmaxs = [int(coordinate.find('xmax').text)]
ymins = [int(coordinate.find('ymin').text)]
ymaxs = [int(coordinate.find('ymax').text)]
classes_text = ['tswift'.encode('utf-8')]
classes = [1]
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(encoded_image_data),
'image/source_id': dataset_util.bytes_feature(encoded_image_data),
'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 main(_):
writer = tf.python_io.TFRecordWriter(FLAGS.output_path)
for filename in os.listdir(FLAGS.images_dir):
tf_example = create_tf_example(filename)
writer.write(tf_example.SerializeToString())
writer.close()
if __name__ == '__main__':
tf.app.run()
尝试了不同的东西: 1.更新请求(一行+删除“\”,因为正如下面有人提到的,这是页面的PHP解释器的错误......它使用了\n并且忘记隐藏输出文本):
python3 convert_to_tfrecord.py
--output_path=train.record
--images_dir=ENFj
--labels_dir=ENFj/xml
2.“查找.-name“.DS_Store”-删除” 3.再试一次:
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/PIL/Image.py", line 2543, in open fp = builtins.open(filename, "rb") IsADirectoryError: [Errno 21] Is a directory: '/Users/jaskier/Downloads/models/research/ENFj/xml'
最佳答案
该代码被设置为将 --images_dir
中的所有文件解释为某些图像处理机器。这意味着 --images_dir
中的任何非图像资源都会导致脚本中断。
一种解决方案是确保 --images_dir
仅包含图像文件(即确保该目录不包含 XML 文件或以 . 开头的文件,例如 .git
或 .DS_Store
。
另一个解决方案是修改源代码本身,使其仅适用于图像文件。可以使用这样的东西:
import glob
# only match jpg files in the images_dir
for filename in glob.glob(FLAGS.images_dir + '/*.jpg'):
tf_example = create_tf_example(filename)
# copy the other lines here as needed
关于python - 苹果系统; Python3; FileNotFoundError : [Errno 2] No such file or directory: '' . 如何修复?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49477633/