自訂功能解碼

tfds.decode API 可讓您覆寫預設的功能解碼。主要用途是略過圖片解碼,以提升效能。

用法範例

略過圖片解碼

為了完整掌控解碼管線,或是在圖片解碼前套用篩選器 (以提升效能),您可以完全略過圖片解碼。tfds.features.Imagetfds.features.Video 皆適用。

ds = tfds.load('imagenet2012', split='train', decoders={
    'image': tfds.decode.SkipDecoding(),
})

for example in ds.take(1):
  assert example['image'].dtype == tf.string  # Images are not decoded

在圖片解碼前篩選/隨機排序資料集

與先前的範例類似,您可以使用 tfds.decode.SkipDecoding() 在解碼圖片前插入額外的 tf.data 管線自訂設定。如此一來,已篩選的圖片就不會經過解碼,而您可以使用更大的隨機排序緩衝區。

# Load the base dataset without decoding
ds, ds_info = tfds.load(
    'imagenet2012',
    split='train',
    decoders={
        'image': tfds.decode.SkipDecoding(),  # Image won't be decoded here
    },
    as_supervised=True,
    with_info=True,
)
# Apply filter and shuffle
ds = ds.filter(lambda image, label: label != 10)
ds = ds.shuffle(10000)
# Then decode with ds_info.features['image']
ds = ds.map(
    lambda image, label: ds_info.features['image'].decode_example(image), label)

同時裁剪和解碼

若要覆寫預設的 tf.io.decode_image 作業,您可以使用 tfds.decode.make_decoder() 裝飾器建立新的 tfds.decode.Decoder 物件。

@tfds.decode.make_decoder()
def decode_example(serialized_image, feature):
  crop_y, crop_x, crop_height, crop_width = 10, 10, 64, 64
  return tf.image.decode_and_crop_jpeg(
      serialized_image,
      [crop_y, crop_x, crop_height, crop_width],
      channels=feature.feature.shape[-1],
  )

ds = tfds.load('imagenet2012', split='train', decoders={
    # With video, decoders are applied to individual frames
    'image': decode_example(),
})

這相當於

def decode_example(serialized_image, feature):
  crop_y, crop_x, crop_height, crop_width = 10, 10, 64, 64
  return tf.image.decode_and_crop_jpeg(
      serialized_image,
      [crop_y, crop_x, crop_height, crop_width],
      channels=feature.shape[-1],
  )

ds, ds_info = tfds.load(
    'imagenet2012',
    split='train',
    with_info=True,
    decoders={
        'image': tfds.decode.SkipDecoding(),  # Skip frame decoding
    },
)
ds = ds.map(functools.partial(decode_example, feature=ds_info.features['image']))

自訂影片解碼

影片是 Sequence(Image())。套用自訂解碼器時,這些解碼器會套用至個別影格。這表示圖片的解碼器會自動與影片相容。

@tfds.decode.make_decoder()
def decode_example(serialized_image, feature):
  crop_y, crop_x, crop_height, crop_width = 10, 10, 64, 64
  return tf.image.decode_and_crop_jpeg(
      serialized_image,
      [crop_y, crop_x, crop_height, crop_width],
      channels=feature.feature.shape[-1],
  )

ds = tfds.load('ucf101', split='train', decoders={
    # With video, decoders are applied to individual frames
    'video': decode_example(),
})

這相當於

def decode_frame(serialized_image):
  """Decodes a single frame."""
  crop_y, crop_x, crop_height, crop_width = 10, 10, 64, 64
  return tf.image.decode_and_crop_jpeg(
      serialized_image,
      [crop_y, crop_x, crop_height, crop_width],
      channels=ds_info.features['video'].shape[-1],
  )


def decode_video(example):
  """Decodes all individual frames of the video."""
  video = example['video']
  video = tf.map_fn(
      decode_frame,
      video,
      dtype=ds_info.features['video'].dtype,
      parallel_iterations=10,
  )
  example['video'] = video
  return example


ds, ds_info = tfds.load('ucf101', split='train', with_info=True, decoders={
    'video': tfds.decode.SkipDecoding(),  # Skip frame decoding
})
ds = ds.map(decode_video)  # Decode the video

僅解碼部分功能子集。

也可以僅指定您需要的功能,完全略過某些功能。所有其他功能都會遭到忽略/略過。

builder = tfds.builder('my_dataset')
builder.as_dataset(split='train', decoders=tfds.decode.PartialDecoding({
    'image': True,
    'metadata': {'num_objects', 'scene_name'},
    'objects': {'label'},
})

TFDS 會選取符合指定 tfds.decode.PartialDecoding 結構的 builder.info.features 子集。

在上述程式碼中,功能會隱含擷取以符合 builder.info.features。也可以明確定義功能。上述程式碼相當於

builder = tfds.builder('my_dataset')
builder.as_dataset(split='train', decoders=tfds.decode.PartialDecoding({
    'image': tfds.features.Image(),
    'metadata': {
        'num_objects': tf.int64,
        'scene_name': tfds.features.Text(),
    },
    'objects': tfds.features.Sequence({
        'label': tfds.features.ClassLabel(names=[]),
    }),
})

原始中繼資料 (標籤名稱、圖片形狀等等) 會自動重複使用,因此不需要提供。

tfds.decode.SkipDecoding 可以透過 PartialDecoding(..., decoders={}) kwargs 傳遞至 tfds.decode.PartialDecoding