You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将图像掩码对及Carvana数据集转为适配DeepLab V3的TFRecord

Alright, let's break this down step by step—first the general method for converting image/mask pairs to TFRecord, then we'll tailor it specifically for the Carvana dataset to work with DeepLab V3.

1. General Approach to Convert Image/Mask Pairs to TFRecord Format

TFRecord is TensorFlow's optimized binary format for fast data loading, so the core idea is packaging each image and its corresponding mask into a serialized tf.train.Example protobuf. Here's a straightforward, actionable workflow:

  • Step 1: Organize your data
    Ensure you have a clear mapping between images and masks—either use a CSV/JSON file to list pairs, or follow a consistent naming pattern (like image_001.jpg pairing with image_001_mask.png).

  • Step 2: Write a helper to serialize data
    Convert images/masks to byte strings, then pack them into a tf.train.Example with metadata (height, width, file format). Here's a reusable code snippet:

    import tensorflow as tf
    import os
    
    def serialize_example(image_path, mask_path):
        # Read raw image and mask files
        image_bytes = tf.io.read_file(image_path)
        mask_bytes = tf.io.read_file(mask_path)
    
        # Extract image metadata
        image = tf.image.decode_jpeg(image_bytes)
        height, width = image.shape[0], image.shape[1]
        image_format = b'jpeg'
        mask_format = b'png'  # Use PNG for masks to preserve pixel value integrity
    
        # Build the feature dictionary
        feature = {
            'image/encoded': tf.train.Feature(bytes_list=tf.train.BytesList(value=[image_bytes.numpy()])),
            'image/format': tf.train.Feature(bytes_list=tf.train.BytesList(value=[image_format])),
            'image/height': tf.train.Feature(int64_list=tf.train.Int64List(value=[height])),
            'image/width': tf.train.Feature(int64_list=tf.train.Int64List(value=[width])),
            'mask/encoded': tf.train.Feature(bytes_list=tf.train.BytesList(value=[mask_bytes.numpy()])),
            'mask/format': tf.train.Feature(bytes_list=tf.train.BytesList(value=[mask_format])),
        }
    
        # Create and serialize the Example proto
        example_proto = tf.train.Example(features=tf.train.Features(feature=feature))
        return example_proto.SerializeToString()
    
  • Step 3: Write to TFRecord file
    Iterate over your image/mask pairs, serialize each, and write to a TFRecord file:

    def create_tfrecord(output_path, image_mask_pairs):
        with tf.io.TFRecordWriter(output_path) as writer:
            for img_path, mask_path in image_mask_pairs:
                serialized_example = serialize_example(img_path, mask_path)
                writer.write(serialized_example)
    
    # Example usage: map your image and mask directories
    image_dir = '/path/to/your/images'
    mask_dir = '/path/to/your/masks'
    image_filenames = [f for f in os.listdir(image_dir) if f.endswith('.jpg')]
    image_mask_pairs = [(os.path.join(image_dir, f), os.path.join(mask_dir, f.replace('.jpg', '_mask.png'))) for f in image_filenames]
    
    create_tfrecord('general_output.tfrecord', image_mask_pairs)
    
2. Converting Carvana Dataset to DeepLab V3-Compatible TFRecord

DeepLab V3 expects TFRecords formatted to match VOC/Cityscapes standards—meaning masks should be single-channel class index maps (each pixel is an integer representing a class, e.g., 0 for background, 1 for the car) instead of Carvana's binary RGB masks. Here's how to adapt the workflow:

Key Differences to Address:

  • Carvana masks are RGB images where the car is white ([255,255,255]) and background is black ([0,0,0]). We need to convert this to a single-channel mask with values 0 (background) and 1 (car).
  • DeepLab uses specific feature keys like image/segmentation/class/encoded instead of generic mask/encoded to align with its input pipeline.

Adapted Conversion Script:

import tensorflow as tf
import os

def process_carvana_mask(mask_path):
    # Convert Carvana's RGB mask to a single-channel class index map
    mask_bytes = tf.io.read_file(mask_path)
    mask = tf.image.decode_jpeg(mask_bytes)
    # Collapse RGB channels to a single channel (values 0 or 255)
    mask = tf.reduce_max(mask, axis=-1)
    # Convert white pixels (255) to 1, black (0) to 0
    mask = tf.cast(mask == 255, tf.uint8)
    # Encode as PNG to preserve integer values
    mask_encoded = tf.io.encode_png(mask)
    return mask_encoded

def serialize_deeplab_example(image_path, mask_path):
    # Process the input image
    image_bytes = tf.io.read_file(image_path)
    image = tf.image.decode_jpeg(image_bytes)
    height, width = image.shape[0], image.shape[1]

    # Process mask to match DeepLab's requirements
    mask_encoded = process_carvana_mask(mask_path)

    # Follow DeepLab's standard feature schema
    feature = {
        'image/encoded': tf.train.Feature(bytes_list=tf.train.BytesList(value=[image_bytes.numpy()])),
        'image/format': tf.train.Feature(bytes_list=tf.train.BytesList(value=[b'jpeg'])),
        'image/height': tf.train.Feature(int64_list=tf.train.Int64List(value=[height])),
        'image/width': tf.train.Feature(int64_list=tf.train.Int64List(value=[width])),
        'image/segmentation/class/encoded': tf.train.Feature(bytes_list=tf.train.BytesList(value=[mask_encoded.numpy()])),
        'image/segmentation/class/format': tf.train.Feature(bytes_list=tf.train.BytesList(value=[b'png'])),
    }

    example_proto = tf.train.Example(features=tf.train.Features(feature=feature))
    return example_proto.SerializeToString()

def create_deeplab_tfrecord(output_path, image_mask_pairs):
    with tf.io.TFRecordWriter(output_path) as writer:
        for img_path, mask_path in image_mask_pairs:
            serialized_example = serialize_deeplab_example(img_path, mask_path)
            writer.write(serialized_example)

# Example usage for Carvana dataset
carvana_image_dir = '/path/to/carvana/train'
carvana_mask_dir = '/path/to/carvana/train_masks'
image_filenames = [f for f in os.listdir(carvana_image_dir) if f.endswith('.jpg')]
# Carvana masks follow the pattern: {image_name}_mask.jpg
image_mask_pairs = [(os.path.join(carvana_image_dir, f), os.path.join(carvana_mask_dir, f.replace('.jpg', '_mask.jpg'))) for f in image_filenames]

create_deeplab_tfrecord('carvana_deeplab.tfrecord', image_mask_pairs)

Post-Validation Check:

To confirm your TFRecord works with DeepLab, write a quick script to inspect a sample:

def parse_deeplab_tfrecord(example_proto):
    feature_description = {
        'image/encoded': tf.io.FixedLenFeature([], tf.string),
        'image/segmentation/class/encoded': tf.io.FixedLenFeature([], tf.string),
    }
    example = tf.io.parse_single_example(example_proto, feature_description)
    image = tf.image.decode_jpeg(example['image/encoded'])
    mask = tf.image.decode_png(example['image/segmentation/class/encoded'])
    return image, mask

dataset = tf.data.TFRecordDataset('carvana_deeplab.tfrecord')
dataset = dataset.map(parse_deeplab_tfrecord)

# Check one sample
for img, mask in dataset.take(1):
    print(f"Image shape: {img.shape}, Mask shape: {mask.shape}")
    print(f"Mask unique values: {tf.unique(tf.reshape(mask, [-1]))[0].numpy()}")  # Should output [0, 1]

This will give you a TFRecord that integrates seamlessly with DeepLab V3's input pipeline, matching the schema it expects from VOC/Cityscapes.

内容的提问来源于stack exchange,提问作者user288609

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 06:56:25