如何解决这个错误“tensorflow.python.framework.errors_impl.NotFoundError:”

问题描述 投票:0回答:1

我正在使用下面提供的代码在 pycharm 中将 csv 转换为 tfrecord,但我无法获取 tfrecord 文件。我收到这条消息:

“tensorflow.python.framework.errors_impl.NotFoundError: 无法创建 NewWriteableFile: : 系统找不到指定的文件。 ;没有这样的文件或目录”

感谢您的帮助。

from __future__ import division
from __future__ import print_function
from __future__ import absolute_import
import os
import io
import sys
import pandas as pd
import tensorflow as tf
import protos
from PIL import Image
from object_detection.utils import dataset_util
from collections import namedtuple, OrderedDict
from numpy import split

flagsMJ = tf.compat.v1.flags
flagsMJ.DEFINE_string('csv_input', '', 'C:/Users/Documents/Model A/MTrain_labels.csv')
flagsMJ.DEFINE_string('output_path', '',' C:/Users/Documents/Model A/Train.record')
flagsMJ.DEFINE_string('image_dir', '', 'C:/Users/Documents/Model A/Base')
FLAGS = flagsMJ.FLAGS

def class_text_to_int(row_label):
    if row_label == "MM":
        return 1
    elif row_label == "JJ":
         return 2
    else:
         row_label = None

 def slip(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.io.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(_):
    writer = tf.compat.v1.python_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()
python tensorflow deep-learning pycharm object-detection
1个回答
0
投票

错误

tensorflow.python.framework.errors_impl.NotFoundError
表示找不到指定的文件或目录。检查这些:

  1. 路径字符串:您已经定义了标志,但也提供了默认路径作为字符串参数。从字符串参数中删除路径。

    flagsMJ.DEFINE_string('csv_input', '', 'Path to CSV')
    

    而不是

    flagsMJ.DEFINE_string('csv_input', '', 'C:/Users/Documents/Model A/MTrain_labels.csv')
    
  2. 目录存在:验证目录

    'C:/Users/Documents/Model A/'
    'C:/Users/Documents/Model A/Base'
    确实存在。

  3. 文件存在:确认指定目录中存在

    MTrain_labels.csv

  4. 以管理员身份运行:有时,写入权限可能是一个问题。尝试以管理员身份运行 PyCharm。

  5. 标志用法:要设置标志,请像这样运行脚本:

    python your_script.py --csv_input="C:/path/to/csv" --output_path="C:/path/to/output" --image_dir="C:/path/to/images"
    
  6. 缩进:您的代码的缩进似乎已关闭。确保其一致以避免 Python 错误。

解决这些问题,你就可以开始了。

© www.soinside.com 2019 - 2024. All rights reserved.