फीचर डिकोडिंग को अनुकूलित करना

tfds.decode API आपको डिफ़ॉल्ट सुविधा डिकोडिंग को ओवरराइड करने की अनुमति देता है। मुख्य उपयोग का मामला बेहतर प्रदर्शन के लिए छवि डिकोडिंग को छोड़ना है।

उपयोग के उदाहरण

छवि डिकोडिंग को छोड़ा जा रहा है

डिकोडिंग पाइपलाइन पर पूर्ण नियंत्रण रखने के लिए, या छवियों के डिकोड होने से पहले फ़िल्टर लागू करने के लिए (बेहतर प्रदर्शन के लिए), आप छवि डिकोडिंग को पूरी तरह से छोड़ सकते हैं। यह tfds.features.Image और tfds.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

छवियों को डिकोड करने से पहले डेटासेट को फ़िल्टर/फेरबदल करें

पिछले उदाहरण के समान, आप छवि को डिकोड करने से पहले अतिरिक्त tf.data पाइपलाइन अनुकूलन सम्मिलित करने के लिए tfds.decode.SkipDecoding() उपयोग कर सकते हैं। इस तरह फ़िल्टर की गई छवियां डिकोड नहीं की जाएंगी और आप एक बड़े शफ़ल बफ़र का उपयोग कर सकते हैं।

# 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.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 तक पास किया जा सकता है।