सुविधा डिकोडिंग को अनुकूलित करना

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

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

छवि डिकोडिंग छोड़ना

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

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

पिछले उदाहरण के लिए इसी तरह, आप उपयोग कर सकते हैं 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.Decoder का उपयोग कर वस्तु tfds.decode.make_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 के सबसेट का चयन करेंगे builder.info.features दिया मिलान tfds.decode.PartialDecoding संरचना।

उपरोक्त कोड में, विशेषताओं परोक्ष मैच के लिए निकाले जाते हैं 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 को पारित किया जा सकता tfds.decode.PartialDecoding के माध्यम से, PartialDecoding(..., decoders={}) kwargs।