বৈশিষ্ট্য ডিকোডিং কাস্টমাইজ করা

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 প্রদত্ত 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 এ পাস করা যেতে পারে।