【问题标题】:Trying to generate TFrecord but I,m getting this error "FileNotFoundError: [Errno 2] No such file or directory"尝试生成 TFrecord 但我收到此错误“FileNotFoundError: [Errno 2] No such file or directory”
【发布时间】:2021-03-30 12:27:15
【问题描述】:

我正在关注此链接https://colab.research.google.com/drive/11ko0DBnI1QLxVoJQR8gt9b4JDcvbCrtU#scrollTo=A_tyvKnBP6qD 来构建我的对象检测器。我正在使用谷歌合作。我的工作区结构与此链接中的完全相同。在这段代码之前一切都很好:

from object_detection.utils import dataset_util
%cd /content/drive/MyDrive/Gun_Detection/models

data_base_url = '/content/drive/MyDrive/Gun_Detection/data'
image_dir = data_base_url + 'images/'

def class_text_to_int(row_label):
        if row_label == 'pistol':
                return 1
        else:
                None


def split(df, group):
        data = namedtuple('data', ['filename', 'object']) #we wanna group by
        gb = df.groupby(group) #split data into group data by splitting, applying n combining
        return [data(filename, gb.get_group(x)) 
  for filename, x in zip(gb.groups.keys(), gb.groups)] #add group keys to index to identify pieces.

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

for csv in ['train_labels', 'test_labels']:
  writer = tf.io.TFRecordWriter(data_base_url + csv + '.record')
  path = os.path.join(image_dir)
  examples = pd.read_csv(data_base_url + csv + '.csv')
  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(), data_base_url + csv + '.record')
  print('Successfully created the TFRecords: {}'.format(data_base_url + csv + '.record'))

发生此错误后,datatrain_label.record 在我驱动器的 Gun Detection 文件夹中生成。我很困惑 ????我不能再继续下去了。请帮忙!

N.B:我不是 python 专家,我还在学习。努力理解代码,但我真的没有。

【问题讨论】:

    标签: object-detection tfrecord


    【解决方案1】:

    提供 data_base_url 路径为'/content/drive/MyDrive/Gun_Detection/data/',您在其末尾缺少/,因此您的代码无法找到 image_dir。 p>

    使用os.path.join()函数来避免这种情况。

    【讨论】:

    • 我做了,但它仍然产生同样的错误。
    • 你能编辑你的问题并发布更新的代码吗,因为它是路径问题,如果你使用 os.path.join() 而不是 +,你不会有问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-29
    • 1970-01-01
    • 2016-10-25
    • 2022-01-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多