Skip to content

Commit 27d7962

Browse files
zsdonghaoluomai
authored andcommitted
Affine transformation APIs for high-performance image augmentation (#857)
* release affine transform matrix APIs * chaneglog * affine transform example * example with dataset API; * rm unused import * update docs * x * remove pickle * shear v2 * x * respective zoom * Add text * minor fix * x * x * cv2 * remove random * woring on cv2 * cv2 transform done * Keypoints done * test and clean up tutorial and doc. * add vertical flipping. * docs * fix * imporve doc * fix * fix * tune figure positions. * :scale: -> :width: * change pdf to png * use jpeg * fix alignment * update doc * fix doc * resize then random crop * fix docs * try fix doc indent (#864) doc test fix * update CHANGELOG * Update CHANGELOG.md * fix lints
1 parent 6007ef3 commit 27d7962

File tree

10 files changed

+1029
-374
lines changed

10 files changed

+1029
-374
lines changed

CHANGELOG.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,17 @@ To release a new version, please update the changelog as followed:
7272
### Added
7373
- Layer:
7474
- Release `GroupNormLayer` (PR #850)
75+
- Image affine transformation APIs
76+
- `affine_rotation_matrix` (PR #857)
77+
- `affine_horizontal_flip_matrix` (PR #857)
78+
- `affine_vertical_flip_matrix` (PR #857)
79+
- `affine_shift_matrix` (PR #857)
80+
- `affine_shear_matrix` (PR #857)
81+
- `affine_zoom_matrix` (PR #857)
82+
- `affine_transform_cv2` (PR #857)
83+
- `affine_transform_keypoints` (PR #857)
84+
- Affine transformation tutorial
85+
- `examples/data_process/tutorial_fast_affine_transform.py` (PR #857)
7586

7687
### Changed
7788

@@ -95,6 +106,8 @@ To release a new version, please update the changelog as followed:
95106
### Contributors
96107
- @2wins: #850 #855
97108
- @DEKHTIARJonathan: #853
109+
- @zsdonghao: #857
110+
- @luomai: #857
98111

99112
## [1.10.1] - 2018-09-07
100113

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

137+
124138
## [1.10.0] - 2018-09-02
125139

126140
### Added

README.md

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,15 @@
44
</div>
55
</a>
66

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

129

1310
![PyPI Stable Version](http://ec2-35-178-47-120.eu-west-2.compute.amazonaws.com/github/release/tensorlayer/tensorlayer.svg?label=PyPI%20-%20Release)
1411
![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)
1512
[![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)
1613
[![PyPI - Python Version](http://ec2-35-178-47-120.eu-west-2.compute.amazonaws.com/pypi/pyversions/tensorlayer.svg)](https://pypi.org/project/tensorlayer/)
1714
[![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)
15+
[![Downloads](http://pepy.tech/badge/tensorlayer)](http://pepy.tech/project/tensorlayer)
1816

1917
[![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)
2018
[![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)
@@ -62,16 +60,26 @@ TensorLayer has a fast growing usage among top researchers and engineers, from u
6260
Imperial College London, UC Berkeley, Carnegie Mellon University, Stanford University, and
6361
University of Technology of Compiegne (UTC), and companies like Google, Microsoft, Alibaba, Tencent, Xiaomi, and Bloomberg.
6462

65-
# Tutorials, Examples and Applications
63+
# Tutorials and Real-World Applications
64+
65+
You can find a large collection of tutorials, examples and real-world applications using TensorLayer through the following space:
6666

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

73+
# Documentation
74+
75+
TensorLayer has extensive documentation for both beginners and professionals. The documentation is available in
76+
both English and Chinese. Please click the following icons to find the documents you need:
77+
78+
[![English Documentation](https://img.shields.io/badge/documentation-english-blue.svg)](https://tensorlayer.readthedocs.io/)
79+
[![Chinese Documentation](https://img.shields.io/badge/documentation-%E4%B8%AD%E6%96%87-blue.svg)](https://tensorlayercn.readthedocs.io/)
80+
[![Chinese Book](https://img.shields.io/badge/book-%E4%B8%AD%E6%96%87-blue.svg)](http://www.broadview.com.cn/book/5059/)
7381

74-
# Installation Guide
82+
# Install
7583

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

100-
## Using Docker - a ready-to-use environment
108+
## Using Docker
101109

102110
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/):
103111

89.9 KB
Loading

docs/images/affine_transform_why.jpg

90.8 KB
Loading

docs/index.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Welcome to TensorLayer
33

44

55
.. image:: user/my_figs/tl_transparent_logo.png
6-
:scale: 30 %
6+
:width: 30 %
77
:align: center
88
:target: https://github.com/tensorlayer/tensorlayer
99

@@ -50,14 +50,14 @@ method, this part of the documentation is for you.
5050
modules/activation
5151
modules/array_ops
5252
modules/cost
53+
modules/prepro
5354
modules/distributed
5455
modules/files
5556
modules/iterate
5657
modules/layers
5758
modules/models
5859
modules/nlp
5960
modules/optimizers
60-
modules/prepro
6161
modules/rein
6262
modules/utils
6363
modules/visualize

docs/modules/prepro.rst

Lines changed: 182 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
1-
API - Preprocessing
1+
API - Data Pre-processing
22
=========================
33

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

136
.. autosummary::
147

15-
threading_data
8+
affine_rotation_matrix
9+
affine_horizontal_flip_matrix
10+
affine_vertical_flip_matrix
11+
affine_shift_matrix
12+
affine_shear_matrix
13+
affine_zoom_matrix
14+
affine_respective_zoom_matrix
15+
16+
transform_matrix_offset_center
17+
affine_transform
18+
affine_transform_cv2
19+
affine_transform_keypoints
20+
projective_transform_by_points
1621

1722
rotation
1823
rotation_multi
@@ -33,6 +38,7 @@ Some of the code in this package are borrowed from Keras.
3338
elastic_transform_multi
3439

3540
zoom
41+
respective_zoom
3642
zoom_multi
3743

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

5763
drop
5864

59-
transform_matrix_offset_center
60-
apply_transform
61-
projective_transform_by_points
62-
6365
array_to_img
6466

6567
find_contours
@@ -88,7 +90,7 @@ Some of the code in this package are borrowed from Keras.
8890
obj_box_zoom
8991

9092
keypoint_random_crop
91-
keypoint_random_crop2
93+
keypoint_resize_random_crop
9294
keypoint_random_rotate
9395
keypoint_random_flip
9496
keypoint_random_resize
@@ -103,10 +105,166 @@ Some of the code in this package are borrowed from Keras.
103105
sequences_get_mask
104106

105107

108+
..
109+
Threading
110+
------------
111+
.. autofunction:: threading_data
112+
113+
114+
Affine Transform
115+
----------------
116+
117+
118+
Python can be FAST
119+
^^^^^^^^^^^^^^^^^^
120+
121+
Image augmentation is a critical step in deep learning.
122+
Though TensorFlow has provided ``tf.image``,
123+
image augmentation often remains as a key bottleneck.
124+
``tf.image`` has three limitations:
125+
126+
- Real-world visual tasks such as object detection, segmentation, and pose estimation
127+
must cope with image meta-data (e.g., coordinates).
128+
These data are beyond ``tf.image``
129+
which processes images as tensors.
130+
131+
- ``tf.image`` operators
132+
breaks the pure Python programing experience (i.e., users have to
133+
use ``tf.py_func`` in order to call image functions written in Python); however,
134+
frequent uses of ``tf.py_func`` slow down TensorFlow,
135+
making users hard to balance flexibility and performance.
136+
137+
- ``tf.image`` API is inflexible. Image operations are
138+
performed in an order. They are hard to jointly optimize. More importantly,
139+
sequential image operations can significantly
140+
reduces the quality of images, thus affecting training accuracy.
141+
142+
143+
TensorLayer addresses these limitations by providing a
144+
high-performance image augmentation API in Python.
145+
This API bases on affine transformation and ``cv2.wrapAffine``.
146+
It allows you to combine multiple image processing functions into
147+
a single matrix operation. This combined operation
148+
is executed by the fast ``cv2`` library, offering 78x performance improvement (observed in
149+
`openpose-plus <https://github.com/tensorlayer/openpose-plus>`_ for example).
150+
The following example illustrates the rationale
151+
behind this tremendous speed up.
152+
153+
154+
Example
155+
^^^^^^^
156+
157+
The source code of complete examples can be found \
158+
`here <https://github.com/tensorlayer/tensorlayer/tree/master/examples/data_process/tutorial_fast_affine_transform.py>`__.
159+
The following is a typical Python program that applies rotation, shifting, flipping, zooming and shearing to an image,
160+
161+
.. code-block:: python
162+
163+
image = tl.vis.read_image('tiger.jpeg')
164+
165+
xx = tl.prepro.rotation(image, rg=-20, is_random=False)
166+
xx = tl.prepro.flip_axis(xx, axis=1, is_random=False)
167+
xx = tl.prepro.shear2(xx, shear=(0., -0.2), is_random=False)
168+
xx = tl.prepro.zoom(xx, zoom_range=1/0.8)
169+
xx = tl.prepro.shift(xx, wrg=-0.1, hrg=0, is_random=False)
170+
171+
tl.vis.save_image(xx, '_result_slow.png')
172+
173+
174+
However, by leveraging affine transformation, image operations can be combined into one:
175+
176+
.. code-block:: python
177+
178+
# 1. Create required affine transformation matrices
179+
M_rotate = tl.prepro.affine_rotation_matrix(angle=20)
180+
M_flip = tl.prepro.affine_horizontal_flip_matrix(prob=1)
181+
M_shift = tl.prepro.affine_shift_matrix(wrg=0.1, hrg=0, h=h, w=w)
182+
M_shear = tl.prepro.affine_shear_matrix(x_shear=0.2, y_shear=0)
183+
M_zoom = tl.prepro.affine_zoom_matrix(zoom_range=0.8)
184+
185+
# 2. Combine matrices
186+
# NOTE: operations are applied in a reversed order (i.e., rotation is performed first)
187+
M_combined = M_shift.dot(M_zoom).dot(M_shear).dot(M_flip).dot(M_rotate)
188+
189+
# 3. Convert the matrix from Cartesian coordinates (the origin in the middle of image)
190+
# to image coordinates (the origin on the top-left of image)
191+
transform_matrix = tl.prepro.transform_matrix_offset_center(M_combined, x=w, y=h)
192+
193+
# 4. Transform the image using a single operation
194+
result = tl.prepro.affine_transform_cv2(image, transform_matrix) # 76 times faster
195+
196+
tl.vis.save_image(result, '_result_fast.png')
197+
198+
199+
The following figure illustrates the rational behind combined affine transformation.
200+
201+
.. image:: ../images/affine_transform_why.jpg
202+
:width: 100 %
203+
:align: center
204+
205+
206+
Using combined affine transformation has two key benefits. First, it allows \
207+
you to leverage a pure Python API to achieve orders of magnitudes of speed up in image augmentation,
208+
and thus prevent data pre-processing from becoming a bottleneck in training. \
209+
Second, performing sequential image transformation requires multiple image interpolations. \
210+
This produces low-quality input images. In contrast, a combined transformation performs the \
211+
interpolation only once, and thus
212+
preserve the content in an image. The following figure illustrates these two benefits:
213+
214+
.. image:: ../images/affine_transform_comparison.jpg
215+
:width: 100 %
216+
:align: center
217+
218+
219+
Get rotation matrix
220+
^^^^^^^^^^^^^^^^^^^^^^^^^
221+
.. autofunction:: affine_rotation_matrix
222+
223+
Get horizontal flipping matrix
224+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
225+
.. autofunction:: affine_horizontal_flip_matrix
226+
227+
Get vertical flipping matrix
228+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
229+
.. autofunction:: affine_vertical_flip_matrix
230+
231+
Get shifting matrix
232+
^^^^^^^^^^^^^^^^^^^^^^^^^
233+
.. autofunction:: affine_shift_matrix
234+
235+
Get shearing matrix
236+
^^^^^^^^^^^^^^^^^^^^^^^^^
237+
.. autofunction:: affine_shear_matrix
238+
239+
Get zooming matrix
240+
^^^^^^^^^^^^^^^^^^^^^^^^^
241+
.. autofunction:: affine_zoom_matrix
242+
243+
Get respective zooming matrix
244+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
245+
.. autofunction:: affine_respective_zoom_matrix
246+
247+
Cartesian to image coordinates
248+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
249+
.. autofunction:: transform_matrix_offset_center
250+
251+
..
252+
Apply image transform
253+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
254+
.. autofunction:: affine_transform
255+
256+
Apply image transform
257+
^^^^^^^^^^^^^^^^^^^^^
258+
.. autofunction:: affine_transform_cv2
259+
260+
Apply keypoint transform
261+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
262+
.. autofunction:: affine_transform_keypoints
263+
264+
Projective transform by points
265+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
266+
.. autofunction:: projective_transform_by_points
106267

107-
Threading
108-
------------
109-
.. autofunction:: threading_data
110268

111269
Images
112270
-----------
@@ -160,6 +318,10 @@ Zoom
160318
.. autofunction:: zoom
161319
.. autofunction:: zoom_multi
162320

321+
Respective Zoom
322+
^^^^^^^^^^^^^^^^^
323+
.. autofunction:: respective_zoom
324+
163325
Brightness
164326
^^^^^^^^^^^^
165327
.. autofunction:: brightness
@@ -203,18 +365,6 @@ Noise
203365
^^^^^^^^^^^^^^
204366
.. autofunction:: drop
205367

206-
Transform matrix offset
207-
^^^^^^^^^^^^^^^^^^^^^^^^^
208-
.. autofunction:: transform_matrix_offset_center
209-
210-
Apply affine transform by matrix
211-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
212-
.. autofunction:: apply_transform
213-
214-
Projective transform by points
215-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
216-
.. autofunction:: projective_transform_by_points
217-
218368
Numpy and PIL
219369
^^^^^^^^^^^^^^
220370
.. autofunction:: array_to_img
@@ -430,7 +580,9 @@ Image Aug - Crop
430580
^^^^^^^^^^^^^^^^^^^^
431581
.. autofunction:: keypoint_random_crop
432582

433-
.. autofunction:: keypoint_random_crop2
583+
Image Aug - Resize then Crop
584+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
585+
.. autofunction:: keypoint_resize_random_crop
434586

435587
Image Aug - Rotate
436588
^^^^^^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)