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
กรอง/สับเปลี่ยนชุดข้อมูลก่อนที่รูปภาพจะถูกถอดรหัส
ในทำนองเดียวกันกับตัวอย่างก่อนหน้านี้คุณสามารถใช้ 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