为了账号安全,请及时绑定邮箱和手机立即绑定

当我将帕斯卡数据集转换为 tfrecord 时出现“KeyError:face”

当我将帕斯卡数据集转换为 tfrecord 时出现“KeyError:face”

Helenr 2022-07-26 09:38:44
我正在使用tensorflow 模型,object_detection create_pascal_tf_record.py重命名create_face_tf_record.py将更宽的_face 数据集转换为 TF-Record:D:\0-STUDY\models\research>python object_detection\dataset_tools\create_face_tf_record.py \                             --data_dir=D:/0-STUDY \                             --year=widerface \                             --output_path=D:\0-STUDY\datasets\widerface\TF_data\train.record \                             --set=train包装只是为了看起来不错的输出:2020-02-11 09:41:46.804523: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudart64_100.dllWARNING:tensorflow:From object_detection\dataset_tools\create_face_tf_record.py:189: The name tf.app.run is deprecated. Please use tf.compat.v1.app.run instead.WARNING:tensorflow:From object_detection\dataset_tools\create_face_tf_record.py:163: The name tf.python_io.TFRecordWriter is deprecated. Please use tf.io.TFRecordWriter instead.W0211 09:41:49.443757 16972 module_wrapper.py:139] From object_detection\dataset_tools\create_face_tf_record.py:163: The name tf.python_io.TFRecordWriter is deprecated. Please use tf.io.TFRecordWriter instead.WARNING:tensorflow:From D:\0-STUDY\models\research\object_detection\utils\label_map_util.py:138: The name tf.gfile.GFile is deprecated. Please use tf.io.gfile.GFile instead.W0211 09:41:49.445752 16972 module_wrapper.py:139] From D:\0-STUDY\models\research\object_detection\utils\label_map_util.py:138: The name tf.gfile.GFile is deprecated. Please use tf.io.gfile.GFile instead.I0211 09:41:49.448744 16972 create_face_tf_record.py:168] Reading from PASCAL widerface dataset.I0211 09:41:49.491163 16972 create_face_tf_record.py:175] On image 0 of 12880D:\0-STUDY\models\research\object_detection\utils\dataset_util.py:79: FutureWarning: The behavior of this method will change in future versions. Use specific 'len(elem)' or 'elem is not None' test instead.
查看完整描述

1 回答

?
郎朗坤

TA贡献1921条经验 获得超9个赞

您可以使用以下方法将更宽的数据集转换为 TFRecords。


1.您需要创建一个config.py文件。


# Training

TRAIN_WIDER_PATH = "widerface/WIDER_train/"


#Validation

VAL_WIDER_PATH = "widerface/WIDER_val/"


#Testing

TEST_WIDER_PATH = "widerface/WIDER_test/"


# Ground Truth

GROUND_TRUTH_PATH = "widerface/wider_face_split/"


# Output

OUTPUT_PATH = "datasets/widerface/TF_data/"  

生成 TFRecords 的代码(create_tf_record.py)。

这是代码:


import tensorflow as tf

import numpy

import cv2

import os

import hashlib


import config

from utils import dataset_util


def parse_test_example(f, images_path):

    height = None # Image height

    width = None # Image width

    filename = None # Filename of the image. Empty if image is not from file

    encoded_image_data = None # Encoded image bytes

    image_format = b'jpeg' # b'jpeg' or b'png'


    filename = f.readline().rstrip()

    if not filename:

        raise IOError()


    filepath = os.path.join(images_path, filename)


    image_raw = cv2.imread(filepath)


    encoded_image_data = open(filepath, "rb").read()

    key = hashlib.sha256(encoded_image_data).hexdigest()


    height, width, channel = image_raw.shape


    tf_example = tf.train.Example(features=tf.train.Features(feature={

        'image/height': dataset_util.int64_feature(int(height)),

        'image/width': dataset_util.int64_feature(int(width)),

        'image/filename': dataset_util.bytes_feature(filename.encode('utf-8')),

        'image/source_id': dataset_util.bytes_feature(filename.encode('utf-8')),

        'image/key/sha256': dataset_util.bytes_feature(key.encode('utf8')),

        'image/encoded': dataset_util.bytes_feature(encoded_image_data),

        'image/format': dataset_util.bytes_feature('jpeg'.encode('utf8')),

        }))



    return tf_example



def parse_example(f, images_path):

    height = None # Image height

    width = None # Image width

    filename = None # Filename of the image. Empty if image is not from file

    encoded_image_data = None # Encoded image bytes

    image_format = b'jpeg' # b'jpeg' or b'png'


    xmins = [] # List of normalized left x coordinates in bounding box (1 per box)

    xmaxs = [] # List of normalized right x coordinates in bounding box (1 per box)

    ymins = [] # List of normalized top y coordinates in bounding box (1 per box)

    ymaxs = [] # List of normalized bottom y coordinates in bounding box (1 per box)

    classes_text = [] # List of string class name of bounding box (1 per box)

    classes = [] # List of integer class id of bounding box (1 per box)

    poses = []

    truncated = []

    difficult_obj = []


    filename = f.readline().rstrip()

    if not filename:

        raise IOError()


    filepath = os.path.join(images_path, filename)


    image_raw = cv2.imread(filepath)


    encoded_image_data = open(filepath, "rb").read()

    key = hashlib.sha256(encoded_image_data).hexdigest()


    height, width, channel = image_raw.shape


    face_num = int(f.readline().rstrip())

    if not face_num:

      face_num += 1

        # raise Exception()


    for i in range(face_num):

        annot = f.readline().rstrip().split()

        if not annot:

            raise Exception()


        # WIDER FACE DATASET CONTAINS SOME ANNOTATIONS WHAT EXCEEDS THE IMAGE BOUNDARY

        if(float(annot[2]) > 25.0):

            if(float(annot[3]) > 30.0):

                xmins.append( max(0.005, (float(annot[0]) / width) ) )

                ymins.append( max(0.005, (float(annot[1]) / height) ) )

                xmaxs.append( min(0.995, ((float(annot[0]) + float(annot[2])) / width) ) )

                ymaxs.append( min(0.995, ((float(annot[1]) + float(annot[3])) / height) ) )

                classes_text.append(b'face')

                classes.append(1)

                poses.append("front".encode('utf8'))

                truncated.append(int(0))



    tf_example = tf.train.Example(features=tf.train.Features(feature={

        'image/height': dataset_util.int64_feature(int(height)),

        'image/width': dataset_util.int64_feature(int(width)),

        'image/filename': dataset_util.bytes_feature(filename.encode('utf-8')),

        'image/source_id': dataset_util.bytes_feature(filename.encode('utf-8')),

        'image/key/sha256': dataset_util.bytes_feature(key.encode('utf8')),

        'image/encoded': dataset_util.bytes_feature(encoded_image_data),

        'image/format': dataset_util.bytes_feature('jpeg'.encode('utf8')),

        '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),

        'image/object/difficult': dataset_util.int64_list_feature(int(0)),

        'image/object/truncated': dataset_util.int64_list_feature(truncated),

        'image/object/view': dataset_util.bytes_list_feature(poses),

        }))



    return tf_example



def run(images_path, description_file, output_path, no_bbox=False):

    f = open(description_file)

    writer = tf.python_io.TFRecordWriter(output_path)


    i = 0


    print("Processing {}".format(images_path))

    while True:

        try:

            if no_bbox:

                tf_example = parse_test_example(f, images_path)

            else:

                tf_example = parse_example(f, images_path)


            writer.write(tf_example.SerializeToString())

            i += 1


        except IOError:

            break

        except Exception:

            raise


    writer.close()


    print("Correctly created record for {} images\n".format(i))



def main(unused_argv):

    # Training

    if config.TRAIN_WIDER_PATH is not None:

        images_path = os.path.join(config.TRAIN_WIDER_PATH, "images")

        description_file = os.path.join(config.GROUND_TRUTH_PATH, "wider_face_train_bbx_gt.txt")

        output_path = os.path.join(config.OUTPUT_PATH, "train.tfrecord")

        run(images_path, description_file, output_path)


    # Validation

    if config.VAL_WIDER_PATH is not None:

        images_path = os.path.join(config.VAL_WIDER_PATH, "images")

        description_file = os.path.join(config.GROUND_TRUTH_PATH, "wider_face_val_bbx_gt.txt")

        output_path = os.path.join(config.OUTPUT_PATH, "val.tfrecord")

        run(images_path, description_file, output_path)


    # Testing. This set does not contain bounding boxes, so the tfrecord will contain images only

    if config.TEST_WIDER_PATH is not None:

        images_path = os.path.join(config.TEST_WIDER_PATH, "images")

        description_file = os.path.join(config.GROUND_TRUTH_PATH, "wider_face_test_filelist.txt")

        output_path = os.path.join(config.OUTPUT_PATH, "test.tfrecord")

        run(images_path, description_file, output_path, no_bbox=True)



if __name__ == '__main__':

    tf.app.run()  

运行create_tf_record.py生成 TFRecord 文件。


python create_tf_record.py  

希望这能回答你的问题,快乐学习!


查看完整回答
反对 回复 2022-07-26
  • 1 回答
  • 0 关注
  • 55 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信