|
| 1 | +# Customizing feature decoding |
| 2 | + |
| 3 | +* [Usage examples](#usage-examples) |
| 4 | + * [Skipping the image decoding](#skipping-the-image-decoding) |
| 5 | + * [Filter/shuffle dataset before images get decoded](#filtershuffle-dataset-before-images-get-decoded) |
| 6 | + * [Cropping and decoding at the same time](#cropping-and-decoding-at-the-same-time) |
| 7 | + * [Customizing video decoding](#customizing-video-decoding) |
| 8 | + |
| 9 | +The `tfds.decode` API allows you override the default feature decoding. The main |
| 10 | +use case is to skip the image decoding for better performance. |
| 11 | + |
| 12 | +Warning: This API gives you access to the low-level `tf.train.Example` format on |
| 13 | +disk (as defined by the `FeatureConnector`). This API is targeted towards |
| 14 | +advanced users who want better read performance with images. |
| 15 | + |
| 16 | +## Usage examples |
| 17 | + |
| 18 | +### Skipping the image decoding |
| 19 | + |
| 20 | +To keep full control over the decoding pipeline, or to apply a filter before the |
| 21 | +images get decoded (for better performance), you can skip the image decoding |
| 22 | +entirely. This works with both `tfds.features.Image` and `tfds.features.Video`. |
| 23 | + |
| 24 | +```python |
| 25 | +ds = tfds.load('imagenet2012', split='train', decoders={ |
| 26 | + 'image': tfds.decode.SkipDecoding(), |
| 27 | +}) |
| 28 | + |
| 29 | +for example in ds.take(1): |
| 30 | + assert example['image'].dtype == tf.string # Images are not decoded |
| 31 | +``` |
| 32 | + |
| 33 | +### Filter/shuffle dataset before images get decoded |
| 34 | + |
| 35 | +Similarly to the previous example, you can use `tfds.decode.SkipDecoding()` to |
| 36 | +insert additional `tf.data` pipeline customization before decoding the image. |
| 37 | +That way the filtered images won't be decoded and you can use a bigger shuffle |
| 38 | +buffer. |
| 39 | + |
| 40 | +```python |
| 41 | +# Load the base dataset without decoding |
| 42 | +ds, ds_info = tfds.load( |
| 43 | + 'imagenet2012', |
| 44 | + split='train', |
| 45 | + decoders={ |
| 46 | + 'image': tfds.decode.SkipDecoding(), # Image won't be decoded here |
| 47 | + }, |
| 48 | + as_supervised=True, |
| 49 | + with_info=True, |
| 50 | +) |
| 51 | +# Apply filter and shuffle |
| 52 | +ds = ds.filter(lambda image, label: label != 10) |
| 53 | +ds = ds.shuffle(10000) |
| 54 | +# Then decode with ds_info.features['image'] |
| 55 | +ds = ds.map( |
| 56 | + lambda image, label: ds_info.features['image'].decode_example(image), label) |
| 57 | + |
| 58 | +``` |
| 59 | + |
| 60 | +### Cropping and decoding at the same time |
| 61 | + |
| 62 | +To override the default `tf.io.decode_image` operation, you can create a new |
| 63 | +`tfds.decode.Decoder` object using the `tfds.decode.make_decoder()` decorator. |
| 64 | + |
| 65 | +```python |
| 66 | +@tfds.decode.make_decoder() |
| 67 | +def decode_example(serialized_image, feature): |
| 68 | + crop_y, crop_x, crop_height, crop_width = 10, 10, 64, 64 |
| 69 | + return tf.image.decode_and_crop_jpeg( |
| 70 | + serialized_image, |
| 71 | + [crop_y, crop_x, crop_height, crop_width], |
| 72 | + channels=feature.feature.shape[-1], |
| 73 | + ) |
| 74 | + |
| 75 | +ds = tfds.load('imagenet2012', split='train', decoders={ |
| 76 | + # With video, decoders are applied to individual frames |
| 77 | + 'image': decode_example(), |
| 78 | +}) |
| 79 | +``` |
| 80 | + |
| 81 | +Which is equivalent to: |
| 82 | + |
| 83 | +```python |
| 84 | +def decode_example(serialized_image, feature): |
| 85 | + crop_y, crop_x, crop_height, crop_width = 10, 10, 64, 64 |
| 86 | + return tf.image.decode_and_crop_jpeg( |
| 87 | + serialized_image, |
| 88 | + [crop_y, crop_x, crop_height, crop_width], |
| 89 | + channels=feature.shape[-1], |
| 90 | + ) |
| 91 | + |
| 92 | +ds, ds_info = tfds.load( |
| 93 | + 'imagenet2012', |
| 94 | + split='train', |
| 95 | + with_info=True, |
| 96 | + decoders={ |
| 97 | + 'image': tfds.decode.SkipDecoding(), # Skip frame decoding |
| 98 | + }, |
| 99 | +) |
| 100 | +ds = ds.map(functools.partial(decode_example, feature=ds_info.features['image'])) |
| 101 | +``` |
| 102 | + |
| 103 | +### Customizing video decoding |
| 104 | + |
| 105 | +Video are `Sequence(Image())`. When applying custom decoders, they will be |
| 106 | +applied to individual frames. This mean decoders for images are automatically |
| 107 | +compatible with video. |
| 108 | + |
| 109 | +```python |
| 110 | +@tfds.decode.make_decoder() |
| 111 | +def decode_example(serialized_image, feature): |
| 112 | + crop_y, crop_x, crop_height, crop_width = 10, 10, 64, 64 |
| 113 | + return tf.image.decode_and_crop_jpeg( |
| 114 | + serialized_image, |
| 115 | + [crop_y, crop_x, crop_height, crop_width], |
| 116 | + channels=feature.feature.shape[-1], |
| 117 | + ) |
| 118 | + |
| 119 | +ds = tfds.load('ucf101', split='train', decoders={ |
| 120 | + # With video, decoders are applied to individual frames |
| 121 | + 'video': decode_example(), |
| 122 | +}) |
| 123 | +``` |
| 124 | + |
| 125 | +Which is equivalent to: |
| 126 | + |
| 127 | +```python |
| 128 | +def decode_frame(serialized_image): |
| 129 | + """Decodes a single frame.""" |
| 130 | + crop_y, crop_x, crop_height, crop_width = 10, 10, 64, 64 |
| 131 | + return tf.image.decode_and_crop_jpeg( |
| 132 | + serialized_image, |
| 133 | + [crop_y, crop_x, crop_height, crop_width], |
| 134 | + channels=ds_info.features['video'].shape[-1], |
| 135 | + ) |
| 136 | + |
| 137 | + |
| 138 | +def decode_video(example): |
| 139 | + """Decodes all individual frames of the video.""" |
| 140 | + video = example['video'] |
| 141 | + video = tf.map_fn( |
| 142 | + decode_frame, |
| 143 | + video, |
| 144 | + dtype=ds_info.features['video'].dtype, |
| 145 | + parallel_iterations=10, |
| 146 | + back_prop=False, |
| 147 | + ) |
| 148 | + example['video'] = video |
| 149 | + return example |
| 150 | + |
| 151 | + |
| 152 | +ds, ds_info = tfds.load('ucf101', split='train', with_info=True, decoders={ |
| 153 | + 'video': tfds.decode.SkipDecoding(), # Skip frame decoding |
| 154 | +}) |
| 155 | +ds = ds.map(decode_video) # Decode the video |
| 156 | +``` |
0 commit comments