我有一些github代码将两个.csv文件转换成.records,用于图像识别机器学习。
该github文件位于https://github.com/datitran/raccoon_dataset/blob/master/generate_tfrecord.py处的generate_tfrecord.py,我不得不对其进行一些修改,因为显然其中的某些代码不在它使用的库中。
作为参考,新代码为:
"""
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
和
generate_tfrecord.py --csv_input=images\train_labels.csv --image_dir=images\train --output_path=train.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
我该如何解决
tensorflow.python.framework.errors_impl.NotFoundError:无法创建NewWriteableFile ::系统找不到指定的路径。 ;没有这样的过程
错误?
为进一步参考,有人在遇到几乎相同的问题
tensorflow.python.framework.errors_impl.NotFoundError: Failed to create a NewWriteableFile:
提供的答案是
flags.DEFINE_string('csv_input','','CSV输入路径')flags.DEFINE_string('output_path','','输出TFRecord的路径) 用实际路径替换“ CSV输入的路径”和“输出TFRecord的路径”。
从MyPath开始,这是我从中运行generate_tfrecord.py的地方,我使用的csv文件(分别为test_labels.csv和train_labels.csv)位于名为“ images”的文件夹中,并且有两个文件夹分别名为images其中的“ train”和“ test”以及csv文件”。
我尝试了几种建议的更改路径的方法,例如:
'''
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.DEFINE_string('csv_input', '', '/images/train_labels.csv')
flags.DEFINE_string('output_path', '', '/images/train.record')
flags.DEFINE_string('image_dir', '', '/images/train')
但是它们无效。我尝试了正斜杠,反斜杠,不带引号,带“”引号,但没有任何效果。我也搜索了如何在标志中声明路径,这没有帮助。
我应该怎么做才能修复程序并获取.record文件?
答案 0 :(得分:0)
尝试将其更改为:
var config = new PdfGenerateConfig()
{
MarginBottom = 100,
MarginLeft = 20,
MarginRight = 20,
MarginTop = 100,
PageSize = PageSize.A4
};
PdfDocument pdf = PdfGenerator.GeneratePdf(html, config);