尝试生成 TFrecord 但我收到此错误“FileNotFoundError: [Errno 2] No such file or directory”

时间:2021-03-30 12:27:15

标签: object-detection tfrecord

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

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 专家,我还在学习。努力理解代码,但老实说我没有。

1 个答案:

答案 0 :(得分:1)

提供 data_base_url 路径为 '/content/drive/MyDrive/Gun_Detection/data/' 你在它的末尾缺少一个 / 因为你的代码找不到 image_dir.

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