# An example of Compose?

**URL:** <https://discuss.ai.google.dev/t/an-example-of-compose/31313>\
**Category:** General Discussion\
**Tags:** addons\
**Created:** [June 6, 2021, 3:55pm UTC](https://discuss.ai.google.dev/t/an-example-of-compose/31313 "2021-06-06T15:55:45Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sayak\_Paul](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/sayak_paul/32/13002_2.png) [@Sayak\_Paul](https://discuss.ai.google.dev/u/Sayak_Paul)\
**Post date:** [June 6, 2021, 3:55pm UTC](https://discuss.ai.google.dev/t/an-example-of-compose/31313/1 "2021-06-06T15:55:45Z")

</div>

@Bhack could you provide a minimal example of `tfa.image.compose_transforms()`?

> **[tfa.image.compose\_transforms  |  TensorFlow Addons](https://www.tensorflow.org/addons/api_docs/python/tfa/image/compose_transforms)**
>
> Composes the transforms tensors.

---

<div class="post-metadata">

**Author:** ![Bhack](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/bhack/32/6388_2.png) [@Bhack](https://discuss.ai.google.dev/u/Bhack)\
**Post date:** [June 6, 2021, 8:06pm UTC](https://discuss.ai.google.dev/t/an-example-of-compose/31313/2 "2021-06-06T20:06:23Z")

</div>

Here you have rotation + translation

> <https://github.com/tensorflow/addons/blob/master/tensorflow_addons/image/tests/transform_ops_test.py#L41-L55>

---

<div class="post-metadata">

**Author:** ![Sayak\_Paul](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/sayak_paul/32/13002_2.png) [@Sayak\_Paul](https://discuss.ai.google.dev/u/Sayak_Paul)\
**Post date:** [June 7, 2021, 1:48am UTC](https://discuss.ai.google.dev/t/an-example-of-compose/31313/3 "2021-06-07T01:48:39Z")

</div>

Thanks but in practice what are we supposed to be providing to `compose_transforms()`? Something like the following?

```auto
compose_transforms([tf.image.rot90, tf.image.random_flip_left_right])

```

If so, how it’s supposed to be applied to an image or a batch of images? From the documentation and also the test-case shared above, these questions are not clear.

---

<div class="post-metadata">

**Author:** ![Bhack](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/bhack/32/6388_2.png) [@Bhack](https://discuss.ai.google.dev/u/Bhack)\
**Post date:** [June 7, 2021, 10:06am UTC](https://discuss.ai.google.dev/t/an-example-of-compose/31313/4 "2021-06-07T10:06:00Z")

</div>

Internally it is just a matmul loop over single projective tranfroms matrix in the list (or batched):

> <https://github.com/tensorflow/addons/blob/master/tensorflow_addons/image/transform_ops.py#L177:L179>

---

<div class="post-metadata">

**Author:** ![Sayak\_Paul](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/sayak_paul/32/13002_2.png) [@Sayak\_Paul](https://discuss.ai.google.dev/u/Sayak_Paul)\
**Post date:** [June 7, 2021, 10:27am UTC](https://discuss.ai.google.dev/t/an-example-of-compose/31313/5 "2021-06-07T10:27:17Z")

</div>

Sadly, I am still not clear as to how to use it with a dataset or even a single image having a leading batch dimension.

---

<div class="post-metadata">

**Author:** ![Bhack](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/bhack/32/6388_2.png) [@Bhack](https://discuss.ai.google.dev/u/Bhack)\
**Post date:** [June 7, 2021, 10:42am UTC](https://discuss.ai.google.dev/t/an-example-of-compose/31313/6 "2021-06-07T10:42:43Z")

</div>

That method is just going to compose projective transformations.

The real transformation is in `transform` that is still quite duplicated in addons cause the `transform` in TF is still a private API (but the transform Matrix format is the same).

See the thread starting at

[https://tensorflow-prod.ospodiscourse.com/t/cannot-run-model-fit-locally-help/1648/9](https://tensorflow-prod.ospodiscourse.com/t/cannot-run-model-fit-locally-help/1648/9)

---

<div class="post-metadata">

**Author:** ![Sayak\_Paul](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/sayak_paul/32/13002_2.png) [@Sayak\_Paul](https://discuss.ai.google.dev/u/Sayak_Paul)\
**Post date:** [June 7, 2021, 10:51am UTC](https://discuss.ai.google.dev/t/an-example-of-compose/31313/7 "2021-06-07T10:51:28Z")

</div>

Here’s a shot example applying the composition on a standard image:

> **[Google Colab](https://colab.research.google.com/gist/sayakpaul/6e39e972ba0075801caa907f3fa4eeba/scratchpad.ipynb)**

---

<div class="post-metadata">

**Author:** ![Bhack](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/bhack/32/6388_2.png) [@Bhack](https://discuss.ai.google.dev/u/Bhack)\
**Post date:** [June 7, 2021, 11:53am UTC](https://discuss.ai.google.dev/t/an-example-of-compose/31313/8 "2021-06-07T11:53:53Z")

</div>

This is the same with internals

```python
from tensorflow.python.keras.layers.preprocessing.image_preprocessing import transform
....
image_transformed = transform(tf.expand_dims(image, axis=0),composed)

```

---

<div class="post-metadata">

**Author:** ![Bhack](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/bhack/32/6388_2.png) [@Bhack](https://discuss.ai.google.dev/u/Bhack)\
**Post date:** [June 7, 2021, 12:01pm UTC](https://discuss.ai.google.dev/t/an-example-of-compose/31313/9 "2021-06-07T12:01:07Z")

</div>

E.g. the internal one was used in transformations that support models in model gardens SIG.

[https://github.com/tensorflow/models/blob/master/official/vision/beta/ops/augment.py](https://github.com/tensorflow/models/blob/master/official/vision/beta/ops/augment.py)

These transformations are available for users in  
[https://pypi.org/project/tf-models-official](https://pypi.org/project/tf-models-official)

Most of the transformations available in Addons I think that are duplicated now.

---

<div class="post-metadata">

**Author:** ![Sayak\_Paul](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/sayak_paul/32/13002_2.png) [@Sayak\_Paul](https://discuss.ai.google.dev/u/Sayak_Paul)\
**Post date:** [June 7, 2021, 12:07pm UTC](https://discuss.ai.google.dev/t/an-example-of-compose/31313/10 "2021-06-07T12:07:34Z")

</div>

Cool. But I think we both could agree that the documentation does not clearly reflect on these points. I guess we cannot expect the majority of users to dig into the source code and figure their way out into using these.

---

<div class="post-metadata">

**Author:** ![Bhack](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/bhack/32/6388_2.png) [@Bhack](https://discuss.ai.google.dev/u/Bhack)\
**Post date:** [June 7, 2021, 12:29pm UTC](https://discuss.ai.google.dev/t/an-example-of-compose/31313/11 "2021-06-07T12:29:01Z")

</div>

Yes probably some documentation is missing but I think that we can build a clear path and better documentation or tutorials when we will unify a little bit the image processing API that currently it is quite duplicated or sparse in the ecosystem (also generally we cannot promote private API to the end users like `from tensorflow.python`).

The modern history of this in Addons/Ecosystem it is already quite old:

> <https://github.com/tensorflow/addons/issues/1226>
>
> \*\*Describe the feature and the current behavior/state.\*\*
> RandAugment and AutoAu…gment are both policies for enhanced image preprocessing that are included in EfficientNet, but are still using \`tf.contrib\`.
> 
> https://github.com/tensorflow/tpu/blob/master/models/official/efficientnet/autoaugment.py
> 
> The only \`tf.contrib\` image operations that they use, however, are \[rotate\](https://www.tensorflow.org/addons/api\_docs/python/tfa/image/rotate), \[translate\](https://www.tensorflow.org/addons/api\_docs/python/tfa/image/translate) and \[transform\](https://www.tensorflow.org/addons/api\_docs/python/tfa/image/transform) - all of which have been included in TensorFlow Addons.
> 
> \*\*Relevant information\*\*
> \- Are you willing to contribute it (yes/no): 
> No, but am hoping that someone from the community will pick it up (potentially a Google Summer of Code student)?
> 
> \- Are you willing to maintain it going forward? (yes/no):
> Yes
> 
> \- Is there a relevant academic paper? (if so, where):
> AutoAugment Reference: https://arxiv.org/abs/1805.09501
> RandAugment Reference: https://arxiv.org/abs/1909.13719
> 
> \- Is there already an implementation in another framework? (if so, where):
> See link above; this would be a standard migration from \`tf.contrib\`.
> 
> \- Was it part of tf.contrib? (if so, where):
> Yes
> 
> \*\*Which API type would this fall under (layer, metric, optimizer, etc.)\*\*
> Image
> 
> \*\*Who will benefit with this feature?\*\*
> Anyone doing image preprocessing, especially for EfficientNet.

> <https://github.com/tensorflow/addons/issues/1333>
>
> Part 2: https://github.com/tensorflow/addons/issues/1353
> 
> As part of #1226, re…levant image ops need to be added to \`tfa.image\`. List of ops tracked in this issue (tick the op only once the corresponding PR is \*merged\*):
> 
> \- \[x\] Blend 
> \- \[x\] CutOut 
> \- \[\] Identity
> \- \[x\] Translate X, Y
> \- \[x\] Shear X, Y
> \- \[\] Solarize Ops 
> \- \[\] Posterize
> \- \[\] AutoContrast
> \- \[x\] Equalize 
> \- \[\] Invert  
> \- \[\] Color
> \- \[\] Brightness 
> \- \[x\] Sharpness 
> 
> Reference: https://github.com/tensorflow/tpu/blob/master/models/official/efficientnet/autoaugment.py
