TensorFlow - TFRecords 加载和转换带有边界框的图像

数据挖掘 张量流 图像分类 迁移学习
2022-03-08 14:05:08

我正在尝试使用 TensorFlow 构建“汽车分类器”。

我有 1000 张带标签的 JPG 图像,800x800,带有边界框和相关的 annotations.coco.json;分成训练/验证/测试文件夹。

我已经设法使用以下代码加载 TFRecordDataset:

TFRecord 数据集加载步骤

# Load TfRecord data sets
raw_train = tf.data.TFRecordDataset([training_file])
raw_validation = tf.data.TFRecordDataset([validation_file])
raw_test = tf.data.TFRecordDataset([testing_file])

# Load label map
category_index = label_map_util.create_category_index_from_labelmap(label_map_file, use_display_name=True)

------------------------------------------------------------------------------------------
def extract_features(tfrecord):
    # Extract features using the keys set during creation
    features = {
        'image/object/bbox/xmin': tf.io.VarLenFeature(dtype=tf.float32),
        'image/object/bbox/ymin': tf.io.VarLenFeature(dtype=tf.float32),
        'image/object/bbox/xmax': tf.io.VarLenFeature(dtype=tf.float32),
        'image/object/bbox/ymax': tf.io.VarLenFeature(dtype=tf.float32),        
        'image/object/class/label': tf.io.VarLenFeature(dtype=tf.int64),        
        'image/width': tf.io.FixedLenFeature([], tf.int64),
        'image/height': tf.io.FixedLenFeature([], tf.int64),
        'image/encoded': tf.io.FixedLenFeature([], tf.string)
    }

    # Extract the data record
    sample = tf.io.parse_single_example(tfrecord, features)

    image = tf.io.decode_image(sample['image/encoded'])        
    label = sample['image/object/class/label']
        
    return [image, label]

raw_train = raw_train.map(extract_features)
raw_validation = raw_validation.map(extract_features)
raw_test = raw_test.map(extract_features)

转换/调整图像以进行训练

ORIGINAL_IMG_SIZE = 800
RESIZE_IMG_SIZE = 160 # All images will be resized to 160x160 or 614x614 maybe for Yolo?

def format_example(image, label):
    #https://stackoverflow.com/questions/62957726/i-got-value-error-that-image-has-no-shape-while-converting-image-to-tensor-for-p
    image.set_shape([ORIGINAL_IMG_SIZE, ORIGINAL_IMG_SIZE, 3])
    image = tf.cast(image, tf.float32)
    image = (image/127.5) - 1
    image = tf.image.resize(image, (RESIZE_IMG_SIZE, RESIZE_IMG_SIZE))
    return image, label
  • Tensorflow 示例似乎只讨论调整整个图像的大小,而不是讨论如何处理图像中边界框和边界框标签的大小调整。

  • 有没有人有任何关于如何处理图像大小以及图像中包含的边界框的示例?

培训管道

  • 同样,Tensorflow 示例似乎只训练整个图像,而不是带有边界框和相关边界框标签的图像。

  • 有没有人有任何使用带有边界框和相关边界框标签的图像进行 TensorFlow 迁移学习训练的示例?

0个回答
没有发现任何回复~