Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python未能创建新的可写文件_Python_Tensorflow_Object Detection - Fatal编程技术网

Python未能创建新的可写文件

Python未能创建新的可写文件,python,tensorflow,object-detection,Python,Tensorflow,Object Detection,我有一些github代码将两个.csv文件转换成.records,用于图像识别机器学习 github文件是generate_tfrecord.py at,我必须对它进行一些修改,因为它使用的库中显然不存在其中的一些代码 供参考,新代码为: """ Usage: # From tensorflow/models/ # Create train data: python generate_tfrecord.py --csv_input=data/train_labels.csv --o

我有一些github代码将两个.csv文件转换成.records,用于图像识别机器学习

github文件是generate_tfrecord.py at,我必须对它进行一些修改,因为它使用的库中显然不存在其中的一些代码

供参考,新代码为:

"""
Usage:
  # From tensorflow/models/
  # Create train data:
  python generate_tfrecord.py --csv_input=data/train_labels.csv  --output_path=train.record

  # Create test data:
  python generate_tfrecord.py --csv_input=data/test_labels.csv  --output_path=test.record
"""
from __future__ import division
from __future__ import print_function
from __future__ import absolute_import

import os
import io
import pandas as pd
import tensorflow as tf

from PIL import Image
from object_detection.utils import dataset_util
from collections import namedtuple, OrderedDict

# tf.compat.v1.flags used to be tf.app.flags, but it apparently didn't exist, and like this it works better (aka. doesn't give that error)
flags = tf.compat.v1.flags
flags.DEFINE_string('csv_input', '', 'Path to the CSV input')
flags.DEFINE_string('output_path', '', 'Path to output TFRecord')
flags.DEFINE_string('image_dir', '', 'Path to images')
FLAGS = flags.FLAGS


# TO-DO replace this with label map
def class_text_to_int(row_label):
    if row_label == 'Car_8':
        return 1
    else:
        None


def split(df, group):
    data = namedtuple('data', ['filename', 'object'])
    gb = df.groupby(group)
    return [data(filename, gb.get_group(x)) for filename, x in zip(gb.groups.keys(), gb.groups)]


def create_tf_example(group, path):
    with tf.gfile.GFile(os.path.join(path, '{}'.format(group.filename)), 'rb') as fid:
        encoded_jpg = fid.read()
    encoded_jpg_io = io.BytesIO(encoded_jpg)
    image = Image.open(encoded_jpg_io)
    width, height = image.size

    filename = group.filename.encode('utf8')
    image_format = b'jpg'
    xmins = []
    xmaxs = []
    ymins = []
    ymaxs = []
    classes_text = []
    classes = []

    for index, row in group.object.iterrows():
        xmins.append(row['xmin'] / width)
        xmaxs.append(row['xmax'] / width)
        ymins.append(row['ymin'] / height)
        ymaxs.append(row['ymax'] / height)
        classes_text.append(row['class'].encode('utf8'))
        classes.append(class_text_to_int(row['class']))

    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_jpg),
        '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(_):
    #io used to be python_io, but it apparently didn't exist, and like this it works better (aka. doesn't give that error)
    writer = tf.io.TFRecordWriter(FLAGS.output_path)
    path = os.path.join(FLAGS.image_dir)
    examples = pd.read_csv(FLAGS.csv_input)
    grouped = split(examples, 'filename')
    for group in grouped:
        tf_example = create_tf_example(group, path)
        writer.write(tf_example.SerializeToString())

    writer.close()
    output_path = os.path.join(os.getcwd(), FLAGS.output_path)
    print('Successfully created the TFRecords: {}'.format(output_path))


if __name__ == '__main__':
    # tf.compat.v1.app.run() used to be tf.app.run(), but it apparently didn't exist, and like this it works better (aka. doesn't give that error)
    tf.compat.v1.app.run()
运行时,按照命令提示符中的指定,使用

generate_tfrecord.py --csv_input=images\test_labels.csv --image_dir=images\test --output_path=test.record

我分别得到以下错误:

C:\My_Path>generate_tfrecord.py --csv_input=images\train_labels.csv --image_dir=images\train --output_path=train.record
Traceback (most recent call last):
  File "C:\My_Path\generate_tfrecord.py", line 103, in <module>
    tf.compat.v1.app.run()
  File "C:\Python37\lib\site-packages\tensorflow_core\python\platform\app.py", line 40, in run
    _run(main=main, argv=argv, flags_parser=_parse_flags_tolerate_undef)
  File "C:\Python37\lib\site-packages\absl\app.py", line 299, in run
    _run_main(main, args)
  File "C:\Python37\lib\site-packages\absl\app.py", line 250, in _run_main
    sys.exit(main(argv))
  File "C:\My_Path\generate_tfrecord.py", line 88, in main
    writer = tf.io.TFRecordWriter(FLAGS.output_path)
  File "C:\Python37\lib\site-packages\tensorflow_core\python\lib\io\tf_record.py", line 218, in __init__
    compat.as_bytes(path), options._as_record_writer_options(), status)
  File "C:\Python37\lib\site-packages\tensorflow_core\python\framework\errors_impl.py", line 556, in __exit__
    c_api.TF_GetCode(self.status.status))
tensorflow.python.framework.errors_impl.NotFoundError: Failed to create a NewWriteableFile:  : The system cannot find the path specified.
; No such process
但它们是无效的。我试过前斜杠,后斜杠,没有引号,有“”引号,都没用。我还搜索了如何在标志中声明路径,没有帮助

如何修复程序并获取.record文件?

尝试将其更改为:

flags.DEFINE_string('csv_input', 'C:\\yourfolder\\train_labels.csv', 'Path to the CSV input')
flags.DEFINE_string('output_path', 'C:\\yourfolder\\train.record', 'Path to output TFRecord')
flags.DEFINE_string('image_dir', 'C:\\yourfolder\\images\\train', 'Path to images')
尝试将其更改为:

flags.DEFINE_string('csv_input', 'C:\\yourfolder\\train_labels.csv', 'Path to the CSV input')
flags.DEFINE_string('output_path', 'C:\\yourfolder\\train.record', 'Path to output TFRecord')
flags.DEFINE_string('image_dir', 'C:\\yourfolder\\images\\train', 'Path to images')
flags.DEFINE_string('csv_input', 'C:\\yourfolder\\train_labels.csv', 'Path to the CSV input')
flags.DEFINE_string('output_path', 'C:\\yourfolder\\train.record', 'Path to output TFRecord')
flags.DEFINE_string('image_dir', 'C:\\yourfolder\\images\\train', 'Path to images')