Skip to content

Affine transformation APIs for high-performance image augmentation #857

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 46 commits into from
Oct 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
9052994
release affine transform matrix APIs
zsdonghao Sep 29, 2018
b83095c
chaneglog
zsdonghao Sep 29, 2018
310bf5c
affine transform example
zsdonghao Sep 29, 2018
f5c8e55
example with dataset API;
zsdonghao Sep 30, 2018
ab33466
rm unused import
zsdonghao Sep 30, 2018
34044f3
update docs
zsdonghao Sep 30, 2018
43adedb
x
zsdonghao Sep 30, 2018
3cf2876
remove pickle
zsdonghao Sep 30, 2018
2c272c4
shear v2
zsdonghao Sep 30, 2018
dc8b395
x
zsdonghao Sep 30, 2018
7407f9a
respective zoom
zsdonghao Sep 30, 2018
2ae3633
Add text
luomai Oct 1, 2018
1e5b8f0
minor fix
luomai Oct 1, 2018
be9db82
x
zsdonghao Oct 1, 2018
8174eaa
x
zsdonghao Oct 6, 2018
bb614be
Merge branch 'affine-transform' of github.com:tensorlayer/tensorlayer…
zsdonghao Oct 6, 2018
fa0a3b2
cv2
zsdonghao Oct 7, 2018
11ab297
Merge branch 'master' into affine-transform
zsdonghao Oct 9, 2018
71f30a6
remove random
zsdonghao Oct 9, 2018
f832eb5
Merge branch 'affine-transform' of github.com:tensorlayer/tensorlayer…
zsdonghao Oct 9, 2018
1a6a9d4
woring on cv2
zsdonghao Oct 9, 2018
2c85650
cv2 transform done
zsdonghao Oct 9, 2018
d9cfdea
Keypoints done
zsdonghao Oct 10, 2018
88597b4
test and clean up tutorial and doc.
luomai Oct 10, 2018
84bc759
add vertical flipping.
luomai Oct 10, 2018
1bd4b92
docs
zsdonghao Oct 11, 2018
cd705c0
fix
zsdonghao Oct 11, 2018
62de90a
imporve doc
luomai Oct 11, 2018
9706172
fix
luomai Oct 11, 2018
4e417a2
fix
luomai Oct 11, 2018
8c20a88
tune figure positions.
luomai Oct 11, 2018
e2abcad
:scale: -> :width:
lgarithm Oct 11, 2018
da18880
change pdf to png
luomai Oct 11, 2018
e971b25
use jpeg
luomai Oct 11, 2018
8c27b41
fix alignment
luomai Oct 11, 2018
2e69467
update doc
luomai Oct 12, 2018
00374f7
fix doc
luomai Oct 12, 2018
5229e43
Merge branch 'master' into affine-transform
Oct 12, 2018
12b35b6
resize then random crop
zsdonghao Oct 12, 2018
c1e5f58
fix docs
zsdonghao Oct 12, 2018
64b77bc
try fix doc indent (#864)
lgarithm Oct 13, 2018
cb78af1
Merge branch 'master' into affine-transform
luomai Oct 13, 2018
db3a876
Merge branch 'master' into affine-transform
lgarithm Oct 13, 2018
d314ac9
update CHANGELOG
luomai Oct 13, 2018
9d20f41
Update CHANGELOG.md
Oct 14, 2018
81bdb66
fix lints
lgarithm Oct 14, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,17 @@ To release a new version, please update the changelog as followed:
### Added
- Layer:
- Release `GroupNormLayer` (PR #850)
- Image affine transformation APIs
- `affine_rotation_matrix` (PR #857)
- `affine_horizontal_flip_matrix` (PR #857)
- `affine_vertical_flip_matrix` (PR #857)
- `affine_shift_matrix` (PR #857)
- `affine_shear_matrix` (PR #857)
- `affine_zoom_matrix` (PR #857)
- `affine_transform_cv2` (PR #857)
- `affine_transform_keypoints` (PR #857)
- Affine transformation tutorial
- `examples/data_process/tutorial_fast_affine_transform.py` (PR #857)

### Changed

Expand All @@ -95,6 +106,8 @@ To release a new version, please update the changelog as followed:
### Contributors
- @2wins: #850 #855
- @DEKHTIARJonathan: #853
- @zsdonghao: #857
- @luomai: #857

## [1.10.1] - 2018-09-07

Expand All @@ -121,6 +134,7 @@ To release a new version, please update the changelog as followed:
- @ndiy: #819
- @zsdonghao: #818


## [1.10.0] - 2018-09-02

### Added
Expand Down
22 changes: 15 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,15 @@
</div>
</a>

[![English Documentation](https://img.shields.io/badge/documentation-english-blue.svg)](https://tensorlayer.readthedocs.io/)
[![Chinese Documentation](https://img.shields.io/badge/documentation-%E4%B8%AD%E6%96%87-blue.svg)](https://tensorlayercn.readthedocs.io/)
[![Chinese Book](https://img.shields.io/badge/book-%E4%B8%AD%E6%96%87-blue.svg)](http://www.broadview.com.cn/book/5059/)
[![Downloads](http://pepy.tech/badge/tensorlayer)](http://pepy.tech/project/tensorlayer)



![PyPI Stable Version](http://ec2-35-178-47-120.eu-west-2.compute.amazonaws.com/github/release/tensorlayer/tensorlayer.svg?label=PyPI%20-%20Release)
![PyPI RC Version](http://ec2-35-178-47-120.eu-west-2.compute.amazonaws.com/github/release/tensorlayer/tensorlayer/all.svg?label=PyPI%20-%20Pre-Release)
[![Github commits (since latest release)](http://ec2-35-178-47-120.eu-west-2.compute.amazonaws.com/github/commits-since/tensorlayer/tensorlayer/latest.svg)](https://github.com/tensorlayer/tensorlayer/compare/1.10.1...master)
[![PyPI - Python Version](http://ec2-35-178-47-120.eu-west-2.compute.amazonaws.com/pypi/pyversions/tensorlayer.svg)](https://pypi.org/project/tensorlayer/)
[![Supported TF Version](http://ec2-35-178-47-120.eu-west-2.compute.amazonaws.com/badge/tensorflow-1.6.0+-blue.svg)](https://github.com/tensorflow/tensorflow/releases)
[![Downloads](http://pepy.tech/badge/tensorlayer)](http://pepy.tech/project/tensorlayer)

[![Build Status](http://ec2-35-178-47-120.eu-west-2.compute.amazonaws.com/travis/tensorlayer/tensorlayer/master.svg?label=Travis)](https://travis-ci.org/tensorlayer/tensorlayer)
[![CircleCI](http://ec2-35-178-47-120.eu-west-2.compute.amazonaws.com/circleci/project/github/tensorlayer/tensorlayer/master.svg?label=Docker%20Build)](https://circleci.com/gh/tensorlayer/tensorlayer/tree/master)
Expand Down Expand Up @@ -62,16 +60,26 @@ TensorLayer has a fast growing usage among top researchers and engineers, from u
Imperial College London, UC Berkeley, Carnegie Mellon University, Stanford University, and
University of Technology of Compiegne (UTC), and companies like Google, Microsoft, Alibaba, Tencent, Xiaomi, and Bloomberg.

# Tutorials, Examples and Applications
# Tutorials and Real-World Applications

You can find a large collection of tutorials, examples and real-world applications using TensorLayer through the following space:

<a href="https://github.com/tensorlayer/awesome-tensorlayer/blob/master/readme.md" target="\_blank">
<div align="center">
<img src="img/awesome-mentioned.png" width="40%"/>
</div>
</a>

# Documentation

TensorLayer has extensive documentation for both beginners and professionals. The documentation is available in
both English and Chinese. Please click the following icons to find the documents you need:

[![English Documentation](https://img.shields.io/badge/documentation-english-blue.svg)](https://tensorlayer.readthedocs.io/)
[![Chinese Documentation](https://img.shields.io/badge/documentation-%E4%B8%AD%E6%96%87-blue.svg)](https://tensorlayercn.readthedocs.io/)
[![Chinese Book](https://img.shields.io/badge/book-%E4%B8%AD%E6%96%87-blue.svg)](http://www.broadview.com.cn/book/5059/)

# Installation Guide
# Install

TensorLayer has pre-requisites including TensorFlow, numpy, and others. For GPU support, CUDA and cuDNN are required.
The simplest way to install TensorLayer is to use the **Py**thon **P**ackage **I**ndex (PyPI):
Expand All @@ -97,7 +105,7 @@ pip install https://github.com/tensorlayer/tensorlayer/archive/master.zip
# pip install https://github.com/tensorlayer/tensorlayer/archive/<branch-name>.zip
```

## Using Docker - a ready-to-use environment
## Using Docker

The [TensorLayer containers](https://hub.docker.com/r/tensorlayer/tensorlayer/) are built on top of the official [TensorFlow containers](https://hub.docker.com/r/tensorflow/tensorflow/):

Expand Down
Binary file added docs/images/affine_transform_comparison.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/affine_transform_why.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Welcome to TensorLayer


.. image:: user/my_figs/tl_transparent_logo.png
:scale: 30 %
:width: 30 %
:align: center
:target: https://github.com/tensorlayer/tensorlayer

Expand Down Expand Up @@ -50,14 +50,14 @@ method, this part of the documentation is for you.
modules/activation
modules/array_ops
modules/cost
modules/prepro
modules/distributed
modules/files
modules/iterate
modules/layers
modules/models
modules/nlp
modules/optimizers
modules/prepro
modules/rein
modules/utils
modules/visualize
Expand Down
212 changes: 182 additions & 30 deletions docs/modules/prepro.rst
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
API - Preprocessing
API - Data Pre-processing
=========================


We provide abundant data augmentation and processing functions by using Numpy, Scipy, Threading and Queue.
However, we recommend you to use TensorFlow operation function like ``tf.image.central_crop``,
more TensorFlow data augmentation method can be found
`here <https://www.tensorflow.org/api_guides/python/image.html>`_ and ``tutorial_cifar10_tfrecord.py``.
Some of the code in this package are borrowed from Keras.

.. automodule:: tensorlayer.prepro

.. autosummary::

threading_data
affine_rotation_matrix
affine_horizontal_flip_matrix
affine_vertical_flip_matrix
affine_shift_matrix
affine_shear_matrix
affine_zoom_matrix
affine_respective_zoom_matrix

transform_matrix_offset_center
affine_transform
affine_transform_cv2
affine_transform_keypoints
projective_transform_by_points

rotation
rotation_multi
Expand All @@ -33,6 +38,7 @@ Some of the code in this package are borrowed from Keras.
elastic_transform_multi

zoom
respective_zoom
zoom_multi

brightness
Expand All @@ -56,10 +62,6 @@ Some of the code in this package are borrowed from Keras.

drop

transform_matrix_offset_center
apply_transform
projective_transform_by_points

array_to_img

find_contours
Expand Down Expand Up @@ -88,7 +90,7 @@ Some of the code in this package are borrowed from Keras.
obj_box_zoom

keypoint_random_crop
keypoint_random_crop2
keypoint_resize_random_crop
keypoint_random_rotate
keypoint_random_flip
keypoint_random_resize
Expand All @@ -103,10 +105,166 @@ Some of the code in this package are borrowed from Keras.
sequences_get_mask


..
Threading
------------
.. autofunction:: threading_data


Affine Transform
----------------


Python can be FAST
^^^^^^^^^^^^^^^^^^

Image augmentation is a critical step in deep learning.
Though TensorFlow has provided ``tf.image``,
image augmentation often remains as a key bottleneck.
``tf.image`` has three limitations:

- Real-world visual tasks such as object detection, segmentation, and pose estimation
must cope with image meta-data (e.g., coordinates).
These data are beyond ``tf.image``
which processes images as tensors.

- ``tf.image`` operators
breaks the pure Python programing experience (i.e., users have to
use ``tf.py_func`` in order to call image functions written in Python); however,
frequent uses of ``tf.py_func`` slow down TensorFlow,
making users hard to balance flexibility and performance.

- ``tf.image`` API is inflexible. Image operations are
performed in an order. They are hard to jointly optimize. More importantly,
sequential image operations can significantly
reduces the quality of images, thus affecting training accuracy.


TensorLayer addresses these limitations by providing a
high-performance image augmentation API in Python.
This API bases on affine transformation and ``cv2.wrapAffine``.
It allows you to combine multiple image processing functions into
a single matrix operation. This combined operation
is executed by the fast ``cv2`` library, offering 78x performance improvement (observed in
`openpose-plus <https://github.com/tensorlayer/openpose-plus>`_ for example).
The following example illustrates the rationale
behind this tremendous speed up.


Example
^^^^^^^

The source code of complete examples can be found \
`here <https://github.com/tensorlayer/tensorlayer/tree/master/examples/data_process/tutorial_fast_affine_transform.py>`__.
The following is a typical Python program that applies rotation, shifting, flipping, zooming and shearing to an image,

.. code-block:: python

image = tl.vis.read_image('tiger.jpeg')

xx = tl.prepro.rotation(image, rg=-20, is_random=False)
xx = tl.prepro.flip_axis(xx, axis=1, is_random=False)
xx = tl.prepro.shear2(xx, shear=(0., -0.2), is_random=False)
xx = tl.prepro.zoom(xx, zoom_range=1/0.8)
xx = tl.prepro.shift(xx, wrg=-0.1, hrg=0, is_random=False)

tl.vis.save_image(xx, '_result_slow.png')


However, by leveraging affine transformation, image operations can be combined into one:

.. code-block:: python

# 1. Create required affine transformation matrices
M_rotate = tl.prepro.affine_rotation_matrix(angle=20)
M_flip = tl.prepro.affine_horizontal_flip_matrix(prob=1)
M_shift = tl.prepro.affine_shift_matrix(wrg=0.1, hrg=0, h=h, w=w)
M_shear = tl.prepro.affine_shear_matrix(x_shear=0.2, y_shear=0)
M_zoom = tl.prepro.affine_zoom_matrix(zoom_range=0.8)

# 2. Combine matrices
# NOTE: operations are applied in a reversed order (i.e., rotation is performed first)
M_combined = M_shift.dot(M_zoom).dot(M_shear).dot(M_flip).dot(M_rotate)

# 3. Convert the matrix from Cartesian coordinates (the origin in the middle of image)
# to image coordinates (the origin on the top-left of image)
transform_matrix = tl.prepro.transform_matrix_offset_center(M_combined, x=w, y=h)

# 4. Transform the image using a single operation
result = tl.prepro.affine_transform_cv2(image, transform_matrix) # 76 times faster

tl.vis.save_image(result, '_result_fast.png')


The following figure illustrates the rational behind combined affine transformation.

.. image:: ../images/affine_transform_why.jpg
:width: 100 %
:align: center


Using combined affine transformation has two key benefits. First, it allows \
you to leverage a pure Python API to achieve orders of magnitudes of speed up in image augmentation,
and thus prevent data pre-processing from becoming a bottleneck in training. \
Second, performing sequential image transformation requires multiple image interpolations. \
This produces low-quality input images. In contrast, a combined transformation performs the \
interpolation only once, and thus
preserve the content in an image. The following figure illustrates these two benefits:

.. image:: ../images/affine_transform_comparison.jpg
:width: 100 %
:align: center


Get rotation matrix
^^^^^^^^^^^^^^^^^^^^^^^^^
.. autofunction:: affine_rotation_matrix

Get horizontal flipping matrix
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
.. autofunction:: affine_horizontal_flip_matrix

Get vertical flipping matrix
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
.. autofunction:: affine_vertical_flip_matrix

Get shifting matrix
^^^^^^^^^^^^^^^^^^^^^^^^^
.. autofunction:: affine_shift_matrix

Get shearing matrix
^^^^^^^^^^^^^^^^^^^^^^^^^
.. autofunction:: affine_shear_matrix

Get zooming matrix
^^^^^^^^^^^^^^^^^^^^^^^^^
.. autofunction:: affine_zoom_matrix

Get respective zooming matrix
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
.. autofunction:: affine_respective_zoom_matrix

Cartesian to image coordinates
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
.. autofunction:: transform_matrix_offset_center

..
Apply image transform
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
.. autofunction:: affine_transform

Apply image transform
^^^^^^^^^^^^^^^^^^^^^
.. autofunction:: affine_transform_cv2

Apply keypoint transform
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
.. autofunction:: affine_transform_keypoints

Projective transform by points
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
.. autofunction:: projective_transform_by_points

Threading
------------
.. autofunction:: threading_data

Images
-----------
Expand Down Expand Up @@ -160,6 +318,10 @@ Zoom
.. autofunction:: zoom
.. autofunction:: zoom_multi

Respective Zoom
^^^^^^^^^^^^^^^^^
.. autofunction:: respective_zoom

Brightness
^^^^^^^^^^^^
.. autofunction:: brightness
Expand Down Expand Up @@ -203,18 +365,6 @@ Noise
^^^^^^^^^^^^^^
.. autofunction:: drop

Transform matrix offset
^^^^^^^^^^^^^^^^^^^^^^^^^
.. autofunction:: transform_matrix_offset_center

Apply affine transform by matrix
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
.. autofunction:: apply_transform

Projective transform by points
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
.. autofunction:: projective_transform_by_points

Numpy and PIL
^^^^^^^^^^^^^^
.. autofunction:: array_to_img
Expand Down Expand Up @@ -430,7 +580,9 @@ Image Aug - Crop
^^^^^^^^^^^^^^^^^^^^
.. autofunction:: keypoint_random_crop

.. autofunction:: keypoint_random_crop2
Image Aug - Resize then Crop
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
.. autofunction:: keypoint_resize_random_crop

Image Aug - Rotate
^^^^^^^^^^^^^^^^^^^^
Expand Down
Loading