From 864d7cf6060ac38dd2e425c4548bdeafa9ace704 Mon Sep 17 00:00:00 2001 From: DEKHTIARJonathan Date: Thu, 19 Apr 2018 10:42:04 +0200 Subject: [PATCH 01/22] Activation Cleaning Docstring Test --- tensorlayer/activation.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/tensorlayer/activation.py b/tensorlayer/activation.py index 2bf741b97..1bb10d9d8 100644 --- a/tensorlayer/activation.py +++ b/tensorlayer/activation.py @@ -18,7 +18,8 @@ @deprecated("2018-06-30", "This API will be deprecated soon as tf.identity can do the same thing.") def identity(x): - """The identity activation function. + """Identity activation function. + Shortcut is ``linear``. Parameters @@ -36,7 +37,7 @@ def identity(x): def ramp(x, v_min=0, v_max=1, name=None): - """The ramp activation function. + """Ramp activation function. Parameters ---------- @@ -59,7 +60,7 @@ def ramp(x, v_min=0, v_max=1, name=None): def leaky_relu(x, alpha=0.1, name="lrelu"): - """The LeakyReLU, Shortcut is ``lrelu``. + """LeakyReLU, Shortcut is ``lrelu``. Modified version of ReLU, introducing a nonzero gradient for negative input. @@ -96,7 +97,8 @@ def leaky_relu(x, alpha=0.1, name="lrelu"): def swish(x, name='swish'): - """The Swish function. + """Swish function. + See `Swish: a Self-Gated Activation Function `__. Parameters @@ -193,6 +195,7 @@ def hard_tanh(x, name='htanh'): @deprecated("2018-06-30", "This API will be deprecated soon as tf.nn.softmax can do the same thing.") def pixel_wise_softmax(x, name='pixel_wise_softmax'): """Return the softmax outputs of images, every pixels have multiple label, the sum of a pixel is 1. + Usually be used for image segmentation. Parameters From 04023ba6095d94df8b486118a5b381b672565c35 Mon Sep 17 00:00:00 2001 From: DEKHTIARJonathan Date: Thu, 19 Apr 2018 10:57:56 +0200 Subject: [PATCH 02/22] Requirements Pinned with range to insure tested versions are used. Range are used to prevent updating requirements all the time. --- docs/requirements.txt | 15 ++++++++------- requirements.txt | 10 +++++----- requirements_dev.txt | 1 + setup.py | 2 ++ tests/requirements.txt | 14 +++++++------- 5 files changed, 23 insertions(+), 19 deletions(-) create mode 100644 requirements_dev.txt diff --git a/docs/requirements.txt b/docs/requirements.txt index 536d08901..bc48f2523 100644 --- a/docs/requirements.txt +++ b/docs/requirements.txt @@ -1,7 +1,8 @@ -matplotlib -pymongo -progressbar2==3.37.0 -scikit-image -scipy -sphinx==1.7.1 -tensorflow==1.5.0 +flake8-docstrings>=1.3,<1.4 +matplotlib>=2.2,<2.3 +pymongo>=3.6,<3.7 +progressbar2>=3.37,<3.38 +scikit-image>=0.13,<0.14 +scipy>=1.0,<1.1 +sphinx>=1.7,<1.8 +tensorflow>=1.7,<1.8 diff --git a/requirements.txt b/requirements.txt index e2be80218..bcce7cccb 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,5 @@ -numpy -progressbar2==3.37.0 -scipy -scikit-image -matplotlib +matplotlib>=2.2,<2.3 +numpy>=1.14,<1.15 +progressbar2>=3.37,<3.38 +scikit-image>=0.13,<0.14 +scipy>=1.0,<1.1 diff --git a/requirements_dev.txt b/requirements_dev.txt new file mode 100644 index 000000000..c1454c776 --- /dev/null +++ b/requirements_dev.txt @@ -0,0 +1 @@ +autopep8>=1.3,<1.4 \ No newline at end of file diff --git a/setup.py b/setup.py index 2993ff57a..351b6ac45 100755 --- a/setup.py +++ b/setup.py @@ -131,6 +131,8 @@ def req_file(filename): # dependencies). You can install these using the following syntax, # $ pip install -e .[test] extras_require={ + 'dev': req_file("requirements_dev.txt"), + 'doc': req_file("docs/requirements.txt"), 'test': req_file("tests/requirements.txt") }, scripts=[ diff --git a/tests/requirements.txt b/tests/requirements.txt index 055c17a11..30ea1b241 100644 --- a/tests/requirements.txt +++ b/tests/requirements.txt @@ -1,7 +1,7 @@ -keras==2.1.5 -pydocstyle==2.1.1 -pytest==3.4.1 -pytest-cache==1.0 -pytest-cov==2.5.1 -pytest-xdist==1.22.2 -yapf==0.20.2 \ No newline at end of file +keras>=2.1,<2.2 +pydocstyle>=2.1,<2.2 +pytest>=3.4,<3.5 +pytest-cache>=1.0,<1.1 +pytest-cov>=2.5,<2.6 +pytest-xdist>=1.22,<1.23 +yapf>=0.20,<0.21 \ No newline at end of file From 9167833a65353b4471acc8a85d6c556e9c8a56f7 Mon Sep 17 00:00:00 2001 From: DEKHTIARJonathan Date: Thu, 19 Apr 2018 13:43:47 +0200 Subject: [PATCH 03/22] setup.cfg file added with PEP8 configuration --- setup.cfg | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 setup.cfg diff --git a/setup.cfg b/setup.cfg new file mode 100644 index 000000000..79a16af7e --- /dev/null +++ b/setup.cfg @@ -0,0 +1,2 @@ +[flake8] +max-line-length = 120 \ No newline at end of file From 3a4cda778c83196c5666ad2259d92c4e862095e6 Mon Sep 17 00:00:00 2001 From: DEKHTIARJonathan Date: Thu, 19 Apr 2018 13:43:58 +0200 Subject: [PATCH 04/22] activation.py refactored --- tensorlayer/activation.py | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/tensorlayer/activation.py b/tensorlayer/activation.py index 1bb10d9d8..9861d7e3e 100644 --- a/tensorlayer/activation.py +++ b/tensorlayer/activation.py @@ -1,6 +1,8 @@ #! /usr/bin/python # -*- coding: utf-8 -*- +"""A file containing various activation functions.""" + import tensorflow as tf from tensorflow.python.util.deprecation import deprecated @@ -16,7 +18,9 @@ ] -@deprecated("2018-06-30", "This API will be deprecated soon as tf.identity can do the same thing.") +@deprecated( + "2018-06-30", + "This API will be deprecated soon as tf.identity can do the same thing.") def identity(x): """Identity activation function. @@ -56,7 +60,11 @@ def ramp(x, v_min=0, v_max=1, name=None): A ``Tensor`` in the same type as ``x``. """ - return tf.clip_by_value(x, clip_value_min=v_min, clip_value_max=v_max, name=name) + return tf.clip_by_value( + x, + clip_value_min=v_min, + clip_value_max=v_max, + name=name) def leaky_relu(x, alpha=0.1, name="lrelu"): @@ -85,7 +93,8 @@ def leaky_relu(x, alpha=0.1, name="lrelu"): References ------------ - - `Rectifier Nonlinearities Improve Neural Network Acoustic Models, Maas et al. (2013) `__ + - `Rectifier Nonlinearities Improve Neural Network Acoustic Models, Maas et al. (2013) + `__ """ # with tf.name_scope(name) as scope: @@ -127,7 +136,8 @@ def _sign_grad(unused_op, grad): def sign(x): # https://github.com/AngusG/tensorflow-xnor-bnn/blob/master/models/binary_net.py#L36 """Sign function. - Clip and binarize tensor using the straight through estimator (STE) for the gradient, usually be used for quantizing values in `Binarized Neural Networks `__. + Clip and binarize tensor using the straight through estimator (STE) for the gradient, usually be used for + quantizing values in `Binarized Neural Networks `__. Parameters ---------- @@ -141,7 +151,8 @@ def sign(x): # https://github.com/AngusG/tensorflow-xnor-bnn/blob/master/models References ----------- - - `AngusG/tensorflow-xnor-bnn `__ + - `AngusG/tensorflow-xnor-bnn + `__ """ with tf.get_default_graph().gradient_override_map({"sign": "QuantizeGrad"}): @@ -151,7 +162,9 @@ def sign(x): # https://github.com/AngusG/tensorflow-xnor-bnn/blob/master/models # if tf.__version__ > "1.7": # @tf.custom_gradient # def sign(x): # https://www.tensorflow.org/versions/master/api_docs/python/tf/custom_gradient?hl=ES#top_of_page -# """Differentiable sign function using sigmoid as the derivation function, see `tf.sign `__ and `tf.custom_gradient `__. +# """Differentiable sign function using sigmoid as the derivation function, +# see `tf.sign `__ and `tf.custom_gradient +# `__. # # Parameters # ---------- @@ -192,7 +205,9 @@ def hard_tanh(x, name='htanh'): return tf.clip_by_value(x, -1, 1, name=name) -@deprecated("2018-06-30", "This API will be deprecated soon as tf.nn.softmax can do the same thing.") +@deprecated( + "2018-06-30", + "This API will be deprecated soon as tf.nn.softmax can do the same thing.") def pixel_wise_softmax(x, name='pixel_wise_softmax'): """Return the softmax outputs of images, every pixels have multiple label, the sum of a pixel is 1. From 1af086329a2d0534ad072ccac52c326a53c2800e Mon Sep 17 00:00:00 2001 From: DEKHTIARJonathan Date: Thu, 19 Apr 2018 14:27:57 +0200 Subject: [PATCH 05/22] docstring fixed - ready for documentation unittest --- docs/index.rst | 1 + setup.cfg | 12 +++++++++++- tensorlayer/activation.py | 28 +++++++++++++++++----------- 3 files changed, 29 insertions(+), 12 deletions(-) diff --git a/docs/index.rst b/docs/index.rst index af0cb8b4a..9234cb5e2 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -54,6 +54,7 @@ method, this part of the documentation is for you. modules/activation modules/models modules/distributed + modules/db Command-line Reference diff --git a/setup.cfg b/setup.cfg index 79a16af7e..a7fd940e6 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,2 +1,12 @@ [flake8] -max-line-length = 120 \ No newline at end of file +max-line-length = 120 +ignore = D301 +exclude = + .git, + venv, + __pycache__, + .pytest_cache, + tensorlayer.egg-info, + build, + dist, + img \ No newline at end of file diff --git a/tensorlayer/activation.py b/tensorlayer/activation.py index 9861d7e3e..3a2c18827 100644 --- a/tensorlayer/activation.py +++ b/tensorlayer/activation.py @@ -75,8 +75,7 @@ def leaky_relu(x, alpha=0.1, name="lrelu"): Parameters ---------- x : Tensor - Support input type ``float``, ``double``, ``int32``, ``int64``, ``uint8``, - ``int16``, or ``int8``. + Support input type ``float``, ``double``, ``int32``, ``int64``, ``uint8``, ``int16``, or ``int8``. alpha : float Slope. name : str @@ -92,9 +91,9 @@ def leaky_relu(x, alpha=0.1, name="lrelu"): A ``Tensor`` in the same type as ``x``. References - ------------ - - `Rectifier Nonlinearities Improve Neural Network Acoustic Models, Maas et al. (2013) - `__ + ---------- + - `Rectifier Nonlinearities Improve Neural Network Acoustic Models, Maas et al. (2013)` + http://web.stanford.edu/~awni/papers/relu_hybrid_icml2013_final.pdf """ # with tf.name_scope(name) as scope: @@ -133,26 +132,33 @@ def _sign_grad(unused_op, grad): return tf.clip_by_value(tf.identity(grad), -1, 1) -def sign(x): # https://github.com/AngusG/tensorflow-xnor-bnn/blob/master/models/binary_net.py#L36 +def sign(x): """Sign function. Clip and binarize tensor using the straight through estimator (STE) for the gradient, usually be used for - quantizing values in `Binarized Neural Networks `__. + quantizing values in `Binarized Neural Networks`: https://arxiv.org/abs/1602.02830. Parameters ---------- x : Tensor input. + Examples + -------- + >>> net = tl.layers.DenseLayer(net, 100, act=lambda x : tl.act.lrelu(x, 0.2), name='dense') + Returns ------- Tensor A ``Tensor`` in the same type as ``x``. References - ----------- - - `AngusG/tensorflow-xnor-bnn - `__ + ---------- + - `Rectifier Nonlinearities Improve Neural Network Acoustic Models, Maas et al. (2013)` + http://web.stanford.edu/~awni/papers/relu_hybrid_icml2013_final.pdf + + - `BinaryNet: Training Deep Neural Networks with Weights and Activations Constrained to +1 or -1, \ + Courbariaux et al. (2016)` https://arxiv.org/abs/1602.02830 """ with tf.get_default_graph().gradient_override_map({"sign": "QuantizeGrad"}): @@ -186,7 +192,7 @@ def sign(x): # https://github.com/AngusG/tensorflow-xnor-bnn/blob/master/models def hard_tanh(x, name='htanh'): """Hard tanh activation function. - Which is a ramp function with low bound of -1 and upper bound of 1, shortcut is ``htanh`. + Which is a ramp function with low bound of -1 and upper bound of 1, shortcut is `htanh`. Parameters ---------- From 7ef9e5375a928f06e2c5a050dd0181faf376ad64 Mon Sep 17 00:00:00 2001 From: DEKHTIARJonathan Date: Thu, 19 Apr 2018 15:03:36 +0200 Subject: [PATCH 06/22] Yapf correction for max_line_length: 120 --- .style.yapf | 2 +- example/tutorial_atari_pong.py | 5 +- .../tutorial_binarynet_cifar10_tfrecord.py | 43 ++-- ...ial_bipedalwalker_a3c_continuous_action.py | 13 +- example/tutorial_cartpole_ac.py | 9 +- example/tutorial_cifar10.py | 28 ++- example/tutorial_cifar10_tfrecord.py | 34 ++- .../tutorial_dorefanet_cifar10_tfrecord.py | 43 ++-- example/tutorial_frozenlake_dqn.py | 10 +- example/tutorial_frozenlake_q_table.py | 3 +- example/tutorial_generate_text.py | 18 +- ...torial_imagenet_inceptionV3_distributed.py | 22 +- example/tutorial_imdb_fasttext.py | 6 +- example/tutorial_inceptionV3_tfslim.py | 9 +- example/tutorial_keras.py | 4 +- example/tutorial_mlp_dropout2.py | 4 +- example/tutorial_mnist.py | 39 +++- example/tutorial_mnist_simple.py | 16 +- example/tutorial_mobilenet.py | 7 +- example/tutorial_ptb_lstm.py | 16 +- example/tutorial_ptb_lstm_state_is_tuple.py | 6 +- example/tutorial_squeezenet.py | 7 +- ...tutorial_ternaryweight_cifar10_tfrecord.py | 43 ++-- example/tutorial_tf_dataset_voc.py | 9 +- example/tutorial_tfrecord.py | 3 +- example/tutorial_tfrecord2.py | 15 +- example/tutorial_tfrecord3.py | 24 +- example/tutorial_vgg16.py | 57 +++-- example/tutorial_vgg19.py | 114 ++++++--- example/tutorial_word2vec_basic.py | 28 ++- tensorlayer/activation.py | 15 +- tensorlayer/cost.py | 25 +- tensorlayer/db.py | 12 +- tensorlayer/distributed.py | 16 +- tensorlayer/files.py | 84 +++++-- tensorlayer/iterate.py | 3 +- tensorlayer/layers/binary.py | 103 ++++++-- tensorlayer/layers/convolution.py | 220 +++++++++++++----- tensorlayer/layers/core.py | 80 +++++-- tensorlayer/layers/merge.py | 3 +- tensorlayer/layers/normalization.py | 30 ++- tensorlayer/layers/padding.py | 3 +- tensorlayer/layers/pooling.py | 63 +++-- tensorlayer/layers/recurrent.py | 75 ++++-- tensorlayer/layers/spatial_transformer.py | 7 +- tensorlayer/layers/special_activation.py | 3 +- tensorlayer/models/mobilenetv1.py | 8 +- tensorlayer/models/squeezenetv1.py | 5 +- tensorlayer/models/vgg16.py | 18 +- tensorlayer/nlp.py | 32 ++- tensorlayer/prepro.py | 174 ++++++++++++-- tensorlayer/utils.py | 7 +- tensorlayer/visualize.py | 20 +- tests/test_layers_convolution.py | 12 +- tests/test_layers_core.py | 7 +- tests/test_layers_recurrent.py | 53 ++++- tests/test_layers_spatial_transformer.py | 6 +- tests/test_layers_time_distributed.py | 6 +- tests/test_mnist_simple.py | 3 +- tests/test_yapf_format.py | 3 +- 60 files changed, 1278 insertions(+), 455 deletions(-) diff --git a/.style.yapf b/.style.yapf index 2c65b7bbd..704f8a396 100644 --- a/.style.yapf +++ b/.style.yapf @@ -1,4 +1,4 @@ [style] based_on_style = pep8 indent_width = 4 -column_limit = 160 +column_limit = 120 diff --git a/example/tutorial_atari_pong.py b/example/tutorial_atari_pong.py index 386afe862..dd04ec309 100644 --- a/example/tutorial_atari_pong.py +++ b/example/tutorial_atari_pong.py @@ -133,7 +133,8 @@ def prepro(I): prev_x = None if reward != 0: - print(('episode %d: game %d took %.5fs, reward: %f' % (episode_number, game_number, time.time() - start_time, reward)), - ('' if reward == -1 else ' !!!!!!!!')) + print(('episode %d: game %d took %.5fs, reward: %f' % + (episode_number, game_number, time.time() - start_time, reward)), ('' + if reward == -1 else ' !!!!!!!!')) start_time = time.time() game_number += 1 diff --git a/example/tutorial_binarynet_cifar10_tfrecord.py b/example/tutorial_binarynet_cifar10_tfrecord.py index 6f7cadc42..9671072cb 100644 --- a/example/tutorial_binarynet_cifar10_tfrecord.py +++ b/example/tutorial_binarynet_cifar10_tfrecord.py @@ -94,7 +94,8 @@ def read_and_decode(filename, is_train=None): reader = tf.TFRecordReader() _, serialized_example = reader.read(filename_queue) features = tf.parse_single_example( - serialized_example, features={ + serialized_example, + features={ 'label': tf.FixedLenFeature([], tf.int64), 'img_raw': tf.FixedLenFeature([], tf.string), }) @@ -147,9 +148,11 @@ def read_and_decode(filename, is_train=None): x_test_, y_test_ = read_and_decode("test.cifar10", False) x_train_batch, y_train_batch = tf.train.shuffle_batch( - [x_train_, y_train_], batch_size=batch_size, capacity=2000, min_after_dequeue=1000, num_threads=32) # set the number of threads here + [x_train_, y_train_], batch_size=batch_size, capacity=2000, min_after_dequeue=1000, + num_threads=32) # set the number of threads here # for testing, uses batch instead of shuffle_batch - x_test_batch, y_test_batch = tf.train.batch([x_test_, y_test_], batch_size=batch_size, capacity=50000, num_threads=32) + x_test_batch, y_test_batch = tf.train.batch( + [x_test_, y_test_], batch_size=batch_size, capacity=50000, num_threads=32) def model(x_crop, y_, reuse): """ For more simplified CNN APIs, check tensorlayer.org """ @@ -161,16 +164,24 @@ def model(x_crop, y_, reuse): net = tl.layers.Conv2d(net, 64, (5, 5), (1, 1), act=tf.nn.relu, padding='SAME', W_init=W_init, name='cnn1') net = tl.layers.SignLayer(net) net = tl.layers.MaxPool2d(net, (3, 3), (2, 2), padding='SAME', name='pool1') - net = tl.layers.LocalResponseNormLayer(net, depth_radius=4, bias=1.0, alpha=0.001 / 9.0, beta=0.75, name='norm1') - net = tl.layers.BinaryConv2d(net, 64, (5, 5), (1, 1), act=tf.nn.relu, padding='SAME', W_init=W_init, name='cnn2') - net = tl.layers.LocalResponseNormLayer(net, depth_radius=4, bias=1.0, alpha=0.001 / 9.0, beta=0.75, name='norm2') + net = tl.layers.LocalResponseNormLayer( + net, depth_radius=4, bias=1.0, alpha=0.001 / 9.0, beta=0.75, name='norm1') + net = tl.layers.BinaryConv2d( + net, 64, (5, 5), (1, 1), act=tf.nn.relu, padding='SAME', W_init=W_init, name='cnn2') + net = tl.layers.LocalResponseNormLayer( + net, depth_radius=4, bias=1.0, alpha=0.001 / 9.0, beta=0.75, name='norm2') net = tl.layers.MaxPool2d(net, (3, 3), (2, 2), padding='SAME', name='pool2') net = tl.layers.FlattenLayer(net, name='flatten') # output: (batch_size, 2304) net = tl.layers.SignLayer(net) - net = tl.layers.BinaryDenseLayer(net, n_units=384, act=tf.nn.relu, W_init=W_init2, b_init=b_init2, name='d1relu') # output: (batch_size, 384) + net = tl.layers.BinaryDenseLayer( + net, n_units=384, act=tf.nn.relu, W_init=W_init2, b_init=b_init2, + name='d1relu') # output: (batch_size, 384) net = tl.layers.SignLayer(net) - net = tl.layers.BinaryDenseLayer(net, n_units=192, act=tf.nn.relu, W_init=W_init2, b_init=b_init2, name='d2relu') # output: (batch_size, 192) - net = tl.layers.DenseLayer(net, n_units=10, act=tf.identity, W_init=W_init2, name='output') # output: (batch_size, 10) + net = tl.layers.BinaryDenseLayer( + net, n_units=192, act=tf.nn.relu, W_init=W_init2, b_init=b_init2, + name='d2relu') # output: (batch_size, 192) + net = tl.layers.DenseLayer( + net, n_units=10, act=tf.identity, W_init=W_init2, name='output') # output: (batch_size, 10) y = net.outputs ce = tl.cost.cross_entropy(y, y_, name='cost') @@ -201,9 +212,14 @@ def model_batch_norm(x_crop, y_, reuse, is_train): net = tl.layers.BatchNormLayer(net, is_train, act=tf.nn.relu, name='batch2') net = tl.layers.MaxPool2d(net, (3, 3), (2, 2), padding='SAME', name='pool2') net = tl.layers.FlattenLayer(net, name='flatten') # output: (batch_size, 2304) - net = tl.layers.DenseLayer(net, n_units=384, act=tf.nn.relu, W_init=W_init2, b_init=b_init2, name='d1relu') # output: (batch_size, 384) - net = tl.layers.DenseLayer(net, n_units=192, act=tf.nn.relu, W_init=W_init2, b_init=b_init2, name='d2relu') # output: (batch_size, 192) - net = tl.layers.DenseLayer(net, n_units=10, act=tf.identity, W_init=W_init2, name='output') # output: (batch_size, 10) + net = tl.layers.DenseLayer( + net, n_units=384, act=tf.nn.relu, W_init=W_init2, b_init=b_init2, + name='d1relu') # output: (batch_size, 384) + net = tl.layers.DenseLayer( + net, n_units=192, act=tf.nn.relu, W_init=W_init2, b_init=b_init2, + name='d2relu') # output: (batch_size, 192) + net = tl.layers.DenseLayer( + net, n_units=10, act=tf.identity, W_init=W_init2, name='output') # output: (batch_size, 10) y = net.outputs ce = tl.cost.cross_entropy(y, y_, name='cost') @@ -273,7 +289,8 @@ def model_batch_norm(x_crop, y_, reuse, is_train): n_batch += 1 if epoch + 1 == 1 or (epoch + 1) % print_freq == 0: - print("Epoch %d : Step %d-%d of %d took %fs" % (epoch, step, step + n_step_epoch, n_step, time.time() - start_time)) + print("Epoch %d : Step %d-%d of %d took %fs" % (epoch, step, step + n_step_epoch, n_step, + time.time() - start_time)) print(" train loss: %f" % (train_loss / n_batch)) print(" train acc: %f" % (train_acc / n_batch)) diff --git a/example/tutorial_bipedalwalker_a3c_continuous_action.py b/example/tutorial_bipedalwalker_a3c_continuous_action.py index d112c3bd4..be3404f2a 100644 --- a/example/tutorial_bipedalwalker_a3c_continuous_action.py +++ b/example/tutorial_bipedalwalker_a3c_continuous_action.py @@ -144,7 +144,8 @@ def _build_net(self): self.v = v.outputs def update_global(self, feed_dict): # run by a local - _, _, t = sess.run([self.update_a_op, self.update_c_op, self.test], feed_dict) # local grads applies to global net + _, _, t = sess.run([self.update_a_op, self.update_c_op, self.test], + feed_dict) # local grads applies to global net return t def pull_global(self): # run by a local @@ -156,7 +157,12 @@ def choose_action(self, s): # run by a local def save_ckpt(self): tl.files.exists_or_mkdir(self.scope) - tl.files.save_ckpt(sess=sess, mode_name='model.ckpt', var_list=self.a_params + self.c_params, save_dir=self.scope, printable=True) + tl.files.save_ckpt( + sess=sess, + mode_name='model.ckpt', + var_list=self.a_params + self.c_params, + save_dir=self.scope, + printable=True) def load_ckpt(self): tl.files.load_ckpt(sess=sess, var_list=self.a_params + self.c_params, save_dir=self.scope, printable=True) @@ -202,7 +208,8 @@ def work(self): buffer_v_target.append(v_s_) buffer_v_target.reverse() - buffer_s, buffer_a, buffer_v_target = np.vstack(buffer_s), np.vstack(buffer_a), np.vstack(buffer_v_target) + buffer_s, buffer_a, buffer_v_target = np.vstack(buffer_s), np.vstack(buffer_a), np.vstack( + buffer_v_target) feed_dict = { self.AC.s: buffer_s, self.AC.a_his: buffer_a, diff --git a/example/tutorial_cartpole_ac.py b/example/tutorial_cartpole_ac.py index b39cdbd3e..f86a7ca9a 100644 --- a/example/tutorial_cartpole_ac.py +++ b/example/tutorial_cartpole_ac.py @@ -85,7 +85,8 @@ def __init__(self, sess, n_features, n_actions, lr=0.001): # Hao Dong with tf.variable_scope('loss'): - self.exp_v = tl.rein.cross_entropy_reward_loss(logits=self.acts_logits, actions=self.a, rewards=self.td_error, name='actor_weighted_loss') + self.exp_v = tl.rein.cross_entropy_reward_loss( + logits=self.acts_logits, actions=self.a, rewards=self.td_error, name='actor_weighted_loss') with tf.variable_scope('train'): self.train_op = tf.train.AdamOptimizer(lr).minimize(self.exp_v) @@ -143,7 +144,8 @@ def learn(self, s, r, s_): sess = tf.Session() actor = Actor(sess, n_features=N_F, n_actions=N_A, lr=LR_A) -critic = Critic(sess, n_features=N_F, lr=LR_C) # we need a good teacher, so the teacher should learn faster than the actor +critic = Critic( + sess, n_features=N_F, lr=LR_C) # we need a good teacher, so the teacher should learn faster than the actor tl.layers.initialize_global_variables(sess) @@ -187,7 +189,8 @@ def learn(self, s, r, s_): running_reward = running_reward * 0.95 + ep_rs_sum * 0.05 # start rending if running_reward greater than a threshold # if running_reward > DISPLAY_REWARD_THRESHOLD: RENDER = True - print("Episode: %d reward: %f running_reward %f took: %.5f" % (i_episode, ep_rs_sum, running_reward, time.time() - episode_time)) + print("Episode: %d reward: %f running_reward %f took: %.5f" % (i_episode, ep_rs_sum, running_reward, + time.time() - episode_time)) # Early Stopping for quick check if t >= MAX_EP_STEPS: diff --git a/example/tutorial_cifar10.py b/example/tutorial_cifar10.py index 5ffd5dfff..88a1f1c72 100644 --- a/example/tutorial_cifar10.py +++ b/example/tutorial_cifar10.py @@ -44,9 +44,15 @@ def model(x, y_, reuse): # net = PoolLayer(net, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1], # padding='SAME', pool = tf.nn.max_pool, name ='pool2') # output: (batch_size, 6, 6, 64) net = FlattenLayer(net, name='flatten') # output: (batch_size, 2304) - net = DenseLayer(net, n_units=384, act=tf.nn.relu, W_init=W_init2, b_init=b_init2, name='d1relu') # output: (batch_size, 384) - net = DenseLayer(net, n_units=192, act=tf.nn.relu, W_init=W_init2, b_init=b_init2, name='d2relu') # output: (batch_size, 192) - net = DenseLayer(net, n_units=10, act=tf.identity, W_init=tf.truncated_normal_initializer(stddev=1 / 192.0), name='output') # output: (batch_size, 10) + net = DenseLayer( + net, n_units=384, act=tf.nn.relu, W_init=W_init2, b_init=b_init2, + name='d1relu') # output: (batch_size, 384) + net = DenseLayer( + net, n_units=192, act=tf.nn.relu, W_init=W_init2, b_init=b_init2, + name='d2relu') # output: (batch_size, 192) + net = DenseLayer( + net, n_units=10, act=tf.identity, W_init=tf.truncated_normal_initializer(stddev=1 / 192.0), + name='output') # output: (batch_size, 10) y = net.outputs ce = tl.cost.cross_entropy(y, y_, name='cost') @@ -89,9 +95,15 @@ def model_batch_norm(x, y_, reuse, is_train): # padding='SAME', pool = tf.nn.max_pool, name ='pool2') # output: (batch_size, 6, 6, 64) net = FlattenLayer(net, name='flatten') # output: (batch_size, 2304) - net = DenseLayer(net, n_units=384, act=tf.nn.relu, W_init=W_init2, b_init=b_init2, name='d1relu') # output: (batch_size, 384) - net = DenseLayer(net, n_units=192, act=tf.nn.relu, W_init=W_init2, b_init=b_init2, name='d2relu') # output: (batch_size, 192) - net = DenseLayer(net, n_units=10, act=tf.identity, W_init=tf.truncated_normal_initializer(stddev=1 / 192.0), name='output') # output: (batch_size, 10) + net = DenseLayer( + net, n_units=384, act=tf.nn.relu, W_init=W_init2, b_init=b_init2, + name='d1relu') # output: (batch_size, 384) + net = DenseLayer( + net, n_units=192, act=tf.nn.relu, W_init=W_init2, b_init=b_init2, + name='d2relu') # output: (batch_size, 192) + net = DenseLayer( + net, n_units=10, act=tf.identity, W_init=tf.truncated_normal_initializer(stddev=1 / 192.0), + name='output') # output: (batch_size, 10) y = net.outputs ce = tl.cost.cross_entropy(y, y_, name='cost') @@ -156,7 +168,9 @@ def distort_fn(x, is_train=False): batch_size = 128 train_params = network.all_params -train_op = tf.train.AdamOptimizer(learning_rate, beta1=0.9, beta2=0.999, epsilon=1e-08, use_locking=False).minimize(cost, var_list=train_params) +train_op = tf.train.AdamOptimizer( + learning_rate, beta1=0.9, beta2=0.999, epsilon=1e-08, use_locking=False).minimize( + cost, var_list=train_params) tl.layers.initialize_global_variables(sess) diff --git a/example/tutorial_cifar10_tfrecord.py b/example/tutorial_cifar10_tfrecord.py index 7f15c2e3a..01b37bfbf 100644 --- a/example/tutorial_cifar10_tfrecord.py +++ b/example/tutorial_cifar10_tfrecord.py @@ -95,7 +95,8 @@ def read_and_decode(filename, is_train=None): reader = tf.TFRecordReader() _, serialized_example = reader.read(filename_queue) features = tf.parse_single_example( - serialized_example, features={ + serialized_example, + features={ 'label': tf.FixedLenFeature([], tf.int64), 'img_raw': tf.FixedLenFeature([], tf.string), }) @@ -175,9 +176,11 @@ def read_and_decode(filename, is_train=None): x_test_, y_test_ = read_and_decode("test.cifar10", False) x_train_batch, y_train_batch = tf.train.shuffle_batch( - [x_train_, y_train_], batch_size=batch_size, capacity=2000, min_after_dequeue=1000, num_threads=32) # set the number of threads here + [x_train_, y_train_], batch_size=batch_size, capacity=2000, min_after_dequeue=1000, + num_threads=32) # set the number of threads here # for testing, uses batch instead of shuffle_batch - x_test_batch, y_test_batch = tf.train.batch([x_test_, y_test_], batch_size=batch_size, capacity=50000, num_threads=32) + x_test_batch, y_test_batch = tf.train.batch( + [x_test_, y_test_], batch_size=batch_size, capacity=50000, num_threads=32) def model(x_crop, y_, reuse): """ For more simplified CNN APIs, check tensorlayer.org """ @@ -208,9 +211,14 @@ def model(x_crop, y_, reuse): # net = PoolLayer(net, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1], # padding='SAME', pool = tf.nn.max_pool, name ='pool2') # output: (batch_size, 6, 6, 64) net = FlattenLayer(net, name='flatten') # output: (batch_size, 2304) - net = DenseLayer(net, n_units=384, act=tf.nn.relu, W_init=W_init2, b_init=b_init2, name='d1relu') # output: (batch_size, 384) - net = DenseLayer(net, n_units=192, act=tf.nn.relu, W_init=W_init2, b_init=b_init2, name='d2relu') # output: (batch_size, 192) - net = DenseLayer(net, n_units=10, act=tf.identity, W_init=W_init2, name='output') # output: (batch_size, 10) + net = DenseLayer( + net, n_units=384, act=tf.nn.relu, W_init=W_init2, b_init=b_init2, + name='d1relu') # output: (batch_size, 384) + net = DenseLayer( + net, n_units=192, act=tf.nn.relu, W_init=W_init2, b_init=b_init2, + name='d2relu') # output: (batch_size, 192) + net = DenseLayer( + net, n_units=10, act=tf.identity, W_init=W_init2, name='output') # output: (batch_size, 10) y = net.outputs ce = tl.cost.cross_entropy(y, y_, name='cost') @@ -253,9 +261,14 @@ def model_batch_norm(x_crop, y_, reuse, is_train): # padding='SAME', pool = tf.nn.max_pool, name ='pool2') # output: (batch_size, 6, 6, 64) net = FlattenLayer(net, name='flatten') # output: (batch_size, 2304) - net = DenseLayer(net, n_units=384, act=tf.nn.relu, W_init=W_init2, b_init=b_init2, name='d1relu') # output: (batch_size, 384) - net = DenseLayer(net, n_units=192, act=tf.nn.relu, W_init=W_init2, b_init=b_init2, name='d2relu') # output: (batch_size, 192) - net = DenseLayer(net, n_units=10, act=tf.identity, W_init=W_init2, name='output') # output: (batch_size, 10) + net = DenseLayer( + net, n_units=384, act=tf.nn.relu, W_init=W_init2, b_init=b_init2, + name='d1relu') # output: (batch_size, 384) + net = DenseLayer( + net, n_units=192, act=tf.nn.relu, W_init=W_init2, b_init=b_init2, + name='d2relu') # output: (batch_size, 192) + net = DenseLayer( + net, n_units=10, act=tf.identity, W_init=W_init2, name='output') # output: (batch_size, 10) y = net.outputs ce = tl.cost.cross_entropy(y, y_, name='cost') @@ -325,7 +338,8 @@ def model_batch_norm(x_crop, y_, reuse, is_train): n_batch += 1 if epoch + 1 == 1 or (epoch + 1) % print_freq == 0: - print("Epoch %d : Step %d-%d of %d took %fs" % (epoch, step, step + n_step_epoch, n_step, time.time() - start_time)) + print("Epoch %d : Step %d-%d of %d took %fs" % (epoch, step, step + n_step_epoch, n_step, + time.time() - start_time)) print(" train loss: %f" % (train_loss / n_batch)) print(" train acc: %f" % (train_acc / n_batch)) diff --git a/example/tutorial_dorefanet_cifar10_tfrecord.py b/example/tutorial_dorefanet_cifar10_tfrecord.py index 879913c61..742c8ac5d 100644 --- a/example/tutorial_dorefanet_cifar10_tfrecord.py +++ b/example/tutorial_dorefanet_cifar10_tfrecord.py @@ -94,7 +94,8 @@ def read_and_decode(filename, is_train=None): reader = tf.TFRecordReader() _, serialized_example = reader.read(filename_queue) features = tf.parse_single_example( - serialized_example, features={ + serialized_example, + features={ 'label': tf.FixedLenFeature([], tf.int64), 'img_raw': tf.FixedLenFeature([], tf.string), }) @@ -147,9 +148,11 @@ def read_and_decode(filename, is_train=None): x_test_, y_test_ = read_and_decode("test.cifar10", False) x_train_batch, y_train_batch = tf.train.shuffle_batch( - [x_train_, y_train_], batch_size=batch_size, capacity=2000, min_after_dequeue=1000, num_threads=32) # set the number of threads here + [x_train_, y_train_], batch_size=batch_size, capacity=2000, min_after_dequeue=1000, + num_threads=32) # set the number of threads here # for testing, uses batch instead of shuffle_batch - x_test_batch, y_test_batch = tf.train.batch([x_test_, y_test_], batch_size=batch_size, capacity=50000, num_threads=32) + x_test_batch, y_test_batch = tf.train.batch( + [x_test_, y_test_], batch_size=batch_size, capacity=50000, num_threads=32) def model(x_crop, y_, reuse): """ For more simplified CNN APIs, check tensorlayer.org """ @@ -160,14 +163,22 @@ def model(x_crop, y_, reuse): net = tl.layers.InputLayer(x_crop, name='input') net = tl.layers.Conv2d(net, 64, (5, 5), (1, 1), act=tf.nn.relu, padding='SAME', W_init=W_init, name='cnn1') net = tl.layers.MaxPool2d(net, (3, 3), (2, 2), padding='SAME', name='pool1') - net = tl.layers.LocalResponseNormLayer(net, depth_radius=4, bias=1.0, alpha=0.001 / 9.0, beta=0.75, name='norm1') - net = tl.layers.DorefaConv2d(net, 1, 3, 64, (5, 5), (1, 1), tf.nn.relu, padding='SAME', W_init=W_init, name='cnn2') - net = tl.layers.LocalResponseNormLayer(net, depth_radius=4, bias=1.0, alpha=0.001 / 9.0, beta=0.75, name='norm2') + net = tl.layers.LocalResponseNormLayer( + net, depth_radius=4, bias=1.0, alpha=0.001 / 9.0, beta=0.75, name='norm1') + net = tl.layers.DorefaConv2d( + net, 1, 3, 64, (5, 5), (1, 1), tf.nn.relu, padding='SAME', W_init=W_init, name='cnn2') + net = tl.layers.LocalResponseNormLayer( + net, depth_radius=4, bias=1.0, alpha=0.001 / 9.0, beta=0.75, name='norm2') net = tl.layers.MaxPool2d(net, (3, 3), (2, 2), padding='SAME', name='pool2') net = tl.layers.FlattenLayer(net, name='flatten') # output: (batch_size, 2304) - net = tl.layers.DorefaDenseLayer(net, 1, 3, 384, act=tf.nn.relu, W_init=W_init2, b_init=b_init2, name='d1relu') # output: (batch_size, 384) - net = tl.layers.DorefaDenseLayer(net, 1, 3, 192, act=tf.nn.relu, W_init=W_init2, b_init=b_init2, name='d2relu') # output: (batch_size, 192) - net = tl.layers.DenseLayer(net, n_units=10, act=tf.identity, W_init=W_init2, name='output') # output: (batch_size, 10) + net = tl.layers.DorefaDenseLayer( + net, 1, 3, 384, act=tf.nn.relu, W_init=W_init2, b_init=b_init2, + name='d1relu') # output: (batch_size, 384) + net = tl.layers.DorefaDenseLayer( + net, 1, 3, 192, act=tf.nn.relu, W_init=W_init2, b_init=b_init2, + name='d2relu') # output: (batch_size, 192) + net = tl.layers.DenseLayer( + net, n_units=10, act=tf.identity, W_init=W_init2, name='output') # output: (batch_size, 10) y = net.outputs ce = tl.cost.cross_entropy(y, y_, name='cost') @@ -197,9 +208,14 @@ def model_batch_norm(x_crop, y_, reuse, is_train): net = tl.layers.BatchNormLayer(net, is_train, act=tf.nn.relu, name='batch2') net = tl.layers.MaxPool2d(net, (3, 3), (2, 2), padding='SAME', name='pool2') net = tl.layers.FlattenLayer(net, name='flatten') # output: (batch_size, 2304) - net = tl.layers.DenseLayer(net, n_units=384, act=tf.nn.relu, W_init=W_init2, b_init=b_init2, name='d1relu') # output: (batch_size, 384) - net = tl.layers.DenseLayer(net, n_units=192, act=tf.nn.relu, W_init=W_init2, b_init=b_init2, name='d2relu') # output: (batch_size, 192) - net = tl.layers.DenseLayer(net, n_units=10, act=tf.identity, W_init=W_init2, name='output') # output: (batch_size, 10) + net = tl.layers.DenseLayer( + net, n_units=384, act=tf.nn.relu, W_init=W_init2, b_init=b_init2, + name='d1relu') # output: (batch_size, 384) + net = tl.layers.DenseLayer( + net, n_units=192, act=tf.nn.relu, W_init=W_init2, b_init=b_init2, + name='d2relu') # output: (batch_size, 192) + net = tl.layers.DenseLayer( + net, n_units=10, act=tf.identity, W_init=W_init2, name='output') # output: (batch_size, 10) y = net.outputs ce = tl.cost.cross_entropy(y, y_, name='cost') # L2 for the MLP, without this, the accuracy will be reduced by 15%. @@ -268,7 +284,8 @@ def model_batch_norm(x_crop, y_, reuse, is_train): n_batch += 1 if epoch + 1 == 1 or (epoch + 1) % print_freq == 0: - print("Epoch %d : Step %d-%d of %d took %fs" % (epoch, step, step + n_step_epoch, n_step, time.time() - start_time)) + print("Epoch %d : Step %d-%d of %d took %fs" % (epoch, step, step + n_step_epoch, n_step, + time.time() - start_time)) print(" train loss: %f" % (train_loss / n_batch)) print(" train acc: %f" % (train_acc / n_batch)) diff --git a/example/tutorial_frozenlake_dqn.py b/example/tutorial_frozenlake_dqn.py index d564fe6c5..b2610fd40 100644 --- a/example/tutorial_frozenlake_dqn.py +++ b/example/tutorial_frozenlake_dqn.py @@ -48,9 +48,12 @@ def to_one_hot(i, n_classes=None): # 4x4 grid can be represented by one-hot vector with 16 integers. inputs = tf.placeholder(shape=[1, 16], dtype=tf.float32) net = InputLayer(inputs, name='observation') -net = DenseLayer(net, n_units=4, act=tf.identity, W_init=tf.random_uniform_initializer(0, 0.01), b_init=None, name='q_a_s') +net = DenseLayer( + net, n_units=4, act=tf.identity, W_init=tf.random_uniform_initializer(0, 0.01), b_init=None, name='q_a_s') y = net.outputs # action-value / rewards of 4 actions -predict = tf.argmax(y, 1) # chose action greedily with reward. in Q-Learning, policy is greedy, so we use "max" to select the next action. +predict = tf.argmax( + y, + 1) # chose action greedily with reward. in Q-Learning, policy is greedy, so we use "max" to select the next action. ## Below we obtain the loss by taking the sum of squares difference between the target and prediction Q values. nextQ = tf.placeholder(shape=[1, 4], dtype=tf.float32) @@ -99,5 +102,6 @@ def to_one_hot(i, n_classes=None): ## Note that, the rewards here with random action running_reward = rAll if running_reward is None else running_reward * 0.99 + rAll * 0.01 - print("Episode [%d/%d] sum reward:%f running reward:%f took:%.5fs %s" % (i, num_episodes, rAll, running_reward, time.time() - episode_time, '' + print("Episode [%d/%d] sum reward:%f running reward:%f took:%.5fs %s" % (i, num_episodes, rAll, running_reward, + time.time() - episode_time, '' if rAll == 0 else ' !!!!!!!!')) diff --git a/example/tutorial_frozenlake_q_table.py b/example/tutorial_frozenlake_q_table.py index 4de85a6a1..0375e67fb 100644 --- a/example/tutorial_frozenlake_q_table.py +++ b/example/tutorial_frozenlake_q_table.py @@ -52,7 +52,8 @@ break rList.append(rAll) running_reward = r if running_reward is None else running_reward * 0.99 + r * 0.01 - print("Episode [%d/%d] sum reward:%f running reward:%f took:%.5fs %s" % (i, num_episodes, rAll, running_reward, time.time() - episode_time, '' + print("Episode [%d/%d] sum reward:%f running reward:%f took:%.5fs %s" % (i, num_episodes, rAll, running_reward, + time.time() - episode_time, '' if rAll == 0 else ' !!!!!!!!')) print("Final Q-Table Values:/n %s" % Q) diff --git a/example/tutorial_generate_text.py b/example/tutorial_generate_text.py index ae8122291..58e884415 100644 --- a/example/tutorial_generate_text.py +++ b/example/tutorial_generate_text.py @@ -155,7 +155,8 @@ def main_restore_embedding_layer(): x = tf.placeholder(tf.int32, shape=[batch_size]) - emb_net = tl.layers.EmbeddingInputlayer(inputs=x, vocabulary_size=vocabulary_size, embedding_size=embedding_size, name='embedding_layer') + emb_net = tl.layers.EmbeddingInputlayer( + inputs=x, vocabulary_size=vocabulary_size, embedding_size=embedding_size, name='embedding_layer') # sess.run(tf.initialize_all_variables()) tl.layers.initialize_global_variables(sess) @@ -231,7 +232,8 @@ def inference(x, is_train, sequence_length, reuse=None): print("\nsequence_length: %d, is_train: %s, reuse: %s" % (sequence_length, is_train, reuse)) rnn_init = tf.random_uniform_initializer(-init_scale, init_scale) with tf.variable_scope("model", reuse=reuse): - network = EmbeddingInputlayer(inputs=x, vocabulary_size=vocab_size, embedding_size=hidden_size, E_init=rnn_init, name='embedding') + network = EmbeddingInputlayer( + inputs=x, vocabulary_size=vocab_size, embedding_size=hidden_size, E_init=rnn_init, name='embedding') network = RNNLayer( network, cell_fn=tf.contrib.rnn.BasicLSTMCell, @@ -246,7 +248,8 @@ def inference(x, is_train, sequence_length, reuse=None): return_seq_2d=True, name='lstm1') lstm1 = network - network = DenseLayer(network, n_units=vocab_size, W_init=rnn_init, b_init=rnn_init, act=tf.identity, name='output') + network = DenseLayer( + network, n_units=vocab_size, W_init=rnn_init, b_init=rnn_init, act=tf.identity, name='output') return network, lstm1 # Inference for Training @@ -267,7 +270,8 @@ def loss_fn(outputs, targets, batch_size, sequence_length): # n_examples = batch_size * sequence_length # so # cost is the averaged cost of each mini-batch (concurrent process). - loss = tf.contrib.legacy_seq2seq.sequence_loss_by_example([outputs], [tf.reshape(targets, [-1])], [tf.ones([batch_size * sequence_length])]) + loss = tf.contrib.legacy_seq2seq.sequence_loss_by_example([outputs], [tf.reshape(targets, [-1])], + [tf.ones([batch_size * sequence_length])]) cost = tf.reduce_sum(loss) / batch_size return cost @@ -307,7 +311,8 @@ def loss_fn(outputs, targets, batch_size, sequence_length): state1 = tl.layers.initialize_rnn_state(lstm1.initial_state) for step, (x, y) in enumerate(tl.iterate.ptb_iterator(train_data, batch_size, sequence_length)): _cost, state1, _ = sess.run( - [cost, lstm1.final_state, train_op], feed_dict={ + [cost, lstm1.final_state, train_op], + feed_dict={ input_data: x, targets: y, lstm1.initial_state: state1, @@ -344,7 +349,8 @@ def loss_fn(outputs, targets, batch_size, sequence_length): for _ in range(print_length): a_id = np.asarray(a_id).reshape(1, 1) out, state1 = sess.run( - [y_soft, lstm1_test.final_state], feed_dict={ + [y_soft, lstm1_test.final_state], + feed_dict={ input_data_test: a_id, lstm1_test.initial_state: state1, }) diff --git a/example/tutorial_imagenet_inceptionV3_distributed.py b/example/tutorial_imagenet_inceptionV3_distributed.py index 37b548ed4..d43df39f4 100644 --- a/example/tutorial_imagenet_inceptionV3_distributed.py +++ b/example/tutorial_imagenet_inceptionV3_distributed.py @@ -131,7 +131,8 @@ def _parse_example_fn(line): return image_bytes, one_hot_labels def _map_fn(example_serialized): - image_bytes, one_hot_labels = tf.py_func(_parse_example_fn, [example_serialized], [tf.string, tf.float32], stateful=False) + image_bytes, one_hot_labels = tf.py_func( + _parse_example_fn, [example_serialized], [tf.string, tf.float32], stateful=False) image = tf.image.decode_jpeg(image_bytes, channels=3) image = tf.image.resize_images(image, size=[image_size, image_size]) @@ -158,10 +159,13 @@ def build_network(image_input, num_classes=1001, is_training=False): net_in = tl.layers.InputLayer(image_input, name='input_layer') with slim.arg_scope(inception_v3_arg_scope()): network = tl.layers.SlimNetsLayer( - prev_layer=net_in, slim_layer=inception_v3, slim_args={ + prev_layer=net_in, + slim_layer=inception_v3, + slim_args={ 'num_classes': num_classes, 'is_training': is_training - }, name='InceptionV3') + }, + name='InceptionV3') predictions = tf.nn.sigmoid(network.outputs, name='Predictions') return network, predictions @@ -363,14 +367,19 @@ def run_worker(task_spec, checkpoints_path, batch_size=32, epochs=10): # metrics tf.summary.scalar('learning_rate/value', learning_rate) tf.summary.scalar('loss/logits', loss) - _, metrics_average_ops, metrics_ops = calculate_metrics(predicted_batch=predictions, real_batch=one_hot_classes, is_training=True) + _, metrics_average_ops, metrics_ops = calculate_metrics( + predicted_batch=predictions, real_batch=one_hot_classes, is_training=True) with tf.control_dependencies([train_op]): train_op = tf.group(metrics_average_ops) # start training hooks = [StopAtStepHook(last_step=steps_per_epoch * epochs)] with tl.distributed.DistributedSession( - task_spec=task_spec, hooks=hooks, checkpoint_dir=checkpoints_path, save_summaries_secs=None, save_summaries_steps=300, + task_spec=task_spec, + hooks=hooks, + checkpoint_dir=checkpoints_path, + save_summaries_secs=None, + save_summaries_steps=300, save_checkpoint_secs=60 * 60) as sess: # print network information if task_spec is None or task_spec.is_master(): @@ -392,7 +401,8 @@ def run_worker(task_spec, checkpoints_path, batch_size=32, epochs=10): current_epoch = '{:.3f}'.format(float(step) / steps_per_epoch) max_steps = epochs * steps_per_epoch m = 'Epoch: {}/{} Steps: {}/{} Loss: {} Learning rate: {} Metrics: {}' - logging.info(m.format(current_epoch, epochs, step, max_steps, loss_val, learning_rate_val, metrics)) + logging.info( + m.format(current_epoch, epochs, step, max_steps, loss_val, learning_rate_val, metrics)) except OutOfRangeError: pass diff --git a/example/tutorial_imdb_fasttext.py b/example/tutorial_imdb_fasttext.py index 182e063e3..847815405 100644 --- a/example/tutorial_imdb_fasttext.py +++ b/example/tutorial_imdb_fasttext.py @@ -136,7 +136,8 @@ def train_test_and_save_model(): print('Epoch %d/%d' % (epoch + 1, N_EPOCH)) for X_batch, y_batch in tl.iterate.minibatches(X_train, y_train, batch_size=BATCH_SIZE, shuffle=True): sess.run( - classifier.train_op, feed_dict={ + classifier.train_op, + feed_dict={ classifier.inputs: tl.prepro.pad_sequences(X_batch), classifier.labels: y_batch, }) @@ -144,7 +145,8 @@ def train_test_and_save_model(): print(" took %.5fs" % (time.time() - start_time)) test_accuracy = sess.run( - classifier.accuracy, feed_dict={ + classifier.accuracy, + feed_dict={ classifier.inputs: tl.prepro.pad_sequences(X_test), classifier.labels: y_test, }) diff --git a/example/tutorial_inceptionV3_tfslim.py b/example/tutorial_inceptionV3_tfslim.py index d704776e7..31fffb78b 100644 --- a/example/tutorial_inceptionV3_tfslim.py +++ b/example/tutorial_inceptionV3_tfslim.py @@ -41,7 +41,8 @@ try: from data.imagenet_classes import * except Exception as e: - raise Exception("{} / download the file from: https://github.com/zsdonghao/tensorlayer/tree/master/example/data".format(e)) + raise Exception( + "{} / download the file from: https://github.com/zsdonghao/tensorlayer/tree/master/example/data".format(e)) def load_image(path): @@ -122,7 +123,8 @@ def print_prob(prob): saver = tf.train.Saver() if not os.path.isfile("inception_v3.ckpt"): - raise Exception("Please download inception_v3 ckpt from : https://github.com/tensorflow/models/tree/master/research/slim") + raise Exception( + "Please download inception_v3 ckpt from : https://github.com/tensorflow/models/tree/master/research/slim") try: # TF12+ saver.restore(sess, "./inception_v3.ckpt") @@ -132,7 +134,8 @@ def print_prob(prob): y = network.outputs probs = tf.nn.softmax(y) -img1 = load_image("data/puzzle.jpeg") # test data in github: https://github.com/zsdonghao/tensorlayer/tree/master/example/data +img1 = load_image( + "data/puzzle.jpeg") # test data in github: https://github.com/zsdonghao/tensorlayer/tree/master/example/data img1 = img1.reshape((1, 299, 299, 3)) prob = sess.run(probs, feed_dict={x: img1}) # the 1st time need time to compile diff --git a/example/tutorial_keras.py b/example/tutorial_keras.py index 481d211a8..be3bd5d64 100644 --- a/example/tutorial_keras.py +++ b/example/tutorial_keras.py @@ -43,7 +43,9 @@ def keras_block(x): learning_rate = 0.0001 train_params = network.all_params -train_op = tf.train.AdamOptimizer(learning_rate, beta1=0.9, beta2=0.999, epsilon=1e-08, use_locking=False).minimize(cost, var_list=train_params) +train_op = tf.train.AdamOptimizer( + learning_rate, beta1=0.9, beta2=0.999, epsilon=1e-08, use_locking=False).minimize( + cost, var_list=train_params) tl.layers.initialize_global_variables(sess) diff --git a/example/tutorial_mlp_dropout2.py b/example/tutorial_mlp_dropout2.py index 5ed028cf2..1291a990e 100644 --- a/example/tutorial_mlp_dropout2.py +++ b/example/tutorial_mlp_dropout2.py @@ -41,7 +41,9 @@ def mlp(x, is_train=True, reuse=False): # define the optimizer train_params = tl.layers.get_variables_with_name('MLP', train_only=True, printable=False) -train_op = tf.train.AdamOptimizer(learning_rate=0.0001, beta1=0.9, beta2=0.999, epsilon=1e-08, use_locking=False).minimize(cost, var_list=train_params) +train_op = tf.train.AdamOptimizer( + learning_rate=0.0001, beta1=0.9, beta2=0.999, epsilon=1e-08, use_locking=False).minimize( + cost, var_list=train_params) # initialize all variables in the session tl.layers.initialize_global_variables(sess) diff --git a/example/tutorial_mnist.py b/example/tutorial_mnist.py index c5420320b..1a344651f 100644 --- a/example/tutorial_mnist.py +++ b/example/tutorial_mnist.py @@ -49,8 +49,10 @@ def main_test_layers(model='relu'): network = tl.layers.DenseLayer(network, n_units=10, act=tf.identity, name='output') elif model == 'dropconnect': network = tl.layers.InputLayer(x, name='input') - network = tl.layers.DropconnectDenseLayer(network, keep=0.8, n_units=800, act=tf.nn.relu, name='dropconnect_relu1') - network = tl.layers.DropconnectDenseLayer(network, keep=0.5, n_units=800, act=tf.nn.relu, name='dropconnect_relu2') + network = tl.layers.DropconnectDenseLayer( + network, keep=0.8, n_units=800, act=tf.nn.relu, name='dropconnect_relu1') + network = tl.layers.DropconnectDenseLayer( + network, keep=0.5, n_units=800, act=tf.nn.relu, name='dropconnect_relu2') network = tl.layers.DropconnectDenseLayer(network, keep=0.5, n_units=10, act=tf.identity, name='output') # To print all attributes of a Layer. @@ -189,7 +191,16 @@ def main_test_denoise_AE(model='relu'): ## pretrain print("Pre-train Layer 1") recon_layer1.pretrain( - sess, x=x, X_train=X_train, X_val=X_val, denoise_name='denoising1', n_epoch=200, batch_size=128, print_freq=10, save=True, save_name='w1pre_') + sess, + x=x, + X_train=X_train, + X_val=X_val, + denoise_name='denoising1', + n_epoch=200, + batch_size=128, + print_freq=10, + save=True, + save_name='w1pre_') # You can also disable denoisong by setting denoise_name=None. # recon_layer1.pretrain(sess, x=x, X_train=X_train, X_val=X_val, # denoise_name=None, n_epoch=500, batch_size=128, @@ -260,9 +271,27 @@ def main_test_stacked_denoise_AE(model='relu'): network.print_params() print("\nPre-train Layer 1") recon_layer1.pretrain( - sess, x=x, X_train=X_train, X_val=X_val, denoise_name='denoising1', n_epoch=100, batch_size=128, print_freq=10, save=True, save_name='w1pre_') + sess, + x=x, + X_train=X_train, + X_val=X_val, + denoise_name='denoising1', + n_epoch=100, + batch_size=128, + print_freq=10, + save=True, + save_name='w1pre_') print("\nPre-train Layer 2") - recon_layer2.pretrain(sess, x=x, X_train=X_train, X_val=X_val, denoise_name='denoising1', n_epoch=100, batch_size=128, print_freq=10, save=False) + recon_layer2.pretrain( + sess, + x=x, + X_train=X_train, + X_val=X_val, + denoise_name='denoising1', + n_epoch=100, + batch_size=128, + print_freq=10, + save=False) print("\nAll Network Params after pre-train") network.print_params() diff --git a/example/tutorial_mnist_simple.py b/example/tutorial_mnist_simple.py index 4bc8d6a1b..a9c748d5a 100644 --- a/example/tutorial_mnist_simple.py +++ b/example/tutorial_mnist_simple.py @@ -45,7 +45,21 @@ # train the network tl.utils.fit( - sess, network, train_op, cost, X_train, y_train, x, y_, acc=acc, batch_size=500, n_epoch=500, print_freq=5, X_val=X_val, y_val=y_val, eval_train=False) + sess, + network, + train_op, + cost, + X_train, + y_train, + x, + y_, + acc=acc, + batch_size=500, + n_epoch=500, + print_freq=5, + X_val=X_val, + y_val=y_val, + eval_train=False) # evaluation tl.utils.test(sess, network, acc, X_test, y_test, x, y_, batch_size=None, cost=cost) diff --git a/example/tutorial_mobilenet.py b/example/tutorial_mobilenet.py index 3cc34c81e..e2302f1e1 100644 --- a/example/tutorial_mobilenet.py +++ b/example/tutorial_mobilenet.py @@ -13,7 +13,8 @@ import tensorflow as tf import tensorlayer as tl -from tensorlayer.layers import (BatchNormLayer, Conv2d, DepthwiseConv2d, FlattenLayer, GlobalMeanPool2d, InputLayer, ReshapeLayer) +from tensorlayer.layers import (BatchNormLayer, Conv2d, DepthwiseConv2d, FlattenLayer, GlobalMeanPool2d, InputLayer, + ReshapeLayer) def conv_block(n, n_filter, filter_size=(3, 3), strides=(1, 1), is_train=False, name='conv_block'): @@ -36,7 +37,9 @@ def depthwise_conv_block(n, n_filter, strides=(1, 1), is_train=False, name="dept def decode_predictions(preds, top=5): # keras.applications.resnet50 fpath = os.path.join("data", "imagenet_class_index.json") if tl.files.file_exists(fpath) is False: - raise Exception("{} / download imagenet_class_index.json from: https://github.com/zsdonghao/tensorlayer/tree/master/example/data") + raise Exception( + "{} / download imagenet_class_index.json from: https://github.com/zsdonghao/tensorlayer/tree/master/example/data" + ) if isinstance(preds, np.ndarray) is False: preds = np.asarray(preds) if len(preds.shape) != 2 or preds.shape[1] != 1000: diff --git a/example/tutorial_ptb_lstm.py b/example/tutorial_ptb_lstm.py index b91eb432a..33179b743 100644 --- a/example/tutorial_ptb_lstm.py +++ b/example/tutorial_ptb_lstm.py @@ -194,7 +194,8 @@ def inference(x, is_training, num_steps, reuse=None): print("\nnum_steps : %d, is_training : %s, reuse : %s" % (num_steps, is_training, reuse)) initializer = tf.random_uniform_initializer(-init_scale, init_scale) with tf.variable_scope("model", reuse=reuse): - network = tl.layers.EmbeddingInputlayer(inputs=x, vocabulary_size=vocab_size, embedding_size=hidden_size, E_init=initializer, name='embedding') + network = tl.layers.EmbeddingInputlayer( + inputs=x, vocabulary_size=vocab_size, embedding_size=hidden_size, E_init=initializer, name='embedding') network = tl.layers.DropoutLayer(network, keep=keep_prob, is_fix=True, is_train=is_training, name='drop1') network = tl.layers.RNNLayer( network, @@ -223,7 +224,8 @@ def inference(x, is_training, num_steps, reuse=None): # network = tl.layers.ReshapeLayer(network, # shape=[-1, int(network.outputs._shape[-1])], name='reshape') network = tl.layers.DropoutLayer(network, keep=keep_prob, is_fix=True, is_train=is_training, name='drop3') - network = tl.layers.DenseLayer(network, n_units=vocab_size, W_init=initializer, b_init=initializer, act=tf.identity, name='output') + network = tl.layers.DenseLayer( + network, n_units=vocab_size, W_init=initializer, b_init=initializer, act=tf.identity, name='output') return network, lstm1, lstm2 # Inference for Training @@ -299,7 +301,8 @@ def loss_fn(outputs, targets): #, batch_size, num_steps): } # For training, enable dropout feed_dict.update(network.all_drop) - _cost, state1, state2, _ = sess.run([cost, lstm1.final_state, lstm2.final_state, train_op], feed_dict=feed_dict) + _cost, state1, state2, _ = sess.run( + [cost, lstm1.final_state, lstm2.final_state, train_op], feed_dict=feed_dict) costs += _cost iters += num_steps @@ -323,7 +326,9 @@ def loss_fn(outputs, targets): #, batch_size, num_steps): lstm1_val.initial_state: state1, lstm2_val.initial_state: state2, } - _cost, state1, state2, _ = sess.run([cost_val, lstm1_val.final_state, lstm2_val.final_state, tf.no_op()], feed_dict=feed_dict) + _cost, state1, state2, _ = sess.run( + [cost_val, lstm1_val.final_state, lstm2_val.final_state, + tf.no_op()], feed_dict=feed_dict) costs += _cost iters += num_steps valid_perplexity = np.exp(costs / iters) @@ -345,7 +350,8 @@ def loss_fn(outputs, targets): #, batch_size, num_steps): lstm1_test.initial_state: state1, lstm2_test.initial_state: state2, } - _cost, state1, state2 = sess.run([cost_test, lstm1_test.final_state, lstm2_test.final_state], feed_dict=feed_dict) + _cost, state1, state2 = sess.run( + [cost_test, lstm1_test.final_state, lstm2_test.final_state], feed_dict=feed_dict) costs += _cost iters += 1 test_perplexity = np.exp(costs / iters) diff --git a/example/tutorial_ptb_lstm_state_is_tuple.py b/example/tutorial_ptb_lstm_state_is_tuple.py index 045fa7e39..42501d366 100644 --- a/example/tutorial_ptb_lstm_state_is_tuple.py +++ b/example/tutorial_ptb_lstm_state_is_tuple.py @@ -194,7 +194,8 @@ def inference(x, is_training, num_steps, reuse=None): print("\nnum_steps : %d, is_training : %s, reuse : %s" % (num_steps, is_training, reuse)) initializer = tf.random_uniform_initializer(-init_scale, init_scale) with tf.variable_scope("model", reuse=reuse): - network = tl.layers.EmbeddingInputlayer(inputs=x, vocabulary_size=vocab_size, embedding_size=hidden_size, E_init=initializer, name='embedding') + network = tl.layers.EmbeddingInputlayer( + inputs=x, vocabulary_size=vocab_size, embedding_size=hidden_size, E_init=initializer, name='embedding') network = tl.layers.DropoutLayer(network, keep=keep_prob, is_fix=True, is_train=is_training, name='drop1') network = tl.layers.RNNLayer( network, @@ -229,7 +230,8 @@ def inference(x, is_training, num_steps, reuse=None): # network = tl.layers.ReshapeLayer(network, # shape=[-1, int(network.outputs._shape[-1])], name='reshape') network = tl.layers.DropoutLayer(network, keep=keep_prob, is_fix=True, is_train=is_training, name='drop3') - network = tl.layers.DenseLayer(network, n_units=vocab_size, W_init=initializer, b_init=initializer, act=tf.identity, name='output') + network = tl.layers.DenseLayer( + network, n_units=vocab_size, W_init=initializer, b_init=initializer, act=tf.identity, name='output') return network, lstm1, lstm2 # Inference for Training diff --git a/example/tutorial_squeezenet.py b/example/tutorial_squeezenet.py index d3950e2ee..d23e684e5 100644 --- a/example/tutorial_squeezenet.py +++ b/example/tutorial_squeezenet.py @@ -15,7 +15,9 @@ def decode_predictions(preds, top=5): # keras.applications.resnet50 fpath = os.path.join("data", "imagenet_class_index.json") if tl.files.file_exists(fpath) is False: - raise Exception("{} / download imagenet_class_index.json from: https://github.com/zsdonghao/tensorlayer/tree/master/example/data") + raise Exception( + "{} / download imagenet_class_index.json from: https://github.com/zsdonghao/tensorlayer/tree/master/example/data" + ) if isinstance(preds, np.ndarray) is False: preds = np.asarray(preds) if len(preds.shape) != 2 or preds.shape[1] != 1000: @@ -113,7 +115,8 @@ def squeezenet(x, is_train=True, reuse=False): if tl.files.file_exists('squeezenet.npz'): tl.files.load_and_assign_npz(sess=sess, name='squeezenet.npz', network=n) else: - raise Exception("please download the pre-trained squeezenet.npz from https://github.com/tensorlayer/pretrained-models") + raise Exception( + "please download the pre-trained squeezenet.npz from https://github.com/tensorlayer/pretrained-models") img = tl.vis.read_image('data/tiger.jpeg', '') img = tl.prepro.imresize(img, (224, 224)) diff --git a/example/tutorial_ternaryweight_cifar10_tfrecord.py b/example/tutorial_ternaryweight_cifar10_tfrecord.py index 84399d8eb..380fc4efa 100644 --- a/example/tutorial_ternaryweight_cifar10_tfrecord.py +++ b/example/tutorial_ternaryweight_cifar10_tfrecord.py @@ -93,7 +93,8 @@ def read_and_decode(filename, is_train=None): reader = tf.TFRecordReader() _, serialized_example = reader.read(filename_queue) features = tf.parse_single_example( - serialized_example, features={ + serialized_example, + features={ 'label': tf.FixedLenFeature([], tf.int64), 'img_raw': tf.FixedLenFeature([], tf.string), }) @@ -146,9 +147,11 @@ def read_and_decode(filename, is_train=None): x_test_, y_test_ = read_and_decode("test.cifar10", False) x_train_batch, y_train_batch = tf.train.shuffle_batch( - [x_train_, y_train_], batch_size=batch_size, capacity=2000, min_after_dequeue=1000, num_threads=32) # set the number of threads here + [x_train_, y_train_], batch_size=batch_size, capacity=2000, min_after_dequeue=1000, + num_threads=32) # set the number of threads here # for testing, uses batch instead of shuffle_batch - x_test_batch, y_test_batch = tf.train.batch([x_test_, y_test_], batch_size=batch_size, capacity=50000, num_threads=32) + x_test_batch, y_test_batch = tf.train.batch( + [x_test_, y_test_], batch_size=batch_size, capacity=50000, num_threads=32) def model(x_crop, y_, reuse): """ For more simplified CNN APIs, check tensorlayer.org """ @@ -159,14 +162,22 @@ def model(x_crop, y_, reuse): net = tl.layers.InputLayer(x_crop, name='input') net = tl.layers.Conv2d(net, 64, (5, 5), (1, 1), act=tf.nn.relu, padding='SAME', W_init=W_init, name='cnn1') net = tl.layers.MaxPool2d(net, (3, 3), (2, 2), padding='SAME', name='pool1') - net = tl.layers.LocalResponseNormLayer(net, depth_radius=4, bias=1.0, alpha=0.001 / 9.0, beta=0.75, name='norm1') - net = tl.layers.TernaryConv2d(net, 64, (5, 5), (1, 1), act=tf.nn.relu, padding='SAME', W_init=W_init, name='cnn2') - net = tl.layers.LocalResponseNormLayer(net, depth_radius=4, bias=1.0, alpha=0.001 / 9.0, beta=0.75, name='norm2') + net = tl.layers.LocalResponseNormLayer( + net, depth_radius=4, bias=1.0, alpha=0.001 / 9.0, beta=0.75, name='norm1') + net = tl.layers.TernaryConv2d( + net, 64, (5, 5), (1, 1), act=tf.nn.relu, padding='SAME', W_init=W_init, name='cnn2') + net = tl.layers.LocalResponseNormLayer( + net, depth_radius=4, bias=1.0, alpha=0.001 / 9.0, beta=0.75, name='norm2') net = tl.layers.MaxPool2d(net, (3, 3), (2, 2), padding='SAME', name='pool2') net = tl.layers.FlattenLayer(net, name='flatten') # output: (batch_size, 2304) - net = tl.layers.TernaryDenseLayer(net, n_units=384, act=tf.nn.relu, W_init=W_init2, b_init=b_init2, name='d1relu') # output: (batch_size, 384) - net = tl.layers.TernaryDenseLayer(net, n_units=192, act=tf.nn.relu, W_init=W_init2, b_init=b_init2, name='d2relu') # output: (batch_size, 192) - net = tl.layers.DenseLayer(net, n_units=10, act=tf.identity, W_init=W_init2, name='output') # output: (batch_size, 10) + net = tl.layers.TernaryDenseLayer( + net, n_units=384, act=tf.nn.relu, W_init=W_init2, b_init=b_init2, + name='d1relu') # output: (batch_size, 384) + net = tl.layers.TernaryDenseLayer( + net, n_units=192, act=tf.nn.relu, W_init=W_init2, b_init=b_init2, + name='d2relu') # output: (batch_size, 192) + net = tl.layers.DenseLayer( + net, n_units=10, act=tf.identity, W_init=W_init2, name='output') # output: (batch_size, 10) y = net.outputs ce = tl.cost.cross_entropy(y, y_, name='cost') @@ -196,9 +207,14 @@ def model_batch_norm(x_crop, y_, reuse, is_train): net = tl.layers.BatchNormLayer(net, is_train, act=tf.nn.relu, name='batch2') net = tl.layers.MaxPool2d(net, (3, 3), (2, 2), padding='SAME', name='pool2') net = tl.layers.FlattenLayer(net, name='flatten') # output: (batch_size, 2304) - net = tl.layers.DenseLayer(net, n_units=384, act=tf.nn.relu, W_init=W_init2, b_init=b_init2, name='d1relu') # output: (batch_size, 384) - net = tl.layers.DenseLayer(net, n_units=192, act=tf.nn.relu, W_init=W_init2, b_init=b_init2, name='d2relu') # output: (batch_size, 192) - net = tl.layers.DenseLayer(net, n_units=10, act=tf.identity, W_init=W_init2, name='output') # output: (batch_size, 10) + net = tl.layers.DenseLayer( + net, n_units=384, act=tf.nn.relu, W_init=W_init2, b_init=b_init2, + name='d1relu') # output: (batch_size, 384) + net = tl.layers.DenseLayer( + net, n_units=192, act=tf.nn.relu, W_init=W_init2, b_init=b_init2, + name='d2relu') # output: (batch_size, 192) + net = tl.layers.DenseLayer( + net, n_units=10, act=tf.identity, W_init=W_init2, name='output') # output: (batch_size, 10) y = net.outputs ce = tl.cost.cross_entropy(y, y_, name='cost') # L2 for the MLP, without this, the accuracy will be reduced by 15%. @@ -267,7 +283,8 @@ def model_batch_norm(x_crop, y_, reuse, is_train): n_batch += 1 if epoch + 1 == 1 or (epoch + 1) % print_freq == 0: - print("Epoch %d : Step %d-%d of %d took %fs" % (epoch, step, step + n_step_epoch, n_step, time.time() - start_time)) + print("Epoch %d : Step %d-%d of %d took %fs" % (epoch, step, step + n_step_epoch, n_step, + time.time() - start_time)) print(" train loss: %f" % (train_loss / n_batch)) print(" train acc: %f" % (train_acc / n_batch)) diff --git a/example/tutorial_tf_dataset_voc.py b/example/tutorial_tf_dataset_voc.py index 4c2580fe2..6e752580d 100644 --- a/example/tutorial_tf_dataset_voc.py +++ b/example/tutorial_tf_dataset_voc.py @@ -50,8 +50,10 @@ def _data_aug_fn(im, ann): ## random resize and crop tmp0 = random.randint(1, int(im_size[0] * jitter)) tmp1 = random.randint(1, int(im_size[1] * jitter)) - im, coords = tl.prepro.obj_box_imresize(im, coords, [im_size[0] + tmp0, im_size[1] + tmp1], is_rescale=True, interp='bicubic') - im, clas, coords = tl.prepro.obj_box_crop(im, clas, coords, wrg=im_size[1], hrg=im_size[0], is_rescale=True, is_center=True, is_random=True) + im, coords = tl.prepro.obj_box_imresize( + im, coords, [im_size[0] + tmp0, im_size[1] + tmp1], is_rescale=True, interp='bicubic') + im, clas, coords = tl.prepro.obj_box_crop( + im, clas, coords, wrg=im_size[1], hrg=im_size[0], is_rescale=True, is_center=True, is_random=True) ## value [0, 255] to [-1, 1] (optional) # im = im / 127.5 - 1 ## value [0, 255] to [0, 1] (optional) @@ -97,4 +99,5 @@ def _map_fn(filename, annotation): ## save all images for i in range(len(im)): - tl.vis.draw_boxes_and_labels_to_image(im[i] * 255, ann[i][0], ann[i][1], [], classes, True, save_name='_bbox_vis_%d.png' % i) + tl.vis.draw_boxes_and_labels_to_image( + im[i] * 255, ann[i][0], ann[i][1], [], classes, True, save_name='_bbox_vis_%d.png' % i) diff --git a/example/tutorial_tfrecord.py b/example/tutorial_tfrecord.py index f77cc6ade..c83dc0b1d 100644 --- a/example/tutorial_tfrecord.py +++ b/example/tutorial_tfrecord.py @@ -94,7 +94,8 @@ def read_and_decode(filename): ## Use shuffle_batch or batch # see https://www.tensorflow.org/versions/master/api_docs/python/io_ops.html#shuffle_batch -img_batch, label_batch = tf.train.shuffle_batch([img, label], batch_size=4, capacity=2000, min_after_dequeue=1000, num_threads=16) +img_batch, label_batch = tf.train.shuffle_batch( + [img, label], batch_size=4, capacity=2000, min_after_dequeue=1000, num_threads=16) print("img_batch : %s" % img_batch._shape) print("label_batch : %s" % label_batch._shape) # init = tf.initialize_all_variables() diff --git a/example/tutorial_tfrecord2.py b/example/tutorial_tfrecord2.py index 189b24167..944d995cb 100755 --- a/example/tutorial_tfrecord2.py +++ b/example/tutorial_tfrecord2.py @@ -48,10 +48,11 @@ # image = image.reshape([32, 32, 3]) # tl.visualize.frame(np.asarray(image, dtype=np.uint8), second=1, saveable=False, name='frame', fig_idx=1236) example = tf.train.Example( - features=tf.train.Features(feature={ - "label": tf.train.Feature(int64_list=tf.train.Int64List(value=[label])), - 'img_raw': tf.train.Feature(bytes_list=tf.train.BytesList(value=[img_raw])), - })) + features=tf.train.Features( + feature={ + "label": tf.train.Feature(int64_list=tf.train.Int64List(value=[label])), + 'img_raw': tf.train.Feature(bytes_list=tf.train.BytesList(value=[img_raw])), + })) writer.write(example.SerializeToString()) # Serialize To String writer.close() @@ -62,7 +63,8 @@ def read_and_decode(filename): reader = tf.TFRecordReader() _, serialized_example = reader.read(filename_queue) features = tf.parse_single_example( - serialized_example, features={ + serialized_example, + features={ 'label': tf.FixedLenFeature([], tf.int64), 'img_raw': tf.FixedLenFeature([], tf.string), }) @@ -78,7 +80,8 @@ def read_and_decode(filename): ## Use shuffle_batch or batch # see https://www.tensorflow.org/versions/master/api_docs/python/io_ops.html#shuffle_batch -img_batch, label_batch = tf.train.shuffle_batch([img, label], batch_size=4, capacity=50000, min_after_dequeue=10000, num_threads=1) +img_batch, label_batch = tf.train.shuffle_batch( + [img, label], batch_size=4, capacity=50000, min_after_dequeue=10000, num_threads=1) print("img_batch : %s" % img_batch._shape) print("label_batch : %s" % label_batch._shape) diff --git a/example/tutorial_tfrecord3.py b/example/tutorial_tfrecord3.py index 936ab343f..2759e86e7 100644 --- a/example/tutorial_tfrecord3.py +++ b/example/tutorial_tfrecord3.py @@ -90,10 +90,11 @@ def _bytes_feature_list(values): context = tf.train.Features(feature={ # Non-serial data uses Feature "image/img_raw": _bytes_feature(img_raw), }) - feature_lists = tf.train.FeatureLists(feature_list={ # Serial data uses FeatureLists - "image/caption": _bytes_feature_list(img_capt_b), - "image/caption_ids": _int64_feature_list(img_capt_ids) - }) + feature_lists = tf.train.FeatureLists( + feature_list={ # Serial data uses FeatureLists + "image/caption": _bytes_feature_list(img_capt_b), + "image/caption_ids": _int64_feature_list(img_capt_ids) + }) sequence_example = tf.train.SequenceExample(context=context, feature_lists=feature_lists) writer.write(sequence_example.SerializeToString()) # Serialize To String writer.close() @@ -276,7 +277,11 @@ def prefetch_input_data(reader, filename_queue = tf.train.string_input_producer(data_files, shuffle=True, capacity=16, name=shard_queue_name) min_queue_examples = values_per_shard * input_queue_capacity_factor capacity = min_queue_examples + 100 * batch_size - values_queue = tf.RandomShuffleQueue(capacity=capacity, min_after_dequeue=min_queue_examples, dtypes=[tf.string], name="random_" + value_queue_name) + values_queue = tf.RandomShuffleQueue( + capacity=capacity, + min_after_dequeue=min_queue_examples, + dtypes=[tf.string], + name="random_" + value_queue_name) else: print(" is_training == False : FIFOQueue") filename_queue = tf.train.string_input_producer(data_files, shuffle=False, capacity=1, name=shard_queue_name) @@ -289,7 +294,8 @@ def prefetch_input_data(reader, enqueue_ops.append(values_queue.enqueue([value])) tf.train.queue_runner.add_queue_runner(tf.train.queue_runner.QueueRunner(values_queue, enqueue_ops)) - tf.summary.scalar("queue/%s/fraction_of_%d_full" % (values_queue.name, capacity), tf.cast(values_queue.size(), tf.float32) * (1. / capacity)) + tf.summary.scalar("queue/%s/fraction_of_%d_full" % (values_queue.name, capacity), + tf.cast(values_queue.size(), tf.float32) * (1. / capacity)) return values_queue @@ -327,7 +333,8 @@ def prefetch_input_data(reader, img = tf.image.resize_images(img, size=(resize_height, resize_width), method=tf.image.ResizeMethod.BILINEAR) except Exception: # for TensorFlow 0.10 - img = tf.image.resize_images(img, new_height=resize_height, new_width=resize_width, method=tf.image.ResizeMethod.BILINEAR) + img = tf.image.resize_images( + img, new_height=resize_height, new_width=resize_width, method=tf.image.ResizeMethod.BILINEAR) # Crop to final dimensions. if is_training: img = tf.random_crop(img, [height, width, 3]) @@ -440,7 +447,8 @@ def batch_with_dynamic_pad(images_and_captions, batch_size, queue_capacity, add_ return images, input_seqs, target_seqs, mask -images, input_seqs, target_seqs, input_mask = (batch_with_dynamic_pad(images_and_captions=[[img, img_cap]], batch_size=4, queue_capacity=50000)) +images, input_seqs, target_seqs, input_mask = (batch_with_dynamic_pad( + images_and_captions=[[img, img_cap]], batch_size=4, queue_capacity=50000)) sess = tf.Session() sess.run(tf.initialize_all_variables()) coord = tf.train.Coordinator() diff --git a/example/tutorial_vgg16.py b/example/tutorial_vgg16.py index 606497f32..29305c75c 100644 --- a/example/tutorial_vgg16.py +++ b/example/tutorial_vgg16.py @@ -45,7 +45,8 @@ try: from data.imagenet_classes import * except Exception as e: - raise Exception("{} / download the file from: https://github.com/zsdonghao/tensorlayer/tree/master/example/data".format(e)) + raise Exception( + "{} / download the file from: https://github.com/zsdonghao/tensorlayer/tree/master/example/data".format(e)) def conv_layers(net_in): @@ -71,7 +72,8 @@ def conv_layers(net_in): strides=[1, 1, 1, 1], padding='SAME', name='conv1_2') - network = PoolLayer(network, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', pool=tf.nn.max_pool, name='pool1') + network = PoolLayer( + network, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', pool=tf.nn.max_pool, name='pool1') # conv2 network = Conv2dLayer( @@ -88,7 +90,8 @@ def conv_layers(net_in): strides=[1, 1, 1, 1], padding='SAME', name='conv2_2') - network = PoolLayer(network, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', pool=tf.nn.max_pool, name='pool2') + network = PoolLayer( + network, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', pool=tf.nn.max_pool, name='pool2') # conv3 network = Conv2dLayer( @@ -112,7 +115,8 @@ def conv_layers(net_in): strides=[1, 1, 1, 1], padding='SAME', name='conv3_3') - network = PoolLayer(network, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', pool=tf.nn.max_pool, name='pool3') + network = PoolLayer( + network, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', pool=tf.nn.max_pool, name='pool3') # conv4 network = Conv2dLayer( @@ -136,7 +140,8 @@ def conv_layers(net_in): strides=[1, 1, 1, 1], padding='SAME', name='conv4_3') - network = PoolLayer(network, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', pool=tf.nn.max_pool, name='pool4') + network = PoolLayer( + network, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', pool=tf.nn.max_pool, name='pool4') # conv5 network = Conv2dLayer( @@ -160,7 +165,8 @@ def conv_layers(net_in): strides=[1, 1, 1, 1], padding='SAME', name='conv5_3') - network = PoolLayer(network, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', pool=tf.nn.max_pool, name='pool5') + network = PoolLayer( + network, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', pool=tf.nn.max_pool, name='pool5') return network @@ -173,31 +179,44 @@ def conv_layers_simple_api(net_in): net_in.outputs = net_in.outputs - mean # conv1 - network = Conv2d(net_in, n_filter=64, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv1_1') - network = Conv2d(network, n_filter=64, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv1_2') + network = Conv2d( + net_in, n_filter=64, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv1_1') + network = Conv2d( + network, n_filter=64, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv1_2') network = MaxPool2d(network, filter_size=(2, 2), strides=(2, 2), padding='SAME', name='pool1') # conv2 - network = Conv2d(network, n_filter=128, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv2_1') - network = Conv2d(network, n_filter=128, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv2_2') + network = Conv2d( + network, n_filter=128, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv2_1') + network = Conv2d( + network, n_filter=128, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv2_2') network = MaxPool2d(network, filter_size=(2, 2), strides=(2, 2), padding='SAME', name='pool2') # conv3 - network = Conv2d(network, n_filter=256, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv3_1') - network = Conv2d(network, n_filter=256, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv3_2') - network = Conv2d(network, n_filter=256, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv3_3') + network = Conv2d( + network, n_filter=256, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv3_1') + network = Conv2d( + network, n_filter=256, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv3_2') + network = Conv2d( + network, n_filter=256, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv3_3') network = MaxPool2d(network, filter_size=(2, 2), strides=(2, 2), padding='SAME', name='pool3') # conv4 - network = Conv2d(network, n_filter=512, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv4_1') - network = Conv2d(network, n_filter=512, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv4_2') - network = Conv2d(network, n_filter=512, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv4_3') + network = Conv2d( + network, n_filter=512, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv4_1') + network = Conv2d( + network, n_filter=512, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv4_2') + network = Conv2d( + network, n_filter=512, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv4_3') network = MaxPool2d(network, filter_size=(2, 2), strides=(2, 2), padding='SAME', name='pool4') # conv5 - network = Conv2d(network, n_filter=512, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv5_1') - network = Conv2d(network, n_filter=512, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv5_2') - network = Conv2d(network, n_filter=512, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv5_3') + network = Conv2d( + network, n_filter=512, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv5_1') + network = Conv2d( + network, n_filter=512, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv5_2') + network = Conv2d( + network, n_filter=512, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv5_3') network = MaxPool2d(network, filter_size=(2, 2), strides=(2, 2), padding='SAME', name='pool5') return network diff --git a/example/tutorial_vgg19.py b/example/tutorial_vgg19.py index af1bd034f..0850f9e8a 100755 --- a/example/tutorial_vgg19.py +++ b/example/tutorial_vgg19.py @@ -27,7 +27,8 @@ try: from data.imagenet_classes import * except Exception as e: - raise Exception("{} / download the file from: https://github.com/zsdonghao/tensorlayer/tree/master/example/data".format(e)) + raise Exception( + "{} / download the file from: https://github.com/zsdonghao/tensorlayer/tree/master/example/data".format(e)) VGG_MEAN = [103.939, 116.779, 123.68] @@ -103,31 +104,52 @@ def Vgg19(rgb): # input layer net_in = InputLayer(bgr, name='input') # conv1 - network = Conv2dLayer(net_in, act=tf.nn.relu, shape=[3, 3, 3, 64], strides=[1, 1, 1, 1], padding='SAME', name='conv1_1') - network = Conv2dLayer(network, act=tf.nn.relu, shape=[3, 3, 64, 64], strides=[1, 1, 1, 1], padding='SAME', name='conv1_2') - network = PoolLayer(network, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', pool=tf.nn.max_pool, name='pool1') + network = Conv2dLayer( + net_in, act=tf.nn.relu, shape=[3, 3, 3, 64], strides=[1, 1, 1, 1], padding='SAME', name='conv1_1') + network = Conv2dLayer( + network, act=tf.nn.relu, shape=[3, 3, 64, 64], strides=[1, 1, 1, 1], padding='SAME', name='conv1_2') + network = PoolLayer( + network, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', pool=tf.nn.max_pool, name='pool1') # conv2 - network = Conv2dLayer(network, act=tf.nn.relu, shape=[3, 3, 64, 128], strides=[1, 1, 1, 1], padding='SAME', name='conv2_1') - network = Conv2dLayer(network, act=tf.nn.relu, shape=[3, 3, 128, 128], strides=[1, 1, 1, 1], padding='SAME', name='conv2_2') - network = PoolLayer(network, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', pool=tf.nn.max_pool, name='pool2') + network = Conv2dLayer( + network, act=tf.nn.relu, shape=[3, 3, 64, 128], strides=[1, 1, 1, 1], padding='SAME', name='conv2_1') + network = Conv2dLayer( + network, act=tf.nn.relu, shape=[3, 3, 128, 128], strides=[1, 1, 1, 1], padding='SAME', name='conv2_2') + network = PoolLayer( + network, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', pool=tf.nn.max_pool, name='pool2') # conv3 - network = Conv2dLayer(network, act=tf.nn.relu, shape=[3, 3, 128, 256], strides=[1, 1, 1, 1], padding='SAME', name='conv3_1') - network = Conv2dLayer(network, act=tf.nn.relu, shape=[3, 3, 256, 256], strides=[1, 1, 1, 1], padding='SAME', name='conv3_2') - network = Conv2dLayer(network, act=tf.nn.relu, shape=[3, 3, 256, 256], strides=[1, 1, 1, 1], padding='SAME', name='conv3_3') - network = Conv2dLayer(network, act=tf.nn.relu, shape=[3, 3, 256, 256], strides=[1, 1, 1, 1], padding='SAME', name='conv3_4') - network = PoolLayer(network, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', pool=tf.nn.max_pool, name='pool3') + network = Conv2dLayer( + network, act=tf.nn.relu, shape=[3, 3, 128, 256], strides=[1, 1, 1, 1], padding='SAME', name='conv3_1') + network = Conv2dLayer( + network, act=tf.nn.relu, shape=[3, 3, 256, 256], strides=[1, 1, 1, 1], padding='SAME', name='conv3_2') + network = Conv2dLayer( + network, act=tf.nn.relu, shape=[3, 3, 256, 256], strides=[1, 1, 1, 1], padding='SAME', name='conv3_3') + network = Conv2dLayer( + network, act=tf.nn.relu, shape=[3, 3, 256, 256], strides=[1, 1, 1, 1], padding='SAME', name='conv3_4') + network = PoolLayer( + network, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', pool=tf.nn.max_pool, name='pool3') # conv4 - network = Conv2dLayer(network, act=tf.nn.relu, shape=[3, 3, 256, 512], strides=[1, 1, 1, 1], padding='SAME', name='conv4_1') - network = Conv2dLayer(network, act=tf.nn.relu, shape=[3, 3, 512, 512], strides=[1, 1, 1, 1], padding='SAME', name='conv4_2') - network = Conv2dLayer(network, act=tf.nn.relu, shape=[3, 3, 512, 512], strides=[1, 1, 1, 1], padding='SAME', name='conv4_3') - network = Conv2dLayer(network, act=tf.nn.relu, shape=[3, 3, 512, 512], strides=[1, 1, 1, 1], padding='SAME', name='conv4_4') - network = PoolLayer(network, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', pool=tf.nn.max_pool, name='pool4') + network = Conv2dLayer( + network, act=tf.nn.relu, shape=[3, 3, 256, 512], strides=[1, 1, 1, 1], padding='SAME', name='conv4_1') + network = Conv2dLayer( + network, act=tf.nn.relu, shape=[3, 3, 512, 512], strides=[1, 1, 1, 1], padding='SAME', name='conv4_2') + network = Conv2dLayer( + network, act=tf.nn.relu, shape=[3, 3, 512, 512], strides=[1, 1, 1, 1], padding='SAME', name='conv4_3') + network = Conv2dLayer( + network, act=tf.nn.relu, shape=[3, 3, 512, 512], strides=[1, 1, 1, 1], padding='SAME', name='conv4_4') + network = PoolLayer( + network, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', pool=tf.nn.max_pool, name='pool4') # conv5 - network = Conv2dLayer(network, act=tf.nn.relu, shape=[3, 3, 512, 512], strides=[1, 1, 1, 1], padding='SAME', name='conv5_1') - network = Conv2dLayer(network, act=tf.nn.relu, shape=[3, 3, 512, 512], strides=[1, 1, 1, 1], padding='SAME', name='conv5_2') - network = Conv2dLayer(network, act=tf.nn.relu, shape=[3, 3, 512, 512], strides=[1, 1, 1, 1], padding='SAME', name='conv5_3') - network = Conv2dLayer(network, act=tf.nn.relu, shape=[3, 3, 512, 512], strides=[1, 1, 1, 1], padding='SAME', name='conv5_4') - network = PoolLayer(network, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', pool=tf.nn.max_pool, name='pool5') + network = Conv2dLayer( + network, act=tf.nn.relu, shape=[3, 3, 512, 512], strides=[1, 1, 1, 1], padding='SAME', name='conv5_1') + network = Conv2dLayer( + network, act=tf.nn.relu, shape=[3, 3, 512, 512], strides=[1, 1, 1, 1], padding='SAME', name='conv5_2') + network = Conv2dLayer( + network, act=tf.nn.relu, shape=[3, 3, 512, 512], strides=[1, 1, 1, 1], padding='SAME', name='conv5_3') + network = Conv2dLayer( + network, act=tf.nn.relu, shape=[3, 3, 512, 512], strides=[1, 1, 1, 1], padding='SAME', name='conv5_4') + network = PoolLayer( + network, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', pool=tf.nn.max_pool, name='pool5') # fc 6~8 network = FlattenLayer(network, name='flatten') network = DenseLayer(network, n_units=4096, act=tf.nn.relu, name='fc6') @@ -178,30 +200,46 @@ def Vgg19_simple_api(rgb): # input layer net_in = InputLayer(bgr, name='input') # conv1 - network = Conv2d(net_in, n_filter=64, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv1_1') - network = Conv2d(network, n_filter=64, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv1_2') + network = Conv2d( + net_in, n_filter=64, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv1_1') + network = Conv2d( + network, n_filter=64, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv1_2') network = MaxPool2d(network, filter_size=(2, 2), strides=(2, 2), padding='SAME', name='pool1') # conv2 - network = Conv2d(network, n_filter=128, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv2_1') - network = Conv2d(network, n_filter=128, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv2_2') + network = Conv2d( + network, n_filter=128, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv2_1') + network = Conv2d( + network, n_filter=128, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv2_2') network = MaxPool2d(network, filter_size=(2, 2), strides=(2, 2), padding='SAME', name='pool2') # conv3 - network = Conv2d(network, n_filter=256, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv3_1') - network = Conv2d(network, n_filter=256, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv3_2') - network = Conv2d(network, n_filter=256, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv3_3') - network = Conv2d(network, n_filter=256, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv3_4') + network = Conv2d( + network, n_filter=256, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv3_1') + network = Conv2d( + network, n_filter=256, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv3_2') + network = Conv2d( + network, n_filter=256, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv3_3') + network = Conv2d( + network, n_filter=256, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv3_4') network = MaxPool2d(network, filter_size=(2, 2), strides=(2, 2), padding='SAME', name='pool3') # conv4 - network = Conv2d(network, n_filter=512, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv4_1') - network = Conv2d(network, n_filter=512, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv4_2') - network = Conv2d(network, n_filter=512, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv4_3') - network = Conv2d(network, n_filter=512, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv4_4') + network = Conv2d( + network, n_filter=512, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv4_1') + network = Conv2d( + network, n_filter=512, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv4_2') + network = Conv2d( + network, n_filter=512, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv4_3') + network = Conv2d( + network, n_filter=512, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv4_4') network = MaxPool2d(network, filter_size=(2, 2), strides=(2, 2), padding='SAME', name='pool4') # conv5 - network = Conv2d(network, n_filter=512, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv5_1') - network = Conv2d(network, n_filter=512, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv5_2') - network = Conv2d(network, n_filter=512, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv5_3') - network = Conv2d(network, n_filter=512, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv5_4') + network = Conv2d( + network, n_filter=512, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv5_1') + network = Conv2d( + network, n_filter=512, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv5_2') + network = Conv2d( + network, n_filter=512, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv5_3') + network = Conv2d( + network, n_filter=512, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv5_4') network = MaxPool2d(network, filter_size=(2, 2), strides=(2, 2), padding='SAME', name='pool5') # fc 6~8 network = FlattenLayer(network, name='flatten') diff --git a/example/tutorial_word2vec_basic.py b/example/tutorial_word2vec_basic.py index 0efad97ab..6258814db 100644 --- a/example/tutorial_word2vec_basic.py +++ b/example/tutorial_word2vec_basic.py @@ -136,21 +136,24 @@ def main_word2vec_basic(): data, count, dictionary, reverse_dictionary = \ tl.nlp.build_words_dataset(words, vocabulary_size, True, _UNK) - print('Most 5 common words (+UNK)', count[:5]) # [['UNK', 418391], (b'the', 1061396), (b'of', 593677), (b'and', 416629), (b'one', 411764)] - print('Sample data', data[:10], [ - reverse_dictionary[i] for i in data[:10] - ]) # [5243, 3081, 12, 6, 195, 2, 3135, 46, 59, 156] [b'anarchism', b'originated', b'as', b'a', b'term', b'of', b'abuse', b'first', b'used', b'against'] + print('Most 5 common words (+UNK)', + count[:5]) # [['UNK', 418391], (b'the', 1061396), (b'of', 593677), (b'and', 416629), (b'one', 411764)] + print( + 'Sample data', data[:10], [reverse_dictionary[i] for i in data[:10]] + ) # [5243, 3081, 12, 6, 195, 2, 3135, 46, 59, 156] [b'anarchism', b'originated', b'as', b'a', b'term', b'of', b'abuse', b'first', b'used', b'against'] del words # Hint to reduce memory. # Step 3: Function to generate a training batch for the Skip-Gram model. print() - batch, labels, data_index = tl.nlp.generate_skip_gram_batch(data=data, batch_size=8, num_skips=4, skip_window=2, data_index=0) + batch, labels, data_index = tl.nlp.generate_skip_gram_batch( + data=data, batch_size=8, num_skips=4, skip_window=2, data_index=0) for i in range(8): print(batch[i], reverse_dictionary[batch[i]], '->', labels[i, 0], reverse_dictionary[labels[i, 0]]) - batch, labels, data_index = tl.nlp.generate_skip_gram_batch(data=data, batch_size=8, num_skips=2, skip_window=1, data_index=0) + batch, labels, data_index = tl.nlp.generate_skip_gram_batch( + data=data, batch_size=8, num_skips=2, skip_window=1, data_index=0) for i in range(8): print(batch[i], reverse_dictionary[batch[i]], '->', labels[i, 0], reverse_dictionary[labels[i, 0]]) @@ -195,7 +198,9 @@ def main_word2vec_basic(): cost = emb_net.nce_cost train_params = emb_net.all_params # train_op = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost, var_list=train_params) - train_op = tf.train.AdagradOptimizer(learning_rate, initial_accumulator_value=0.1, use_locking=False).minimize(cost, var_list=train_params) + train_op = tf.train.AdagradOptimizer( + learning_rate, initial_accumulator_value=0.1, use_locking=False).minimize( + cost, var_list=train_params) # Compute the cosine similarity between minibatch examples and all embeddings. # For simple visualization of validation set. @@ -239,7 +244,8 @@ def main_word2vec_basic(): if step % print_freq == 0: if step > 0: average_loss /= print_freq - print("Average loss at step %d/%d. loss:%f took:%fs" % (step, num_steps, average_loss, time.time() - start_time)) + print("Average loss at step %d/%d. loss:%f took:%fs" % (step, num_steps, average_loss, + time.time() - start_time)) average_loss = 0 # Prints out nearby words given a list of words. # Note that this is expensive (~20% slowdown if computed every 500 steps) @@ -267,7 +273,8 @@ def main_word2vec_basic(): 'count': count, 'dictionary': dictionary, 'reverse_dictionary': reverse_dictionary - }, name=model_file_name + '.npy') + }, + name=model_file_name + '.npy') # if step == num_steps-1: # keeptrain = input("Training %d finished enter 1 to keep training: " % num_steps) @@ -281,7 +288,8 @@ def main_word2vec_basic(): print() final_embeddings = sess.run(normalized_embeddings) #.eval() - tl.visualize.tsne_embedding(final_embeddings, reverse_dictionary, plot_only=500, second=5, saveable=False, name='word2vec_basic') + tl.visualize.tsne_embedding( + final_embeddings, reverse_dictionary, plot_only=500, second=5, saveable=False, name='word2vec_basic') # Step 7: Evaluate by analogy questions. see tensorflow/models/embedding/word2vec_optimized.py print() diff --git a/tensorlayer/activation.py b/tensorlayer/activation.py index 3a2c18827..ac3d23db8 100644 --- a/tensorlayer/activation.py +++ b/tensorlayer/activation.py @@ -1,6 +1,5 @@ #! /usr/bin/python # -*- coding: utf-8 -*- - """A file containing various activation functions.""" import tensorflow as tf @@ -18,9 +17,7 @@ ] -@deprecated( - "2018-06-30", - "This API will be deprecated soon as tf.identity can do the same thing.") +@deprecated("2018-06-30", "This API will be deprecated soon as tf.identity can do the same thing.") def identity(x): """Identity activation function. @@ -60,11 +57,7 @@ def ramp(x, v_min=0, v_max=1, name=None): A ``Tensor`` in the same type as ``x``. """ - return tf.clip_by_value( - x, - clip_value_min=v_min, - clip_value_max=v_max, - name=name) + return tf.clip_by_value(x, clip_value_min=v_min, clip_value_max=v_max, name=name) def leaky_relu(x, alpha=0.1, name="lrelu"): @@ -211,9 +204,7 @@ def hard_tanh(x, name='htanh'): return tf.clip_by_value(x, -1, 1, name=name) -@deprecated( - "2018-06-30", - "This API will be deprecated soon as tf.nn.softmax can do the same thing.") +@deprecated("2018-06-30", "This API will be deprecated soon as tf.nn.softmax can do the same thing.") def pixel_wise_softmax(x, name='pixel_wise_softmax'): """Return the softmax outputs of images, every pixels have multiple label, the sum of a pixel is 1. diff --git a/tensorlayer/cost.py b/tensorlayer/cost.py index 4a121b6ea..ed8f3c47c 100644 --- a/tensorlayer/cost.py +++ b/tensorlayer/cost.py @@ -99,7 +99,8 @@ def binary_cross_entropy(output, target, epsilon=1e-8, name='bce_loss'): # output = ops.convert_to_tensor(output, name="preds") # target = ops.convert_to_tensor(targets, name="target") with tf.name_scope(name): - return tf.reduce_mean(tf.reduce_sum(-(target * tf.log(output + epsilon) + (1. - target) * tf.log(1. - output + epsilon)), axis=1)) + return tf.reduce_mean( + tf.reduce_sum(-(target * tf.log(output + epsilon) + (1. - target) * tf.log(1. - output + epsilon)), axis=1)) # For brevity, let `x = output`, `z = target`. The binary cross entropy loss is # @@ -397,7 +398,8 @@ def cross_entropy_seq(logits, target_seqs, batch_size=None): #, batch_size=1, n # except: # sequence_loss_by_example_fn = tf.nn.seq2seq.sequence_loss_by_example - loss = sequence_loss_by_example_fn([logits], [tf.reshape(target_seqs, [-1])], [tf.ones_like(tf.reshape(target_seqs, [-1]), dtype=tf.float32)]) + loss = sequence_loss_by_example_fn([logits], [tf.reshape(target_seqs, [-1])], + [tf.ones_like(tf.reshape(target_seqs, [-1]), dtype=tf.float32)]) # [tf.ones([batch_size * num_steps])]) cost = tf.reduce_sum(loss) #/ batch_size if batch_size is not None: @@ -490,7 +492,8 @@ def cosine_similarity(v1, v2): """ # try: ## TF1.0 - cost = tf.reduce_sum(tf.multiply(v1, v2), 1) / (tf.sqrt(tf.reduce_sum(tf.multiply(v1, v1), 1)) * tf.sqrt(tf.reduce_sum(tf.multiply(v2, v2), 1))) + cost = tf.reduce_sum(tf.multiply(v1, v2), 1) / ( + tf.sqrt(tf.reduce_sum(tf.multiply(v1, v1), 1)) * tf.sqrt(tf.reduce_sum(tf.multiply(v2, v2), 1))) # except: ## TF0.12 # cost = tf.reduce_sum(tf.mul(v1, v2), reduction_indices=1) / (tf.sqrt(tf.reduce_sum(tf.mul(v1, v1), reduction_indices=1)) * tf.sqrt(tf.reduce_sum(tf.mul(v2, v2), reduction_indices=1))) return cost @@ -542,7 +545,10 @@ def li(weights): # standard_ops_fn = standard_ops.mul # else: standard_ops_fn = standard_ops.multiply - return standard_ops_fn(my_scale, standard_ops.reduce_sum(standard_ops.sqrt(standard_ops.reduce_sum(tf.square(weights), 1))), name=scope) + return standard_ops_fn( + my_scale, + standard_ops.reduce_sum(standard_ops.sqrt(standard_ops.reduce_sum(tf.square(weights), 1))), + name=scope) return li @@ -590,7 +596,10 @@ def lo(weights, name='lo_regularizer'): # standard_ops_fn = standard_ops.mul # else: standard_ops_fn = standard_ops.multiply - return standard_ops_fn(my_scale, standard_ops.reduce_sum(standard_ops.sqrt(standard_ops.reduce_sum(tf.square(weights), 0))), name=scope) + return standard_ops_fn( + my_scale, + standard_ops.reduce_sum(standard_ops.sqrt(standard_ops.reduce_sum(tf.square(weights), 0))), + name=scope) return lo @@ -687,7 +696,8 @@ def mn_o(weights, name='maxnorm_o_regularizer'): standard_ops_fn = standard_ops.mul else: standard_ops_fn = standard_ops.multiply - return standard_ops_fn(my_scale, standard_ops.reduce_sum(standard_ops.reduce_max(standard_ops.abs(weights), 0)), name=scope) + return standard_ops_fn( + my_scale, standard_ops.reduce_sum(standard_ops.reduce_max(standard_ops.abs(weights), 0)), name=scope) return mn_o @@ -735,6 +745,7 @@ def mn_i(weights, name='maxnorm_i_regularizer'): standard_ops_fn = standard_ops.mul else: standard_ops_fn = standard_ops.multiply - return standard_ops_fn(my_scale, standard_ops.reduce_sum(standard_ops.reduce_max(standard_ops.abs(weights), 1)), name=scope) + return standard_ops_fn( + my_scale, standard_ops.reduce_sum(standard_ops.reduce_max(standard_ops.abs(weights), 1)), name=scope) return mn_i diff --git a/tensorlayer/db.py b/tensorlayer/db.py index 2499d7d46..265096c56 100644 --- a/tensorlayer/db.py +++ b/tensorlayer/db.py @@ -59,7 +59,8 @@ class TensorDB(object): - You may like to install MongoChef or Mongo Management Studo APP for visualizing or testing your MongoDB. """ - def __init__(self, ip='localhost', port=27017, db_name='db_name', user_name=None, password='password', studyID=None): + def __init__(self, ip='localhost', port=27017, db_name='db_name', user_name=None, password='password', + studyID=None): ## connect mongodb client = MongoClient(ip, port) self.db = client[db_name] @@ -417,7 +418,14 @@ def push_job(self, margs, wargs, dargs, epoch): _ms, mid = self.load_model_architecture(margs) _weight, wid = self.find_one_params(wargs) - args = {"weight": wid, "model": mid, "dargs": dargs, "epoch": epoch, "time": datetime.utcnow(), "Running": False} + args = { + "weight": wid, + "model": mid, + "dargs": dargs, + "epoch": epoch, + "time": datetime.utcnow(), + "Running": False + } self.__autofill(args) self.db.JOBS.insert_one(args) diff --git a/tensorlayer/distributed.py b/tensorlayer/distributed.py index 53c1b407d..55788892c 100644 --- a/tensorlayer/distributed.py +++ b/tensorlayer/distributed.py @@ -71,7 +71,11 @@ def __init__(self, task_type='master', index=0, trial=None, ps_hosts=None, worke self.num_ps = len(self.ps_hosts) self.worker_hosts = worker_hosts if isinstance(worker_hosts, list) else worker_hosts.split(',') if master is not None and len(master) > 0: - self._cluster_spec = tf.train.ClusterSpec({'ps': self.ps_hosts, 'worker': self.worker_hosts, 'master': master}) + self._cluster_spec = tf.train.ClusterSpec({ + 'ps': self.ps_hosts, + 'worker': self.worker_hosts, + 'master': master + }) # master is a worker too self.num_workers = len(self.worker_hosts) + 1 if self.type == 'worker': @@ -104,7 +108,8 @@ def device_fn(self): """Returns the function with the specification to create the graph in this server""" current_device = '/job:{}/task:{}'.format(self.type, self._index) ps_devices = '/job:ps' - return tf.train.replica_device_setter(ps_device=ps_devices, worker_device=current_device, cluster=self._cluster_spec) + return tf.train.replica_device_setter( + ps_device=ps_devices, worker_device=current_device, cluster=self._cluster_spec) def create_server(self): if self._server is None and self.ps_hosts and self.worker_hosts and not self.is_evaluator(): @@ -132,7 +137,12 @@ def use_last_worker_as_evaluator(self): if self.num_workers <= 1: raise Exception('You need more than one worker instance to use one as evaluator') return TaskSpecDef( - task_type=self.type, index=self._index, trial=self.trial, ps_hosts=self.ps_hosts, worker_hosts=self.worker_hosts[:-1], master=self.master) + task_type=self.type, + index=self._index, + trial=self.trial, + ps_hosts=self.ps_hosts, + worker_hosts=self.worker_hosts[:-1], + master=self.master) def create_task_spec_def(): diff --git a/tensorlayer/files.py b/tensorlayer/files.py index 96320de01..4e7fadae8 100644 --- a/tensorlayer/files.py +++ b/tensorlayer/files.py @@ -107,7 +107,8 @@ def load_fashion_mnist_dataset(shape=(-1, 784), path='data'): >>> X_train, y_train, X_val, y_val, X_test, y_test = tl.files.load_fashion_mnist_dataset(shape=(-1,784), path='datasets') >>> X_train, y_train, X_val, y_val, X_test, y_test = tl.files.load_fashion_mnist_dataset(shape=(-1, 28, 28, 1)) """ - return _load_mnist_dataset(shape, path, name='fashion_mnist', url='http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/') + return _load_mnist_dataset( + shape, path, name='fashion_mnist', url='http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/') def _load_mnist_dataset(shape, path, name='mnist', url='http://yann.lecun.com/exdb/mnist/'): @@ -493,7 +494,15 @@ def load_matt_mahoney_text8_dataset(path='data'): return word_list -def load_imdb_dataset(path='data', nb_words=None, skip_top=0, maxlen=None, test_split=0.2, seed=113, start_char=1, oov_char=2, index_from=3): +def load_imdb_dataset(path='data', + nb_words=None, + skip_top=0, + maxlen=None, + test_split=0.2, + seed=113, + start_char=1, + oov_char=2, + index_from=3): """Load IMDB dataset. Parameters @@ -563,7 +572,8 @@ def load_imdb_dataset(path='data', nb_words=None, skip_top=0, maxlen=None, test_ X = new_X labels = new_labels if not X: - raise Exception('After filtering for sequences shorter than maxlen=' + str(maxlen) + ', no sequence was kept. ' 'Increase maxlen.') + raise Exception('After filtering for sequences shorter than maxlen=' + str(maxlen) + ', no sequence was kept. ' + 'Increase maxlen.') if not nb_words: nb_words = max([max(x) for x in X]) @@ -799,7 +809,8 @@ def load_flickr1M_dataset(tag='sky', size=10, path="data", n_threads=50, printab path = os.path.join(path, 'flickr1M') logging.info("[Flickr1M] using {}% of images = {}".format(size * 10, size * 100000)) images_zip = [ - 'images0.zip', 'images1.zip', 'images2.zip', 'images3.zip', 'images4.zip', 'images5.zip', 'images6.zip', 'images7.zip', 'images8.zip', 'images9.zip' + 'images0.zip', 'images1.zip', 'images2.zip', 'images3.zip', 'images4.zip', 'images5.zip', 'images6.zip', + 'images7.zip', 'images8.zip', 'images9.zip' ] tag_zip = 'tags.zip' url = 'http://press.liacs.nl/mirflickr/mirflickr1m/' @@ -930,7 +941,8 @@ def download_file_from_google_drive(ID, destination): def save_response_content(response, destination, chunk_size=32 * 1024): total_size = int(response.headers.get('content-length', 0)) with open(destination, "wb") as f: - for chunk in tqdm(response.iter_content(chunk_size), total=total_size, unit='B', unit_scale=True, desc=destination): + for chunk in tqdm( + response.iter_content(chunk_size), total=total_size, unit='B', unit_scale=True, desc=destination): if chunk: # filter out keep-alive new chunks f.write(chunk) @@ -1097,7 +1109,8 @@ def _recursive_parse_xml_to_dict(xml): elif dataset == "2012test": extracted_filename = "VOC2012test" #"VOCdevkit/VOC2012" logging.info(" [============= VOC 2012 Test Set =============]") - logging.info(" \nAuthor: 2012test only have person annotation, so 2007test is highly recommended for testing !\n") + logging.info( + " \nAuthor: 2012test only have person annotation, so 2007test is highly recommended for testing !\n") import time time.sleep(3) if os.path.isdir(os.path.join(path, extracted_filename)) is False: @@ -1150,8 +1163,8 @@ def _recursive_parse_xml_to_dict(xml): del_folder(os.path.join(path, 'VOCdevkit')) # object classes(labels) NOTE: YOU CAN CUSTOMIZE THIS LIST classes = [ - "aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow", "diningtable", "dog", "horse", "motorbike", "person", - "pottedplant", "sheep", "sofa", "train", "tvmonitor" + "aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow", "diningtable", "dog", + "horse", "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor" ] if contain_classes_in_person: classes_in_person = ["head", "hand", "foot"] @@ -1168,7 +1181,8 @@ def _recursive_parse_xml_to_dict(xml): folder_imgs = os.path.join(path, extracted_filename, "JPEGImages") imgs_file_list = load_file_list(path=folder_imgs, regx='\\.jpg', printable=False) logging.info("[VOC] {} images found".format(len(imgs_file_list))) - imgs_file_list.sort(key=lambda s: int(s.replace('.', ' ').replace('_', '').split(' ')[-2])) # 2007_000027.jpg --> 2007000027 + imgs_file_list.sort( + key=lambda s: int(s.replace('.', ' ').replace('_', '').split(' ')[-2])) # 2007_000027.jpg --> 2007000027 imgs_file_list = [os.path.join(folder_imgs, s) for s in imgs_file_list] # logging.info('IM',imgs_file_list[0::3333], imgs_file_list[-1]) if dataset != "2012test": @@ -1177,7 +1191,8 @@ def _recursive_parse_xml_to_dict(xml): folder_semseg = os.path.join(path, extracted_filename, "SegmentationClass") imgs_semseg_file_list = load_file_list(path=folder_semseg, regx='\\.png', printable=False) logging.info("[VOC] {} maps for semantic segmentation found".format(len(imgs_semseg_file_list))) - imgs_semseg_file_list.sort(key=lambda s: int(s.replace('.', ' ').replace('_', '').split(' ')[-2])) # 2007_000032.png --> 2007000032 + imgs_semseg_file_list.sort( + key=lambda s: int(s.replace('.', ' ').replace('_', '').split(' ')[-2])) # 2007_000032.png --> 2007000032 imgs_semseg_file_list = [os.path.join(folder_semseg, s) for s in imgs_semseg_file_list] # logging.info('Semantic Seg IM',imgs_semseg_file_list[0::333], imgs_semseg_file_list[-1]) ##======== 3. instance segmentation maps path list @@ -1185,7 +1200,8 @@ def _recursive_parse_xml_to_dict(xml): folder_insseg = os.path.join(path, extracted_filename, "SegmentationObject") imgs_insseg_file_list = load_file_list(path=folder_insseg, regx='\\.png', printable=False) logging.info("[VOC] {} maps for instance segmentation found".format(len(imgs_semseg_file_list))) - imgs_insseg_file_list.sort(key=lambda s: int(s.replace('.', ' ').replace('_', '').split(' ')[-2])) # 2007_000032.png --> 2007000032 + imgs_insseg_file_list.sort( + key=lambda s: int(s.replace('.', ' ').replace('_', '').split(' ')[-2])) # 2007_000032.png --> 2007000032 imgs_insseg_file_list = [os.path.join(folder_insseg, s) for s in imgs_insseg_file_list] # logging.info('Instance Seg IM',imgs_insseg_file_list[0::333], imgs_insseg_file_list[-1]) else: @@ -1195,8 +1211,10 @@ def _recursive_parse_xml_to_dict(xml): # folder_ann = path+"/"+extracted_filename+"/Annotations/" folder_ann = os.path.join(path, extracted_filename, "Annotations") imgs_ann_file_list = load_file_list(path=folder_ann, regx='\\.xml', printable=False) - logging.info("[VOC] {} XML annotation files for bounding box and object class found".format(len(imgs_ann_file_list))) - imgs_ann_file_list.sort(key=lambda s: int(s.replace('.', ' ').replace('_', '').split(' ')[-2])) # 2007_000027.xml --> 2007000027 + logging.info("[VOC] {} XML annotation files for bounding box and object class found".format( + len(imgs_ann_file_list))) + imgs_ann_file_list.sort( + key=lambda s: int(s.replace('.', ' ').replace('_', '').split(' ')[-2])) # 2007_000027.xml --> 2007000027 imgs_ann_file_list = [os.path.join(folder_ann, s) for s in imgs_ann_file_list] # logging.info('ANN',imgs_ann_file_list[0::3333], imgs_ann_file_list[-1]) @@ -1248,7 +1266,8 @@ def convert_annotation(file_name): continue cls_id = classes.index(cls) xmlbox = obj.find('bndbox') - b = (float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), float(xmlbox.find('ymin').text), float(xmlbox.find('ymax').text)) + b = (float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), float(xmlbox.find('ymin').text), + float(xmlbox.find('ymax').text)) bb = convert((w, h), b) out_file += str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n' @@ -1260,7 +1279,8 @@ def convert_annotation(file_name): continue cls_id = classes.index(cls) xmlbox = part.find('bndbox') - b = (float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), float(xmlbox.find('ymin').text), float(xmlbox.find('ymax').text)) + b = (float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), + float(xmlbox.find('ymin').text), float(xmlbox.find('ymax').text)) bb = convert((w, h), b) # out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n') out_file += str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n' @@ -1387,8 +1407,9 @@ def save_joints(): head_rect = [] if 'x1' in str(anno['annorect'].dtype): - head_rect = zip([x1[0, 0] for x1 in anno['annorect']['x1'][0]], [y1[0, 0] for y1 in anno['annorect']['y1'][0]], - [x2[0, 0] for x2 in anno['annorect']['x2'][0]], [y2[0, 0] for y2 in anno['annorect']['y2'][0]]) + head_rect = zip( + [x1[0, 0] for x1 in anno['annorect']['x1'][0]], [y1[0, 0] for y1 in anno['annorect']['y1'][0]], + [x2[0, 0] for x2 in anno['annorect']['x2'][0]], [y2[0, 0] for y2 in anno['annorect']['y2'][0]]) else: head_rect = [] # TODO @@ -1398,9 +1419,15 @@ def save_joints(): head_y1s = anno['annorect']['y1'][0] head_x2s = anno['annorect']['x2'][0] head_y2s = anno['annorect']['y2'][0] - for annopoint, head_x1, head_y1, head_x2, head_y2 in zip(annopoints, head_x1s, head_y1s, head_x2s, head_y2s): + for annopoint, head_x1, head_y1, head_x2, head_y2 in zip(annopoints, head_x1s, head_y1s, head_x2s, + head_y2s): if annopoint != []: - head_rect = [float(head_x1[0, 0]), float(head_y1[0, 0]), float(head_x2[0, 0]), float(head_y2[0, 0])] + head_rect = [ + float(head_x1[0, 0]), + float(head_y1[0, 0]), + float(head_x2[0, 0]), + float(head_y2[0, 0]) + ] # joint coordinates annopoint = annopoint['point'][0, 0] @@ -1422,7 +1449,13 @@ def save_joints(): # if len(joint_pos) == 16: if ((is_16_pos_only == True) and (len(joint_pos) == 16)) or (is_16_pos_only == False): # only use image with 16 key points / or use all - data = {'filename': img_fn, 'train': train_flag, 'head_rect': head_rect, 'is_visible': vis, 'joint_pos': joint_pos} + data = { + 'filename': img_fn, + 'train': train_flag, + 'head_rect': head_rect, + 'is_visible': vis, + 'joint_pos': joint_pos + } # print(json.dumps(data), file=fp) # py3 if train_flag: ann_train_list[-1].append(data) @@ -1559,7 +1592,9 @@ def save_npz(save_list=None, name='model.npz', sess=None): try: save_list_var.extend([v.eval() for v in save_list]) except Exception: - logging.info(" Fail to save model, Hint: pass the session into this function, tl.files.save_npz(network.all_params, name='model.npz', sess=sess)") + logging.info( + " Fail to save model, Hint: pass the session into this function, tl.files.save_npz(network.all_params, name='model.npz', sess=sess)" + ) np.savez(name, params=save_list_var) save_list_var = None del save_list_var @@ -1736,7 +1771,12 @@ def load_and_assign_npz_dict(name='model.npz', sess=None): logging.info("[*] Model restored from npz_dict %s" % name) -def save_ckpt(sess=None, mode_name='model.ckpt', save_dir='checkpoint', var_list=None, global_step=None, printable=False): +def save_ckpt(sess=None, + mode_name='model.ckpt', + save_dir='checkpoint', + var_list=None, + global_step=None, + printable=False): """Save parameters into `ckpt` file. Parameters diff --git a/tensorlayer/iterate.py b/tensorlayer/iterate.py index dc3051c7a..9ea07f346 100644 --- a/tensorlayer/iterate.py +++ b/tensorlayer/iterate.py @@ -60,7 +60,8 @@ def minibatches(inputs=None, targets=None, batch_size=None, shuffle=False): else: excerpt = slice(start_idx, start_idx + batch_size) if (isinstance(inputs, list) or isinstance(targets, list)) and (shuffle == True): - yield [inputs[i] for i in excerpt], [targets[i] for i in excerpt] # zsdonghao: for list indexing when shuffle==True + yield [inputs[i] for i in excerpt], [targets[i] + for i in excerpt] # zsdonghao: for list indexing when shuffle==True else: yield inputs[excerpt], targets[excerpt] diff --git a/tensorlayer/layers/binary.py b/tensorlayer/layers/binary.py index 4987fe767..e8052198c 100644 --- a/tensorlayer/layers/binary.py +++ b/tensorlayer/layers/binary.py @@ -80,7 +80,9 @@ def _compute_alpha(x): alpha1_temp2 = tf.where(tf.less(x, -threshold), x, tf.zeros_like(x, tf.float32)) alpha_array = tf.add(alpha1_temp1, alpha1_temp2, name=None) alpha_array_abs = tf.abs(alpha_array) - alpha_array_abs1 = tf.where(tf.greater(alpha_array_abs, 0), tf.ones_like(alpha_array_abs, tf.float32), tf.zeros_like(alpha_array_abs, tf.float32)) + alpha_array_abs1 = tf.where( + tf.greater(alpha_array_abs, 0), tf.ones_like(alpha_array_abs, tf.float32), + tf.zeros_like(alpha_array_abs, tf.float32)) alpha_sum = tf.reduce_sum(alpha_array_abs) n = tf.reduce_sum(alpha_array_abs1) alpha = tf.div(alpha_sum, n) @@ -159,14 +161,16 @@ def __init__( self.n_units = n_units with tf.variable_scope(name): - W = tf.get_variable(name='W', shape=(n_in, n_units), initializer=W_init, dtype=LayersConfig.tf_dtype, **W_init_args) + W = tf.get_variable( + name='W', shape=(n_in, n_units), initializer=W_init, dtype=LayersConfig.tf_dtype, **W_init_args) # W = tl.act.sign(W) # dont update ... W = quantize(W) # W = tf.Variable(W) # print(W) if b_init is not None: try: - b = tf.get_variable(name='b', shape=(n_units), initializer=b_init, dtype=LayersConfig.tf_dtype, **b_init_args) + b = tf.get_variable( + name='b', shape=(n_units), initializer=b_init, dtype=LayersConfig.tf_dtype, **b_init_args) except Exception: # If initializer is a constant, do not specify shape. b = tf.get_variable(name='b', initializer=b_init, dtype=LayersConfig.tf_dtype, **b_init_args) self.outputs = act(tf.matmul(self.inputs, W) + b) @@ -263,7 +267,9 @@ def __init__( name='binary_cnn2d', ): super(BinaryConv2d, self).__init__(prev_layer=prev_layer, name=name) - logging.info("BinaryConv2d %s: n_filter:%d filter_size:%s strides:%s pad:%s act:%s" % (name, n_filter, str(filter_size), str(strides), padding, + logging.info("BinaryConv2d %s: n_filter:%d filter_size:%s strides:%s pad:%s act:%s" % (name, n_filter, + str(filter_size), + str(strides), padding, act.__name__)) self.inputs = prev_layer.outputs @@ -287,14 +293,29 @@ def __init__( shape = (filter_size[0], filter_size[1], pre_channel, n_filter) strides = (1, strides[0], strides[1], 1) with tf.variable_scope(name): - W = tf.get_variable(name='W_conv2d', shape=shape, initializer=W_init, dtype=LayersConfig.tf_dtype, **W_init_args) + W = tf.get_variable( + name='W_conv2d', shape=shape, initializer=W_init, dtype=LayersConfig.tf_dtype, **W_init_args) W = quantize(W) if b_init: - b = tf.get_variable(name='b_conv2d', shape=(shape[-1]), initializer=b_init, dtype=LayersConfig.tf_dtype, **b_init_args) + b = tf.get_variable( + name='b_conv2d', shape=(shape[-1]), initializer=b_init, dtype=LayersConfig.tf_dtype, **b_init_args) self.outputs = act( - tf.nn.conv2d(self.inputs, W, strides=strides, padding=padding, use_cudnn_on_gpu=use_cudnn_on_gpu, data_format=data_format) + b) + tf.nn.conv2d( + self.inputs, + W, + strides=strides, + padding=padding, + use_cudnn_on_gpu=use_cudnn_on_gpu, + data_format=data_format) + b) else: - self.outputs = act(tf.nn.conv2d(self.inputs, W, strides=strides, padding=padding, use_cudnn_on_gpu=use_cudnn_on_gpu, data_format=data_format)) + self.outputs = act( + tf.nn.conv2d( + self.inputs, + W, + strides=strides, + padding=padding, + use_cudnn_on_gpu=use_cudnn_on_gpu, + data_format=data_format)) self.all_layers.append(self.outputs) if b_init: @@ -363,7 +384,8 @@ def __init__( self.n_units = n_units with tf.variable_scope(name): - W = tf.get_variable(name='W', shape=(n_in, n_units), initializer=W_init, dtype=LayersConfig.tf_dtype, **W_init_args) + W = tf.get_variable( + name='W', shape=(n_in, n_units), initializer=W_init, dtype=LayersConfig.tf_dtype, **W_init_args) # W = tl.act.sign(W) # dont update ... alpha = _compute_alpha(W) W = _ternary_operation(W) @@ -372,7 +394,8 @@ def __init__( # print(W) if b_init is not None: try: - b = tf.get_variable(name='b', shape=(n_units), initializer=b_init, dtype=LayersConfig.tf_dtype, **b_init_args) + b = tf.get_variable( + name='b', shape=(n_units), initializer=b_init, dtype=LayersConfig.tf_dtype, **b_init_args) except Exception: # If initializer is a constant, do not specify shape. b = tf.get_variable(name='b', initializer=b_init, dtype=LayersConfig.tf_dtype, **b_init_args) self.outputs = act(tf.matmul(self.inputs, W) + b) @@ -469,7 +492,9 @@ def __init__( name='ternary_cnn2d', ): super(TernaryConv2d, self).__init__(prev_layer=prev_layer, name=name) - logging.info("TernaryConv2d %s: n_filter:%d filter_size:%s strides:%s pad:%s act:%s" % (name, n_filter, str(filter_size), str(strides), padding, + logging.info("TernaryConv2d %s: n_filter:%d filter_size:%s strides:%s pad:%s act:%s" % (name, n_filter, + str(filter_size), + str(strides), padding, act.__name__)) if W_init_args is None: @@ -491,16 +516,31 @@ def __init__( shape = (filter_size[0], filter_size[1], pre_channel, n_filter) strides = (1, strides[0], strides[1], 1) with tf.variable_scope(name): - W = tf.get_variable(name='W_conv2d', shape=shape, initializer=W_init, dtype=LayersConfig.tf_dtype, **W_init_args) + W = tf.get_variable( + name='W_conv2d', shape=shape, initializer=W_init, dtype=LayersConfig.tf_dtype, **W_init_args) alpha = _compute_alpha(W) W = _ternary_operation(W) W = tf.multiply(alpha, W) if b_init: - b = tf.get_variable(name='b_conv2d', shape=(shape[-1]), initializer=b_init, dtype=LayersConfig.tf_dtype, **b_init_args) + b = tf.get_variable( + name='b_conv2d', shape=(shape[-1]), initializer=b_init, dtype=LayersConfig.tf_dtype, **b_init_args) self.outputs = act( - tf.nn.conv2d(self.inputs, W, strides=strides, padding=padding, use_cudnn_on_gpu=use_cudnn_on_gpu, data_format=data_format) + b) + tf.nn.conv2d( + self.inputs, + W, + strides=strides, + padding=padding, + use_cudnn_on_gpu=use_cudnn_on_gpu, + data_format=data_format) + b) else: - self.outputs = act(tf.nn.conv2d(self.inputs, W, strides=strides, padding=padding, use_cudnn_on_gpu=use_cudnn_on_gpu, data_format=data_format)) + self.outputs = act( + tf.nn.conv2d( + self.inputs, + W, + strides=strides, + padding=padding, + use_cudnn_on_gpu=use_cudnn_on_gpu, + data_format=data_format)) self.all_layers.append(self.outputs) if b_init: @@ -576,7 +616,8 @@ def __init__( self.n_units = n_units with tf.variable_scope(name): - W = tf.get_variable(name='W', shape=(n_in, n_units), initializer=W_init, dtype=LayersConfig.tf_dtype, **W_init_args) + W = tf.get_variable( + name='W', shape=(n_in, n_units), initializer=W_init, dtype=LayersConfig.tf_dtype, **W_init_args) # W = tl.act.sign(W) # dont update ... W = _quantize_weight(W, bitW) self.inputs = _quantize_active(_cabs(self.inputs), bitA) @@ -584,7 +625,8 @@ def __init__( # print(W) if b_init is not None: try: - b = tf.get_variable(name='b', shape=(n_units), initializer=b_init, dtype=LayersConfig.tf_dtype, **b_init_args) + b = tf.get_variable( + name='b', shape=(n_units), initializer=b_init, dtype=LayersConfig.tf_dtype, **b_init_args) except Exception: # If initializer is a constant, do not specify shape. b = tf.get_variable(name='b', initializer=b_init, dtype=LayersConfig.tf_dtype, **b_init_args) self.outputs = act(tf.matmul(self.inputs, W) + b) @@ -687,7 +729,9 @@ def __init__( name='dorefa_cnn2d', ): super(DorefaConv2d, self).__init__(prev_layer=prev_layer, name=name) - logging.info("DorefaConv2d %s: n_filter:%d filter_size:%s strides:%s pad:%s act:%s" % (name, n_filter, str(filter_size), str(strides), padding, + logging.info("DorefaConv2d %s: n_filter:%d filter_size:%s strides:%s pad:%s act:%s" % (name, n_filter, + str(filter_size), + str(strides), padding, act.__name__)) self.inputs = prev_layer.outputs @@ -712,15 +756,30 @@ def __init__( shape = (filter_size[0], filter_size[1], pre_channel, n_filter) strides = (1, strides[0], strides[1], 1) with tf.variable_scope(name): - W = tf.get_variable(name='W_conv2d', shape=shape, initializer=W_init, dtype=LayersConfig.tf_dtype, **W_init_args) + W = tf.get_variable( + name='W_conv2d', shape=shape, initializer=W_init, dtype=LayersConfig.tf_dtype, **W_init_args) W = _quantize_weight(W, bitW) self.inputs = _quantize_active(_cabs(self.inputs), bitA) if b_init: - b = tf.get_variable(name='b_conv2d', shape=(shape[-1]), initializer=b_init, dtype=LayersConfig.tf_dtype, **b_init_args) + b = tf.get_variable( + name='b_conv2d', shape=(shape[-1]), initializer=b_init, dtype=LayersConfig.tf_dtype, **b_init_args) self.outputs = act( - tf.nn.conv2d(self.inputs, W, strides=strides, padding=padding, use_cudnn_on_gpu=use_cudnn_on_gpu, data_format=data_format) + b) + tf.nn.conv2d( + self.inputs, + W, + strides=strides, + padding=padding, + use_cudnn_on_gpu=use_cudnn_on_gpu, + data_format=data_format) + b) else: - self.outputs = act(tf.nn.conv2d(self.inputs, W, strides=strides, padding=padding, use_cudnn_on_gpu=use_cudnn_on_gpu, data_format=data_format)) + self.outputs = act( + tf.nn.conv2d( + self.inputs, + W, + strides=strides, + padding=padding, + use_cudnn_on_gpu=use_cudnn_on_gpu, + data_format=data_format)) self.all_layers.append(self.outputs) if b_init: diff --git a/tensorlayer/layers/convolution.py b/tensorlayer/layers/convolution.py index 468b161d5..66be13c52 100644 --- a/tensorlayer/layers/convolution.py +++ b/tensorlayer/layers/convolution.py @@ -79,7 +79,8 @@ def __init__( name='cnn1d', ): super(Conv1dLayer, self).__init__(prev_layer=prev_layer, name=name) - logging.info("Conv1dLayer %s: shape:%s stride:%s pad:%s act:%s" % (name, str(shape), str(stride), padding, act.__name__)) + logging.info("Conv1dLayer %s: shape:%s stride:%s pad:%s act:%s" % (name, str(shape), str(stride), padding, + act.__name__)) self.inputs = prev_layer.outputs @@ -92,10 +93,13 @@ def __init__( b_init_args = {} with tf.variable_scope(name): - W = tf.get_variable(name='W_conv1d', shape=shape, initializer=W_init, dtype=LayersConfig.tf_dtype, **W_init_args) - self.outputs = tf.nn.convolution(self.inputs, W, strides=(stride, ), padding=padding, dilation_rate=(dilation_rate, )) # 1.2 + W = tf.get_variable( + name='W_conv1d', shape=shape, initializer=W_init, dtype=LayersConfig.tf_dtype, **W_init_args) + self.outputs = tf.nn.convolution( + self.inputs, W, strides=(stride, ), padding=padding, dilation_rate=(dilation_rate, )) # 1.2 if b_init: - b = tf.get_variable(name='b_conv1d', shape=(shape[-1]), initializer=b_init, dtype=LayersConfig.tf_dtype, **b_init_args) + b = tf.get_variable( + name='b_conv1d', shape=(shape[-1]), initializer=b_init, dtype=LayersConfig.tf_dtype, **b_init_args) self.outputs = self.outputs + b self.outputs = act(self.outputs) @@ -192,7 +196,8 @@ def __init__( name='cnn_layer', ): super(Conv2dLayer, self).__init__(prev_layer=prev_layer, name=name) - logging.info("Conv2dLayer %s: shape:%s strides:%s pad:%s act:%s" % (name, str(shape), str(strides), padding, act.__name__)) + logging.info("Conv2dLayer %s: shape:%s strides:%s pad:%s act:%s" % (name, str(shape), str(strides), padding, + act.__name__)) self.inputs = prev_layer.outputs @@ -204,13 +209,28 @@ def __init__( act = tf.identity with tf.variable_scope(name): - W = tf.get_variable(name='W_conv2d', shape=shape, initializer=W_init, dtype=LayersConfig.tf_dtype, **W_init_args) + W = tf.get_variable( + name='W_conv2d', shape=shape, initializer=W_init, dtype=LayersConfig.tf_dtype, **W_init_args) if b_init: - b = tf.get_variable(name='b_conv2d', shape=(shape[-1]), initializer=b_init, dtype=LayersConfig.tf_dtype, **b_init_args) + b = tf.get_variable( + name='b_conv2d', shape=(shape[-1]), initializer=b_init, dtype=LayersConfig.tf_dtype, **b_init_args) self.outputs = act( - tf.nn.conv2d(self.inputs, W, strides=strides, padding=padding, use_cudnn_on_gpu=use_cudnn_on_gpu, data_format=data_format) + b) + tf.nn.conv2d( + self.inputs, + W, + strides=strides, + padding=padding, + use_cudnn_on_gpu=use_cudnn_on_gpu, + data_format=data_format) + b) else: - self.outputs = act(tf.nn.conv2d(self.inputs, W, strides=strides, padding=padding, use_cudnn_on_gpu=use_cudnn_on_gpu, data_format=data_format)) + self.outputs = act( + tf.nn.conv2d( + self.inputs, + W, + strides=strides, + padding=padding, + use_cudnn_on_gpu=use_cudnn_on_gpu, + data_format=data_format)) self.all_layers.append(self.outputs) if b_init: @@ -312,7 +332,9 @@ def __init__( name='decnn2d_layer', ): super(DeConv2dLayer, self).__init__(prev_layer=prev_layer, name=name) - logging.info("DeConv2dLayer %s: shape:%s out_shape:%s strides:%s pad:%s act:%s" % (name, str(shape), str(output_shape), str(strides), padding, + logging.info("DeConv2dLayer %s: shape:%s out_shape:%s strides:%s pad:%s act:%s" % (name, str(shape), + str(output_shape), + str(strides), padding, act.__name__)) self.inputs = prev_layer.outputs @@ -326,12 +348,21 @@ def __init__( # logging.info(" DeConv2dLayer: Untested") with tf.variable_scope(name): - W = tf.get_variable(name='W_deconv2d', shape=shape, initializer=W_init, dtype=LayersConfig.tf_dtype, **W_init_args) + W = tf.get_variable( + name='W_deconv2d', shape=shape, initializer=W_init, dtype=LayersConfig.tf_dtype, **W_init_args) if b_init: - b = tf.get_variable(name='b_deconv2d', shape=(shape[-2]), initializer=b_init, dtype=LayersConfig.tf_dtype, **b_init_args) - self.outputs = act(tf.nn.conv2d_transpose(self.inputs, W, output_shape=output_shape, strides=strides, padding=padding) + b) + b = tf.get_variable( + name='b_deconv2d', + shape=(shape[-2]), + initializer=b_init, + dtype=LayersConfig.tf_dtype, + **b_init_args) + self.outputs = act( + tf.nn.conv2d_transpose(self.inputs, W, output_shape=output_shape, strides=strides, padding=padding) + + b) else: - self.outputs = act(tf.nn.conv2d_transpose(self.inputs, W, output_shape=output_shape, strides=strides, padding=padding)) + self.outputs = act( + tf.nn.conv2d_transpose(self.inputs, W, output_shape=output_shape, strides=strides, padding=padding)) # self.all_layers = list(layer.all_layers) # self.all_params = list(layer.all_params) @@ -394,7 +425,8 @@ def __init__( name='cnn3d_layer', ): super(Conv3dLayer, self).__init__(prev_layer=prev_layer, name=name) - logging.info("Conv3dLayer %s: shape:%s strides:%s pad:%s act:%s" % (name, str(shape), str(strides), padding, act.__name__)) + logging.info("Conv3dLayer %s: shape:%s strides:%s pad:%s act:%s" % (name, str(shape), str(strides), padding, + act.__name__)) self.inputs = prev_layer.outputs @@ -408,9 +440,11 @@ def __init__( with tf.variable_scope(name): # W = tf.Variable(W_init(shape=shape, **W_init_args), name='W_conv') # b = tf.Variable(b_init(shape=[shape[-1]], **b_init_args), name='b_conv') - W = tf.get_variable(name='W_conv3d', shape=shape, initializer=W_init, dtype=LayersConfig.tf_dtype, **W_init_args) + W = tf.get_variable( + name='W_conv3d', shape=shape, initializer=W_init, dtype=LayersConfig.tf_dtype, **W_init_args) if b_init: - b = tf.get_variable(name='b_conv3d', shape=(shape[-1]), initializer=b_init, dtype=LayersConfig.tf_dtype, **b_init_args) + b = tf.get_variable( + name='b_conv3d', shape=(shape[-1]), initializer=b_init, dtype=LayersConfig.tf_dtype, **b_init_args) self.outputs = act(tf.nn.conv3d(self.inputs, W, strides=strides, padding=padding, name=None) + b) else: self.outputs = act(tf.nn.conv3d(self.inputs, W, strides=strides, padding=padding, name=None)) @@ -474,7 +508,9 @@ def __init__( name='decnn3d_layer', ): super(DeConv3dLayer, self).__init__(prev_layer=prev_layer, name=name) - logging.info("DeConv3dLayer %s: shape:%s out_shape:%s strides:%s pad:%s act:%s" % (name, str(shape), str(output_shape), str(strides), padding, + logging.info("DeConv3dLayer %s: shape:%s out_shape:%s strides:%s pad:%s act:%s" % (name, str(shape), + str(output_shape), + str(strides), padding, act.__name__)) self.inputs = prev_layer.outputs @@ -487,12 +523,21 @@ def __init__( act = tf.identity with tf.variable_scope(name): - W = tf.get_variable(name='W_deconv3d', shape=shape, initializer=W_init, dtype=LayersConfig.tf_dtype, **W_init_args) + W = tf.get_variable( + name='W_deconv3d', shape=shape, initializer=W_init, dtype=LayersConfig.tf_dtype, **W_init_args) if b_init: - b = tf.get_variable(name='b_deconv3d', shape=(shape[-2]), initializer=b_init, dtype=LayersConfig.tf_dtype, **b_init_args) - self.outputs = act(tf.nn.conv3d_transpose(self.inputs, W, output_shape=output_shape, strides=strides, padding=padding) + b) + b = tf.get_variable( + name='b_deconv3d', + shape=(shape[-2]), + initializer=b_init, + dtype=LayersConfig.tf_dtype, + **b_init_args) + self.outputs = act( + tf.nn.conv3d_transpose(self.inputs, W, output_shape=output_shape, strides=strides, padding=padding) + + b) else: - self.outputs = act(tf.nn.conv3d_transpose(self.inputs, W, output_shape=output_shape, strides=strides, padding=padding)) + self.outputs = act( + tf.nn.conv3d_transpose(self.inputs, W, output_shape=output_shape, strides=strides, padding=padding)) # self.all_layers = list(layer.all_layers) # self.all_params = list(layer.all_params) @@ -539,7 +584,8 @@ def __init__( name='upsample2d_layer', ): super(UpSampling2dLayer, self).__init__(prev_layer=prev_layer, name=name) - logging.info("UpSampling2dLayer %s: is_scale:%s size:%s method:%d align_corners:%s" % (name, is_scale, size, method, align_corners)) + logging.info("UpSampling2dLayer %s: is_scale:%s size:%s method:%d align_corners:%s" % (name, is_scale, size, + method, align_corners)) self.inputs = prev_layer.outputs @@ -563,9 +609,11 @@ def __init__( with tf.variable_scope(name): try: - self.outputs = tf.image.resize_images(self.inputs, size=size, method=method, align_corners=align_corners) + self.outputs = tf.image.resize_images( + self.inputs, size=size, method=method, align_corners=align_corners) except Exception: # for TF 0.10 - self.outputs = tf.image.resize_images(self.inputs, new_height=size[0], new_width=size[1], method=method, align_corners=align_corners) + self.outputs = tf.image.resize_images( + self.inputs, new_height=size[0], new_width=size[1], method=method, align_corners=align_corners) # self.all_layers = list(layer.all_layers) # self.all_params = list(layer.all_params) @@ -608,7 +656,8 @@ def __init__( name='downsample2d_layer', ): super(DownSampling2dLayer, self).__init__(prev_layer=prev_layer, name=name) - logging.info("DownSampling2dLayer %s: is_scale:%s size:%s method:%d, align_corners:%s" % (name, is_scale, size, method, align_corners)) + logging.info("DownSampling2dLayer %s: is_scale:%s size:%s method:%d, align_corners:%s" % + (name, is_scale, size, method, align_corners)) self.inputs = prev_layer.outputs @@ -630,9 +679,11 @@ def __init__( with tf.variable_scope(name): try: - self.outputs = tf.image.resize_images(self.inputs, size=size, method=method, align_corners=align_corners) + self.outputs = tf.image.resize_images( + self.inputs, size=size, method=method, align_corners=align_corners) except Exception: # for TF 0.10 - self.outputs = tf.image.resize_images(self.inputs, new_height=size[0], new_width=size[1], method=method, align_corners=align_corners) + self.outputs = tf.image.resize_images( + self.inputs, new_height=size[0], new_width=size[1], method=method, align_corners=align_corners) # self.all_layers = list(layer.all_layers) # self.all_params = list(layer.all_params) @@ -704,7 +755,8 @@ def __init__( b_init_args=None): if tf.__version__ < "1.4": - raise Exception("Deformable CNN layer requires tensrflow 1.4 or higher version | current version %s" % tf.__version__) + raise Exception( + "Deformable CNN layer requires tensrflow 1.4 or higher version | current version %s" % tf.__version__) if W_init_args is None: W_init_args = {} @@ -841,7 +893,8 @@ def _tf_batch_map_offsets(inputs, offsets, grid_offset): return mapped_vals super(DeformableConv2d, self).__init__(prev_layer=prev_layer, name=name) - logging.info("DeformableConv2d %s: n_filter: %d, filter_size: %s act:%s" % (name, n_filter, str(filter_size), act.__name__)) + logging.info("DeformableConv2d %s: n_filter: %d, filter_size: %s act:%s" % (name, n_filter, str(filter_size), + act.__name__)) self.inputs = prev_layer.outputs @@ -864,7 +917,8 @@ def _tf_batch_map_offsets(inputs, offsets, grid_offset): input_h = int(self.inputs.get_shape()[1]) input_w = int(self.inputs.get_shape()[2]) kernel_n = shape[0] * shape[1] - initial_offsets = tf.stack(tf.meshgrid(tf.range(shape[0]), tf.range(shape[1]), indexing='ij')) # initial_offsets --> (kh, kw, 2) + initial_offsets = tf.stack(tf.meshgrid(tf.range(shape[0]), tf.range(shape[1]), + indexing='ij')) # initial_offsets --> (kh, kw, 2) initial_offsets = tf.reshape(initial_offsets, (-1, 2)) # initial_offsets --> (n, 2) initial_offsets = tf.expand_dims(initial_offsets, 0) # initial_offsets --> (1, n, 2) initial_offsets = tf.expand_dims(initial_offsets, 0) # initial_offsets --> (1, 1, n, 2) @@ -891,7 +945,12 @@ def _tf_batch_map_offsets(inputs, offsets, grid_offset): **W_init_args) if b_init: - b = tf.get_variable(name='b_deformableconv2d', shape=(shape[-1]), initializer=b_init, dtype=LayersConfig.tf_dtype, **b_init_args) + b = tf.get_variable( + name='b_deformableconv2d', + shape=(shape[-1]), + initializer=b_init, + dtype=LayersConfig.tf_dtype, + **b_init_args) self.outputs = tf.reshape( act(tf.nn.conv3d(input_deform, W, strides=[1, 1, 1, 1, 1], padding='VALID', name=None) + b), (tf.shape(self.inputs)[0], input_h, input_w, shape[-1])) @@ -1044,7 +1103,8 @@ def __init__(self, name='atrou2d'): super(AtrousConv2dLayer, self).__init__(prev_layer=prev_layer, name=name) - logging.info("AtrousConv2dLayer %s: n_filter:%d filter_size:%s rate:%d pad:%s act:%s" % (name, n_filter, filter_size, rate, padding, act.__name__)) + logging.info("AtrousConv2dLayer %s: n_filter:%d filter_size:%s rate:%d pad:%s act:%s" % + (name, n_filter, filter_size, rate, padding, act.__name__)) self.inputs = prev_layer.outputs @@ -1057,9 +1117,11 @@ def __init__(self, with tf.variable_scope(name): shape = [filter_size[0], filter_size[1], int(self.inputs.get_shape()[-1]), n_filter] - filters = tf.get_variable(name='filter', shape=shape, initializer=W_init, dtype=LayersConfig.tf_dtype, **W_init_args) + filters = tf.get_variable( + name='filter', shape=shape, initializer=W_init, dtype=LayersConfig.tf_dtype, **W_init_args) if b_init: - b = tf.get_variable(name='b', shape=(n_filter), initializer=b_init, dtype=LayersConfig.tf_dtype, **b_init_args) + b = tf.get_variable( + name='b', shape=(n_filter), initializer=b_init, dtype=LayersConfig.tf_dtype, **b_init_args) self.outputs = act(tf.nn.atrous_conv2d(self.inputs, filters, rate, padding) + b) else: self.outputs = act(tf.nn.atrous_conv2d(self.inputs, filters, rate, padding)) @@ -1150,8 +1212,10 @@ def __init__(self, name='atrou2d'): super(_SeparableConv2dLayer, self).__init__(prev_layer=prev_layer, name=name) - logging.info("SeparableConv2dLayer %s: n_filter:%d filter_size:%s strides:%s padding:%s dilation_rate:%s depth_multiplier:%s act:%s" % - (name, n_filter, filter_size, str(strides), padding, str(dilation_rate), str(depth_multiplier), act.__name__)) + logging.info( + "SeparableConv2dLayer %s: n_filter:%d filter_size:%s strides:%s padding:%s dilation_rate:%s depth_multiplier:%s act:%s" + % (name, n_filter, filter_size, str(strides), padding, str(dilation_rate), str(depth_multiplier), + act.__name__)) self.inputs = prev_layer.outputs @@ -1231,7 +1295,8 @@ def deconv2d_bilinear_upsampling_initializer(shape): if shape[0] != shape[1]: raise Exception('deconv2d_bilinear_upsampling_initializer only supports symmetrical filter sizes') if shape[3] < shape[2]: - raise Exception('deconv2d_bilinear_upsampling_initializer behaviour is not defined for num_in_channels < num_out_channels ') + raise Exception( + 'deconv2d_bilinear_upsampling_initializer behaviour is not defined for num_in_channels < num_out_channels ') filter_size = shape[0] num_out_channels = shape[2] @@ -1325,8 +1390,8 @@ def __init__(self, name='conv1d'): super(Conv1d, self).__init__(prev_layer=prev_layer, name=name) - logging.info("Conv1d %s: n_filter:%d filter_size:%s stride:%d pad:%s act:%s dilation_rate:%d" % (name, n_filter, filter_size, stride, padding, - act.__name__, dilation_rate)) + logging.info("Conv1d %s: n_filter:%d filter_size:%s stride:%d pad:%s act:%s dilation_rate:%d" % + (name, n_filter, filter_size, stride, padding, act.__name__, dilation_rate)) self.inputs = prev_layer.outputs if tf.__version__ > '1.3': @@ -1482,7 +1547,9 @@ def __init__( act = tf.identity if tf.__version__ > '1.5': - logging.info("Conv2d %s: n_filter:%d filter_size:%s strides:%s pad:%s act:%s" % (self.name, n_filter, str(filter_size), str(strides), padding, + logging.info("Conv2d %s: n_filter:%d filter_size:%s strides:%s pad:%s act:%s" % (self.name, n_filter, + str(filter_size), + str(strides), padding, act.__name__)) # with tf.variable_scope(name) as vs: conv2d = tf.layers.Conv2D( @@ -1521,17 +1588,36 @@ def __init__( shape = (filter_size[0], filter_size[1], pre_channel, n_filter) # 32 features for each 5x5 patch strides = (1, strides[0], strides[1], 1) - logging.info("Conv2d %s: shape:%s strides:%s pad:%s act:%s" % (self.name, str(shape), str(strides), padding, act.__name__)) + logging.info("Conv2d %s: shape:%s strides:%s pad:%s act:%s" % (self.name, str(shape), str(strides), padding, + act.__name__)) with tf.variable_scope(name): - W = tf.get_variable(name='W_conv2d', shape=shape, initializer=W_init, dtype=LayersConfig.tf_dtype, **W_init_args) + W = tf.get_variable( + name='W_conv2d', shape=shape, initializer=W_init, dtype=LayersConfig.tf_dtype, **W_init_args) if b_init: - b = tf.get_variable(name='b_conv2d', shape=(shape[-1]), initializer=b_init, dtype=LayersConfig.tf_dtype, **b_init_args) + b = tf.get_variable( + name='b_conv2d', + shape=(shape[-1]), + initializer=b_init, + dtype=LayersConfig.tf_dtype, + **b_init_args) self.outputs = act( - tf.nn.conv2d(self.inputs, W, strides=strides, padding=padding, use_cudnn_on_gpu=use_cudnn_on_gpu, data_format=data_format) + b) + tf.nn.conv2d( + self.inputs, + W, + strides=strides, + padding=padding, + use_cudnn_on_gpu=use_cudnn_on_gpu, + data_format=data_format) + b) else: self.outputs = act( - tf.nn.conv2d(self.inputs, W, strides=strides, padding=padding, use_cudnn_on_gpu=use_cudnn_on_gpu, data_format=data_format)) + tf.nn.conv2d( + self.inputs, + W, + strides=strides, + padding=padding, + use_cudnn_on_gpu=use_cudnn_on_gpu, + data_format=data_format)) self.all_layers.append(self.outputs) if b_init: @@ -1575,7 +1661,9 @@ class DeConv2d(Layer): """ - @deprecated_alias(layer='prev_layer', n_out_channel='n_filter', end_support_version=1.9) # TODO remove this line for the 1.9 release + @deprecated_alias( + layer='prev_layer', n_out_channel='n_filter', + end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( self, prev_layer, @@ -1592,7 +1680,8 @@ def __init__( b_init_args=None, # remove name='decnn2d'): super(DeConv2d, self).__init__(prev_layer=prev_layer, name=name) - logging.info("DeConv2d %s: n_filters:%s strides:%s pad:%s act:%s" % (name, str(n_filter), str(strides), padding, act.__name__)) + logging.info("DeConv2d %s: n_filters:%s strides:%s pad:%s act:%s" % (name, str(n_filter), str(strides), padding, + act.__name__)) if W_init_args is None: W_init_args = {} @@ -1683,7 +1772,8 @@ def __init__(self, name='decnn3d'): super(DeConv3d, self).__init__(prev_layer=prev_layer, name=name) - logging.info("DeConv3d %s: n_filters:%s strides:%s pad:%s act:%s" % (name, str(n_filter), str(strides), padding, act.__name__)) + logging.info("DeConv3d %s: n_filters:%s strides:%s pad:%s act:%s" % (name, str(n_filter), str(strides), padding, + act.__name__)) self.inputs = prev_layer.outputs @@ -1780,7 +1870,8 @@ def __init__( name='depthwise_conv2d', ): super(DepthwiseConv2d, self).__init__(prev_layer=prev_layer, name=name) - logging.info("DepthwiseConv2d %s: shape:%s strides:%s pad:%s act:%s" % (name, str(shape), str(strides), padding, act.__name__)) + logging.info("DepthwiseConv2d %s: shape:%s strides:%s pad:%s act:%s" % (name, str(shape), str(strides), padding, + act.__name__)) self.inputs = prev_layer.outputs @@ -1810,10 +1901,16 @@ def __init__( **W_init_args) # [filter_height, filter_width, in_channels, depth_multiplier] if b_init: b = tf.get_variable( - name='b_depthwise2d', shape=(pre_channel * depth_multiplier), initializer=b_init, dtype=LayersConfig.tf_dtype, **b_init_args) - self.outputs = act(tf.nn.depthwise_conv2d(self.inputs, W, strides=strides, padding=padding, rate=dilation_rate) + b) + name='b_depthwise2d', + shape=(pre_channel * depth_multiplier), + initializer=b_init, + dtype=LayersConfig.tf_dtype, + **b_init_args) + self.outputs = act( + tf.nn.depthwise_conv2d(self.inputs, W, strides=strides, padding=padding, rate=dilation_rate) + b) else: - self.outputs = act(tf.nn.depthwise_conv2d(self.inputs, W, strides=strides, padding=padding, rate=dilation_rate)) + self.outputs = act( + tf.nn.depthwise_conv2d(self.inputs, W, strides=strides, padding=padding, rate=dilation_rate)) # self.all_layers = list(layer.all_layers) # self.all_params = list(layer.all_params) @@ -1895,9 +1992,8 @@ def __init__( # b_init_args = {} super(SeparableConv2d, self).__init__(prev_layer=prev_layer, name=name) - logging.info("SeparableConv2d %s: n_filter:%d filter_size:%s filter_size:%s depth_multiplier:%d act:%s" % (self.name, n_filter, str(filter_size), - str(strides), depth_multiplier, - act.__name__)) + logging.info("SeparableConv2d %s: n_filter:%d filter_size:%s filter_size:%s depth_multiplier:%d act:%s" % + (self.name, n_filter, str(filter_size), str(strides), depth_multiplier, act.__name__)) self.inputs = prev_layer.outputs @@ -1980,8 +2076,8 @@ def __init__( ): # Windaway super(GroupConv2d, self).__init__(prev_layer=prev_layer, name=name) - logging.info("GroupConv2d %s: n_filter:%d size:%s strides:%s n_group:%d pad:%s act:%s" % (name, n_filter, str(filter_size), str(strides), n_group, - padding, act.__name__)) + logging.info("GroupConv2d %s: n_filter:%d size:%s strides:%s n_group:%d pad:%s act:%s" % + (name, n_filter, str(filter_size), str(strides), n_group, padding, act.__name__)) self.inputs = prev_layer.outputs @@ -2002,7 +2098,13 @@ def __init__( trainable=True, **W_init_args) if b_init: - bi = tf.get_variable(name='b', shape=n_filter, initializer=b_init, dtype=LayersConfig.tf_dtype, trainable=True, **b_init_args) + bi = tf.get_variable( + name='b', + shape=n_filter, + initializer=b_init, + dtype=LayersConfig.tf_dtype, + trainable=True, + **b_init_args) if n_group == 1: conv = groupConv(self.inputs, We) else: diff --git a/tensorlayer/layers/core.py b/tensorlayer/layers/core.py index 73b7f1919..05697866b 100644 --- a/tensorlayer/layers/core.py +++ b/tensorlayer/layers/core.py @@ -428,11 +428,14 @@ def print_params(self, details=True, session=None): try: # logging.info(" param {:3}: {:15} (mean: {:<18}, median: {:<18}, std: {:<18}) {}".format(i, str(p.eval().shape), p.eval().mean(), np.median(p.eval()), p.eval().std(), p.name)) val = p.eval(session=session) - logging.info(" param {:3}: {:20} {:15} {} (mean: {:<18}, median: {:<18}, std: {:<18}) ".format( - i, p.name, str(val.shape), p.dtype.name, val.mean(), np.median(val), val.std())) + logging.info( + " param {:3}: {:20} {:15} {} (mean: {:<18}, median: {:<18}, std: {:<18}) ".format( + i, p.name, str(val.shape), p.dtype.name, val.mean(), np.median(val), val.std())) except Exception as e: logging.info(str(e)) - raise Exception("Hint: print params details after tl.layers.initialize_global_variables(sess) or use network.print_params(False).") + raise Exception( + "Hint: print params details after tl.layers.initialize_global_variables(sess) or use network.print_params(False)." + ) else: logging.info(" param {:3}: {:20} {:15} {}".format(i, p.name, str(p.get_shape()), p.dtype.name)) logging.info(" num of params: %d" % self.count_params()) @@ -441,7 +444,8 @@ def print_layers(self): """Print all info of layers in the network""" for i, layer in enumerate(self.all_layers): # logging.info(" layer %d: %s" % (i, str(layer))) - logging.info(" layer {:3}: {:20} {:15} {}".format(i, layer.name, str(layer.get_shape()), layer.dtype.name)) + logging.info(" layer {:3}: {:20} {:15} {}".format(i, layer.name, str(layer.get_shape()), + layer.dtype.name)) def count_params(self): """Return the number of parameters in the network""" @@ -680,12 +684,25 @@ def __init__( # row vector with 'embedding_size' values. with tf.variable_scope(name): embeddings = tf.get_variable( - name='embeddings', shape=(vocabulary_size, embedding_size), initializer=E_init, dtype=LayersConfig.tf_dtype, **E_init_args) + name='embeddings', + shape=(vocabulary_size, embedding_size), + initializer=E_init, + dtype=LayersConfig.tf_dtype, + **E_init_args) embed = tf.nn.embedding_lookup(embeddings, self.inputs) # Construct the variables for the NCE loss (i.e. negative sampling) nce_weights = tf.get_variable( - name='nce_weights', shape=(vocabulary_size, embedding_size), initializer=nce_W_init, dtype=LayersConfig.tf_dtype, **nce_W_init_args) - nce_biases = tf.get_variable(name='nce_biases', shape=(vocabulary_size), initializer=nce_b_init, dtype=LayersConfig.tf_dtype, **nce_b_init_args) + name='nce_weights', + shape=(vocabulary_size, embedding_size), + initializer=nce_W_init, + dtype=LayersConfig.tf_dtype, + **nce_W_init_args) + nce_biases = tf.get_variable( + name='nce_biases', + shape=(vocabulary_size), + initializer=nce_b_init, + dtype=LayersConfig.tf_dtype, + **nce_b_init_args) # Compute the average NCE loss for the batch. # tf.nce_loss automatically draws a new sample of the negative labels @@ -765,7 +782,11 @@ def __init__( with tf.variable_scope(name): embeddings = tf.get_variable( - name='embeddings', shape=(vocabulary_size, embedding_size), initializer=E_init, dtype=LayersConfig.tf_dtype, **E_init_args) + name='embeddings', + shape=(vocabulary_size, embedding_size), + initializer=E_init, + dtype=LayersConfig.tf_dtype, + **E_init_args) embed = tf.nn.embedding_lookup(embeddings, self.inputs) self.outputs = embed @@ -951,10 +972,12 @@ def __init__( n_in = int(self.inputs.get_shape()[-1]) with tf.variable_scope(name): - W = tf.get_variable(name='W', shape=(n_in, n_units), initializer=W_init, dtype=LayersConfig.tf_dtype, **W_init_args) + W = tf.get_variable( + name='W', shape=(n_in, n_units), initializer=W_init, dtype=LayersConfig.tf_dtype, **W_init_args) if b_init is not None: try: - b = tf.get_variable(name='b', shape=(n_units), initializer=b_init, dtype=LayersConfig.tf_dtype, **b_init_args) + b = tf.get_variable( + name='b', shape=(n_units), initializer=b_init, dtype=LayersConfig.tf_dtype, **b_init_args) except Exception: # If initializer is a constant, do not specify shape. b = tf.get_variable(name='b', initializer=b_init, dtype=LayersConfig.tf_dtype, **b_init_args) self.outputs = act(tf.matmul(self.inputs, W) + b) @@ -1064,7 +1087,8 @@ def __init__( # L1 of activation outputs activation_out = self.all_layers[-2] - L1_a = 0.001 * tf.reduce_mean(activation_out) # : theano: T.mean( self.a[i] ) # some neuron are broken, white and black + L1_a = 0.001 * tf.reduce_mean( + activation_out) # : theano: T.mean( self.a[i] ) # some neuron are broken, white and black # L1_a = 0.001 * tf.reduce_mean( tf.reduce_sum(activation_out, 0) ) # : some neuron are broken, white and black # L1_a = 0.001 * 100 * tf.reduce_mean( tf.reduce_sum(activation_out, 1) ) # : some neuron are broken, white and black # KL Divergence @@ -1072,9 +1096,11 @@ def __init__( rho = 0.15 p_hat = tf.reduce_mean(activation_out, 0) # theano: p_hat = T.mean( self.a[i], axis=0 ) try: # TF1.0 - KLD = beta * tf.reduce_sum(rho * tf.log(tf.divide(rho, p_hat)) + (1 - rho) * tf.log((1 - rho) / (tf.subtract(float(1), p_hat)))) + KLD = beta * tf.reduce_sum(rho * tf.log(tf.divide(rho, p_hat)) + + (1 - rho) * tf.log((1 - rho) / (tf.subtract(float(1), p_hat)))) except Exception: # TF0.12 - KLD = beta * tf.reduce_sum(rho * tf.log(tf.div(rho, p_hat)) + (1 - rho) * tf.log((1 - rho) / (tf.sub(float(1), p_hat)))) + KLD = beta * tf.reduce_sum(rho * tf.log(tf.div(rho, p_hat)) + + (1 - rho) * tf.log((1 - rho) / (tf.sub(float(1), p_hat)))) # KLD = beta * tf.reduce_sum( rho * tf.log(rho/ p_hat) + (1- rho) * tf.log((1- rho)/(1- p_hat)) ) # theano: L1_a = l1_a[i] * T.sum( rho[i] * T.log(rho[i]/ p_hat) + (1- rho[i]) * T.log((1- rho[i])/(1- p_hat)) ) # Total cost @@ -1107,7 +1133,17 @@ def __init__( self.cost, var_list=self.train_params) # self.train_op = tf.train.GradientDescentOptimizer(1.0).minimize(self.cost, var_list=self.train_params) - def pretrain(self, sess, x, X_train, X_val, denoise_name=None, n_epoch=100, batch_size=128, print_freq=10, save=True, save_name='w1pre_'): + def pretrain(self, + sess, + x, + X_train, + X_val, + denoise_name=None, + n_epoch=100, + batch_size=128, + print_freq=10, + save=True, + save_name='w1pre_'): # ==================================================== # # You need to modify the cost function in __init__() so as to @@ -1155,11 +1191,17 @@ def pretrain(self, sess, x, X_train, X_val, denoise_name=None, n_epoch=100, batc if save: try: visualize.draw_weights( - self.train_params[0].eval(), second=10, saveable=True, shape=[28, 28], name=save_name + str(epoch + 1), fig_idx=2012) + self.train_params[0].eval(), + second=10, + saveable=True, + shape=[28, 28], + name=save_name + str(epoch + 1), + fig_idx=2012) files.save_npz([self.all_params[0]], name=save_name + str(epoch + 1) + '.npz') except Exception: raise Exception( - "You should change the visualize.W() in ReconLayer.pretrain(), if you want to save the feature images for different dataset") + "You should change the visualize.W() in ReconLayer.pretrain(), if you want to save the feature images for different dataset" + ) class DropoutLayer(Layer): @@ -1409,8 +1451,10 @@ def __init__( self.n_units = n_units with tf.variable_scope(name): - W = tf.get_variable(name='W', shape=(n_in, n_units), initializer=W_init, dtype=LayersConfig.tf_dtype, **W_init_args) - b = tf.get_variable(name='b', shape=(n_units), initializer=b_init, dtype=LayersConfig.tf_dtype, **b_init_args) + W = tf.get_variable( + name='W', shape=(n_in, n_units), initializer=W_init, dtype=LayersConfig.tf_dtype, **W_init_args) + b = tf.get_variable( + name='b', shape=(n_units), initializer=b_init, dtype=LayersConfig.tf_dtype, **b_init_args) # self.outputs = act(tf.matmul(self.inputs, W) + b) LayersConfig.set_keep[name] = tf.placeholder(tf.float32) diff --git a/tensorlayer/layers/merge.py b/tensorlayer/layers/merge.py index 66fe05f98..e5399603b 100644 --- a/tensorlayer/layers/merge.py +++ b/tensorlayer/layers/merge.py @@ -124,7 +124,8 @@ def __init__( ): super(ElementwiseLayer, self).__init__(prev_layer=layers, name=name) - logging.info("ElementwiseLayer %s: size:%s fn:%s" % (self.name, layers[0].outputs.get_shape(), combine_fn.__name__)) + logging.info("ElementwiseLayer %s: size:%s fn:%s" % (self.name, layers[0].outputs.get_shape(), + combine_fn.__name__)) self.outputs = layers[0].outputs diff --git a/tensorlayer/layers/normalization.py b/tensorlayer/layers/normalization.py index febc70270..b8e0a6a0b 100644 --- a/tensorlayer/layers/normalization.py +++ b/tensorlayer/layers/normalization.py @@ -49,7 +49,11 @@ def __init__( name='lrn_layer', ): super(LocalResponseNormLayer, self).__init__(prev_layer=prev_layer, name=name) - logging.info("LocalResponseNormLayer %s: depth_radius: %s, bias: %s, alpha: %s, beta: %s" % (name, str(depth_radius), str(bias), str(alpha), str(beta))) + logging.info("LocalResponseNormLayer %s: depth_radius: %s, bias: %s, alpha: %s, beta: %s" % (name, + str(depth_radius), + str(bias), + str(alpha), + str(beta))) self.inputs = prev_layer.outputs @@ -110,7 +114,8 @@ def __init__( name='batchnorm_layer', ): super(BatchNormLayer, self).__init__(prev_layer=prev_layer, name=name) - logging.info("BatchNormLayer %s: decay:%f epsilon:%f act:%s is_train:%s" % (name, decay, epsilon, act.__name__, is_train)) + logging.info("BatchNormLayer %s: decay:%f epsilon:%f act:%s is_train:%s" % (name, decay, epsilon, act.__name__, + is_train)) self.inputs = prev_layer.outputs @@ -126,7 +131,8 @@ def __init__( if beta_init: if tf.__version__ > '0.12.1' and beta_init == tf.zeros_initializer: beta_init = beta_init() - beta = tf.get_variable('beta', shape=params_shape, initializer=beta_init, dtype=LayersConfig.tf_dtype, trainable=is_train) + beta = tf.get_variable( + 'beta', shape=params_shape, initializer=beta_init, dtype=LayersConfig.tf_dtype, trainable=is_train) variables.append(beta) else: beta = None @@ -148,7 +154,8 @@ def __init__( moving_mean_init = tf.zeros_initializer() else: moving_mean_init = tf.zeros_initializer - moving_mean = tf.get_variable('moving_mean', params_shape, initializer=moving_mean_init, dtype=LayersConfig.tf_dtype, trainable=False) + moving_mean = tf.get_variable( + 'moving_mean', params_shape, initializer=moving_mean_init, dtype=LayersConfig.tf_dtype, trainable=False) moving_variance = tf.get_variable( 'moving_variance', params_shape, @@ -161,7 +168,8 @@ def __init__( # These ops will only be preformed when training. mean, variance = tf.nn.moments(self.inputs, axis) try: # TF12 - update_moving_mean = moving_averages.assign_moving_average(moving_mean, mean, decay, zero_debias=False) # if zero_debias=True, has bias + update_moving_mean = moving_averages.assign_moving_average( + moving_mean, mean, decay, zero_debias=False) # if zero_debias=True, has bias update_moving_variance = moving_averages.assign_moving_average( moving_variance, variance, decay, zero_debias=False) # if zero_debias=True, has bias # logging.info("TF12 moving") @@ -178,7 +186,8 @@ def mean_var_with_update(): mean, var = mean_var_with_update() self.outputs = act(tf.nn.batch_normalization(self.inputs, mean, var, beta, gamma, epsilon)) else: - self.outputs = act(tf.nn.batch_normalization(self.inputs, moving_mean, moving_variance, beta, gamma, epsilon)) + self.outputs = act( + tf.nn.batch_normalization(self.inputs, moving_mean, moving_variance, beta, gamma, epsilon)) variables.extend([moving_mean, moving_variance]) @@ -226,8 +235,13 @@ def __init__( with tf.variable_scope(name) as vs: mean, var = tf.nn.moments(self.inputs, [1, 2], keep_dims=True) scale = tf.get_variable( - 'scale', [self.inputs.get_shape()[-1]], initializer=tf.truncated_normal_initializer(mean=1.0, stddev=0.02), dtype=LayersConfig.tf_dtype) - offset = tf.get_variable('offset', [self.inputs.get_shape()[-1]], initializer=tf.constant_initializer(0.0), dtype=LayersConfig.tf_dtype) + 'scale', [self.inputs.get_shape()[-1]], + initializer=tf.truncated_normal_initializer(mean=1.0, stddev=0.02), + dtype=LayersConfig.tf_dtype) + offset = tf.get_variable( + 'offset', [self.inputs.get_shape()[-1]], + initializer=tf.constant_initializer(0.0), + dtype=LayersConfig.tf_dtype) self.outputs = scale * tf.div(self.inputs - mean, tf.sqrt(var + epsilon)) + offset self.outputs = act(self.outputs) variables = tf.get_collection(TF_GRAPHKEYS_VARIABLES, scope=vs.name) diff --git a/tensorlayer/layers/padding.py b/tensorlayer/layers/padding.py index 716f9da10..12cfcecdc 100644 --- a/tensorlayer/layers/padding.py +++ b/tensorlayer/layers/padding.py @@ -51,7 +51,8 @@ def __init__( self.inputs = prev_layer.outputs if padding is None: - raise Exception("padding should be a Tensor of type int32. see https://www.tensorflow.org/api_docs/python/tf/pad") + raise Exception( + "padding should be a Tensor of type int32. see https://www.tensorflow.org/api_docs/python/tf/pad") self.outputs = tf.pad(self.inputs, paddings=padding, mode=mode, name=name) self.all_layers.append(self.outputs) diff --git a/tensorlayer/layers/pooling.py b/tensorlayer/layers/pooling.py index 5bd5989dd..5b70570ea 100644 --- a/tensorlayer/layers/pooling.py +++ b/tensorlayer/layers/pooling.py @@ -67,7 +67,8 @@ def __init__( name='pool_layer', ): super(PoolLayer, self).__init__(prev_layer=prev_layer, name=name) - logging.info("PoolLayer %s: ksize:%s strides:%s padding:%s pool:%s" % (name, str(ksize), str(strides), padding, pool.__name__)) + logging.info("PoolLayer %s: ksize:%s strides:%s padding:%s pool:%s" % (name, str(ksize), str(strides), + padding, pool.__name__)) self.inputs = prev_layer.outputs @@ -103,8 +104,10 @@ def maxpool1d(prev_layer, filter_size=3, strides=2, padding='valid', data_format A max pooling 1-D layer with a output rank as 3. """ - logging.info("MaxPool1d %s: filter_size:%s strides:%s padding:%s" % (name, str(filter_size), str(strides), str(padding))) - outputs = tf.layers.max_pooling1d(prev_layer.outputs, filter_size, strides, padding=padding, data_format=data_format, name=name) + logging.info("MaxPool1d %s: filter_size:%s strides:%s padding:%s" % (name, str(filter_size), str(strides), + str(padding))) + outputs = tf.layers.max_pooling1d( + prev_layer.outputs, filter_size, strides, padding=padding, data_format=data_format, name=name) net_new = copy.copy(prev_layer) net_new.outputs = outputs @@ -140,8 +143,10 @@ def meanpool1d(prev_layer, filter_size=3, strides=2, padding='valid', data_forma A mean pooling 1-D layer with a output rank as 3. """ - logging.info("MeanPool1d %s: filter_size:%s strides:%s padding:%s" % (name, str(filter_size), str(strides), str(padding))) - outputs = tf.layers.average_pooling1d(prev_layer.outputs, filter_size, strides, padding=padding, data_format=data_format, name=name) + logging.info("MeanPool1d %s: filter_size:%s strides:%s padding:%s" % (name, str(filter_size), str(strides), + str(padding))) + outputs = tf.layers.average_pooling1d( + prev_layer.outputs, filter_size, strides, padding=padding, data_format=data_format, name=name) net_new = copy.copy(prev_layer) net_new.outputs = outputs @@ -175,7 +180,8 @@ def maxpool2d(prev_layer, filter_size=(3, 3), strides=(2, 2), padding='SAME', na if strides is None: strides = filter_size if tf.__version__ > '1.5': - outputs = tf.layers.max_pooling2d(prev_layer.outputs, filter_size, strides, padding=padding, data_format='channels_last', name=name) + outputs = tf.layers.max_pooling2d( + prev_layer.outputs, filter_size, strides, padding=padding, data_format='channels_last', name=name) net_new = copy.copy(prev_layer) net_new.outputs = outputs net_new.all_layers.extend([outputs]) @@ -185,7 +191,12 @@ def maxpool2d(prev_layer, filter_size=(3, 3), strides=(2, 2), padding='SAME', na assert len(strides) == 2, "len(strides) should be 2, MaxPool2d and PoolLayer are different." prev_layer = PoolLayer( - prev_layer, ksize=[1, filter_size[0], filter_size[1], 1], strides=[1, strides[0], strides[1], 1], padding=padding, pool=tf.nn.max_pool, name=name) + prev_layer, + ksize=[1, filter_size[0], filter_size[1], 1], + strides=[1, strides[0], strides[1], 1], + padding=padding, + pool=tf.nn.max_pool, + name=name) return prev_layer @@ -216,7 +227,8 @@ def meanpool2d(prev_layer, filter_size=(3, 3), strides=(2, 2), padding='SAME', n if strides is None: strides = filter_size if tf.__version__ > '1.5': - outputs = tf.layers.average_pooling2d(prev_layer.outputs, filter_size, strides, padding=padding, data_format='channels_last', name=name) + outputs = tf.layers.average_pooling2d( + prev_layer.outputs, filter_size, strides, padding=padding, data_format='channels_last', name=name) net_new = copy.copy(prev_layer) net_new.outputs = outputs net_new.all_layers.extend([outputs]) @@ -226,7 +238,12 @@ def meanpool2d(prev_layer, filter_size=(3, 3), strides=(2, 2), padding='SAME', n assert len(strides) == 2, "len(strides) should be 2, MeanPool2d and PoolLayer are different." prev_layer = PoolLayer( - prev_layer, ksize=[1, filter_size[0], filter_size[1], 1], strides=[1, strides[0], strides[1], 1], padding=padding, pool=tf.nn.avg_pool, name=name) + prev_layer, + ksize=[1, filter_size[0], filter_size[1], 1], + strides=[1, strides[0], strides[1], 1], + padding=padding, + pool=tf.nn.avg_pool, + name=name) return prev_layer @@ -260,14 +277,22 @@ class MaxPool3d(Layer): """ @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release - def __init__(self, prev_layer, filter_size=(3, 3, 3), strides=(2, 2, 2), padding='valid', data_format='channels_last', name='maxpool3d'): + def __init__(self, + prev_layer, + filter_size=(3, 3, 3), + strides=(2, 2, 2), + padding='valid', + data_format='channels_last', + name='maxpool3d'): super(MaxPool3d, self).__init__(prev_layer=prev_layer, name=name) - logging.info("MaxPool3d %s: filter_size:%s strides:%s padding:%s" % (name, str(filter_size), str(strides), str(padding))) + logging.info("MaxPool3d %s: filter_size:%s strides:%s padding:%s" % (name, str(filter_size), str(strides), + str(padding))) self.inputs = prev_layer.outputs - self.outputs = tf.layers.max_pooling3d(prev_layer.outputs, filter_size, strides, padding=padding, data_format=data_format, name=name) + self.outputs = tf.layers.max_pooling3d( + prev_layer.outputs, filter_size, strides, padding=padding, data_format=data_format, name=name) # update layer (customized) self.all_layers.append(self.outputs) @@ -302,15 +327,23 @@ class MeanPool3d(Layer): """ @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release - def __init__(self, prev_layer, filter_size=(3, 3, 3), strides=(2, 2, 2), padding='valid', data_format='channels_last', name='meanpool3d'): + def __init__(self, + prev_layer, + filter_size=(3, 3, 3), + strides=(2, 2, 2), + padding='valid', + data_format='channels_last', + name='meanpool3d'): super(MeanPool3d, self).__init__(prev_layer=prev_layer, name=name) - logging.info("MeanPool3d %s: filter_size:%s strides:%s padding:%s" % (name, str(filter_size), str(strides), str(padding))) + logging.info("MeanPool3d %s: filter_size:%s strides:%s padding:%s" % (name, str(filter_size), str(strides), + str(padding))) self.inputs = prev_layer.outputs # operation (customized) - self.outputs = tf.layers.average_pooling3d(prev_layer.outputs, filter_size, strides, padding=padding, data_format=data_format, name=name) + self.outputs = tf.layers.average_pooling3d( + prev_layer.outputs, filter_size, strides, padding=padding, data_format=data_format, name=name) # update layer (customized) self.all_layers.append(self.outputs) diff --git a/tensorlayer/layers/recurrent.py b/tensorlayer/layers/recurrent.py index 09affd4c7..f283fd358 100644 --- a/tensorlayer/layers/recurrent.py +++ b/tensorlayer/layers/recurrent.py @@ -160,8 +160,9 @@ def __init__( except Exception: logging.warning('pop state_is_tuple fails.') - logging.info("RNNLayer %s: n_hidden:%d n_steps:%d in_dim:%d in_shape:%s cell_fn:%s " % (self.name, n_hidden, n_steps, self.inputs.get_shape().ndims, - self.inputs.get_shape(), cell_fn.__name__)) + logging.info("RNNLayer %s: n_hidden:%d n_steps:%d in_dim:%d in_shape:%s cell_fn:%s " % + (self.name, n_hidden, n_steps, self.inputs.get_shape().ndims, self.inputs.get_shape(), + cell_fn.__name__)) # You can get the dimension by .get_shape() or ._shape, and check the # dimension by .with_rank() as follow. # self.inputs.get_shape().with_rank(2) @@ -351,10 +352,9 @@ def __init__( except Exception: logging.warning("pop state_is_tuple fails.") - logging.info("BiRNNLayer %s: n_hidden:%d n_steps:%d in_dim:%d in_shape:%s cell_fn:%s dropout:%s n_layer:%d " % (self.name, n_hidden, n_steps, - self.inputs.get_shape().ndims, - self.inputs.get_shape(), - cell_fn.__name__, dropout, n_layer)) + logging.info("BiRNNLayer %s: n_hidden:%d n_steps:%d in_dim:%d in_shape:%s cell_fn:%s dropout:%s n_layer:%d " % + (self.name, n_hidden, n_steps, self.inputs.get_shape().ndims, self.inputs.get_shape(), + cell_fn.__name__, dropout, n_layer)) fixed_batch_size = self.inputs.get_shape().with_rank_at_least(1)[0] @@ -404,8 +404,10 @@ def __init__( MultiRNNCell_fn = tf.nn.rnn_cell.MultiRNNCell if dropout: try: - self.fw_cell = MultiRNNCell_fn([cell_creator(is_last=i == n_layer - 1) for i in range(n_layer)], state_is_tuple=True) - self.bw_cell = MultiRNNCell_fn([cell_creator(is_last=i == n_layer - 1) for i in range(n_layer)], state_is_tuple=True) + self.fw_cell = MultiRNNCell_fn( + [cell_creator(is_last=i == n_layer - 1) for i in range(n_layer)], state_is_tuple=True) + self.bw_cell = MultiRNNCell_fn( + [cell_creator(is_last=i == n_layer - 1) for i in range(n_layer)], state_is_tuple=True) except Exception: self.fw_cell = MultiRNNCell_fn([cell_creator(is_last=i == n_layer - 1) for i in range(n_layer)]) self.bw_cell = MultiRNNCell_fn([cell_creator(is_last=i == n_layer - 1) for i in range(n_layer)]) @@ -419,11 +421,13 @@ def __init__( # Initial state of RNN if fw_initial_state is None: - self.fw_initial_state = self.fw_cell.zero_state(self.batch_size, dtype=LayersConfig.tf_dtype) # dtype=tf.float32) + self.fw_initial_state = self.fw_cell.zero_state( + self.batch_size, dtype=LayersConfig.tf_dtype) # dtype=tf.float32) else: self.fw_initial_state = fw_initial_state if bw_initial_state is None: - self.bw_initial_state = self.bw_cell.zero_state(self.batch_size, dtype=LayersConfig.tf_dtype) # dtype=tf.float32) + self.bw_initial_state = self.bw_cell.zero_state( + self.batch_size, dtype=LayersConfig.tf_dtype) # dtype=tf.float32) else: self.bw_initial_state = bw_initial_state # exit() @@ -534,7 +538,14 @@ class BasicConvLSTMCell(ConvRNNCell): """ - def __init__(self, shape, filter_size, num_features, forget_bias=1.0, input_size=None, state_is_tuple=False, act=tf.nn.tanh): + def __init__(self, + shape, + filter_size, + num_features, + forget_bias=1.0, + input_size=None, + state_is_tuple=False, + act=tf.nn.tanh): """Initialize the basic Conv LSTM cell.""" # if not state_is_tuple: # logging.warn("%s: Using a concatenated state is slower and will soon be " @@ -624,14 +635,16 @@ def _conv_linear(args, filter_size, num_features, bias, bias_start=0.0, scope=No # Now the computation. with tf.variable_scope(scope or "Conv"): - matrix = tf.get_variable("Matrix", [filter_size[0], filter_size[1], total_arg_size_depth, num_features], dtype=dtype) + matrix = tf.get_variable( + "Matrix", [filter_size[0], filter_size[1], total_arg_size_depth, num_features], dtype=dtype) if len(args) == 1: res = tf.nn.conv2d(args[0], matrix, strides=[1, 1, 1, 1], padding='SAME') else: res = tf.nn.conv2d(tf.concat(args, 3), matrix, strides=[1, 1, 1, 1], padding='SAME') if not bias: return res - bias_term = tf.get_variable("Bias", [num_features], dtype=dtype, initializer=tf.constant_initializer(bias_start, dtype=dtype)) + bias_term = tf.get_variable( + "Bias", [num_features], dtype=dtype, initializer=tf.constant_initializer(bias_start, dtype=dtype)) return res + bias_term @@ -711,7 +724,8 @@ def __init__( self.inputs = prev_layer.outputs logging.info("ConvLSTMLayer %s: feature_map:%d, n_steps:%d, " - "in_dim:%d %s, cell_fn:%s " % (self.name, feature_map, n_steps, self.inputs.get_shape().ndims, self.inputs.get_shape(), cell_fn.__name__)) + "in_dim:%d %s, cell_fn:%s " % (self.name, feature_map, n_steps, self.inputs.get_shape().ndims, + self.inputs.get_shape(), cell_fn.__name__)) # You can get the dimension by .get_shape() or ._shape, and check the # dimension by .with_rank() as follow. # self.inputs.get_shape().with_rank(2) @@ -721,7 +735,8 @@ def __init__( try: self.inputs.get_shape().with_rank(5) except Exception: - raise Exception("RNN : Input dimension should be rank 5 : [batch_size, n_steps, input_x, " "input_y, feature_map]") + raise Exception("RNN : Input dimension should be rank 5 : [batch_size, n_steps, input_x, " + "input_y, feature_map]") fixed_batch_size = self.inputs.get_shape().with_rank_at_least(1)[0] @@ -762,7 +777,8 @@ def __init__( else: # : stack more RNN layer after that # 5D Tensor [n_example/n_steps, n_steps, h, w, c] - self.outputs = tf.reshape(tf.concat(outputs, 1), [-1, n_steps, cell_shape[0], cell_shape[1], feature_map]) + self.outputs = tf.reshape( + tf.concat(outputs, 1), [-1, n_steps, cell_shape[0], cell_shape[1], feature_map]) self.final_state = state @@ -905,7 +921,8 @@ def retrieve_seq_length_op3(data, pad_val=0): # HangSheng: return tensor for se elif data_shape_size == 1: raise ValueError("retrieve_seq_length_op3: data has wrong shape!") else: - raise ValueError("retrieve_seq_length_op3: handling data_shape_size %s hasn't been implemented!" % (data_shape_size)) + raise ValueError("retrieve_seq_length_op3: handling data_shape_size %s hasn't been implemented!" % + (data_shape_size)) def target_mask_op(data, pad_val=0): # HangSheng: return tensor for mask,if input is tf.string @@ -1059,7 +1076,8 @@ def __init__( logging.warning("pop state_is_tuple fails.") logging.info("DynamicRNNLayer %s: n_hidden:%d, in_dim:%d in_shape:%s cell_fn:%s dropout:%s n_layer:%d" % - (self.name, n_hidden, self.inputs.get_shape().ndims, self.inputs.get_shape(), cell_fn.__name__, dropout, n_layer)) + (self.name, n_hidden, self.inputs.get_shape().ndims, self.inputs.get_shape(), cell_fn.__name__, + dropout, n_layer)) # Input dimension should be rank 3 [batch_size, n_steps(max), n_features] try: @@ -1119,7 +1137,8 @@ def __init__( if dropout: try: # cell_instance_fn=lambda: MultiRNNCell_fn([cell_instance_fn2() for _ in range(n_layer)], state_is_tuple=True) # HanSheng - self.cell = MultiRNNCell_fn([cell_creator(is_last=i == n_layer - 1) for i in range(n_layer)], state_is_tuple=True) + self.cell = MultiRNNCell_fn( + [cell_creator(is_last=i == n_layer - 1) for i in range(n_layer)], state_is_tuple=True) except Exception: # when GRU # cell_instance_fn=lambda: MultiRNNCell_fn([cell_instance_fn2() for _ in range(n_layer)]) # HanSheng self.cell = MultiRNNCell_fn([cell_creator(is_last=i == n_layer - 1) for i in range(n_layer)]) @@ -1140,9 +1159,11 @@ def __init__( # Computes sequence_length if sequence_length is None: try: # TF1.0 - sequence_length = retrieve_seq_length_op(self.inputs if isinstance(self.inputs, tf.Tensor) else tf.stack(self.inputs)) + sequence_length = retrieve_seq_length_op( + self.inputs if isinstance(self.inputs, tf.Tensor) else tf.stack(self.inputs)) except Exception: # TF0.12 - sequence_length = retrieve_seq_length_op(self.inputs if isinstance(self.inputs, tf.Tensor) else tf.pack(self.inputs)) + sequence_length = retrieve_seq_length_op( + self.inputs if isinstance(self.inputs, tf.Tensor) else tf.pack(self.inputs)) # Main - Computes outputs and last_states with tf.variable_scope(name, initializer=initializer) as vs: @@ -1313,7 +1334,8 @@ def __init__( logging.warning("pop state_is_tuple fails.") logging.info("BiDynamicRNNLayer %s: n_hidden:%d in_dim:%d in_shape:%s cell_fn:%s dropout:%s n_layer:%d" % - (self.name, n_hidden, self.inputs.get_shape().ndims, self.inputs.get_shape(), cell_fn.__name__, dropout, n_layer)) + (self.name, n_hidden, self.inputs.get_shape().ndims, self.inputs.get_shape(), cell_fn.__name__, + dropout, n_layer)) # Input dimension should be rank 3 [batch_size, n_steps(max), n_features] try: @@ -1376,9 +1398,11 @@ def __init__( # Computes sequence_length if sequence_length is None: try: # TF1.0 - sequence_length = retrieve_seq_length_op(self.inputs if isinstance(self.inputs, tf.Tensor) else tf.stack(self.inputs)) + sequence_length = retrieve_seq_length_op( + self.inputs if isinstance(self.inputs, tf.Tensor) else tf.stack(self.inputs)) except Exception: # TF0.12 - sequence_length = retrieve_seq_length_op(self.inputs if isinstance(self.inputs, tf.Tensor) else tf.pack(self.inputs)) + sequence_length = retrieve_seq_length_op( + self.inputs if isinstance(self.inputs, tf.Tensor) else tf.pack(self.inputs)) if n_layer > 1: if dropout: @@ -1597,7 +1621,8 @@ def __init__( except Exception: logging.warning("pop state_is_tuple fails.") # self.inputs = layer.outputs - logging.info("[*] Seq2Seq %s: n_hidden:%d cell_fn:%s dropout:%s n_layer:%d" % (self.name, n_hidden, cell_fn.__name__, dropout, n_layer)) + logging.info("[*] Seq2Seq %s: n_hidden:%d cell_fn:%s dropout:%s n_layer:%d" % + (self.name, n_hidden, cell_fn.__name__, dropout, n_layer)) with tf.variable_scope(name): # tl.layers.set_name_reuse(reuse) diff --git a/tensorlayer/layers/spatial_transformer.py b/tensorlayer/layers/spatial_transformer.py index 3ab4fb3f4..38fa8ae11 100644 --- a/tensorlayer/layers/spatial_transformer.py +++ b/tensorlayer/layers/spatial_transformer.py @@ -133,7 +133,9 @@ def _meshgrid(height, width): # np.linspace(-1, 1, height)) # ones = np.ones(np.prod(x_t.shape)) # grid = np.vstack([x_t.flatten(), y_t.flatten(), ones]) - x_t = tf.matmul(tf.ones(shape=tf.stack([height, 1])), tf.transpose(tf.expand_dims(tf.linspace(-1.0, 1.0, width), 1), [1, 0])) + x_t = tf.matmul( + tf.ones(shape=tf.stack([height, 1])), + tf.transpose(tf.expand_dims(tf.linspace(-1.0, 1.0, width), 1), [1, 0])) y_t = tf.matmul(tf.expand_dims(tf.linspace(-1.0, 1.0, height), 1), tf.ones(shape=tf.stack([1, width]))) x_t_flat = tf.reshape(x_t, (1, -1)) @@ -243,7 +245,8 @@ def __init__( if out_size is None: out_size = [40, 40] - logging.info("SpatialTransformer2dAffineLayer %s: in_size:%s out_size:%s" % (name, self.inputs.get_shape().as_list(), out_size)) + logging.info("SpatialTransformer2dAffineLayer %s: in_size:%s out_size:%s" % + (name, self.inputs.get_shape().as_list(), out_size)) with tf.variable_scope(name) as vs: # 1. make the localisation network to [batch, 6] via Flatten and Dense. diff --git a/tensorlayer/layers/special_activation.py b/tensorlayer/layers/special_activation.py index 4d5a472dd..7b04a7f08 100644 --- a/tensorlayer/layers/special_activation.py +++ b/tensorlayer/layers/special_activation.py @@ -60,7 +60,8 @@ def __init__( # with tf.name_scope(name) as scope: with tf.variable_scope(name): - alphas = tf.get_variable(name='alphas', shape=w_shape, initializer=a_init, dtype=LayersConfig.tf_dtype, **a_init_args) + alphas = tf.get_variable( + name='alphas', shape=w_shape, initializer=a_init, dtype=LayersConfig.tf_dtype, **a_init_args) try: # TF 1.0 self.outputs = tf.nn.relu(self.inputs) + tf.multiply(alphas, (self.inputs - tf.abs(self.inputs))) * 0.5 except Exception: # TF 0.12 diff --git a/tensorlayer/models/mobilenetv1.py b/tensorlayer/models/mobilenetv1.py index 2f496f94e..4330dff85 100644 --- a/tensorlayer/models/mobilenetv1.py +++ b/tensorlayer/models/mobilenetv1.py @@ -7,7 +7,8 @@ # import numpy as np import tensorflow as tf from .. import _logging as logging -from ..layers import (Layer, BatchNormLayer, Conv2d, DepthwiseConv2d, FlattenLayer, GlobalMeanPool2d, InputLayer, ReshapeLayer) +from ..layers import (Layer, BatchNormLayer, Conv2d, DepthwiseConv2d, FlattenLayer, GlobalMeanPool2d, InputLayer, + ReshapeLayer) from ..files import maybe_download_and_extract, assign_params, load_npz __all__ = [ @@ -151,7 +152,10 @@ def depthwise_conv_block(cls, n, n_filter, strides=(1, 1), is_train=False, name= def restore_params(self, sess, path='models'): logging.info("Restore pre-trained parameters") maybe_download_and_extract( - 'mobilenet.npz', path, 'https://github.com/tensorlayer/pretrained-models/raw/master/models/', expected_bytes=25600116) # ls -al + 'mobilenet.npz', + path, + 'https://github.com/tensorlayer/pretrained-models/raw/master/models/', + expected_bytes=25600116) # ls -al params = load_npz(name=os.path.join(path, 'mobilenet.npz')) assign_params(sess, params[:len(self.net.all_params)], self.net) del params diff --git a/tensorlayer/models/squeezenetv1.py b/tensorlayer/models/squeezenetv1.py index faf8f295e..40168e173 100644 --- a/tensorlayer/models/squeezenetv1.py +++ b/tensorlayer/models/squeezenetv1.py @@ -161,7 +161,10 @@ def squeezenetv1(cls, x, end_with='output', is_train=False, reuse=None): def restore_params(self, sess, path='models'): logging.info("Restore pre-trained parameters") maybe_download_and_extract( - 'squeezenet.npz', path, 'https://github.com/tensorlayer/pretrained-models/raw/master/models/', expected_bytes=7405613) # ls -al + 'squeezenet.npz', + path, + 'https://github.com/tensorlayer/pretrained-models/raw/master/models/', + expected_bytes=7405613) # ls -al params = load_npz(name=os.path.join(path, 'squeezenet.npz')) assign_params(sess, params[:len(self.net.all_params)], self.net) del params diff --git a/tensorlayer/models/vgg16.py b/tensorlayer/models/vgg16.py index 52dd090c1..40ba19129 100644 --- a/tensorlayer/models/vgg16.py +++ b/tensorlayer/models/vgg16.py @@ -63,7 +63,8 @@ def conv_layers(net_in): strides=[1, 1, 1, 1], padding='SAME', name='conv1_2') - net = PoolLayer(net, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', pool=tf.nn.max_pool, name='pool1') + net = PoolLayer( + net, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', pool=tf.nn.max_pool, name='pool1') # conv2 net = Conv2dLayer( @@ -80,7 +81,8 @@ def conv_layers(net_in): strides=[1, 1, 1, 1], padding='SAME', name='conv2_2') - net = PoolLayer(net, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', pool=tf.nn.max_pool, name='pool2') + net = PoolLayer( + net, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', pool=tf.nn.max_pool, name='pool2') # conv3 net = Conv2dLayer( @@ -104,7 +106,8 @@ def conv_layers(net_in): strides=[1, 1, 1, 1], padding='SAME', name='conv3_3') - net = PoolLayer(net, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', pool=tf.nn.max_pool, name='pool3') + net = PoolLayer( + net, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', pool=tf.nn.max_pool, name='pool3') # conv4 net = Conv2dLayer( @@ -128,7 +131,8 @@ def conv_layers(net_in): strides=[1, 1, 1, 1], padding='SAME', name='conv4_3') - net = PoolLayer(net, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', pool=tf.nn.max_pool, name='pool4') + net = PoolLayer( + net, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', pool=tf.nn.max_pool, name='pool4') # conv5 net = Conv2dLayer( @@ -152,7 +156,8 @@ def conv_layers(net_in): strides=[1, 1, 1, 1], padding='SAME', name='conv5_3') - net = PoolLayer(net, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', pool=tf.nn.max_pool, name='pool5') + net = PoolLayer( + net, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', pool=tf.nn.max_pool, name='pool5') return net @staticmethod @@ -216,7 +221,8 @@ def vgg16_simple_api(net_in, end_with): def restore_params(self, sess): logging.info("Restore pre-trained parameters") - maybe_download_and_extract('vgg16_weights.npz', 'models', 'http://www.cs.toronto.edu/~frossard/vgg16/', expected_bytes=553436134) + maybe_download_and_extract( + 'vgg16_weights.npz', 'models', 'http://www.cs.toronto.edu/~frossard/vgg16/', expected_bytes=553436134) npz = np.load(os.path.join('models', 'vgg16_weights.npz')) params = [] diff --git a/tensorlayer/nlp.py b/tensorlayer/nlp.py index 7d3df60ca..fcf1a1c34 100755 --- a/tensorlayer/nlp.py +++ b/tensorlayer/nlp.py @@ -693,7 +693,8 @@ def build_words_dataset(words=None, vocabulary_size=50000, printable=True, unk_k logging.info('Limited vocabulary size {}'.format(vocabulary_size)) if len(collections.Counter(words).keys()) < vocabulary_size: raise Exception( - "len(collections.Counter(words).keys()) >= vocabulary_size , the limited vocabulary_size must be less than or equal to the read vocabulary_size") + "len(collections.Counter(words).keys()) >= vocabulary_size , the limited vocabulary_size must be less than or equal to the read vocabulary_size" + ) return data, count, dictionary, reverse_dictionary @@ -863,7 +864,13 @@ def basic_tokenizer(sentence, _WORD_SPLIT=re.compile(b"([.,!?\"':;)(])")): return [w for w in words if w] -def create_vocabulary(vocabulary_path, data_path, max_vocabulary_size, tokenizer=None, normalize_digits=True, _DIGIT_RE=re.compile(br"\d"), _START_VOCAB=None): +def create_vocabulary(vocabulary_path, + data_path, + max_vocabulary_size, + tokenizer=None, + normalize_digits=True, + _DIGIT_RE=re.compile(br"\d"), + _START_VOCAB=None): """Create vocabulary file (if it does not exist yet) from data file. Data file is assumed to contain one sentence per line. Each sentence is @@ -968,7 +975,12 @@ def initialize_vocabulary(vocabulary_path): raise ValueError("Vocabulary file %s not found.", vocabulary_path) -def sentence_to_token_ids(sentence, vocabulary, tokenizer=None, normalize_digits=True, UNK_ID=3, _DIGIT_RE=re.compile(br"\d")): +def sentence_to_token_ids(sentence, + vocabulary, + tokenizer=None, + normalize_digits=True, + UNK_ID=3, + _DIGIT_RE=re.compile(br"\d")): """Convert a string to list of integers representing token-ids. For example, a sentence "I have a dog" may become tokenized into @@ -1002,7 +1014,13 @@ def sentence_to_token_ids(sentence, vocabulary, tokenizer=None, normalize_digits return [vocabulary.get(re.sub(_DIGIT_RE, b"0", w), UNK_ID) for w in words] -def data_to_token_ids(data_path, target_path, vocabulary_path, tokenizer=None, normalize_digits=True, UNK_ID=3, _DIGIT_RE=re.compile(br"\d")): +def data_to_token_ids(data_path, + target_path, + vocabulary_path, + tokenizer=None, + normalize_digits=True, + UNK_ID=3, + _DIGIT_RE=re.compile(br"\d")): """Tokenize data file and turn into token-ids using given vocabulary file. This function loads data line-by-line from data_path, calls the above @@ -1037,7 +1055,8 @@ def data_to_token_ids(data_path, target_path, vocabulary_path, tokenizer=None, n counter += 1 if counter % 100000 == 0: logging.info(" tokenizing line %d" % counter) - token_ids = sentence_to_token_ids(line, vocab, tokenizer, normalize_digits, UNK_ID=UNK_ID, _DIGIT_RE=_DIGIT_RE) + token_ids = sentence_to_token_ids( + line, vocab, tokenizer, normalize_digits, UNK_ID=UNK_ID, _DIGIT_RE=_DIGIT_RE) tokens_file.write(" ".join([str(tok) for tok in token_ids]) + "\n") else: logging.info("Target path %s exists" % target_path) @@ -1077,7 +1096,8 @@ def moses_multi_bleu(hypotheses, references, lowercase=False): # Get MOSES multi-bleu script try: - multi_bleu_path, _ = urllib.request.urlretrieve("https://raw.githubusercontent.com/moses-smt/mosesdecoder/" "master/scripts/generic/multi-bleu.perl") + multi_bleu_path, _ = urllib.request.urlretrieve("https://raw.githubusercontent.com/moses-smt/mosesdecoder/" + "master/scripts/generic/multi-bleu.perl") os.chmod(multi_bleu_path, 0o755) except Exception: # pylint: disable=W0702 tf.logging.info("Unable to fetch multi-bleu.perl script, using local.") diff --git a/tensorlayer/prepro.py b/tensorlayer/prepro.py index 0105bf15e..2274fc48a 100644 --- a/tensorlayer/prepro.py +++ b/tensorlayer/prepro.py @@ -180,7 +180,8 @@ def apply_fn(results, i, data, kwargs): results = [None] * thread_count threads = [] for i in range(thread_count): - t = threading.Thread(name='threading_and_return', target=apply_fn, args=(results, i, data[divs[i]:divs[i + 1]], kwargs)) + t = threading.Thread( + name='threading_and_return', target=apply_fn, args=(results, i, data[divs[i]:divs[i + 1]], kwargs)) t.start() threads.append(t) @@ -196,7 +197,15 @@ def apply_fn(results, i, data, kwargs): return np.concatenate(results) -def rotation(x, rg=20, is_random=False, row_index=0, col_index=1, channel_index=2, fill_mode='nearest', cval=0., order=1): +def rotation(x, + rg=20, + is_random=False, + row_index=0, + col_index=1, + channel_index=2, + fill_mode='nearest', + cval=0., + order=1): """Rotate an image randomly or non-randomly. Parameters @@ -240,7 +249,15 @@ def rotation(x, rg=20, is_random=False, row_index=0, col_index=1, channel_index= return x -def rotation_multi(x, rg=20, is_random=False, row_index=0, col_index=1, channel_index=2, fill_mode='nearest', cval=0., order=1): +def rotation_multi(x, + rg=20, + is_random=False, + row_index=0, + col_index=1, + channel_index=2, + fill_mode='nearest', + cval=0., + order=1): """Rotate multiple images with the same arguments, randomly or non-randomly. Usually be used for image segmentation which x=[X, Y], X and Y should be matched. @@ -442,7 +459,16 @@ def flip_axis_multi(x, axis, is_random=False): # shift -def shift(x, wrg=0.1, hrg=0.1, is_random=False, row_index=0, col_index=1, channel_index=2, fill_mode='nearest', cval=0., order=1): +def shift(x, + wrg=0.1, + hrg=0.1, + is_random=False, + row_index=0, + col_index=1, + channel_index=2, + fill_mode='nearest', + cval=0., + order=1): """Shift an image randomly or non-randomly. Parameters @@ -483,7 +509,16 @@ def shift(x, wrg=0.1, hrg=0.1, is_random=False, row_index=0, col_index=1, channe return x -def shift_multi(x, wrg=0.1, hrg=0.1, is_random=False, row_index=0, col_index=1, channel_index=2, fill_mode='nearest', cval=0., order=1): +def shift_multi(x, + wrg=0.1, + hrg=0.1, + is_random=False, + row_index=0, + col_index=1, + channel_index=2, + fill_mode='nearest', + cval=0., + order=1): """Shift images with the same arguments, randomly or non-randomly. Usually be used for image segmentation which x=[X, Y], X and Y should be matched. @@ -516,7 +551,15 @@ def shift_multi(x, wrg=0.1, hrg=0.1, is_random=False, row_index=0, col_index=1, # shear -def shear(x, intensity=0.1, is_random=False, row_index=0, col_index=1, channel_index=2, fill_mode='nearest', cval=0., order=1): +def shear(x, + intensity=0.1, + is_random=False, + row_index=0, + col_index=1, + channel_index=2, + fill_mode='nearest', + cval=0., + order=1): """Shear an image randomly or non-randomly. Parameters @@ -559,7 +602,15 @@ def shear(x, intensity=0.1, is_random=False, row_index=0, col_index=1, channel_i return x -def shear_multi(x, intensity=0.1, is_random=False, row_index=0, col_index=1, channel_index=2, fill_mode='nearest', cval=0., order=1): +def shear_multi(x, + intensity=0.1, + is_random=False, + row_index=0, + col_index=1, + channel_index=2, + fill_mode='nearest', + cval=0., + order=1): """Shear images with the same arguments, randomly or non-randomly. Usually be used for image segmentation which x=[X, Y], X and Y should be matched. @@ -590,7 +641,15 @@ def shear_multi(x, intensity=0.1, is_random=False, row_index=0, col_index=1, cha return np.asarray(results) -def shear2(x, shear=(0.1, 0.1), is_random=False, row_index=0, col_index=1, channel_index=2, fill_mode='nearest', cval=0., order=1): +def shear2(x, + shear=(0.1, 0.1), + is_random=False, + row_index=0, + col_index=1, + channel_index=2, + fill_mode='nearest', + cval=0., + order=1): """Shear an image randomly or non-randomly. Parameters @@ -620,7 +679,9 @@ def shear2(x, shear=(0.1, 0.1), is_random=False, row_index=0, col_index=1, chann - `Affine transformation `__ """ - assert len(shear) == 2, "shear should be tuple of 2 floats, or you want to use tl.prepro.shear rather than tl.prepro.shear2 ?" + assert len( + shear + ) == 2, "shear should be tuple of 2 floats, or you want to use tl.prepro.shear rather than tl.prepro.shear2 ?" if is_random: shear[0] = np.random.uniform(-shear[0], shear[0]) shear[1] = np.random.uniform(-shear[1], shear[1]) @@ -633,7 +694,15 @@ def shear2(x, shear=(0.1, 0.1), is_random=False, row_index=0, col_index=1, chann return x -def shear_multi2(x, shear=(0.1, 0.1), is_random=False, row_index=0, col_index=1, channel_index=2, fill_mode='nearest', cval=0., order=1): +def shear_multi2(x, + shear=(0.1, 0.1), + is_random=False, + row_index=0, + col_index=1, + channel_index=2, + fill_mode='nearest', + cval=0., + order=1): """Shear images with the same arguments, randomly or non-randomly. Usually be used for image segmentation which x=[X, Y], X and Y should be matched. @@ -650,7 +719,9 @@ def shear_multi2(x, shear=(0.1, 0.1), is_random=False, row_index=0, col_index=1, A list of processed images. """ - assert len(shear) == 2, "shear should be tuple of 2 floats, or you want to use tl.prepro.shear_multi rather than tl.prepro.shear_multi2 ?" + assert len( + shear + ) == 2, "shear should be tuple of 2 floats, or you want to use tl.prepro.shear_multi rather than tl.prepro.shear_multi2 ?" if is_random: shear[0] = np.random.uniform(-shear[0], shear[0]) shear[1] = np.random.uniform(-shear[1], shear[1]) @@ -925,7 +996,15 @@ def elastic_transform_multi(x, alpha, sigma, mode="constant", cval=0, is_random= # zoom -def zoom(x, zoom_range=(0.9, 1.1), is_random=False, row_index=0, col_index=1, channel_index=2, fill_mode='nearest', cval=0., order=1): +def zoom(x, + zoom_range=(0.9, 1.1), + is_random=False, + row_index=0, + col_index=1, + channel_index=2, + fill_mode='nearest', + cval=0., + order=1): """Zoom in and out of a single image, randomly or non-randomly. Parameters @@ -972,7 +1051,15 @@ def zoom(x, zoom_range=(0.9, 1.1), is_random=False, row_index=0, col_index=1, ch return x -def zoom_multi(x, zoom_range=(0.9, 1.1), is_random=False, row_index=0, col_index=1, channel_index=2, fill_mode='nearest', cval=0., order=1): +def zoom_multi(x, + zoom_range=(0.9, 1.1), + is_random=False, + row_index=0, + col_index=1, + channel_index=2, + fill_mode='nearest', + cval=0., + order=1): """Zoom in and out of images with the same arguments, randomly or non-randomly. Usually be used for image segmentation which x=[X, Y], X and Y should be matched. @@ -1120,7 +1207,8 @@ def illumination(x, gamma=1., contrast=1., saturation=1., is_random=False): if is_random: try: - assert len(gamma) == len(contrast) == len(saturation) == 2, "if is_random = True, the arguments are (min, max)" + assert len(gamma) == len(contrast) == len( + saturation) == 2, "if is_random = True, the arguments are (min, max)" except: raise Exception("if is_random = True, the arguments are (min, max)") ## random change brightness # small --> brighter @@ -1383,7 +1471,12 @@ def pixel_value_scale(im, val=0.9, clip=(-np.inf, np.inf), is_random=False): # normailization -def samplewise_norm(x, rescale=None, samplewise_center=False, samplewise_std_normalization=False, channel_index=2, epsilon=1e-7): +def samplewise_norm(x, + rescale=None, + samplewise_center=False, + samplewise_std_normalization=False, + channel_index=2, + epsilon=1e-7): """Normalize an image by rescale, samplewise centering and samplewise centering in order. Parameters @@ -1717,14 +1810,24 @@ def apply_transform(x, transform_matrix, channel_index=2, fill_mode='nearest', c final_affine_matrix = transform_matrix[:2, :2] final_offset = transform_matrix[:2, 2] channel_images = [ - ndi.interpolation.affine_transform(x_channel, final_affine_matrix, final_offset, order=order, mode=fill_mode, cval=cval) for x_channel in x + ndi.interpolation.affine_transform( + x_channel, final_affine_matrix, final_offset, order=order, mode=fill_mode, cval=cval) for x_channel in x ] x = np.stack(channel_images, axis=0) x = np.rollaxis(x, 0, channel_index + 1) return x -def projective_transform_by_points(x, src, dst, map_args=None, output_shape=None, order=1, mode='constant', cval=0.0, clip=True, preserve_range=False): +def projective_transform_by_points(x, + src, + dst, + map_args=None, + output_shape=None, + order=1, + mode='constant', + cval=0.0, + clip=True, + preserve_range=False): """Projective transform by given coordinates, usually 4 coordinates. see `scikit-image `__. @@ -1791,7 +1894,16 @@ def projective_transform_by_points(x, src, dst, map_args=None, output_shape=None m = transform.ProjectiveTransform() m.estimate(dst, src) - warped = transform.warp(x, m, map_args=map_args, output_shape=output_shape, order=order, mode=mode, cval=cval, clip=clip, preserve_range=preserve_range) + warped = transform.warp( + x, + m, + map_args=map_args, + output_shape=output_shape, + order=order, + mode=mode, + cval=cval, + clip=clip, + preserve_range=preserve_range) return warped @@ -1863,7 +1975,8 @@ def find_contours(x, level=0.8, fully_connected='low', positive_orientation='low Each contour is an ndarray of shape (n, 2), consisting of n (row, column) coordinates along the contour. """ - return skimage.measure.find_contours(x, level, fully_connected=fully_connected, positive_orientation=positive_orientation) + return skimage.measure.find_contours( + x, level, fully_connected=fully_connected, positive_orientation=positive_orientation) def pt2map(list_points=None, size=(100, 100), val=1): @@ -2455,7 +2568,16 @@ def obj_box_imresize(im, coords=None, size=None, interp='bicubic', mode=None, is # exit() -def obj_box_crop(im, classes=None, coords=None, wrg=100, hrg=100, is_rescale=False, is_center=False, is_random=False, thresh_wh=0.02, thresh_wh2=12.): +def obj_box_crop(im, + classes=None, + coords=None, + wrg=100, + hrg=100, + is_rescale=False, + is_center=False, + is_random=False, + thresh_wh=0.02, + thresh_wh2=12.): """Randomly or centrally crop an image, and compute the new bounding box coordinates. Objects outside the cropped image will be removed. @@ -2565,7 +2687,8 @@ def _get_coord(coord): # logging.info('xx', w, h) return None - if (w / (im_new.shape[1] * 1.) < thresh_wh) or (h / (im_new.shape[0] * 1.) < thresh_wh): # object shape strange: too narrow + if (w / (im_new.shape[1] * 1.) < thresh_wh) or (h / (im_new.shape[0] * 1.) < + thresh_wh): # object shape strange: too narrow # logging.info('yy', w, im_new.shape[1], h, im_new.shape[0]) return None @@ -2705,7 +2828,8 @@ def _get_coord(coord): # logging.info('xx', w, h) return None - if (w / (im_new.shape[1] * 1.) < thresh_wh) or (h / (im_new.shape[0] * 1.) < thresh_wh): # object shape strange: too narrow + if (w / (im_new.shape[1] * 1.) < thresh_wh) or (h / (im_new.shape[0] * 1.) < + thresh_wh): # object shape strange: too narrow # logging.info('yy', w, im_new.shape[1], h, im_new.shape[0]) return None @@ -2848,7 +2972,8 @@ def _get_coord(coord): # logging.info('xx', w, h) return None - if (w / (im_new.shape[1] * 1.) < thresh_wh) or (h / (im_new.shape[0] * 1.) < thresh_wh): # object shape strange: too narrow + if (w / (im_new.shape[1] * 1.) < thresh_wh) or (h / (im_new.shape[0] * 1.) < + thresh_wh): # object shape strange: too narrow # logging.info('yy', w, im_new.shape[1], h, im_new.shape[0]) return None @@ -2948,7 +3073,8 @@ def pad_sequences(sequences, maxlen=None, dtype='int32', padding='post', truncat # check `trunc` has expected shape trunc = np.asarray(trunc, dtype=dtype) if trunc.shape[1:] != sample_shape: - raise ValueError('Shape of sample %s of sequence at position %s is different from expected shape %s' % (trunc.shape[1:], idx, sample_shape)) + raise ValueError('Shape of sample %s of sequence at position %s is different from expected shape %s' % + (trunc.shape[1:], idx, sample_shape)) if padding == 'post': x[idx, :len(trunc)] = trunc diff --git a/tensorlayer/utils.py b/tensorlayer/utils.py index d26ff75d3..8abb9990c 100644 --- a/tensorlayer/utils.py +++ b/tensorlayer/utils.py @@ -218,7 +218,8 @@ def fit(sess, if acc is not None: logging.info(" val acc: %f" % (val_acc / n_batch)) else: - logging.info("Epoch %d of %d took %fs, loss %f" % (epoch + 1, n_epoch, time.time() - start_time, loss_ep)) + logging.info("Epoch %d of %d took %fs, loss %f" % (epoch + 1, n_epoch, time.time() - start_time, + loss_ep)) logging.info("Total training time: %fs" % (time.time() - start_time_begin)) @@ -568,7 +569,9 @@ def exit_tensorflow(sess=None, port=6006): _exit() elif _platform == "darwin": logging.info('OS X: %s' % text) - subprocess.Popen("lsof -i tcp:" + str(port) + " | grep -v PID | awk '{print $2}' | xargs kill", shell=True) # kill tensorboard + subprocess.Popen( + "lsof -i tcp:" + str(port) + " | grep -v PID | awk '{print $2}' | xargs kill", + shell=True) # kill tensorboard elif _platform == "win32": raise NotImplementedError("this function is not supported on the Windows platform") else: diff --git a/tensorlayer/visualize.py b/tensorlayer/visualize.py index d868dba10..f6d3f1309 100644 --- a/tensorlayer/visualize.py +++ b/tensorlayer/visualize.py @@ -134,11 +134,20 @@ def merge(images, size): def imsave(images, size, path): return scipy.misc.imsave(path, merge(images, size)) - assert len(images) <= size[0] * size[1], "number of images should be equal or less than size[0] * size[1] {}".format(len(images)) + assert len( + images) <= size[0] * size[1], "number of images should be equal or less than size[0] * size[1] {}".format( + len(images)) return imsave(images, size, image_path) -def draw_boxes_and_labels_to_image(image, classes, coords, scores, classes_list, is_center=True, is_rescale=True, save_name=None): +def draw_boxes_and_labels_to_image(image, + classes, + coords, + scores, + classes_list, + is_center=True, + is_rescale=True, + save_name=None): """Draw bboxes and class labels on image. Return or save the image with bboxes, example in the docs of ``tl.prepro``. Parameters @@ -434,7 +443,8 @@ def CNN2d(CNN=None, second=10, saveable=True, name='cnn', fig_idx=3119362): if n_color == 1: plt.imshow(np.reshape(CNN[:, :, :, count - 1], (n_row, n_col)), cmap='gray', interpolation="nearest") elif n_color == 3: - plt.imshow(np.reshape(CNN[:, :, :, count - 1], (n_row, n_col, n_color)), cmap='gray', interpolation="nearest") + plt.imshow( + np.reshape(CNN[:, :, :, count - 1], (n_row, n_col, n_color)), cmap='gray', interpolation="nearest") else: raise Exception("Unknown n_color") plt.gca().xaxis.set_major_locator(plt.NullLocator()) # distable tick @@ -623,7 +633,9 @@ def draw_weights(W=None, second=10, saveable=True, shape=None, name='mnist', fig # feature = np.zeros_like(feature) # if np.mean(feature) < -0.015: # condition threshold # feature = np.zeros_like(feature) - plt.imshow(np.reshape(feature, (shape[0], shape[1])), cmap='gray', interpolation="nearest") #, vmin=np.min(feature), vmax=np.max(feature)) + plt.imshow( + np.reshape(feature, (shape[0], shape[1])), cmap='gray', + interpolation="nearest") #, vmin=np.min(feature), vmax=np.max(feature)) # plt.title(name) # ------------------------------------------------------------ # plt.imshow(np.reshape(W[:,count-1] ,(np.sqrt(size),np.sqrt(size))), cmap='gray', interpolation="nearest") diff --git a/tests/test_layers_convolution.py b/tests/test_layers_convolution.py index d408ec101..9765975ea 100644 --- a/tests/test_layers_convolution.py +++ b/tests/test_layers_convolution.py @@ -44,24 +44,28 @@ def setUpClass(cls): cls.shape_n4 = n4.outputs.get_shape().as_list() cls.n4_params = n4.all_params - n5 = tl.layers.Conv2d(nin2, n_filter=32, filter_size=(3, 3), strides=(2, 2), act=tf.nn.relu, b_init=None, name='conv2d_no_bias') + n5 = tl.layers.Conv2d( + nin2, n_filter=32, filter_size=(3, 3), strides=(2, 2), act=tf.nn.relu, b_init=None, name='conv2d_no_bias') cls.shape_n5 = n5.outputs.get_shape().as_list() cls.n5_params = n5.all_params - n6 = tl.layers.DeConv2dLayer(nin2, shape=(5, 5, 32, 3), output_shape=(100, 200, 200, 32), strides=(1, 2, 2, 1), name='deconv2dlayer') + n6 = tl.layers.DeConv2dLayer( + nin2, shape=(5, 5, 32, 3), output_shape=(100, 200, 200, 32), strides=(1, 2, 2, 1), name='deconv2dlayer') cls.shape_n6 = n6.outputs.get_shape().as_list() n7 = tl.layers.DeConv2d(nin2, n_filter=32, filter_size=(3, 3), strides=(2, 2), name='DeConv2d') cls.shape_n7 = n7.outputs.get_shape().as_list() - n8 = tl.layers.DepthwiseConv2d(nin2, shape=(3, 3), strides=(2, 2), act=tf.nn.relu, depth_multiplier=2, name='depthwise') + n8 = tl.layers.DepthwiseConv2d( + nin2, shape=(3, 3), strides=(2, 2), act=tf.nn.relu, depth_multiplier=2, name='depthwise') cls.shape_n8 = n8.outputs.get_shape().as_list() n9 = tl.layers.Conv2d(nin2, n_filter=32, filter_size=(3, 3), strides=(2, 2), act=tf.nn.relu, name='conv2d2') n9 = tl.layers.GroupConv2d(n9, n_filter=32, filter_size=(3, 3), strides=(2, 2), name='group') cls.shape_n9 = n9.outputs.get_shape().as_list() - n10 = tl.layers.SeparableConv2d(nin2, n_filter=32, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, name='seperable1') + n10 = tl.layers.SeparableConv2d( + nin2, n_filter=32, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, name='seperable1') cls.shape_n10 = n10.outputs.get_shape().as_list() cls.n10_all_layers = n10.all_layers cls.n10_params = n10.all_params diff --git a/tests/test_layers_core.py b/tests/test_layers_core.py index c83d14b4f..f34025bee 100644 --- a/tests/test_layers_core.py +++ b/tests/test_layers_core.py @@ -44,7 +44,12 @@ def setUpClass(cls): train_inputs = tf.placeholder(tf.int32, shape=cls.batch_size) train_labels = tf.placeholder(tf.int32, shape=(cls.batch_size, 1)) net3 = tl.layers.Word2vecEmbeddingInputlayer( - inputs=train_inputs, train_labels=train_labels, vocabulary_size=1000, embedding_size=200, num_sampled=64, name='word2vec') + inputs=train_inputs, + train_labels=train_labels, + vocabulary_size=1000, + embedding_size=200, + num_sampled=64, + name='word2vec') net3.print_layers() net3.print_params(False) diff --git a/tests/test_layers_recurrent.py b/tests/test_layers_recurrent.py index feb19dfd5..468e89edb 100644 --- a/tests/test_layers_recurrent.py +++ b/tests/test_layers_recurrent.py @@ -30,14 +30,27 @@ def setUpClass(cls): input_data = tf.placeholder(tf.int32, [cls.net1_batch_size, cls.num_steps]) - net1 = tl.layers.EmbeddingInputlayer(inputs=input_data, vocabulary_size=cls.vocab_size, embedding_size=cls.hidden_size, name='embed') + net1 = tl.layers.EmbeddingInputlayer( + inputs=input_data, vocabulary_size=cls.vocab_size, embedding_size=cls.hidden_size, name='embed') net1 = tl.layers.DropoutLayer(net1, keep=cls.keep_prob, is_fix=True, is_train=cls.is_train, name='drop1') - net1 = tl.layers.RNNLayer(net1, cell_fn=tf.contrib.rnn.BasicLSTMCell, n_hidden=cls.hidden_size, n_steps=cls.num_steps, return_last=False, name='lstm1') + net1 = tl.layers.RNNLayer( + net1, + cell_fn=tf.contrib.rnn.BasicLSTMCell, + n_hidden=cls.hidden_size, + n_steps=cls.num_steps, + return_last=False, + name='lstm1') # lstm1 = net1 net1 = tl.layers.DropoutLayer(net1, keep=cls.keep_prob, is_fix=True, is_train=cls.is_train, name='drop2') - net1 = tl.layers.RNNLayer(net1, cell_fn=tf.contrib.rnn.BasicLSTMCell, n_hidden=cls.hidden_size, n_steps=cls.num_steps, return_last=True, name='lstm2') + net1 = tl.layers.RNNLayer( + net1, + cell_fn=tf.contrib.rnn.BasicLSTMCell, + n_hidden=cls.hidden_size, + n_steps=cls.num_steps, + return_last=True, + name='lstm2') # lstm2 = net1 @@ -66,7 +79,13 @@ def setUpClass(cls): net2 = tl.layers.ReshapeLayer(net2, shape=(-1, cls.num_steps, int(net2.outputs._shape[-1]))) rnn = tl.layers.RNNLayer( - net2, cell_fn=tf.contrib.rnn.BasicLSTMCell, n_hidden=200, n_steps=cls.num_steps, return_last=False, return_seq_2d=True, name='rnn') + net2, + cell_fn=tf.contrib.rnn.BasicLSTMCell, + n_hidden=200, + n_steps=cls.num_steps, + return_last=False, + return_seq_2d=True, + name='rnn') net2 = tl.layers.DenseLayer(rnn, n_units=3, name='out') @@ -82,9 +101,16 @@ def setUpClass(cls): x3 = tf.placeholder(tf.int32, [cls.net3_batch_size, cls.num_steps]) - net3 = tl.layers.EmbeddingInputlayer(inputs=x3, vocabulary_size=cls.vocab_size, embedding_size=cls.hidden_size, name='emb') + net3 = tl.layers.EmbeddingInputlayer( + inputs=x3, vocabulary_size=cls.vocab_size, embedding_size=cls.hidden_size, name='emb') net3 = tl.layers.BiRNNLayer( - net3, cell_fn=tf.contrib.rnn.BasicLSTMCell, n_hidden=cls.hidden_size, n_steps=cls.num_steps, return_last=False, return_seq_2d=False, name='birnn') + net3, + cell_fn=tf.contrib.rnn.BasicLSTMCell, + n_hidden=cls.hidden_size, + n_steps=cls.num_steps, + return_last=False, + return_seq_2d=False, + name='birnn') net3.print_layers() net3.print_params(False) @@ -95,7 +121,8 @@ def setUpClass(cls): cls.net3_n_params = net3.count_params() # n_layer=2 - net4 = tl.layers.EmbeddingInputlayer(inputs=x3, vocabulary_size=cls.vocab_size, embedding_size=cls.hidden_size, name='emb2') + net4 = tl.layers.EmbeddingInputlayer( + inputs=x3, vocabulary_size=cls.vocab_size, embedding_size=cls.hidden_size, name='emb2') net4 = tl.layers.BiRNNLayer( net4, cell_fn=tf.contrib.rnn.BasicLSTMCell, @@ -134,7 +161,8 @@ def setUpClass(cls): # =============================== Dynamic Synced input and output =============================== input_seqs = tf.placeholder(dtype=tf.int64, shape=[cls.net5_batch_size, None], name="input") - nin = tl.layers.EmbeddingInputlayer(inputs=input_seqs, vocabulary_size=cls.vocab_size, embedding_size=cls.embedding_size, name='seq_embedding') + nin = tl.layers.EmbeddingInputlayer( + inputs=input_seqs, vocabulary_size=cls.vocab_size, embedding_size=cls.embedding_size, name='seq_embedding') rnn = tl.layers.DynamicRNNLayer( nin, @@ -158,7 +186,8 @@ def setUpClass(cls): cls.net5_n_params = net5.count_params() # n_layer=3 - nin = tl.layers.EmbeddingInputlayer(inputs=input_seqs, vocabulary_size=cls.vocab_size, embedding_size=cls.embedding_size, name='seq_embedding2') + nin = tl.layers.EmbeddingInputlayer( + inputs=input_seqs, vocabulary_size=cls.vocab_size, embedding_size=cls.embedding_size, name='seq_embedding2') rnn = tl.layers.DynamicRNNLayer( nin, cell_fn=tf.contrib.rnn.BasicLSTMCell, @@ -281,10 +310,12 @@ def setUpClass(cls): # for translation, you may want to use 2 seperated embedding layers with tf.variable_scope("embedding") as vs: - net_encode = tl.layers.EmbeddingInputlayer(inputs=encode_seqs, vocabulary_size=10000, embedding_size=200, name='seq_embed') + net_encode = tl.layers.EmbeddingInputlayer( + inputs=encode_seqs, vocabulary_size=10000, embedding_size=200, name='seq_embed') vs.reuse_variables() # tl.layers.set_name_reuse(True) - net_decode = tl.layers.EmbeddingInputlayer(inputs=decode_seqs, vocabulary_size=10000, embedding_size=200, name='seq_embed') + net_decode = tl.layers.EmbeddingInputlayer( + inputs=decode_seqs, vocabulary_size=10000, embedding_size=200, name='seq_embed') net11 = tl.layers.Seq2Seq( net_encode, diff --git a/tests/test_layers_spatial_transformer.py b/tests/test_layers_spatial_transformer.py index 0976d0f49..d483040fb 100644 --- a/tests/test_layers_spatial_transformer.py +++ b/tests/test_layers_spatial_transformer.py @@ -26,8 +26,10 @@ def model(x, is_train, reuse): n = tl.layers.SpatialTransformer2dAffineLayer(nin, theta_layer=nt, out_size=[40, 40], name='spatial') s = n ## 3. Classifier - n = tl.layers.Conv2d(n, n_filter=16, filter_size=(3, 3), strides=(2, 2), act=tf.nn.relu, padding='SAME', name='conv1') - n = tl.layers.Conv2d(n, n_filter=16, filter_size=(3, 3), strides=(2, 2), act=tf.nn.relu, padding='SAME', name='conv2') + n = tl.layers.Conv2d( + n, n_filter=16, filter_size=(3, 3), strides=(2, 2), act=tf.nn.relu, padding='SAME', name='conv1') + n = tl.layers.Conv2d( + n, n_filter=16, filter_size=(3, 3), strides=(2, 2), act=tf.nn.relu, padding='SAME', name='conv2') n = tl.layers.FlattenLayer(n, name='flatten2') n = tl.layers.DenseLayer(n, n_units=1024, act=tf.nn.relu, name='out1') n = tl.layers.DenseLayer(n, n_units=10, act=tf.identity, name='out2') diff --git a/tests/test_layers_time_distributed.py b/tests/test_layers_time_distributed.py index bac25201a..b95994549 100644 --- a/tests/test_layers_time_distributed.py +++ b/tests/test_layers_time_distributed.py @@ -14,7 +14,11 @@ def model(x, is_train=True, reuse=False, name_scope="env1"): with tf.variable_scope(name_scope, reuse=reuse): net = tl.layers.InputLayer(x, name='input') - net = tl.layers.TimeDistributedLayer(net, layer_class=tl.layers.DenseLayer, args={'n_units': 50, 'name': 'dense'}, name='time_dense') + net = tl.layers.TimeDistributedLayer( + net, layer_class=tl.layers.DenseLayer, args={ + 'n_units': 50, + 'name': 'dense' + }, name='time_dense') return net diff --git a/tests/test_mnist_simple.py b/tests/test_mnist_simple.py index 5829d0017..44fcba446 100644 --- a/tests/test_mnist_simple.py +++ b/tests/test_mnist_simple.py @@ -86,7 +86,8 @@ def test_reuse_vgg(self): eval_train=False) # evaluation - tl.utils.test(sess, self.network, self.acc, X_test, y_test, self.x, self.y_, batch_size=None, cost=self.cost) + tl.utils.test( + sess, self.network, self.acc, X_test, y_test, self.x, self.y_, batch_size=None, cost=self.cost) # save the network to .npz file tl.files.save_npz(self.network.all_params, name='model.npz') diff --git a/tests/test_yapf_format.py b/tests/test_yapf_format.py index c2569ca51..b82261399 100644 --- a/tests/test_yapf_format.py +++ b/tests/test_yapf_format.py @@ -46,7 +46,8 @@ def test_unformated_files(self): for filename in self.files_badly_formated: print('yapf -i %s' % filename) - raise Exception("Bad Coding Style: %d files need to be formatted, run the following commands to fix" % len(self.files_badly_formated)) + raise Exception("Bad Coding Style: %d files need to be formatted, run the following commands to fix" % len( + self.files_badly_formated)) if __name__ == '__main__': From dc46dfc0ae6015248aa057a4a0f0aaa68401b28a Mon Sep 17 00:00:00 2001 From: DEKHTIARJonathan Date: Thu, 19 Apr 2018 16:02:21 +0200 Subject: [PATCH 07/22] test yapf refactored --- tests/test_yapf_format.py | 38 ++++++++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/tests/test_yapf_format.py b/tests/test_yapf_format.py index b82261399..8e68d4dc3 100644 --- a/tests/test_yapf_format.py +++ b/tests/test_yapf_format.py @@ -6,8 +6,10 @@ try: import tests.testing as testing + from tests.unittests_helper import CustomTestCase except ImportError: import testing + from unittests_helper import CustomTestCase from yapf.yapflib.yapf_api import FormatCode @@ -21,33 +23,41 @@ def _read_utf_8_file(filename): return f.read() -class YAPF_Style_Test(unittest.TestCase): +class YAPF_Style_Test(CustomTestCase): @classmethod def setUpClass(cls): - cls.files_badly_formated = list() + cls.badly_formatted_files = list() + cls.files_2_test = testing.list_all_py_files() - for filename in testing.list_all_py_files(): + def test_files_format(self): - print(filename) - code = _read_utf_8_file(filename) + for file in testing.list_all_py_files(): + + print(file) + code = _read_utf_8_file(file) # https://pypi.python.org/pypi/yapf/0.20.2#example-as-a-module - diff, changed = FormatCode(code, filename=filename, style_config='.style.yapf', print_diff=True) + diff, changed = FormatCode(code, filename=file, style_config='.style.yapf', print_diff=True) if changed: print(diff) - cls.files_badly_formated.append(filename) + self.badly_formatted_files.append(file) + + with self.assertNotRaises(Exception): + + str_err = "" - def test_unformated_files(self): - if self.files_badly_formated: - print() + if self.badly_formatted_files: + for filename in self.badly_formatted_files: + str_err += 'yapf -i %s\n' % filename - for filename in self.files_badly_formated: - print('yapf -i %s' % filename) + str_err = "\n======================================================================================\n" \ + "Bad Coding Style: %d file(s) need to be formatted, run the following commands to fix: \n%s" \ + "======================================================================================" % ( + len(self.badly_formatted_files), str_err) - raise Exception("Bad Coding Style: %d files need to be formatted, run the following commands to fix" % len( - self.files_badly_formated)) + raise Exception(str_err) if __name__ == '__main__': From 40b196eec015e2757c3f77d545e4fd6806dd367e Mon Sep 17 00:00:00 2001 From: DEKHTIARJonathan Date: Thu, 19 Apr 2018 22:26:52 +0200 Subject: [PATCH 08/22] Requirements conflict solved --- tests/requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/requirements.txt b/tests/requirements.txt index 30ea1b241..43144cf40 100644 --- a/tests/requirements.txt +++ b/tests/requirements.txt @@ -1,4 +1,5 @@ keras>=2.1,<2.2 +pycodestyle<2.4.0,>=2.0.0 pydocstyle>=2.1,<2.2 pytest>=3.4,<3.5 pytest-cache>=1.0,<1.1 From 8a52165fb842ec013cb3d8c09cfece91927749ed Mon Sep 17 00:00:00 2001 From: DEKHTIARJonathan Date: Fri, 20 Apr 2018 11:48:53 +0200 Subject: [PATCH 09/22] Yapf Style modified and merged in file "setup.cfg" --- .style.yapf | 4 ---- setup.cfg | 28 ++++++++++++++++++++++++++-- tests/test_yapf_format.py | 4 ++-- 3 files changed, 28 insertions(+), 8 deletions(-) delete mode 100644 .style.yapf diff --git a/.style.yapf b/.style.yapf deleted file mode 100644 index 704f8a396..000000000 --- a/.style.yapf +++ /dev/null @@ -1,4 +0,0 @@ -[style] -based_on_style = pep8 -indent_width = 4 -column_limit = 120 diff --git a/setup.cfg b/setup.cfg index a7fd940e6..dc41e142a 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,6 +1,9 @@ [flake8] max-line-length = 120 -ignore = D301 +ignore = + D301 + E221 # Space before equal sign + E251 # Space after equal sign exclude = .git, venv, @@ -9,4 +12,25 @@ exclude = tensorlayer.egg-info, build, dist, - img \ No newline at end of file + img + +[yapf] +based_on_style = pep8 +indent_width = 4 +column_limit = 120 +coalesce_brackets = True +dedent_closing_brackets = True +each_dict_entry_on_separate_line = True + +# For list comprehensions and generator expressions with multiple clauses +# (e.g multiple "for" calls, "if" filter expressions) and which need to be reflowed, split each clause onto its own line +split_complex_comprehension = True + +# Split before arguments if the argument list is terminated by a comma. +split_arguments_when_comma_terminated = True + +# Split named assignments onto individual lines. +split_before_named_assigns = True + +# If an argument / parameter list is going to be split, then split before the first argument. +split_before_first_argument = True \ No newline at end of file diff --git a/tests/test_yapf_format.py b/tests/test_yapf_format.py index 8e68d4dc3..5844d9f94 100644 --- a/tests/test_yapf_format.py +++ b/tests/test_yapf_format.py @@ -38,7 +38,7 @@ def test_files_format(self): code = _read_utf_8_file(file) # https://pypi.python.org/pypi/yapf/0.20.2#example-as-a-module - diff, changed = FormatCode(code, filename=file, style_config='.style.yapf', print_diff=True) + diff, changed = FormatCode(code, filename=file, style_config='setup.cfg', print_diff=True) if changed: print(diff) @@ -50,7 +50,7 @@ def test_files_format(self): if self.badly_formatted_files: for filename in self.badly_formatted_files: - str_err += 'yapf -i %s\n' % filename + str_err += 'yapf -i --style=setup.cfg %s\n' % filename str_err = "\n======================================================================================\n" \ "Bad Coding Style: %d file(s) need to be formatted, run the following commands to fix: \n%s" \ From 5b9a699499c3556f6a3c391f3fb0df3a6b8222ed Mon Sep 17 00:00:00 2001 From: DEKHTIARJonathan Date: Fri, 20 Apr 2018 12:30:07 +0200 Subject: [PATCH 10/22] Yapf Confiuguration Updated --- setup.cfg | 45 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 2 deletions(-) diff --git a/setup.cfg b/setup.cfg index dc41e142a..9b2617c64 100644 --- a/setup.cfg +++ b/setup.cfg @@ -16,14 +16,26 @@ exclude = [yapf] based_on_style = pep8 + +# The number of columns to use for indentation. indent_width = 4 + +# The column limit (or max line-length) column_limit = 120 + +# Do not split consecutive brackets. Only relevant when DEDENT_CLOSING_BRACKETS is set coalesce_brackets = True + +# Put closing brackets on a separate line, dedented, +# if the bracketed expression can't fit in a single line. dedent_closing_brackets = True + +# Place each dictionary entry onto its own line. each_dict_entry_on_separate_line = True # For list comprehensions and generator expressions with multiple clauses -# (e.g multiple "for" calls, "if" filter expressions) and which need to be reflowed, split each clause onto its own line +# (e.g multiple "for" calls, "if" filter expressions) +# and which need to be reflowed, split each clause onto its own line split_complex_comprehension = True # Split before arguments if the argument list is terminated by a comma. @@ -33,4 +45,33 @@ split_arguments_when_comma_terminated = True split_before_named_assigns = True # If an argument / parameter list is going to be split, then split before the first argument. -split_before_first_argument = True \ No newline at end of file +split_before_first_argument = True + +# Split after the opening paren which surrounds an expression if it doesn't fit on a single line. +split_before_expression_after_opening_paren = True + +# Split before the closing bracket if a list or dict literal doesn't fit on a single line. +# split_before_closing_bracket = True + +# Allow lambdas to be formatted on more than one line. +allow_multiline_lambdas = True + +# Allow splits before the dictionary value. +allow_split_before_dict_value = False + +# Join short lines into one line. E.g., single line if statements. +join_multiple_lines = True + +# Do not include spaces around selected binary operators. +# Example: 1 + 2 * 3 - 4 / 5 => 1 + 2*3 - 4/5 +no_spaces_around_selected_binary_operators = True + + +SPLIT_PENALTY_AFTER_OPENING_BRACKET = -50 +SPLIT_PENALTY_AFTER_UNARY_OPERATOR = -5000 +SPLIT_PENALTY_BEFORE_IF_EXPR = -5000 +SPLIT_PENALTY_FOR_ADDED_LINE_SPLIT = -8 +SPLIT_PENALTY_IMPORT_NAMES = -5000 +SPLIT_PENALTY_LOGICAL_OPERATOR = -5000 + + From c7365f5eebe6d36eb156b1a2e96b01dc26dbbd77 Mon Sep 17 00:00:00 2001 From: DEKHTIARJonathan Date: Fri, 20 Apr 2018 12:31:18 +0200 Subject: [PATCH 11/22] Code Refactored with new YAPF formating style --- example/tutorial_atari_pong.py | 7 +- .../tutorial_binarynet_cifar10_tfrecord.py | 109 +++- ...ial_bipedalwalker_a3c_continuous_action.py | 7 +- example/tutorial_cartpole_ac.py | 20 +- example/tutorial_cifar10.py | 69 ++- example/tutorial_cifar10_tfrecord.py | 83 ++- .../tutorial_dorefanet_cifar10_tfrecord.py | 115 +++- example/tutorial_frozenlake_dqn.py | 26 +- example/tutorial_frozenlake_q_table.py | 14 +- example/tutorial_generate_text.py | 62 +- ...torial_imagenet_inceptionV3_distributed.py | 28 +- example/tutorial_imdb_fasttext.py | 6 +- example/tutorial_inceptionV3_tfslim.py | 9 +- example/tutorial_keras.py | 10 +- example/tutorial_mlp_dropout2.py | 10 +- example/tutorial_mnist.py | 23 +- example/tutorial_mnist_distributed.py | 3 +- example/tutorial_mnist_float16.py | 6 +- example/tutorial_mnist_simple.py | 3 +- example/tutorial_mobilenet.py | 21 +- example/tutorial_ptb_lstm.py | 43 +- example/tutorial_ptb_lstm_state_is_tuple.py | 29 +- example/tutorial_squeezenet.py | 13 +- ...tutorial_ternaryweight_cifar10_tfrecord.py | 109 +++- example/tutorial_tf_dataset_voc.py | 26 +- example/tutorial_tfrecord.py | 10 +- example/tutorial_tfrecord2.py | 14 +- example/tutorial_tfrecord3.py | 58 +- example/tutorial_vgg16.py | 199 ++++++- example/tutorial_vgg19.py | 337 +++++++++-- example/tutorial_word2vec_basic.py | 53 +- tensorlayer/cost.py | 28 +- tensorlayer/db.py | 11 +- tensorlayer/deprecation.py | 11 +- tensorlayer/distributed.py | 50 +- tensorlayer/files.py | 143 +++-- tensorlayer/iterate.py | 2 +- tensorlayer/layers/binary.py | 153 ++++- tensorlayer/layers/convolution.py | 561 +++++++++++++----- tensorlayer/layers/core.py | 115 +++- tensorlayer/layers/importer.py | 3 +- tensorlayer/layers/merge.py | 5 +- tensorlayer/layers/normalization.py | 89 ++- tensorlayer/layers/padding.py | 3 +- tensorlayer/layers/pooling.py | 128 +++- tensorlayer/layers/recurrent.py | 164 +++-- tensorlayer/layers/spatial_transformer.py | 11 +- tensorlayer/layers/special_activation.py | 10 +- tensorlayer/models/mobilenetv1.py | 15 +- tensorlayer/models/squeezenetv1.py | 3 +- tensorlayer/models/vgg16.py | 137 ++++- tensorlayer/nlp.py | 60 +- tensorlayer/prepro.py | 425 +++++++------ tensorlayer/utils.py | 22 +- tensorlayer/visualize.py | 53 +- tests/test_layers_convolution.py | 40 +- tests/test_layers_core.py | 3 +- tests/test_layers_recurrent.py | 81 ++- tests/test_layers_spatial_transformer.py | 18 +- tests/test_layers_time_distributed.py | 8 +- tests/test_mnist_simple.py | 12 +- 61 files changed, 2865 insertions(+), 1021 deletions(-) diff --git a/example/tutorial_atari_pong.py b/example/tutorial_atari_pong.py index dd04ec309..6bddabba8 100644 --- a/example/tutorial_atari_pong.py +++ b/example/tutorial_atari_pong.py @@ -133,8 +133,9 @@ def prepro(I): prev_x = None if reward != 0: - print(('episode %d: game %d took %.5fs, reward: %f' % - (episode_number, game_number, time.time() - start_time, reward)), ('' - if reward == -1 else ' !!!!!!!!')) + print(( + 'episode %d: game %d took %.5fs, reward: %f' % + (episode_number, game_number, time.time() - start_time, reward) + ), ('' if reward == -1 else ' !!!!!!!!')) start_time = time.time() game_number += 1 diff --git a/example/tutorial_binarynet_cifar10_tfrecord.py b/example/tutorial_binarynet_cifar10_tfrecord.py index 9671072cb..efcc131a1 100644 --- a/example/tutorial_binarynet_cifar10_tfrecord.py +++ b/example/tutorial_binarynet_cifar10_tfrecord.py @@ -83,7 +83,9 @@ def data_to_tfrecord(images, labels, filename): feature={ "label": tf.train.Feature(int64_list=tf.train.Int64List(value=[label])), 'img_raw': tf.train.Feature(bytes_list=tf.train.BytesList(value=[img_raw])), - })) + } + ) + ) writer.write(example.SerializeToString()) # Serialize To String writer.close() @@ -98,12 +100,13 @@ def read_and_decode(filename, is_train=None): features={ 'label': tf.FixedLenFeature([], tf.int64), 'img_raw': tf.FixedLenFeature([], tf.string), - }) + } + ) # You can do more image distortion here for training data img = tf.decode_raw(features['img_raw'], tf.float32) img = tf.reshape(img, [32, 32, 3]) # img = tf.cast(img, tf.float32) #* (1. / 255) - 0.5 - if is_train == True: + if is_train ==True: # 1. Randomly crop a [height, width] section of the image. img = tf.random_crop(img, [24, 24, 3]) # 2. Randomly flip the image horizontally. @@ -148,11 +151,17 @@ def read_and_decode(filename, is_train=None): x_test_, y_test_ = read_and_decode("test.cifar10", False) x_train_batch, y_train_batch = tf.train.shuffle_batch( - [x_train_, y_train_], batch_size=batch_size, capacity=2000, min_after_dequeue=1000, - num_threads=32) # set the number of threads here + [x_train_, y_train_], + batch_size=batch_size, + capacity=2000, + min_after_dequeue=1000, + num_threads=32 + ) # set the number of threads here # for testing, uses batch instead of shuffle_batch - x_test_batch, y_test_batch = tf.train.batch( - [x_test_, y_test_], batch_size=batch_size, capacity=50000, num_threads=32) + x_test_batch, y_test_batch = tf.train.batch([x_test_, y_test_], + batch_size=batch_size, + capacity=50000, + num_threads=32) def model(x_crop, y_, reuse): """ For more simplified CNN APIs, check tensorlayer.org """ @@ -165,23 +174,58 @@ def model(x_crop, y_, reuse): net = tl.layers.SignLayer(net) net = tl.layers.MaxPool2d(net, (3, 3), (2, 2), padding='SAME', name='pool1') net = tl.layers.LocalResponseNormLayer( - net, depth_radius=4, bias=1.0, alpha=0.001 / 9.0, beta=0.75, name='norm1') + net, + depth_radius=4, + bias=1.0, + alpha=0.001 / 9.0, + beta=0.75, + name='norm1' + ) net = tl.layers.BinaryConv2d( - net, 64, (5, 5), (1, 1), act=tf.nn.relu, padding='SAME', W_init=W_init, name='cnn2') + net, + 64, + (5, 5), + (1, 1), + act=tf.nn.relu, + padding='SAME', + W_init=W_init, + name='cnn2' + ) net = tl.layers.LocalResponseNormLayer( - net, depth_radius=4, bias=1.0, alpha=0.001 / 9.0, beta=0.75, name='norm2') + net, + depth_radius=4, + bias=1.0, + alpha=0.001 / 9.0, + beta=0.75, + name='norm2' + ) net = tl.layers.MaxPool2d(net, (3, 3), (2, 2), padding='SAME', name='pool2') net = tl.layers.FlattenLayer(net, name='flatten') # output: (batch_size, 2304) net = tl.layers.SignLayer(net) net = tl.layers.BinaryDenseLayer( - net, n_units=384, act=tf.nn.relu, W_init=W_init2, b_init=b_init2, - name='d1relu') # output: (batch_size, 384) + net, + n_units=384, + act=tf.nn.relu, + W_init=W_init2, + b_init=b_init2, + name='d1relu' + ) # output: (batch_size, 384) net = tl.layers.SignLayer(net) net = tl.layers.BinaryDenseLayer( - net, n_units=192, act=tf.nn.relu, W_init=W_init2, b_init=b_init2, - name='d2relu') # output: (batch_size, 192) + net, + n_units=192, + act=tf.nn.relu, + W_init=W_init2, + b_init=b_init2, + name='d2relu' + ) # output: (batch_size, 192) net = tl.layers.DenseLayer( - net, n_units=10, act=tf.identity, W_init=W_init2, name='output') # output: (batch_size, 10) + net, + n_units=10, + act=tf.identity, + W_init=W_init2, + name='output' + ) # output: (batch_size, 10) y = net.outputs ce = tl.cost.cross_entropy(y, y_, name='cost') @@ -213,13 +257,28 @@ def model_batch_norm(x_crop, y_, reuse, is_train): net = tl.layers.MaxPool2d(net, (3, 3), (2, 2), padding='SAME', name='pool2') net = tl.layers.FlattenLayer(net, name='flatten') # output: (batch_size, 2304) net = tl.layers.DenseLayer( - net, n_units=384, act=tf.nn.relu, W_init=W_init2, b_init=b_init2, - name='d1relu') # output: (batch_size, 384) + net, + n_units=384, + act=tf.nn.relu, + W_init=W_init2, + b_init=b_init2, + name='d1relu' + ) # output: (batch_size, 384) net = tl.layers.DenseLayer( - net, n_units=192, act=tf.nn.relu, W_init=W_init2, b_init=b_init2, - name='d2relu') # output: (batch_size, 192) + net, + n_units=192, + act=tf.nn.relu, + W_init=W_init2, + b_init=b_init2, + name='d2relu' + ) # output: (batch_size, 192) net = tl.layers.DenseLayer( - net, n_units=10, act=tf.identity, W_init=W_init2, name='output') # output: (batch_size, 10) + net, + n_units=10, + act=tf.identity, + W_init=W_init2, + name='output' + ) # output: (batch_size, 10) y = net.outputs ce = tl.cost.cross_entropy(y, y_, name='cost') @@ -289,8 +348,14 @@ def model_batch_norm(x_crop, y_, reuse, is_train): n_batch += 1 if epoch + 1 == 1 or (epoch + 1) % print_freq == 0: - print("Epoch %d : Step %d-%d of %d took %fs" % (epoch, step, step + n_step_epoch, n_step, - time.time() - start_time)) + print( + "Epoch %d : Step %d-%d of %d took %fs" % + (epoch, + step, + step + n_step_epoch, + n_step, + time.time() - start_time) + ) print(" train loss: %f" % (train_loss / n_batch)) print(" train acc: %f" % (train_acc / n_batch)) diff --git a/example/tutorial_bipedalwalker_a3c_continuous_action.py b/example/tutorial_bipedalwalker_a3c_continuous_action.py index be3404f2a..2f251efb0 100644 --- a/example/tutorial_bipedalwalker_a3c_continuous_action.py +++ b/example/tutorial_bipedalwalker_a3c_continuous_action.py @@ -162,7 +162,8 @@ def save_ckpt(self): mode_name='model.ckpt', var_list=self.a_params + self.c_params, save_dir=self.scope, - printable=True) + printable=True + ) def load_ckpt(self): tl.files.load_ckpt(sess=sess, var_list=self.a_params + self.c_params, save_dir=self.scope, printable=True) @@ -208,8 +209,8 @@ def work(self): buffer_v_target.append(v_s_) buffer_v_target.reverse() - buffer_s, buffer_a, buffer_v_target = np.vstack(buffer_s), np.vstack(buffer_a), np.vstack( - buffer_v_target) + buffer_s, buffer_a, buffer_v_target = np.vstack(buffer_s), np.vstack(buffer_a + ), np.vstack(buffer_v_target) feed_dict = { self.AC.s: buffer_s, self.AC.a_his: buffer_a, diff --git a/example/tutorial_cartpole_ac.py b/example/tutorial_cartpole_ac.py index f86a7ca9a..7b9d91955 100644 --- a/example/tutorial_cartpole_ac.py +++ b/example/tutorial_cartpole_ac.py @@ -86,7 +86,11 @@ def __init__(self, sess, n_features, n_actions, lr=0.001): # Hao Dong with tf.variable_scope('loss'): self.exp_v = tl.rein.cross_entropy_reward_loss( - logits=self.acts_logits, actions=self.a, rewards=self.td_error, name='actor_weighted_loss') + logits=self.acts_logits, + actions=self.a, + rewards=self.td_error, + name='actor_weighted_loss' + ) with tf.variable_scope('train'): self.train_op = tf.train.AdamOptimizer(lr).minimize(self.exp_v) @@ -145,7 +149,10 @@ def learn(self, s, r, s_): actor = Actor(sess, n_features=N_F, n_actions=N_A, lr=LR_A) critic = Critic( - sess, n_features=N_F, lr=LR_C) # we need a good teacher, so the teacher should learn faster than the actor + sess, + n_features=N_F, + lr=LR_C +) # we need a good teacher, so the teacher should learn faster than the actor tl.layers.initialize_global_variables(sess) @@ -189,8 +196,13 @@ def learn(self, s, r, s_): running_reward = running_reward * 0.95 + ep_rs_sum * 0.05 # start rending if running_reward greater than a threshold # if running_reward > DISPLAY_REWARD_THRESHOLD: RENDER = True - print("Episode: %d reward: %f running_reward %f took: %.5f" % (i_episode, ep_rs_sum, running_reward, - time.time() - episode_time)) + print( + "Episode: %d reward: %f running_reward %f took: %.5f" % + (i_episode, + ep_rs_sum, + running_reward, + time.time() - episode_time) + ) # Early Stopping for quick check if t >= MAX_EP_STEPS: diff --git a/example/tutorial_cifar10.py b/example/tutorial_cifar10.py index 88a1f1c72..bc4b3ef4c 100644 --- a/example/tutorial_cifar10.py +++ b/example/tutorial_cifar10.py @@ -45,14 +45,28 @@ def model(x, y_, reuse): # padding='SAME', pool = tf.nn.max_pool, name ='pool2') # output: (batch_size, 6, 6, 64) net = FlattenLayer(net, name='flatten') # output: (batch_size, 2304) net = DenseLayer( - net, n_units=384, act=tf.nn.relu, W_init=W_init2, b_init=b_init2, - name='d1relu') # output: (batch_size, 384) + net, + n_units=384, + act=tf.nn.relu, + W_init=W_init2, + b_init=b_init2, + name='d1relu' + ) # output: (batch_size, 384) net = DenseLayer( - net, n_units=192, act=tf.nn.relu, W_init=W_init2, b_init=b_init2, - name='d2relu') # output: (batch_size, 192) + net, + n_units=192, + act=tf.nn.relu, + W_init=W_init2, + b_init=b_init2, + name='d2relu' + ) # output: (batch_size, 192) net = DenseLayer( - net, n_units=10, act=tf.identity, W_init=tf.truncated_normal_initializer(stddev=1 / 192.0), - name='output') # output: (batch_size, 10) + net, + n_units=10, + act=tf.identity, + W_init=tf.truncated_normal_initializer(stddev=1 / 192.0), + name='output' + ) # output: (batch_size, 10) y = net.outputs ce = tl.cost.cross_entropy(y, y_, name='cost') @@ -96,14 +110,28 @@ def model_batch_norm(x, y_, reuse, is_train): net = FlattenLayer(net, name='flatten') # output: (batch_size, 2304) net = DenseLayer( - net, n_units=384, act=tf.nn.relu, W_init=W_init2, b_init=b_init2, - name='d1relu') # output: (batch_size, 384) + net, + n_units=384, + act=tf.nn.relu, + W_init=W_init2, + b_init=b_init2, + name='d1relu' + ) # output: (batch_size, 384) net = DenseLayer( - net, n_units=192, act=tf.nn.relu, W_init=W_init2, b_init=b_init2, - name='d2relu') # output: (batch_size, 192) + net, + n_units=192, + act=tf.nn.relu, + W_init=W_init2, + b_init=b_init2, + name='d2relu' + ) # output: (batch_size, 192) net = DenseLayer( - net, n_units=10, act=tf.identity, W_init=tf.truncated_normal_initializer(stddev=1 / 192.0), - name='output') # output: (batch_size, 10) + net, + n_units=10, + act=tf.identity, + W_init=tf.truncated_normal_initializer(stddev=1 / 192.0), + name='output' + ) # output: (batch_size, 10) y = net.outputs ce = tl.cost.cross_entropy(y, y_, name='cost') @@ -150,9 +178,12 @@ def distort_fn(x, is_train=False): x = tf.placeholder(tf.float32, shape=[None, 24, 24, 3], name='x') y_ = tf.placeholder( - tf.int64, shape=[ + tf.int64, + shape=[ None, - ], name='y_') + ], + name='y_' +) ## using local response normalization # network, cost, _ = model(x, y_, False) @@ -169,8 +200,14 @@ def distort_fn(x, is_train=False): train_params = network.all_params train_op = tf.train.AdamOptimizer( - learning_rate, beta1=0.9, beta2=0.999, epsilon=1e-08, use_locking=False).minimize( - cost, var_list=train_params) + learning_rate, + beta1=0.9, + beta2=0.999, + epsilon=1e-08, + use_locking=False +).minimize( + cost, var_list=train_params +) tl.layers.initialize_global_variables(sess) diff --git a/example/tutorial_cifar10_tfrecord.py b/example/tutorial_cifar10_tfrecord.py index 01b37bfbf..51902ec6f 100644 --- a/example/tutorial_cifar10_tfrecord.py +++ b/example/tutorial_cifar10_tfrecord.py @@ -84,7 +84,9 @@ def data_to_tfrecord(images, labels, filename): feature={ "label": tf.train.Feature(int64_list=tf.train.Int64List(value=[label])), 'img_raw': tf.train.Feature(bytes_list=tf.train.BytesList(value=[img_raw])), - })) + } + ) + ) writer.write(example.SerializeToString()) # Serialize To String writer.close() @@ -99,12 +101,13 @@ def read_and_decode(filename, is_train=None): features={ 'label': tf.FixedLenFeature([], tf.int64), 'img_raw': tf.FixedLenFeature([], tf.string), - }) + } + ) # You can do more image distortion here for training data img = tf.decode_raw(features['img_raw'], tf.float32) img = tf.reshape(img, [32, 32, 3]) # img = tf.cast(img, tf.float32) #* (1. / 255) - 0.5 - if is_train == True: + if is_train ==True: # 1. Randomly crop a [height, width] section of the image. img = tf.random_crop(img, [24, 24, 3]) # 2. Randomly flip the image horizontally. @@ -176,11 +179,17 @@ def read_and_decode(filename, is_train=None): x_test_, y_test_ = read_and_decode("test.cifar10", False) x_train_batch, y_train_batch = tf.train.shuffle_batch( - [x_train_, y_train_], batch_size=batch_size, capacity=2000, min_after_dequeue=1000, - num_threads=32) # set the number of threads here + [x_train_, y_train_], + batch_size=batch_size, + capacity=2000, + min_after_dequeue=1000, + num_threads=32 + ) # set the number of threads here # for testing, uses batch instead of shuffle_batch - x_test_batch, y_test_batch = tf.train.batch( - [x_test_, y_test_], batch_size=batch_size, capacity=50000, num_threads=32) + x_test_batch, y_test_batch = tf.train.batch([x_test_, y_test_], + batch_size=batch_size, + capacity=50000, + num_threads=32) def model(x_crop, y_, reuse): """ For more simplified CNN APIs, check tensorlayer.org """ @@ -212,13 +221,28 @@ def model(x_crop, y_, reuse): # padding='SAME', pool = tf.nn.max_pool, name ='pool2') # output: (batch_size, 6, 6, 64) net = FlattenLayer(net, name='flatten') # output: (batch_size, 2304) net = DenseLayer( - net, n_units=384, act=tf.nn.relu, W_init=W_init2, b_init=b_init2, - name='d1relu') # output: (batch_size, 384) + net, + n_units=384, + act=tf.nn.relu, + W_init=W_init2, + b_init=b_init2, + name='d1relu' + ) # output: (batch_size, 384) net = DenseLayer( - net, n_units=192, act=tf.nn.relu, W_init=W_init2, b_init=b_init2, - name='d2relu') # output: (batch_size, 192) + net, + n_units=192, + act=tf.nn.relu, + W_init=W_init2, + b_init=b_init2, + name='d2relu' + ) # output: (batch_size, 192) net = DenseLayer( - net, n_units=10, act=tf.identity, W_init=W_init2, name='output') # output: (batch_size, 10) + net, + n_units=10, + act=tf.identity, + W_init=W_init2, + name='output' + ) # output: (batch_size, 10) y = net.outputs ce = tl.cost.cross_entropy(y, y_, name='cost') @@ -262,13 +286,28 @@ def model_batch_norm(x_crop, y_, reuse, is_train): net = FlattenLayer(net, name='flatten') # output: (batch_size, 2304) net = DenseLayer( - net, n_units=384, act=tf.nn.relu, W_init=W_init2, b_init=b_init2, - name='d1relu') # output: (batch_size, 384) + net, + n_units=384, + act=tf.nn.relu, + W_init=W_init2, + b_init=b_init2, + name='d1relu' + ) # output: (batch_size, 384) net = DenseLayer( - net, n_units=192, act=tf.nn.relu, W_init=W_init2, b_init=b_init2, - name='d2relu') # output: (batch_size, 192) + net, + n_units=192, + act=tf.nn.relu, + W_init=W_init2, + b_init=b_init2, + name='d2relu' + ) # output: (batch_size, 192) net = DenseLayer( - net, n_units=10, act=tf.identity, W_init=W_init2, name='output') # output: (batch_size, 10) + net, + n_units=10, + act=tf.identity, + W_init=W_init2, + name='output' + ) # output: (batch_size, 10) y = net.outputs ce = tl.cost.cross_entropy(y, y_, name='cost') @@ -338,8 +377,14 @@ def model_batch_norm(x_crop, y_, reuse, is_train): n_batch += 1 if epoch + 1 == 1 or (epoch + 1) % print_freq == 0: - print("Epoch %d : Step %d-%d of %d took %fs" % (epoch, step, step + n_step_epoch, n_step, - time.time() - start_time)) + print( + "Epoch %d : Step %d-%d of %d took %fs" % + (epoch, + step, + step + n_step_epoch, + n_step, + time.time() - start_time) + ) print(" train loss: %f" % (train_loss / n_batch)) print(" train acc: %f" % (train_acc / n_batch)) diff --git a/example/tutorial_dorefanet_cifar10_tfrecord.py b/example/tutorial_dorefanet_cifar10_tfrecord.py index 742c8ac5d..c720bc738 100644 --- a/example/tutorial_dorefanet_cifar10_tfrecord.py +++ b/example/tutorial_dorefanet_cifar10_tfrecord.py @@ -83,7 +83,9 @@ def data_to_tfrecord(images, labels, filename): feature={ "label": tf.train.Feature(int64_list=tf.train.Int64List(value=[label])), 'img_raw': tf.train.Feature(bytes_list=tf.train.BytesList(value=[img_raw])), - })) + } + ) + ) writer.write(example.SerializeToString()) # Serialize To String writer.close() @@ -98,12 +100,13 @@ def read_and_decode(filename, is_train=None): features={ 'label': tf.FixedLenFeature([], tf.int64), 'img_raw': tf.FixedLenFeature([], tf.string), - }) + } + ) # You can do more image distortion here for training data img = tf.decode_raw(features['img_raw'], tf.float32) img = tf.reshape(img, [32, 32, 3]) # img = tf.cast(img, tf.float32) #* (1. / 255) - 0.5 - if is_train == True: + if is_train ==True: # 1. Randomly crop a [height, width] section of the image. img = tf.random_crop(img, [24, 24, 3]) # 2. Randomly flip the image horizontally. @@ -148,11 +151,17 @@ def read_and_decode(filename, is_train=None): x_test_, y_test_ = read_and_decode("test.cifar10", False) x_train_batch, y_train_batch = tf.train.shuffle_batch( - [x_train_, y_train_], batch_size=batch_size, capacity=2000, min_after_dequeue=1000, - num_threads=32) # set the number of threads here + [x_train_, y_train_], + batch_size=batch_size, + capacity=2000, + min_after_dequeue=1000, + num_threads=32 + ) # set the number of threads here # for testing, uses batch instead of shuffle_batch - x_test_batch, y_test_batch = tf.train.batch( - [x_test_, y_test_], batch_size=batch_size, capacity=50000, num_threads=32) + x_test_batch, y_test_batch = tf.train.batch([x_test_, y_test_], + batch_size=batch_size, + capacity=50000, + num_threads=32) def model(x_crop, y_, reuse): """ For more simplified CNN APIs, check tensorlayer.org """ @@ -164,21 +173,62 @@ def model(x_crop, y_, reuse): net = tl.layers.Conv2d(net, 64, (5, 5), (1, 1), act=tf.nn.relu, padding='SAME', W_init=W_init, name='cnn1') net = tl.layers.MaxPool2d(net, (3, 3), (2, 2), padding='SAME', name='pool1') net = tl.layers.LocalResponseNormLayer( - net, depth_radius=4, bias=1.0, alpha=0.001 / 9.0, beta=0.75, name='norm1') + net, + depth_radius=4, + bias=1.0, + alpha=0.001 / 9.0, + beta=0.75, + name='norm1' + ) net = tl.layers.DorefaConv2d( - net, 1, 3, 64, (5, 5), (1, 1), tf.nn.relu, padding='SAME', W_init=W_init, name='cnn2') + net, + 1, + 3, + 64, + (5, 5), + (1, 1), + tf.nn.relu, + padding='SAME', + W_init=W_init, + name='cnn2' + ) net = tl.layers.LocalResponseNormLayer( - net, depth_radius=4, bias=1.0, alpha=0.001 / 9.0, beta=0.75, name='norm2') + net, + depth_radius=4, + bias=1.0, + alpha=0.001 / 9.0, + beta=0.75, + name='norm2' + ) net = tl.layers.MaxPool2d(net, (3, 3), (2, 2), padding='SAME', name='pool2') net = tl.layers.FlattenLayer(net, name='flatten') # output: (batch_size, 2304) net = tl.layers.DorefaDenseLayer( - net, 1, 3, 384, act=tf.nn.relu, W_init=W_init2, b_init=b_init2, - name='d1relu') # output: (batch_size, 384) + net, + 1, + 3, + 384, + act=tf.nn.relu, + W_init=W_init2, + b_init=b_init2, + name='d1relu' + ) # output: (batch_size, 384) net = tl.layers.DorefaDenseLayer( - net, 1, 3, 192, act=tf.nn.relu, W_init=W_init2, b_init=b_init2, - name='d2relu') # output: (batch_size, 192) + net, + 1, + 3, + 192, + act=tf.nn.relu, + W_init=W_init2, + b_init=b_init2, + name='d2relu' + ) # output: (batch_size, 192) net = tl.layers.DenseLayer( - net, n_units=10, act=tf.identity, W_init=W_init2, name='output') # output: (batch_size, 10) + net, + n_units=10, + act=tf.identity, + W_init=W_init2, + name='output' + ) # output: (batch_size, 10) y = net.outputs ce = tl.cost.cross_entropy(y, y_, name='cost') @@ -209,13 +259,28 @@ def model_batch_norm(x_crop, y_, reuse, is_train): net = tl.layers.MaxPool2d(net, (3, 3), (2, 2), padding='SAME', name='pool2') net = tl.layers.FlattenLayer(net, name='flatten') # output: (batch_size, 2304) net = tl.layers.DenseLayer( - net, n_units=384, act=tf.nn.relu, W_init=W_init2, b_init=b_init2, - name='d1relu') # output: (batch_size, 384) + net, + n_units=384, + act=tf.nn.relu, + W_init=W_init2, + b_init=b_init2, + name='d1relu' + ) # output: (batch_size, 384) net = tl.layers.DenseLayer( - net, n_units=192, act=tf.nn.relu, W_init=W_init2, b_init=b_init2, - name='d2relu') # output: (batch_size, 192) + net, + n_units=192, + act=tf.nn.relu, + W_init=W_init2, + b_init=b_init2, + name='d2relu' + ) # output: (batch_size, 192) net = tl.layers.DenseLayer( - net, n_units=10, act=tf.identity, W_init=W_init2, name='output') # output: (batch_size, 10) + net, + n_units=10, + act=tf.identity, + W_init=W_init2, + name='output' + ) # output: (batch_size, 10) y = net.outputs ce = tl.cost.cross_entropy(y, y_, name='cost') # L2 for the MLP, without this, the accuracy will be reduced by 15%. @@ -284,8 +349,14 @@ def model_batch_norm(x_crop, y_, reuse, is_train): n_batch += 1 if epoch + 1 == 1 or (epoch + 1) % print_freq == 0: - print("Epoch %d : Step %d-%d of %d took %fs" % (epoch, step, step + n_step_epoch, n_step, - time.time() - start_time)) + print( + "Epoch %d : Step %d-%d of %d took %fs" % + (epoch, + step, + step + n_step_epoch, + n_step, + time.time() - start_time) + ) print(" train loss: %f" % (train_loss / n_batch)) print(" train acc: %f" % (train_acc / n_batch)) diff --git a/example/tutorial_frozenlake_dqn.py b/example/tutorial_frozenlake_dqn.py index b2610fd40..d8f81a34d 100644 --- a/example/tutorial_frozenlake_dqn.py +++ b/example/tutorial_frozenlake_dqn.py @@ -49,11 +49,17 @@ def to_one_hot(i, n_classes=None): inputs = tf.placeholder(shape=[1, 16], dtype=tf.float32) net = InputLayer(inputs, name='observation') net = DenseLayer( - net, n_units=4, act=tf.identity, W_init=tf.random_uniform_initializer(0, 0.01), b_init=None, name='q_a_s') + net, + n_units=4, + act=tf.identity, + W_init=tf.random_uniform_initializer(0, 0.01), + b_init=None, + name='q_a_s' +) y = net.outputs # action-value / rewards of 4 actions predict = tf.argmax( - y, - 1) # chose action greedily with reward. in Q-Learning, policy is greedy, so we use "max" to select the next action. + y, 1 +) # chose action greedily with reward. in Q-Learning, policy is greedy, so we use "max" to select the next action. ## Below we obtain the loss by taking the sum of squares difference between the target and prediction Q values. nextQ = tf.placeholder(shape=[1, 4], dtype=tf.float32) @@ -96,12 +102,18 @@ def to_one_hot(i, n_classes=None): rAll += r s = s1 ## Reduce chance of random action if an episode is done. - if d == True: + if d ==True: e = 1. / ((i / 50) + 10) # reduce e, GLIE: Greey in the limit with infinite Exploration break ## Note that, the rewards here with random action running_reward = rAll if running_reward is None else running_reward * 0.99 + rAll * 0.01 - print("Episode [%d/%d] sum reward:%f running reward:%f took:%.5fs %s" % (i, num_episodes, rAll, running_reward, - time.time() - episode_time, '' - if rAll == 0 else ' !!!!!!!!')) + print( + "Episode [%d/%d] sum reward:%f running reward:%f took:%.5fs %s" % + (i, + num_episodes, + rAll, + running_reward, + time.time() - episode_time, + '' if rAll == 0 else ' !!!!!!!!') + ) diff --git a/example/tutorial_frozenlake_q_table.py b/example/tutorial_frozenlake_q_table.py index 0375e67fb..427ee6329 100644 --- a/example/tutorial_frozenlake_q_table.py +++ b/example/tutorial_frozenlake_q_table.py @@ -48,12 +48,18 @@ Q[s, a] = Q[s, a] + lr * (r + lambd * np.max(Q[s1, :]) - Q[s, a]) rAll += r s = s1 - if d == True: + if d ==True: break rList.append(rAll) running_reward = r if running_reward is None else running_reward * 0.99 + r * 0.01 - print("Episode [%d/%d] sum reward:%f running reward:%f took:%.5fs %s" % (i, num_episodes, rAll, running_reward, - time.time() - episode_time, '' - if rAll == 0 else ' !!!!!!!!')) + print( + "Episode [%d/%d] sum reward:%f running reward:%f took:%.5fs %s" % + (i, + num_episodes, + rAll, + running_reward, + time.time() - episode_time, + '' if rAll == 0 else ' !!!!!!!!') + ) print("Final Q-Table Values:/n %s" % Q) diff --git a/example/tutorial_generate_text.py b/example/tutorial_generate_text.py index 58e884415..b53b13dda 100644 --- a/example/tutorial_generate_text.py +++ b/example/tutorial_generate_text.py @@ -156,7 +156,11 @@ def main_restore_embedding_layer(): x = tf.placeholder(tf.int32, shape=[batch_size]) emb_net = tl.layers.EmbeddingInputlayer( - inputs=x, vocabulary_size=vocabulary_size, embedding_size=embedding_size, name='embedding_layer') + inputs=x, + vocabulary_size=vocabulary_size, + embedding_size=embedding_size, + name='embedding_layer' + ) # sess.run(tf.initialize_all_variables()) tl.layers.initialize_global_variables(sess) @@ -233,7 +237,12 @@ def inference(x, is_train, sequence_length, reuse=None): rnn_init = tf.random_uniform_initializer(-init_scale, init_scale) with tf.variable_scope("model", reuse=reuse): network = EmbeddingInputlayer( - inputs=x, vocabulary_size=vocab_size, embedding_size=hidden_size, E_init=rnn_init, name='embedding') + inputs=x, + vocabulary_size=vocab_size, + embedding_size=hidden_size, + E_init=rnn_init, + name='embedding' + ) network = RNNLayer( network, cell_fn=tf.contrib.rnn.BasicLSTMCell, @@ -246,10 +255,17 @@ def inference(x, is_train, sequence_length, reuse=None): n_steps=sequence_length, return_last=False, return_seq_2d=True, - name='lstm1') + name='lstm1' + ) lstm1 = network network = DenseLayer( - network, n_units=vocab_size, W_init=rnn_init, b_init=rnn_init, act=tf.identity, name='output') + network, + n_units=vocab_size, + W_init=rnn_init, + b_init=rnn_init, + act=tf.identity, + name='output' + ) return network, lstm1 # Inference for Training @@ -310,19 +326,22 @@ def loss_fn(outputs, targets, batch_size, sequence_length): ## reset all states at the begining of every epoch state1 = tl.layers.initialize_rnn_state(lstm1.initial_state) for step, (x, y) in enumerate(tl.iterate.ptb_iterator(train_data, batch_size, sequence_length)): - _cost, state1, _ = sess.run( - [cost, lstm1.final_state, train_op], - feed_dict={ - input_data: x, - targets: y, - lstm1.initial_state: state1, - }) + _cost, state1, _ = sess.run([cost, lstm1.final_state, train_op], + feed_dict={ + input_data: x, + targets: y, + lstm1.initial_state: state1, + }) costs += _cost iters += sequence_length if step % (epoch_size // 10) == 1: - print("%.3f perplexity: %.3f speed: %.0f wps" % (step * 1.0 / epoch_size, np.exp(costs / iters), - iters * batch_size / (time.time() - start_time))) + print( + "%.3f perplexity: %.3f speed: %.0f wps" % + (step * 1.0 / epoch_size, + np.exp(costs / iters), + iters * batch_size / (time.time() - start_time)) + ) train_perplexity = np.exp(costs / iters) # print("Epoch: %d Train Perplexity: %.3f" % (i + 1, train_perplexity)) print("Epoch: %d/%d Train Perplexity: %.3f" % (i + 1, max_max_epoch, train_perplexity)) @@ -340,20 +359,21 @@ def loss_fn(outputs, targets, batch_size, sequence_length): state1 = sess.run( [ lstm1_test.final_state, - ], feed_dict={ + ], + feed_dict={ input_data_test: a_id, lstm1_test.initial_state: state1, - }) + } + ) # feed the last word in seed, and start to generate sentence. a_id = outs_id[-1] for _ in range(print_length): a_id = np.asarray(a_id).reshape(1, 1) - out, state1 = sess.run( - [y_soft, lstm1_test.final_state], - feed_dict={ - input_data_test: a_id, - lstm1_test.initial_state: state1, - }) + out, state1 = sess.run([y_soft, lstm1_test.final_state], + feed_dict={ + input_data_test: a_id, + lstm1_test.initial_state: state1, + }) ## Without sampling # a_id = np.argmax(out[0]) ## Sample from all words, if vocab_size is large, diff --git a/example/tutorial_imagenet_inceptionV3_distributed.py b/example/tutorial_imagenet_inceptionV3_distributed.py index d43df39f4..31adc94b0 100644 --- a/example/tutorial_imagenet_inceptionV3_distributed.py +++ b/example/tutorial_imagenet_inceptionV3_distributed.py @@ -132,7 +132,11 @@ def _parse_example_fn(line): def _map_fn(example_serialized): image_bytes, one_hot_labels = tf.py_func( - _parse_example_fn, [example_serialized], [tf.string, tf.float32], stateful=False) + _parse_example_fn, + [example_serialized], + [tf.string, tf.float32], + stateful=False + ) image = tf.image.decode_jpeg(image_bytes, channels=3) image = tf.image.resize_images(image, size=[image_size, image_size]) @@ -165,7 +169,8 @@ def build_network(image_input, num_classes=1001, is_training=False): 'num_classes': num_classes, 'is_training': is_training }, - name='InceptionV3') + name='InceptionV3' + ) predictions = tf.nn.sigmoid(network.outputs, name='Predictions') return network, predictions @@ -354,7 +359,8 @@ def run_worker(task_spec, checkpoints_path, batch_size=32, epochs=10): decay_steps=steps_per_epoch * 2, # 2 epochs decay_rate=0.94, staircase=True, - name='learning_rate') + name='learning_rate' + ) optimizer = tf.train.RMSPropOptimizer(learning_rate=learning_rate, decay=0.9, epsilon=1.0) # clip and apply gradients gvs = optimizer.compute_gradients(loss=loss, var_list=network.all_params) @@ -368,7 +374,10 @@ def run_worker(task_spec, checkpoints_path, batch_size=32, epochs=10): tf.summary.scalar('learning_rate/value', learning_rate) tf.summary.scalar('loss/logits', loss) _, metrics_average_ops, metrics_ops = calculate_metrics( - predicted_batch=predictions, real_batch=one_hot_classes, is_training=True) + predicted_batch=predictions, + real_batch=one_hot_classes, + is_training=True + ) with tf.control_dependencies([train_op]): train_op = tf.group(metrics_average_ops) @@ -402,7 +411,16 @@ def run_worker(task_spec, checkpoints_path, batch_size=32, epochs=10): max_steps = epochs * steps_per_epoch m = 'Epoch: {}/{} Steps: {}/{} Loss: {} Learning rate: {} Metrics: {}' logging.info( - m.format(current_epoch, epochs, step, max_steps, loss_val, learning_rate_val, metrics)) + m.format( + current_epoch, + epochs, + step, + max_steps, + loss_val, + learning_rate_val, + metrics + ) + ) except OutOfRangeError: pass diff --git a/example/tutorial_imdb_fasttext.py b/example/tutorial_imdb_fasttext.py index 847815405..2cc2f4068 100644 --- a/example/tutorial_imdb_fasttext.py +++ b/example/tutorial_imdb_fasttext.py @@ -140,7 +140,8 @@ def train_test_and_save_model(): feed_dict={ classifier.inputs: tl.prepro.pad_sequences(X_batch), classifier.labels: y_batch, - }) + } + ) print(" took %.5fs" % (time.time() - start_time)) @@ -149,7 +150,8 @@ def train_test_and_save_model(): feed_dict={ classifier.inputs: tl.prepro.pad_sequences(X_test), classifier.labels: y_test, - }) + } + ) print('Test accuracy: %.5f' % test_accuracy) classifier.save(sess, MODEL_FILE_PATH) diff --git a/example/tutorial_inceptionV3_tfslim.py b/example/tutorial_inceptionV3_tfslim.py index 31fffb78b..69a45769c 100644 --- a/example/tutorial_inceptionV3_tfslim.py +++ b/example/tutorial_inceptionV3_tfslim.py @@ -42,7 +42,8 @@ from data.imagenet_classes import * except Exception as e: raise Exception( - "{} / download the file from: https://github.com/zsdonghao/tensorlayer/tree/master/example/data".format(e)) + "{} / download the file from: https://github.com/zsdonghao/tensorlayer/tree/master/example/data".format(e) + ) def load_image(path): @@ -124,7 +125,8 @@ def print_prob(prob): saver = tf.train.Saver() if not os.path.isfile("inception_v3.ckpt"): raise Exception( - "Please download inception_v3 ckpt from : https://github.com/tensorflow/models/tree/master/research/slim") + "Please download inception_v3 ckpt from : https://github.com/tensorflow/models/tree/master/research/slim" + ) try: # TF12+ saver.restore(sess, "./inception_v3.ckpt") @@ -135,7 +137,8 @@ def print_prob(prob): y = network.outputs probs = tf.nn.softmax(y) img1 = load_image( - "data/puzzle.jpeg") # test data in github: https://github.com/zsdonghao/tensorlayer/tree/master/example/data + "data/puzzle.jpeg" +) # test data in github: https://github.com/zsdonghao/tensorlayer/tree/master/example/data img1 = img1.reshape((1, 299, 299, 3)) prob = sess.run(probs, feed_dict={x: img1}) # the 1st time need time to compile diff --git a/example/tutorial_keras.py b/example/tutorial_keras.py index be3bd5d64..2d07f8993 100644 --- a/example/tutorial_keras.py +++ b/example/tutorial_keras.py @@ -44,8 +44,14 @@ def keras_block(x): train_params = network.all_params train_op = tf.train.AdamOptimizer( - learning_rate, beta1=0.9, beta2=0.999, epsilon=1e-08, use_locking=False).minimize( - cost, var_list=train_params) + learning_rate, + beta1=0.9, + beta2=0.999, + epsilon=1e-08, + use_locking=False +).minimize( + cost, var_list=train_params +) tl.layers.initialize_global_variables(sess) diff --git a/example/tutorial_mlp_dropout2.py b/example/tutorial_mlp_dropout2.py index 1291a990e..ded39ed76 100644 --- a/example/tutorial_mlp_dropout2.py +++ b/example/tutorial_mlp_dropout2.py @@ -42,8 +42,14 @@ def mlp(x, is_train=True, reuse=False): # define the optimizer train_params = tl.layers.get_variables_with_name('MLP', train_only=True, printable=False) train_op = tf.train.AdamOptimizer( - learning_rate=0.0001, beta1=0.9, beta2=0.999, epsilon=1e-08, use_locking=False).minimize( - cost, var_list=train_params) + learning_rate=0.0001, + beta1=0.9, + beta2=0.999, + epsilon=1e-08, + use_locking=False +).minimize( + cost, var_list=train_params +) # initialize all variables in the session tl.layers.initialize_global_variables(sess) diff --git a/example/tutorial_mnist.py b/example/tutorial_mnist.py index 1a344651f..96e46a84d 100644 --- a/example/tutorial_mnist.py +++ b/example/tutorial_mnist.py @@ -50,9 +50,19 @@ def main_test_layers(model='relu'): elif model == 'dropconnect': network = tl.layers.InputLayer(x, name='input') network = tl.layers.DropconnectDenseLayer( - network, keep=0.8, n_units=800, act=tf.nn.relu, name='dropconnect_relu1') + network, + keep=0.8, + n_units=800, + act=tf.nn.relu, + name='dropconnect_relu1' + ) network = tl.layers.DropconnectDenseLayer( - network, keep=0.5, n_units=800, act=tf.nn.relu, name='dropconnect_relu2') + network, + keep=0.5, + n_units=800, + act=tf.nn.relu, + name='dropconnect_relu2' + ) network = tl.layers.DropconnectDenseLayer(network, keep=0.5, n_units=10, act=tf.identity, name='output') # To print all attributes of a Layer. @@ -200,7 +210,8 @@ def main_test_denoise_AE(model='relu'): batch_size=128, print_freq=10, save=True, - save_name='w1pre_') + save_name='w1pre_' + ) # You can also disable denoisong by setting denoise_name=None. # recon_layer1.pretrain(sess, x=x, X_train=X_train, X_val=X_val, # denoise_name=None, n_epoch=500, batch_size=128, @@ -280,7 +291,8 @@ def main_test_stacked_denoise_AE(model='relu'): batch_size=128, print_freq=10, save=True, - save_name='w1pre_') + save_name='w1pre_' + ) print("\nPre-train Layer 2") recon_layer2.pretrain( sess, @@ -291,7 +303,8 @@ def main_test_stacked_denoise_AE(model='relu'): n_epoch=100, batch_size=128, print_freq=10, - save=False) + save=False + ) print("\nAll Network Params after pre-train") network.print_params() diff --git a/example/tutorial_mnist_distributed.py b/example/tutorial_mnist_distributed.py index 0a5ad307e..3e1a6f607 100644 --- a/example/tutorial_mnist_distributed.py +++ b/example/tutorial_mnist_distributed.py @@ -84,7 +84,8 @@ print_freq=print_freq, X_val=X_val, y_val=y_val, - eval_train=eval_train) + eval_train=eval_train + ) if task_spec.is_master(): # evaluation diff --git a/example/tutorial_mnist_float16.py b/example/tutorial_mnist_float16.py index be77ae620..97ea1cd31 100644 --- a/example/tutorial_mnist_float16.py +++ b/example/tutorial_mnist_float16.py @@ -62,8 +62,10 @@ def model(x, is_train=True, reuse=False): beta2=0.999, # epsilon=1e-08, # for float32 as default epsilon=1e-4, # for float16, see https://stackoverflow.com/questions/42064941/tensorflow-float16-support-is-broken - use_locking=False).minimize( - cost, var_list=train_params) + use_locking=False +).minimize( + cost, var_list=train_params +) # initialize all variables in the session tl.layers.initialize_global_variables(sess) diff --git a/example/tutorial_mnist_simple.py b/example/tutorial_mnist_simple.py index a9c748d5a..d1f25b243 100644 --- a/example/tutorial_mnist_simple.py +++ b/example/tutorial_mnist_simple.py @@ -59,7 +59,8 @@ print_freq=5, X_val=X_val, y_val=y_val, - eval_train=False) + eval_train=False +) # evaluation tl.utils.test(sess, network, acc, X_test, y_test, x, y_, batch_size=None, cost=cost) diff --git a/example/tutorial_mobilenet.py b/example/tutorial_mobilenet.py index e2302f1e1..0e6582bb7 100644 --- a/example/tutorial_mobilenet.py +++ b/example/tutorial_mobilenet.py @@ -13,8 +13,15 @@ import tensorflow as tf import tensorlayer as tl -from tensorlayer.layers import (BatchNormLayer, Conv2d, DepthwiseConv2d, FlattenLayer, GlobalMeanPool2d, InputLayer, - ReshapeLayer) +from tensorlayer.layers import ( + BatchNormLayer, + Conv2d, + DepthwiseConv2d, + FlattenLayer, + GlobalMeanPool2d, + InputLayer, + ReshapeLayer +) def conv_block(n, n_filter, filter_size=(3, 3), strides=(1, 1), is_train=False, name='conv_block'): @@ -43,10 +50,12 @@ def decode_predictions(preds, top=5): # keras.applications.resnet50 if isinstance(preds, np.ndarray) is False: preds = np.asarray(preds) if len(preds.shape) != 2 or preds.shape[1] != 1000: - raise ValueError('`decode_predictions` expects ' - 'a batch of predictions ' - '(i.e. a 2D array of shape (samples, 1000)). ' - 'Found array with shape: ' + str(preds.shape)) + raise ValueError( + '`decode_predictions` expects ' + 'a batch of predictions ' + '(i.e. a 2D array of shape (samples, 1000)). ' + 'Found array with shape: ' + str(preds.shape) + ) with open(fpath) as f: CLASS_INDEX = json.load(f) results = [] diff --git a/example/tutorial_ptb_lstm.py b/example/tutorial_ptb_lstm.py index 33179b743..c0ac93bc0 100644 --- a/example/tutorial_ptb_lstm.py +++ b/example/tutorial_ptb_lstm.py @@ -195,7 +195,12 @@ def inference(x, is_training, num_steps, reuse=None): initializer = tf.random_uniform_initializer(-init_scale, init_scale) with tf.variable_scope("model", reuse=reuse): network = tl.layers.EmbeddingInputlayer( - inputs=x, vocabulary_size=vocab_size, embedding_size=hidden_size, E_init=initializer, name='embedding') + inputs=x, + vocabulary_size=vocab_size, + embedding_size=hidden_size, + E_init=initializer, + name='embedding' + ) network = tl.layers.DropoutLayer(network, keep=keep_prob, is_fix=True, is_train=is_training, name='drop1') network = tl.layers.RNNLayer( network, @@ -205,7 +210,8 @@ def inference(x, is_training, num_steps, reuse=None): initializer=initializer, n_steps=num_steps, return_last=False, - name='basic_lstm_layer1') + name='basic_lstm_layer1' + ) lstm1 = network network = tl.layers.DropoutLayer(network, keep=keep_prob, is_fix=True, is_train=is_training, name='drop2') network = tl.layers.RNNLayer( @@ -217,7 +223,8 @@ def inference(x, is_training, num_steps, reuse=None): n_steps=num_steps, return_last=False, return_seq_2d=True, - name='basic_lstm_layer2') + name='basic_lstm_layer2' + ) lstm2 = network # Alternatively, if return_seq_2d=False, in the above RNN layer, # you can reshape the outputs as follow: @@ -225,7 +232,13 @@ def inference(x, is_training, num_steps, reuse=None): # shape=[-1, int(network.outputs._shape[-1])], name='reshape') network = tl.layers.DropoutLayer(network, keep=keep_prob, is_fix=True, is_train=is_training, name='drop3') network = tl.layers.DenseLayer( - network, n_units=vocab_size, W_init=initializer, b_init=initializer, act=tf.identity, name='output') + network, + n_units=vocab_size, + W_init=initializer, + b_init=initializer, + act=tf.identity, + name='output' + ) return network, lstm1, lstm2 # Inference for Training @@ -301,14 +314,18 @@ def loss_fn(outputs, targets): #, batch_size, num_steps): } # For training, enable dropout feed_dict.update(network.all_drop) - _cost, state1, state2, _ = sess.run( - [cost, lstm1.final_state, lstm2.final_state, train_op], feed_dict=feed_dict) + _cost, state1, state2, _ = sess.run([cost, lstm1.final_state, lstm2.final_state, train_op], + feed_dict=feed_dict) costs += _cost iters += num_steps if step % (epoch_size // 10) == 10: - print("%.3f perplexity: %.3f speed: %.0f wps" % (step * 1.0 / epoch_size, np.exp(costs / iters), - iters * batch_size / (time.time() - start_time))) + print( + "%.3f perplexity: %.3f speed: %.0f wps" % + (step * 1.0 / epoch_size, + np.exp(costs / iters), + iters * batch_size / (time.time() - start_time)) + ) train_perplexity = np.exp(costs / iters) print("Epoch: %d/%d Train Perplexity: %.3f" % (i + 1, max_max_epoch, train_perplexity)) @@ -326,9 +343,9 @@ def loss_fn(outputs, targets): #, batch_size, num_steps): lstm1_val.initial_state: state1, lstm2_val.initial_state: state2, } - _cost, state1, state2, _ = sess.run( - [cost_val, lstm1_val.final_state, lstm2_val.final_state, - tf.no_op()], feed_dict=feed_dict) + _cost, state1, state2, _ = sess.run([cost_val, lstm1_val.final_state, lstm2_val.final_state, + tf.no_op()], + feed_dict=feed_dict) costs += _cost iters += num_steps valid_perplexity = np.exp(costs / iters) @@ -350,8 +367,8 @@ def loss_fn(outputs, targets): #, batch_size, num_steps): lstm1_test.initial_state: state1, lstm2_test.initial_state: state2, } - _cost, state1, state2 = sess.run( - [cost_test, lstm1_test.final_state, lstm2_test.final_state], feed_dict=feed_dict) + _cost, state1, state2 = sess.run([cost_test, lstm1_test.final_state, lstm2_test.final_state], + feed_dict=feed_dict) costs += _cost iters += 1 test_perplexity = np.exp(costs / iters) diff --git a/example/tutorial_ptb_lstm_state_is_tuple.py b/example/tutorial_ptb_lstm_state_is_tuple.py index 42501d366..e0146b421 100644 --- a/example/tutorial_ptb_lstm_state_is_tuple.py +++ b/example/tutorial_ptb_lstm_state_is_tuple.py @@ -195,7 +195,12 @@ def inference(x, is_training, num_steps, reuse=None): initializer = tf.random_uniform_initializer(-init_scale, init_scale) with tf.variable_scope("model", reuse=reuse): network = tl.layers.EmbeddingInputlayer( - inputs=x, vocabulary_size=vocab_size, embedding_size=hidden_size, E_init=initializer, name='embedding') + inputs=x, + vocabulary_size=vocab_size, + embedding_size=hidden_size, + E_init=initializer, + name='embedding' + ) network = tl.layers.DropoutLayer(network, keep=keep_prob, is_fix=True, is_train=is_training, name='drop1') network = tl.layers.RNNLayer( network, @@ -208,7 +213,8 @@ def inference(x, is_training, num_steps, reuse=None): initializer=initializer, n_steps=num_steps, return_last=False, - name='basic_lstm1') + name='basic_lstm1' + ) lstm1 = network network = tl.layers.DropoutLayer(network, keep=keep_prob, is_fix=True, is_train=is_training, name='drop2') network = tl.layers.RNNLayer( @@ -223,7 +229,8 @@ def inference(x, is_training, num_steps, reuse=None): n_steps=num_steps, return_last=False, return_seq_2d=True, - name='basic_lstm2') + name='basic_lstm2' + ) lstm2 = network # Alternatively, if return_seq_2d=False, in the above RNN layer, # you can reshape the outputs as follow: @@ -231,7 +238,13 @@ def inference(x, is_training, num_steps, reuse=None): # shape=[-1, int(network.outputs._shape[-1])], name='reshape') network = tl.layers.DropoutLayer(network, keep=keep_prob, is_fix=True, is_train=is_training, name='drop3') network = tl.layers.DenseLayer( - network, n_units=vocab_size, W_init=initializer, b_init=initializer, act=tf.identity, name='output') + network, + n_units=vocab_size, + W_init=initializer, + b_init=initializer, + act=tf.identity, + name='output' + ) return network, lstm1, lstm2 # Inference for Training @@ -325,8 +338,12 @@ def loss_fn(outputs, targets, batch_size): iters += num_steps if step % (epoch_size // 10) == 10: - print("%.3f perplexity: %.3f speed: %.0f wps" % (step * 1.0 / epoch_size, np.exp(costs / iters), - iters * batch_size / (time.time() - start_time))) + print( + "%.3f perplexity: %.3f speed: %.0f wps" % + (step * 1.0 / epoch_size, + np.exp(costs / iters), + iters * batch_size / (time.time() - start_time)) + ) train_perplexity = np.exp(costs / iters) print("Epoch: %d/%d Train Perplexity: %.3f" % (i + 1, max_max_epoch, train_perplexity)) diff --git a/example/tutorial_squeezenet.py b/example/tutorial_squeezenet.py index d23e684e5..238174633 100644 --- a/example/tutorial_squeezenet.py +++ b/example/tutorial_squeezenet.py @@ -21,10 +21,12 @@ def decode_predictions(preds, top=5): # keras.applications.resnet50 if isinstance(preds, np.ndarray) is False: preds = np.asarray(preds) if len(preds.shape) != 2 or preds.shape[1] != 1000: - raise ValueError('`decode_predictions` expects ' - 'a batch of predictions ' - '(i.e. a 2D array of shape (samples, 1000)). ' - 'Found array with shape: ' + str(preds.shape)) + raise ValueError( + '`decode_predictions` expects ' + 'a batch of predictions ' + '(i.e. a 2D array of shape (samples, 1000)). ' + 'Found array with shape: ' + str(preds.shape) + ) with open(fpath) as f: CLASS_INDEX = json.load(f) results = [] @@ -116,7 +118,8 @@ def squeezenet(x, is_train=True, reuse=False): tl.files.load_and_assign_npz(sess=sess, name='squeezenet.npz', network=n) else: raise Exception( - "please download the pre-trained squeezenet.npz from https://github.com/tensorlayer/pretrained-models") + "please download the pre-trained squeezenet.npz from https://github.com/tensorlayer/pretrained-models" + ) img = tl.vis.read_image('data/tiger.jpeg', '') img = tl.prepro.imresize(img, (224, 224)) diff --git a/example/tutorial_ternaryweight_cifar10_tfrecord.py b/example/tutorial_ternaryweight_cifar10_tfrecord.py index 380fc4efa..1a3cc2228 100644 --- a/example/tutorial_ternaryweight_cifar10_tfrecord.py +++ b/example/tutorial_ternaryweight_cifar10_tfrecord.py @@ -82,7 +82,9 @@ def data_to_tfrecord(images, labels, filename): feature={ "label": tf.train.Feature(int64_list=tf.train.Int64List(value=[label])), 'img_raw': tf.train.Feature(bytes_list=tf.train.BytesList(value=[img_raw])), - })) + } + ) + ) writer.write(example.SerializeToString()) # Serialize To String writer.close() @@ -97,12 +99,13 @@ def read_and_decode(filename, is_train=None): features={ 'label': tf.FixedLenFeature([], tf.int64), 'img_raw': tf.FixedLenFeature([], tf.string), - }) + } + ) # You can do more image distortion here for training data img = tf.decode_raw(features['img_raw'], tf.float32) img = tf.reshape(img, [32, 32, 3]) # img = tf.cast(img, tf.float32) #* (1. / 255) - 0.5 - if is_train == True: + if is_train ==True: # 1. Randomly crop a [height, width] section of the image. img = tf.random_crop(img, [24, 24, 3]) # 2. Randomly flip the image horizontally. @@ -147,11 +150,17 @@ def read_and_decode(filename, is_train=None): x_test_, y_test_ = read_and_decode("test.cifar10", False) x_train_batch, y_train_batch = tf.train.shuffle_batch( - [x_train_, y_train_], batch_size=batch_size, capacity=2000, min_after_dequeue=1000, - num_threads=32) # set the number of threads here + [x_train_, y_train_], + batch_size=batch_size, + capacity=2000, + min_after_dequeue=1000, + num_threads=32 + ) # set the number of threads here # for testing, uses batch instead of shuffle_batch - x_test_batch, y_test_batch = tf.train.batch( - [x_test_, y_test_], batch_size=batch_size, capacity=50000, num_threads=32) + x_test_batch, y_test_batch = tf.train.batch([x_test_, y_test_], + batch_size=batch_size, + capacity=50000, + num_threads=32) def model(x_crop, y_, reuse): """ For more simplified CNN APIs, check tensorlayer.org """ @@ -163,21 +172,56 @@ def model(x_crop, y_, reuse): net = tl.layers.Conv2d(net, 64, (5, 5), (1, 1), act=tf.nn.relu, padding='SAME', W_init=W_init, name='cnn1') net = tl.layers.MaxPool2d(net, (3, 3), (2, 2), padding='SAME', name='pool1') net = tl.layers.LocalResponseNormLayer( - net, depth_radius=4, bias=1.0, alpha=0.001 / 9.0, beta=0.75, name='norm1') + net, + depth_radius=4, + bias=1.0, + alpha=0.001 / 9.0, + beta=0.75, + name='norm1' + ) net = tl.layers.TernaryConv2d( - net, 64, (5, 5), (1, 1), act=tf.nn.relu, padding='SAME', W_init=W_init, name='cnn2') + net, + 64, + (5, 5), + (1, 1), + act=tf.nn.relu, + padding='SAME', + W_init=W_init, + name='cnn2' + ) net = tl.layers.LocalResponseNormLayer( - net, depth_radius=4, bias=1.0, alpha=0.001 / 9.0, beta=0.75, name='norm2') + net, + depth_radius=4, + bias=1.0, + alpha=0.001 / 9.0, + beta=0.75, + name='norm2' + ) net = tl.layers.MaxPool2d(net, (3, 3), (2, 2), padding='SAME', name='pool2') net = tl.layers.FlattenLayer(net, name='flatten') # output: (batch_size, 2304) net = tl.layers.TernaryDenseLayer( - net, n_units=384, act=tf.nn.relu, W_init=W_init2, b_init=b_init2, - name='d1relu') # output: (batch_size, 384) + net, + n_units=384, + act=tf.nn.relu, + W_init=W_init2, + b_init=b_init2, + name='d1relu' + ) # output: (batch_size, 384) net = tl.layers.TernaryDenseLayer( - net, n_units=192, act=tf.nn.relu, W_init=W_init2, b_init=b_init2, - name='d2relu') # output: (batch_size, 192) + net, + n_units=192, + act=tf.nn.relu, + W_init=W_init2, + b_init=b_init2, + name='d2relu' + ) # output: (batch_size, 192) net = tl.layers.DenseLayer( - net, n_units=10, act=tf.identity, W_init=W_init2, name='output') # output: (batch_size, 10) + net, + n_units=10, + act=tf.identity, + W_init=W_init2, + name='output' + ) # output: (batch_size, 10) y = net.outputs ce = tl.cost.cross_entropy(y, y_, name='cost') @@ -208,13 +252,28 @@ def model_batch_norm(x_crop, y_, reuse, is_train): net = tl.layers.MaxPool2d(net, (3, 3), (2, 2), padding='SAME', name='pool2') net = tl.layers.FlattenLayer(net, name='flatten') # output: (batch_size, 2304) net = tl.layers.DenseLayer( - net, n_units=384, act=tf.nn.relu, W_init=W_init2, b_init=b_init2, - name='d1relu') # output: (batch_size, 384) + net, + n_units=384, + act=tf.nn.relu, + W_init=W_init2, + b_init=b_init2, + name='d1relu' + ) # output: (batch_size, 384) net = tl.layers.DenseLayer( - net, n_units=192, act=tf.nn.relu, W_init=W_init2, b_init=b_init2, - name='d2relu') # output: (batch_size, 192) + net, + n_units=192, + act=tf.nn.relu, + W_init=W_init2, + b_init=b_init2, + name='d2relu' + ) # output: (batch_size, 192) net = tl.layers.DenseLayer( - net, n_units=10, act=tf.identity, W_init=W_init2, name='output') # output: (batch_size, 10) + net, + n_units=10, + act=tf.identity, + W_init=W_init2, + name='output' + ) # output: (batch_size, 10) y = net.outputs ce = tl.cost.cross_entropy(y, y_, name='cost') # L2 for the MLP, without this, the accuracy will be reduced by 15%. @@ -283,8 +342,14 @@ def model_batch_norm(x_crop, y_, reuse, is_train): n_batch += 1 if epoch + 1 == 1 or (epoch + 1) % print_freq == 0: - print("Epoch %d : Step %d-%d of %d took %fs" % (epoch, step, step + n_step_epoch, n_step, - time.time() - start_time)) + print( + "Epoch %d : Step %d-%d of %d took %fs" % + (epoch, + step, + step + n_step_epoch, + n_step, + time.time() - start_time) + ) print(" train loss: %f" % (train_loss / n_batch)) print(" train acc: %f" % (train_acc / n_batch)) diff --git a/example/tutorial_tf_dataset_voc.py b/example/tutorial_tf_dataset_voc.py index 6e752580d..61e0b6734 100644 --- a/example/tutorial_tf_dataset_voc.py +++ b/example/tutorial_tf_dataset_voc.py @@ -51,9 +51,22 @@ def _data_aug_fn(im, ann): tmp0 = random.randint(1, int(im_size[0] * jitter)) tmp1 = random.randint(1, int(im_size[1] * jitter)) im, coords = tl.prepro.obj_box_imresize( - im, coords, [im_size[0] + tmp0, im_size[1] + tmp1], is_rescale=True, interp='bicubic') + im, + coords, + [im_size[0] + tmp0, im_size[1] + tmp1], + is_rescale=True, + interp='bicubic' + ) im, clas, coords = tl.prepro.obj_box_crop( - im, clas, coords, wrg=im_size[1], hrg=im_size[0], is_rescale=True, is_center=True, is_random=True) + im, + clas, + coords, + wrg=im_size[1], + hrg=im_size[0], + is_rescale=True, + is_center=True, + is_random=True + ) ## value [0, 255] to [-1, 1] (optional) # im = im / 127.5 - 1 ## value [0, 255] to [0, 1] (optional) @@ -100,4 +113,11 @@ def _map_fn(filename, annotation): ## save all images for i in range(len(im)): tl.vis.draw_boxes_and_labels_to_image( - im[i] * 255, ann[i][0], ann[i][1], [], classes, True, save_name='_bbox_vis_%d.png' % i) + im[i] * 255, + ann[i][0], + ann[i][1], + [], + classes, + True, + save_name='_bbox_vis_%d.png' % i + ) diff --git a/example/tutorial_tfrecord.py b/example/tutorial_tfrecord.py index c83dc0b1d..5e490a26a 100644 --- a/example/tutorial_tfrecord.py +++ b/example/tutorial_tfrecord.py @@ -81,7 +81,8 @@ def read_and_decode(filename): features={ 'label': tf.FixedLenFeature([], tf.int64), 'img_raw': tf.FixedLenFeature([], tf.string), - }) + } + ) # You can do more image distortion here for training data img = tf.decode_raw(features['img_raw'], tf.uint8) img = tf.reshape(img, [224, 224, 3]) @@ -95,7 +96,12 @@ def read_and_decode(filename): ## Use shuffle_batch or batch # see https://www.tensorflow.org/versions/master/api_docs/python/io_ops.html#shuffle_batch img_batch, label_batch = tf.train.shuffle_batch( - [img, label], batch_size=4, capacity=2000, min_after_dequeue=1000, num_threads=16) + [img, label], + batch_size=4, + capacity=2000, + min_after_dequeue=1000, + num_threads=16 +) print("img_batch : %s" % img_batch._shape) print("label_batch : %s" % label_batch._shape) # init = tf.initialize_all_variables() diff --git a/example/tutorial_tfrecord2.py b/example/tutorial_tfrecord2.py index 944d995cb..e30864ce8 100755 --- a/example/tutorial_tfrecord2.py +++ b/example/tutorial_tfrecord2.py @@ -52,7 +52,9 @@ feature={ "label": tf.train.Feature(int64_list=tf.train.Int64List(value=[label])), 'img_raw': tf.train.Feature(bytes_list=tf.train.BytesList(value=[img_raw])), - })) + } + ) + ) writer.write(example.SerializeToString()) # Serialize To String writer.close() @@ -67,7 +69,8 @@ def read_and_decode(filename): features={ 'label': tf.FixedLenFeature([], tf.int64), 'img_raw': tf.FixedLenFeature([], tf.string), - }) + } + ) # You can do more image distortion here for training data img = tf.decode_raw(features['img_raw'], tf.float32) img = tf.reshape(img, [32, 32, 3]) @@ -81,7 +84,12 @@ def read_and_decode(filename): ## Use shuffle_batch or batch # see https://www.tensorflow.org/versions/master/api_docs/python/io_ops.html#shuffle_batch img_batch, label_batch = tf.train.shuffle_batch( - [img, label], batch_size=4, capacity=50000, min_after_dequeue=10000, num_threads=1) + [img, label], + batch_size=4, + capacity=50000, + min_after_dequeue=10000, + num_threads=1 +) print("img_batch : %s" % img_batch._shape) print("label_batch : %s" % label_batch._shape) diff --git a/example/tutorial_tfrecord3.py b/example/tutorial_tfrecord3.py index 2759e86e7..3223db6e0 100644 --- a/example/tutorial_tfrecord3.py +++ b/example/tutorial_tfrecord3.py @@ -112,7 +112,8 @@ def _bytes_feature_list(values): sequence_features={ "image/caption": tf.FixedLenSequenceFeature([], dtype=tf.string), "image/caption_ids": tf.FixedLenSequenceFeature([], dtype=tf.int64), - }) + } +) c = tf.contrib.learn.run_n(features, n=1, feed_dict=None) im = Image.frombytes('RGB', (299, 299), c[0]['image/img_raw']) tl.visualize.frame(np.asarray(im), second=1, saveable=False, name='frame', fig_idx=1236) @@ -231,15 +232,17 @@ def distort_image(image, thread_id): # return image -def prefetch_input_data(reader, - file_pattern, - is_training, - batch_size, - values_per_shard, - input_queue_capacity_factor=16, - num_reader_threads=1, - shard_queue_name="filename_queue", - value_queue_name="input_queue"): +def prefetch_input_data( + reader, + file_pattern, + is_training, + batch_size, + values_per_shard, + input_queue_capacity_factor=16, + num_reader_threads=1, + shard_queue_name="filename_queue", + value_queue_name="input_queue" +): """Prefetches string values from disk into an input queue. In training the capacity of the queue is important because a larger queue @@ -281,7 +284,8 @@ def prefetch_input_data(reader, capacity=capacity, min_after_dequeue=min_queue_examples, dtypes=[tf.string], - name="random_" + value_queue_name) + name="random_" + value_queue_name + ) else: print(" is_training == False : FIFOQueue") filename_queue = tf.train.string_input_producer(data_files, shuffle=False, capacity=1, name=shard_queue_name) @@ -294,8 +298,10 @@ def prefetch_input_data(reader, enqueue_ops.append(values_queue.enqueue([value])) tf.train.queue_runner.add_queue_runner(tf.train.queue_runner.QueueRunner(values_queue, enqueue_ops)) - tf.summary.scalar("queue/%s/fraction_of_%d_full" % (values_queue.name, capacity), - tf.cast(values_queue.size(), tf.float32) * (1. / capacity)) + tf.summary.scalar( + "queue/%s/fraction_of_%d_full" % (values_queue.name, capacity), + tf.cast(values_queue.size(), tf.float32) * (1. / capacity) + ) return values_queue @@ -322,7 +328,8 @@ def prefetch_input_data(reader, sequence_features={ "image/caption": tf.FixedLenSequenceFeature([], dtype=tf.string), "image/caption_ids": tf.FixedLenSequenceFeature([], dtype=tf.int64), - }) + } +) img = tf.decode_raw(context["image/img_raw"], tf.uint8) img = tf.reshape(img, [height, width, 3]) @@ -334,7 +341,11 @@ def prefetch_input_data(reader, except Exception: # for TensorFlow 0.10 img = tf.image.resize_images( - img, new_height=resize_height, new_width=resize_width, method=tf.image.ResizeMethod.BILINEAR) + img, + new_height=resize_height, + new_width=resize_width, + method=tf.image.ResizeMethod.BILINEAR + ) # Crop to final dimensions. if is_training: img = tf.random_crop(img, [height, width, 3]) @@ -354,7 +365,8 @@ def prefetch_input_data(reader, batch_size=4, capacity=50000, dynamic_pad=True, # string list pad with '', int list pad with 0 - num_threads=4) + num_threads=4 +) sess = tf.Session() # sess.run(tf.initialize_all_variables()) tl.layers.initialize_global_variables(sess) @@ -436,7 +448,12 @@ def batch_with_dynamic_pad(images_and_captions, batch_size, queue_capacity, add_ enqueue_list.append([image, input_seq, target_seq, indicator]) images, input_seqs, target_seqs, mask = tf.train.batch_join( - enqueue_list, batch_size=batch_size, capacity=queue_capacity, dynamic_pad=True, name="batch_and_pad") + enqueue_list, + batch_size=batch_size, + capacity=queue_capacity, + dynamic_pad=True, + name="batch_and_pad" + ) if add_summaries: lengths = tf.add(tf.reduce_sum(mask, 1), 1) @@ -447,8 +464,11 @@ def batch_with_dynamic_pad(images_and_captions, batch_size, queue_capacity, add_ return images, input_seqs, target_seqs, mask -images, input_seqs, target_seqs, input_mask = (batch_with_dynamic_pad( - images_and_captions=[[img, img_cap]], batch_size=4, queue_capacity=50000)) +images, input_seqs, target_seqs, input_mask = ( + batch_with_dynamic_pad(images_and_captions=[[img, img_cap]], + batch_size=4, + queue_capacity=50000) +) sess = tf.Session() sess.run(tf.initialize_all_variables()) coord = tf.train.Coordinator() diff --git a/example/tutorial_vgg16.py b/example/tutorial_vgg16.py index 29305c75c..0b5c77469 100644 --- a/example/tutorial_vgg16.py +++ b/example/tutorial_vgg16.py @@ -46,7 +46,8 @@ from data.imagenet_classes import * except Exception as e: raise Exception( - "{} / download the file from: https://github.com/zsdonghao/tensorlayer/tree/master/example/data".format(e)) + "{} / download the file from: https://github.com/zsdonghao/tensorlayer/tree/master/example/data".format(e) + ) def conv_layers(net_in): @@ -64,16 +65,24 @@ def conv_layers(net_in): shape=[3, 3, 3, 64], # 64 features for each 3x3 patch strides=[1, 1, 1, 1], padding='SAME', - name='conv1_1') + name='conv1_1' + ) network = Conv2dLayer( network, act=tf.nn.relu, shape=[3, 3, 64, 64], # 64 features for each 3x3 patch strides=[1, 1, 1, 1], padding='SAME', - name='conv1_2') + name='conv1_2' + ) network = PoolLayer( - network, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', pool=tf.nn.max_pool, name='pool1') + network, + ksize=[1, 2, 2, 1], + strides=[1, 2, 2, 1], + padding='SAME', + pool=tf.nn.max_pool, + name='pool1' + ) # conv2 network = Conv2dLayer( @@ -82,16 +91,24 @@ def conv_layers(net_in): shape=[3, 3, 64, 128], # 128 features for each 3x3 patch strides=[1, 1, 1, 1], padding='SAME', - name='conv2_1') + name='conv2_1' + ) network = Conv2dLayer( network, act=tf.nn.relu, shape=[3, 3, 128, 128], # 128 features for each 3x3 patch strides=[1, 1, 1, 1], padding='SAME', - name='conv2_2') + name='conv2_2' + ) network = PoolLayer( - network, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', pool=tf.nn.max_pool, name='pool2') + network, + ksize=[1, 2, 2, 1], + strides=[1, 2, 2, 1], + padding='SAME', + pool=tf.nn.max_pool, + name='pool2' + ) # conv3 network = Conv2dLayer( @@ -100,23 +117,32 @@ def conv_layers(net_in): shape=[3, 3, 128, 256], # 256 features for each 3x3 patch strides=[1, 1, 1, 1], padding='SAME', - name='conv3_1') + name='conv3_1' + ) network = Conv2dLayer( network, act=tf.nn.relu, shape=[3, 3, 256, 256], # 256 features for each 3x3 patch strides=[1, 1, 1, 1], padding='SAME', - name='conv3_2') + name='conv3_2' + ) network = Conv2dLayer( network, act=tf.nn.relu, shape=[3, 3, 256, 256], # 256 features for each 3x3 patch strides=[1, 1, 1, 1], padding='SAME', - name='conv3_3') + name='conv3_3' + ) network = PoolLayer( - network, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', pool=tf.nn.max_pool, name='pool3') + network, + ksize=[1, 2, 2, 1], + strides=[1, 2, 2, 1], + padding='SAME', + pool=tf.nn.max_pool, + name='pool3' + ) # conv4 network = Conv2dLayer( @@ -125,23 +151,32 @@ def conv_layers(net_in): shape=[3, 3, 256, 512], # 512 features for each 3x3 patch strides=[1, 1, 1, 1], padding='SAME', - name='conv4_1') + name='conv4_1' + ) network = Conv2dLayer( network, act=tf.nn.relu, shape=[3, 3, 512, 512], # 512 features for each 3x3 patch strides=[1, 1, 1, 1], padding='SAME', - name='conv4_2') + name='conv4_2' + ) network = Conv2dLayer( network, act=tf.nn.relu, shape=[3, 3, 512, 512], # 512 features for each 3x3 patch strides=[1, 1, 1, 1], padding='SAME', - name='conv4_3') + name='conv4_3' + ) network = PoolLayer( - network, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', pool=tf.nn.max_pool, name='pool4') + network, + ksize=[1, 2, 2, 1], + strides=[1, 2, 2, 1], + padding='SAME', + pool=tf.nn.max_pool, + name='pool4' + ) # conv5 network = Conv2dLayer( @@ -150,23 +185,32 @@ def conv_layers(net_in): shape=[3, 3, 512, 512], # 512 features for each 3x3 patch strides=[1, 1, 1, 1], padding='SAME', - name='conv5_1') + name='conv5_1' + ) network = Conv2dLayer( network, act=tf.nn.relu, shape=[3, 3, 512, 512], # 512 features for each 3x3 patch strides=[1, 1, 1, 1], padding='SAME', - name='conv5_2') + name='conv5_2' + ) network = Conv2dLayer( network, act=tf.nn.relu, shape=[3, 3, 512, 512], # 512 features for each 3x3 patch strides=[1, 1, 1, 1], padding='SAME', - name='conv5_3') + name='conv5_3' + ) network = PoolLayer( - network, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', pool=tf.nn.max_pool, name='pool5') + network, + ksize=[1, 2, 2, 1], + strides=[1, 2, 2, 1], + padding='SAME', + pool=tf.nn.max_pool, + name='pool5' + ) return network @@ -180,43 +224,134 @@ def conv_layers_simple_api(net_in): # conv1 network = Conv2d( - net_in, n_filter=64, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv1_1') + net_in, + n_filter=64, + filter_size=(3, 3), + strides=(1, 1), + act=tf.nn.relu, + padding='SAME', + name='conv1_1' + ) network = Conv2d( - network, n_filter=64, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv1_2') + network, + n_filter=64, + filter_size=(3, 3), + strides=(1, 1), + act=tf.nn.relu, + padding='SAME', + name='conv1_2' + ) network = MaxPool2d(network, filter_size=(2, 2), strides=(2, 2), padding='SAME', name='pool1') # conv2 network = Conv2d( - network, n_filter=128, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv2_1') + network, + n_filter=128, + filter_size=(3, 3), + strides=(1, 1), + act=tf.nn.relu, + padding='SAME', + name='conv2_1' + ) network = Conv2d( - network, n_filter=128, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv2_2') + network, + n_filter=128, + filter_size=(3, 3), + strides=(1, 1), + act=tf.nn.relu, + padding='SAME', + name='conv2_2' + ) network = MaxPool2d(network, filter_size=(2, 2), strides=(2, 2), padding='SAME', name='pool2') # conv3 network = Conv2d( - network, n_filter=256, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv3_1') + network, + n_filter=256, + filter_size=(3, 3), + strides=(1, 1), + act=tf.nn.relu, + padding='SAME', + name='conv3_1' + ) network = Conv2d( - network, n_filter=256, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv3_2') + network, + n_filter=256, + filter_size=(3, 3), + strides=(1, 1), + act=tf.nn.relu, + padding='SAME', + name='conv3_2' + ) network = Conv2d( - network, n_filter=256, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv3_3') + network, + n_filter=256, + filter_size=(3, 3), + strides=(1, 1), + act=tf.nn.relu, + padding='SAME', + name='conv3_3' + ) network = MaxPool2d(network, filter_size=(2, 2), strides=(2, 2), padding='SAME', name='pool3') # conv4 network = Conv2d( - network, n_filter=512, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv4_1') + network, + n_filter=512, + filter_size=(3, 3), + strides=(1, 1), + act=tf.nn.relu, + padding='SAME', + name='conv4_1' + ) network = Conv2d( - network, n_filter=512, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv4_2') + network, + n_filter=512, + filter_size=(3, 3), + strides=(1, 1), + act=tf.nn.relu, + padding='SAME', + name='conv4_2' + ) network = Conv2d( - network, n_filter=512, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv4_3') + network, + n_filter=512, + filter_size=(3, 3), + strides=(1, 1), + act=tf.nn.relu, + padding='SAME', + name='conv4_3' + ) network = MaxPool2d(network, filter_size=(2, 2), strides=(2, 2), padding='SAME', name='pool4') # conv5 network = Conv2d( - network, n_filter=512, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv5_1') + network, + n_filter=512, + filter_size=(3, 3), + strides=(1, 1), + act=tf.nn.relu, + padding='SAME', + name='conv5_1' + ) network = Conv2d( - network, n_filter=512, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv5_2') + network, + n_filter=512, + filter_size=(3, 3), + strides=(1, 1), + act=tf.nn.relu, + padding='SAME', + name='conv5_2' + ) network = Conv2d( - network, n_filter=512, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv5_3') + network, + n_filter=512, + filter_size=(3, 3), + strides=(1, 1), + act=tf.nn.relu, + padding='SAME', + name='conv5_3' + ) network = MaxPool2d(network, filter_size=(2, 2), strides=(2, 2), padding='SAME', name='pool5') return network diff --git a/example/tutorial_vgg19.py b/example/tutorial_vgg19.py index 0850f9e8a..b9c098b8a 100755 --- a/example/tutorial_vgg19.py +++ b/example/tutorial_vgg19.py @@ -28,7 +28,8 @@ from data.imagenet_classes import * except Exception as e: raise Exception( - "{} / download the file from: https://github.com/zsdonghao/tensorlayer/tree/master/example/data".format(e)) + "{} / download the file from: https://github.com/zsdonghao/tensorlayer/tree/master/example/data".format(e) + ) VGG_MEAN = [103.939, 116.779, 123.68] @@ -93,63 +94,188 @@ def Vgg19(rgb): red - VGG_MEAN[2], ]) else: - bgr = tf.concat( - [ - blue - VGG_MEAN[0], - green - VGG_MEAN[1], - red - VGG_MEAN[2], - ], axis=3) + bgr = tf.concat([ + blue - VGG_MEAN[0], + green - VGG_MEAN[1], + red - VGG_MEAN[2], + ], axis=3) if bgr.get_shape().as_list()[1:] != [224, 224, 3]: raise Exception("image size unmatch") # input layer net_in = InputLayer(bgr, name='input') # conv1 network = Conv2dLayer( - net_in, act=tf.nn.relu, shape=[3, 3, 3, 64], strides=[1, 1, 1, 1], padding='SAME', name='conv1_1') + net_in, + act=tf.nn.relu, + shape=[3, 3, 3, 64], + strides=[1, 1, 1, 1], + padding='SAME', + name='conv1_1' + ) network = Conv2dLayer( - network, act=tf.nn.relu, shape=[3, 3, 64, 64], strides=[1, 1, 1, 1], padding='SAME', name='conv1_2') + network, + act=tf.nn.relu, + shape=[3, 3, 64, 64], + strides=[1, 1, 1, 1], + padding='SAME', + name='conv1_2' + ) network = PoolLayer( - network, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', pool=tf.nn.max_pool, name='pool1') + network, + ksize=[1, 2, 2, 1], + strides=[1, 2, 2, 1], + padding='SAME', + pool=tf.nn.max_pool, + name='pool1' + ) # conv2 network = Conv2dLayer( - network, act=tf.nn.relu, shape=[3, 3, 64, 128], strides=[1, 1, 1, 1], padding='SAME', name='conv2_1') + network, + act=tf.nn.relu, + shape=[3, 3, 64, 128], + strides=[1, 1, 1, 1], + padding='SAME', + name='conv2_1' + ) network = Conv2dLayer( - network, act=tf.nn.relu, shape=[3, 3, 128, 128], strides=[1, 1, 1, 1], padding='SAME', name='conv2_2') + network, + act=tf.nn.relu, + shape=[3, 3, 128, 128], + strides=[1, 1, 1, 1], + padding='SAME', + name='conv2_2' + ) network = PoolLayer( - network, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', pool=tf.nn.max_pool, name='pool2') + network, + ksize=[1, 2, 2, 1], + strides=[1, 2, 2, 1], + padding='SAME', + pool=tf.nn.max_pool, + name='pool2' + ) # conv3 network = Conv2dLayer( - network, act=tf.nn.relu, shape=[3, 3, 128, 256], strides=[1, 1, 1, 1], padding='SAME', name='conv3_1') + network, + act=tf.nn.relu, + shape=[3, 3, 128, 256], + strides=[1, 1, 1, 1], + padding='SAME', + name='conv3_1' + ) network = Conv2dLayer( - network, act=tf.nn.relu, shape=[3, 3, 256, 256], strides=[1, 1, 1, 1], padding='SAME', name='conv3_2') + network, + act=tf.nn.relu, + shape=[3, 3, 256, 256], + strides=[1, 1, 1, 1], + padding='SAME', + name='conv3_2' + ) network = Conv2dLayer( - network, act=tf.nn.relu, shape=[3, 3, 256, 256], strides=[1, 1, 1, 1], padding='SAME', name='conv3_3') + network, + act=tf.nn.relu, + shape=[3, 3, 256, 256], + strides=[1, 1, 1, 1], + padding='SAME', + name='conv3_3' + ) network = Conv2dLayer( - network, act=tf.nn.relu, shape=[3, 3, 256, 256], strides=[1, 1, 1, 1], padding='SAME', name='conv3_4') + network, + act=tf.nn.relu, + shape=[3, 3, 256, 256], + strides=[1, 1, 1, 1], + padding='SAME', + name='conv3_4' + ) network = PoolLayer( - network, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', pool=tf.nn.max_pool, name='pool3') + network, + ksize=[1, 2, 2, 1], + strides=[1, 2, 2, 1], + padding='SAME', + pool=tf.nn.max_pool, + name='pool3' + ) # conv4 network = Conv2dLayer( - network, act=tf.nn.relu, shape=[3, 3, 256, 512], strides=[1, 1, 1, 1], padding='SAME', name='conv4_1') + network, + act=tf.nn.relu, + shape=[3, 3, 256, 512], + strides=[1, 1, 1, 1], + padding='SAME', + name='conv4_1' + ) network = Conv2dLayer( - network, act=tf.nn.relu, shape=[3, 3, 512, 512], strides=[1, 1, 1, 1], padding='SAME', name='conv4_2') + network, + act=tf.nn.relu, + shape=[3, 3, 512, 512], + strides=[1, 1, 1, 1], + padding='SAME', + name='conv4_2' + ) network = Conv2dLayer( - network, act=tf.nn.relu, shape=[3, 3, 512, 512], strides=[1, 1, 1, 1], padding='SAME', name='conv4_3') + network, + act=tf.nn.relu, + shape=[3, 3, 512, 512], + strides=[1, 1, 1, 1], + padding='SAME', + name='conv4_3' + ) network = Conv2dLayer( - network, act=tf.nn.relu, shape=[3, 3, 512, 512], strides=[1, 1, 1, 1], padding='SAME', name='conv4_4') + network, + act=tf.nn.relu, + shape=[3, 3, 512, 512], + strides=[1, 1, 1, 1], + padding='SAME', + name='conv4_4' + ) network = PoolLayer( - network, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', pool=tf.nn.max_pool, name='pool4') + network, + ksize=[1, 2, 2, 1], + strides=[1, 2, 2, 1], + padding='SAME', + pool=tf.nn.max_pool, + name='pool4' + ) # conv5 network = Conv2dLayer( - network, act=tf.nn.relu, shape=[3, 3, 512, 512], strides=[1, 1, 1, 1], padding='SAME', name='conv5_1') + network, + act=tf.nn.relu, + shape=[3, 3, 512, 512], + strides=[1, 1, 1, 1], + padding='SAME', + name='conv5_1' + ) network = Conv2dLayer( - network, act=tf.nn.relu, shape=[3, 3, 512, 512], strides=[1, 1, 1, 1], padding='SAME', name='conv5_2') + network, + act=tf.nn.relu, + shape=[3, 3, 512, 512], + strides=[1, 1, 1, 1], + padding='SAME', + name='conv5_2' + ) network = Conv2dLayer( - network, act=tf.nn.relu, shape=[3, 3, 512, 512], strides=[1, 1, 1, 1], padding='SAME', name='conv5_3') + network, + act=tf.nn.relu, + shape=[3, 3, 512, 512], + strides=[1, 1, 1, 1], + padding='SAME', + name='conv5_3' + ) network = Conv2dLayer( - network, act=tf.nn.relu, shape=[3, 3, 512, 512], strides=[1, 1, 1, 1], padding='SAME', name='conv5_4') + network, + act=tf.nn.relu, + shape=[3, 3, 512, 512], + strides=[1, 1, 1, 1], + padding='SAME', + name='conv5_4' + ) network = PoolLayer( - network, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', pool=tf.nn.max_pool, name='pool5') + network, + ksize=[1, 2, 2, 1], + strides=[1, 2, 2, 1], + padding='SAME', + pool=tf.nn.max_pool, + name='pool5' + ) # fc 6~8 network = FlattenLayer(network, name='flatten') network = DenseLayer(network, n_units=4096, act=tf.nn.relu, name='fc6') @@ -189,57 +315,168 @@ def Vgg19_simple_api(rgb): red - VGG_MEAN[2], ]) else: - bgr = tf.concat( - [ - blue - VGG_MEAN[0], - green - VGG_MEAN[1], - red - VGG_MEAN[2], - ], axis=3) + bgr = tf.concat([ + blue - VGG_MEAN[0], + green - VGG_MEAN[1], + red - VGG_MEAN[2], + ], axis=3) if bgr.get_shape().as_list()[1:] != [224, 224, 3]: raise Exception("image size unmatch") # input layer net_in = InputLayer(bgr, name='input') # conv1 network = Conv2d( - net_in, n_filter=64, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv1_1') + net_in, + n_filter=64, + filter_size=(3, 3), + strides=(1, 1), + act=tf.nn.relu, + padding='SAME', + name='conv1_1' + ) network = Conv2d( - network, n_filter=64, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv1_2') + network, + n_filter=64, + filter_size=(3, 3), + strides=(1, 1), + act=tf.nn.relu, + padding='SAME', + name='conv1_2' + ) network = MaxPool2d(network, filter_size=(2, 2), strides=(2, 2), padding='SAME', name='pool1') # conv2 network = Conv2d( - network, n_filter=128, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv2_1') + network, + n_filter=128, + filter_size=(3, 3), + strides=(1, 1), + act=tf.nn.relu, + padding='SAME', + name='conv2_1' + ) network = Conv2d( - network, n_filter=128, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv2_2') + network, + n_filter=128, + filter_size=(3, 3), + strides=(1, 1), + act=tf.nn.relu, + padding='SAME', + name='conv2_2' + ) network = MaxPool2d(network, filter_size=(2, 2), strides=(2, 2), padding='SAME', name='pool2') # conv3 network = Conv2d( - network, n_filter=256, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv3_1') + network, + n_filter=256, + filter_size=(3, 3), + strides=(1, 1), + act=tf.nn.relu, + padding='SAME', + name='conv3_1' + ) network = Conv2d( - network, n_filter=256, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv3_2') + network, + n_filter=256, + filter_size=(3, 3), + strides=(1, 1), + act=tf.nn.relu, + padding='SAME', + name='conv3_2' + ) network = Conv2d( - network, n_filter=256, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv3_3') + network, + n_filter=256, + filter_size=(3, 3), + strides=(1, 1), + act=tf.nn.relu, + padding='SAME', + name='conv3_3' + ) network = Conv2d( - network, n_filter=256, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv3_4') + network, + n_filter=256, + filter_size=(3, 3), + strides=(1, 1), + act=tf.nn.relu, + padding='SAME', + name='conv3_4' + ) network = MaxPool2d(network, filter_size=(2, 2), strides=(2, 2), padding='SAME', name='pool3') # conv4 network = Conv2d( - network, n_filter=512, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv4_1') + network, + n_filter=512, + filter_size=(3, 3), + strides=(1, 1), + act=tf.nn.relu, + padding='SAME', + name='conv4_1' + ) network = Conv2d( - network, n_filter=512, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv4_2') + network, + n_filter=512, + filter_size=(3, 3), + strides=(1, 1), + act=tf.nn.relu, + padding='SAME', + name='conv4_2' + ) network = Conv2d( - network, n_filter=512, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv4_3') + network, + n_filter=512, + filter_size=(3, 3), + strides=(1, 1), + act=tf.nn.relu, + padding='SAME', + name='conv4_3' + ) network = Conv2d( - network, n_filter=512, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv4_4') + network, + n_filter=512, + filter_size=(3, 3), + strides=(1, 1), + act=tf.nn.relu, + padding='SAME', + name='conv4_4' + ) network = MaxPool2d(network, filter_size=(2, 2), strides=(2, 2), padding='SAME', name='pool4') # conv5 network = Conv2d( - network, n_filter=512, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv5_1') + network, + n_filter=512, + filter_size=(3, 3), + strides=(1, 1), + act=tf.nn.relu, + padding='SAME', + name='conv5_1' + ) network = Conv2d( - network, n_filter=512, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv5_2') + network, + n_filter=512, + filter_size=(3, 3), + strides=(1, 1), + act=tf.nn.relu, + padding='SAME', + name='conv5_2' + ) network = Conv2d( - network, n_filter=512, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv5_3') + network, + n_filter=512, + filter_size=(3, 3), + strides=(1, 1), + act=tf.nn.relu, + padding='SAME', + name='conv5_3' + ) network = Conv2d( - network, n_filter=512, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv5_4') + network, + n_filter=512, + filter_size=(3, 3), + strides=(1, 1), + act=tf.nn.relu, + padding='SAME', + name='conv5_4' + ) network = MaxPool2d(network, filter_size=(2, 2), strides=(2, 2), padding='SAME', name='pool5') # fc 6~8 network = FlattenLayer(network, name='flatten') diff --git a/example/tutorial_word2vec_basic.py b/example/tutorial_word2vec_basic.py index 6258814db..8e54a8bff 100644 --- a/example/tutorial_word2vec_basic.py +++ b/example/tutorial_word2vec_basic.py @@ -139,7 +139,9 @@ def main_word2vec_basic(): print('Most 5 common words (+UNK)', count[:5]) # [['UNK', 418391], (b'the', 1061396), (b'of', 593677), (b'and', 416629), (b'one', 411764)] print( - 'Sample data', data[:10], [reverse_dictionary[i] for i in data[:10]] + 'Sample data', + data[:10], + [reverse_dictionary[i] for i in data[:10]] ) # [5243, 3081, 12, 6, 195, 2, 3135, 46, 59, 156] [b'anarchism', b'originated', b'as', b'a', b'term', b'of', b'abuse', b'first', b'used', b'against'] del words # Hint to reduce memory. @@ -148,12 +150,22 @@ def main_word2vec_basic(): print() batch, labels, data_index = tl.nlp.generate_skip_gram_batch( - data=data, batch_size=8, num_skips=4, skip_window=2, data_index=0) + data=data, + batch_size=8, + num_skips=4, + skip_window=2, + data_index=0 + ) for i in range(8): print(batch[i], reverse_dictionary[batch[i]], '->', labels[i, 0], reverse_dictionary[labels[i, 0]]) batch, labels, data_index = tl.nlp.generate_skip_gram_batch( - data=data, batch_size=8, num_skips=2, skip_window=1, data_index=0) + data=data, + batch_size=8, + num_skips=2, + skip_window=1, + data_index=0 + ) for i in range(8): print(batch[i], reverse_dictionary[batch[i]], '->', labels[i, 0], reverse_dictionary[labels[i, 0]]) @@ -199,8 +211,12 @@ def main_word2vec_basic(): train_params = emb_net.all_params # train_op = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost, var_list=train_params) train_op = tf.train.AdagradOptimizer( - learning_rate, initial_accumulator_value=0.1, use_locking=False).minimize( - cost, var_list=train_params) + learning_rate, + initial_accumulator_value=0.1, + use_locking=False + ).minimize( + cost, var_list=train_params + ) # Compute the cosine similarity between minibatch examples and all embeddings. # For simple visualization of validation set. @@ -234,7 +250,12 @@ def main_word2vec_basic(): while step < num_steps: start_time = time.time() batch_inputs, batch_labels, data_index = tl.nlp.generate_skip_gram_batch( - data=data, batch_size=batch_size, num_skips=num_skips, skip_window=skip_window, data_index=data_index) + data=data, + batch_size=batch_size, + num_skips=num_skips, + skip_window=skip_window, + data_index=data_index + ) feed_dict = {train_inputs: batch_inputs, train_labels: batch_labels} # We perform one update step by evaluating the train_op (including it # in the list of returned values for sess.run() @@ -244,8 +265,13 @@ def main_word2vec_basic(): if step % print_freq == 0: if step > 0: average_loss /= print_freq - print("Average loss at step %d/%d. loss:%f took:%fs" % (step, num_steps, average_loss, - time.time() - start_time)) + print( + "Average loss at step %d/%d. loss:%f took:%fs" % + (step, + num_steps, + average_loss, + time.time() - start_time) + ) average_loss = 0 # Prints out nearby words given a list of words. # Note that this is expensive (~20% slowdown if computed every 500 steps) @@ -274,7 +300,8 @@ def main_word2vec_basic(): 'dictionary': dictionary, 'reverse_dictionary': reverse_dictionary }, - name=model_file_name + '.npy') + name=model_file_name + '.npy' + ) # if step == num_steps-1: # keeptrain = input("Training %d finished enter 1 to keep training: " % num_steps) @@ -289,7 +316,13 @@ def main_word2vec_basic(): final_embeddings = sess.run(normalized_embeddings) #.eval() tl.visualize.tsne_embedding( - final_embeddings, reverse_dictionary, plot_only=500, second=5, saveable=False, name='word2vec_basic') + final_embeddings, + reverse_dictionary, + plot_only=500, + second=5, + saveable=False, + name='word2vec_basic' + ) # Step 7: Evaluate by analogy questions. see tensorflow/models/embedding/word2vec_optimized.py print() diff --git a/tensorlayer/cost.py b/tensorlayer/cost.py index ed8f3c47c..fe9810199 100644 --- a/tensorlayer/cost.py +++ b/tensorlayer/cost.py @@ -100,7 +100,8 @@ def binary_cross_entropy(output, target, epsilon=1e-8, name='bce_loss'): # target = ops.convert_to_tensor(targets, name="target") with tf.name_scope(name): return tf.reduce_mean( - tf.reduce_sum(-(target * tf.log(output + epsilon) + (1. - target) * tf.log(1. - output + epsilon)), axis=1)) + tf.reduce_sum(-(target * tf.log(output + epsilon) + (1. - target) * tf.log(1. - output + epsilon)), axis=1) + ) # For brevity, let `x = output`, `z = target`. The binary cross entropy loss is # @@ -398,7 +399,8 @@ def cross_entropy_seq(logits, target_seqs, batch_size=None): #, batch_size=1, n # except: # sequence_loss_by_example_fn = tf.nn.seq2seq.sequence_loss_by_example - loss = sequence_loss_by_example_fn([logits], [tf.reshape(target_seqs, [-1])], + loss = sequence_loss_by_example_fn([logits], + [tf.reshape(target_seqs, [-1])], [tf.ones_like(tf.reshape(target_seqs, [-1]), dtype=tf.float32)]) # [tf.ones([batch_size * num_steps])]) cost = tf.reduce_sum(loss) #/ batch_size @@ -462,7 +464,8 @@ def cross_entropy_seq_with_mask(logits, target_seqs, input_mask, return_details= loss = tf.divide( tf.reduce_sum(losses), # loss from mask. reduce_sum before element-wise mul with mask !! tf.reduce_sum(weights), - name="seq_loss_with_mask") + name="seq_loss_with_mask" + ) # except: ## TF0.12 # loss = tf.div(tf.reduce_sum(losses), # loss from mask. reduce_sum before element-wise mul with mask !! # tf.reduce_sum(weights), @@ -493,7 +496,8 @@ def cosine_similarity(v1, v2): """ # try: ## TF1.0 cost = tf.reduce_sum(tf.multiply(v1, v2), 1) / ( - tf.sqrt(tf.reduce_sum(tf.multiply(v1, v1), 1)) * tf.sqrt(tf.reduce_sum(tf.multiply(v2, v2), 1))) + tf.sqrt(tf.reduce_sum(tf.multiply(v1, v1), 1)) * tf.sqrt(tf.reduce_sum(tf.multiply(v2, v2), 1)) + ) # except: ## TF0.12 # cost = tf.reduce_sum(tf.mul(v1, v2), reduction_indices=1) / (tf.sqrt(tf.reduce_sum(tf.mul(v1, v1), reduction_indices=1)) * tf.sqrt(tf.reduce_sum(tf.mul(v2, v2), reduction_indices=1))) return cost @@ -548,7 +552,8 @@ def li(weights): return standard_ops_fn( my_scale, standard_ops.reduce_sum(standard_ops.sqrt(standard_ops.reduce_sum(tf.square(weights), 1))), - name=scope) + name=scope + ) return li @@ -599,7 +604,8 @@ def lo(weights, name='lo_regularizer'): return standard_ops_fn( my_scale, standard_ops.reduce_sum(standard_ops.sqrt(standard_ops.reduce_sum(tf.square(weights), 0))), - name=scope) + name=scope + ) return lo @@ -697,7 +703,10 @@ def mn_o(weights, name='maxnorm_o_regularizer'): else: standard_ops_fn = standard_ops.multiply return standard_ops_fn( - my_scale, standard_ops.reduce_sum(standard_ops.reduce_max(standard_ops.abs(weights), 0)), name=scope) + my_scale, + standard_ops.reduce_sum(standard_ops.reduce_max(standard_ops.abs(weights), 0)), + name=scope + ) return mn_o @@ -746,6 +755,9 @@ def mn_i(weights, name='maxnorm_i_regularizer'): else: standard_ops_fn = standard_ops.multiply return standard_ops_fn( - my_scale, standard_ops.reduce_sum(standard_ops.reduce_max(standard_ops.abs(weights), 1)), name=scope) + my_scale, + standard_ops.reduce_sum(standard_ops.reduce_max(standard_ops.abs(weights), 1)), + name=scope + ) return mn_i diff --git a/tensorlayer/db.py b/tensorlayer/db.py index 265096c56..3704f30b4 100644 --- a/tensorlayer/db.py +++ b/tensorlayer/db.py @@ -59,8 +59,15 @@ class TensorDB(object): - You may like to install MongoChef or Mongo Management Studo APP for visualizing or testing your MongoDB. """ - def __init__(self, ip='localhost', port=27017, db_name='db_name', user_name=None, password='password', - studyID=None): + def __init__( + self, + ip='localhost', + port=27017, + db_name='db_name', + user_name=None, + password='password', + studyID=None + ): ## connect mongodb client = MongoClient(ip, port) self.db = client[db_name] diff --git a/tensorlayer/deprecation.py b/tensorlayer/deprecation.py index 61a153db6..d0da09bb8 100644 --- a/tensorlayer/deprecation.py +++ b/tensorlayer/deprecation.py @@ -36,7 +36,12 @@ def rename_kwargs(kwargs, aliases, end_support_version, func_name): raise TypeError('{}() received both {} and {}'.format(func_name, alias, new)) warnings.warn('{}() - {} is deprecated; use {}'.format(func_name, alias, new), DeprecationWarning) - logging.warning("DeprecationWarning: {}(): " - "`{}` argument is deprecated and will be removed in version {}, " - "please change for `{}.`".format(func_name, alias, end_support_version, new)) + logging.warning( + "DeprecationWarning: {}(): " + "`{}` argument is deprecated and will be removed in version {}, " + "please change for `{}.`".format(func_name, + alias, + end_support_version, + new) + ) kwargs[new] = kwargs.pop(alias) diff --git a/tensorlayer/distributed.py b/tensorlayer/distributed.py index 55788892c..155954221 100644 --- a/tensorlayer/distributed.py +++ b/tensorlayer/distributed.py @@ -109,7 +109,10 @@ def device_fn(self): current_device = '/job:{}/task:{}'.format(self.type, self._index) ps_devices = '/job:ps' return tf.train.replica_device_setter( - ps_device=ps_devices, worker_device=current_device, cluster=self._cluster_spec) + ps_device=ps_devices, + worker_device=current_device, + cluster=self._cluster_spec + ) def create_server(self): if self._server is None and self.ps_hosts and self.worker_hosts and not self.is_evaluator(): @@ -136,13 +139,15 @@ def use_last_worker_as_evaluator(self): """ if self.num_workers <= 1: raise Exception('You need more than one worker instance to use one as evaluator') + return TaskSpecDef( task_type=self.type, index=self._index, trial=self.trial, ps_hosts=self.ps_hosts, worker_hosts=self.worker_hosts[:-1], - master=self.master) + master=self.master + ) def create_task_spec_def(): @@ -162,10 +167,17 @@ def create_task_spec_def(): return TaskSpecDef( task_type=task_data['type'], index=task_data['index'], - trial=task_data['trial'] if 'trial' in task_data else None, + trial=task_data['trial'] if + 'trial' in task_data + else + None, ps_hosts=cluster_data['ps'], worker_hosts=cluster_data['worker'], - master=cluster_data['master'] if 'master' in cluster_data else None) + master=cluster_data['master'] if + 'master' in cluster_data + else + None + ) elif 'JOB_NAME' in os.environ: # JOB_NAME, TASK_INDEX, PS_HOSTS, WORKER_HOSTS and MASTER_HOST are used in TensorPort return TaskSpecDef( @@ -173,22 +185,25 @@ def create_task_spec_def(): index=os.environ['TASK_INDEX'], ps_hosts=os.environ.get('PS_HOSTS', None), worker_hosts=os.environ.get('WORKER_HOSTS', None), - master=os.environ.get('MASTER_HOST', None)) + master=os.environ.get('MASTER_HOST', None) + ) else: raise Exception('You need to setup TF_CONFIG or JOB_NAME to define the task.') -def create_distributed_session(task_spec=None, - checkpoint_dir=None, - scaffold=None, - hooks=None, - chief_only_hooks=None, - save_checkpoint_secs=600, - save_summaries_steps=object(), - save_summaries_secs=object(), - config=None, - stop_grace_period_secs=120, - log_step_count_steps=100): +def create_distributed_session( + task_spec=None, + checkpoint_dir=None, + scaffold=None, + hooks=None, + chief_only_hooks=None, + save_checkpoint_secs=600, + save_summaries_steps=object(), + save_summaries_secs=object(), + config=None, + stop_grace_period_secs=120, + log_step_count_steps=100 +): """Creates a distributed session. It calls `MonitoredTrainingSession` to create a :class:`MonitoredSession` for distributed training. @@ -284,7 +299,8 @@ def create_distributed_session(task_spec=None, stop_grace_period_secs=stop_grace_period_secs, config=config, hooks=hooks, - chief_only_hooks=chief_only_hooks) + chief_only_hooks=chief_only_hooks + ) class StopAtTimeHook(session_run_hook.SessionRunHook): diff --git a/tensorlayer/files.py b/tensorlayer/files.py index 4e7fadae8..a01bf163a 100644 --- a/tensorlayer/files.py +++ b/tensorlayer/files.py @@ -108,7 +108,11 @@ def load_fashion_mnist_dataset(shape=(-1, 784), path='data'): >>> X_train, y_train, X_val, y_val, X_test, y_test = tl.files.load_fashion_mnist_dataset(shape=(-1, 28, 28, 1)) """ return _load_mnist_dataset( - shape, path, name='fashion_mnist', url='http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/') + shape, + path, + name='fashion_mnist', + url='http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/' + ) def _load_mnist_dataset(shape, path, name='mnist', url='http://yann.lecun.com/exdb/mnist/'): @@ -494,15 +498,17 @@ def load_matt_mahoney_text8_dataset(path='data'): return word_list -def load_imdb_dataset(path='data', - nb_words=None, - skip_top=0, - maxlen=None, - test_split=0.2, - seed=113, - start_char=1, - oov_char=2, - index_from=3): +def load_imdb_dataset( + path='data', + nb_words=None, + skip_top=0, + maxlen=None, + test_split=0.2, + seed=113, + start_char=1, + oov_char=2, + index_from=3 +): """Load IMDB dataset. Parameters @@ -572,8 +578,10 @@ def load_imdb_dataset(path='data', X = new_X labels = new_labels if not X: - raise Exception('After filtering for sequences shorter than maxlen=' + str(maxlen) + ', no sequence was kept. ' - 'Increase maxlen.') + raise Exception( + 'After filtering for sequences shorter than maxlen=' + str(maxlen) + ', no sequence was kept. ' + 'Increase maxlen.' + ) if not nb_words: nb_words = max([max(x) for x in X]) @@ -809,8 +817,16 @@ def load_flickr1M_dataset(tag='sky', size=10, path="data", n_threads=50, printab path = os.path.join(path, 'flickr1M') logging.info("[Flickr1M] using {}% of images = {}".format(size * 10, size * 100000)) images_zip = [ - 'images0.zip', 'images1.zip', 'images2.zip', 'images3.zip', 'images4.zip', 'images5.zip', 'images6.zip', - 'images7.zip', 'images8.zip', 'images9.zip' + 'images0.zip', + 'images1.zip', + 'images2.zip', + 'images3.zip', + 'images4.zip', + 'images5.zip', + 'images6.zip', + 'images7.zip', + 'images8.zip', + 'images9.zip' ] tag_zip = 'tags.zip' url = 'http://press.liacs.nl/mirflickr/mirflickr1m/' @@ -942,7 +958,11 @@ def save_response_content(response, destination, chunk_size=32 * 1024): total_size = int(response.headers.get('content-length', 0)) with open(destination, "wb") as f: for chunk in tqdm( - response.iter_content(chunk_size), total=total_size, unit='B', unit_scale=True, desc=destination): + response.iter_content(chunk_size), + total=total_size, + unit='B', + unit_scale=True, + desc=destination): if chunk: # filter out keep-alive new chunks f.write(chunk) @@ -1110,7 +1130,8 @@ def _recursive_parse_xml_to_dict(xml): extracted_filename = "VOC2012test" #"VOCdevkit/VOC2012" logging.info(" [============= VOC 2012 Test Set =============]") logging.info( - " \nAuthor: 2012test only have person annotation, so 2007test is highly recommended for testing !\n") + " \nAuthor: 2012test only have person annotation, so 2007test is highly recommended for testing !\n" + ) import time time.sleep(3) if os.path.isdir(os.path.join(path, extracted_filename)) is False: @@ -1163,8 +1184,26 @@ def _recursive_parse_xml_to_dict(xml): del_folder(os.path.join(path, 'VOCdevkit')) # object classes(labels) NOTE: YOU CAN CUSTOMIZE THIS LIST classes = [ - "aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow", "diningtable", "dog", - "horse", "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor" + "aeroplane", + "bicycle", + "bird", + "boat", + "bottle", + "bus", + "car", + "cat", + "chair", + "cow", + "diningtable", + "dog", + "horse", + "motorbike", + "person", + "pottedplant", + "sheep", + "sofa", + "train", + "tvmonitor" ] if contain_classes_in_person: classes_in_person = ["head", "hand", "foot"] @@ -1182,7 +1221,8 @@ def _recursive_parse_xml_to_dict(xml): imgs_file_list = load_file_list(path=folder_imgs, regx='\\.jpg', printable=False) logging.info("[VOC] {} images found".format(len(imgs_file_list))) imgs_file_list.sort( - key=lambda s: int(s.replace('.', ' ').replace('_', '').split(' ')[-2])) # 2007_000027.jpg --> 2007000027 + key=lambda s: int(s.replace('.', ' ').replace('_', '').split(' ')[-2]) + ) # 2007_000027.jpg --> 2007000027 imgs_file_list = [os.path.join(folder_imgs, s) for s in imgs_file_list] # logging.info('IM',imgs_file_list[0::3333], imgs_file_list[-1]) if dataset != "2012test": @@ -1192,7 +1232,8 @@ def _recursive_parse_xml_to_dict(xml): imgs_semseg_file_list = load_file_list(path=folder_semseg, regx='\\.png', printable=False) logging.info("[VOC] {} maps for semantic segmentation found".format(len(imgs_semseg_file_list))) imgs_semseg_file_list.sort( - key=lambda s: int(s.replace('.', ' ').replace('_', '').split(' ')[-2])) # 2007_000032.png --> 2007000032 + key=lambda s: int(s.replace('.', ' ').replace('_', '').split(' ')[-2]) + ) # 2007_000032.png --> 2007000032 imgs_semseg_file_list = [os.path.join(folder_semseg, s) for s in imgs_semseg_file_list] # logging.info('Semantic Seg IM',imgs_semseg_file_list[0::333], imgs_semseg_file_list[-1]) ##======== 3. instance segmentation maps path list @@ -1201,7 +1242,8 @@ def _recursive_parse_xml_to_dict(xml): imgs_insseg_file_list = load_file_list(path=folder_insseg, regx='\\.png', printable=False) logging.info("[VOC] {} maps for instance segmentation found".format(len(imgs_semseg_file_list))) imgs_insseg_file_list.sort( - key=lambda s: int(s.replace('.', ' ').replace('_', '').split(' ')[-2])) # 2007_000032.png --> 2007000032 + key=lambda s: int(s.replace('.', ' ').replace('_', '').split(' ')[-2]) + ) # 2007_000032.png --> 2007000032 imgs_insseg_file_list = [os.path.join(folder_insseg, s) for s in imgs_insseg_file_list] # logging.info('Instance Seg IM',imgs_insseg_file_list[0::333], imgs_insseg_file_list[-1]) else: @@ -1211,10 +1253,12 @@ def _recursive_parse_xml_to_dict(xml): # folder_ann = path+"/"+extracted_filename+"/Annotations/" folder_ann = os.path.join(path, extracted_filename, "Annotations") imgs_ann_file_list = load_file_list(path=folder_ann, regx='\\.xml', printable=False) - logging.info("[VOC] {} XML annotation files for bounding box and object class found".format( - len(imgs_ann_file_list))) + logging.info( + "[VOC] {} XML annotation files for bounding box and object class found".format(len(imgs_ann_file_list)) + ) imgs_ann_file_list.sort( - key=lambda s: int(s.replace('.', ' ').replace('_', '').split(' ')[-2])) # 2007_000027.xml --> 2007000027 + key=lambda s: int(s.replace('.', ' ').replace('_', '').split(' ')[-2]) + ) # 2007_000027.xml --> 2007000027 imgs_ann_file_list = [os.path.join(folder_ann, s) for s in imgs_ann_file_list] # logging.info('ANN',imgs_ann_file_list[0::3333], imgs_ann_file_list[-1]) @@ -1266,8 +1310,12 @@ def convert_annotation(file_name): continue cls_id = classes.index(cls) xmlbox = obj.find('bndbox') - b = (float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), float(xmlbox.find('ymin').text), - float(xmlbox.find('ymax').text)) + b = ( + float(xmlbox.find('xmin').text), + float(xmlbox.find('xmax').text), + float(xmlbox.find('ymin').text), + float(xmlbox.find('ymax').text) + ) bb = convert((w, h), b) out_file += str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n' @@ -1279,8 +1327,12 @@ def convert_annotation(file_name): continue cls_id = classes.index(cls) xmlbox = part.find('bndbox') - b = (float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), - float(xmlbox.find('ymin').text), float(xmlbox.find('ymax').text)) + b = ( + float(xmlbox.find('xmin').text), + float(xmlbox.find('xmax').text), + float(xmlbox.find('ymin').text), + float(xmlbox.find('ymax').text) + ) bb = convert((w, h), b) # out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n') out_file += str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n' @@ -1391,7 +1443,9 @@ def save_joints(): mat = sio.loadmat(os.path.join(path, extracted_filename, "mpii_human_pose_v1_u12_1.mat")) for _, (anno, train_flag) in enumerate( # all images - zip(mat['RELEASE']['annolist'][0, 0][0], mat['RELEASE']['img_train'][0, 0][0])): + zip( + mat['RELEASE']['annolist'][0, 0][0], + mat['RELEASE']['img_train'][0, 0][0])): img_fn = anno['image']['name'][0, 0][0] train_flag = int(train_flag) @@ -1407,9 +1461,10 @@ def save_joints(): head_rect = [] if 'x1' in str(anno['annorect'].dtype): - head_rect = zip( - [x1[0, 0] for x1 in anno['annorect']['x1'][0]], [y1[0, 0] for y1 in anno['annorect']['y1'][0]], - [x2[0, 0] for x2 in anno['annorect']['x2'][0]], [y2[0, 0] for y2 in anno['annorect']['y2'][0]]) + head_rect = zip([x1[0, 0] for x1 in anno['annorect']['x1'][0]], + [y1[0, 0] for y1 in anno['annorect']['y1'][0]], + [x2[0, 0] for x2 in anno['annorect']['x2'][0]], + [y2[0, 0] for y2 in anno['annorect']['y2'][0]]) else: head_rect = [] # TODO @@ -1419,8 +1474,12 @@ def save_joints(): head_y1s = anno['annorect']['y1'][0] head_x2s = anno['annorect']['x2'][0] head_y2s = anno['annorect']['y2'][0] - for annopoint, head_x1, head_y1, head_x2, head_y2 in zip(annopoints, head_x1s, head_y1s, head_x2s, - head_y2s): + for annopoint, head_x1, head_y1, head_x2, head_y2 in zip( + annopoints, + head_x1s, + head_y1s, + head_x2s, + head_y2s): if annopoint != []: head_rect = [ float(head_x1[0, 0]), @@ -1447,7 +1506,7 @@ def save_joints(): vis = None # if len(joint_pos) == 16: - if ((is_16_pos_only == True) and (len(joint_pos) == 16)) or (is_16_pos_only == False): + if ((is_16_pos_only ==True) and (len(joint_pos) == 16)) or (is_16_pos_only == False): # only use image with 16 key points / or use all data = { 'filename': img_fn, @@ -1771,12 +1830,14 @@ def load_and_assign_npz_dict(name='model.npz', sess=None): logging.info("[*] Model restored from npz_dict %s" % name) -def save_ckpt(sess=None, - mode_name='model.ckpt', - save_dir='checkpoint', - var_list=None, - global_step=None, - printable=False): +def save_ckpt( + sess=None, + mode_name='model.ckpt', + save_dir='checkpoint', + var_list=None, + global_step=None, + printable=False +): """Save parameters into `ckpt` file. Parameters diff --git a/tensorlayer/iterate.py b/tensorlayer/iterate.py index 9ea07f346..e37949550 100644 --- a/tensorlayer/iterate.py +++ b/tensorlayer/iterate.py @@ -59,7 +59,7 @@ def minibatches(inputs=None, targets=None, batch_size=None, shuffle=False): excerpt = indices[start_idx:start_idx + batch_size] else: excerpt = slice(start_idx, start_idx + batch_size) - if (isinstance(inputs, list) or isinstance(targets, list)) and (shuffle == True): + if (isinstance(inputs, list) or isinstance(targets, list)) and (shuffle ==True): yield [inputs[i] for i in excerpt], [targets[i] for i in excerpt] # zsdonghao: for list indexing when shuffle==True else: diff --git a/tensorlayer/layers/binary.py b/tensorlayer/layers/binary.py index e8052198c..f27f3fa89 100644 --- a/tensorlayer/layers/binary.py +++ b/tensorlayer/layers/binary.py @@ -81,8 +81,10 @@ def _compute_alpha(x): alpha_array = tf.add(alpha1_temp1, alpha1_temp2, name=None) alpha_array_abs = tf.abs(alpha_array) alpha_array_abs1 = tf.where( - tf.greater(alpha_array_abs, 0), tf.ones_like(alpha_array_abs, tf.float32), - tf.zeros_like(alpha_array_abs, tf.float32)) + tf.greater(alpha_array_abs, 0), + tf.ones_like(alpha_array_abs, tf.float32), + tf.zeros_like(alpha_array_abs, tf.float32) + ) alpha_sum = tf.reduce_sum(alpha_array_abs) n = tf.reduce_sum(alpha_array_abs1) alpha = tf.div(alpha_sum, n) @@ -162,7 +164,12 @@ def __init__( with tf.variable_scope(name): W = tf.get_variable( - name='W', shape=(n_in, n_units), initializer=W_init, dtype=LayersConfig.tf_dtype, **W_init_args) + name='W', + shape=(n_in, n_units), + initializer=W_init, + dtype=LayersConfig.tf_dtype, + **W_init_args + ) # W = tl.act.sign(W) # dont update ... W = quantize(W) # W = tf.Variable(W) @@ -170,7 +177,12 @@ def __init__( if b_init is not None: try: b = tf.get_variable( - name='b', shape=(n_units), initializer=b_init, dtype=LayersConfig.tf_dtype, **b_init_args) + name='b', + shape=(n_units), + initializer=b_init, + dtype=LayersConfig.tf_dtype, + **b_init_args + ) except Exception: # If initializer is a constant, do not specify shape. b = tf.get_variable(name='b', initializer=b_init, dtype=LayersConfig.tf_dtype, **b_init_args) self.outputs = act(tf.matmul(self.inputs, W) + b) @@ -267,10 +279,15 @@ def __init__( name='binary_cnn2d', ): super(BinaryConv2d, self).__init__(prev_layer=prev_layer, name=name) - logging.info("BinaryConv2d %s: n_filter:%d filter_size:%s strides:%s pad:%s act:%s" % (name, n_filter, - str(filter_size), - str(strides), padding, - act.__name__)) + logging.info( + "BinaryConv2d %s: n_filter:%d filter_size:%s strides:%s pad:%s act:%s" % + (name, + n_filter, + str(filter_size), + str(strides), + padding, + act.__name__) + ) self.inputs = prev_layer.outputs @@ -294,11 +311,21 @@ def __init__( strides = (1, strides[0], strides[1], 1) with tf.variable_scope(name): W = tf.get_variable( - name='W_conv2d', shape=shape, initializer=W_init, dtype=LayersConfig.tf_dtype, **W_init_args) + name='W_conv2d', + shape=shape, + initializer=W_init, + dtype=LayersConfig.tf_dtype, + **W_init_args + ) W = quantize(W) if b_init: b = tf.get_variable( - name='b_conv2d', shape=(shape[-1]), initializer=b_init, dtype=LayersConfig.tf_dtype, **b_init_args) + name='b_conv2d', + shape=(shape[-1]), + initializer=b_init, + dtype=LayersConfig.tf_dtype, + **b_init_args + ) self.outputs = act( tf.nn.conv2d( self.inputs, @@ -306,7 +333,9 @@ def __init__( strides=strides, padding=padding, use_cudnn_on_gpu=use_cudnn_on_gpu, - data_format=data_format) + b) + data_format=data_format + ) + b + ) else: self.outputs = act( tf.nn.conv2d( @@ -315,7 +344,9 @@ def __init__( strides=strides, padding=padding, use_cudnn_on_gpu=use_cudnn_on_gpu, - data_format=data_format)) + data_format=data_format + ) + ) self.all_layers.append(self.outputs) if b_init: @@ -385,7 +416,12 @@ def __init__( with tf.variable_scope(name): W = tf.get_variable( - name='W', shape=(n_in, n_units), initializer=W_init, dtype=LayersConfig.tf_dtype, **W_init_args) + name='W', + shape=(n_in, n_units), + initializer=W_init, + dtype=LayersConfig.tf_dtype, + **W_init_args + ) # W = tl.act.sign(W) # dont update ... alpha = _compute_alpha(W) W = _ternary_operation(W) @@ -395,7 +431,12 @@ def __init__( if b_init is not None: try: b = tf.get_variable( - name='b', shape=(n_units), initializer=b_init, dtype=LayersConfig.tf_dtype, **b_init_args) + name='b', + shape=(n_units), + initializer=b_init, + dtype=LayersConfig.tf_dtype, + **b_init_args + ) except Exception: # If initializer is a constant, do not specify shape. b = tf.get_variable(name='b', initializer=b_init, dtype=LayersConfig.tf_dtype, **b_init_args) self.outputs = act(tf.matmul(self.inputs, W) + b) @@ -492,10 +533,15 @@ def __init__( name='ternary_cnn2d', ): super(TernaryConv2d, self).__init__(prev_layer=prev_layer, name=name) - logging.info("TernaryConv2d %s: n_filter:%d filter_size:%s strides:%s pad:%s act:%s" % (name, n_filter, - str(filter_size), - str(strides), padding, - act.__name__)) + logging.info( + "TernaryConv2d %s: n_filter:%d filter_size:%s strides:%s pad:%s act:%s" % + (name, + n_filter, + str(filter_size), + str(strides), + padding, + act.__name__) + ) if W_init_args is None: W_init_args = {} @@ -517,13 +563,23 @@ def __init__( strides = (1, strides[0], strides[1], 1) with tf.variable_scope(name): W = tf.get_variable( - name='W_conv2d', shape=shape, initializer=W_init, dtype=LayersConfig.tf_dtype, **W_init_args) + name='W_conv2d', + shape=shape, + initializer=W_init, + dtype=LayersConfig.tf_dtype, + **W_init_args + ) alpha = _compute_alpha(W) W = _ternary_operation(W) W = tf.multiply(alpha, W) if b_init: b = tf.get_variable( - name='b_conv2d', shape=(shape[-1]), initializer=b_init, dtype=LayersConfig.tf_dtype, **b_init_args) + name='b_conv2d', + shape=(shape[-1]), + initializer=b_init, + dtype=LayersConfig.tf_dtype, + **b_init_args + ) self.outputs = act( tf.nn.conv2d( self.inputs, @@ -531,7 +587,9 @@ def __init__( strides=strides, padding=padding, use_cudnn_on_gpu=use_cudnn_on_gpu, - data_format=data_format) + b) + data_format=data_format + ) + b + ) else: self.outputs = act( tf.nn.conv2d( @@ -540,7 +598,9 @@ def __init__( strides=strides, padding=padding, use_cudnn_on_gpu=use_cudnn_on_gpu, - data_format=data_format)) + data_format=data_format + ) + ) self.all_layers.append(self.outputs) if b_init: @@ -617,7 +677,12 @@ def __init__( with tf.variable_scope(name): W = tf.get_variable( - name='W', shape=(n_in, n_units), initializer=W_init, dtype=LayersConfig.tf_dtype, **W_init_args) + name='W', + shape=(n_in, n_units), + initializer=W_init, + dtype=LayersConfig.tf_dtype, + **W_init_args + ) # W = tl.act.sign(W) # dont update ... W = _quantize_weight(W, bitW) self.inputs = _quantize_active(_cabs(self.inputs), bitA) @@ -626,7 +691,12 @@ def __init__( if b_init is not None: try: b = tf.get_variable( - name='b', shape=(n_units), initializer=b_init, dtype=LayersConfig.tf_dtype, **b_init_args) + name='b', + shape=(n_units), + initializer=b_init, + dtype=LayersConfig.tf_dtype, + **b_init_args + ) except Exception: # If initializer is a constant, do not specify shape. b = tf.get_variable(name='b', initializer=b_init, dtype=LayersConfig.tf_dtype, **b_init_args) self.outputs = act(tf.matmul(self.inputs, W) + b) @@ -729,10 +799,15 @@ def __init__( name='dorefa_cnn2d', ): super(DorefaConv2d, self).__init__(prev_layer=prev_layer, name=name) - logging.info("DorefaConv2d %s: n_filter:%d filter_size:%s strides:%s pad:%s act:%s" % (name, n_filter, - str(filter_size), - str(strides), padding, - act.__name__)) + logging.info( + "DorefaConv2d %s: n_filter:%d filter_size:%s strides:%s pad:%s act:%s" % + (name, + n_filter, + str(filter_size), + str(strides), + padding, + act.__name__) + ) self.inputs = prev_layer.outputs @@ -757,12 +832,22 @@ def __init__( strides = (1, strides[0], strides[1], 1) with tf.variable_scope(name): W = tf.get_variable( - name='W_conv2d', shape=shape, initializer=W_init, dtype=LayersConfig.tf_dtype, **W_init_args) + name='W_conv2d', + shape=shape, + initializer=W_init, + dtype=LayersConfig.tf_dtype, + **W_init_args + ) W = _quantize_weight(W, bitW) self.inputs = _quantize_active(_cabs(self.inputs), bitA) if b_init: b = tf.get_variable( - name='b_conv2d', shape=(shape[-1]), initializer=b_init, dtype=LayersConfig.tf_dtype, **b_init_args) + name='b_conv2d', + shape=(shape[-1]), + initializer=b_init, + dtype=LayersConfig.tf_dtype, + **b_init_args + ) self.outputs = act( tf.nn.conv2d( self.inputs, @@ -770,7 +855,9 @@ def __init__( strides=strides, padding=padding, use_cudnn_on_gpu=use_cudnn_on_gpu, - data_format=data_format) + b) + data_format=data_format + ) + b + ) else: self.outputs = act( tf.nn.conv2d( @@ -779,7 +866,9 @@ def __init__( strides=strides, padding=padding, use_cudnn_on_gpu=use_cudnn_on_gpu, - data_format=data_format)) + data_format=data_format + ) + ) self.all_layers.append(self.outputs) if b_init: diff --git a/tensorlayer/layers/convolution.py b/tensorlayer/layers/convolution.py index 66be13c52..222a74bf0 100644 --- a/tensorlayer/layers/convolution.py +++ b/tensorlayer/layers/convolution.py @@ -79,8 +79,12 @@ def __init__( name='cnn1d', ): super(Conv1dLayer, self).__init__(prev_layer=prev_layer, name=name) - logging.info("Conv1dLayer %s: shape:%s stride:%s pad:%s act:%s" % (name, str(shape), str(stride), padding, - act.__name__)) + logging.info( + "Conv1dLayer %s: shape:%s stride:%s pad:%s act:%s" % (name, + str(shape), + str(stride), + padding, act.__name__) + ) self.inputs = prev_layer.outputs @@ -94,12 +98,27 @@ def __init__( with tf.variable_scope(name): W = tf.get_variable( - name='W_conv1d', shape=shape, initializer=W_init, dtype=LayersConfig.tf_dtype, **W_init_args) + name='W_conv1d', + shape=shape, + initializer=W_init, + dtype=LayersConfig.tf_dtype, + **W_init_args + ) self.outputs = tf.nn.convolution( - self.inputs, W, strides=(stride, ), padding=padding, dilation_rate=(dilation_rate, )) # 1.2 + self.inputs, + W, + strides=(stride, ), + padding=padding, + dilation_rate=(dilation_rate, ) + ) # 1.2 if b_init: b = tf.get_variable( - name='b_conv1d', shape=(shape[-1]), initializer=b_init, dtype=LayersConfig.tf_dtype, **b_init_args) + name='b_conv1d', + shape=(shape[-1]), + initializer=b_init, + dtype=LayersConfig.tf_dtype, + **b_init_args + ) self.outputs = self.outputs + b self.outputs = act(self.outputs) @@ -196,8 +215,13 @@ def __init__( name='cnn_layer', ): super(Conv2dLayer, self).__init__(prev_layer=prev_layer, name=name) - logging.info("Conv2dLayer %s: shape:%s strides:%s pad:%s act:%s" % (name, str(shape), str(strides), padding, - act.__name__)) + logging.info( + "Conv2dLayer %s: shape:%s strides:%s pad:%s act:%s" % + (name, + str(shape), + str(strides), + padding, act.__name__) + ) self.inputs = prev_layer.outputs @@ -210,10 +234,20 @@ def __init__( with tf.variable_scope(name): W = tf.get_variable( - name='W_conv2d', shape=shape, initializer=W_init, dtype=LayersConfig.tf_dtype, **W_init_args) + name='W_conv2d', + shape=shape, + initializer=W_init, + dtype=LayersConfig.tf_dtype, + **W_init_args + ) if b_init: b = tf.get_variable( - name='b_conv2d', shape=(shape[-1]), initializer=b_init, dtype=LayersConfig.tf_dtype, **b_init_args) + name='b_conv2d', + shape=(shape[-1]), + initializer=b_init, + dtype=LayersConfig.tf_dtype, + **b_init_args + ) self.outputs = act( tf.nn.conv2d( self.inputs, @@ -221,7 +255,9 @@ def __init__( strides=strides, padding=padding, use_cudnn_on_gpu=use_cudnn_on_gpu, - data_format=data_format) + b) + data_format=data_format + ) + b + ) else: self.outputs = act( tf.nn.conv2d( @@ -230,7 +266,9 @@ def __init__( strides=strides, padding=padding, use_cudnn_on_gpu=use_cudnn_on_gpu, - data_format=data_format)) + data_format=data_format + ) + ) self.all_layers.append(self.outputs) if b_init: @@ -332,10 +370,14 @@ def __init__( name='decnn2d_layer', ): super(DeConv2dLayer, self).__init__(prev_layer=prev_layer, name=name) - logging.info("DeConv2dLayer %s: shape:%s out_shape:%s strides:%s pad:%s act:%s" % (name, str(shape), - str(output_shape), - str(strides), padding, - act.__name__)) + logging.info( + "DeConv2dLayer %s: shape:%s out_shape:%s strides:%s pad:%s act:%s" % + (name, + str(shape), + str(output_shape), + str(strides), + padding, act.__name__) + ) self.inputs = prev_layer.outputs @@ -349,20 +391,32 @@ def __init__( # logging.info(" DeConv2dLayer: Untested") with tf.variable_scope(name): W = tf.get_variable( - name='W_deconv2d', shape=shape, initializer=W_init, dtype=LayersConfig.tf_dtype, **W_init_args) + name='W_deconv2d', + shape=shape, + initializer=W_init, + dtype=LayersConfig.tf_dtype, + **W_init_args + ) if b_init: b = tf.get_variable( name='b_deconv2d', shape=(shape[-2]), initializer=b_init, dtype=LayersConfig.tf_dtype, - **b_init_args) + **b_init_args + ) self.outputs = act( tf.nn.conv2d_transpose(self.inputs, W, output_shape=output_shape, strides=strides, padding=padding) - + b) + + b + ) else: self.outputs = act( - tf.nn.conv2d_transpose(self.inputs, W, output_shape=output_shape, strides=strides, padding=padding)) + tf.nn.conv2d_transpose(self.inputs, + W, + output_shape=output_shape, + strides=strides, + padding=padding) + ) # self.all_layers = list(layer.all_layers) # self.all_params = list(layer.all_params) @@ -425,8 +479,13 @@ def __init__( name='cnn3d_layer', ): super(Conv3dLayer, self).__init__(prev_layer=prev_layer, name=name) - logging.info("Conv3dLayer %s: shape:%s strides:%s pad:%s act:%s" % (name, str(shape), str(strides), padding, - act.__name__)) + logging.info( + "Conv3dLayer %s: shape:%s strides:%s pad:%s act:%s" % + (name, + str(shape), + str(strides), + padding, act.__name__) + ) self.inputs = prev_layer.outputs @@ -441,10 +500,20 @@ def __init__( # W = tf.Variable(W_init(shape=shape, **W_init_args), name='W_conv') # b = tf.Variable(b_init(shape=[shape[-1]], **b_init_args), name='b_conv') W = tf.get_variable( - name='W_conv3d', shape=shape, initializer=W_init, dtype=LayersConfig.tf_dtype, **W_init_args) + name='W_conv3d', + shape=shape, + initializer=W_init, + dtype=LayersConfig.tf_dtype, + **W_init_args + ) if b_init: b = tf.get_variable( - name='b_conv3d', shape=(shape[-1]), initializer=b_init, dtype=LayersConfig.tf_dtype, **b_init_args) + name='b_conv3d', + shape=(shape[-1]), + initializer=b_init, + dtype=LayersConfig.tf_dtype, + **b_init_args + ) self.outputs = act(tf.nn.conv3d(self.inputs, W, strides=strides, padding=padding, name=None) + b) else: self.outputs = act(tf.nn.conv3d(self.inputs, W, strides=strides, padding=padding, name=None)) @@ -508,10 +577,14 @@ def __init__( name='decnn3d_layer', ): super(DeConv3dLayer, self).__init__(prev_layer=prev_layer, name=name) - logging.info("DeConv3dLayer %s: shape:%s out_shape:%s strides:%s pad:%s act:%s" % (name, str(shape), - str(output_shape), - str(strides), padding, - act.__name__)) + logging.info( + "DeConv3dLayer %s: shape:%s out_shape:%s strides:%s pad:%s act:%s" % + (name, + str(shape), + str(output_shape), + str(strides), + padding, act.__name__) + ) self.inputs = prev_layer.outputs @@ -524,20 +597,32 @@ def __init__( with tf.variable_scope(name): W = tf.get_variable( - name='W_deconv3d', shape=shape, initializer=W_init, dtype=LayersConfig.tf_dtype, **W_init_args) + name='W_deconv3d', + shape=shape, + initializer=W_init, + dtype=LayersConfig.tf_dtype, + **W_init_args + ) if b_init: b = tf.get_variable( name='b_deconv3d', shape=(shape[-2]), initializer=b_init, dtype=LayersConfig.tf_dtype, - **b_init_args) + **b_init_args + ) self.outputs = act( tf.nn.conv3d_transpose(self.inputs, W, output_shape=output_shape, strides=strides, padding=padding) - + b) + + b + ) else: self.outputs = act( - tf.nn.conv3d_transpose(self.inputs, W, output_shape=output_shape, strides=strides, padding=padding)) + tf.nn.conv3d_transpose(self.inputs, + W, + output_shape=output_shape, + strides=strides, + padding=padding) + ) # self.all_layers = list(layer.all_layers) # self.all_params = list(layer.all_params) @@ -584,8 +669,14 @@ def __init__( name='upsample2d_layer', ): super(UpSampling2dLayer, self).__init__(prev_layer=prev_layer, name=name) - logging.info("UpSampling2dLayer %s: is_scale:%s size:%s method:%d align_corners:%s" % (name, is_scale, size, - method, align_corners)) + logging.info( + "UpSampling2dLayer %s: is_scale:%s size:%s method:%d align_corners:%s" % + (name, + is_scale, + size, + method, + align_corners) + ) self.inputs = prev_layer.outputs @@ -610,10 +701,19 @@ def __init__( with tf.variable_scope(name): try: self.outputs = tf.image.resize_images( - self.inputs, size=size, method=method, align_corners=align_corners) + self.inputs, + size=size, + method=method, + align_corners=align_corners + ) except Exception: # for TF 0.10 self.outputs = tf.image.resize_images( - self.inputs, new_height=size[0], new_width=size[1], method=method, align_corners=align_corners) + self.inputs, + new_height=size[0], + new_width=size[1], + method=method, + align_corners=align_corners + ) # self.all_layers = list(layer.all_layers) # self.all_params = list(layer.all_params) @@ -656,8 +756,14 @@ def __init__( name='downsample2d_layer', ): super(DownSampling2dLayer, self).__init__(prev_layer=prev_layer, name=name) - logging.info("DownSampling2dLayer %s: is_scale:%s size:%s method:%d, align_corners:%s" % - (name, is_scale, size, method, align_corners)) + logging.info( + "DownSampling2dLayer %s: is_scale:%s size:%s method:%d, align_corners:%s" % + (name, + is_scale, + size, + method, + align_corners) + ) self.inputs = prev_layer.outputs @@ -680,10 +786,19 @@ def __init__( with tf.variable_scope(name): try: self.outputs = tf.image.resize_images( - self.inputs, size=size, method=method, align_corners=align_corners) + self.inputs, + size=size, + method=method, + align_corners=align_corners + ) except Exception: # for TF 0.10 self.outputs = tf.image.resize_images( - self.inputs, new_height=size[0], new_width=size[1], method=method, align_corners=align_corners) + self.inputs, + new_height=size[0], + new_width=size[1], + method=method, + align_corners=align_corners + ) # self.all_layers = list(layer.all_layers) # self.all_params = list(layer.all_params) @@ -752,11 +867,13 @@ def __init__( W_init=tf.truncated_normal_initializer(stddev=0.02), b_init=tf.constant_initializer(value=0.0), W_init_args=None, - b_init_args=None): + b_init_args=None + ): if tf.__version__ < "1.4": raise Exception( - "Deformable CNN layer requires tensrflow 1.4 or higher version | current version %s" % tf.__version__) + "Deformable CNN layer requires tensrflow 1.4 or higher version | current version %s" % tf.__version__ + ) if W_init_args is None: W_init_args = {} @@ -878,12 +995,11 @@ def _tf_batch_map_offsets(inputs, offsets, grid_offset): coords = tf.tile(coords, [batch_size, 1, 1, 1, 1]) + offsets # grid_offset --> (b, h, w, n, 2) # clip out of bound - coords = tf.stack( - [ - tf.clip_by_value(coords[:, :, :, :, 0], 0.0, tf.cast(input_h - 1, 'float32')), - tf.clip_by_value(coords[:, :, :, :, 1], 0.0, tf.cast(input_w - 1, 'float32')) - ], - axis=-1) + coords = tf.stack([ + tf.clip_by_value(coords[:, :, :, :, 0], 0.0, tf.cast(input_h - 1, 'float32')), + tf.clip_by_value(coords[:, :, :, :, 1], 0.0, tf.cast(input_w - 1, 'float32')) + ], + axis=-1) coords = tf.tile(coords, [channel, 1, 1, 1, 1]) mapped_vals = _tf_batch_map_coordinates(inputs, coords) @@ -893,8 +1009,13 @@ def _tf_batch_map_offsets(inputs, offsets, grid_offset): return mapped_vals super(DeformableConv2d, self).__init__(prev_layer=prev_layer, name=name) - logging.info("DeformableConv2d %s: n_filter: %d, filter_size: %s act:%s" % (name, n_filter, str(filter_size), - act.__name__)) + logging.info( + "DeformableConv2d %s: n_filter: %d, filter_size: %s act:%s" % + (name, + n_filter, + str(filter_size), + act.__name__) + ) self.inputs = prev_layer.outputs @@ -927,7 +1048,8 @@ def _tf_batch_map_offsets(inputs, offsets, grid_offset): grid = tf.meshgrid( tf.range(-int((shape[0] - 1) / 2.0), int(input_h - int((shape[0] - 1) / 2.0)), 1), tf.range(-int((shape[1] - 1) / 2.0), int(input_w - int((shape[1] - 1) / 2.0)), 1), - indexing='ij') + indexing='ij' + ) grid = tf.stack(grid, axis=-1) grid = tf.cast(grid, 'float32') # grid --> (h, w, 2) @@ -939,10 +1061,17 @@ def _tf_batch_map_offsets(inputs, offsets, grid_offset): W = tf.get_variable( name='W_deformableconv2d', - shape=[1, 1, shape[0] * shape[1], shape[-2], shape[-1]], + shape=[ + 1, + 1, + shape[0] * shape[1], + shape[-2], + shape[-1] + ], initializer=W_init, dtype=LayersConfig.tf_dtype, - **W_init_args) + **W_init_args + ) if b_init: b = tf.get_variable( @@ -950,14 +1079,28 @@ def _tf_batch_map_offsets(inputs, offsets, grid_offset): shape=(shape[-1]), initializer=b_init, dtype=LayersConfig.tf_dtype, - **b_init_args) + **b_init_args + ) self.outputs = tf.reshape( act(tf.nn.conv3d(input_deform, W, strides=[1, 1, 1, 1, 1], padding='VALID', name=None) + b), - (tf.shape(self.inputs)[0], input_h, input_w, shape[-1])) + (tf.shape(self.inputs)[0], input_h, input_w, shape[-1]) + ) else: self.outputs = tf.reshape( - act(tf.nn.conv3d(input_deform, W, strides=[1, 1, 1, 1, 1], padding='VALID', name=None)), - (tf.shape(self.inputs)[0], input_h, input_w, shape[-1])) + act(tf.nn.conv3d( + input_deform, + W, + strides=[ + 1, + 1, + 1, + 1, + 1 + ], + padding='VALID', + name=None + )), (tf.shape(self.inputs)[0], input_h, input_w, shape[-1]) + ) # fixed # self.all_layers = list(layer.all_layers) @@ -1089,22 +1232,31 @@ class AtrousConv2dLayer(Layer): """ @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release - def __init__(self, - prev_layer, - n_filter=32, - filter_size=(3, 3), - rate=2, - act=tf.identity, - padding='SAME', - W_init=tf.truncated_normal_initializer(stddev=0.02), - b_init=tf.constant_initializer(value=0.0), - W_init_args=None, - b_init_args=None, - name='atrou2d'): + def __init__( + self, + prev_layer, + n_filter=32, + filter_size=(3, 3), + rate=2, + act=tf.identity, + padding='SAME', + W_init=tf.truncated_normal_initializer(stddev=0.02), + b_init=tf.constant_initializer(value=0.0), + W_init_args=None, + b_init_args=None, + name='atrou2d' + ): super(AtrousConv2dLayer, self).__init__(prev_layer=prev_layer, name=name) - logging.info("AtrousConv2dLayer %s: n_filter:%d filter_size:%s rate:%d pad:%s act:%s" % - (name, n_filter, filter_size, rate, padding, act.__name__)) + logging.info( + "AtrousConv2dLayer %s: n_filter:%d filter_size:%s rate:%d pad:%s act:%s" % + (name, + n_filter, + filter_size, + rate, + padding, + act.__name__) + ) self.inputs = prev_layer.outputs @@ -1118,10 +1270,20 @@ def __init__(self, with tf.variable_scope(name): shape = [filter_size[0], filter_size[1], int(self.inputs.get_shape()[-1]), n_filter] filters = tf.get_variable( - name='filter', shape=shape, initializer=W_init, dtype=LayersConfig.tf_dtype, **W_init_args) + name='filter', + shape=shape, + initializer=W_init, + dtype=LayersConfig.tf_dtype, + **W_init_args + ) if b_init: b = tf.get_variable( - name='b', shape=(n_filter), initializer=b_init, dtype=LayersConfig.tf_dtype, **b_init_args) + name='b', + shape=(n_filter), + initializer=b_init, + dtype=LayersConfig.tf_dtype, + **b_init_args + ) self.outputs = act(tf.nn.atrous_conv2d(self.inputs, filters, rate, padding) + b) else: self.outputs = act(tf.nn.atrous_conv2d(self.inputs, filters, rate, padding)) @@ -1191,31 +1353,42 @@ class _SeparableConv2dLayer(Layer): # TODO """ @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release - def __init__(self, - prev_layer, - n_filter, - filter_size=5, - strides=(1, 1), - padding='valid', - data_format='channels_last', - dilation_rate=(1, 1), - depth_multiplier=1, - act=tf.identity, - use_bias=True, - depthwise_initializer=None, - pointwise_initializer=None, - bias_initializer=tf.zeros_initializer, - depthwise_regularizer=None, - pointwise_regularizer=None, - bias_regularizer=None, - activity_regularizer=None, - name='atrou2d'): + def __init__( + self, + prev_layer, + n_filter, + filter_size=5, + strides=(1, 1), + padding='valid', + data_format='channels_last', + dilation_rate=(1, 1), + depth_multiplier=1, + act=tf.identity, + use_bias=True, + depthwise_initializer=None, + pointwise_initializer=None, + bias_initializer=tf.zeros_initializer, + depthwise_regularizer=None, + pointwise_regularizer=None, + bias_regularizer=None, + activity_regularizer=None, + name='atrou2d' + ): super(_SeparableConv2dLayer, self).__init__(prev_layer=prev_layer, name=name) logging.info( "SeparableConv2dLayer %s: n_filter:%d filter_size:%s strides:%s padding:%s dilation_rate:%s depth_multiplier:%s act:%s" - % (name, n_filter, filter_size, str(strides), padding, str(dilation_rate), str(depth_multiplier), - act.__name__)) + % ( + name, + n_filter, + filter_size, + str(strides), + padding, + str(dilation_rate), + str(depth_multiplier), + act.__name__ + ) + ) self.inputs = prev_layer.outputs @@ -1296,7 +1469,8 @@ def deconv2d_bilinear_upsampling_initializer(shape): raise Exception('deconv2d_bilinear_upsampling_initializer only supports symmetrical filter sizes') if shape[3] < shape[2]: raise Exception( - 'deconv2d_bilinear_upsampling_initializer behaviour is not defined for num_in_channels < num_out_channels ') + 'deconv2d_bilinear_upsampling_initializer behaviour is not defined for num_in_channels < num_out_channels ' + ) filter_size = shape[0] num_out_channels = shape[2] @@ -1374,24 +1548,34 @@ class Conv1d(Layer): """ @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release - def __init__(self, - prev_layer, - n_filter=32, - filter_size=5, - stride=1, - dilation_rate=1, - act=tf.identity, - padding='SAME', - data_format="channels_last", - W_init=tf.truncated_normal_initializer(stddev=0.02), - b_init=tf.constant_initializer(value=0.0), - W_init_args=None, - b_init_args=None, - name='conv1d'): + def __init__( + self, + prev_layer, + n_filter=32, + filter_size=5, + stride=1, + dilation_rate=1, + act=tf.identity, + padding='SAME', + data_format="channels_last", + W_init=tf.truncated_normal_initializer(stddev=0.02), + b_init=tf.constant_initializer(value=0.0), + W_init_args=None, + b_init_args=None, + name='conv1d' + ): super(Conv1d, self).__init__(prev_layer=prev_layer, name=name) - logging.info("Conv1d %s: n_filter:%d filter_size:%s stride:%d pad:%s act:%s dilation_rate:%d" % - (name, n_filter, filter_size, stride, padding, act.__name__, dilation_rate)) + logging.info( + "Conv1d %s: n_filter:%d filter_size:%s stride:%d pad:%s act:%s dilation_rate:%d" % + (name, + n_filter, + filter_size, + stride, + padding, + act.__name__, + dilation_rate) + ) self.inputs = prev_layer.outputs if tf.__version__ > '1.3': @@ -1406,7 +1590,8 @@ def __init__(self, use_bias=(True if b_init else False), kernel_initializer=W_init, bias_initializer=b_init, - name=name) + name=name + ) # con1d.dtype = LayersConfig.tf_dtype # unsupport, it will use the same dtype of inputs self.outputs = con1d(self.inputs) new_variables = con1d.weights # new_variables = tf.get_collection(TF_GRAPHKEYS_VARIABLES, scope=vs.name) @@ -1547,10 +1732,15 @@ def __init__( act = tf.identity if tf.__version__ > '1.5': - logging.info("Conv2d %s: n_filter:%d filter_size:%s strides:%s pad:%s act:%s" % (self.name, n_filter, - str(filter_size), - str(strides), padding, - act.__name__)) + logging.info( + "Conv2d %s: n_filter:%d filter_size:%s strides:%s pad:%s act:%s" % + (self.name, + n_filter, + str(filter_size), + str(strides), + padding, + act.__name__) + ) # with tf.variable_scope(name) as vs: conv2d = tf.layers.Conv2D( # inputs=self.inputs, @@ -1588,19 +1778,30 @@ def __init__( shape = (filter_size[0], filter_size[1], pre_channel, n_filter) # 32 features for each 5x5 patch strides = (1, strides[0], strides[1], 1) - logging.info("Conv2d %s: shape:%s strides:%s pad:%s act:%s" % (self.name, str(shape), str(strides), padding, - act.__name__)) + logging.info( + "Conv2d %s: shape:%s strides:%s pad:%s act:%s" % + (self.name, + str(shape), + str(strides), + padding, act.__name__) + ) with tf.variable_scope(name): W = tf.get_variable( - name='W_conv2d', shape=shape, initializer=W_init, dtype=LayersConfig.tf_dtype, **W_init_args) + name='W_conv2d', + shape=shape, + initializer=W_init, + dtype=LayersConfig.tf_dtype, + **W_init_args + ) if b_init: b = tf.get_variable( name='b_conv2d', shape=(shape[-1]), initializer=b_init, dtype=LayersConfig.tf_dtype, - **b_init_args) + **b_init_args + ) self.outputs = act( tf.nn.conv2d( self.inputs, @@ -1608,7 +1809,9 @@ def __init__( strides=strides, padding=padding, use_cudnn_on_gpu=use_cudnn_on_gpu, - data_format=data_format) + b) + data_format=data_format + ) + b + ) else: self.outputs = act( tf.nn.conv2d( @@ -1617,7 +1820,9 @@ def __init__( strides=strides, padding=padding, use_cudnn_on_gpu=use_cudnn_on_gpu, - data_format=data_format)) + data_format=data_format + ) + ) self.all_layers.append(self.outputs) if b_init: @@ -1662,8 +1867,10 @@ class DeConv2d(Layer): """ @deprecated_alias( - layer='prev_layer', n_out_channel='n_filter', - end_support_version=1.9) # TODO remove this line for the 1.9 release + layer='prev_layer', + n_out_channel='n_filter', + end_support_version=1.9 + ) # TODO remove this line for the 1.9 release def __init__( self, prev_layer, @@ -1678,10 +1885,16 @@ def __init__( b_init=tf.constant_initializer(value=0.0), W_init_args=None, # remove b_init_args=None, # remove - name='decnn2d'): + name='decnn2d' + ): super(DeConv2d, self).__init__(prev_layer=prev_layer, name=name) - logging.info("DeConv2d %s: n_filters:%s strides:%s pad:%s act:%s" % (name, str(n_filter), str(strides), padding, - act.__name__)) + logging.info( + "DeConv2d %s: n_filters:%s strides:%s pad:%s act:%s" % + (name, + str(n_filter), + str(strides), + padding, act.__name__) + ) if W_init_args is None: W_init_args = {} @@ -1704,7 +1917,8 @@ def __init__( activation=act, kernel_initializer=W_init, bias_initializer=b_init, - name=name) + name=name + ) self.outputs = conv2d_transpose(self.inputs) new_variables = conv2d_transpose.weights # new_variables = tf.get_collection(TF_GRAPHKEYS_VARIABLES, scope=vs.name) self.all_layers.append(self.outputs) @@ -1760,20 +1974,27 @@ class DeConv3d(Layer): """ @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release - def __init__(self, - prev_layer, - n_filter=32, - filter_size=(3, 3, 3), - strides=(2, 2, 2), - padding='SAME', - act=tf.identity, - W_init=tf.truncated_normal_initializer(stddev=0.02), - b_init=tf.constant_initializer(value=0.0), - name='decnn3d'): + def __init__( + self, + prev_layer, + n_filter=32, + filter_size=(3, 3, 3), + strides=(2, 2, 2), + padding='SAME', + act=tf.identity, + W_init=tf.truncated_normal_initializer(stddev=0.02), + b_init=tf.constant_initializer(value=0.0), + name='decnn3d' + ): super(DeConv3d, self).__init__(prev_layer=prev_layer, name=name) - logging.info("DeConv3d %s: n_filters:%s strides:%s pad:%s act:%s" % (name, str(n_filter), str(strides), padding, - act.__name__)) + logging.info( + "DeConv3d %s: n_filters:%s strides:%s pad:%s act:%s" % + (name, + str(n_filter), + str(strides), + padding, act.__name__) + ) self.inputs = prev_layer.outputs @@ -1870,8 +2091,13 @@ def __init__( name='depthwise_conv2d', ): super(DepthwiseConv2d, self).__init__(prev_layer=prev_layer, name=name) - logging.info("DepthwiseConv2d %s: shape:%s strides:%s pad:%s act:%s" % (name, str(shape), str(strides), padding, - act.__name__)) + logging.info( + "DepthwiseConv2d %s: shape:%s strides:%s pad:%s act:%s" % + (name, + str(shape), + str(strides), + padding, act.__name__) + ) self.inputs = prev_layer.outputs @@ -1897,20 +2123,31 @@ def __init__( with tf.variable_scope(name): W = tf.get_variable( - name='W_depthwise2d', shape=shape, initializer=W_init, dtype=LayersConfig.tf_dtype, - **W_init_args) # [filter_height, filter_width, in_channels, depth_multiplier] + name='W_depthwise2d', + shape=shape, + initializer=W_init, + dtype=LayersConfig.tf_dtype, + **W_init_args + ) # [filter_height, filter_width, in_channels, depth_multiplier] if b_init: b = tf.get_variable( name='b_depthwise2d', shape=(pre_channel * depth_multiplier), initializer=b_init, dtype=LayersConfig.tf_dtype, - **b_init_args) + **b_init_args + ) self.outputs = act( - tf.nn.depthwise_conv2d(self.inputs, W, strides=strides, padding=padding, rate=dilation_rate) + b) + tf.nn.depthwise_conv2d(self.inputs, W, strides=strides, padding=padding, rate=dilation_rate) + b + ) else: self.outputs = act( - tf.nn.depthwise_conv2d(self.inputs, W, strides=strides, padding=padding, rate=dilation_rate)) + tf.nn.depthwise_conv2d(self.inputs, + W, + strides=strides, + padding=padding, + rate=dilation_rate) + ) # self.all_layers = list(layer.all_layers) # self.all_params = list(layer.all_params) @@ -1992,8 +2229,15 @@ def __init__( # b_init_args = {} super(SeparableConv2d, self).__init__(prev_layer=prev_layer, name=name) - logging.info("SeparableConv2d %s: n_filter:%d filter_size:%s filter_size:%s depth_multiplier:%d act:%s" % - (self.name, n_filter, str(filter_size), str(strides), depth_multiplier, act.__name__)) + logging.info( + "SeparableConv2d %s: n_filter:%d filter_size:%s filter_size:%s depth_multiplier:%d act:%s" % + (self.name, + n_filter, + str(filter_size), + str(strides), + depth_multiplier, + act.__name__) + ) self.inputs = prev_layer.outputs @@ -2020,7 +2264,8 @@ def __init__( # pointwise_constraint=None, # bias_constraint=None, trainable=True, - name=None) + name=None + ) new_variables = tf.get_collection(TF_GRAPHKEYS_VARIABLES, scope=vs.name) self.all_layers.append(self.outputs) @@ -2076,8 +2321,16 @@ def __init__( ): # Windaway super(GroupConv2d, self).__init__(prev_layer=prev_layer, name=name) - logging.info("GroupConv2d %s: n_filter:%d size:%s strides:%s n_group:%d pad:%s act:%s" % - (name, n_filter, str(filter_size), str(strides), n_group, padding, act.__name__)) + logging.info( + "GroupConv2d %s: n_filter:%d size:%s strides:%s n_group:%d pad:%s act:%s" % + (name, + n_filter, + str(filter_size), + str(strides), + n_group, + padding, + act.__name__) + ) self.inputs = prev_layer.outputs @@ -2096,7 +2349,8 @@ def __init__( initializer=W_init, dtype=LayersConfig.tf_dtype, trainable=True, - **W_init_args) + **W_init_args + ) if b_init: bi = tf.get_variable( name='b', @@ -2104,7 +2358,8 @@ def __init__( initializer=b_init, dtype=LayersConfig.tf_dtype, trainable=True, - **b_init_args) + **b_init_args + ) if n_group == 1: conv = groupConv(self.inputs, We) else: diff --git a/tensorlayer/layers/core.py b/tensorlayer/layers/core.py index 05697866b..994fef763 100644 --- a/tensorlayer/layers/core.py +++ b/tensorlayer/layers/core.py @@ -430,7 +430,15 @@ def print_params(self, details=True, session=None): val = p.eval(session=session) logging.info( " param {:3}: {:20} {:15} {} (mean: {:<18}, median: {:<18}, std: {:<18}) ".format( - i, p.name, str(val.shape), p.dtype.name, val.mean(), np.median(val), val.std())) + i, + p.name, + str(val.shape), + p.dtype.name, + val.mean(), + np.median(val), + val.std() + ) + ) except Exception as e: logging.info(str(e)) raise Exception( @@ -444,8 +452,12 @@ def print_layers(self): """Print all info of layers in the network""" for i, layer in enumerate(self.all_layers): # logging.info(" layer %d: %s" % (i, str(layer))) - logging.info(" layer {:3}: {:20} {:15} {}".format(i, layer.name, str(layer.get_shape()), - layer.dtype.name)) + logging.info( + " layer {:3}: {:20} {:15} {}".format(i, + layer.name, + str(layer.get_shape()), + layer.dtype.name) + ) def count_params(self): """Return the number of parameters in the network""" @@ -688,7 +700,8 @@ def __init__( shape=(vocabulary_size, embedding_size), initializer=E_init, dtype=LayersConfig.tf_dtype, - **E_init_args) + **E_init_args + ) embed = tf.nn.embedding_lookup(embeddings, self.inputs) # Construct the variables for the NCE loss (i.e. negative sampling) nce_weights = tf.get_variable( @@ -696,13 +709,15 @@ def __init__( shape=(vocabulary_size, embedding_size), initializer=nce_W_init, dtype=LayersConfig.tf_dtype, - **nce_W_init_args) + **nce_W_init_args + ) nce_biases = tf.get_variable( name='nce_biases', shape=(vocabulary_size), initializer=nce_b_init, dtype=LayersConfig.tf_dtype, - **nce_b_init_args) + **nce_b_init_args + ) # Compute the average NCE loss for the batch. # tf.nce_loss automatically draws a new sample of the negative labels @@ -715,7 +730,9 @@ def __init__( labels=train_labels, num_sampled=num_sampled, num_classes=vocabulary_size, - **nce_loss_args)) + **nce_loss_args + ) + ) self.outputs = embed self.normalized_embeddings = tf.nn.l2_normalize(embeddings, 1) @@ -786,7 +803,8 @@ def __init__( shape=(vocabulary_size, embedding_size), initializer=E_init, dtype=LayersConfig.tf_dtype, - **E_init_args) + **E_init_args + ) embed = tf.nn.embedding_lookup(embeddings, self.inputs) self.outputs = embed @@ -892,7 +910,8 @@ def __init__( sentence_embeddings = tf.divide( sum_word_embeddings, sentence_lengths + 1e-8, # Add epsilon to avoid dividing by 0 - name='sentence_embeddings') + name='sentence_embeddings' + ) self.outputs = sentence_embeddings self.all_layers = [self.outputs] @@ -973,11 +992,21 @@ def __init__( with tf.variable_scope(name): W = tf.get_variable( - name='W', shape=(n_in, n_units), initializer=W_init, dtype=LayersConfig.tf_dtype, **W_init_args) + name='W', + shape=(n_in, n_units), + initializer=W_init, + dtype=LayersConfig.tf_dtype, + **W_init_args + ) if b_init is not None: try: b = tf.get_variable( - name='b', shape=(n_units), initializer=b_init, dtype=LayersConfig.tf_dtype, **b_init_args) + name='b', + shape=(n_units), + initializer=b_init, + dtype=LayersConfig.tf_dtype, + **b_init_args + ) except Exception: # If initializer is a constant, do not specify shape. b = tf.get_variable(name='b', initializer=b_init, dtype=LayersConfig.tf_dtype, **b_init_args) self.outputs = act(tf.matmul(self.inputs, W) + b) @@ -1088,7 +1117,8 @@ def __init__( # L1 of activation outputs activation_out = self.all_layers[-2] L1_a = 0.001 * tf.reduce_mean( - activation_out) # : theano: T.mean( self.a[i] ) # some neuron are broken, white and black + activation_out + ) # : theano: T.mean( self.a[i] ) # some neuron are broken, white and black # L1_a = 0.001 * tf.reduce_mean( tf.reduce_sum(activation_out, 0) ) # : some neuron are broken, white and black # L1_a = 0.001 * 100 * tf.reduce_mean( tf.reduce_sum(activation_out, 1) ) # : some neuron are broken, white and black # KL Divergence @@ -1096,11 +1126,13 @@ def __init__( rho = 0.15 p_hat = tf.reduce_mean(activation_out, 0) # theano: p_hat = T.mean( self.a[i], axis=0 ) try: # TF1.0 - KLD = beta * tf.reduce_sum(rho * tf.log(tf.divide(rho, p_hat)) + - (1 - rho) * tf.log((1 - rho) / (tf.subtract(float(1), p_hat)))) + KLD = beta * tf.reduce_sum( + rho * tf.log(tf.divide(rho, p_hat)) + (1 - rho) * tf.log((1 - rho) / (tf.subtract(float(1), p_hat))) + ) except Exception: # TF0.12 - KLD = beta * tf.reduce_sum(rho * tf.log(tf.div(rho, p_hat)) + - (1 - rho) * tf.log((1 - rho) / (tf.sub(float(1), p_hat)))) + KLD = beta * tf.reduce_sum( + rho * tf.log(tf.div(rho, p_hat)) + (1 - rho) * tf.log((1 - rho) / (tf.sub(float(1), p_hat))) + ) # KLD = beta * tf.reduce_sum( rho * tf.log(rho/ p_hat) + (1- rho) * tf.log((1- rho)/(1- p_hat)) ) # theano: L1_a = l1_a[i] * T.sum( rho[i] * T.log(rho[i]/ p_hat) + (1- rho[i]) * T.log((1- rho[i])/(1- p_hat)) ) # Total cost @@ -1129,21 +1161,29 @@ def __init__( raise Exception("Don't support the given reconstruct activation function") self.train_op = tf.train.AdamOptimizer( - learning_rate, beta1=0.9, beta2=0.999, epsilon=1e-08, use_locking=False).minimize( - self.cost, var_list=self.train_params) + learning_rate, + beta1=0.9, + beta2=0.999, + epsilon=1e-08, + use_locking=False + ).minimize( + self.cost, var_list=self.train_params + ) # self.train_op = tf.train.GradientDescentOptimizer(1.0).minimize(self.cost, var_list=self.train_params) - def pretrain(self, - sess, - x, - X_train, - X_val, - denoise_name=None, - n_epoch=100, - batch_size=128, - print_freq=10, - save=True, - save_name='w1pre_'): + def pretrain( + self, + sess, + x, + X_train, + X_val, + denoise_name=None, + n_epoch=100, + batch_size=128, + print_freq=10, + save=True, + save_name='w1pre_' + ): # ==================================================== # # You need to modify the cost function in __init__() so as to @@ -1196,7 +1236,8 @@ def pretrain(self, saveable=True, shape=[28, 28], name=save_name + str(epoch + 1), - fig_idx=2012) + fig_idx=2012 + ) files.save_npz([self.all_params[0]], name=save_name + str(epoch + 1) + '.npz') except Exception: raise Exception( @@ -1452,9 +1493,19 @@ def __init__( with tf.variable_scope(name): W = tf.get_variable( - name='W', shape=(n_in, n_units), initializer=W_init, dtype=LayersConfig.tf_dtype, **W_init_args) + name='W', + shape=(n_in, n_units), + initializer=W_init, + dtype=LayersConfig.tf_dtype, + **W_init_args + ) b = tf.get_variable( - name='b', shape=(n_units), initializer=b_init, dtype=LayersConfig.tf_dtype, **b_init_args) + name='b', + shape=(n_units), + initializer=b_init, + dtype=LayersConfig.tf_dtype, + **b_init_args + ) # self.outputs = act(tf.matmul(self.inputs, W) + b) LayersConfig.set_keep[name] = tf.placeholder(tf.float32) diff --git a/tensorlayer/layers/importer.py b/tensorlayer/layers/importer.py index 3951b01ed..632b59d99 100644 --- a/tensorlayer/layers/importer.py +++ b/tensorlayer/layers/importer.py @@ -138,7 +138,8 @@ def __init__( if slim_variables == []: logging.info( "No variables found under %s : the name of SlimNetsLayer should be matched with the begining of the ckpt file, see tutorial_inceptionV3_tfslim.py for more details" - % name) + % name + ) self.outputs = net diff --git a/tensorlayer/layers/merge.py b/tensorlayer/layers/merge.py index e5399603b..9837d4b0f 100644 --- a/tensorlayer/layers/merge.py +++ b/tensorlayer/layers/merge.py @@ -124,8 +124,9 @@ def __init__( ): super(ElementwiseLayer, self).__init__(prev_layer=layers, name=name) - logging.info("ElementwiseLayer %s: size:%s fn:%s" % (self.name, layers[0].outputs.get_shape(), - combine_fn.__name__)) + logging.info( + "ElementwiseLayer %s: size:%s fn:%s" % (self.name, layers[0].outputs.get_shape(), combine_fn.__name__) + ) self.outputs = layers[0].outputs diff --git a/tensorlayer/layers/normalization.py b/tensorlayer/layers/normalization.py index b8e0a6a0b..b4808d61e 100644 --- a/tensorlayer/layers/normalization.py +++ b/tensorlayer/layers/normalization.py @@ -49,11 +49,14 @@ def __init__( name='lrn_layer', ): super(LocalResponseNormLayer, self).__init__(prev_layer=prev_layer, name=name) - logging.info("LocalResponseNormLayer %s: depth_radius: %s, bias: %s, alpha: %s, beta: %s" % (name, - str(depth_radius), - str(bias), - str(alpha), - str(beta))) + logging.info( + "LocalResponseNormLayer %s: depth_radius: %s, bias: %s, alpha: %s, beta: %s" % + (name, + str(depth_radius), + str(bias), + str(alpha), + str(beta)) + ) self.inputs = prev_layer.outputs @@ -114,8 +117,14 @@ def __init__( name='batchnorm_layer', ): super(BatchNormLayer, self).__init__(prev_layer=prev_layer, name=name) - logging.info("BatchNormLayer %s: decay:%f epsilon:%f act:%s is_train:%s" % (name, decay, epsilon, act.__name__, - is_train)) + logging.info( + "BatchNormLayer %s: decay:%f epsilon:%f act:%s is_train:%s" % + (name, + decay, + epsilon, + act.__name__, + is_train) + ) self.inputs = prev_layer.outputs @@ -132,7 +141,12 @@ def __init__( if tf.__version__ > '0.12.1' and beta_init == tf.zeros_initializer: beta_init = beta_init() beta = tf.get_variable( - 'beta', shape=params_shape, initializer=beta_init, dtype=LayersConfig.tf_dtype, trainable=is_train) + 'beta', + shape=params_shape, + initializer=beta_init, + dtype=LayersConfig.tf_dtype, + trainable=is_train + ) variables.append(beta) else: beta = None @@ -155,7 +169,12 @@ def __init__( else: moving_mean_init = tf.zeros_initializer moving_mean = tf.get_variable( - 'moving_mean', params_shape, initializer=moving_mean_init, dtype=LayersConfig.tf_dtype, trainable=False) + 'moving_mean', + params_shape, + initializer=moving_mean_init, + dtype=LayersConfig.tf_dtype, + trainable=False + ) moving_variance = tf.get_variable( 'moving_variance', params_shape, @@ -169,9 +188,17 @@ def __init__( mean, variance = tf.nn.moments(self.inputs, axis) try: # TF12 update_moving_mean = moving_averages.assign_moving_average( - moving_mean, mean, decay, zero_debias=False) # if zero_debias=True, has bias + moving_mean, + mean, + decay, + zero_debias=False + ) # if zero_debias=True, has bias update_moving_variance = moving_averages.assign_moving_average( - moving_variance, variance, decay, zero_debias=False) # if zero_debias=True, has bias + moving_variance, + variance, + decay, + zero_debias=False + ) # if zero_debias=True, has bias # logging.info("TF12 moving") except Exception: # TF11 update_moving_mean = moving_averages.assign_moving_average(moving_mean, mean, decay) @@ -187,7 +214,13 @@ def mean_var_with_update(): self.outputs = act(tf.nn.batch_normalization(self.inputs, mean, var, beta, gamma, epsilon)) else: self.outputs = act( - tf.nn.batch_normalization(self.inputs, moving_mean, moving_variance, beta, gamma, epsilon)) + tf.nn.batch_normalization(self.inputs, + moving_mean, + moving_variance, + beta, + gamma, + epsilon) + ) variables.extend([moving_mean, moving_variance]) @@ -237,11 +270,13 @@ def __init__( scale = tf.get_variable( 'scale', [self.inputs.get_shape()[-1]], initializer=tf.truncated_normal_initializer(mean=1.0, stddev=0.02), - dtype=LayersConfig.tf_dtype) + dtype=LayersConfig.tf_dtype + ) offset = tf.get_variable( 'offset', [self.inputs.get_shape()[-1]], initializer=tf.constant_initializer(0.0), - dtype=LayersConfig.tf_dtype) + dtype=LayersConfig.tf_dtype + ) self.outputs = scale * tf.div(self.inputs - mean, tf.sqrt(var + epsilon)) + offset self.outputs = act(self.outputs) variables = tf.get_collection(TF_GRAPHKEYS_VARIABLES, scope=vs.name) @@ -269,18 +304,20 @@ class LayerNormLayer(Layer): """ @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release - def __init__(self, - prev_layer, - center=True, - scale=True, - act=tf.identity, - reuse=None, - variables_collections=None, - outputs_collections=None, - trainable=True, - begin_norm_axis=1, - begin_params_axis=-1, - name='layernorm'): + def __init__( + self, + prev_layer, + center=True, + scale=True, + act=tf.identity, + reuse=None, + variables_collections=None, + outputs_collections=None, + trainable=True, + begin_norm_axis=1, + begin_params_axis=-1, + name='layernorm' + ): super(LayerNormLayer, self).__init__(prev_layer=prev_layer, name=name) logging.info("LayerNormLayer %s: act:%s" % (name, act.__name__)) diff --git a/tensorlayer/layers/padding.py b/tensorlayer/layers/padding.py index 12cfcecdc..64314c589 100644 --- a/tensorlayer/layers/padding.py +++ b/tensorlayer/layers/padding.py @@ -52,7 +52,8 @@ def __init__( if padding is None: raise Exception( - "padding should be a Tensor of type int32. see https://www.tensorflow.org/api_docs/python/tf/pad") + "padding should be a Tensor of type int32. see https://www.tensorflow.org/api_docs/python/tf/pad" + ) self.outputs = tf.pad(self.inputs, paddings=padding, mode=mode, name=name) self.all_layers.append(self.outputs) diff --git a/tensorlayer/layers/pooling.py b/tensorlayer/layers/pooling.py index 5b70570ea..dce062eb4 100644 --- a/tensorlayer/layers/pooling.py +++ b/tensorlayer/layers/pooling.py @@ -67,8 +67,13 @@ def __init__( name='pool_layer', ): super(PoolLayer, self).__init__(prev_layer=prev_layer, name=name) - logging.info("PoolLayer %s: ksize:%s strides:%s padding:%s pool:%s" % (name, str(ksize), str(strides), - padding, pool.__name__)) + logging.info( + "PoolLayer %s: ksize:%s strides:%s padding:%s pool:%s" % + (name, + str(ksize), + str(strides), + padding, pool.__name__) + ) self.inputs = prev_layer.outputs @@ -104,10 +109,20 @@ def maxpool1d(prev_layer, filter_size=3, strides=2, padding='valid', data_format A max pooling 1-D layer with a output rank as 3. """ - logging.info("MaxPool1d %s: filter_size:%s strides:%s padding:%s" % (name, str(filter_size), str(strides), - str(padding))) + logging.info( + "MaxPool1d %s: filter_size:%s strides:%s padding:%s" % (name, + str(filter_size), + str(strides), + str(padding)) + ) outputs = tf.layers.max_pooling1d( - prev_layer.outputs, filter_size, strides, padding=padding, data_format=data_format, name=name) + prev_layer.outputs, + filter_size, + strides, + padding=padding, + data_format=data_format, + name=name + ) net_new = copy.copy(prev_layer) net_new.outputs = outputs @@ -143,10 +158,20 @@ def meanpool1d(prev_layer, filter_size=3, strides=2, padding='valid', data_forma A mean pooling 1-D layer with a output rank as 3. """ - logging.info("MeanPool1d %s: filter_size:%s strides:%s padding:%s" % (name, str(filter_size), str(strides), - str(padding))) + logging.info( + "MeanPool1d %s: filter_size:%s strides:%s padding:%s" % (name, + str(filter_size), + str(strides), + str(padding)) + ) outputs = tf.layers.average_pooling1d( - prev_layer.outputs, filter_size, strides, padding=padding, data_format=data_format, name=name) + prev_layer.outputs, + filter_size, + strides, + padding=padding, + data_format=data_format, + name=name + ) net_new = copy.copy(prev_layer) net_new.outputs = outputs @@ -181,7 +206,13 @@ def maxpool2d(prev_layer, filter_size=(3, 3), strides=(2, 2), padding='SAME', na strides = filter_size if tf.__version__ > '1.5': outputs = tf.layers.max_pooling2d( - prev_layer.outputs, filter_size, strides, padding=padding, data_format='channels_last', name=name) + prev_layer.outputs, + filter_size, + strides, + padding=padding, + data_format='channels_last', + name=name + ) net_new = copy.copy(prev_layer) net_new.outputs = outputs net_new.all_layers.extend([outputs]) @@ -196,7 +227,8 @@ def maxpool2d(prev_layer, filter_size=(3, 3), strides=(2, 2), padding='SAME', na strides=[1, strides[0], strides[1], 1], padding=padding, pool=tf.nn.max_pool, - name=name) + name=name + ) return prev_layer @@ -228,7 +260,13 @@ def meanpool2d(prev_layer, filter_size=(3, 3), strides=(2, 2), padding='SAME', n strides = filter_size if tf.__version__ > '1.5': outputs = tf.layers.average_pooling2d( - prev_layer.outputs, filter_size, strides, padding=padding, data_format='channels_last', name=name) + prev_layer.outputs, + filter_size, + strides, + padding=padding, + data_format='channels_last', + name=name + ) net_new = copy.copy(prev_layer) net_new.outputs = outputs net_new.all_layers.extend([outputs]) @@ -243,7 +281,8 @@ def meanpool2d(prev_layer, filter_size=(3, 3), strides=(2, 2), padding='SAME', n strides=[1, strides[0], strides[1], 1], padding=padding, pool=tf.nn.avg_pool, - name=name) + name=name + ) return prev_layer @@ -277,22 +316,34 @@ class MaxPool3d(Layer): """ @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release - def __init__(self, - prev_layer, - filter_size=(3, 3, 3), - strides=(2, 2, 2), - padding='valid', - data_format='channels_last', - name='maxpool3d'): + def __init__( + self, + prev_layer, + filter_size=(3, 3, 3), + strides=(2, 2, 2), + padding='valid', + data_format='channels_last', + name='maxpool3d' + ): super(MaxPool3d, self).__init__(prev_layer=prev_layer, name=name) - logging.info("MaxPool3d %s: filter_size:%s strides:%s padding:%s" % (name, str(filter_size), str(strides), - str(padding))) + logging.info( + "MaxPool3d %s: filter_size:%s strides:%s padding:%s" % (name, + str(filter_size), + str(strides), + str(padding)) + ) self.inputs = prev_layer.outputs self.outputs = tf.layers.max_pooling3d( - prev_layer.outputs, filter_size, strides, padding=padding, data_format=data_format, name=name) + prev_layer.outputs, + filter_size, + strides, + padding=padding, + data_format=data_format, + name=name + ) # update layer (customized) self.all_layers.append(self.outputs) @@ -327,23 +378,36 @@ class MeanPool3d(Layer): """ @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release - def __init__(self, - prev_layer, - filter_size=(3, 3, 3), - strides=(2, 2, 2), - padding='valid', - data_format='channels_last', - name='meanpool3d'): + def __init__( + self, + prev_layer, + filter_size=(3, 3, 3), + strides=(2, 2, 2), + padding='valid', + data_format='channels_last', + name='meanpool3d' + ): super(MeanPool3d, self).__init__(prev_layer=prev_layer, name=name) - logging.info("MeanPool3d %s: filter_size:%s strides:%s padding:%s" % (name, str(filter_size), str(strides), - str(padding))) + logging.info( + "MeanPool3d %s: filter_size:%s strides:%s padding:%s" % + (name, + str(filter_size), + str(strides), + str(padding)) + ) self.inputs = prev_layer.outputs # operation (customized) self.outputs = tf.layers.average_pooling3d( - prev_layer.outputs, filter_size, strides, padding=padding, data_format=data_format, name=name) + prev_layer.outputs, + filter_size, + strides, + padding=padding, + data_format=data_format, + name=name + ) # update layer (customized) self.all_layers.append(self.outputs) diff --git a/tensorlayer/layers/recurrent.py b/tensorlayer/layers/recurrent.py index f283fd358..94dde3a22 100644 --- a/tensorlayer/layers/recurrent.py +++ b/tensorlayer/layers/recurrent.py @@ -160,9 +160,15 @@ def __init__( except Exception: logging.warning('pop state_is_tuple fails.') - logging.info("RNNLayer %s: n_hidden:%d n_steps:%d in_dim:%d in_shape:%s cell_fn:%s " % - (self.name, n_hidden, n_steps, self.inputs.get_shape().ndims, self.inputs.get_shape(), - cell_fn.__name__)) + logging.info( + "RNNLayer %s: n_hidden:%d n_steps:%d in_dim:%d in_shape:%s cell_fn:%s " % + (self.name, + n_hidden, + n_steps, + self.inputs.get_shape().ndims, + self.inputs.get_shape(), + cell_fn.__name__) + ) # You can get the dimension by .get_shape() or ._shape, and check the # dimension by .with_rank() as follow. # self.inputs.get_shape().with_rank(2) @@ -352,9 +358,18 @@ def __init__( except Exception: logging.warning("pop state_is_tuple fails.") - logging.info("BiRNNLayer %s: n_hidden:%d n_steps:%d in_dim:%d in_shape:%s cell_fn:%s dropout:%s n_layer:%d " % - (self.name, n_hidden, n_steps, self.inputs.get_shape().ndims, self.inputs.get_shape(), - cell_fn.__name__, dropout, n_layer)) + logging.info( + "BiRNNLayer %s: n_hidden:%d n_steps:%d in_dim:%d in_shape:%s cell_fn:%s dropout:%s n_layer:%d " % ( + self.name, + n_hidden, + n_steps, + self.inputs.get_shape().ndims, + self.inputs.get_shape(), + cell_fn.__name__, + dropout, + n_layer + ) + ) fixed_batch_size = self.inputs.get_shape().with_rank_at_least(1)[0] @@ -404,10 +419,10 @@ def __init__( MultiRNNCell_fn = tf.nn.rnn_cell.MultiRNNCell if dropout: try: - self.fw_cell = MultiRNNCell_fn( - [cell_creator(is_last=i == n_layer - 1) for i in range(n_layer)], state_is_tuple=True) - self.bw_cell = MultiRNNCell_fn( - [cell_creator(is_last=i == n_layer - 1) for i in range(n_layer)], state_is_tuple=True) + self.fw_cell = MultiRNNCell_fn([cell_creator(is_last=i == n_layer - 1) for i in range(n_layer)], + state_is_tuple=True) + self.bw_cell = MultiRNNCell_fn([cell_creator(is_last=i == n_layer - 1) for i in range(n_layer)], + state_is_tuple=True) except Exception: self.fw_cell = MultiRNNCell_fn([cell_creator(is_last=i == n_layer - 1) for i in range(n_layer)]) self.bw_cell = MultiRNNCell_fn([cell_creator(is_last=i == n_layer - 1) for i in range(n_layer)]) @@ -422,12 +437,14 @@ def __init__( # Initial state of RNN if fw_initial_state is None: self.fw_initial_state = self.fw_cell.zero_state( - self.batch_size, dtype=LayersConfig.tf_dtype) # dtype=tf.float32) + self.batch_size, dtype=LayersConfig.tf_dtype + ) # dtype=tf.float32) else: self.fw_initial_state = fw_initial_state if bw_initial_state is None: self.bw_initial_state = self.bw_cell.zero_state( - self.batch_size, dtype=LayersConfig.tf_dtype) # dtype=tf.float32) + self.batch_size, dtype=LayersConfig.tf_dtype + ) # dtype=tf.float32) else: self.bw_initial_state = bw_initial_state # exit() @@ -538,14 +555,16 @@ class BasicConvLSTMCell(ConvRNNCell): """ - def __init__(self, - shape, - filter_size, - num_features, - forget_bias=1.0, - input_size=None, - state_is_tuple=False, - act=tf.nn.tanh): + def __init__( + self, + shape, + filter_size, + num_features, + forget_bias=1.0, + input_size=None, + state_is_tuple=False, + act=tf.nn.tanh + ): """Initialize the basic Conv LSTM cell.""" # if not state_is_tuple: # logging.warn("%s: Using a concatenated state is slower and will soon be " @@ -636,7 +655,10 @@ def _conv_linear(args, filter_size, num_features, bias, bias_start=0.0, scope=No # Now the computation. with tf.variable_scope(scope or "Conv"): matrix = tf.get_variable( - "Matrix", [filter_size[0], filter_size[1], total_arg_size_depth, num_features], dtype=dtype) + "Matrix", + [filter_size[0], filter_size[1], total_arg_size_depth, num_features], + dtype=dtype + ) if len(args) == 1: res = tf.nn.conv2d(args[0], matrix, strides=[1, 1, 1, 1], padding='SAME') else: @@ -644,7 +666,11 @@ def _conv_linear(args, filter_size, num_features, bias, bias_start=0.0, scope=No if not bias: return res bias_term = tf.get_variable( - "Bias", [num_features], dtype=dtype, initializer=tf.constant_initializer(bias_start, dtype=dtype)) + "Bias", + [num_features], + dtype=dtype, + initializer=tf.constant_initializer(bias_start, dtype=dtype) + ) return res + bias_term @@ -723,9 +749,16 @@ def __init__( self.inputs = prev_layer.outputs - logging.info("ConvLSTMLayer %s: feature_map:%d, n_steps:%d, " - "in_dim:%d %s, cell_fn:%s " % (self.name, feature_map, n_steps, self.inputs.get_shape().ndims, - self.inputs.get_shape(), cell_fn.__name__)) + logging.info( + "ConvLSTMLayer %s: feature_map:%d, n_steps:%d, " + "in_dim:%d %s, cell_fn:%s " % + (self.name, + feature_map, + n_steps, + self.inputs.get_shape().ndims, + self.inputs.get_shape(), + cell_fn.__name__) + ) # You can get the dimension by .get_shape() or ._shape, and check the # dimension by .with_rank() as follow. # self.inputs.get_shape().with_rank(2) @@ -735,8 +768,10 @@ def __init__( try: self.inputs.get_shape().with_rank(5) except Exception: - raise Exception("RNN : Input dimension should be rank 5 : [batch_size, n_steps, input_x, " - "input_y, feature_map]") + raise Exception( + "RNN : Input dimension should be rank 5 : [batch_size, n_steps, input_x, " + "input_y, feature_map]" + ) fixed_batch_size = self.inputs.get_shape().with_rank_at_least(1)[0] @@ -778,7 +813,9 @@ def __init__( # : stack more RNN layer after that # 5D Tensor [n_example/n_steps, n_steps, h, w, c] self.outputs = tf.reshape( - tf.concat(outputs, 1), [-1, n_steps, cell_shape[0], cell_shape[1], feature_map]) + tf.concat(outputs, 1), + [-1, n_steps, cell_shape[0], cell_shape[1], feature_map] + ) self.final_state = state @@ -921,8 +958,9 @@ def retrieve_seq_length_op3(data, pad_val=0): # HangSheng: return tensor for se elif data_shape_size == 1: raise ValueError("retrieve_seq_length_op3: data has wrong shape!") else: - raise ValueError("retrieve_seq_length_op3: handling data_shape_size %s hasn't been implemented!" % - (data_shape_size)) + raise ValueError( + "retrieve_seq_length_op3: handling data_shape_size %s hasn't been implemented!" % (data_shape_size) + ) def target_mask_op(data, pad_val=0): # HangSheng: return tensor for mask,if input is tf.string @@ -1075,9 +1113,17 @@ def __init__( except Exception: logging.warning("pop state_is_tuple fails.") - logging.info("DynamicRNNLayer %s: n_hidden:%d, in_dim:%d in_shape:%s cell_fn:%s dropout:%s n_layer:%d" % - (self.name, n_hidden, self.inputs.get_shape().ndims, self.inputs.get_shape(), cell_fn.__name__, - dropout, n_layer)) + logging.info( + "DynamicRNNLayer %s: n_hidden:%d, in_dim:%d in_shape:%s cell_fn:%s dropout:%s n_layer:%d" % ( + self.name, + n_hidden, + self.inputs.get_shape().ndims, + self.inputs.get_shape(), + cell_fn.__name__, + dropout, + n_layer + ) + ) # Input dimension should be rank 3 [batch_size, n_steps(max), n_features] try: @@ -1137,8 +1183,8 @@ def __init__( if dropout: try: # cell_instance_fn=lambda: MultiRNNCell_fn([cell_instance_fn2() for _ in range(n_layer)], state_is_tuple=True) # HanSheng - self.cell = MultiRNNCell_fn( - [cell_creator(is_last=i == n_layer - 1) for i in range(n_layer)], state_is_tuple=True) + self.cell = MultiRNNCell_fn([cell_creator(is_last=i == n_layer - 1) for i in range(n_layer)], + state_is_tuple=True) except Exception: # when GRU # cell_instance_fn=lambda: MultiRNNCell_fn([cell_instance_fn2() for _ in range(n_layer)]) # HanSheng self.cell = MultiRNNCell_fn([cell_creator(is_last=i == n_layer - 1) for i in range(n_layer)]) @@ -1160,10 +1206,12 @@ def __init__( if sequence_length is None: try: # TF1.0 sequence_length = retrieve_seq_length_op( - self.inputs if isinstance(self.inputs, tf.Tensor) else tf.stack(self.inputs)) + self.inputs if isinstance(self.inputs, tf.Tensor) else tf.stack(self.inputs) + ) except Exception: # TF0.12 sequence_length = retrieve_seq_length_op( - self.inputs if isinstance(self.inputs, tf.Tensor) else tf.pack(self.inputs)) + self.inputs if isinstance(self.inputs, tf.Tensor) else tf.pack(self.inputs) + ) # Main - Computes outputs and last_states with tf.variable_scope(name, initializer=initializer) as vs: @@ -1174,7 +1222,8 @@ def __init__( # dtype=tf.float64, sequence_length=sequence_length, initial_state=self.initial_state, - **dynamic_rnn_init_args) + **dynamic_rnn_init_args + ) rnn_variables = tf.get_collection(TF_GRAPHKEYS_VARIABLES, scope=vs.name) # logging.info(" n_params : %d" % (len(rnn_variables))) @@ -1333,9 +1382,17 @@ def __init__( except Exception: logging.warning("pop state_is_tuple fails.") - logging.info("BiDynamicRNNLayer %s: n_hidden:%d in_dim:%d in_shape:%s cell_fn:%s dropout:%s n_layer:%d" % - (self.name, n_hidden, self.inputs.get_shape().ndims, self.inputs.get_shape(), cell_fn.__name__, - dropout, n_layer)) + logging.info( + "BiDynamicRNNLayer %s: n_hidden:%d in_dim:%d in_shape:%s cell_fn:%s dropout:%s n_layer:%d" % ( + self.name, + n_hidden, + self.inputs.get_shape().ndims, + self.inputs.get_shape(), + cell_fn.__name__, + dropout, + n_layer + ) + ) # Input dimension should be rank 3 [batch_size, n_steps(max), n_features] try: @@ -1399,10 +1456,12 @@ def __init__( if sequence_length is None: try: # TF1.0 sequence_length = retrieve_seq_length_op( - self.inputs if isinstance(self.inputs, tf.Tensor) else tf.stack(self.inputs)) + self.inputs if isinstance(self.inputs, tf.Tensor) else tf.stack(self.inputs) + ) except Exception: # TF0.12 sequence_length = retrieve_seq_length_op( - self.inputs if isinstance(self.inputs, tf.Tensor) else tf.pack(self.inputs)) + self.inputs if isinstance(self.inputs, tf.Tensor) else tf.pack(self.inputs) + ) if n_layer > 1: if dropout: @@ -1420,7 +1479,8 @@ def __init__( initial_states_fw=self.fw_initial_state, initial_states_bw=self.bw_initial_state, dtype=LayersConfig.tf_dtype, - **dynamic_rnn_init_args) + **dynamic_rnn_init_args + ) else: self.fw_cell = cell_creator() @@ -1621,8 +1681,14 @@ def __init__( except Exception: logging.warning("pop state_is_tuple fails.") # self.inputs = layer.outputs - logging.info("[*] Seq2Seq %s: n_hidden:%d cell_fn:%s dropout:%s n_layer:%d" % - (self.name, n_hidden, cell_fn.__name__, dropout, n_layer)) + logging.info( + "[*] Seq2Seq %s: n_hidden:%d cell_fn:%s dropout:%s n_layer:%d" % + (self.name, + n_hidden, + cell_fn.__name__, + dropout, + n_layer) + ) with tf.variable_scope(name): # tl.layers.set_name_reuse(reuse) @@ -1639,7 +1705,8 @@ def __init__( sequence_length=encode_sequence_length, return_last=False, return_seq_2d=True, - name='encode') + name='encode' + ) # vs.reuse_variables() # tl.layers.set_name_reuse(True) network_decode = DynamicRNNLayer( @@ -1654,7 +1721,8 @@ def __init__( sequence_length=decode_sequence_length, return_last=False, return_seq_2d=return_seq_2d, - name='decode') + name='decode' + ) self.outputs = network_decode.outputs # rnn_variables = tf.get_collection(TF_GRAPHKEYS_VARIABLES, scope=vs.name) diff --git a/tensorlayer/layers/spatial_transformer.py b/tensorlayer/layers/spatial_transformer.py index 38fa8ae11..5c50e465c 100644 --- a/tensorlayer/layers/spatial_transformer.py +++ b/tensorlayer/layers/spatial_transformer.py @@ -135,7 +135,10 @@ def _meshgrid(height, width): # grid = np.vstack([x_t.flatten(), y_t.flatten(), ones]) x_t = tf.matmul( tf.ones(shape=tf.stack([height, 1])), - tf.transpose(tf.expand_dims(tf.linspace(-1.0, 1.0, width), 1), [1, 0])) + tf.transpose(tf.expand_dims(tf.linspace(-1.0, + 1.0, + width), 1), [1, 0]) + ) y_t = tf.matmul(tf.expand_dims(tf.linspace(-1.0, 1.0, height), 1), tf.ones(shape=tf.stack([1, width]))) x_t_flat = tf.reshape(x_t, (1, -1)) @@ -245,8 +248,10 @@ def __init__( if out_size is None: out_size = [40, 40] - logging.info("SpatialTransformer2dAffineLayer %s: in_size:%s out_size:%s" % - (name, self.inputs.get_shape().as_list(), out_size)) + logging.info( + "SpatialTransformer2dAffineLayer %s: in_size:%s out_size:%s" % + (name, self.inputs.get_shape().as_list(), out_size) + ) with tf.variable_scope(name) as vs: # 1. make the localisation network to [batch, 6] via Flatten and Dense. diff --git a/tensorlayer/layers/special_activation.py b/tensorlayer/layers/special_activation.py index 7b04a7f08..7d50fa5bb 100644 --- a/tensorlayer/layers/special_activation.py +++ b/tensorlayer/layers/special_activation.py @@ -43,7 +43,8 @@ def __init__( a_init=tf.constant_initializer(value=0.0), a_init_args=None, # restore = True, - name="prelu_layer"): + name="prelu_layer" + ): if a_init_args is None: a_init_args = {} @@ -61,7 +62,12 @@ def __init__( # with tf.name_scope(name) as scope: with tf.variable_scope(name): alphas = tf.get_variable( - name='alphas', shape=w_shape, initializer=a_init, dtype=LayersConfig.tf_dtype, **a_init_args) + name='alphas', + shape=w_shape, + initializer=a_init, + dtype=LayersConfig.tf_dtype, + **a_init_args + ) try: # TF 1.0 self.outputs = tf.nn.relu(self.inputs) + tf.multiply(alphas, (self.inputs - tf.abs(self.inputs))) * 0.5 except Exception: # TF 0.12 diff --git a/tensorlayer/models/mobilenetv1.py b/tensorlayer/models/mobilenetv1.py index 4330dff85..b80b117c0 100644 --- a/tensorlayer/models/mobilenetv1.py +++ b/tensorlayer/models/mobilenetv1.py @@ -7,8 +7,16 @@ # import numpy as np import tensorflow as tf from .. import _logging as logging -from ..layers import (Layer, BatchNormLayer, Conv2d, DepthwiseConv2d, FlattenLayer, GlobalMeanPool2d, InputLayer, - ReshapeLayer) +from ..layers import ( + Layer, + BatchNormLayer, + Conv2d, + DepthwiseConv2d, + FlattenLayer, + GlobalMeanPool2d, + InputLayer, + ReshapeLayer +) from ..files import maybe_download_and_extract, assign_params, load_npz __all__ = [ @@ -155,7 +163,8 @@ def restore_params(self, sess, path='models'): 'mobilenet.npz', path, 'https://github.com/tensorlayer/pretrained-models/raw/master/models/', - expected_bytes=25600116) # ls -al + expected_bytes=25600116 + ) # ls -al params = load_npz(name=os.path.join(path, 'mobilenet.npz')) assign_params(sess, params[:len(self.net.all_params)], self.net) del params diff --git a/tensorlayer/models/squeezenetv1.py b/tensorlayer/models/squeezenetv1.py index 40168e173..2d30cb3b1 100644 --- a/tensorlayer/models/squeezenetv1.py +++ b/tensorlayer/models/squeezenetv1.py @@ -164,7 +164,8 @@ def restore_params(self, sess, path='models'): 'squeezenet.npz', path, 'https://github.com/tensorlayer/pretrained-models/raw/master/models/', - expected_bytes=7405613) # ls -al + expected_bytes=7405613 + ) # ls -al params = load_npz(name=os.path.join(path, 'squeezenet.npz')) assign_params(sess, params[:len(self.net.all_params)], self.net) del params diff --git a/tensorlayer/models/vgg16.py b/tensorlayer/models/vgg16.py index 40ba19129..c3dafc51f 100644 --- a/tensorlayer/models/vgg16.py +++ b/tensorlayer/models/vgg16.py @@ -55,16 +55,24 @@ def conv_layers(net_in): shape=[3, 3, 3, 64], # 64 features for each 3x3 patch strides=[1, 1, 1, 1], padding='SAME', - name='conv1_1') + name='conv1_1' + ) net = Conv2dLayer( net, act=tf.nn.relu, shape=[3, 3, 64, 64], # 64 features for each 3x3 patch strides=[1, 1, 1, 1], padding='SAME', - name='conv1_2') + name='conv1_2' + ) net = PoolLayer( - net, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', pool=tf.nn.max_pool, name='pool1') + net, + ksize=[1, 2, 2, 1], + strides=[1, 2, 2, 1], + padding='SAME', + pool=tf.nn.max_pool, + name='pool1' + ) # conv2 net = Conv2dLayer( @@ -73,16 +81,24 @@ def conv_layers(net_in): shape=[3, 3, 64, 128], # 128 features for each 3x3 patch strides=[1, 1, 1, 1], padding='SAME', - name='conv2_1') + name='conv2_1' + ) net = Conv2dLayer( net, act=tf.nn.relu, shape=[3, 3, 128, 128], # 128 features for each 3x3 patch strides=[1, 1, 1, 1], padding='SAME', - name='conv2_2') + name='conv2_2' + ) net = PoolLayer( - net, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', pool=tf.nn.max_pool, name='pool2') + net, + ksize=[1, 2, 2, 1], + strides=[1, 2, 2, 1], + padding='SAME', + pool=tf.nn.max_pool, + name='pool2' + ) # conv3 net = Conv2dLayer( @@ -91,23 +107,32 @@ def conv_layers(net_in): shape=[3, 3, 128, 256], # 256 features for each 3x3 patch strides=[1, 1, 1, 1], padding='SAME', - name='conv3_1') + name='conv3_1' + ) net = Conv2dLayer( net, act=tf.nn.relu, shape=[3, 3, 256, 256], # 256 features for each 3x3 patch strides=[1, 1, 1, 1], padding='SAME', - name='conv3_2') + name='conv3_2' + ) net = Conv2dLayer( net, act=tf.nn.relu, shape=[3, 3, 256, 256], # 256 features for each 3x3 patch strides=[1, 1, 1, 1], padding='SAME', - name='conv3_3') + name='conv3_3' + ) net = PoolLayer( - net, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', pool=tf.nn.max_pool, name='pool3') + net, + ksize=[1, 2, 2, 1], + strides=[1, 2, 2, 1], + padding='SAME', + pool=tf.nn.max_pool, + name='pool3' + ) # conv4 net = Conv2dLayer( @@ -116,23 +141,32 @@ def conv_layers(net_in): shape=[3, 3, 256, 512], # 512 features for each 3x3 patch strides=[1, 1, 1, 1], padding='SAME', - name='conv4_1') + name='conv4_1' + ) net = Conv2dLayer( net, act=tf.nn.relu, shape=[3, 3, 512, 512], # 512 features for each 3x3 patch strides=[1, 1, 1, 1], padding='SAME', - name='conv4_2') + name='conv4_2' + ) net = Conv2dLayer( net, act=tf.nn.relu, shape=[3, 3, 512, 512], # 512 features for each 3x3 patch strides=[1, 1, 1, 1], padding='SAME', - name='conv4_3') + name='conv4_3' + ) net = PoolLayer( - net, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', pool=tf.nn.max_pool, name='pool4') + net, + ksize=[1, 2, 2, 1], + strides=[1, 2, 2, 1], + padding='SAME', + pool=tf.nn.max_pool, + name='pool4' + ) # conv5 net = Conv2dLayer( @@ -141,23 +175,32 @@ def conv_layers(net_in): shape=[3, 3, 512, 512], # 512 features for each 3x3 patch strides=[1, 1, 1, 1], padding='SAME', - name='conv5_1') + name='conv5_1' + ) net = Conv2dLayer( net, act=tf.nn.relu, shape=[3, 3, 512, 512], # 512 features for each 3x3 patch strides=[1, 1, 1, 1], padding='SAME', - name='conv5_2') + name='conv5_2' + ) net = Conv2dLayer( net, act=tf.nn.relu, shape=[3, 3, 512, 512], # 512 features for each 3x3 patch strides=[1, 1, 1, 1], padding='SAME', - name='conv5_3') + name='conv5_3' + ) net = PoolLayer( - net, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', pool=tf.nn.max_pool, name='pool5') + net, + ksize=[1, 2, 2, 1], + strides=[1, 2, 2, 1], + padding='SAME', + pool=tf.nn.max_pool, + name='pool5' + ) return net @staticmethod @@ -179,31 +222,57 @@ def vgg16_simple_api(net_in, end_with): layers = [ # conv1 - lambda net: Conv2d(net_in, n_filter=64, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv1_1'), - lambda net: Conv2d(net, n_filter=64, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv1_2'), + lambda net: Conv2d( + net_in, n_filter=64, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv1_1' + ), + lambda net: Conv2d( + net, n_filter=64, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv1_2' + ), lambda net: MaxPool2d(net, filter_size=(2, 2), strides=(2, 2), padding='SAME', name='pool1'), # conv2 - lambda net: Conv2d(net, n_filter=128, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv2_1'), - lambda net: Conv2d(net, n_filter=128, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv2_2'), + lambda net: Conv2d( + net, n_filter=128, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv2_1' + ), + lambda net: Conv2d( + net, n_filter=128, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv2_2' + ), lambda net: MaxPool2d(net, filter_size=(2, 2), strides=(2, 2), padding='SAME', name='pool2'), # conv3 - lambda net: Conv2d(net, n_filter=256, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv3_1'), - lambda net: Conv2d(net, n_filter=256, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv3_2'), - lambda net: Conv2d(net, n_filter=256, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv3_3'), + lambda net: Conv2d( + net, n_filter=256, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv3_1' + ), + lambda net: Conv2d( + net, n_filter=256, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv3_2' + ), + lambda net: Conv2d( + net, n_filter=256, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv3_3' + ), lambda net: MaxPool2d(net, filter_size=(2, 2), strides=(2, 2), padding='SAME', name='pool3'), # conv4 - lambda net: Conv2d(net, n_filter=512, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv4_1'), - lambda net: Conv2d(net, n_filter=512, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv4_2'), - lambda net: Conv2d(net, n_filter=512, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv4_3'), + lambda net: Conv2d( + net, n_filter=512, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv4_1' + ), + lambda net: Conv2d( + net, n_filter=512, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv4_2' + ), + lambda net: Conv2d( + net, n_filter=512, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv4_3' + ), lambda net: MaxPool2d(net, filter_size=(2, 2), strides=(2, 2), padding='SAME', name='pool4'), # conv5 - lambda net: Conv2d(net, n_filter=512, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv5_1'), - lambda net: Conv2d(net, n_filter=512, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv5_2'), - lambda net: Conv2d(net, n_filter=512, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv5_3'), + lambda net: Conv2d( + net, n_filter=512, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv5_1' + ), + lambda net: Conv2d( + net, n_filter=512, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv5_2' + ), + lambda net: Conv2d( + net, n_filter=512, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv5_3' + ), lambda net: MaxPool2d(net, filter_size=(2, 2), strides=(2, 2), padding='SAME', name='pool5'), lambda net: FlattenLayer(net, name='flatten'), lambda net: DenseLayer(net, n_units=4096, act=tf.nn.relu, name='fc1_relu'), @@ -222,7 +291,11 @@ def vgg16_simple_api(net_in, end_with): def restore_params(self, sess): logging.info("Restore pre-trained parameters") maybe_download_and_extract( - 'vgg16_weights.npz', 'models', 'http://www.cs.toronto.edu/~frossard/vgg16/', expected_bytes=553436134) + 'vgg16_weights.npz', + 'models', + 'http://www.cs.toronto.edu/~frossard/vgg16/', + expected_bytes=553436134 + ) npz = np.load(os.path.join('models', 'vgg16_weights.npz')) params = [] diff --git a/tensorlayer/nlp.py b/tensorlayer/nlp.py index fcf1a1c34..75791dba7 100755 --- a/tensorlayer/nlp.py +++ b/tensorlayer/nlp.py @@ -864,13 +864,15 @@ def basic_tokenizer(sentence, _WORD_SPLIT=re.compile(b"([.,!?\"':;)(])")): return [w for w in words if w] -def create_vocabulary(vocabulary_path, - data_path, - max_vocabulary_size, - tokenizer=None, - normalize_digits=True, - _DIGIT_RE=re.compile(br"\d"), - _START_VOCAB=None): +def create_vocabulary( + vocabulary_path, + data_path, + max_vocabulary_size, + tokenizer=None, + normalize_digits=True, + _DIGIT_RE=re.compile(br"\d"), + _START_VOCAB=None +): """Create vocabulary file (if it does not exist yet) from data file. Data file is assumed to contain one sentence per line. Each sentence is @@ -975,12 +977,14 @@ def initialize_vocabulary(vocabulary_path): raise ValueError("Vocabulary file %s not found.", vocabulary_path) -def sentence_to_token_ids(sentence, - vocabulary, - tokenizer=None, - normalize_digits=True, - UNK_ID=3, - _DIGIT_RE=re.compile(br"\d")): +def sentence_to_token_ids( + sentence, + vocabulary, + tokenizer=None, + normalize_digits=True, + UNK_ID=3, + _DIGIT_RE=re.compile(br"\d") +): """Convert a string to list of integers representing token-ids. For example, a sentence "I have a dog" may become tokenized into @@ -1014,13 +1018,15 @@ def sentence_to_token_ids(sentence, return [vocabulary.get(re.sub(_DIGIT_RE, b"0", w), UNK_ID) for w in words] -def data_to_token_ids(data_path, - target_path, - vocabulary_path, - tokenizer=None, - normalize_digits=True, - UNK_ID=3, - _DIGIT_RE=re.compile(br"\d")): +def data_to_token_ids( + data_path, + target_path, + vocabulary_path, + tokenizer=None, + normalize_digits=True, + UNK_ID=3, + _DIGIT_RE=re.compile(br"\d") +): """Tokenize data file and turn into token-ids using given vocabulary file. This function loads data line-by-line from data_path, calls the above @@ -1056,7 +1062,13 @@ def data_to_token_ids(data_path, if counter % 100000 == 0: logging.info(" tokenizing line %d" % counter) token_ids = sentence_to_token_ids( - line, vocab, tokenizer, normalize_digits, UNK_ID=UNK_ID, _DIGIT_RE=_DIGIT_RE) + line, + vocab, + tokenizer, + normalize_digits, + UNK_ID=UNK_ID, + _DIGIT_RE=_DIGIT_RE + ) tokens_file.write(" ".join([str(tok) for tok in token_ids]) + "\n") else: logging.info("Target path %s exists" % target_path) @@ -1096,8 +1108,10 @@ def moses_multi_bleu(hypotheses, references, lowercase=False): # Get MOSES multi-bleu script try: - multi_bleu_path, _ = urllib.request.urlretrieve("https://raw.githubusercontent.com/moses-smt/mosesdecoder/" - "master/scripts/generic/multi-bleu.perl") + multi_bleu_path, _ = urllib.request.urlretrieve( + "https://raw.githubusercontent.com/moses-smt/mosesdecoder/" + "master/scripts/generic/multi-bleu.perl" + ) os.chmod(multi_bleu_path, 0o755) except Exception: # pylint: disable=W0702 tf.logging.info("Unable to fetch multi-bleu.perl script, using local.") diff --git a/tensorlayer/prepro.py b/tensorlayer/prepro.py index 2274fc48a..9480aeb85 100644 --- a/tensorlayer/prepro.py +++ b/tensorlayer/prepro.py @@ -181,7 +181,13 @@ def apply_fn(results, i, data, kwargs): threads = [] for i in range(thread_count): t = threading.Thread( - name='threading_and_return', target=apply_fn, args=(results, i, data[divs[i]:divs[i + 1]], kwargs)) + name='threading_and_return', + target=apply_fn, + args=(results, + i, + data[divs[i]:divs[i + 1]], + kwargs) + ) t.start() threads.append(t) @@ -197,15 +203,17 @@ def apply_fn(results, i, data, kwargs): return np.concatenate(results) -def rotation(x, - rg=20, - is_random=False, - row_index=0, - col_index=1, - channel_index=2, - fill_mode='nearest', - cval=0., - order=1): +def rotation( + x, + rg=20, + is_random=False, + row_index=0, + col_index=1, + channel_index=2, + fill_mode='nearest', + cval=0., + order=1 +): """Rotate an image randomly or non-randomly. Parameters @@ -249,15 +257,17 @@ def rotation(x, return x -def rotation_multi(x, - rg=20, - is_random=False, - row_index=0, - col_index=1, - channel_index=2, - fill_mode='nearest', - cval=0., - order=1): +def rotation_multi( + x, + rg=20, + is_random=False, + row_index=0, + col_index=1, + channel_index=2, + fill_mode='nearest', + cval=0., + order=1 +): """Rotate multiple images with the same arguments, randomly or non-randomly. Usually be used for image segmentation which x=[X, Y], X and Y should be matched. @@ -459,16 +469,18 @@ def flip_axis_multi(x, axis, is_random=False): # shift -def shift(x, - wrg=0.1, - hrg=0.1, - is_random=False, - row_index=0, - col_index=1, - channel_index=2, - fill_mode='nearest', - cval=0., - order=1): +def shift( + x, + wrg=0.1, + hrg=0.1, + is_random=False, + row_index=0, + col_index=1, + channel_index=2, + fill_mode='nearest', + cval=0., + order=1 +): """Shift an image randomly or non-randomly. Parameters @@ -509,16 +521,18 @@ def shift(x, return x -def shift_multi(x, - wrg=0.1, - hrg=0.1, - is_random=False, - row_index=0, - col_index=1, - channel_index=2, - fill_mode='nearest', - cval=0., - order=1): +def shift_multi( + x, + wrg=0.1, + hrg=0.1, + is_random=False, + row_index=0, + col_index=1, + channel_index=2, + fill_mode='nearest', + cval=0., + order=1 +): """Shift images with the same arguments, randomly or non-randomly. Usually be used for image segmentation which x=[X, Y], X and Y should be matched. @@ -551,15 +565,17 @@ def shift_multi(x, # shear -def shear(x, - intensity=0.1, - is_random=False, - row_index=0, - col_index=1, - channel_index=2, - fill_mode='nearest', - cval=0., - order=1): +def shear( + x, + intensity=0.1, + is_random=False, + row_index=0, + col_index=1, + channel_index=2, + fill_mode='nearest', + cval=0., + order=1 +): """Shear an image randomly or non-randomly. Parameters @@ -602,15 +618,17 @@ def shear(x, return x -def shear_multi(x, - intensity=0.1, - is_random=False, - row_index=0, - col_index=1, - channel_index=2, - fill_mode='nearest', - cval=0., - order=1): +def shear_multi( + x, + intensity=0.1, + is_random=False, + row_index=0, + col_index=1, + channel_index=2, + fill_mode='nearest', + cval=0., + order=1 +): """Shear images with the same arguments, randomly or non-randomly. Usually be used for image segmentation which x=[X, Y], X and Y should be matched. @@ -641,15 +659,17 @@ def shear_multi(x, return np.asarray(results) -def shear2(x, - shear=(0.1, 0.1), - is_random=False, - row_index=0, - col_index=1, - channel_index=2, - fill_mode='nearest', - cval=0., - order=1): +def shear2( + x, + shear=(0.1, 0.1), + is_random=False, + row_index=0, + col_index=1, + channel_index=2, + fill_mode='nearest', + cval=0., + order=1 +): """Shear an image randomly or non-randomly. Parameters @@ -694,15 +714,17 @@ def shear2(x, return x -def shear_multi2(x, - shear=(0.1, 0.1), - is_random=False, - row_index=0, - col_index=1, - channel_index=2, - fill_mode='nearest', - cval=0., - order=1): +def shear_multi2( + x, + shear=(0.1, 0.1), + is_random=False, + row_index=0, + col_index=1, + channel_index=2, + fill_mode='nearest', + cval=0., + order=1 +): """Shear images with the same arguments, randomly or non-randomly. Usually be used for image segmentation which x=[X, Y], X and Y should be matched. @@ -737,18 +759,20 @@ def shear_multi2(x, # swirl -def swirl(x, - center=None, - strength=1, - radius=100, - rotation=0, - output_shape=None, - order=1, - mode='constant', - cval=0, - clip=True, - preserve_range=False, - is_random=False): +def swirl( + x, + center=None, + strength=1, + radius=100, + rotation=0, + output_shape=None, + order=1, + mode='constant', + cval=0, + clip=True, + preserve_range=False, + is_random=False +): """Swirl an image randomly or non-randomly, see `scikit-image swirl API `__ and `example `__. @@ -819,24 +843,27 @@ def swirl(x, mode=mode, cval=cval, clip=clip, - preserve_range=preserve_range) + preserve_range=preserve_range + ) if max_v > 1: swirled = swirled * max_v return swirled -def swirl_multi(x, - center=None, - strength=1, - radius=100, - rotation=0, - output_shape=None, - order=1, - mode='constant', - cval=0, - clip=True, - preserve_range=False, - is_random=False): +def swirl_multi( + x, + center=None, + strength=1, + radius=100, + rotation=0, + output_shape=None, + order=1, + mode='constant', + cval=0, + clip=True, + preserve_range=False, + is_random=False +): """Swirl multiple images with the same arguments, randomly or non-randomly. Usually be used for image segmentation which x=[X, Y], X and Y should be matched. @@ -879,7 +906,8 @@ def swirl_multi(x, mode=mode, cval=cval, clip=clip, - preserve_range=preserve_range) + preserve_range=preserve_range + ) if max_v > 1: swirled = swirled * max_v results.append(swirled) @@ -996,15 +1024,17 @@ def elastic_transform_multi(x, alpha, sigma, mode="constant", cval=0, is_random= # zoom -def zoom(x, - zoom_range=(0.9, 1.1), - is_random=False, - row_index=0, - col_index=1, - channel_index=2, - fill_mode='nearest', - cval=0., - order=1): +def zoom( + x, + zoom_range=(0.9, 1.1), + is_random=False, + row_index=0, + col_index=1, + channel_index=2, + fill_mode='nearest', + cval=0., + order=1 +): """Zoom in and out of a single image, randomly or non-randomly. Parameters @@ -1051,15 +1081,17 @@ def zoom(x, return x -def zoom_multi(x, - zoom_range=(0.9, 1.1), - is_random=False, - row_index=0, - col_index=1, - channel_index=2, - fill_mode='nearest', - cval=0., - order=1): +def zoom_multi( + x, + zoom_range=(0.9, 1.1), + is_random=False, + row_index=0, + col_index=1, + channel_index=2, + fill_mode='nearest', + cval=0., + order=1 +): """Zoom in and out of images with the same arguments, randomly or non-randomly. Usually be used for image segmentation which x=[X, Y], X and Y should be matched. @@ -1207,8 +1239,8 @@ def illumination(x, gamma=1., contrast=1., saturation=1., is_random=False): if is_random: try: - assert len(gamma) == len(contrast) == len( - saturation) == 2, "if is_random = True, the arguments are (min, max)" + assert len(gamma) == len(contrast + ) == len(saturation) == 2, "if is_random = True, the arguments are (min, max)" except: raise Exception("if is_random = True, the arguments are (min, max)") ## random change brightness # small --> brighter @@ -1471,12 +1503,14 @@ def pixel_value_scale(im, val=0.9, clip=(-np.inf, np.inf), is_random=False): # normailization -def samplewise_norm(x, - rescale=None, - samplewise_center=False, - samplewise_std_normalization=False, - channel_index=2, - epsilon=1e-7): +def samplewise_norm( + x, + rescale=None, + samplewise_center=False, + samplewise_std_normalization=False, + channel_index=2, + epsilon=1e-7 +): """Normalize an image by rescale, samplewise centering and samplewise centering in order. Parameters @@ -1811,23 +1845,31 @@ def apply_transform(x, transform_matrix, channel_index=2, fill_mode='nearest', c final_offset = transform_matrix[:2, 2] channel_images = [ ndi.interpolation.affine_transform( - x_channel, final_affine_matrix, final_offset, order=order, mode=fill_mode, cval=cval) for x_channel in x + x_channel, + final_affine_matrix, + final_offset, + order=order, + mode=fill_mode, + cval=cval + ) for x_channel in x ] x = np.stack(channel_images, axis=0) x = np.rollaxis(x, 0, channel_index + 1) return x -def projective_transform_by_points(x, - src, - dst, - map_args=None, - output_shape=None, - order=1, - mode='constant', - cval=0.0, - clip=True, - preserve_range=False): +def projective_transform_by_points( + x, + src, + dst, + map_args=None, + output_shape=None, + order=1, + mode='constant', + cval=0.0, + clip=True, + preserve_range=False +): """Projective transform by given coordinates, usually 4 coordinates. see `scikit-image `__. @@ -1903,7 +1945,8 @@ def projective_transform_by_points(x, mode=mode, cval=cval, clip=clip, - preserve_range=preserve_range) + preserve_range=preserve_range + ) return warped @@ -1976,7 +2019,11 @@ def find_contours(x, level=0.8, fully_connected='low', positive_orientation='low """ return skimage.measure.find_contours( - x, level, fully_connected=fully_connected, positive_orientation=positive_orientation) + x, + level, + fully_connected=fully_connected, + positive_orientation=positive_orientation + ) def pt2map(list_points=None, size=(100, 100), val=1): @@ -2568,16 +2615,18 @@ def obj_box_imresize(im, coords=None, size=None, interp='bicubic', mode=None, is # exit() -def obj_box_crop(im, - classes=None, - coords=None, - wrg=100, - hrg=100, - is_rescale=False, - is_center=False, - is_random=False, - thresh_wh=0.02, - thresh_wh2=12.): +def obj_box_crop( + im, + classes=None, + coords=None, + wrg=100, + hrg=100, + is_rescale=False, + is_center=False, + is_random=False, + thresh_wh=0.02, + thresh_wh2=12. +): """Randomly or centrally crop an image, and compute the new bounding box coordinates. Objects outside the cropped image will be removed. @@ -2721,22 +2770,24 @@ def _get_coord(coord): return im_new, classes_new, coords_new -def obj_box_shift(im, - classes=None, - coords=None, - wrg=0.1, - hrg=0.1, - row_index=0, - col_index=1, - channel_index=2, - fill_mode='nearest', - cval=0., - order=1, - is_rescale=False, - is_center=False, - is_random=False, - thresh_wh=0.02, - thresh_wh2=12.): +def obj_box_shift( + im, + classes=None, + coords=None, + wrg=0.1, + hrg=0.1, + row_index=0, + col_index=1, + channel_index=2, + fill_mode='nearest', + cval=0., + order=1, + is_rescale=False, + is_center=False, + is_random=False, + thresh_wh=0.02, + thresh_wh2=12. +): """Shift an image randomly or non-randomly, and compute the new bounding box coordinates. Objects outside the cropped image will be removed. @@ -2862,21 +2913,23 @@ def _get_coord(coord): return im_new, classes_new, coords_new -def obj_box_zoom(im, - classes=None, - coords=None, - zoom_range=(0.9, 1.1), - row_index=0, - col_index=1, - channel_index=2, - fill_mode='nearest', - cval=0., - order=1, - is_rescale=False, - is_center=False, - is_random=False, - thresh_wh=0.02, - thresh_wh2=12.): +def obj_box_zoom( + im, + classes=None, + coords=None, + zoom_range=(0.9, 1.1), + row_index=0, + col_index=1, + channel_index=2, + fill_mode='nearest', + cval=0., + order=1, + is_rescale=False, + is_center=False, + is_random=False, + thresh_wh=0.02, + thresh_wh2=12. +): """Zoom in and out of a single image, randomly or non-randomly, and compute the new bounding box coordinates. Objects outside the cropped image will be removed. @@ -3073,8 +3126,12 @@ def pad_sequences(sequences, maxlen=None, dtype='int32', padding='post', truncat # check `trunc` has expected shape trunc = np.asarray(trunc, dtype=dtype) if trunc.shape[1:] != sample_shape: - raise ValueError('Shape of sample %s of sequence at position %s is different from expected shape %s' % - (trunc.shape[1:], idx, sample_shape)) + raise ValueError( + 'Shape of sample %s of sequence at position %s is different from expected shape %s' % + (trunc.shape[1:], + idx, + sample_shape) + ) if padding == 'post': x[idx, :len(trunc)] = trunc @@ -3160,7 +3217,7 @@ def process_sequences(sequences, end_id=0, pad_val=0, is_shorten=True, remain_en max_length = i_w if remain_end_id is False: seq[i_w] = pad_val # set end_id to pad_val - elif is_end == True: + elif is_end ==True: seq[i_w] = pad_val if remain_end_id is True: diff --git a/tensorlayer/utils.py b/tensorlayer/utils.py index 8abb9990c..1d8362144 100644 --- a/tensorlayer/utils.py +++ b/tensorlayer/utils.py @@ -32,7 +32,8 @@ ] -def fit(sess, +def fit( + sess, network, train_op, cost, @@ -50,7 +51,8 @@ def fit(sess, tensorboard=False, tensorboard_epoch_freq=5, tensorboard_weight_histograms=True, - tensorboard_graph_vis=True): + tensorboard_graph_vis=True +): """Training a given non time-series network by the given cost function, training data, batch_size, n_epoch etc. - MNIST example click `here `_. @@ -218,8 +220,12 @@ def fit(sess, if acc is not None: logging.info(" val acc: %f" % (val_acc / n_batch)) else: - logging.info("Epoch %d of %d took %fs, loss %f" % (epoch + 1, n_epoch, time.time() - start_time, - loss_ep)) + logging.info( + "Epoch %d of %d took %fs, loss %f" % (epoch + 1, + n_epoch, + time.time() - start_time, + loss_ep) + ) logging.info("Total training time: %fs" % (time.time() - start_time_begin)) @@ -570,8 +576,8 @@ def exit_tensorflow(sess=None, port=6006): elif _platform == "darwin": logging.info('OS X: %s' % text) subprocess.Popen( - "lsof -i tcp:" + str(port) + " | grep -v PID | awk '{print $2}' | xargs kill", - shell=True) # kill tensorboard + "lsof -i tcp:" + str(port) + " | grep -v PID | awk '{print $2}' | xargs kill", shell=True + ) # kill tensorboard elif _platform == "win32": raise NotImplementedError("this function is not supported on the Windows platform") else: @@ -600,8 +606,8 @@ def open_tensorboard(log_dir='/tmp/tensorflow', port=6006): elif _platform == "darwin": logging.info('OS X: %s' % text) subprocess.Popen( - sys.prefix + " | python -m tensorflow.tensorboard --logdir=" + log_dir + " --port=" + str(port), - shell=True) # open tensorboard in localhost:6006/ or whatever port you chose + sys.prefix + " | python -m tensorflow.tensorboard --logdir=" + log_dir + " --port=" + str(port), shell=True + ) # open tensorboard in localhost:6006/ or whatever port you chose elif _platform == "win32": raise NotImplementedError("this function is not supported on the Windows platform") else: diff --git a/tensorlayer/visualize.py b/tensorlayer/visualize.py index f6d3f1309..61fc821ae 100644 --- a/tensorlayer/visualize.py +++ b/tensorlayer/visualize.py @@ -134,20 +134,23 @@ def merge(images, size): def imsave(images, size, path): return scipy.misc.imsave(path, merge(images, size)) - assert len( - images) <= size[0] * size[1], "number of images should be equal or less than size[0] * size[1] {}".format( - len(images)) + assert len(images) <= size[0] * size[1 + ], "number of images should be equal or less than size[0] * size[1] {}".format( + len(images) + ) return imsave(images, size, image_path) -def draw_boxes_and_labels_to_image(image, - classes, - coords, - scores, - classes_list, - is_center=True, - is_rescale=True, - save_name=None): +def draw_boxes_and_labels_to_image( + image, + classes, + coords, + scores, + classes_list, + is_center=True, + is_rescale=True, + save_name=None +): """Draw bboxes and class labels on image. Return or save the image with bboxes, example in the docs of ``tl.prepro``. Parameters @@ -212,7 +215,8 @@ def draw_boxes_and_labels_to_image(image, (int(x), int(y)), (int(x2), int(y2)), # up-left and botton-right [0, 255, 0], - thick) + thick + ) cv2.putText( image, @@ -221,7 +225,8 @@ def draw_boxes_and_labels_to_image(image, 0, 1.5e-3 * imh, # bigger = larger font [0, 0, 256], # self.meta['colors'][max_indx], - int(thick / 2) + 1) # bold + int(thick / 2) + 1 + ) # bold if save_name is not None: # cv2.imwrite('_my.png', image) @@ -312,7 +317,9 @@ def draw_mpii_pose_to_image(image, poses, save_name='image.png'): [(12, 8), [255, 0, 255]], # right hand [(8, 13), [0, 255, 255]], [(13, 14), [100, 255, 255]], - [(14, 15), [200, 255, 255]] # left hand + [(14, 15), [200, + 255, + 255]] # left hand ] for line in lines: start, end = line[0] @@ -322,7 +329,8 @@ def draw_mpii_pose_to_image(image, poses, save_name='image.png'): (int(joint_pos[start][0]), int(joint_pos[start][1])), (int(joint_pos[end][0]), int(joint_pos[end][1])), # up-left and botton-right line[1], - thick) + thick + ) # rr, cc, val = skimage.draw.line_aa(int(joint_pos[start][1]), int(joint_pos[start][0]), int(joint_pos[end][1]), int(joint_pos[end][0])) # image[rr, cc] = line[1] # draw circles @@ -341,7 +349,8 @@ def draw_mpii_pose_to_image(image, poses, save_name='image.png'): (int(head_rect[0]), int(head_rect[1])), (int(head_rect[2]), int(head_rect[3])), # up-left and botton-right [0, 180, 0], - thick) + thick + ) if save_name is not None: # cv2.imwrite(save_name, image) @@ -444,7 +453,11 @@ def CNN2d(CNN=None, second=10, saveable=True, name='cnn', fig_idx=3119362): plt.imshow(np.reshape(CNN[:, :, :, count - 1], (n_row, n_col)), cmap='gray', interpolation="nearest") elif n_color == 3: plt.imshow( - np.reshape(CNN[:, :, :, count - 1], (n_row, n_col, n_color)), cmap='gray', interpolation="nearest") + np.reshape(CNN[:, :, :, count - 1], + (n_row, n_col, n_color)), + cmap='gray', + interpolation="nearest" + ) else: raise Exception("Unknown n_color") plt.gca().xaxis.set_major_locator(plt.NullLocator()) # distable tick @@ -634,8 +647,10 @@ def draw_weights(W=None, second=10, saveable=True, shape=None, name='mnist', fig # if np.mean(feature) < -0.015: # condition threshold # feature = np.zeros_like(feature) plt.imshow( - np.reshape(feature, (shape[0], shape[1])), cmap='gray', - interpolation="nearest") #, vmin=np.min(feature), vmax=np.max(feature)) + np.reshape(feature, (shape[0], shape[1])), + cmap='gray', + interpolation="nearest" + ) #, vmin=np.min(feature), vmax=np.max(feature)) # plt.title(name) # ------------------------------------------------------------ # plt.imshow(np.reshape(W[:,count-1] ,(np.sqrt(size),np.sqrt(size))), cmap='gray', interpolation="nearest") diff --git a/tests/test_layers_convolution.py b/tests/test_layers_convolution.py index 9765975ea..cb6fe0b4a 100644 --- a/tests/test_layers_convolution.py +++ b/tests/test_layers_convolution.py @@ -37,7 +37,8 @@ def setUpClass(cls): padding='SAME', W_init=tf.truncated_normal_initializer(stddev=5e-2), b_init=tf.constant_initializer(value=0.0), - name='conv2dlayer') + name='conv2dlayer' + ) cls.shape_n3 = n3.outputs.get_shape().as_list() n4 = tl.layers.Conv2d(nin2, n_filter=32, filter_size=(3, 3), strides=(2, 2), act=None, name='conv2d') @@ -45,19 +46,42 @@ def setUpClass(cls): cls.n4_params = n4.all_params n5 = tl.layers.Conv2d( - nin2, n_filter=32, filter_size=(3, 3), strides=(2, 2), act=tf.nn.relu, b_init=None, name='conv2d_no_bias') + nin2, + n_filter=32, + filter_size=(3, 3), + strides=(2, 2), + act=tf.nn.relu, + b_init=None, + name='conv2d_no_bias' + ) cls.shape_n5 = n5.outputs.get_shape().as_list() cls.n5_params = n5.all_params n6 = tl.layers.DeConv2dLayer( - nin2, shape=(5, 5, 32, 3), output_shape=(100, 200, 200, 32), strides=(1, 2, 2, 1), name='deconv2dlayer') + nin2, shape=(5, + 5, + 32, + 3), output_shape=(100, + 200, + 200, + 32), strides=(1, + 2, + 2, + 1), name='deconv2dlayer' + ) cls.shape_n6 = n6.outputs.get_shape().as_list() n7 = tl.layers.DeConv2d(nin2, n_filter=32, filter_size=(3, 3), strides=(2, 2), name='DeConv2d') cls.shape_n7 = n7.outputs.get_shape().as_list() n8 = tl.layers.DepthwiseConv2d( - nin2, shape=(3, 3), strides=(2, 2), act=tf.nn.relu, depth_multiplier=2, name='depthwise') + nin2, + shape=(3, 3), + strides=(2, 2), + act=tf.nn.relu, + depth_multiplier=2, + name='depthwise' + ) cls.shape_n8 = n8.outputs.get_shape().as_list() n9 = tl.layers.Conv2d(nin2, n_filter=32, filter_size=(3, 3), strides=(2, 2), act=tf.nn.relu, name='conv2d2') @@ -65,7 +89,13 @@ def setUpClass(cls): cls.shape_n9 = n9.outputs.get_shape().as_list() n10 = tl.layers.SeparableConv2d( - nin2, n_filter=32, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, name='seperable1') + nin2, + n_filter=32, + filter_size=(3, 3), + strides=(1, 1), + act=tf.nn.relu, + name='seperable1' + ) cls.shape_n10 = n10.outputs.get_shape().as_list() cls.n10_all_layers = n10.all_layers cls.n10_params = n10.all_params diff --git a/tests/test_layers_core.py b/tests/test_layers_core.py index f34025bee..8e90acd3a 100644 --- a/tests/test_layers_core.py +++ b/tests/test_layers_core.py @@ -49,7 +49,8 @@ def setUpClass(cls): vocabulary_size=1000, embedding_size=200, num_sampled=64, - name='word2vec') + name='word2vec' + ) net3.print_layers() net3.print_params(False) diff --git a/tests/test_layers_recurrent.py b/tests/test_layers_recurrent.py index 468e89edb..840ae11bb 100644 --- a/tests/test_layers_recurrent.py +++ b/tests/test_layers_recurrent.py @@ -31,7 +31,11 @@ def setUpClass(cls): input_data = tf.placeholder(tf.int32, [cls.net1_batch_size, cls.num_steps]) net1 = tl.layers.EmbeddingInputlayer( - inputs=input_data, vocabulary_size=cls.vocab_size, embedding_size=cls.hidden_size, name='embed') + inputs=input_data, + vocabulary_size=cls.vocab_size, + embedding_size=cls.hidden_size, + name='embed' + ) net1 = tl.layers.DropoutLayer(net1, keep=cls.keep_prob, is_fix=True, is_train=cls.is_train, name='drop1') net1 = tl.layers.RNNLayer( net1, @@ -39,7 +43,8 @@ def setUpClass(cls): n_hidden=cls.hidden_size, n_steps=cls.num_steps, return_last=False, - name='lstm1') + name='lstm1' + ) # lstm1 = net1 @@ -50,7 +55,8 @@ def setUpClass(cls): n_hidden=cls.hidden_size, n_steps=cls.num_steps, return_last=True, - name='lstm2') + name='lstm2' + ) # lstm2 = net1 @@ -85,7 +91,8 @@ def setUpClass(cls): n_steps=cls.num_steps, return_last=False, return_seq_2d=True, - name='rnn') + name='rnn' + ) net2 = tl.layers.DenseLayer(rnn, n_units=3, name='out') @@ -102,7 +109,11 @@ def setUpClass(cls): x3 = tf.placeholder(tf.int32, [cls.net3_batch_size, cls.num_steps]) net3 = tl.layers.EmbeddingInputlayer( - inputs=x3, vocabulary_size=cls.vocab_size, embedding_size=cls.hidden_size, name='emb') + inputs=x3, + vocabulary_size=cls.vocab_size, + embedding_size=cls.hidden_size, + name='emb' + ) net3 = tl.layers.BiRNNLayer( net3, cell_fn=tf.contrib.rnn.BasicLSTMCell, @@ -110,7 +121,8 @@ def setUpClass(cls): n_steps=cls.num_steps, return_last=False, return_seq_2d=False, - name='birnn') + name='birnn' + ) net3.print_layers() net3.print_params(False) @@ -122,7 +134,11 @@ def setUpClass(cls): # n_layer=2 net4 = tl.layers.EmbeddingInputlayer( - inputs=x3, vocabulary_size=cls.vocab_size, embedding_size=cls.hidden_size, name='emb2') + inputs=x3, + vocabulary_size=cls.vocab_size, + embedding_size=cls.hidden_size, + name='emb2' + ) net4 = tl.layers.BiRNNLayer( net4, cell_fn=tf.contrib.rnn.BasicLSTMCell, @@ -131,7 +147,8 @@ def setUpClass(cls): n_layer=2, return_last=False, return_seq_2d=False, - name='birnn2') + name='birnn2' + ) net4.print_layers() net4.print_params(False) @@ -162,7 +179,11 @@ def setUpClass(cls): input_seqs = tf.placeholder(dtype=tf.int64, shape=[cls.net5_batch_size, None], name="input") nin = tl.layers.EmbeddingInputlayer( - inputs=input_seqs, vocabulary_size=cls.vocab_size, embedding_size=cls.embedding_size, name='seq_embedding') + inputs=input_seqs, + vocabulary_size=cls.vocab_size, + embedding_size=cls.embedding_size, + name='seq_embedding' + ) rnn = tl.layers.DynamicRNNLayer( nin, @@ -172,7 +193,8 @@ def setUpClass(cls): sequence_length=tl.layers.retrieve_seq_length_op2(input_seqs), return_last=False, return_seq_2d=True, - name='dynamicrnn') + name='dynamicrnn' + ) net5 = tl.layers.DenseLayer(rnn, n_units=cls.vocab_size, name="o") @@ -187,7 +209,11 @@ def setUpClass(cls): # n_layer=3 nin = tl.layers.EmbeddingInputlayer( - inputs=input_seqs, vocabulary_size=cls.vocab_size, embedding_size=cls.embedding_size, name='seq_embedding2') + inputs=input_seqs, + vocabulary_size=cls.vocab_size, + embedding_size=cls.embedding_size, + name='seq_embedding2' + ) rnn = tl.layers.DynamicRNNLayer( nin, cell_fn=tf.contrib.rnn.BasicLSTMCell, @@ -197,7 +223,8 @@ def setUpClass(cls): n_layer=3, return_last=False, return_seq_2d=True, - name='dynamicrnn2') + name='dynamicrnn2' + ) # net6 = tl.layers.DenseLayer(rnn, n_units=cls.vocab_size, name="o2") @@ -210,7 +237,8 @@ def setUpClass(cls): n_layer=3, return_last=False, return_seq_2d=False, - name='dynamicrnn3') + name='dynamicrnn3' + ) # net6 = tl.layers.DenseLayer(rnn, n_units=vocab_size, name="o3") @@ -229,7 +257,8 @@ def setUpClass(cls): n_layer=1, return_last=True, return_seq_2d=False, - name='dynamicrnn4') + name='dynamicrnn4' + ) net7.print_layers() net7.print_params(False) @@ -245,7 +274,8 @@ def setUpClass(cls): n_layer=1, return_last=True, return_seq_2d=True, - name='dynamicrnn5') + name='dynamicrnn5' + ) net8.print_layers() net8.print_params(False) @@ -262,7 +292,8 @@ def setUpClass(cls): sequence_length=tl.layers.retrieve_seq_length_op2(input_seqs), return_last=False, return_seq_2d=True, - name='bidynamicrnn') + name='bidynamicrnn' + ) net9 = tl.layers.DenseLayer(rnn, n_units=cls.vocab_size, name="o4") @@ -285,7 +316,8 @@ def setUpClass(cls): n_layer=2, return_last=False, return_seq_2d=True, - name='bidynamicrnn2') + name='bidynamicrnn2' + ) net10 = tl.layers.DenseLayer(rnn, n_units=cls.vocab_size, name="o5") @@ -311,11 +343,19 @@ def setUpClass(cls): with tf.variable_scope("embedding") as vs: net_encode = tl.layers.EmbeddingInputlayer( - inputs=encode_seqs, vocabulary_size=10000, embedding_size=200, name='seq_embed') + inputs=encode_seqs, + vocabulary_size=10000, + embedding_size=200, + name='seq_embed' + ) vs.reuse_variables() # tl.layers.set_name_reuse(True) net_decode = tl.layers.EmbeddingInputlayer( - inputs=decode_seqs, vocabulary_size=10000, embedding_size=200, name='seq_embed') + inputs=decode_seqs, + vocabulary_size=10000, + embedding_size=200, + name='seq_embed' + ) net11 = tl.layers.Seq2Seq( net_encode, @@ -329,7 +369,8 @@ def setUpClass(cls): dropout=None, n_layer=2, return_seq_2d=True, - name='Seq2seq') + name='Seq2seq' + ) net11 = tl.layers.DenseLayer(net11, n_units=10000, act=tf.identity, name='oo') diff --git a/tests/test_layers_spatial_transformer.py b/tests/test_layers_spatial_transformer.py index d483040fb..835a620dc 100644 --- a/tests/test_layers_spatial_transformer.py +++ b/tests/test_layers_spatial_transformer.py @@ -27,9 +27,23 @@ def model(x, is_train, reuse): s = n ## 3. Classifier n = tl.layers.Conv2d( - n, n_filter=16, filter_size=(3, 3), strides=(2, 2), act=tf.nn.relu, padding='SAME', name='conv1') + n, + n_filter=16, + filter_size=(3, 3), + strides=(2, 2), + act=tf.nn.relu, + padding='SAME', + name='conv1' + ) n = tl.layers.Conv2d( - n, n_filter=16, filter_size=(3, 3), strides=(2, 2), act=tf.nn.relu, padding='SAME', name='conv2') + n, + n_filter=16, + filter_size=(3, 3), + strides=(2, 2), + act=tf.nn.relu, + padding='SAME', + name='conv2' + ) n = tl.layers.FlattenLayer(n, name='flatten2') n = tl.layers.DenseLayer(n, n_units=1024, act=tf.nn.relu, name='out1') n = tl.layers.DenseLayer(n, n_units=10, act=tf.identity, name='out2') diff --git a/tests/test_layers_time_distributed.py b/tests/test_layers_time_distributed.py index b95994549..6ef6969e9 100644 --- a/tests/test_layers_time_distributed.py +++ b/tests/test_layers_time_distributed.py @@ -15,10 +15,14 @@ def model(x, is_train=True, reuse=False, name_scope="env1"): with tf.variable_scope(name_scope, reuse=reuse): net = tl.layers.InputLayer(x, name='input') net = tl.layers.TimeDistributedLayer( - net, layer_class=tl.layers.DenseLayer, args={ + net, + layer_class=tl.layers.DenseLayer, + args={ 'n_units': 50, 'name': 'dense' - }, name='time_dense') + }, + name='time_dense' + ) return net diff --git a/tests/test_mnist_simple.py b/tests/test_mnist_simple.py index 44fcba446..b1e51db30 100644 --- a/tests/test_mnist_simple.py +++ b/tests/test_mnist_simple.py @@ -83,11 +83,19 @@ def test_reuse_vgg(self): print_freq=1, X_val=X_val, y_val=y_val, - eval_train=False) + eval_train=False + ) # evaluation tl.utils.test( - sess, self.network, self.acc, X_test, y_test, self.x, self.y_, batch_size=None, cost=self.cost) + sess, self.network, self.acc, + X_test, + y_test, + self.x, + self.y_, + batch_size=None, + cost=self.cost + ) # save the network to .npz file tl.files.save_npz(self.network.all_params, name='model.npz') From 835092a4f2903cef9e20d4acc6398f93762ddb43 Mon Sep 17 00:00:00 2001 From: DEKHTIARJonathan Date: Fri, 20 Apr 2018 12:36:32 +0200 Subject: [PATCH 12/22] Code Refactored with new YAPF formating style --- ...torial_imagenet_inceptionV3_distributed.py | 13 +- example/tutorial_tfrecord3.py | 18 +- setup.cfg | 2 +- tensorlayer/db.py | 14 +- tensorlayer/distributed.py | 22 +- tensorlayer/files.py | 52 +- tensorlayer/layers/binary.py | 232 ++++----- tensorlayer/layers/convolution.py | 472 +++++++++--------- tensorlayer/layers/core.py | 158 +++--- tensorlayer/layers/extend.py | 8 +- tensorlayer/layers/importer.py | 40 +- tensorlayer/layers/merge.py | 18 +- tensorlayer/layers/normalization.py | 66 +-- tensorlayer/layers/object_detection.py | 12 +- tensorlayer/layers/padding.py | 34 +- tensorlayer/layers/pooling.py | 42 +- tensorlayer/layers/recurrent.py | 90 ++-- tensorlayer/layers/spatial_transformer.py | 10 +- tensorlayer/layers/special_activation.py | 14 +- tensorlayer/layers/stack.py | 8 +- tensorlayer/layers/time_distribution.py | 10 +- tensorlayer/nlp.py | 40 +- tensorlayer/prepro.py | 346 ++++++------- tensorlayer/utils.py | 38 +- tensorlayer/visualize.py | 16 +- 25 files changed, 889 insertions(+), 886 deletions(-) diff --git a/example/tutorial_imagenet_inceptionV3_distributed.py b/example/tutorial_imagenet_inceptionV3_distributed.py index 31adc94b0..f7ea0c88f 100644 --- a/example/tutorial_imagenet_inceptionV3_distributed.py +++ b/example/tutorial_imagenet_inceptionV3_distributed.py @@ -384,12 +384,13 @@ def run_worker(task_spec, checkpoints_path, batch_size=32, epochs=10): # start training hooks = [StopAtStepHook(last_step=steps_per_epoch * epochs)] with tl.distributed.DistributedSession( - task_spec=task_spec, - hooks=hooks, - checkpoint_dir=checkpoints_path, - save_summaries_secs=None, - save_summaries_steps=300, - save_checkpoint_secs=60 * 60) as sess: + task_spec=task_spec, + hooks=hooks, + checkpoint_dir=checkpoints_path, + save_summaries_secs=None, + save_summaries_steps=300, + save_checkpoint_secs=60 * 60 + ) as sess: # print network information if task_spec is None or task_spec.is_master(): network.print_params(False, session=sess) diff --git a/example/tutorial_tfrecord3.py b/example/tutorial_tfrecord3.py index 3223db6e0..ef71d4293 100644 --- a/example/tutorial_tfrecord3.py +++ b/example/tutorial_tfrecord3.py @@ -233,15 +233,15 @@ def distort_image(image, thread_id): def prefetch_input_data( - reader, - file_pattern, - is_training, - batch_size, - values_per_shard, - input_queue_capacity_factor=16, - num_reader_threads=1, - shard_queue_name="filename_queue", - value_queue_name="input_queue" + reader, + file_pattern, + is_training, + batch_size, + values_per_shard, + input_queue_capacity_factor=16, + num_reader_threads=1, + shard_queue_name="filename_queue", + value_queue_name="input_queue" ): """Prefetches string values from disk into an input queue. diff --git a/setup.cfg b/setup.cfg index 9b2617c64..65b8faa24 100644 --- a/setup.cfg +++ b/setup.cfg @@ -45,7 +45,7 @@ split_arguments_when_comma_terminated = True split_before_named_assigns = True # If an argument / parameter list is going to be split, then split before the first argument. -split_before_first_argument = True +split_before_first_argument = False # Split after the opening paren which surrounds an expression if it doesn't fit on a single line. split_before_expression_after_opening_paren = True diff --git a/tensorlayer/db.py b/tensorlayer/db.py index 3704f30b4..c5897235b 100644 --- a/tensorlayer/db.py +++ b/tensorlayer/db.py @@ -60,13 +60,13 @@ class TensorDB(object): """ def __init__( - self, - ip='localhost', - port=27017, - db_name='db_name', - user_name=None, - password='password', - studyID=None + self, + ip='localhost', + port=27017, + db_name='db_name', + user_name=None, + password='password', + studyID=None ): ## connect mongodb client = MongoClient(ip, port) diff --git a/tensorlayer/distributed.py b/tensorlayer/distributed.py index 155954221..d9ebb495d 100644 --- a/tensorlayer/distributed.py +++ b/tensorlayer/distributed.py @@ -192,17 +192,17 @@ def create_task_spec_def(): def create_distributed_session( - task_spec=None, - checkpoint_dir=None, - scaffold=None, - hooks=None, - chief_only_hooks=None, - save_checkpoint_secs=600, - save_summaries_steps=object(), - save_summaries_secs=object(), - config=None, - stop_grace_period_secs=120, - log_step_count_steps=100 + task_spec=None, + checkpoint_dir=None, + scaffold=None, + hooks=None, + chief_only_hooks=None, + save_checkpoint_secs=600, + save_summaries_steps=object(), + save_summaries_secs=object(), + config=None, + stop_grace_period_secs=120, + log_step_count_steps=100 ): """Creates a distributed session. diff --git a/tensorlayer/files.py b/tensorlayer/files.py index a01bf163a..5f9e57dcc 100644 --- a/tensorlayer/files.py +++ b/tensorlayer/files.py @@ -499,15 +499,15 @@ def load_matt_mahoney_text8_dataset(path='data'): def load_imdb_dataset( - path='data', - nb_words=None, - skip_top=0, - maxlen=None, - test_split=0.2, - seed=113, - start_char=1, - oov_char=2, - index_from=3 + path='data', + nb_words=None, + skip_top=0, + maxlen=None, + test_split=0.2, + seed=113, + start_char=1, + oov_char=2, + index_from=3 ): """Load IMDB dataset. @@ -958,11 +958,12 @@ def save_response_content(response, destination, chunk_size=32 * 1024): total_size = int(response.headers.get('content-length', 0)) with open(destination, "wb") as f: for chunk in tqdm( - response.iter_content(chunk_size), - total=total_size, - unit='B', - unit_scale=True, - desc=destination): + response.iter_content(chunk_size), + total=total_size, + unit='B', + unit_scale=True, + desc=destination + ): if chunk: # filter out keep-alive new chunks f.write(chunk) @@ -1475,11 +1476,12 @@ def save_joints(): head_x2s = anno['annorect']['x2'][0] head_y2s = anno['annorect']['y2'][0] for annopoint, head_x1, head_y1, head_x2, head_y2 in zip( - annopoints, - head_x1s, - head_y1s, - head_x2s, - head_y2s): + annopoints, + head_x1s, + head_y1s, + head_x2s, + head_y2s + ): if annopoint != []: head_rect = [ float(head_x1[0, 0]), @@ -1831,12 +1833,12 @@ def load_and_assign_npz_dict(name='model.npz', sess=None): def save_ckpt( - sess=None, - mode_name='model.ckpt', - save_dir='checkpoint', - var_list=None, - global_step=None, - printable=False + sess=None, + mode_name='model.ckpt', + save_dir='checkpoint', + var_list=None, + global_step=None, + printable=False ): """Save parameters into `ckpt` file. diff --git a/tensorlayer/layers/binary.py b/tensorlayer/layers/binary.py index f27f3fa89..c48ba0957 100644 --- a/tensorlayer/layers/binary.py +++ b/tensorlayer/layers/binary.py @@ -132,16 +132,16 @@ class BinaryDenseLayer(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - n_units=100, - act=tf.identity, - use_gemm=False, - W_init=tf.truncated_normal_initializer(stddev=0.1), - b_init=tf.constant_initializer(value=0.0), - W_init_args=None, - b_init_args=None, - name='binary_dense', + self, + prev_layer, + n_units=100, + act=tf.identity, + use_gemm=False, + W_init=tf.truncated_normal_initializer(stddev=0.1), + b_init=tf.constant_initializer(value=0.0), + W_init_args=None, + b_init_args=None, + name='binary_dense', ): super(BinaryDenseLayer, self).__init__(prev_layer=prev_layer, name=name) logging.info("BinaryDenseLayer %s: %d %s" % (name, n_units, act.__name__)) @@ -252,31 +252,31 @@ class BinaryConv2d(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - n_filter=32, - filter_size=(3, 3), - strides=(1, 1), - act=tf.identity, - padding='SAME', - use_gemm=False, - W_init=tf.truncated_normal_initializer(stddev=0.02), - b_init=tf.constant_initializer(value=0.0), - W_init_args=None, - b_init_args=None, - use_cudnn_on_gpu=None, - data_format=None, - # act=tf.identity, - # shape=(5, 5, 1, 100), - # strides=(1, 1, 1, 1), - # padding='SAME', - # W_init=tf.truncated_normal_initializer(stddev=0.02), - # b_init=tf.constant_initializer(value=0.0), - # W_init_args=None, - # b_init_args=None, - # use_cudnn_on_gpu=None, - # data_format=None, - name='binary_cnn2d', + self, + prev_layer, + n_filter=32, + filter_size=(3, 3), + strides=(1, 1), + act=tf.identity, + padding='SAME', + use_gemm=False, + W_init=tf.truncated_normal_initializer(stddev=0.02), + b_init=tf.constant_initializer(value=0.0), + W_init_args=None, + b_init_args=None, + use_cudnn_on_gpu=None, + data_format=None, + # act=tf.identity, + # shape=(5, 5, 1, 100), + # strides=(1, 1, 1, 1), + # padding='SAME', + # W_init=tf.truncated_normal_initializer(stddev=0.02), + # b_init=tf.constant_initializer(value=0.0), + # W_init_args=None, + # b_init_args=None, + # use_cudnn_on_gpu=None, + # data_format=None, + name='binary_cnn2d', ): super(BinaryConv2d, self).__init__(prev_layer=prev_layer, name=name) logging.info( @@ -385,16 +385,16 @@ class TernaryDenseLayer(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - n_units=100, - act=tf.identity, - use_gemm=False, - W_init=tf.truncated_normal_initializer(stddev=0.1), - b_init=tf.constant_initializer(value=0.0), - W_init_args=None, - b_init_args=None, - name='ternary_dense', + self, + prev_layer, + n_units=100, + act=tf.identity, + use_gemm=False, + W_init=tf.truncated_normal_initializer(stddev=0.1), + b_init=tf.constant_initializer(value=0.0), + W_init_args=None, + b_init_args=None, + name='ternary_dense', ): super(TernaryDenseLayer, self).__init__(prev_layer=prev_layer, name=name) logging.info("TernaryDenseLayer %s: %d %s" % (name, n_units, act.__name__)) @@ -506,31 +506,31 @@ class TernaryConv2d(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - n_filter=32, - filter_size=(3, 3), - strides=(1, 1), - act=tf.identity, - padding='SAME', - use_gemm=False, - W_init=tf.truncated_normal_initializer(stddev=0.02), - b_init=tf.constant_initializer(value=0.0), - W_init_args=None, - b_init_args=None, - use_cudnn_on_gpu=None, - data_format=None, - # act=tf.identity, - # shape=(5, 5, 1, 100), - # strides=(1, 1, 1, 1), - # padding='SAME', - # W_init=tf.truncated_normal_initializer(stddev=0.02), - # b_init=tf.constant_initializer(value=0.0), - # W_init_args=None, - # b_init_args=None, - # use_cudnn_on_gpu=None, - # data_format=None, - name='ternary_cnn2d', + self, + prev_layer, + n_filter=32, + filter_size=(3, 3), + strides=(1, 1), + act=tf.identity, + padding='SAME', + use_gemm=False, + W_init=tf.truncated_normal_initializer(stddev=0.02), + b_init=tf.constant_initializer(value=0.0), + W_init_args=None, + b_init_args=None, + use_cudnn_on_gpu=None, + data_format=None, + # act=tf.identity, + # shape=(5, 5, 1, 100), + # strides=(1, 1, 1, 1), + # padding='SAME', + # W_init=tf.truncated_normal_initializer(stddev=0.02), + # b_init=tf.constant_initializer(value=0.0), + # W_init_args=None, + # b_init_args=None, + # use_cudnn_on_gpu=None, + # data_format=None, + name='ternary_cnn2d', ): super(TernaryConv2d, self).__init__(prev_layer=prev_layer, name=name) logging.info( @@ -644,18 +644,18 @@ class DorefaDenseLayer(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - bitW=1, - bitA=3, - n_units=100, - act=tf.identity, - use_gemm=False, - W_init=tf.truncated_normal_initializer(stddev=0.1), - b_init=tf.constant_initializer(value=0.0), - W_init_args=None, - b_init_args=None, - name='dorefa_dense', + self, + prev_layer, + bitW=1, + bitA=3, + n_units=100, + act=tf.identity, + use_gemm=False, + W_init=tf.truncated_normal_initializer(stddev=0.1), + b_init=tf.constant_initializer(value=0.0), + W_init_args=None, + b_init_args=None, + name='dorefa_dense', ): super(DorefaDenseLayer, self).__init__(prev_layer=prev_layer, name=name) logging.info("DorefaDenseLayer %s: %d %s" % (name, n_units, act.__name__)) @@ -770,33 +770,33 @@ class DorefaConv2d(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - bitW=1, - bitA=3, - n_filter=32, - filter_size=(3, 3), - strides=(1, 1), - act=tf.identity, - padding='SAME', - use_gemm=False, - W_init=tf.truncated_normal_initializer(stddev=0.02), - b_init=tf.constant_initializer(value=0.0), - W_init_args=None, - b_init_args=None, - use_cudnn_on_gpu=None, - data_format=None, - # act=tf.identity, - # shape=(5, 5, 1, 100), - # strides=(1, 1, 1, 1), - # padding='SAME', - # W_init=tf.truncated_normal_initializer(stddev=0.02), - # b_init=tf.constant_initializer(value=0.0), - # W_init_args=None, - # b_init_args=None, - # use_cudnn_on_gpu=None, - # data_format=None, - name='dorefa_cnn2d', + self, + prev_layer, + bitW=1, + bitA=3, + n_filter=32, + filter_size=(3, 3), + strides=(1, 1), + act=tf.identity, + padding='SAME', + use_gemm=False, + W_init=tf.truncated_normal_initializer(stddev=0.02), + b_init=tf.constant_initializer(value=0.0), + W_init_args=None, + b_init_args=None, + use_cudnn_on_gpu=None, + data_format=None, + # act=tf.identity, + # shape=(5, 5, 1, 100), + # strides=(1, 1, 1, 1), + # padding='SAME', + # W_init=tf.truncated_normal_initializer(stddev=0.02), + # b_init=tf.constant_initializer(value=0.0), + # W_init_args=None, + # b_init_args=None, + # use_cudnn_on_gpu=None, + # data_format=None, + name='dorefa_cnn2d', ): super(DorefaConv2d, self).__init__(prev_layer=prev_layer, name=name) logging.info( @@ -891,9 +891,9 @@ class SignLayer(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - name='sign', + self, + prev_layer, + name='sign', ): super(SignLayer, self).__init__(prev_layer=prev_layer, name=name) @@ -924,10 +924,10 @@ class ScaleLayer(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - init_scale=0.05, - name='scale', + self, + prev_layer, + init_scale=0.05, + name='scale', ): super(ScaleLayer, self).__init__(prev_layer=prev_layer, name=name) logging.info("ScaleLayer %s: init_scale: %f" % (name, init_scale)) diff --git a/tensorlayer/layers/convolution.py b/tensorlayer/layers/convolution.py index 222a74bf0..95b69c9ef 100644 --- a/tensorlayer/layers/convolution.py +++ b/tensorlayer/layers/convolution.py @@ -64,19 +64,19 @@ class Conv1dLayer(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - act=tf.identity, - shape=(5, 1, 5), - stride=1, - dilation_rate=1, - padding='SAME', - data_format='NWC', - W_init=tf.truncated_normal_initializer(stddev=0.02), - b_init=tf.constant_initializer(value=0.0), - W_init_args=None, - b_init_args=None, - name='cnn1d', + self, + prev_layer, + act=tf.identity, + shape=(5, 1, 5), + stride=1, + dilation_rate=1, + padding='SAME', + data_format='NWC', + W_init=tf.truncated_normal_initializer(stddev=0.02), + b_init=tf.constant_initializer(value=0.0), + W_init_args=None, + b_init_args=None, + name='cnn1d', ): super(Conv1dLayer, self).__init__(prev_layer=prev_layer, name=name) logging.info( @@ -200,19 +200,19 @@ class Conv2dLayer(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - act=tf.identity, - shape=(5, 5, 1, 100), - strides=(1, 1, 1, 1), - padding='SAME', - W_init=tf.truncated_normal_initializer(stddev=0.02), - b_init=tf.constant_initializer(value=0.0), - W_init_args=None, - b_init_args=None, - use_cudnn_on_gpu=None, - data_format=None, - name='cnn_layer', + self, + prev_layer, + act=tf.identity, + shape=(5, 5, 1, 100), + strides=(1, 1, 1, 1), + padding='SAME', + W_init=tf.truncated_normal_initializer(stddev=0.02), + b_init=tf.constant_initializer(value=0.0), + W_init_args=None, + b_init_args=None, + use_cudnn_on_gpu=None, + data_format=None, + name='cnn_layer', ): super(Conv2dLayer, self).__init__(prev_layer=prev_layer, name=name) logging.info( @@ -356,18 +356,18 @@ class DeConv2dLayer(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - act=tf.identity, - shape=(3, 3, 128, 256), - output_shape=(1, 256, 256, 128), - strides=(1, 2, 2, 1), - padding='SAME', - W_init=tf.truncated_normal_initializer(stddev=0.02), - b_init=tf.constant_initializer(value=0.0), - W_init_args=None, - b_init_args=None, - name='decnn2d_layer', + self, + prev_layer, + act=tf.identity, + shape=(3, 3, 128, 256), + output_shape=(1, 256, 256, 128), + strides=(1, 2, 2, 1), + padding='SAME', + W_init=tf.truncated_normal_initializer(stddev=0.02), + b_init=tf.constant_initializer(value=0.0), + W_init_args=None, + b_init_args=None, + name='decnn2d_layer', ): super(DeConv2dLayer, self).__init__(prev_layer=prev_layer, name=name) logging.info( @@ -466,17 +466,17 @@ class Conv3dLayer(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - act=tf.identity, - shape=(2, 2, 2, 3, 32), - strides=(1, 2, 2, 2, 1), - padding='SAME', - W_init=tf.truncated_normal_initializer(stddev=0.02), - b_init=tf.constant_initializer(value=0.0), - W_init_args=None, - b_init_args=None, - name='cnn3d_layer', + self, + prev_layer, + act=tf.identity, + shape=(2, 2, 2, 3, 32), + strides=(1, 2, 2, 2, 1), + padding='SAME', + W_init=tf.truncated_normal_initializer(stddev=0.02), + b_init=tf.constant_initializer(value=0.0), + W_init_args=None, + b_init_args=None, + name='cnn3d_layer', ): super(Conv3dLayer, self).__init__(prev_layer=prev_layer, name=name) logging.info( @@ -563,18 +563,18 @@ class DeConv3dLayer(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - act=tf.identity, - shape=(2, 2, 2, 128, 256), - output_shape=(1, 12, 32, 32, 128), - strides=(1, 2, 2, 2, 1), - padding='SAME', - W_init=tf.truncated_normal_initializer(stddev=0.02), - b_init=tf.constant_initializer(value=0.0), - W_init_args=None, - b_init_args=None, - name='decnn3d_layer', + self, + prev_layer, + act=tf.identity, + shape=(2, 2, 2, 128, 256), + output_shape=(1, 12, 32, 32, 128), + strides=(1, 2, 2, 2, 1), + padding='SAME', + W_init=tf.truncated_normal_initializer(stddev=0.02), + b_init=tf.constant_initializer(value=0.0), + W_init_args=None, + b_init_args=None, + name='decnn3d_layer', ): super(DeConv3dLayer, self).__init__(prev_layer=prev_layer, name=name) logging.info( @@ -660,13 +660,13 @@ class UpSampling2dLayer(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - size, - is_scale=True, - method=0, - align_corners=False, - name='upsample2d_layer', + self, + prev_layer, + size, + is_scale=True, + method=0, + align_corners=False, + name='upsample2d_layer', ): super(UpSampling2dLayer, self).__init__(prev_layer=prev_layer, name=name) logging.info( @@ -747,13 +747,13 @@ class DownSampling2dLayer(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - size, - is_scale=True, - method=0, - align_corners=False, - name='downsample2d_layer', + self, + prev_layer, + size, + is_scale=True, + method=0, + align_corners=False, + name='downsample2d_layer', ): super(DownSampling2dLayer, self).__init__(prev_layer=prev_layer, name=name) logging.info( @@ -856,18 +856,18 @@ class DeformableConv2d(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - offset_layer=None, - # shape=(3, 3, 1, 100), - n_filter=32, - filter_size=(3, 3), - act=tf.identity, - name='deformable_conv_2d', - W_init=tf.truncated_normal_initializer(stddev=0.02), - b_init=tf.constant_initializer(value=0.0), - W_init_args=None, - b_init_args=None + self, + prev_layer, + offset_layer=None, + # shape=(3, 3, 1, 100), + n_filter=32, + filter_size=(3, 3), + act=tf.identity, + name='deformable_conv_2d', + W_init=tf.truncated_normal_initializer(stddev=0.02), + b_init=tf.constant_initializer(value=0.0), + W_init_args=None, + b_init_args=None ): if tf.__version__ < "1.4": @@ -1125,19 +1125,19 @@ def _tf_batch_map_offsets(inputs, offsets, grid_offset): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def atrous_conv1d( - prev_layer, - n_filter=32, - filter_size=2, - stride=1, - dilation=1, - act=tf.identity, - padding='SAME', - data_format='NWC', - W_init=tf.truncated_normal_initializer(stddev=0.02), - b_init=tf.constant_initializer(value=0.0), - W_init_args=None, - b_init_args=None, - name='conv1d', + prev_layer, + n_filter=32, + filter_size=2, + stride=1, + dilation=1, + act=tf.identity, + padding='SAME', + data_format='NWC', + W_init=tf.truncated_normal_initializer(stddev=0.02), + b_init=tf.constant_initializer(value=0.0), + W_init_args=None, + b_init_args=None, + name='conv1d', ): """Simplified version of :class:`AtrousConv1dLayer`. @@ -1233,18 +1233,18 @@ class AtrousConv2dLayer(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - n_filter=32, - filter_size=(3, 3), - rate=2, - act=tf.identity, - padding='SAME', - W_init=tf.truncated_normal_initializer(stddev=0.02), - b_init=tf.constant_initializer(value=0.0), - W_init_args=None, - b_init_args=None, - name='atrou2d' + self, + prev_layer, + n_filter=32, + filter_size=(3, 3), + rate=2, + act=tf.identity, + padding='SAME', + W_init=tf.truncated_normal_initializer(stddev=0.02), + b_init=tf.constant_initializer(value=0.0), + W_init_args=None, + b_init_args=None, + name='atrou2d' ): super(AtrousConv2dLayer, self).__init__(prev_layer=prev_layer, name=name) @@ -1354,25 +1354,25 @@ class _SeparableConv2dLayer(Layer): # TODO @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - n_filter, - filter_size=5, - strides=(1, 1), - padding='valid', - data_format='channels_last', - dilation_rate=(1, 1), - depth_multiplier=1, - act=tf.identity, - use_bias=True, - depthwise_initializer=None, - pointwise_initializer=None, - bias_initializer=tf.zeros_initializer, - depthwise_regularizer=None, - pointwise_regularizer=None, - bias_regularizer=None, - activity_regularizer=None, - name='atrou2d' + self, + prev_layer, + n_filter, + filter_size=5, + strides=(1, 1), + padding='valid', + data_format='channels_last', + dilation_rate=(1, 1), + depth_multiplier=1, + act=tf.identity, + use_bias=True, + depthwise_initializer=None, + pointwise_initializer=None, + bias_initializer=tf.zeros_initializer, + depthwise_regularizer=None, + pointwise_regularizer=None, + bias_regularizer=None, + activity_regularizer=None, + name='atrou2d' ): super(_SeparableConv2dLayer, self).__init__(prev_layer=prev_layer, name=name) @@ -1549,20 +1549,20 @@ class Conv1d(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - n_filter=32, - filter_size=5, - stride=1, - dilation_rate=1, - act=tf.identity, - padding='SAME', - data_format="channels_last", - W_init=tf.truncated_normal_initializer(stddev=0.02), - b_init=tf.constant_initializer(value=0.0), - W_init_args=None, - b_init_args=None, - name='conv1d' + self, + prev_layer, + n_filter=32, + filter_size=5, + stride=1, + dilation_rate=1, + act=tf.identity, + padding='SAME', + data_format="channels_last", + W_init=tf.truncated_normal_initializer(stddev=0.02), + b_init=tf.constant_initializer(value=0.0), + W_init_args=None, + b_init_args=None, + name='conv1d' ): super(Conv1d, self).__init__(prev_layer=prev_layer, name=name) @@ -1677,21 +1677,21 @@ class Conv2d(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - n_filter=32, - filter_size=(3, 3), - strides=(1, 1), - act=tf.identity, - padding='SAME', - dilation_rate=(1, 1), - W_init=tf.truncated_normal_initializer(stddev=0.02), - b_init=tf.constant_initializer(value=0.0), - W_init_args=None, - b_init_args=None, - use_cudnn_on_gpu=None, - data_format=None, - name='conv2d', + self, + prev_layer, + n_filter=32, + filter_size=(3, 3), + strides=(1, 1), + act=tf.identity, + padding='SAME', + dilation_rate=(1, 1), + W_init=tf.truncated_normal_initializer(stddev=0.02), + b_init=tf.constant_initializer(value=0.0), + W_init_args=None, + b_init_args=None, + use_cudnn_on_gpu=None, + data_format=None, + name='conv2d', ): # if W_init_args is None: # W_init_args = {} @@ -1872,20 +1872,20 @@ class DeConv2d(Layer): end_support_version=1.9 ) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - n_filter=32, - filter_size=(3, 3), - out_size=(30, 30), # remove - strides=(2, 2), - padding='SAME', - batch_size=None, # remove - act=tf.identity, - W_init=tf.truncated_normal_initializer(stddev=0.02), - b_init=tf.constant_initializer(value=0.0), - W_init_args=None, # remove - b_init_args=None, # remove - name='decnn2d' + self, + prev_layer, + n_filter=32, + filter_size=(3, 3), + out_size=(30, 30), # remove + strides=(2, 2), + padding='SAME', + batch_size=None, # remove + act=tf.identity, + W_init=tf.truncated_normal_initializer(stddev=0.02), + b_init=tf.constant_initializer(value=0.0), + W_init_args=None, # remove + b_init_args=None, # remove + name='decnn2d' ): super(DeConv2d, self).__init__(prev_layer=prev_layer, name=name) logging.info( @@ -1975,16 +1975,16 @@ class DeConv3d(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - n_filter=32, - filter_size=(3, 3, 3), - strides=(2, 2, 2), - padding='SAME', - act=tf.identity, - W_init=tf.truncated_normal_initializer(stddev=0.02), - b_init=tf.constant_initializer(value=0.0), - name='decnn3d' + self, + prev_layer, + n_filter=32, + filter_size=(3, 3, 3), + strides=(2, 2, 2), + padding='SAME', + act=tf.identity, + W_init=tf.truncated_normal_initializer(stddev=0.02), + b_init=tf.constant_initializer(value=0.0), + name='decnn3d' ): super(DeConv3d, self).__init__(prev_layer=prev_layer, name=name) @@ -2076,19 +2076,19 @@ class DepthwiseConv2d(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - shape=(3, 3), - strides=(1, 1), - act=tf.identity, - padding='SAME', - dilation_rate=(1, 1), - depth_multiplier=1, - W_init=tf.truncated_normal_initializer(stddev=0.02), - b_init=tf.constant_initializer(value=0.0), - W_init_args=None, - b_init_args=None, - name='depthwise_conv2d', + self, + prev_layer, + shape=(3, 3), + strides=(1, 1), + act=tf.identity, + padding='SAME', + dilation_rate=(1, 1), + depth_multiplier=1, + W_init=tf.truncated_normal_initializer(stddev=0.02), + b_init=tf.constant_initializer(value=0.0), + W_init_args=None, + b_init_args=None, + name='depthwise_conv2d', ): super(DepthwiseConv2d, self).__init__(prev_layer=prev_layer, name=name) logging.info( @@ -2196,32 +2196,32 @@ class SeparableConv2d(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - n_filter=100, - filter_size=(3, 3), - strides=(1, 1), - act=tf.identity, - padding='valid', - data_format='channels_last', - dilation_rate=(1, 1), - depth_multiplier=1, - # activation=None, - # use_bias=True, - depthwise_init=None, - pointwise_init=None, - b_init=tf.zeros_initializer(), - # depthwise_regularizer=None, - # pointwise_regularizer=None, - # bias_regularizer=None, - # activity_regularizer=None, - # depthwise_constraint=None, - # pointwise_constraint=None, - # W_init=tf.truncated_normal_initializer(stddev=0.1), - # b_init=tf.constant_initializer(value=0.0), - # W_init_args=None, - # b_init_args=None, - name='seperable', + self, + prev_layer, + n_filter=100, + filter_size=(3, 3), + strides=(1, 1), + act=tf.identity, + padding='valid', + data_format='channels_last', + dilation_rate=(1, 1), + depth_multiplier=1, + # activation=None, + # use_bias=True, + depthwise_init=None, + pointwise_init=None, + b_init=tf.zeros_initializer(), + # depthwise_regularizer=None, + # pointwise_regularizer=None, + # bias_regularizer=None, + # activity_regularizer=None, + # depthwise_constraint=None, + # pointwise_constraint=None, + # W_init=tf.truncated_normal_initializer(stddev=0.1), + # b_init=tf.constant_initializer(value=0.0), + # W_init_args=None, + # b_init_args=None, + name='seperable', ): # if W_init_args is None: # W_init_args = {} @@ -2305,19 +2305,19 @@ class GroupConv2d(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - n_filter=32, - filter_size=(3, 3), - strides=(2, 2), - n_group=2, - act=tf.identity, - padding='SAME', - W_init=tf.truncated_normal_initializer(stddev=0.02), - b_init=tf.constant_initializer(value=0.0), - W_init_args=None, - b_init_args=None, - name='groupconv', + self, + prev_layer, + n_filter=32, + filter_size=(3, 3), + strides=(2, 2), + n_group=2, + act=tf.identity, + padding='SAME', + W_init=tf.truncated_normal_initializer(stddev=0.02), + b_init=tf.constant_initializer(value=0.0), + W_init_args=None, + b_init_args=None, + name='groupconv', ): # Windaway super(GroupConv2d, self).__init__(prev_layer=prev_layer, name=name) diff --git a/tensorlayer/layers/core.py b/tensorlayer/layers/core.py index 994fef763..5c8d19a64 100644 --- a/tensorlayer/layers/core.py +++ b/tensorlayer/layers/core.py @@ -658,20 +658,20 @@ class Word2vecEmbeddingInputlayer(Layer): """ def __init__( - self, - inputs=None, - train_labels=None, - vocabulary_size=80000, - embedding_size=200, - num_sampled=64, - nce_loss_args=None, - E_init=tf.random_uniform_initializer(minval=-1.0, maxval=1.0), - E_init_args=None, - nce_W_init=tf.truncated_normal_initializer(stddev=0.03), - nce_W_init_args=None, - nce_b_init=tf.constant_initializer(value=0.0), - nce_b_init_args=None, - name='word2vec', + self, + inputs=None, + train_labels=None, + vocabulary_size=80000, + embedding_size=200, + num_sampled=64, + nce_loss_args=None, + E_init=tf.random_uniform_initializer(minval=-1.0, maxval=1.0), + E_init_args=None, + nce_W_init=tf.truncated_normal_initializer(stddev=0.03), + nce_W_init_args=None, + nce_b_init=tf.constant_initializer(value=0.0), + nce_b_init_args=None, + name='word2vec', ): if nce_loss_args is None: nce_loss_args = {} @@ -781,13 +781,13 @@ class EmbeddingInputlayer(Layer): """ def __init__( - self, - inputs=None, - vocabulary_size=80000, - embedding_size=200, - E_init=tf.random_uniform_initializer(-0.1, 0.1), - E_init_args=None, - name='embedding', + self, + inputs=None, + vocabulary_size=80000, + embedding_size=200, + E_init=tf.random_uniform_initializer(-0.1, 0.1), + E_init_args=None, + name='embedding', ): if E_init_args is None: E_init_args = {} @@ -852,14 +852,14 @@ class AverageEmbeddingInputlayer(Layer): """ def __init__( - self, - inputs, - vocabulary_size, - embedding_size, - pad_value=0, - embeddings_initializer=tf.random_uniform_initializer(-0.1, 0.1), - embeddings_kwargs=None, - name='average_embedding', + self, + inputs, + vocabulary_size, + embedding_size, + pad_value=0, + embeddings_initializer=tf.random_uniform_initializer(-0.1, 0.1), + embeddings_kwargs=None, + name='average_embedding', ): super(AverageEmbeddingInputlayer, self).__init__(prev_layer=None, name=name) @@ -963,15 +963,15 @@ class DenseLayer(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - n_units=100, - act=tf.identity, - W_init=tf.truncated_normal_initializer(stddev=0.1), - b_init=tf.constant_initializer(value=0.0), - W_init_args=None, - b_init_args=None, - name='dense', + self, + prev_layer, + n_units=100, + act=tf.identity, + W_init=tf.truncated_normal_initializer(stddev=0.1), + b_init=tf.constant_initializer(value=0.0), + W_init_args=None, + b_init_args=None, + name='dense', ): super(DenseLayer, self).__init__(prev_layer=prev_layer, name=name) @@ -1068,12 +1068,12 @@ class ReconLayer(DenseLayer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - x_recon=None, - n_units=784, - act=tf.nn.softplus, - name='recon', + self, + prev_layer, + x_recon=None, + n_units=784, + act=tf.nn.softplus, + name='recon', ): super(ReconLayer, self).__init__(prev_layer=prev_layer, n_units=n_units, act=act, name=name) @@ -1172,17 +1172,17 @@ def __init__( # self.train_op = tf.train.GradientDescentOptimizer(1.0).minimize(self.cost, var_list=self.train_params) def pretrain( - self, - sess, - x, - X_train, - X_val, - denoise_name=None, - n_epoch=100, - batch_size=128, - print_freq=10, - save=True, - save_name='w1pre_' + self, + sess, + x, + X_train, + X_val, + denoise_name=None, + n_epoch=100, + batch_size=128, + print_freq=10, + save=True, + save_name='w1pre_' ): # ==================================================== # @@ -1305,13 +1305,13 @@ class DropoutLayer(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - keep=0.5, - is_fix=False, - is_train=True, - seed=None, - name='dropout_layer', + self, + prev_layer, + keep=0.5, + is_fix=False, + is_train=True, + seed=None, + name='dropout_layer', ): super(DropoutLayer, self).__init__(prev_layer=prev_layer, name=name) logging.info("DropoutLayer %s: keep:%f is_fix:%s" % (name, keep, is_fix)) @@ -1390,13 +1390,13 @@ class GaussianNoiseLayer(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - mean=0.0, - stddev=1.0, - is_train=True, - seed=None, - name='gaussian_noise_layer', + self, + prev_layer, + mean=0.0, + stddev=1.0, + is_train=True, + seed=None, + name='gaussian_noise_layer', ): super(GaussianNoiseLayer, self).__init__(prev_layer=prev_layer, name=name) @@ -1465,16 +1465,16 @@ class DropconnectDenseLayer(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - keep=0.5, - n_units=100, - act=tf.identity, - W_init=tf.truncated_normal_initializer(stddev=0.1), - b_init=tf.constant_initializer(value=0.0), - W_init_args=None, - b_init_args=None, - name='dropconnect_layer', + self, + prev_layer, + keep=0.5, + n_units=100, + act=tf.identity, + W_init=tf.truncated_normal_initializer(stddev=0.1), + b_init=tf.constant_initializer(value=0.0), + W_init_args=None, + b_init_args=None, + name='dropconnect_layer', ): super(DropconnectDenseLayer, self).__init__(prev_layer=prev_layer, name=name) logging.info("DropconnectDenseLayer %s: %d %s" % (name, n_units, act.__name__)) diff --git a/tensorlayer/layers/extend.py b/tensorlayer/layers/extend.py index 8c3fa52b1..87b5cbc21 100644 --- a/tensorlayer/layers/extend.py +++ b/tensorlayer/layers/extend.py @@ -37,10 +37,10 @@ class ExpandDimsLayer(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - axis, - name='expand_dims', + self, + prev_layer, + axis, + name='expand_dims', ): super(ExpandDimsLayer, self).__init__(prev_layer=prev_layer, name=name) logging.info("ExpandDimsLayer %s: axis:%d" % (name, axis)) diff --git a/tensorlayer/layers/importer.py b/tensorlayer/layers/importer.py index 632b59d99..dd0b73a60 100644 --- a/tensorlayer/layers/importer.py +++ b/tensorlayer/layers/importer.py @@ -57,11 +57,11 @@ class LambdaLayer(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - fn, - fn_args=None, - name='lambda_layer', + self, + prev_layer, + fn, + fn_args=None, + name='lambda_layer', ): super(LambdaLayer, self).__init__(prev_layer=prev_layer, name=name) @@ -111,11 +111,11 @@ class SlimNetsLayer(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - slim_layer, - slim_args=None, - name='tfslim_layer', + self, + prev_layer, + slim_layer, + slim_args=None, + name='tfslim_layer', ): super(SlimNetsLayer, self).__init__(prev_layer=prev_layer, name=name) @@ -177,11 +177,11 @@ class KerasLayer(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - keras_layer, - keras_args=None, - name='keras_layer', + self, + prev_layer, + keras_layer, + keras_args=None, + name='keras_layer', ): super(KerasLayer, self).__init__(prev_layer=prev_layer, name=name) @@ -227,11 +227,11 @@ class EstimatorLayer(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - model_fn, - args=None, - name='estimator_layer', + self, + prev_layer, + model_fn, + args=None, + name='estimator_layer', ): super(EstimatorLayer, self).__init__(prev_layer=prev_layer, name=name) logging.info("EstimatorLayer %s: %s" % (name, model_fn)) diff --git a/tensorlayer/layers/merge.py b/tensorlayer/layers/merge.py index 9837d4b0f..95fbfbeb9 100644 --- a/tensorlayer/layers/merge.py +++ b/tensorlayer/layers/merge.py @@ -50,10 +50,10 @@ class ConcatLayer(Layer): """ def __init__( - self, - layers, - concat_dim=-1, - name='concat_layer', + self, + layers, + concat_dim=-1, + name='concat_layer', ): super(ConcatLayer, self).__init__(prev_layer=layers, name=name) @@ -116,11 +116,11 @@ class ElementwiseLayer(Layer): """ def __init__( - self, - layers, - combine_fn=tf.minimum, - act=None, - name='elementwise_layer', + self, + layers, + combine_fn=tf.minimum, + act=None, + name='elementwise_layer', ): super(ElementwiseLayer, self).__init__(prev_layer=layers, name=name) diff --git a/tensorlayer/layers/normalization.py b/tensorlayer/layers/normalization.py index b4808d61e..110acf8b4 100644 --- a/tensorlayer/layers/normalization.py +++ b/tensorlayer/layers/normalization.py @@ -40,13 +40,13 @@ class LocalResponseNormLayer(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - depth_radius=None, - bias=None, - alpha=None, - beta=None, - name='lrn_layer', + self, + prev_layer, + depth_radius=None, + bias=None, + alpha=None, + beta=None, + name='lrn_layer', ): super(LocalResponseNormLayer, self).__init__(prev_layer=prev_layer, name=name) logging.info( @@ -106,15 +106,15 @@ class BatchNormLayer(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - decay=0.9, - epsilon=0.00001, - act=tf.identity, - is_train=False, - beta_init=tf.zeros_initializer, - gamma_init=tf.random_normal_initializer(mean=1.0, stddev=0.002), - name='batchnorm_layer', + self, + prev_layer, + decay=0.9, + epsilon=0.00001, + act=tf.identity, + is_train=False, + beta_init=tf.zeros_initializer, + gamma_init=tf.random_normal_initializer(mean=1.0, stddev=0.002), + name='batchnorm_layer', ): super(BatchNormLayer, self).__init__(prev_layer=prev_layer, name=name) logging.info( @@ -254,11 +254,11 @@ class InstanceNormLayer(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - act=tf.identity, - epsilon=1e-5, - name='instan_norm', + self, + prev_layer, + act=tf.identity, + epsilon=1e-5, + name='instan_norm', ): super(InstanceNormLayer, self).__init__(prev_layer=prev_layer, name=name) logging.info("InstanceNormLayer %s: epsilon:%f act:%s" % (self.name, epsilon, act.__name__)) @@ -305,18 +305,18 @@ class LayerNormLayer(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - center=True, - scale=True, - act=tf.identity, - reuse=None, - variables_collections=None, - outputs_collections=None, - trainable=True, - begin_norm_axis=1, - begin_params_axis=-1, - name='layernorm' + self, + prev_layer, + center=True, + scale=True, + act=tf.identity, + reuse=None, + variables_collections=None, + outputs_collections=None, + trainable=True, + begin_norm_axis=1, + begin_params_axis=-1, + name='layernorm' ): super(LayerNormLayer, self).__init__(prev_layer=prev_layer, name=name) diff --git a/tensorlayer/layers/object_detection.py b/tensorlayer/layers/object_detection.py index 2fb1a2ccf..981d03232 100644 --- a/tensorlayer/layers/object_detection.py +++ b/tensorlayer/layers/object_detection.py @@ -36,12 +36,12 @@ class ROIPoolingLayer(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - rois, - pool_height=2, - pool_width=2, - name='roipooling_layer', + self, + prev_layer, + rois, + pool_height=2, + pool_width=2, + name='roipooling_layer', ): super(ROIPoolingLayer, self).__init__(prev_layer=prev_layer, name=name) logging.info("ROIPoolingLayer %s: (%d, %d)" % (name, pool_height, pool_width)) diff --git a/tensorlayer/layers/padding.py b/tensorlayer/layers/padding.py index 64314c589..32d4d119c 100644 --- a/tensorlayer/layers/padding.py +++ b/tensorlayer/layers/padding.py @@ -39,11 +39,11 @@ class PadLayer(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - padding=None, - mode='CONSTANT', - name='pad_layer', + self, + prev_layer, + padding=None, + mode='CONSTANT', + name='pad_layer', ): super(PadLayer, self).__init__(prev_layer=prev_layer, name=name) logging.info("PadLayer %s: padding:%s mode:%s" % (name, list(padding), mode)) @@ -77,10 +77,10 @@ class ZeroPad1d(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - padding, - name='zeropad1d', + self, + prev_layer, + padding, + name='zeropad1d', ): super(ZeroPad1d, self).__init__(prev_layer=prev_layer, name=name) logging.info("ZeroPad1d %s: padding:%s" % (name, str(padding))) @@ -113,10 +113,10 @@ class ZeroPad2d(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - padding, - name='zeropad2d', + self, + prev_layer, + padding, + name='zeropad2d', ): super(ZeroPad2d, self).__init__(prev_layer=prev_layer, name=name) logging.info("ZeroPad2d %s: padding:%s" % (name, str(padding))) @@ -148,10 +148,10 @@ class ZeroPad3d(Layer): """ def __init__( - self, - prev_layer, - padding, - name='zeropad3d', + self, + prev_layer, + padding, + name='zeropad3d', ): super(ZeroPad3d, self).__init__(prev_layer=prev_layer, name=name) logging.info("ZeroPad3d %s: padding:%s" % (name, str(padding))) diff --git a/tensorlayer/layers/pooling.py b/tensorlayer/layers/pooling.py index dce062eb4..2929dad5f 100644 --- a/tensorlayer/layers/pooling.py +++ b/tensorlayer/layers/pooling.py @@ -58,13 +58,13 @@ class PoolLayer(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - ksize=(1, 2, 2, 1), - strides=(1, 2, 2, 1), - padding='SAME', - pool=tf.nn.max_pool, - name='pool_layer', + self, + prev_layer, + ksize=(1, 2, 2, 1), + strides=(1, 2, 2, 1), + padding='SAME', + pool=tf.nn.max_pool, + name='pool_layer', ): super(PoolLayer, self).__init__(prev_layer=prev_layer, name=name) logging.info( @@ -317,13 +317,13 @@ class MaxPool3d(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - filter_size=(3, 3, 3), - strides=(2, 2, 2), - padding='valid', - data_format='channels_last', - name='maxpool3d' + self, + prev_layer, + filter_size=(3, 3, 3), + strides=(2, 2, 2), + padding='valid', + data_format='channels_last', + name='maxpool3d' ): super(MaxPool3d, self).__init__(prev_layer=prev_layer, name=name) @@ -379,13 +379,13 @@ class MeanPool3d(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - filter_size=(3, 3, 3), - strides=(2, 2, 2), - padding='valid', - data_format='channels_last', - name='meanpool3d' + self, + prev_layer, + filter_size=(3, 3, 3), + strides=(2, 2, 2), + padding='valid', + data_format='channels_last', + name='meanpool3d' ): super(MeanPool3d, self).__init__(prev_layer=prev_layer, name=name) diff --git a/tensorlayer/layers/recurrent.py b/tensorlayer/layers/recurrent.py index 94dde3a22..0ddac43b5 100644 --- a/tensorlayer/layers/recurrent.py +++ b/tensorlayer/layers/recurrent.py @@ -134,17 +134,17 @@ class RNNLayer(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - cell_fn, - cell_init_args=None, - n_hidden=100, - initializer=tf.random_uniform_initializer(-0.1, 0.1), - n_steps=5, - initial_state=None, - return_last=False, - return_seq_2d=False, - name='rnn', + self, + prev_layer, + cell_fn, + cell_init_args=None, + n_hidden=100, + initializer=tf.random_uniform_initializer(-0.1, 0.1), + n_steps=5, + initial_state=None, + return_last=False, + return_seq_2d=False, + name='rnn', ): super(RNNLayer, self).__init__(prev_layer=prev_layer, name=name) @@ -329,20 +329,20 @@ class BiRNNLayer(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - cell_fn, - cell_init_args=None, - n_hidden=100, - initializer=tf.random_uniform_initializer(-0.1, 0.1), - n_steps=5, - fw_initial_state=None, - bw_initial_state=None, - dropout=None, - n_layer=1, - return_last=False, - return_seq_2d=False, - name='birnn', + self, + prev_layer, + cell_fn, + cell_init_args=None, + n_hidden=100, + initializer=tf.random_uniform_initializer(-0.1, 0.1), + n_steps=5, + fw_initial_state=None, + bw_initial_state=None, + dropout=None, + n_layer=1, + return_last=False, + return_seq_2d=False, + name='birnn', ): super(BiRNNLayer, self).__init__(prev_layer=prev_layer, name=name) @@ -556,14 +556,14 @@ class BasicConvLSTMCell(ConvRNNCell): """ def __init__( - self, - shape, - filter_size, - num_features, - forget_bias=1.0, - input_size=None, - state_is_tuple=False, - act=tf.nn.tanh + self, + shape, + filter_size, + num_features, + forget_bias=1.0, + input_size=None, + state_is_tuple=False, + act=tf.nn.tanh ): """Initialize the basic Conv LSTM cell.""" # if not state_is_tuple: @@ -732,18 +732,18 @@ class ConvLSTMLayer(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - cell_shape=None, - feature_map=1, - filter_size=(3, 3), - cell_fn=BasicConvLSTMCell, - initializer=tf.random_uniform_initializer(-0.1, 0.1), - n_steps=5, - initial_state=None, - return_last=False, - return_seq_2d=False, - name='convlstm', + self, + prev_layer, + cell_shape=None, + feature_map=1, + filter_size=(3, 3), + cell_fn=BasicConvLSTMCell, + initializer=tf.random_uniform_initializer(-0.1, 0.1), + n_steps=5, + initial_state=None, + return_last=False, + return_seq_2d=False, + name='convlstm', ): super(ConvLSTMLayer, self).__init__(prev_layer=prev_layer, name=name) diff --git a/tensorlayer/layers/spatial_transformer.py b/tensorlayer/layers/spatial_transformer.py index 5c50e465c..a918a0df2 100644 --- a/tensorlayer/layers/spatial_transformer.py +++ b/tensorlayer/layers/spatial_transformer.py @@ -233,11 +233,11 @@ class SpatialTransformer2dAffineLayer(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - theta_layer, - out_size=None, - name='spatial_trans_2d_affine', + self, + prev_layer, + theta_layer, + out_size=None, + name='spatial_trans_2d_affine', ): super(SpatialTransformer2dAffineLayer, self).__init__(prev_layer=[prev_layer, theta_layer], name=name) diff --git a/tensorlayer/layers/special_activation.py b/tensorlayer/layers/special_activation.py index 7d50fa5bb..7d042193a 100644 --- a/tensorlayer/layers/special_activation.py +++ b/tensorlayer/layers/special_activation.py @@ -37,13 +37,13 @@ class PReluLayer(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - channel_shared=False, - a_init=tf.constant_initializer(value=0.0), - a_init_args=None, - # restore = True, - name="prelu_layer" + self, + prev_layer, + channel_shared=False, + a_init=tf.constant_initializer(value=0.0), + a_init_args=None, + # restore = True, + name="prelu_layer" ): if a_init_args is None: diff --git a/tensorlayer/layers/stack.py b/tensorlayer/layers/stack.py index dd9bde8be..3e1b76bb2 100644 --- a/tensorlayer/layers/stack.py +++ b/tensorlayer/layers/stack.py @@ -39,10 +39,10 @@ class StackLayer(Layer): """ def __init__( - self, - layers, - axis=1, - name='stack', + self, + layers, + axis=1, + name='stack', ): super(StackLayer, self).__init__(prev_layer=layers, name=name) diff --git a/tensorlayer/layers/time_distribution.py b/tensorlayer/layers/time_distribution.py index fcec358e1..7aec9cfd1 100644 --- a/tensorlayer/layers/time_distribution.py +++ b/tensorlayer/layers/time_distribution.py @@ -50,11 +50,11 @@ class TimeDistributedLayer(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - layer_class=None, - args=None, - name='time_distributed', + self, + prev_layer, + layer_class=None, + args=None, + name='time_distributed', ): super(TimeDistributedLayer, self).__init__(prev_layer=prev_layer, name=name) logging.info("TimeDistributedLayer %s: layer_class:%s args:%s" % (self.name, layer_class.__name__, args)) diff --git a/tensorlayer/nlp.py b/tensorlayer/nlp.py index 75791dba7..642444a18 100755 --- a/tensorlayer/nlp.py +++ b/tensorlayer/nlp.py @@ -865,13 +865,13 @@ def basic_tokenizer(sentence, _WORD_SPLIT=re.compile(b"([.,!?\"':;)(])")): def create_vocabulary( - vocabulary_path, - data_path, - max_vocabulary_size, - tokenizer=None, - normalize_digits=True, - _DIGIT_RE=re.compile(br"\d"), - _START_VOCAB=None + vocabulary_path, + data_path, + max_vocabulary_size, + tokenizer=None, + normalize_digits=True, + _DIGIT_RE=re.compile(br"\d"), + _START_VOCAB=None ): """Create vocabulary file (if it does not exist yet) from data file. @@ -978,12 +978,12 @@ def initialize_vocabulary(vocabulary_path): def sentence_to_token_ids( - sentence, - vocabulary, - tokenizer=None, - normalize_digits=True, - UNK_ID=3, - _DIGIT_RE=re.compile(br"\d") + sentence, + vocabulary, + tokenizer=None, + normalize_digits=True, + UNK_ID=3, + _DIGIT_RE=re.compile(br"\d") ): """Convert a string to list of integers representing token-ids. @@ -1019,13 +1019,13 @@ def sentence_to_token_ids( def data_to_token_ids( - data_path, - target_path, - vocabulary_path, - tokenizer=None, - normalize_digits=True, - UNK_ID=3, - _DIGIT_RE=re.compile(br"\d") + data_path, + target_path, + vocabulary_path, + tokenizer=None, + normalize_digits=True, + UNK_ID=3, + _DIGIT_RE=re.compile(br"\d") ): """Tokenize data file and turn into token-ids using given vocabulary file. diff --git a/tensorlayer/prepro.py b/tensorlayer/prepro.py index 9480aeb85..4e236628c 100644 --- a/tensorlayer/prepro.py +++ b/tensorlayer/prepro.py @@ -204,15 +204,15 @@ def apply_fn(results, i, data, kwargs): def rotation( - x, - rg=20, - is_random=False, - row_index=0, - col_index=1, - channel_index=2, - fill_mode='nearest', - cval=0., - order=1 + x, + rg=20, + is_random=False, + row_index=0, + col_index=1, + channel_index=2, + fill_mode='nearest', + cval=0., + order=1 ): """Rotate an image randomly or non-randomly. @@ -258,15 +258,15 @@ def rotation( def rotation_multi( - x, - rg=20, - is_random=False, - row_index=0, - col_index=1, - channel_index=2, - fill_mode='nearest', - cval=0., - order=1 + x, + rg=20, + is_random=False, + row_index=0, + col_index=1, + channel_index=2, + fill_mode='nearest', + cval=0., + order=1 ): """Rotate multiple images with the same arguments, randomly or non-randomly. Usually be used for image segmentation which x=[X, Y], X and Y should be matched. @@ -470,16 +470,16 @@ def flip_axis_multi(x, axis, is_random=False): # shift def shift( - x, - wrg=0.1, - hrg=0.1, - is_random=False, - row_index=0, - col_index=1, - channel_index=2, - fill_mode='nearest', - cval=0., - order=1 + x, + wrg=0.1, + hrg=0.1, + is_random=False, + row_index=0, + col_index=1, + channel_index=2, + fill_mode='nearest', + cval=0., + order=1 ): """Shift an image randomly or non-randomly. @@ -522,16 +522,16 @@ def shift( def shift_multi( - x, - wrg=0.1, - hrg=0.1, - is_random=False, - row_index=0, - col_index=1, - channel_index=2, - fill_mode='nearest', - cval=0., - order=1 + x, + wrg=0.1, + hrg=0.1, + is_random=False, + row_index=0, + col_index=1, + channel_index=2, + fill_mode='nearest', + cval=0., + order=1 ): """Shift images with the same arguments, randomly or non-randomly. Usually be used for image segmentation which x=[X, Y], X and Y should be matched. @@ -566,15 +566,15 @@ def shift_multi( # shear def shear( - x, - intensity=0.1, - is_random=False, - row_index=0, - col_index=1, - channel_index=2, - fill_mode='nearest', - cval=0., - order=1 + x, + intensity=0.1, + is_random=False, + row_index=0, + col_index=1, + channel_index=2, + fill_mode='nearest', + cval=0., + order=1 ): """Shear an image randomly or non-randomly. @@ -619,15 +619,15 @@ def shear( def shear_multi( - x, - intensity=0.1, - is_random=False, - row_index=0, - col_index=1, - channel_index=2, - fill_mode='nearest', - cval=0., - order=1 + x, + intensity=0.1, + is_random=False, + row_index=0, + col_index=1, + channel_index=2, + fill_mode='nearest', + cval=0., + order=1 ): """Shear images with the same arguments, randomly or non-randomly. Usually be used for image segmentation which x=[X, Y], X and Y should be matched. @@ -660,15 +660,15 @@ def shear_multi( def shear2( - x, - shear=(0.1, 0.1), - is_random=False, - row_index=0, - col_index=1, - channel_index=2, - fill_mode='nearest', - cval=0., - order=1 + x, + shear=(0.1, 0.1), + is_random=False, + row_index=0, + col_index=1, + channel_index=2, + fill_mode='nearest', + cval=0., + order=1 ): """Shear an image randomly or non-randomly. @@ -715,15 +715,15 @@ def shear2( def shear_multi2( - x, - shear=(0.1, 0.1), - is_random=False, - row_index=0, - col_index=1, - channel_index=2, - fill_mode='nearest', - cval=0., - order=1 + x, + shear=(0.1, 0.1), + is_random=False, + row_index=0, + col_index=1, + channel_index=2, + fill_mode='nearest', + cval=0., + order=1 ): """Shear images with the same arguments, randomly or non-randomly. Usually be used for image segmentation which x=[X, Y], X and Y should be matched. @@ -760,18 +760,18 @@ def shear_multi2( # swirl def swirl( - x, - center=None, - strength=1, - radius=100, - rotation=0, - output_shape=None, - order=1, - mode='constant', - cval=0, - clip=True, - preserve_range=False, - is_random=False + x, + center=None, + strength=1, + radius=100, + rotation=0, + output_shape=None, + order=1, + mode='constant', + cval=0, + clip=True, + preserve_range=False, + is_random=False ): """Swirl an image randomly or non-randomly, see `scikit-image swirl API `__ and `example `__. @@ -851,18 +851,18 @@ def swirl( def swirl_multi( - x, - center=None, - strength=1, - radius=100, - rotation=0, - output_shape=None, - order=1, - mode='constant', - cval=0, - clip=True, - preserve_range=False, - is_random=False + x, + center=None, + strength=1, + radius=100, + rotation=0, + output_shape=None, + order=1, + mode='constant', + cval=0, + clip=True, + preserve_range=False, + is_random=False ): """Swirl multiple images with the same arguments, randomly or non-randomly. Usually be used for image segmentation which x=[X, Y], X and Y should be matched. @@ -1025,15 +1025,15 @@ def elastic_transform_multi(x, alpha, sigma, mode="constant", cval=0, is_random= # zoom def zoom( - x, - zoom_range=(0.9, 1.1), - is_random=False, - row_index=0, - col_index=1, - channel_index=2, - fill_mode='nearest', - cval=0., - order=1 + x, + zoom_range=(0.9, 1.1), + is_random=False, + row_index=0, + col_index=1, + channel_index=2, + fill_mode='nearest', + cval=0., + order=1 ): """Zoom in and out of a single image, randomly or non-randomly. @@ -1082,15 +1082,15 @@ def zoom( def zoom_multi( - x, - zoom_range=(0.9, 1.1), - is_random=False, - row_index=0, - col_index=1, - channel_index=2, - fill_mode='nearest', - cval=0., - order=1 + x, + zoom_range=(0.9, 1.1), + is_random=False, + row_index=0, + col_index=1, + channel_index=2, + fill_mode='nearest', + cval=0., + order=1 ): """Zoom in and out of images with the same arguments, randomly or non-randomly. Usually be used for image segmentation which x=[X, Y], X and Y should be matched. @@ -1504,12 +1504,12 @@ def pixel_value_scale(im, val=0.9, clip=(-np.inf, np.inf), is_random=False): # normailization def samplewise_norm( - x, - rescale=None, - samplewise_center=False, - samplewise_std_normalization=False, - channel_index=2, - epsilon=1e-7 + x, + rescale=None, + samplewise_center=False, + samplewise_std_normalization=False, + channel_index=2, + epsilon=1e-7 ): """Normalize an image by rescale, samplewise centering and samplewise centering in order. @@ -1859,16 +1859,16 @@ def apply_transform(x, transform_matrix, channel_index=2, fill_mode='nearest', c def projective_transform_by_points( - x, - src, - dst, - map_args=None, - output_shape=None, - order=1, - mode='constant', - cval=0.0, - clip=True, - preserve_range=False + x, + src, + dst, + map_args=None, + output_shape=None, + order=1, + mode='constant', + cval=0.0, + clip=True, + preserve_range=False ): """Projective transform by given coordinates, usually 4 coordinates. @@ -2616,16 +2616,16 @@ def obj_box_imresize(im, coords=None, size=None, interp='bicubic', mode=None, is def obj_box_crop( - im, - classes=None, - coords=None, - wrg=100, - hrg=100, - is_rescale=False, - is_center=False, - is_random=False, - thresh_wh=0.02, - thresh_wh2=12. + im, + classes=None, + coords=None, + wrg=100, + hrg=100, + is_rescale=False, + is_center=False, + is_random=False, + thresh_wh=0.02, + thresh_wh2=12. ): """Randomly or centrally crop an image, and compute the new bounding box coordinates. Objects outside the cropped image will be removed. @@ -2771,22 +2771,22 @@ def _get_coord(coord): def obj_box_shift( - im, - classes=None, - coords=None, - wrg=0.1, - hrg=0.1, - row_index=0, - col_index=1, - channel_index=2, - fill_mode='nearest', - cval=0., - order=1, - is_rescale=False, - is_center=False, - is_random=False, - thresh_wh=0.02, - thresh_wh2=12. + im, + classes=None, + coords=None, + wrg=0.1, + hrg=0.1, + row_index=0, + col_index=1, + channel_index=2, + fill_mode='nearest', + cval=0., + order=1, + is_rescale=False, + is_center=False, + is_random=False, + thresh_wh=0.02, + thresh_wh2=12. ): """Shift an image randomly or non-randomly, and compute the new bounding box coordinates. Objects outside the cropped image will be removed. @@ -2914,21 +2914,21 @@ def _get_coord(coord): def obj_box_zoom( - im, - classes=None, - coords=None, - zoom_range=(0.9, 1.1), - row_index=0, - col_index=1, - channel_index=2, - fill_mode='nearest', - cval=0., - order=1, - is_rescale=False, - is_center=False, - is_random=False, - thresh_wh=0.02, - thresh_wh2=12. + im, + classes=None, + coords=None, + zoom_range=(0.9, 1.1), + row_index=0, + col_index=1, + channel_index=2, + fill_mode='nearest', + cval=0., + order=1, + is_rescale=False, + is_center=False, + is_random=False, + thresh_wh=0.02, + thresh_wh2=12. ): """Zoom in and out of a single image, randomly or non-randomly, and compute the new bounding box coordinates. Objects outside the cropped image will be removed. diff --git a/tensorlayer/utils.py b/tensorlayer/utils.py index 1d8362144..148c71fec 100644 --- a/tensorlayer/utils.py +++ b/tensorlayer/utils.py @@ -33,25 +33,25 @@ def fit( - sess, - network, - train_op, - cost, - X_train, - y_train, - x, - y_, - acc=None, - batch_size=100, - n_epoch=100, - print_freq=5, - X_val=None, - y_val=None, - eval_train=True, - tensorboard=False, - tensorboard_epoch_freq=5, - tensorboard_weight_histograms=True, - tensorboard_graph_vis=True + sess, + network, + train_op, + cost, + X_train, + y_train, + x, + y_, + acc=None, + batch_size=100, + n_epoch=100, + print_freq=5, + X_val=None, + y_val=None, + eval_train=True, + tensorboard=False, + tensorboard_epoch_freq=5, + tensorboard_weight_histograms=True, + tensorboard_graph_vis=True ): """Training a given non time-series network by the given cost function, training data, batch_size, n_epoch etc. diff --git a/tensorlayer/visualize.py b/tensorlayer/visualize.py index 61fc821ae..98c611bc7 100644 --- a/tensorlayer/visualize.py +++ b/tensorlayer/visualize.py @@ -142,14 +142,14 @@ def imsave(images, size, path): def draw_boxes_and_labels_to_image( - image, - classes, - coords, - scores, - classes_list, - is_center=True, - is_rescale=True, - save_name=None + image, + classes, + coords, + scores, + classes_list, + is_center=True, + is_rescale=True, + save_name=None ): """Draw bboxes and class labels on image. Return or save the image with bboxes, example in the docs of ``tl.prepro``. From 078d804037f473de29da46536c813b850600fa95 Mon Sep 17 00:00:00 2001 From: DEKHTIARJonathan Date: Fri, 20 Apr 2018 12:55:31 +0200 Subject: [PATCH 13/22] Code Refactored with new YAPF formating style --- example/tutorial_generate_text.py | 16 +++++++--------- .../tutorial_imagenet_inceptionV3_distributed.py | 16 +++++++--------- setup.cfg | 10 +++++----- tensorlayer/layers/convolution.py | 16 ++++++++++++---- 4 files changed, 31 insertions(+), 27 deletions(-) diff --git a/example/tutorial_generate_text.py b/example/tutorial_generate_text.py index b53b13dda..e61baf8b0 100644 --- a/example/tutorial_generate_text.py +++ b/example/tutorial_generate_text.py @@ -356,15 +356,13 @@ def loss_fn(outputs, targets, batch_size, sequence_length): # feed the seed to initialize the state for generation. for ids in outs_id[:-1]: a_id = np.asarray(ids).reshape(1, 1) - state1 = sess.run( - [ - lstm1_test.final_state, - ], - feed_dict={ - input_data_test: a_id, - lstm1_test.initial_state: state1, - } - ) + state1 = sess.run([ + lstm1_test.final_state, + ], + feed_dict={ + input_data_test: a_id, + lstm1_test.initial_state: state1, + }) # feed the last word in seed, and start to generate sentence. a_id = outs_id[-1] for _ in range(print_length): diff --git a/example/tutorial_imagenet_inceptionV3_distributed.py b/example/tutorial_imagenet_inceptionV3_distributed.py index f7ea0c88f..cbdb41e28 100644 --- a/example/tutorial_imagenet_inceptionV3_distributed.py +++ b/example/tutorial_imagenet_inceptionV3_distributed.py @@ -412,15 +412,13 @@ def run_worker(task_spec, checkpoints_path, batch_size=32, epochs=10): max_steps = epochs * steps_per_epoch m = 'Epoch: {}/{} Steps: {}/{} Loss: {} Learning rate: {} Metrics: {}' logging.info( - m.format( - current_epoch, - epochs, - step, - max_steps, - loss_val, - learning_rate_val, - metrics - ) + m.format(current_epoch, + epochs, + step, + max_steps, + loss_val, + learning_rate_val, + metrics) ) except OutOfRangeError: pass diff --git a/setup.cfg b/setup.cfg index 65b8faa24..f1f4b2acc 100644 --- a/setup.cfg +++ b/setup.cfg @@ -67,11 +67,11 @@ join_multiple_lines = True no_spaces_around_selected_binary_operators = True -SPLIT_PENALTY_AFTER_OPENING_BRACKET = -50 -SPLIT_PENALTY_AFTER_UNARY_OPERATOR = -5000 -SPLIT_PENALTY_BEFORE_IF_EXPR = -5000 +SPLIT_PENALTY_AFTER_OPENING_BRACKET = -30 +#SPLIT_PENALTY_AFTER_UNARY_OPERATOR = -5000 +#SPLIT_PENALTY_BEFORE_IF_EXPR = -5000 SPLIT_PENALTY_FOR_ADDED_LINE_SPLIT = -8 -SPLIT_PENALTY_IMPORT_NAMES = -5000 -SPLIT_PENALTY_LOGICAL_OPERATOR = -5000 +#SPLIT_PENALTY_IMPORT_NAMES = -5000 +#SPLIT_PENALTY_LOGICAL_OPERATOR = -5000 diff --git a/tensorlayer/layers/convolution.py b/tensorlayer/layers/convolution.py index 95b69c9ef..bf8b90703 100644 --- a/tensorlayer/layers/convolution.py +++ b/tensorlayer/layers/convolution.py @@ -1081,13 +1081,17 @@ def _tf_batch_map_offsets(inputs, offsets, grid_offset): dtype=LayersConfig.tf_dtype, **b_init_args ) + tf.reshape() self.outputs = tf.reshape( - act(tf.nn.conv3d(input_deform, W, strides=[1, 1, 1, 1, 1], padding='VALID', name=None) + b), - (tf.shape(self.inputs)[0], input_h, input_w, shape[-1]) + tensor=act(tf.nn.conv3d(input_deform, W, strides=[1, 1, 1, 1, 1], padding='VALID', name=None) + b), + shape=(tf.shape(self.inputs)[0], + input_h, + input_w, + shape[-1]) ) else: self.outputs = tf.reshape( - act(tf.nn.conv3d( + tensor=act(tf.nn.conv3d( input_deform, W, strides=[ @@ -1099,7 +1103,11 @@ def _tf_batch_map_offsets(inputs, offsets, grid_offset): ], padding='VALID', name=None - )), (tf.shape(self.inputs)[0], input_h, input_w, shape[-1]) + )), + shape=[tf.shape(self.inputs)[0], + input_h, + input_w, + shape[-1]] ) # fixed From f726da2eef25eb2377a82ec46bd8bac24c2cff4a Mon Sep 17 00:00:00 2001 From: DEKHTIARJonathan Date: Fri, 20 Apr 2018 14:05:18 +0200 Subject: [PATCH 14/22] tl.layers.pooling YAPF reformat --- tensorlayer/layers/pooling.py | 120 +++++++++++++++++++++++++++++----- 1 file changed, 105 insertions(+), 15 deletions(-) diff --git a/tensorlayer/layers/pooling.py b/tensorlayer/layers/pooling.py index d6f55bd79..5b13f3f59 100644 --- a/tensorlayer/layers/pooling.py +++ b/tensorlayer/layers/pooling.py @@ -104,13 +104,34 @@ class MaxPool1d(Layer): A unique layer name. """ + @deprecated_alias(net='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release - def __init__(self, prev_layer, filter_size=3, strides=2, padding='valid', data_format='channels_last', name='maxpool1d'): + def __init__( + self, + prev_layer, + filter_size=3, + strides=2, + padding='valid', + data_format='channels_last', + name='maxpool1d' + ): super(MaxPool1d, self).__init__(prev_layer=prev_layer, name=name) - logging.info("MaxPool1d %s: filter_size:%s strides:%s padding:%s" % (name, str(filter_size), str(strides), str(padding))) + logging.info( + "MaxPool1d %s: filter_size:%s strides:%s padding:%s" % (name, + str(filter_size), + str(strides), + str(padding)) + ) self.inputs = prev_layer.outputs # operation (customized) - self.outputs = tf.layers.max_pooling1d(self.inputs, filter_size, strides, padding=padding, data_format=data_format, name=name) + self.outputs = tf.layers.max_pooling1d( + self.inputs, + filter_size, + strides, + padding=padding, + data_format=data_format, + name=name + ) # update layer (customized) self.all_layers.append(self.outputs) @@ -145,12 +166,33 @@ class MeanPool1d(Layer): # net_new.all_layers.extend([outputs]) # return net_new @deprecated_alias(net='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release - def __init__(self, prev_layer, filter_size=3, strides=2, padding='valid', data_format='channels_last', name='meanpool1d'): + def __init__( + self, + prev_layer, + filter_size=3, + strides=2, + padding='valid', + data_format='channels_last', + name='meanpool1d' + ): super(MeanPool1d, self).__init__(prev_layer=prev_layer, name=name) - logging.info("MeanPool1d %s: filter_size:%s strides:%s padding:%s" % (name, str(filter_size), str(strides), str(padding))) + logging.info( + "MeanPool1d %s: filter_size:%s strides:%s padding:%s" % + (name, + str(filter_size), + str(strides), + str(padding)) + ) # operation (customized) - self.outputs = tf.layers.average_pooling1d(prev_layer.outputs, filter_size, strides, padding=padding, data_format=data_format, name=name) + self.outputs = tf.layers.average_pooling1d( + prev_layer.outputs, + filter_size, + strides, + padding=padding, + data_format=data_format, + name=name + ) # update layer (customized) self.all_layers.append(self.outputs) @@ -173,17 +215,30 @@ class MaxPool2d(Layer): A unique layer name. """ + @deprecated_alias(net='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__(self, prev_layer, filter_size=(3, 3), strides=(2, 2), padding='SAME', name='maxpool2d'): if strides is None: strides = filter_size - + super(MaxPool2d, self).__init__(prev_layer=prev_layer, name=name) - logging.info("MaxPool2d %s: filter_size:%s strides:%s padding:%s" % (name, str(filter_size), str(strides), str(padding))) + logging.info( + "MaxPool2d %s: filter_size:%s strides:%s padding:%s" % (name, + str(filter_size), + str(strides), + str(padding)) + ) self.inputs = prev_layer.outputs # operation (customized) if tf.__version__ > '1.5': - self.outputs = tf.layers.max_pooling2d(self.inputs, filter_size, strides, padding=padding, data_format='channels_last', name=name) + self.outputs = tf.layers.max_pooling2d( + self.inputs, + filter_size, + strides, + padding=padding, + data_format='channels_last', + name=name + ) else: if len(strides) == 2: raise Exception("len(strides) should be 2.") @@ -193,6 +248,7 @@ def __init__(self, prev_layer, filter_size=(3, 3), strides=(2, 2), padding='SAME # update layer (customized) self.all_layers.append(self.outputs) + class MeanPool2d(Layer): """Mean pooling for 2D image [batch, height, width, channel]. @@ -210,17 +266,31 @@ class MeanPool2d(Layer): A unique layer name. """ + @deprecated_alias(net='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__(self, prev_layer, filter_size=(3, 3), strides=(2, 2), padding='SAME', name='meanpool2d'): if strides is None: strides = filter_size super(MeanPool2d, self).__init__(prev_layer=prev_layer, name=name) - logging.info("MeanPool2d %s: filter_size:%s strides:%s padding:%s" % (name, str(filter_size), str(strides), str(padding))) + logging.info( + "MeanPool2d %s: filter_size:%s strides:%s padding:%s" % + (name, + str(filter_size), + str(strides), + str(padding)) + ) self.inputs = prev_layer.outputs # operation (customized) if tf.__version__ > '1.5': - self.outputs = tf.layers.average_pooling2d(self.inputs, filter_size, strides, padding=padding, data_format='channels_last', name=name) + self.outputs = tf.layers.average_pooling2d( + self.inputs, + filter_size, + strides, + padding=padding, + data_format='channels_last', + name=name + ) else: if len(strides) == 2: raise Exception("len(strides) should be 2.") @@ -261,13 +331,33 @@ class MaxPool3d(Layer): """ @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release - def __init__(self, prev_layer, filter_size=(3, 3, 3), strides=(2, 2, 2), padding='valid', data_format='channels_last', name='maxpool3d'): + def __init__( + self, + prev_layer, + filter_size=(3, 3, 3), + strides=(2, 2, 2), + padding='valid', + data_format='channels_last', + name='maxpool3d' + ): super(MaxPool3d, self).__init__(prev_layer=prev_layer, name=name) - logging.info("MaxPool3d %s: filter_size:%s strides:%s padding:%s" % (name, str(filter_size), str(strides), str(padding))) + logging.info( + "MaxPool3d %s: filter_size:%s strides:%s padding:%s" % (name, + str(filter_size), + str(strides), + str(padding)) + ) # operation (customized) self.inputs = prev_layer.outputs - self.outputs = tf.layers.max_pooling3d(self.inputs, filter_size, strides, padding=padding, data_format=data_format, name=name) - + self.outputs = tf.layers.max_pooling3d( + self.inputs, + filter_size, + strides, + padding=padding, + data_format=data_format, + name=name + ) + # update layer (customized) self.all_layers.append(self.outputs) From 034b3dfd74d7c3189e147519d23b320bef0dd134 Mon Sep 17 00:00:00 2001 From: DEKHTIARJonathan Date: Fri, 20 Apr 2018 19:13:47 +0200 Subject: [PATCH 15/22] yapf updated --- .style.yapf | 64 +++++++++++++++++++++++++++++++++++++++ tests/test_yapf_format.py | 6 ++-- 2 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 .style.yapf diff --git a/.style.yapf b/.style.yapf new file mode 100644 index 000000000..ac0f8d513 --- /dev/null +++ b/.style.yapf @@ -0,0 +1,64 @@ +[style] +based_on_style=pep8 + +# The column limit. +column_limit=119 + +# Align closing bracket with visual indentation. +align_closing_bracket_with_visual_indent=False + +# Put closing brackets on a separate line, dedented, if the bracketed +# expression can't fit in a single line. Applies to all kinds of brackets, +# including function definitions and calls. For example: +# +# config = { +# 'key1': 'value1', +# 'key2': 'value2', +# } # <--- this bracket is dedented and on a separate line +# +# time_series = self.remote_client.query_entity_counters( +# entity='dev3246.region1', +# key='dns.query_latency_tcp', +# transform=Transformation.AVERAGE(window=timedelta(seconds=60)), +# start_ts=now()-timedelta(days=3), +# end_ts=now(), +# ) # <--- this bracket is dedented and on a separate line +dedent_closing_brackets=True + +# Insert a space between the ending comma and closing bracket of a list, +# etc. +space_between_ending_comma_and_closing_bracket=False + +# Split after the opening paren which surrounds an expression if it doesn't +# fit on a single line. +split_before_expression_after_opening_paren=True + +# Set to True to split list comprehensions and generators that have +# non-trivial expressions and multiple clauses before each of these +# clauses. For example: +# +# result = [ +# a_long_var + 100 for a_long_var in xrange(1000) +# if a_long_var % 10] +# +# would reformat to something like: +# +# result = [ +# a_long_var + 100 +# for a_long_var in xrange(1000) +# if a_long_var % 10] +split_complex_comprehension=True + +# Insert a blank line before a 'def' or 'class' immediately nested +# within another 'def' or 'class'. For example: +# +# class Foo: +# # <------ this blank line +# def method(): +# ... +blank_line_before_nested_class_or_def=True + +# The i18n function call names. The presence of this function stops +# reformattting on that line, because the string it has cannot be moved +# away from the i18n comment. +i18n_function_call=['_'] \ No newline at end of file diff --git a/tests/test_yapf_format.py b/tests/test_yapf_format.py index 5844d9f94..2e4a47c3a 100644 --- a/tests/test_yapf_format.py +++ b/tests/test_yapf_format.py @@ -38,7 +38,8 @@ def test_files_format(self): code = _read_utf_8_file(file) # https://pypi.python.org/pypi/yapf/0.20.2#example-as-a-module - diff, changed = FormatCode(code, filename=file, style_config='setup.cfg', print_diff=True) + #diff, changed = FormatCode(code, filename=file, style_config='setup.cfg', print_diff=True) + diff, changed = FormatCode(code, filename=file, style_config='.style.yapf', print_diff=True) if changed: print(diff) @@ -50,7 +51,8 @@ def test_files_format(self): if self.badly_formatted_files: for filename in self.badly_formatted_files: - str_err += 'yapf -i --style=setup.cfg %s\n' % filename + #str_err += 'yapf -i --style=setup.cfg %s\n' % filename + str_err += 'yapf -i --style=.style.yapf %s\n' % filename str_err = "\n======================================================================================\n" \ "Bad Coding Style: %d file(s) need to be formatted, run the following commands to fix: \n%s" \ From 45e4772a9513310c7348aa65c7f6e4f8d1cf1a49 Mon Sep 17 00:00:00 2001 From: DEKHTIARJonathan Date: Fri, 20 Apr 2018 19:14:03 +0200 Subject: [PATCH 16/22] gitignore updated --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index 2ec50d5e9..c468f8a4f 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,5 @@ tensorlayer.egg-info tensorlayer/__pacache__ venv/ .pytest_cache/ +update_tl.bat +update_tl.py From e2021f5530f4488d54ca6cdada38ba0b6dc9cb93 Mon Sep 17 00:00:00 2001 From: DEKHTIARJonathan Date: Fri, 20 Apr 2018 20:26:35 +0200 Subject: [PATCH 17/22] YAPF Style Fixing Attempt --- .style.yapf | 80 ++--- example/tutorial_atari_pong.py | 10 +- .../tutorial_binarynet_cifar10_tfrecord.py | 82 +---- ...ial_bipedalwalker_a3c_continuous_action.py | 8 +- example/tutorial_cartpole_ac.py | 16 +- example/tutorial_cifar10.py | 56 +--- example/tutorial_cifar10_tfrecord.py | 59 +--- .../tutorial_dorefanet_cifar10_tfrecord.py | 88 +----- example/tutorial_frozenlake_dqn.py | 14 +- example/tutorial_frozenlake_q_table.py | 7 +- example/tutorial_generate_text.py | 68 ++-- ...torial_imagenet_inceptionV3_distributed.py | 19 +- example/tutorial_keras.py | 6 +- example/tutorial_mlp_dropout2.py | 6 +- example/tutorial_mnist.py | 12 +- example/tutorial_mobilenet.py | 10 +- example/tutorial_ptb_lstm.py | 34 +- example/tutorial_ptb_lstm_state_is_tuple.py | 17 +- example/tutorial_squeezenet.py | 2 +- ...tutorial_ternaryweight_cifar10_tfrecord.py | 82 +---- example/tutorial_tf_dataset_voc.py | 23 +- example/tutorial_tfrecord.py | 6 +- example/tutorial_tfrecord2.py | 6 +- example/tutorial_tfrecord3.py | 15 +- example/tutorial_vgg16.py | 139 ++------ example/tutorial_vgg19.py | 299 +++--------------- example/tutorial_word2vec_basic.py | 38 +-- setup.cfg | 2 +- tensorlayer/cli/train.py | 10 +- tensorlayer/cost.py | 14 +- tensorlayer/db.py | 9 +- tensorlayer/deprecation.py | 7 +- tensorlayer/distributed.py | 26 +- tensorlayer/files.py | 107 ++----- tensorlayer/iterate.py | 8 +- tensorlayer/layers/binary.py | 96 +----- tensorlayer/layers/convolution.py | 275 +++------------- tensorlayer/layers/core.py | 43 +-- tensorlayer/layers/normalization.py | 41 +-- tensorlayer/layers/pooling.py | 93 +----- tensorlayer/layers/recurrent.py | 171 ++++------ tensorlayer/layers/spatial_transformer.py | 4 +- tensorlayer/layers/special_activation.py | 8 +- tensorlayer/models/mobilenetv1.py | 9 +- tensorlayer/models/vgg16.py | 40 +-- tensorlayer/nlp.py | 14 +- tensorlayer/prepro.py | 90 +----- tensorlayer/utils.py | 5 +- tensorlayer/visualize.py | 28 +- tests/test_layers_basic.py | 1 + tests/test_layers_convolution.py | 34 +- tests/test_layers_core.py | 3 +- tests/test_layers_extend.py | 1 + tests/test_layers_flow_control.py | 1 + tests/test_layers_importer.py | 2 + tests/test_layers_merge.py | 1 + tests/test_layers_normalization.py | 1 + tests/test_layers_padding.py | 1 + tests/test_layers_pooling.py | 1 + tests/test_layers_recurrent.py | 36 +-- tests/test_layers_shape.py | 1 + tests/test_layers_spatial_transformer.py | 17 +- tests/test_layers_special_activation.py | 1 + tests/test_layers_stack.py | 1 + tests/test_layers_super_resolution.py | 1 + tests/test_layers_time_distributed.py | 8 +- tests/test_mnist_simple.py | 9 +- tests/test_models.py | 1 + tests/test_pydocstyle.py | 1 + tests/test_reuse_mlp.py | 1 + tests/test_yapf_format.py | 3 +- tests/unittests_helper.py | 1 + 72 files changed, 562 insertions(+), 1867 deletions(-) diff --git a/.style.yapf b/.style.yapf index ac0f8d513..672a9d6e7 100644 --- a/.style.yapf +++ b/.style.yapf @@ -1,11 +1,14 @@ [style] -based_on_style=pep8 +based_on_style=google + +# The number of columns to use for indentation. +indent_width = 4 # The column limit. -column_limit=119 +column_limit=120 -# Align closing bracket with visual indentation. -align_closing_bracket_with_visual_indent=False +# Place each dictionary entry onto its own line. +each_dict_entry_on_separate_line = True # Put closing brackets on a separate line, dedented, if the bracketed # expression can't fit in a single line. Applies to all kinds of brackets, @@ -25,40 +28,39 @@ align_closing_bracket_with_visual_indent=False # ) # <--- this bracket is dedented and on a separate line dedent_closing_brackets=True -# Insert a space between the ending comma and closing bracket of a list, -# etc. -space_between_ending_comma_and_closing_bracket=False +# Do not split consecutive brackets. Only relevant when DEDENT_CLOSING_BRACKETS is set +coalesce_brackets = False -# Split after the opening paren which surrounds an expression if it doesn't -# fit on a single line. -split_before_expression_after_opening_paren=True +# Align closing bracket with visual indentation. +align_closing_bracket_with_visual_indent = False -# Set to True to split list comprehensions and generators that have -# non-trivial expressions and multiple clauses before each of these -# clauses. For example: -# -# result = [ -# a_long_var + 100 for a_long_var in xrange(1000) -# if a_long_var % 10] -# -# would reformat to something like: -# -# result = [ -# a_long_var + 100 -# for a_long_var in xrange(1000) -# if a_long_var % 10] -split_complex_comprehension=True - -# Insert a blank line before a 'def' or 'class' immediately nested -# within another 'def' or 'class'. For example: -# -# class Foo: -# # <------ this blank line -# def method(): -# ... -blank_line_before_nested_class_or_def=True - -# The i18n function call names. The presence of this function stops -# reformattting on that line, because the string it has cannot be moved -# away from the i18n comment. -i18n_function_call=['_'] \ No newline at end of file +# Split named assignments onto individual lines. +split_before_named_assigns = True + +# If an argument / parameter list is going to be split, then split before the first argument. +split_before_first_argument = False + +# Split before arguments if the argument list is terminated by a comma. +split_arguments_when_comma_terminated = False + +# Split after the opening paren which surrounds an expression if it doesn't fit on a single line. +split_before_expression_after_opening_paren = False + +# Insert a space between the ending comma and closing bracket of a list, etc. +space_between_ending_comma_and_closing_bracket = False + +# Allow splits before the dictionary value. +allow_split_before_dict_value = True + +# Join short lines into one line. E.g., single line if statements. +join_multiple_lines = True + +# Do not include spaces around selected binary operators. +# Example: 1 + 2 * 3 - 4 / 5 => 1 + 2*3 - 4/5 +no_spaces_around_selected_binary_operators = True + +# Allow lambdas to be formatted on more than one line. +allow_multiline_lambdas = True + +SPLIT_PENALTY_FOR_ADDED_LINE_SPLIT = 10 +SPLIT_PENALTY_AFTER_OPENING_BRACKET = 500 \ No newline at end of file diff --git a/example/tutorial_atari_pong.py b/example/tutorial_atari_pong.py index 6bddabba8..35246ebde 100644 --- a/example/tutorial_atari_pong.py +++ b/example/tutorial_atari_pong.py @@ -133,9 +133,11 @@ def prepro(I): prev_x = None if reward != 0: - print(( - 'episode %d: game %d took %.5fs, reward: %f' % - (episode_number, game_number, time.time() - start_time, reward) - ), ('' if reward == -1 else ' !!!!!!!!')) + print( + ( + 'episode %d: game %d took %.5fs, reward: %f' % + (episode_number, game_number, time.time() - start_time, reward) + ), ('' if reward == -1 else ' !!!!!!!!') + ) start_time = time.time() game_number += 1 diff --git a/example/tutorial_binarynet_cifar10_tfrecord.py b/example/tutorial_binarynet_cifar10_tfrecord.py index efcc131a1..e23366775 100644 --- a/example/tutorial_binarynet_cifar10_tfrecord.py +++ b/example/tutorial_binarynet_cifar10_tfrecord.py @@ -151,17 +151,12 @@ def read_and_decode(filename, is_train=None): x_test_, y_test_ = read_and_decode("test.cifar10", False) x_train_batch, y_train_batch = tf.train.shuffle_batch( - [x_train_, y_train_], - batch_size=batch_size, - capacity=2000, - min_after_dequeue=1000, - num_threads=32 + [x_train_, y_train_], batch_size=batch_size, capacity=2000, min_after_dequeue=1000, num_threads=32 ) # set the number of threads here # for testing, uses batch instead of shuffle_batch - x_test_batch, y_test_batch = tf.train.batch([x_test_, y_test_], - batch_size=batch_size, - capacity=50000, - num_threads=32) + x_test_batch, y_test_batch = tf.train.batch( + [x_test_, y_test_], batch_size=batch_size, capacity=50000, num_threads=32 + ) def model(x_crop, y_, reuse): """ For more simplified CNN APIs, check tensorlayer.org """ @@ -174,57 +169,26 @@ def model(x_crop, y_, reuse): net = tl.layers.SignLayer(net) net = tl.layers.MaxPool2d(net, (3, 3), (2, 2), padding='SAME', name='pool1') net = tl.layers.LocalResponseNormLayer( - net, - depth_radius=4, - bias=1.0, - alpha=0.001 / 9.0, - beta=0.75, - name='norm1' + net, depth_radius=4, bias=1.0, alpha=0.001 / 9.0, beta=0.75, name='norm1' ) net = tl.layers.BinaryConv2d( - net, - 64, - (5, 5), - (1, 1), - act=tf.nn.relu, - padding='SAME', - W_init=W_init, - name='cnn2' + net, 64, (5, 5), (1, 1), act=tf.nn.relu, padding='SAME', W_init=W_init, name='cnn2' ) net = tl.layers.LocalResponseNormLayer( - net, - depth_radius=4, - bias=1.0, - alpha=0.001 / 9.0, - beta=0.75, - name='norm2' + net, depth_radius=4, bias=1.0, alpha=0.001 / 9.0, beta=0.75, name='norm2' ) net = tl.layers.MaxPool2d(net, (3, 3), (2, 2), padding='SAME', name='pool2') net = tl.layers.FlattenLayer(net, name='flatten') # output: (batch_size, 2304) net = tl.layers.SignLayer(net) net = tl.layers.BinaryDenseLayer( - net, - n_units=384, - act=tf.nn.relu, - W_init=W_init2, - b_init=b_init2, - name='d1relu' + net, n_units=384, act=tf.nn.relu, W_init=W_init2, b_init=b_init2, name='d1relu' ) # output: (batch_size, 384) net = tl.layers.SignLayer(net) net = tl.layers.BinaryDenseLayer( - net, - n_units=192, - act=tf.nn.relu, - W_init=W_init2, - b_init=b_init2, - name='d2relu' + net, n_units=192, act=tf.nn.relu, W_init=W_init2, b_init=b_init2, name='d2relu' ) # output: (batch_size, 192) net = tl.layers.DenseLayer( - net, - n_units=10, - act=tf.identity, - W_init=W_init2, - name='output' + net, n_units=10, act=tf.identity, W_init=W_init2, name='output' ) # output: (batch_size, 10) y = net.outputs @@ -257,27 +221,13 @@ def model_batch_norm(x_crop, y_, reuse, is_train): net = tl.layers.MaxPool2d(net, (3, 3), (2, 2), padding='SAME', name='pool2') net = tl.layers.FlattenLayer(net, name='flatten') # output: (batch_size, 2304) net = tl.layers.DenseLayer( - net, - n_units=384, - act=tf.nn.relu, - W_init=W_init2, - b_init=b_init2, - name='d1relu' + net, n_units=384, act=tf.nn.relu, W_init=W_init2, b_init=b_init2, name='d1relu' ) # output: (batch_size, 384) net = tl.layers.DenseLayer( - net, - n_units=192, - act=tf.nn.relu, - W_init=W_init2, - b_init=b_init2, - name='d2relu' + net, n_units=192, act=tf.nn.relu, W_init=W_init2, b_init=b_init2, name='d2relu' ) # output: (batch_size, 192) net = tl.layers.DenseLayer( - net, - n_units=10, - act=tf.identity, - W_init=W_init2, - name='output' + net, n_units=10, act=tf.identity, W_init=W_init2, name='output' ) # output: (batch_size, 10) y = net.outputs @@ -350,11 +300,7 @@ def model_batch_norm(x_crop, y_, reuse, is_train): if epoch + 1 == 1 or (epoch + 1) % print_freq == 0: print( "Epoch %d : Step %d-%d of %d took %fs" % - (epoch, - step, - step + n_step_epoch, - n_step, - time.time() - start_time) + (epoch, step, step + n_step_epoch, n_step, time.time() - start_time) ) print(" train loss: %f" % (train_loss / n_batch)) print(" train acc: %f" % (train_acc / n_batch)) diff --git a/example/tutorial_bipedalwalker_a3c_continuous_action.py b/example/tutorial_bipedalwalker_a3c_continuous_action.py index 2f251efb0..23f5fa0f3 100644 --- a/example/tutorial_bipedalwalker_a3c_continuous_action.py +++ b/example/tutorial_bipedalwalker_a3c_continuous_action.py @@ -67,6 +67,7 @@ class ACNet(object): + def __init__(self, scope, globalAC=None): self.scope = scope if scope == GLOBAL_NET_SCOPE: @@ -171,6 +172,7 @@ def load_ckpt(self): class Worker(object): + def __init__(self, name, globalAC): self.env = gym.make(GAME) self.name = name @@ -209,8 +211,10 @@ def work(self): buffer_v_target.append(v_s_) buffer_v_target.reverse() - buffer_s, buffer_a, buffer_v_target = np.vstack(buffer_s), np.vstack(buffer_a - ), np.vstack(buffer_v_target) + buffer_s, buffer_a, buffer_v_target = ( + np.vstack(buffer_s), np.vstack(buffer_a), np.vstack(buffer_v_target) + ) + feed_dict = { self.AC.s: buffer_s, self.AC.a_his: buffer_a, diff --git a/example/tutorial_cartpole_ac.py b/example/tutorial_cartpole_ac.py index 7b9d91955..b7fd64ba7 100644 --- a/example/tutorial_cartpole_ac.py +++ b/example/tutorial_cartpole_ac.py @@ -69,6 +69,7 @@ class Actor(object): + def __init__(self, sess, n_features, n_actions, lr=0.001): self.sess = sess self.s = tf.placeholder(tf.float32, [1, n_features], "state") @@ -86,10 +87,7 @@ def __init__(self, sess, n_features, n_actions, lr=0.001): # Hao Dong with tf.variable_scope('loss'): self.exp_v = tl.rein.cross_entropy_reward_loss( - logits=self.acts_logits, - actions=self.a, - rewards=self.td_error, - name='actor_weighted_loss' + logits=self.acts_logits, actions=self.a, rewards=self.td_error, name='actor_weighted_loss' ) with tf.variable_scope('train'): @@ -118,6 +116,7 @@ def choose_action_greedy(self, s): class Critic(object): + def __init__(self, sess, n_features, lr=0.01): self.sess = sess self.s = tf.placeholder(tf.float32, [1, n_features], "state") @@ -149,9 +148,7 @@ def learn(self, s, r, s_): actor = Actor(sess, n_features=N_F, n_actions=N_A, lr=LR_A) critic = Critic( - sess, - n_features=N_F, - lr=LR_C + sess, n_features=N_F, lr=LR_C ) # we need a good teacher, so the teacher should learn faster than the actor tl.layers.initialize_global_variables(sess) @@ -198,10 +195,7 @@ def learn(self, s, r, s_): # if running_reward > DISPLAY_REWARD_THRESHOLD: RENDER = True print( "Episode: %d reward: %f running_reward %f took: %.5f" % - (i_episode, - ep_rs_sum, - running_reward, - time.time() - episode_time) + (i_episode, ep_rs_sum, running_reward, time.time() - episode_time) ) # Early Stopping for quick check diff --git a/example/tutorial_cifar10.py b/example/tutorial_cifar10.py index bc4b3ef4c..bae25a679 100644 --- a/example/tutorial_cifar10.py +++ b/example/tutorial_cifar10.py @@ -45,27 +45,13 @@ def model(x, y_, reuse): # padding='SAME', pool = tf.nn.max_pool, name ='pool2') # output: (batch_size, 6, 6, 64) net = FlattenLayer(net, name='flatten') # output: (batch_size, 2304) net = DenseLayer( - net, - n_units=384, - act=tf.nn.relu, - W_init=W_init2, - b_init=b_init2, - name='d1relu' + net, n_units=384, act=tf.nn.relu, W_init=W_init2, b_init=b_init2, name='d1relu' ) # output: (batch_size, 384) net = DenseLayer( - net, - n_units=192, - act=tf.nn.relu, - W_init=W_init2, - b_init=b_init2, - name='d2relu' + net, n_units=192, act=tf.nn.relu, W_init=W_init2, b_init=b_init2, name='d2relu' ) # output: (batch_size, 192) net = DenseLayer( - net, - n_units=10, - act=tf.identity, - W_init=tf.truncated_normal_initializer(stddev=1 / 192.0), - name='output' + net, n_units=10, act=tf.identity, W_init=tf.truncated_normal_initializer(stddev=1 / 192.0), name='output' ) # output: (batch_size, 10) y = net.outputs @@ -110,27 +96,13 @@ def model_batch_norm(x, y_, reuse, is_train): net = FlattenLayer(net, name='flatten') # output: (batch_size, 2304) net = DenseLayer( - net, - n_units=384, - act=tf.nn.relu, - W_init=W_init2, - b_init=b_init2, - name='d1relu' + net, n_units=384, act=tf.nn.relu, W_init=W_init2, b_init=b_init2, name='d1relu' ) # output: (batch_size, 384) net = DenseLayer( - net, - n_units=192, - act=tf.nn.relu, - W_init=W_init2, - b_init=b_init2, - name='d2relu' + net, n_units=192, act=tf.nn.relu, W_init=W_init2, b_init=b_init2, name='d2relu' ) # output: (batch_size, 192) net = DenseLayer( - net, - n_units=10, - act=tf.identity, - W_init=tf.truncated_normal_initializer(stddev=1 / 192.0), - name='output' + net, n_units=10, act=tf.identity, W_init=tf.truncated_normal_initializer(stddev=1 / 192.0), name='output' ) # output: (batch_size, 10) y = net.outputs @@ -176,14 +148,8 @@ def distort_fn(x, is_train=False): return x -x = tf.placeholder(tf.float32, shape=[None, 24, 24, 3], name='x') -y_ = tf.placeholder( - tf.int64, - shape=[ - None, - ], - name='y_' -) +x = tf.placeholder(dtype=tf.float32, shape=[None, 24, 24, 3], name='x') +y_ = tf.placeholder(dtype=tf.int64, shape=[None], name='y_') ## using local response normalization # network, cost, _ = model(x, y_, False) @@ -200,11 +166,7 @@ def distort_fn(x, is_train=False): train_params = network.all_params train_op = tf.train.AdamOptimizer( - learning_rate, - beta1=0.9, - beta2=0.999, - epsilon=1e-08, - use_locking=False + learning_rate, beta1=0.9, beta2=0.999, epsilon=1e-08, use_locking=False ).minimize( cost, var_list=train_params ) diff --git a/example/tutorial_cifar10_tfrecord.py b/example/tutorial_cifar10_tfrecord.py index 51902ec6f..ef82ce4a3 100644 --- a/example/tutorial_cifar10_tfrecord.py +++ b/example/tutorial_cifar10_tfrecord.py @@ -179,17 +179,12 @@ def read_and_decode(filename, is_train=None): x_test_, y_test_ = read_and_decode("test.cifar10", False) x_train_batch, y_train_batch = tf.train.shuffle_batch( - [x_train_, y_train_], - batch_size=batch_size, - capacity=2000, - min_after_dequeue=1000, - num_threads=32 + [x_train_, y_train_], batch_size=batch_size, capacity=2000, min_after_dequeue=1000, num_threads=32 ) # set the number of threads here # for testing, uses batch instead of shuffle_batch - x_test_batch, y_test_batch = tf.train.batch([x_test_, y_test_], - batch_size=batch_size, - capacity=50000, - num_threads=32) + x_test_batch, y_test_batch = tf.train.batch( + [x_test_, y_test_], batch_size=batch_size, capacity=50000, num_threads=32 + ) def model(x_crop, y_, reuse): """ For more simplified CNN APIs, check tensorlayer.org """ @@ -221,27 +216,13 @@ def model(x_crop, y_, reuse): # padding='SAME', pool = tf.nn.max_pool, name ='pool2') # output: (batch_size, 6, 6, 64) net = FlattenLayer(net, name='flatten') # output: (batch_size, 2304) net = DenseLayer( - net, - n_units=384, - act=tf.nn.relu, - W_init=W_init2, - b_init=b_init2, - name='d1relu' + net, n_units=384, act=tf.nn.relu, W_init=W_init2, b_init=b_init2, name='d1relu' ) # output: (batch_size, 384) net = DenseLayer( - net, - n_units=192, - act=tf.nn.relu, - W_init=W_init2, - b_init=b_init2, - name='d2relu' + net, n_units=192, act=tf.nn.relu, W_init=W_init2, b_init=b_init2, name='d2relu' ) # output: (batch_size, 192) net = DenseLayer( - net, - n_units=10, - act=tf.identity, - W_init=W_init2, - name='output' + net, n_units=10, act=tf.identity, W_init=W_init2, name='output' ) # output: (batch_size, 10) y = net.outputs @@ -286,27 +267,13 @@ def model_batch_norm(x_crop, y_, reuse, is_train): net = FlattenLayer(net, name='flatten') # output: (batch_size, 2304) net = DenseLayer( - net, - n_units=384, - act=tf.nn.relu, - W_init=W_init2, - b_init=b_init2, - name='d1relu' + net, n_units=384, act=tf.nn.relu, W_init=W_init2, b_init=b_init2, name='d1relu' ) # output: (batch_size, 384) net = DenseLayer( - net, - n_units=192, - act=tf.nn.relu, - W_init=W_init2, - b_init=b_init2, - name='d2relu' + net, n_units=192, act=tf.nn.relu, W_init=W_init2, b_init=b_init2, name='d2relu' ) # output: (batch_size, 192) net = DenseLayer( - net, - n_units=10, - act=tf.identity, - W_init=W_init2, - name='output' + net, n_units=10, act=tf.identity, W_init=W_init2, name='output' ) # output: (batch_size, 10) y = net.outputs @@ -379,11 +346,7 @@ def model_batch_norm(x_crop, y_, reuse, is_train): if epoch + 1 == 1 or (epoch + 1) % print_freq == 0: print( "Epoch %d : Step %d-%d of %d took %fs" % - (epoch, - step, - step + n_step_epoch, - n_step, - time.time() - start_time) + (epoch, step, step + n_step_epoch, n_step, time.time() - start_time) ) print(" train loss: %f" % (train_loss / n_batch)) print(" train acc: %f" % (train_acc / n_batch)) diff --git a/example/tutorial_dorefanet_cifar10_tfrecord.py b/example/tutorial_dorefanet_cifar10_tfrecord.py index c720bc738..113f4d40f 100644 --- a/example/tutorial_dorefanet_cifar10_tfrecord.py +++ b/example/tutorial_dorefanet_cifar10_tfrecord.py @@ -151,17 +151,12 @@ def read_and_decode(filename, is_train=None): x_test_, y_test_ = read_and_decode("test.cifar10", False) x_train_batch, y_train_batch = tf.train.shuffle_batch( - [x_train_, y_train_], - batch_size=batch_size, - capacity=2000, - min_after_dequeue=1000, - num_threads=32 + [x_train_, y_train_], batch_size=batch_size, capacity=2000, min_after_dequeue=1000, num_threads=32 ) # set the number of threads here # for testing, uses batch instead of shuffle_batch - x_test_batch, y_test_batch = tf.train.batch([x_test_, y_test_], - batch_size=batch_size, - capacity=50000, - num_threads=32) + x_test_batch, y_test_batch = tf.train.batch( + [x_test_, y_test_], batch_size=batch_size, capacity=50000, num_threads=32 + ) def model(x_crop, y_, reuse): """ For more simplified CNN APIs, check tensorlayer.org """ @@ -173,61 +168,24 @@ def model(x_crop, y_, reuse): net = tl.layers.Conv2d(net, 64, (5, 5), (1, 1), act=tf.nn.relu, padding='SAME', W_init=W_init, name='cnn1') net = tl.layers.MaxPool2d(net, (3, 3), (2, 2), padding='SAME', name='pool1') net = tl.layers.LocalResponseNormLayer( - net, - depth_radius=4, - bias=1.0, - alpha=0.001 / 9.0, - beta=0.75, - name='norm1' + net, depth_radius=4, bias=1.0, alpha=0.001 / 9.0, beta=0.75, name='norm1' ) net = tl.layers.DorefaConv2d( - net, - 1, - 3, - 64, - (5, 5), - (1, 1), - tf.nn.relu, - padding='SAME', - W_init=W_init, - name='cnn2' + net, 1, 3, 64, (5, 5), (1, 1), tf.nn.relu, padding='SAME', W_init=W_init, name='cnn2' ) net = tl.layers.LocalResponseNormLayer( - net, - depth_radius=4, - bias=1.0, - alpha=0.001 / 9.0, - beta=0.75, - name='norm2' + net, depth_radius=4, bias=1.0, alpha=0.001 / 9.0, beta=0.75, name='norm2' ) net = tl.layers.MaxPool2d(net, (3, 3), (2, 2), padding='SAME', name='pool2') net = tl.layers.FlattenLayer(net, name='flatten') # output: (batch_size, 2304) net = tl.layers.DorefaDenseLayer( - net, - 1, - 3, - 384, - act=tf.nn.relu, - W_init=W_init2, - b_init=b_init2, - name='d1relu' + net, 1, 3, 384, act=tf.nn.relu, W_init=W_init2, b_init=b_init2, name='d1relu' ) # output: (batch_size, 384) net = tl.layers.DorefaDenseLayer( - net, - 1, - 3, - 192, - act=tf.nn.relu, - W_init=W_init2, - b_init=b_init2, - name='d2relu' + net, 1, 3, 192, act=tf.nn.relu, W_init=W_init2, b_init=b_init2, name='d2relu' ) # output: (batch_size, 192) net = tl.layers.DenseLayer( - net, - n_units=10, - act=tf.identity, - W_init=W_init2, - name='output' + net, n_units=10, act=tf.identity, W_init=W_init2, name='output' ) # output: (batch_size, 10) y = net.outputs @@ -259,27 +217,13 @@ def model_batch_norm(x_crop, y_, reuse, is_train): net = tl.layers.MaxPool2d(net, (3, 3), (2, 2), padding='SAME', name='pool2') net = tl.layers.FlattenLayer(net, name='flatten') # output: (batch_size, 2304) net = tl.layers.DenseLayer( - net, - n_units=384, - act=tf.nn.relu, - W_init=W_init2, - b_init=b_init2, - name='d1relu' + net, n_units=384, act=tf.nn.relu, W_init=W_init2, b_init=b_init2, name='d1relu' ) # output: (batch_size, 384) net = tl.layers.DenseLayer( - net, - n_units=192, - act=tf.nn.relu, - W_init=W_init2, - b_init=b_init2, - name='d2relu' + net, n_units=192, act=tf.nn.relu, W_init=W_init2, b_init=b_init2, name='d2relu' ) # output: (batch_size, 192) net = tl.layers.DenseLayer( - net, - n_units=10, - act=tf.identity, - W_init=W_init2, - name='output' + net, n_units=10, act=tf.identity, W_init=W_init2, name='output' ) # output: (batch_size, 10) y = net.outputs ce = tl.cost.cross_entropy(y, y_, name='cost') @@ -351,11 +295,7 @@ def model_batch_norm(x_crop, y_, reuse, is_train): if epoch + 1 == 1 or (epoch + 1) % print_freq == 0: print( "Epoch %d : Step %d-%d of %d took %fs" % - (epoch, - step, - step + n_step_epoch, - n_step, - time.time() - start_time) + (epoch, step, step + n_step_epoch, n_step, time.time() - start_time) ) print(" train loss: %f" % (train_loss / n_batch)) print(" train acc: %f" % (train_acc / n_batch)) diff --git a/example/tutorial_frozenlake_dqn.py b/example/tutorial_frozenlake_dqn.py index d8f81a34d..11ebc4d26 100644 --- a/example/tutorial_frozenlake_dqn.py +++ b/example/tutorial_frozenlake_dqn.py @@ -49,12 +49,7 @@ def to_one_hot(i, n_classes=None): inputs = tf.placeholder(shape=[1, 16], dtype=tf.float32) net = InputLayer(inputs, name='observation') net = DenseLayer( - net, - n_units=4, - act=tf.identity, - W_init=tf.random_uniform_initializer(0, 0.01), - b_init=None, - name='q_a_s' + net, n_units=4, act=tf.identity, W_init=tf.random_uniform_initializer(0, 0.01), b_init=None, name='q_a_s' ) y = net.outputs # action-value / rewards of 4 actions predict = tf.argmax( @@ -110,10 +105,5 @@ def to_one_hot(i, n_classes=None): running_reward = rAll if running_reward is None else running_reward * 0.99 + rAll * 0.01 print( "Episode [%d/%d] sum reward:%f running reward:%f took:%.5fs %s" % - (i, - num_episodes, - rAll, - running_reward, - time.time() - episode_time, - '' if rAll == 0 else ' !!!!!!!!') + (i, num_episodes, rAll, running_reward, time.time() - episode_time, '' if rAll == 0 else ' !!!!!!!!') ) diff --git a/example/tutorial_frozenlake_q_table.py b/example/tutorial_frozenlake_q_table.py index 427ee6329..f63127e76 100644 --- a/example/tutorial_frozenlake_q_table.py +++ b/example/tutorial_frozenlake_q_table.py @@ -54,12 +54,7 @@ running_reward = r if running_reward is None else running_reward * 0.99 + r * 0.01 print( "Episode [%d/%d] sum reward:%f running reward:%f took:%.5fs %s" % - (i, - num_episodes, - rAll, - running_reward, - time.time() - episode_time, - '' if rAll == 0 else ' !!!!!!!!') + (i, num_episodes, rAll, running_reward, time.time() - episode_time, '' if rAll == 0 else ' !!!!!!!!') ) print("Final Q-Table Values:/n %s" % Q) diff --git a/example/tutorial_generate_text.py b/example/tutorial_generate_text.py index e61baf8b0..bfab7d0ee 100644 --- a/example/tutorial_generate_text.py +++ b/example/tutorial_generate_text.py @@ -156,10 +156,7 @@ def main_restore_embedding_layer(): x = tf.placeholder(tf.int32, shape=[batch_size]) emb_net = tl.layers.EmbeddingInputlayer( - inputs=x, - vocabulary_size=vocabulary_size, - embedding_size=embedding_size, - name='embedding_layer' + inputs=x, vocabulary_size=vocabulary_size, embedding_size=embedding_size, name='embedding_layer' ) # sess.run(tf.initialize_all_variables()) @@ -237,11 +234,7 @@ def inference(x, is_train, sequence_length, reuse=None): rnn_init = tf.random_uniform_initializer(-init_scale, init_scale) with tf.variable_scope("model", reuse=reuse): network = EmbeddingInputlayer( - inputs=x, - vocabulary_size=vocab_size, - embedding_size=hidden_size, - E_init=rnn_init, - name='embedding' + inputs=x, vocabulary_size=vocab_size, embedding_size=hidden_size, E_init=rnn_init, name='embedding' ) network = RNNLayer( network, @@ -259,12 +252,7 @@ def inference(x, is_train, sequence_length, reuse=None): ) lstm1 = network network = DenseLayer( - network, - n_units=vocab_size, - W_init=rnn_init, - b_init=rnn_init, - act=tf.identity, - name='output' + network, n_units=vocab_size, W_init=rnn_init, b_init=rnn_init, act=tf.identity, name='output' ) return network, lstm1 @@ -286,8 +274,9 @@ def loss_fn(outputs, targets, batch_size, sequence_length): # n_examples = batch_size * sequence_length # so # cost is the averaged cost of each mini-batch (concurrent process). - loss = tf.contrib.legacy_seq2seq.sequence_loss_by_example([outputs], [tf.reshape(targets, [-1])], - [tf.ones([batch_size * sequence_length])]) + loss = tf.contrib.legacy_seq2seq.sequence_loss_by_example( + [outputs], [tf.reshape(targets, [-1])], [tf.ones([batch_size * sequence_length])] + ) cost = tf.reduce_sum(loss) / batch_size return cost @@ -326,21 +315,21 @@ def loss_fn(outputs, targets, batch_size, sequence_length): ## reset all states at the begining of every epoch state1 = tl.layers.initialize_rnn_state(lstm1.initial_state) for step, (x, y) in enumerate(tl.iterate.ptb_iterator(train_data, batch_size, sequence_length)): - _cost, state1, _ = sess.run([cost, lstm1.final_state, train_op], - feed_dict={ - input_data: x, - targets: y, - lstm1.initial_state: state1, - }) + _cost, state1, _ = sess.run( + [cost, lstm1.final_state, train_op], + feed_dict={ + input_data: x, + targets: y, + lstm1.initial_state: state1, + } + ) costs += _cost iters += sequence_length if step % (epoch_size // 10) == 1: print( "%.3f perplexity: %.3f speed: %.0f wps" % - (step * 1.0 / epoch_size, - np.exp(costs / iters), - iters * batch_size / (time.time() - start_time)) + (step * 1.0 / epoch_size, np.exp(costs / iters), iters * batch_size / (time.time() - start_time)) ) train_perplexity = np.exp(costs / iters) # print("Epoch: %d Train Perplexity: %.3f" % (i + 1, train_perplexity)) @@ -356,22 +345,25 @@ def loss_fn(outputs, targets, batch_size, sequence_length): # feed the seed to initialize the state for generation. for ids in outs_id[:-1]: a_id = np.asarray(ids).reshape(1, 1) - state1 = sess.run([ - lstm1_test.final_state, - ], - feed_dict={ - input_data_test: a_id, - lstm1_test.initial_state: state1, - }) + state1 = sess.run( + [ + lstm1_test.final_state, + ], feed_dict={ + input_data_test: a_id, + lstm1_test.initial_state: state1, + } + ) # feed the last word in seed, and start to generate sentence. a_id = outs_id[-1] for _ in range(print_length): a_id = np.asarray(a_id).reshape(1, 1) - out, state1 = sess.run([y_soft, lstm1_test.final_state], - feed_dict={ - input_data_test: a_id, - lstm1_test.initial_state: state1, - }) + out, state1 = sess.run( + [y_soft, lstm1_test.final_state], + feed_dict={ + input_data_test: a_id, + lstm1_test.initial_state: state1, + } + ) ## Without sampling # a_id = np.argmax(out[0]) ## Sample from all words, if vocab_size is large, diff --git a/example/tutorial_imagenet_inceptionV3_distributed.py b/example/tutorial_imagenet_inceptionV3_distributed.py index cbdb41e28..1009a767d 100644 --- a/example/tutorial_imagenet_inceptionV3_distributed.py +++ b/example/tutorial_imagenet_inceptionV3_distributed.py @@ -132,10 +132,7 @@ def _parse_example_fn(line): def _map_fn(example_serialized): image_bytes, one_hot_labels = tf.py_func( - _parse_example_fn, - [example_serialized], - [tf.string, tf.float32], - stateful=False + _parse_example_fn, [example_serialized], [tf.string, tf.float32], stateful=False ) image = tf.image.decode_jpeg(image_bytes, channels=3) @@ -180,11 +177,13 @@ def build_network(image_input, num_classes=1001, is_training=False): class EvaluatorStops(Exception): + def __init__(self, message): super(EvaluatorStops, self).__init__(message) class EvaluatorHook(session_run_hook.SessionRunHook): + def __init__(self, checkpoints_path, saver): self.checkpoints_path = checkpoints_path self.summary_writer = tf.summary.FileWriter(os.path.join(checkpoints_path, 'validation')) @@ -374,9 +373,7 @@ def run_worker(task_spec, checkpoints_path, batch_size=32, epochs=10): tf.summary.scalar('learning_rate/value', learning_rate) tf.summary.scalar('loss/logits', loss) _, metrics_average_ops, metrics_ops = calculate_metrics( - predicted_batch=predictions, - real_batch=one_hot_classes, - is_training=True + predicted_batch=predictions, real_batch=one_hot_classes, is_training=True ) with tf.control_dependencies([train_op]): train_op = tf.group(metrics_average_ops) @@ -412,13 +409,7 @@ def run_worker(task_spec, checkpoints_path, batch_size=32, epochs=10): max_steps = epochs * steps_per_epoch m = 'Epoch: {}/{} Steps: {}/{} Loss: {} Learning rate: {} Metrics: {}' logging.info( - m.format(current_epoch, - epochs, - step, - max_steps, - loss_val, - learning_rate_val, - metrics) + m.format(current_epoch, epochs, step, max_steps, loss_val, learning_rate_val, metrics) ) except OutOfRangeError: pass diff --git a/example/tutorial_keras.py b/example/tutorial_keras.py index 2d07f8993..e62db3283 100644 --- a/example/tutorial_keras.py +++ b/example/tutorial_keras.py @@ -44,11 +44,7 @@ def keras_block(x): train_params = network.all_params train_op = tf.train.AdamOptimizer( - learning_rate, - beta1=0.9, - beta2=0.999, - epsilon=1e-08, - use_locking=False + learning_rate, beta1=0.9, beta2=0.999, epsilon=1e-08, use_locking=False ).minimize( cost, var_list=train_params ) diff --git a/example/tutorial_mlp_dropout2.py b/example/tutorial_mlp_dropout2.py index ded39ed76..58817d754 100644 --- a/example/tutorial_mlp_dropout2.py +++ b/example/tutorial_mlp_dropout2.py @@ -42,11 +42,7 @@ def mlp(x, is_train=True, reuse=False): # define the optimizer train_params = tl.layers.get_variables_with_name('MLP', train_only=True, printable=False) train_op = tf.train.AdamOptimizer( - learning_rate=0.0001, - beta1=0.9, - beta2=0.999, - epsilon=1e-08, - use_locking=False + learning_rate=0.0001, beta1=0.9, beta2=0.999, epsilon=1e-08, use_locking=False ).minimize( cost, var_list=train_params ) diff --git a/example/tutorial_mnist.py b/example/tutorial_mnist.py index 96e46a84d..43ad1fe75 100644 --- a/example/tutorial_mnist.py +++ b/example/tutorial_mnist.py @@ -50,18 +50,10 @@ def main_test_layers(model='relu'): elif model == 'dropconnect': network = tl.layers.InputLayer(x, name='input') network = tl.layers.DropconnectDenseLayer( - network, - keep=0.8, - n_units=800, - act=tf.nn.relu, - name='dropconnect_relu1' + network, keep=0.8, n_units=800, act=tf.nn.relu, name='dropconnect_relu1' ) network = tl.layers.DropconnectDenseLayer( - network, - keep=0.5, - n_units=800, - act=tf.nn.relu, - name='dropconnect_relu2' + network, keep=0.5, n_units=800, act=tf.nn.relu, name='dropconnect_relu2' ) network = tl.layers.DropconnectDenseLayer(network, keep=0.5, n_units=10, act=tf.identity, name='output') diff --git a/example/tutorial_mobilenet.py b/example/tutorial_mobilenet.py index 0e6582bb7..4b32dbaf4 100644 --- a/example/tutorial_mobilenet.py +++ b/example/tutorial_mobilenet.py @@ -14,13 +14,7 @@ import tensorlayer as tl from tensorlayer.layers import ( - BatchNormLayer, - Conv2d, - DepthwiseConv2d, - FlattenLayer, - GlobalMeanPool2d, - InputLayer, - ReshapeLayer + BatchNormLayer, Conv2d, DepthwiseConv2d, FlattenLayer, GlobalMeanPool2d, InputLayer, ReshapeLayer ) @@ -61,7 +55,7 @@ def decode_predictions(preds, top=5): # keras.applications.resnet50 results = [] for pred in preds: top_indices = pred.argsort()[-top:][::-1] - result = [tuple(CLASS_INDEX[str(i)]) + (pred[i], ) for i in top_indices] + result = [tuple(CLASS_INDEX[str(i)]) + (pred[i],) for i in top_indices] result.sort(key=lambda x: x[2], reverse=True) results.append(result) return results diff --git a/example/tutorial_ptb_lstm.py b/example/tutorial_ptb_lstm.py index c0ac93bc0..418bd587a 100644 --- a/example/tutorial_ptb_lstm.py +++ b/example/tutorial_ptb_lstm.py @@ -195,11 +195,7 @@ def inference(x, is_training, num_steps, reuse=None): initializer = tf.random_uniform_initializer(-init_scale, init_scale) with tf.variable_scope("model", reuse=reuse): network = tl.layers.EmbeddingInputlayer( - inputs=x, - vocabulary_size=vocab_size, - embedding_size=hidden_size, - E_init=initializer, - name='embedding' + inputs=x, vocabulary_size=vocab_size, embedding_size=hidden_size, E_init=initializer, name='embedding' ) network = tl.layers.DropoutLayer(network, keep=keep_prob, is_fix=True, is_train=is_training, name='drop1') network = tl.layers.RNNLayer( @@ -232,12 +228,7 @@ def inference(x, is_training, num_steps, reuse=None): # shape=[-1, int(network.outputs._shape[-1])], name='reshape') network = tl.layers.DropoutLayer(network, keep=keep_prob, is_fix=True, is_train=is_training, name='drop3') network = tl.layers.DenseLayer( - network, - n_units=vocab_size, - W_init=initializer, - b_init=initializer, - act=tf.identity, - name='output' + network, n_units=vocab_size, W_init=initializer, b_init=initializer, act=tf.identity, name='output' ) return network, lstm1, lstm2 @@ -314,17 +305,16 @@ def loss_fn(outputs, targets): #, batch_size, num_steps): } # For training, enable dropout feed_dict.update(network.all_drop) - _cost, state1, state2, _ = sess.run([cost, lstm1.final_state, lstm2.final_state, train_op], - feed_dict=feed_dict) + _cost, state1, state2, _ = sess.run( + [cost, lstm1.final_state, lstm2.final_state, train_op], feed_dict=feed_dict + ) costs += _cost iters += num_steps if step % (epoch_size // 10) == 10: print( "%.3f perplexity: %.3f speed: %.0f wps" % - (step * 1.0 / epoch_size, - np.exp(costs / iters), - iters * batch_size / (time.time() - start_time)) + (step * 1.0 / epoch_size, np.exp(costs / iters), iters * batch_size / (time.time() - start_time)) ) train_perplexity = np.exp(costs / iters) print("Epoch: %d/%d Train Perplexity: %.3f" % (i + 1, max_max_epoch, train_perplexity)) @@ -343,9 +333,10 @@ def loss_fn(outputs, targets): #, batch_size, num_steps): lstm1_val.initial_state: state1, lstm2_val.initial_state: state2, } - _cost, state1, state2, _ = sess.run([cost_val, lstm1_val.final_state, lstm2_val.final_state, - tf.no_op()], - feed_dict=feed_dict) + _cost, state1, state2, _ = sess.run( + [cost_val, lstm1_val.final_state, lstm2_val.final_state, + tf.no_op()], feed_dict=feed_dict + ) costs += _cost iters += num_steps valid_perplexity = np.exp(costs / iters) @@ -367,8 +358,9 @@ def loss_fn(outputs, targets): #, batch_size, num_steps): lstm1_test.initial_state: state1, lstm2_test.initial_state: state2, } - _cost, state1, state2 = sess.run([cost_test, lstm1_test.final_state, lstm2_test.final_state], - feed_dict=feed_dict) + _cost, state1, state2 = sess.run( + [cost_test, lstm1_test.final_state, lstm2_test.final_state], feed_dict=feed_dict + ) costs += _cost iters += 1 test_perplexity = np.exp(costs / iters) diff --git a/example/tutorial_ptb_lstm_state_is_tuple.py b/example/tutorial_ptb_lstm_state_is_tuple.py index e0146b421..1202a4524 100644 --- a/example/tutorial_ptb_lstm_state_is_tuple.py +++ b/example/tutorial_ptb_lstm_state_is_tuple.py @@ -195,11 +195,7 @@ def inference(x, is_training, num_steps, reuse=None): initializer = tf.random_uniform_initializer(-init_scale, init_scale) with tf.variable_scope("model", reuse=reuse): network = tl.layers.EmbeddingInputlayer( - inputs=x, - vocabulary_size=vocab_size, - embedding_size=hidden_size, - E_init=initializer, - name='embedding' + inputs=x, vocabulary_size=vocab_size, embedding_size=hidden_size, E_init=initializer, name='embedding' ) network = tl.layers.DropoutLayer(network, keep=keep_prob, is_fix=True, is_train=is_training, name='drop1') network = tl.layers.RNNLayer( @@ -238,12 +234,7 @@ def inference(x, is_training, num_steps, reuse=None): # shape=[-1, int(network.outputs._shape[-1])], name='reshape') network = tl.layers.DropoutLayer(network, keep=keep_prob, is_fix=True, is_train=is_training, name='drop3') network = tl.layers.DenseLayer( - network, - n_units=vocab_size, - W_init=initializer, - b_init=initializer, - act=tf.identity, - name='output' + network, n_units=vocab_size, W_init=initializer, b_init=initializer, act=tf.identity, name='output' ) return network, lstm1, lstm2 @@ -340,9 +331,7 @@ def loss_fn(outputs, targets, batch_size): if step % (epoch_size // 10) == 10: print( "%.3f perplexity: %.3f speed: %.0f wps" % - (step * 1.0 / epoch_size, - np.exp(costs / iters), - iters * batch_size / (time.time() - start_time)) + (step * 1.0 / epoch_size, np.exp(costs / iters), iters * batch_size / (time.time() - start_time)) ) train_perplexity = np.exp(costs / iters) print("Epoch: %d/%d Train Perplexity: %.3f" % (i + 1, max_max_epoch, train_perplexity)) diff --git a/example/tutorial_squeezenet.py b/example/tutorial_squeezenet.py index 238174633..d60d8ae9b 100644 --- a/example/tutorial_squeezenet.py +++ b/example/tutorial_squeezenet.py @@ -32,7 +32,7 @@ def decode_predictions(preds, top=5): # keras.applications.resnet50 results = [] for pred in preds: top_indices = pred.argsort()[-top:][::-1] - result = [tuple(CLASS_INDEX[str(i)]) + (pred[i], ) for i in top_indices] + result = [tuple(CLASS_INDEX[str(i)]) + (pred[i],) for i in top_indices] result.sort(key=lambda x: x[2], reverse=True) results.append(result) return results diff --git a/example/tutorial_ternaryweight_cifar10_tfrecord.py b/example/tutorial_ternaryweight_cifar10_tfrecord.py index 1a3cc2228..4db31c723 100644 --- a/example/tutorial_ternaryweight_cifar10_tfrecord.py +++ b/example/tutorial_ternaryweight_cifar10_tfrecord.py @@ -150,17 +150,12 @@ def read_and_decode(filename, is_train=None): x_test_, y_test_ = read_and_decode("test.cifar10", False) x_train_batch, y_train_batch = tf.train.shuffle_batch( - [x_train_, y_train_], - batch_size=batch_size, - capacity=2000, - min_after_dequeue=1000, - num_threads=32 + [x_train_, y_train_], batch_size=batch_size, capacity=2000, min_after_dequeue=1000, num_threads=32 ) # set the number of threads here # for testing, uses batch instead of shuffle_batch - x_test_batch, y_test_batch = tf.train.batch([x_test_, y_test_], - batch_size=batch_size, - capacity=50000, - num_threads=32) + x_test_batch, y_test_batch = tf.train.batch( + [x_test_, y_test_], batch_size=batch_size, capacity=50000, num_threads=32 + ) def model(x_crop, y_, reuse): """ For more simplified CNN APIs, check tensorlayer.org """ @@ -172,55 +167,24 @@ def model(x_crop, y_, reuse): net = tl.layers.Conv2d(net, 64, (5, 5), (1, 1), act=tf.nn.relu, padding='SAME', W_init=W_init, name='cnn1') net = tl.layers.MaxPool2d(net, (3, 3), (2, 2), padding='SAME', name='pool1') net = tl.layers.LocalResponseNormLayer( - net, - depth_radius=4, - bias=1.0, - alpha=0.001 / 9.0, - beta=0.75, - name='norm1' + net, depth_radius=4, bias=1.0, alpha=0.001 / 9.0, beta=0.75, name='norm1' ) net = tl.layers.TernaryConv2d( - net, - 64, - (5, 5), - (1, 1), - act=tf.nn.relu, - padding='SAME', - W_init=W_init, - name='cnn2' + net, 64, (5, 5), (1, 1), act=tf.nn.relu, padding='SAME', W_init=W_init, name='cnn2' ) net = tl.layers.LocalResponseNormLayer( - net, - depth_radius=4, - bias=1.0, - alpha=0.001 / 9.0, - beta=0.75, - name='norm2' + net, depth_radius=4, bias=1.0, alpha=0.001 / 9.0, beta=0.75, name='norm2' ) net = tl.layers.MaxPool2d(net, (3, 3), (2, 2), padding='SAME', name='pool2') net = tl.layers.FlattenLayer(net, name='flatten') # output: (batch_size, 2304) net = tl.layers.TernaryDenseLayer( - net, - n_units=384, - act=tf.nn.relu, - W_init=W_init2, - b_init=b_init2, - name='d1relu' + net, n_units=384, act=tf.nn.relu, W_init=W_init2, b_init=b_init2, name='d1relu' ) # output: (batch_size, 384) net = tl.layers.TernaryDenseLayer( - net, - n_units=192, - act=tf.nn.relu, - W_init=W_init2, - b_init=b_init2, - name='d2relu' + net, n_units=192, act=tf.nn.relu, W_init=W_init2, b_init=b_init2, name='d2relu' ) # output: (batch_size, 192) net = tl.layers.DenseLayer( - net, - n_units=10, - act=tf.identity, - W_init=W_init2, - name='output' + net, n_units=10, act=tf.identity, W_init=W_init2, name='output' ) # output: (batch_size, 10) y = net.outputs @@ -252,27 +216,13 @@ def model_batch_norm(x_crop, y_, reuse, is_train): net = tl.layers.MaxPool2d(net, (3, 3), (2, 2), padding='SAME', name='pool2') net = tl.layers.FlattenLayer(net, name='flatten') # output: (batch_size, 2304) net = tl.layers.DenseLayer( - net, - n_units=384, - act=tf.nn.relu, - W_init=W_init2, - b_init=b_init2, - name='d1relu' + net, n_units=384, act=tf.nn.relu, W_init=W_init2, b_init=b_init2, name='d1relu' ) # output: (batch_size, 384) net = tl.layers.DenseLayer( - net, - n_units=192, - act=tf.nn.relu, - W_init=W_init2, - b_init=b_init2, - name='d2relu' + net, n_units=192, act=tf.nn.relu, W_init=W_init2, b_init=b_init2, name='d2relu' ) # output: (batch_size, 192) net = tl.layers.DenseLayer( - net, - n_units=10, - act=tf.identity, - W_init=W_init2, - name='output' + net, n_units=10, act=tf.identity, W_init=W_init2, name='output' ) # output: (batch_size, 10) y = net.outputs ce = tl.cost.cross_entropy(y, y_, name='cost') @@ -344,11 +294,7 @@ def model_batch_norm(x_crop, y_, reuse, is_train): if epoch + 1 == 1 or (epoch + 1) % print_freq == 0: print( "Epoch %d : Step %d-%d of %d took %fs" % - (epoch, - step, - step + n_step_epoch, - n_step, - time.time() - start_time) + (epoch, step, step + n_step_epoch, n_step, time.time() - start_time) ) print(" train loss: %f" % (train_loss / n_batch)) print(" train acc: %f" % (train_acc / n_batch)) diff --git a/example/tutorial_tf_dataset_voc.py b/example/tutorial_tf_dataset_voc.py index 61e0b6734..43e405547 100644 --- a/example/tutorial_tf_dataset_voc.py +++ b/example/tutorial_tf_dataset_voc.py @@ -51,21 +51,10 @@ def _data_aug_fn(im, ann): tmp0 = random.randint(1, int(im_size[0] * jitter)) tmp1 = random.randint(1, int(im_size[1] * jitter)) im, coords = tl.prepro.obj_box_imresize( - im, - coords, - [im_size[0] + tmp0, im_size[1] + tmp1], - is_rescale=True, - interp='bicubic' + im, coords, [im_size[0] + tmp0, im_size[1] + tmp1], is_rescale=True, interp='bicubic' ) im, clas, coords = tl.prepro.obj_box_crop( - im, - clas, - coords, - wrg=im_size[1], - hrg=im_size[0], - is_rescale=True, - is_center=True, - is_random=True + im, clas, coords, wrg=im_size[1], hrg=im_size[0], is_rescale=True, is_center=True, is_random=True ) ## value [0, 255] to [-1, 1] (optional) # im = im / 127.5 - 1 @@ -113,11 +102,5 @@ def _map_fn(filename, annotation): ## save all images for i in range(len(im)): tl.vis.draw_boxes_and_labels_to_image( - im[i] * 255, - ann[i][0], - ann[i][1], - [], - classes, - True, - save_name='_bbox_vis_%d.png' % i + im[i] * 255, ann[i][0], ann[i][1], [], classes, True, save_name='_bbox_vis_%d.png' % i ) diff --git a/example/tutorial_tfrecord.py b/example/tutorial_tfrecord.py index 5e490a26a..0224e55c2 100644 --- a/example/tutorial_tfrecord.py +++ b/example/tutorial_tfrecord.py @@ -96,11 +96,7 @@ def read_and_decode(filename): ## Use shuffle_batch or batch # see https://www.tensorflow.org/versions/master/api_docs/python/io_ops.html#shuffle_batch img_batch, label_batch = tf.train.shuffle_batch( - [img, label], - batch_size=4, - capacity=2000, - min_after_dequeue=1000, - num_threads=16 + [img, label], batch_size=4, capacity=2000, min_after_dequeue=1000, num_threads=16 ) print("img_batch : %s" % img_batch._shape) print("label_batch : %s" % label_batch._shape) diff --git a/example/tutorial_tfrecord2.py b/example/tutorial_tfrecord2.py index e30864ce8..e7c410695 100755 --- a/example/tutorial_tfrecord2.py +++ b/example/tutorial_tfrecord2.py @@ -84,11 +84,7 @@ def read_and_decode(filename): ## Use shuffle_batch or batch # see https://www.tensorflow.org/versions/master/api_docs/python/io_ops.html#shuffle_batch img_batch, label_batch = tf.train.shuffle_batch( - [img, label], - batch_size=4, - capacity=50000, - min_after_dequeue=10000, - num_threads=1 + [img, label], batch_size=4, capacity=50000, min_after_dequeue=10000, num_threads=1 ) print("img_batch : %s" % img_batch._shape) diff --git a/example/tutorial_tfrecord3.py b/example/tutorial_tfrecord3.py index ef71d4293..2d5f7137e 100644 --- a/example/tutorial_tfrecord3.py +++ b/example/tutorial_tfrecord3.py @@ -341,10 +341,7 @@ def prefetch_input_data( except Exception: # for TensorFlow 0.10 img = tf.image.resize_images( - img, - new_height=resize_height, - new_width=resize_width, - method=tf.image.ResizeMethod.BILINEAR + img, new_height=resize_height, new_width=resize_width, method=tf.image.ResizeMethod.BILINEAR ) # Crop to final dimensions. if is_training: @@ -448,11 +445,7 @@ def batch_with_dynamic_pad(images_and_captions, batch_size, queue_capacity, add_ enqueue_list.append([image, input_seq, target_seq, indicator]) images, input_seqs, target_seqs, mask = tf.train.batch_join( - enqueue_list, - batch_size=batch_size, - capacity=queue_capacity, - dynamic_pad=True, - name="batch_and_pad" + enqueue_list, batch_size=batch_size, capacity=queue_capacity, dynamic_pad=True, name="batch_and_pad" ) if add_summaries: @@ -465,9 +458,7 @@ def batch_with_dynamic_pad(images_and_captions, batch_size, queue_capacity, add_ images, input_seqs, target_seqs, input_mask = ( - batch_with_dynamic_pad(images_and_captions=[[img, img_cap]], - batch_size=4, - queue_capacity=50000) + batch_with_dynamic_pad(images_and_captions=[[img, img_cap]], batch_size=4, queue_capacity=50000) ) sess = tf.Session() sess.run(tf.initialize_all_variables()) diff --git a/example/tutorial_vgg16.py b/example/tutorial_vgg16.py index 0b5c77469..faab073b4 100644 --- a/example/tutorial_vgg16.py +++ b/example/tutorial_vgg16.py @@ -76,12 +76,7 @@ def conv_layers(net_in): name='conv1_2' ) network = PoolLayer( - network, - ksize=[1, 2, 2, 1], - strides=[1, 2, 2, 1], - padding='SAME', - pool=tf.nn.max_pool, - name='pool1' + network, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', pool=tf.nn.max_pool, name='pool1' ) # conv2 @@ -102,12 +97,7 @@ def conv_layers(net_in): name='conv2_2' ) network = PoolLayer( - network, - ksize=[1, 2, 2, 1], - strides=[1, 2, 2, 1], - padding='SAME', - pool=tf.nn.max_pool, - name='pool2' + network, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', pool=tf.nn.max_pool, name='pool2' ) # conv3 @@ -136,12 +126,7 @@ def conv_layers(net_in): name='conv3_3' ) network = PoolLayer( - network, - ksize=[1, 2, 2, 1], - strides=[1, 2, 2, 1], - padding='SAME', - pool=tf.nn.max_pool, - name='pool3' + network, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', pool=tf.nn.max_pool, name='pool3' ) # conv4 @@ -170,12 +155,7 @@ def conv_layers(net_in): name='conv4_3' ) network = PoolLayer( - network, - ksize=[1, 2, 2, 1], - strides=[1, 2, 2, 1], - padding='SAME', - pool=tf.nn.max_pool, - name='pool4' + network, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', pool=tf.nn.max_pool, name='pool4' ) # conv5 @@ -204,12 +184,7 @@ def conv_layers(net_in): name='conv5_3' ) network = PoolLayer( - network, - ksize=[1, 2, 2, 1], - strides=[1, 2, 2, 1], - padding='SAME', - pool=tf.nn.max_pool, - name='pool5' + network, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', pool=tf.nn.max_pool, name='pool5' ) return network @@ -224,133 +199,55 @@ def conv_layers_simple_api(net_in): # conv1 network = Conv2d( - net_in, - n_filter=64, - filter_size=(3, 3), - strides=(1, 1), - act=tf.nn.relu, - padding='SAME', - name='conv1_1' + net_in, n_filter=64, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv1_1' ) network = Conv2d( - network, - n_filter=64, - filter_size=(3, 3), - strides=(1, 1), - act=tf.nn.relu, - padding='SAME', - name='conv1_2' + network, n_filter=64, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv1_2' ) network = MaxPool2d(network, filter_size=(2, 2), strides=(2, 2), padding='SAME', name='pool1') # conv2 network = Conv2d( - network, - n_filter=128, - filter_size=(3, 3), - strides=(1, 1), - act=tf.nn.relu, - padding='SAME', - name='conv2_1' + network, n_filter=128, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv2_1' ) network = Conv2d( - network, - n_filter=128, - filter_size=(3, 3), - strides=(1, 1), - act=tf.nn.relu, - padding='SAME', - name='conv2_2' + network, n_filter=128, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv2_2' ) network = MaxPool2d(network, filter_size=(2, 2), strides=(2, 2), padding='SAME', name='pool2') # conv3 network = Conv2d( - network, - n_filter=256, - filter_size=(3, 3), - strides=(1, 1), - act=tf.nn.relu, - padding='SAME', - name='conv3_1' + network, n_filter=256, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv3_1' ) network = Conv2d( - network, - n_filter=256, - filter_size=(3, 3), - strides=(1, 1), - act=tf.nn.relu, - padding='SAME', - name='conv3_2' + network, n_filter=256, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv3_2' ) network = Conv2d( - network, - n_filter=256, - filter_size=(3, 3), - strides=(1, 1), - act=tf.nn.relu, - padding='SAME', - name='conv3_3' + network, n_filter=256, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv3_3' ) network = MaxPool2d(network, filter_size=(2, 2), strides=(2, 2), padding='SAME', name='pool3') # conv4 network = Conv2d( - network, - n_filter=512, - filter_size=(3, 3), - strides=(1, 1), - act=tf.nn.relu, - padding='SAME', - name='conv4_1' + network, n_filter=512, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv4_1' ) network = Conv2d( - network, - n_filter=512, - filter_size=(3, 3), - strides=(1, 1), - act=tf.nn.relu, - padding='SAME', - name='conv4_2' + network, n_filter=512, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv4_2' ) network = Conv2d( - network, - n_filter=512, - filter_size=(3, 3), - strides=(1, 1), - act=tf.nn.relu, - padding='SAME', - name='conv4_3' + network, n_filter=512, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv4_3' ) network = MaxPool2d(network, filter_size=(2, 2), strides=(2, 2), padding='SAME', name='pool4') # conv5 network = Conv2d( - network, - n_filter=512, - filter_size=(3, 3), - strides=(1, 1), - act=tf.nn.relu, - padding='SAME', - name='conv5_1' + network, n_filter=512, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv5_1' ) network = Conv2d( - network, - n_filter=512, - filter_size=(3, 3), - strides=(1, 1), - act=tf.nn.relu, - padding='SAME', - name='conv5_2' + network, n_filter=512, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv5_2' ) network = Conv2d( - network, - n_filter=512, - filter_size=(3, 3), - strides=(1, 1), - act=tf.nn.relu, - padding='SAME', - name='conv5_3' + network, n_filter=512, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv5_3' ) network = MaxPool2d(network, filter_size=(2, 2), strides=(2, 2), padding='SAME', name='pool5') return network diff --git a/example/tutorial_vgg19.py b/example/tutorial_vgg19.py index b9c098b8a..f1685a6ed 100755 --- a/example/tutorial_vgg19.py +++ b/example/tutorial_vgg19.py @@ -94,187 +94,84 @@ def Vgg19(rgb): red - VGG_MEAN[2], ]) else: - bgr = tf.concat([ - blue - VGG_MEAN[0], - green - VGG_MEAN[1], - red - VGG_MEAN[2], - ], axis=3) + bgr = tf.concat( + [ + blue - VGG_MEAN[0], + green - VGG_MEAN[1], + red - VGG_MEAN[2], + ], axis=3 + ) if bgr.get_shape().as_list()[1:] != [224, 224, 3]: raise Exception("image size unmatch") # input layer net_in = InputLayer(bgr, name='input') # conv1 network = Conv2dLayer( - net_in, - act=tf.nn.relu, - shape=[3, 3, 3, 64], - strides=[1, 1, 1, 1], - padding='SAME', - name='conv1_1' + net_in, act=tf.nn.relu, shape=[3, 3, 3, 64], strides=[1, 1, 1, 1], padding='SAME', name='conv1_1' ) network = Conv2dLayer( - network, - act=tf.nn.relu, - shape=[3, 3, 64, 64], - strides=[1, 1, 1, 1], - padding='SAME', - name='conv1_2' + network, act=tf.nn.relu, shape=[3, 3, 64, 64], strides=[1, 1, 1, 1], padding='SAME', name='conv1_2' ) network = PoolLayer( - network, - ksize=[1, 2, 2, 1], - strides=[1, 2, 2, 1], - padding='SAME', - pool=tf.nn.max_pool, - name='pool1' + network, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', pool=tf.nn.max_pool, name='pool1' ) # conv2 network = Conv2dLayer( - network, - act=tf.nn.relu, - shape=[3, 3, 64, 128], - strides=[1, 1, 1, 1], - padding='SAME', - name='conv2_1' + network, act=tf.nn.relu, shape=[3, 3, 64, 128], strides=[1, 1, 1, 1], padding='SAME', name='conv2_1' ) network = Conv2dLayer( - network, - act=tf.nn.relu, - shape=[3, 3, 128, 128], - strides=[1, 1, 1, 1], - padding='SAME', - name='conv2_2' + network, act=tf.nn.relu, shape=[3, 3, 128, 128], strides=[1, 1, 1, 1], padding='SAME', name='conv2_2' ) network = PoolLayer( - network, - ksize=[1, 2, 2, 1], - strides=[1, 2, 2, 1], - padding='SAME', - pool=tf.nn.max_pool, - name='pool2' + network, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', pool=tf.nn.max_pool, name='pool2' ) # conv3 network = Conv2dLayer( - network, - act=tf.nn.relu, - shape=[3, 3, 128, 256], - strides=[1, 1, 1, 1], - padding='SAME', - name='conv3_1' + network, act=tf.nn.relu, shape=[3, 3, 128, 256], strides=[1, 1, 1, 1], padding='SAME', name='conv3_1' ) network = Conv2dLayer( - network, - act=tf.nn.relu, - shape=[3, 3, 256, 256], - strides=[1, 1, 1, 1], - padding='SAME', - name='conv3_2' + network, act=tf.nn.relu, shape=[3, 3, 256, 256], strides=[1, 1, 1, 1], padding='SAME', name='conv3_2' ) network = Conv2dLayer( - network, - act=tf.nn.relu, - shape=[3, 3, 256, 256], - strides=[1, 1, 1, 1], - padding='SAME', - name='conv3_3' + network, act=tf.nn.relu, shape=[3, 3, 256, 256], strides=[1, 1, 1, 1], padding='SAME', name='conv3_3' ) network = Conv2dLayer( - network, - act=tf.nn.relu, - shape=[3, 3, 256, 256], - strides=[1, 1, 1, 1], - padding='SAME', - name='conv3_4' + network, act=tf.nn.relu, shape=[3, 3, 256, 256], strides=[1, 1, 1, 1], padding='SAME', name='conv3_4' ) network = PoolLayer( - network, - ksize=[1, 2, 2, 1], - strides=[1, 2, 2, 1], - padding='SAME', - pool=tf.nn.max_pool, - name='pool3' + network, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', pool=tf.nn.max_pool, name='pool3' ) # conv4 network = Conv2dLayer( - network, - act=tf.nn.relu, - shape=[3, 3, 256, 512], - strides=[1, 1, 1, 1], - padding='SAME', - name='conv4_1' + network, act=tf.nn.relu, shape=[3, 3, 256, 512], strides=[1, 1, 1, 1], padding='SAME', name='conv4_1' ) network = Conv2dLayer( - network, - act=tf.nn.relu, - shape=[3, 3, 512, 512], - strides=[1, 1, 1, 1], - padding='SAME', - name='conv4_2' + network, act=tf.nn.relu, shape=[3, 3, 512, 512], strides=[1, 1, 1, 1], padding='SAME', name='conv4_2' ) network = Conv2dLayer( - network, - act=tf.nn.relu, - shape=[3, 3, 512, 512], - strides=[1, 1, 1, 1], - padding='SAME', - name='conv4_3' + network, act=tf.nn.relu, shape=[3, 3, 512, 512], strides=[1, 1, 1, 1], padding='SAME', name='conv4_3' ) network = Conv2dLayer( - network, - act=tf.nn.relu, - shape=[3, 3, 512, 512], - strides=[1, 1, 1, 1], - padding='SAME', - name='conv4_4' + network, act=tf.nn.relu, shape=[3, 3, 512, 512], strides=[1, 1, 1, 1], padding='SAME', name='conv4_4' ) network = PoolLayer( - network, - ksize=[1, 2, 2, 1], - strides=[1, 2, 2, 1], - padding='SAME', - pool=tf.nn.max_pool, - name='pool4' + network, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', pool=tf.nn.max_pool, name='pool4' ) # conv5 network = Conv2dLayer( - network, - act=tf.nn.relu, - shape=[3, 3, 512, 512], - strides=[1, 1, 1, 1], - padding='SAME', - name='conv5_1' + network, act=tf.nn.relu, shape=[3, 3, 512, 512], strides=[1, 1, 1, 1], padding='SAME', name='conv5_1' ) network = Conv2dLayer( - network, - act=tf.nn.relu, - shape=[3, 3, 512, 512], - strides=[1, 1, 1, 1], - padding='SAME', - name='conv5_2' + network, act=tf.nn.relu, shape=[3, 3, 512, 512], strides=[1, 1, 1, 1], padding='SAME', name='conv5_2' ) network = Conv2dLayer( - network, - act=tf.nn.relu, - shape=[3, 3, 512, 512], - strides=[1, 1, 1, 1], - padding='SAME', - name='conv5_3' + network, act=tf.nn.relu, shape=[3, 3, 512, 512], strides=[1, 1, 1, 1], padding='SAME', name='conv5_3' ) network = Conv2dLayer( - network, - act=tf.nn.relu, - shape=[3, 3, 512, 512], - strides=[1, 1, 1, 1], - padding='SAME', - name='conv5_4' + network, act=tf.nn.relu, shape=[3, 3, 512, 512], strides=[1, 1, 1, 1], padding='SAME', name='conv5_4' ) network = PoolLayer( - network, - ksize=[1, 2, 2, 1], - strides=[1, 2, 2, 1], - padding='SAME', - pool=tf.nn.max_pool, - name='pool5' + network, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', pool=tf.nn.max_pool, name='pool5' ) # fc 6~8 network = FlattenLayer(network, name='flatten') @@ -315,167 +212,73 @@ def Vgg19_simple_api(rgb): red - VGG_MEAN[2], ]) else: - bgr = tf.concat([ - blue - VGG_MEAN[0], - green - VGG_MEAN[1], - red - VGG_MEAN[2], - ], axis=3) + bgr = tf.concat( + [ + blue - VGG_MEAN[0], + green - VGG_MEAN[1], + red - VGG_MEAN[2], + ], axis=3 + ) if bgr.get_shape().as_list()[1:] != [224, 224, 3]: raise Exception("image size unmatch") # input layer net_in = InputLayer(bgr, name='input') # conv1 network = Conv2d( - net_in, - n_filter=64, - filter_size=(3, 3), - strides=(1, 1), - act=tf.nn.relu, - padding='SAME', - name='conv1_1' + net_in, n_filter=64, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv1_1' ) network = Conv2d( - network, - n_filter=64, - filter_size=(3, 3), - strides=(1, 1), - act=tf.nn.relu, - padding='SAME', - name='conv1_2' + network, n_filter=64, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv1_2' ) network = MaxPool2d(network, filter_size=(2, 2), strides=(2, 2), padding='SAME', name='pool1') # conv2 network = Conv2d( - network, - n_filter=128, - filter_size=(3, 3), - strides=(1, 1), - act=tf.nn.relu, - padding='SAME', - name='conv2_1' + network, n_filter=128, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv2_1' ) network = Conv2d( - network, - n_filter=128, - filter_size=(3, 3), - strides=(1, 1), - act=tf.nn.relu, - padding='SAME', - name='conv2_2' + network, n_filter=128, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv2_2' ) network = MaxPool2d(network, filter_size=(2, 2), strides=(2, 2), padding='SAME', name='pool2') # conv3 network = Conv2d( - network, - n_filter=256, - filter_size=(3, 3), - strides=(1, 1), - act=tf.nn.relu, - padding='SAME', - name='conv3_1' + network, n_filter=256, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv3_1' ) network = Conv2d( - network, - n_filter=256, - filter_size=(3, 3), - strides=(1, 1), - act=tf.nn.relu, - padding='SAME', - name='conv3_2' + network, n_filter=256, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv3_2' ) network = Conv2d( - network, - n_filter=256, - filter_size=(3, 3), - strides=(1, 1), - act=tf.nn.relu, - padding='SAME', - name='conv3_3' + network, n_filter=256, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv3_3' ) network = Conv2d( - network, - n_filter=256, - filter_size=(3, 3), - strides=(1, 1), - act=tf.nn.relu, - padding='SAME', - name='conv3_4' + network, n_filter=256, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv3_4' ) network = MaxPool2d(network, filter_size=(2, 2), strides=(2, 2), padding='SAME', name='pool3') # conv4 network = Conv2d( - network, - n_filter=512, - filter_size=(3, 3), - strides=(1, 1), - act=tf.nn.relu, - padding='SAME', - name='conv4_1' + network, n_filter=512, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv4_1' ) network = Conv2d( - network, - n_filter=512, - filter_size=(3, 3), - strides=(1, 1), - act=tf.nn.relu, - padding='SAME', - name='conv4_2' + network, n_filter=512, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv4_2' ) network = Conv2d( - network, - n_filter=512, - filter_size=(3, 3), - strides=(1, 1), - act=tf.nn.relu, - padding='SAME', - name='conv4_3' + network, n_filter=512, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv4_3' ) network = Conv2d( - network, - n_filter=512, - filter_size=(3, 3), - strides=(1, 1), - act=tf.nn.relu, - padding='SAME', - name='conv4_4' + network, n_filter=512, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv4_4' ) network = MaxPool2d(network, filter_size=(2, 2), strides=(2, 2), padding='SAME', name='pool4') # conv5 network = Conv2d( - network, - n_filter=512, - filter_size=(3, 3), - strides=(1, 1), - act=tf.nn.relu, - padding='SAME', - name='conv5_1' + network, n_filter=512, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv5_1' ) network = Conv2d( - network, - n_filter=512, - filter_size=(3, 3), - strides=(1, 1), - act=tf.nn.relu, - padding='SAME', - name='conv5_2' + network, n_filter=512, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv5_2' ) network = Conv2d( - network, - n_filter=512, - filter_size=(3, 3), - strides=(1, 1), - act=tf.nn.relu, - padding='SAME', - name='conv5_3' + network, n_filter=512, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv5_3' ) network = Conv2d( - network, - n_filter=512, - filter_size=(3, 3), - strides=(1, 1), - act=tf.nn.relu, - padding='SAME', - name='conv5_4' + network, n_filter=512, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, padding='SAME', name='conv5_4' ) network = MaxPool2d(network, filter_size=(2, 2), strides=(2, 2), padding='SAME', name='pool5') # fc 6~8 diff --git a/example/tutorial_word2vec_basic.py b/example/tutorial_word2vec_basic.py index 8e54a8bff..360ea7dfc 100644 --- a/example/tutorial_word2vec_basic.py +++ b/example/tutorial_word2vec_basic.py @@ -139,9 +139,7 @@ def main_word2vec_basic(): print('Most 5 common words (+UNK)', count[:5]) # [['UNK', 418391], (b'the', 1061396), (b'of', 593677), (b'and', 416629), (b'one', 411764)] print( - 'Sample data', - data[:10], - [reverse_dictionary[i] for i in data[:10]] + 'Sample data', data[:10], [reverse_dictionary[i] for i in data[:10]] ) # [5243, 3081, 12, 6, 195, 2, 3135, 46, 59, 156] [b'anarchism', b'originated', b'as', b'a', b'term', b'of', b'abuse', b'first', b'used', b'against'] del words # Hint to reduce memory. @@ -150,21 +148,13 @@ def main_word2vec_basic(): print() batch, labels, data_index = tl.nlp.generate_skip_gram_batch( - data=data, - batch_size=8, - num_skips=4, - skip_window=2, - data_index=0 + data=data, batch_size=8, num_skips=4, skip_window=2, data_index=0 ) for i in range(8): print(batch[i], reverse_dictionary[batch[i]], '->', labels[i, 0], reverse_dictionary[labels[i, 0]]) batch, labels, data_index = tl.nlp.generate_skip_gram_batch( - data=data, - batch_size=8, - num_skips=2, - skip_window=1, - data_index=0 + data=data, batch_size=8, num_skips=2, skip_window=1, data_index=0 ) for i in range(8): print(batch[i], reverse_dictionary[batch[i]], '->', labels[i, 0], reverse_dictionary[labels[i, 0]]) @@ -211,9 +201,7 @@ def main_word2vec_basic(): train_params = emb_net.all_params # train_op = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost, var_list=train_params) train_op = tf.train.AdagradOptimizer( - learning_rate, - initial_accumulator_value=0.1, - use_locking=False + learning_rate, initial_accumulator_value=0.1, use_locking=False ).minimize( cost, var_list=train_params ) @@ -250,11 +238,7 @@ def main_word2vec_basic(): while step < num_steps: start_time = time.time() batch_inputs, batch_labels, data_index = tl.nlp.generate_skip_gram_batch( - data=data, - batch_size=batch_size, - num_skips=num_skips, - skip_window=skip_window, - data_index=data_index + data=data, batch_size=batch_size, num_skips=num_skips, skip_window=skip_window, data_index=data_index ) feed_dict = {train_inputs: batch_inputs, train_labels: batch_labels} # We perform one update step by evaluating the train_op (including it @@ -267,10 +251,7 @@ def main_word2vec_basic(): average_loss /= print_freq print( "Average loss at step %d/%d. loss:%f took:%fs" % - (step, - num_steps, - average_loss, - time.time() - start_time) + (step, num_steps, average_loss, time.time() - start_time) ) average_loss = 0 # Prints out nearby words given a list of words. @@ -316,12 +297,7 @@ def main_word2vec_basic(): final_embeddings = sess.run(normalized_embeddings) #.eval() tl.visualize.tsne_embedding( - final_embeddings, - reverse_dictionary, - plot_only=500, - second=5, - saveable=False, - name='word2vec_basic' + final_embeddings, reverse_dictionary, plot_only=500, second=5, saveable=False, name='word2vec_basic' ) # Step 7: Evaluate by analogy questions. see tensorflow/models/embedding/word2vec_optimized.py diff --git a/setup.cfg b/setup.cfg index f1f4b2acc..96f68c83d 100644 --- a/setup.cfg +++ b/setup.cfg @@ -24,7 +24,7 @@ indent_width = 4 column_limit = 120 # Do not split consecutive brackets. Only relevant when DEDENT_CLOSING_BRACKETS is set -coalesce_brackets = True +coalesce_brackets = False # Put closing brackets on a separate line, dedented, # if the bracketed expression can't fit in a single line. diff --git a/tensorlayer/cli/train.py b/tensorlayer/cli/train.py index 24744a479..f5524af41 100755 --- a/tensorlayer/cli/train.py +++ b/tensorlayer/cli/train.py @@ -108,10 +108,12 @@ def create_tf_jobs(cluster_spec, prog, args): for job_type in cluster_spec: for task_index in range(len(cluster_spec[job_type])): new_env = os.environ.copy() - new_env.update({ - 'CUDA_VISIBLE_DEVICES': str(gpu_assignment.get((job_type, task_index), '')), - 'TF_CONFIG': json.dumps(create_tf_config(cluster_spec, job_type, task_index)), - }) + new_env.update( + { + 'CUDA_VISIBLE_DEVICES': str(gpu_assignment.get((job_type, task_index), '')), + 'TF_CONFIG': json.dumps(create_tf_config(cluster_spec, job_type, task_index)), + } + ) yield subprocess.Popen(['python3', prog] + args, env=new_env) diff --git a/tensorlayer/cost.py b/tensorlayer/cost.py index fe9810199..c30752b39 100644 --- a/tensorlayer/cost.py +++ b/tensorlayer/cost.py @@ -399,9 +399,9 @@ def cross_entropy_seq(logits, target_seqs, batch_size=None): #, batch_size=1, n # except: # sequence_loss_by_example_fn = tf.nn.seq2seq.sequence_loss_by_example - loss = sequence_loss_by_example_fn([logits], - [tf.reshape(target_seqs, [-1])], - [tf.ones_like(tf.reshape(target_seqs, [-1]), dtype=tf.float32)]) + loss = sequence_loss_by_example_fn( + [logits], [tf.reshape(target_seqs, [-1])], [tf.ones_like(tf.reshape(target_seqs, [-1]), dtype=tf.float32)] + ) # [tf.ones([batch_size * num_steps])]) cost = tf.reduce_sum(loss) #/ batch_size if batch_size is not None: @@ -703,9 +703,7 @@ def mn_o(weights, name='maxnorm_o_regularizer'): else: standard_ops_fn = standard_ops.multiply return standard_ops_fn( - my_scale, - standard_ops.reduce_sum(standard_ops.reduce_max(standard_ops.abs(weights), 0)), - name=scope + my_scale, standard_ops.reduce_sum(standard_ops.reduce_max(standard_ops.abs(weights), 0)), name=scope ) return mn_o @@ -755,9 +753,7 @@ def mn_i(weights, name='maxnorm_i_regularizer'): else: standard_ops_fn = standard_ops.multiply return standard_ops_fn( - my_scale, - standard_ops.reduce_sum(standard_ops.reduce_max(standard_ops.abs(weights), 1)), - name=scope + my_scale, standard_ops.reduce_sum(standard_ops.reduce_max(standard_ops.abs(weights), 1)), name=scope ) return mn_i diff --git a/tensorlayer/db.py b/tensorlayer/db.py index c5897235b..2db3ab9fa 100644 --- a/tensorlayer/db.py +++ b/tensorlayer/db.py @@ -16,6 +16,7 @@ def AutoFill(func): + def func_wrapper(self, *args, **kwargs): d = inspect.getcallargs(func, self, *args, **kwargs) d['args'].update({"studyID": self.studyID}) @@ -60,13 +61,7 @@ class TensorDB(object): """ def __init__( - self, - ip='localhost', - port=27017, - db_name='db_name', - user_name=None, - password='password', - studyID=None + self, ip='localhost', port=27017, db_name='db_name', user_name=None, password='password', studyID=None ): ## connect mongodb client = MongoClient(ip, port) diff --git a/tensorlayer/deprecation.py b/tensorlayer/deprecation.py index d0da09bb8..7b0e78c8a 100644 --- a/tensorlayer/deprecation.py +++ b/tensorlayer/deprecation.py @@ -8,7 +8,9 @@ def deprecated_alias(end_support_version, **aliases): + def deco(f): + @functools.wraps(f) def wrapper(*args, **kwargs): @@ -39,9 +41,6 @@ def rename_kwargs(kwargs, aliases, end_support_version, func_name): logging.warning( "DeprecationWarning: {}(): " "`{}` argument is deprecated and will be removed in version {}, " - "please change for `{}.`".format(func_name, - alias, - end_support_version, - new) + "please change for `{}.`".format(func_name, alias, end_support_version, new) ) kwargs[new] = kwargs.pop(alias) diff --git a/tensorlayer/distributed.py b/tensorlayer/distributed.py index d9ebb495d..88b0d7b69 100644 --- a/tensorlayer/distributed.py +++ b/tensorlayer/distributed.py @@ -71,11 +71,13 @@ def __init__(self, task_type='master', index=0, trial=None, ps_hosts=None, worke self.num_ps = len(self.ps_hosts) self.worker_hosts = worker_hosts if isinstance(worker_hosts, list) else worker_hosts.split(',') if master is not None and len(master) > 0: - self._cluster_spec = tf.train.ClusterSpec({ - 'ps': self.ps_hosts, - 'worker': self.worker_hosts, - 'master': master - }) + self._cluster_spec = tf.train.ClusterSpec( + { + 'ps': self.ps_hosts, + 'worker': self.worker_hosts, + 'master': master + } + ) # master is a worker too self.num_workers = len(self.worker_hosts) + 1 if self.type == 'worker': @@ -109,9 +111,7 @@ def device_fn(self): current_device = '/job:{}/task:{}'.format(self.type, self._index) ps_devices = '/job:ps' return tf.train.replica_device_setter( - ps_device=ps_devices, - worker_device=current_device, - cluster=self._cluster_spec + ps_device=ps_devices, worker_device=current_device, cluster=self._cluster_spec ) def create_server(self): @@ -167,16 +167,10 @@ def create_task_spec_def(): return TaskSpecDef( task_type=task_data['type'], index=task_data['index'], - trial=task_data['trial'] if - 'trial' in task_data - else - None, + trial=task_data['trial'] if 'trial' in task_data else None, ps_hosts=cluster_data['ps'], worker_hosts=cluster_data['worker'], - master=cluster_data['master'] if - 'master' in cluster_data - else - None + master=cluster_data['master'] if 'master' in cluster_data else None ) elif 'JOB_NAME' in os.environ: # JOB_NAME, TASK_INDEX, PS_HOSTS, WORKER_HOSTS and MASTER_HOST are used in TensorPort diff --git a/tensorlayer/files.py b/tensorlayer/files.py index 8a7f1ca80..3f911027c 100644 --- a/tensorlayer/files.py +++ b/tensorlayer/files.py @@ -108,10 +108,7 @@ def load_fashion_mnist_dataset(shape=(-1, 784), path='data'): >>> X_train, y_train, X_val, y_val, X_test, y_test = tl.files.load_fashion_mnist_dataset(shape=(-1, 28, 28, 1)) """ return _load_mnist_dataset( - shape, - path, - name='fashion_mnist', - url='http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/' + shape, path, name='fashion_mnist', url='http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/' ) @@ -817,16 +814,8 @@ def load_flickr1M_dataset(tag='sky', size=10, path="data", n_threads=50, printab path = os.path.join(path, 'flickr1M') logging.info("[Flickr1M] using {}% of images = {}".format(size * 10, size * 100000)) images_zip = [ - 'images0.zip', - 'images1.zip', - 'images2.zip', - 'images3.zip', - 'images4.zip', - 'images5.zip', - 'images6.zip', - 'images7.zip', - 'images8.zip', - 'images9.zip' + 'images0.zip', 'images1.zip', 'images2.zip', 'images3.zip', 'images4.zip', 'images5.zip', 'images6.zip', + 'images7.zip', 'images8.zip', 'images9.zip' ] tag_zip = 'tags.zip' url = 'http://press.liacs.nl/mirflickr/mirflickr1m/' @@ -958,11 +947,7 @@ def save_response_content(response, destination, chunk_size=32 * 1024): total_size = int(response.headers.get('content-length', 0)) with open(destination, "wb") as f: for chunk in tqdm( - response.iter_content(chunk_size), - total=total_size, - unit='B', - unit_scale=True, - desc=destination + response.iter_content(chunk_size), total=total_size, unit='B', unit_scale=True, desc=destination ): if chunk: # filter out keep-alive new chunks f.write(chunk) @@ -1185,26 +1170,8 @@ def _recursive_parse_xml_to_dict(xml): del_folder(os.path.join(path, 'VOCdevkit')) # object classes(labels) NOTE: YOU CAN CUSTOMIZE THIS LIST classes = [ - "aeroplane", - "bicycle", - "bird", - "boat", - "bottle", - "bus", - "car", - "cat", - "chair", - "cow", - "diningtable", - "dog", - "horse", - "motorbike", - "person", - "pottedplant", - "sheep", - "sofa", - "train", - "tvmonitor" + "aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow", "diningtable", "dog", + "horse", "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor" ] if contain_classes_in_person: classes_in_person = ["head", "hand", "foot"] @@ -1221,9 +1188,10 @@ def _recursive_parse_xml_to_dict(xml): folder_imgs = os.path.join(path, extracted_filename, "JPEGImages") imgs_file_list = load_file_list(path=folder_imgs, regx='\\.jpg', printable=False) logging.info("[VOC] {} images found".format(len(imgs_file_list))) - imgs_file_list.sort( - key=lambda s: int(s.replace('.', ' ').replace('_', '').split(' ')[-2]) - ) # 2007_000027.jpg --> 2007000027 + + imgs_file_list.sort(key=lambda s: int(s.replace('.', ' ').replace('_', '').split(' ')[-2]) + ) # 2007_000027.jpg --> 2007000027 + imgs_file_list = [os.path.join(folder_imgs, s) for s in imgs_file_list] # logging.info('IM',imgs_file_list[0::3333], imgs_file_list[-1]) if dataset != "2012test": @@ -1232,9 +1200,8 @@ def _recursive_parse_xml_to_dict(xml): folder_semseg = os.path.join(path, extracted_filename, "SegmentationClass") imgs_semseg_file_list = load_file_list(path=folder_semseg, regx='\\.png', printable=False) logging.info("[VOC] {} maps for semantic segmentation found".format(len(imgs_semseg_file_list))) - imgs_semseg_file_list.sort( - key=lambda s: int(s.replace('.', ' ').replace('_', '').split(' ')[-2]) - ) # 2007_000032.png --> 2007000032 + imgs_semseg_file_list.sort(key=lambda s: int(s.replace('.', ' ').replace('_', '').split(' ')[-2]) + ) # 2007_000032.png --> 2007000032 imgs_semseg_file_list = [os.path.join(folder_semseg, s) for s in imgs_semseg_file_list] # logging.info('Semantic Seg IM',imgs_semseg_file_list[0::333], imgs_semseg_file_list[-1]) ##======== 3. instance segmentation maps path list @@ -1242,9 +1209,8 @@ def _recursive_parse_xml_to_dict(xml): folder_insseg = os.path.join(path, extracted_filename, "SegmentationObject") imgs_insseg_file_list = load_file_list(path=folder_insseg, regx='\\.png', printable=False) logging.info("[VOC] {} maps for instance segmentation found".format(len(imgs_semseg_file_list))) - imgs_insseg_file_list.sort( - key=lambda s: int(s.replace('.', ' ').replace('_', '').split(' ')[-2]) - ) # 2007_000032.png --> 2007000032 + imgs_insseg_file_list.sort(key=lambda s: int(s.replace('.', ' ').replace('_', '').split(' ')[-2]) + ) # 2007_000032.png --> 2007000032 imgs_insseg_file_list = [os.path.join(folder_insseg, s) for s in imgs_insseg_file_list] # logging.info('Instance Seg IM',imgs_insseg_file_list[0::333], imgs_insseg_file_list[-1]) else: @@ -1257,9 +1223,8 @@ def _recursive_parse_xml_to_dict(xml): logging.info( "[VOC] {} XML annotation files for bounding box and object class found".format(len(imgs_ann_file_list)) ) - imgs_ann_file_list.sort( - key=lambda s: int(s.replace('.', ' ').replace('_', '').split(' ')[-2]) - ) # 2007_000027.xml --> 2007000027 + imgs_ann_file_list.sort(key=lambda s: int(s.replace('.', ' ').replace('_', '').split(' ')[-2]) + ) # 2007_000027.xml --> 2007000027 imgs_ann_file_list = [os.path.join(folder_ann, s) for s in imgs_ann_file_list] # logging.info('ANN',imgs_ann_file_list[0::3333], imgs_ann_file_list[-1]) @@ -1312,9 +1277,7 @@ def convert_annotation(file_name): cls_id = classes.index(cls) xmlbox = obj.find('bndbox') b = ( - float(xmlbox.find('xmin').text), - float(xmlbox.find('xmax').text), - float(xmlbox.find('ymin').text), + float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), float(xmlbox.find('ymin').text), float(xmlbox.find('ymax').text) ) bb = convert((w, h), b) @@ -1329,10 +1292,8 @@ def convert_annotation(file_name): cls_id = classes.index(cls) xmlbox = part.find('bndbox') b = ( - float(xmlbox.find('xmin').text), - float(xmlbox.find('xmax').text), - float(xmlbox.find('ymin').text), - float(xmlbox.find('ymax').text) + float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), + float(xmlbox.find('ymin').text), float(xmlbox.find('ymax').text) ) bb = convert((w, h), b) # out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n') @@ -1443,10 +1404,11 @@ def save_joints(): # fp = open(joint_data_fn, 'w') mat = sio.loadmat(os.path.join(path, extracted_filename, "mpii_human_pose_v1_u12_1.mat")) - for _, (anno, train_flag) in enumerate( # all images - zip( - mat['RELEASE']['annolist'][0, 0][0], - mat['RELEASE']['img_train'][0, 0][0])): + for _, ( + anno, train_flag + ) in enumerate( # all images + zip(mat['RELEASE']['annolist'][0, 0][0], mat['RELEASE']['img_train'][0, 0][0]) + ): img_fn = anno['image']['name'][0, 0][0] train_flag = int(train_flag) @@ -1462,10 +1424,10 @@ def save_joints(): head_rect = [] if 'x1' in str(anno['annorect'].dtype): - head_rect = zip([x1[0, 0] for x1 in anno['annorect']['x1'][0]], - [y1[0, 0] for y1 in anno['annorect']['y1'][0]], - [x2[0, 0] for x2 in anno['annorect']['x2'][0]], - [y2[0, 0] for y2 in anno['annorect']['y2'][0]]) + head_rect = zip( + [x1[0, 0] for x1 in anno['annorect']['x1'][0]], [y1[0, 0] for y1 in anno['annorect']['y1'][0]], + [x2[0, 0] for x2 in anno['annorect']['x2'][0]], [y2[0, 0] for y2 in anno['annorect']['y2'][0]] + ) else: head_rect = [] # TODO @@ -1477,11 +1439,7 @@ def save_joints(): head_y2s = anno['annorect']['y2'][0] for annopoint, head_x1, head_y1, head_x2, head_y2 in zip( - annopoints, - head_x1s, - head_y1s, - head_x2s, - head_y2s + annopoints, head_x1s, head_y1s, head_x2s, head_y2s ): # if annopoint != []: # if len(annopoint) != 0: @@ -1836,12 +1794,7 @@ def load_and_assign_npz_dict(name='model.npz', sess=None): def save_ckpt( - sess=None, - mode_name='model.ckpt', - save_dir='checkpoint', - var_list=None, - global_step=None, - printable=False + sess=None, mode_name='model.ckpt', save_dir='checkpoint', var_list=None, global_step=None, printable=False ): """Save parameters into `ckpt` file. diff --git a/tensorlayer/iterate.py b/tensorlayer/iterate.py index e37949550..67af7a322 100644 --- a/tensorlayer/iterate.py +++ b/tensorlayer/iterate.py @@ -60,8 +60,8 @@ def minibatches(inputs=None, targets=None, batch_size=None, shuffle=False): else: excerpt = slice(start_idx, start_idx + batch_size) if (isinstance(inputs, list) or isinstance(targets, list)) and (shuffle ==True): - yield [inputs[i] for i in excerpt], [targets[i] - for i in excerpt] # zsdonghao: for list indexing when shuffle==True + # zsdonghao: for list indexing when shuffle==True + yield [inputs[i] for i in excerpt], [targets[i] for i in excerpt] else: yield inputs[excerpt], targets[excerpt] @@ -136,8 +136,8 @@ def seq_minibatches(inputs, targets, batch_size, seq_length, stride=1): end_seq_idx = start_seq_idx + seq_length seq_inputs[b_idx] = inputs[start_seq_idx:end_seq_idx] seq_targets[b_idx] = targets[start_seq_idx:end_seq_idx] - flatten_inputs = seq_inputs.reshape((-1, ) + inputs.shape[1:]) - flatten_targets = seq_targets.reshape((-1, ) + targets.shape[1:]) + flatten_inputs = seq_inputs.reshape((-1,) + inputs.shape[1:]) + flatten_targets = seq_targets.reshape((-1,) + targets.shape[1:]) yield flatten_inputs, flatten_targets diff --git a/tensorlayer/layers/binary.py b/tensorlayer/layers/binary.py index c48ba0957..6c846f3cc 100644 --- a/tensorlayer/layers/binary.py +++ b/tensorlayer/layers/binary.py @@ -81,8 +81,7 @@ def _compute_alpha(x): alpha_array = tf.add(alpha1_temp1, alpha1_temp2, name=None) alpha_array_abs = tf.abs(alpha_array) alpha_array_abs1 = tf.where( - tf.greater(alpha_array_abs, 0), - tf.ones_like(alpha_array_abs, tf.float32), + tf.greater(alpha_array_abs, 0), tf.ones_like(alpha_array_abs, tf.float32), tf.zeros_like(alpha_array_abs, tf.float32) ) alpha_sum = tf.reduce_sum(alpha_array_abs) @@ -164,11 +163,7 @@ def __init__( with tf.variable_scope(name): W = tf.get_variable( - name='W', - shape=(n_in, n_units), - initializer=W_init, - dtype=LayersConfig.tf_dtype, - **W_init_args + name='W', shape=(n_in, n_units), initializer=W_init, dtype=LayersConfig.tf_dtype, **W_init_args ) # W = tl.act.sign(W) # dont update ... W = quantize(W) @@ -177,11 +172,7 @@ def __init__( if b_init is not None: try: b = tf.get_variable( - name='b', - shape=(n_units), - initializer=b_init, - dtype=LayersConfig.tf_dtype, - **b_init_args + name='b', shape=(n_units), initializer=b_init, dtype=LayersConfig.tf_dtype, **b_init_args ) except Exception: # If initializer is a constant, do not specify shape. b = tf.get_variable(name='b', initializer=b_init, dtype=LayersConfig.tf_dtype, **b_init_args) @@ -281,12 +272,7 @@ def __init__( super(BinaryConv2d, self).__init__(prev_layer=prev_layer, name=name) logging.info( "BinaryConv2d %s: n_filter:%d filter_size:%s strides:%s pad:%s act:%s" % - (name, - n_filter, - str(filter_size), - str(strides), - padding, - act.__name__) + (name, n_filter, str(filter_size), str(strides), padding, act.__name__) ) self.inputs = prev_layer.outputs @@ -311,20 +297,12 @@ def __init__( strides = (1, strides[0], strides[1], 1) with tf.variable_scope(name): W = tf.get_variable( - name='W_conv2d', - shape=shape, - initializer=W_init, - dtype=LayersConfig.tf_dtype, - **W_init_args + name='W_conv2d', shape=shape, initializer=W_init, dtype=LayersConfig.tf_dtype, **W_init_args ) W = quantize(W) if b_init: b = tf.get_variable( - name='b_conv2d', - shape=(shape[-1]), - initializer=b_init, - dtype=LayersConfig.tf_dtype, - **b_init_args + name='b_conv2d', shape=(shape[-1]), initializer=b_init, dtype=LayersConfig.tf_dtype, **b_init_args ) self.outputs = act( tf.nn.conv2d( @@ -416,11 +394,7 @@ def __init__( with tf.variable_scope(name): W = tf.get_variable( - name='W', - shape=(n_in, n_units), - initializer=W_init, - dtype=LayersConfig.tf_dtype, - **W_init_args + name='W', shape=(n_in, n_units), initializer=W_init, dtype=LayersConfig.tf_dtype, **W_init_args ) # W = tl.act.sign(W) # dont update ... alpha = _compute_alpha(W) @@ -431,11 +405,7 @@ def __init__( if b_init is not None: try: b = tf.get_variable( - name='b', - shape=(n_units), - initializer=b_init, - dtype=LayersConfig.tf_dtype, - **b_init_args + name='b', shape=(n_units), initializer=b_init, dtype=LayersConfig.tf_dtype, **b_init_args ) except Exception: # If initializer is a constant, do not specify shape. b = tf.get_variable(name='b', initializer=b_init, dtype=LayersConfig.tf_dtype, **b_init_args) @@ -535,12 +505,7 @@ def __init__( super(TernaryConv2d, self).__init__(prev_layer=prev_layer, name=name) logging.info( "TernaryConv2d %s: n_filter:%d filter_size:%s strides:%s pad:%s act:%s" % - (name, - n_filter, - str(filter_size), - str(strides), - padding, - act.__name__) + (name, n_filter, str(filter_size), str(strides), padding, act.__name__) ) if W_init_args is None: @@ -563,22 +528,14 @@ def __init__( strides = (1, strides[0], strides[1], 1) with tf.variable_scope(name): W = tf.get_variable( - name='W_conv2d', - shape=shape, - initializer=W_init, - dtype=LayersConfig.tf_dtype, - **W_init_args + name='W_conv2d', shape=shape, initializer=W_init, dtype=LayersConfig.tf_dtype, **W_init_args ) alpha = _compute_alpha(W) W = _ternary_operation(W) W = tf.multiply(alpha, W) if b_init: b = tf.get_variable( - name='b_conv2d', - shape=(shape[-1]), - initializer=b_init, - dtype=LayersConfig.tf_dtype, - **b_init_args + name='b_conv2d', shape=(shape[-1]), initializer=b_init, dtype=LayersConfig.tf_dtype, **b_init_args ) self.outputs = act( tf.nn.conv2d( @@ -677,11 +634,7 @@ def __init__( with tf.variable_scope(name): W = tf.get_variable( - name='W', - shape=(n_in, n_units), - initializer=W_init, - dtype=LayersConfig.tf_dtype, - **W_init_args + name='W', shape=(n_in, n_units), initializer=W_init, dtype=LayersConfig.tf_dtype, **W_init_args ) # W = tl.act.sign(W) # dont update ... W = _quantize_weight(W, bitW) @@ -691,11 +644,7 @@ def __init__( if b_init is not None: try: b = tf.get_variable( - name='b', - shape=(n_units), - initializer=b_init, - dtype=LayersConfig.tf_dtype, - **b_init_args + name='b', shape=(n_units), initializer=b_init, dtype=LayersConfig.tf_dtype, **b_init_args ) except Exception: # If initializer is a constant, do not specify shape. b = tf.get_variable(name='b', initializer=b_init, dtype=LayersConfig.tf_dtype, **b_init_args) @@ -801,12 +750,7 @@ def __init__( super(DorefaConv2d, self).__init__(prev_layer=prev_layer, name=name) logging.info( "DorefaConv2d %s: n_filter:%d filter_size:%s strides:%s pad:%s act:%s" % - (name, - n_filter, - str(filter_size), - str(strides), - padding, - act.__name__) + (name, n_filter, str(filter_size), str(strides), padding, act.__name__) ) self.inputs = prev_layer.outputs @@ -832,21 +776,13 @@ def __init__( strides = (1, strides[0], strides[1], 1) with tf.variable_scope(name): W = tf.get_variable( - name='W_conv2d', - shape=shape, - initializer=W_init, - dtype=LayersConfig.tf_dtype, - **W_init_args + name='W_conv2d', shape=shape, initializer=W_init, dtype=LayersConfig.tf_dtype, **W_init_args ) W = _quantize_weight(W, bitW) self.inputs = _quantize_active(_cabs(self.inputs), bitA) if b_init: b = tf.get_variable( - name='b_conv2d', - shape=(shape[-1]), - initializer=b_init, - dtype=LayersConfig.tf_dtype, - **b_init_args + name='b_conv2d', shape=(shape[-1]), initializer=b_init, dtype=LayersConfig.tf_dtype, **b_init_args ) self.outputs = act( tf.nn.conv2d( diff --git a/tensorlayer/layers/convolution.py b/tensorlayer/layers/convolution.py index bf8b90703..386ff9cb1 100644 --- a/tensorlayer/layers/convolution.py +++ b/tensorlayer/layers/convolution.py @@ -80,10 +80,7 @@ def __init__( ): super(Conv1dLayer, self).__init__(prev_layer=prev_layer, name=name) logging.info( - "Conv1dLayer %s: shape:%s stride:%s pad:%s act:%s" % (name, - str(shape), - str(stride), - padding, act.__name__) + "Conv1dLayer %s: shape:%s stride:%s pad:%s act:%s" % (name, str(shape), str(stride), padding, act.__name__) ) self.inputs = prev_layer.outputs @@ -98,26 +95,14 @@ def __init__( with tf.variable_scope(name): W = tf.get_variable( - name='W_conv1d', - shape=shape, - initializer=W_init, - dtype=LayersConfig.tf_dtype, - **W_init_args + name='W_conv1d', shape=shape, initializer=W_init, dtype=LayersConfig.tf_dtype, **W_init_args ) self.outputs = tf.nn.convolution( - self.inputs, - W, - strides=(stride, ), - padding=padding, - dilation_rate=(dilation_rate, ) + self.inputs, W, strides=(stride,), padding=padding, dilation_rate=(dilation_rate,) ) # 1.2 if b_init: b = tf.get_variable( - name='b_conv1d', - shape=(shape[-1]), - initializer=b_init, - dtype=LayersConfig.tf_dtype, - **b_init_args + name='b_conv1d', shape=(shape[-1]), initializer=b_init, dtype=LayersConfig.tf_dtype, **b_init_args ) self.outputs = self.outputs + b @@ -217,10 +202,7 @@ def __init__( super(Conv2dLayer, self).__init__(prev_layer=prev_layer, name=name) logging.info( "Conv2dLayer %s: shape:%s strides:%s pad:%s act:%s" % - (name, - str(shape), - str(strides), - padding, act.__name__) + (name, str(shape), str(strides), padding, act.__name__) ) self.inputs = prev_layer.outputs @@ -234,19 +216,11 @@ def __init__( with tf.variable_scope(name): W = tf.get_variable( - name='W_conv2d', - shape=shape, - initializer=W_init, - dtype=LayersConfig.tf_dtype, - **W_init_args + name='W_conv2d', shape=shape, initializer=W_init, dtype=LayersConfig.tf_dtype, **W_init_args ) if b_init: b = tf.get_variable( - name='b_conv2d', - shape=(shape[-1]), - initializer=b_init, - dtype=LayersConfig.tf_dtype, - **b_init_args + name='b_conv2d', shape=(shape[-1]), initializer=b_init, dtype=LayersConfig.tf_dtype, **b_init_args ) self.outputs = act( tf.nn.conv2d( @@ -372,11 +346,7 @@ def __init__( super(DeConv2dLayer, self).__init__(prev_layer=prev_layer, name=name) logging.info( "DeConv2dLayer %s: shape:%s out_shape:%s strides:%s pad:%s act:%s" % - (name, - str(shape), - str(output_shape), - str(strides), - padding, act.__name__) + (name, str(shape), str(output_shape), str(strides), padding, act.__name__) ) self.inputs = prev_layer.outputs @@ -391,11 +361,7 @@ def __init__( # logging.info(" DeConv2dLayer: Untested") with tf.variable_scope(name): W = tf.get_variable( - name='W_deconv2d', - shape=shape, - initializer=W_init, - dtype=LayersConfig.tf_dtype, - **W_init_args + name='W_deconv2d', shape=shape, initializer=W_init, dtype=LayersConfig.tf_dtype, **W_init_args ) if b_init: b = tf.get_variable( @@ -411,11 +377,7 @@ def __init__( ) else: self.outputs = act( - tf.nn.conv2d_transpose(self.inputs, - W, - output_shape=output_shape, - strides=strides, - padding=padding) + tf.nn.conv2d_transpose(self.inputs, W, output_shape=output_shape, strides=strides, padding=padding) ) # self.all_layers = list(layer.all_layers) @@ -481,10 +443,7 @@ def __init__( super(Conv3dLayer, self).__init__(prev_layer=prev_layer, name=name) logging.info( "Conv3dLayer %s: shape:%s strides:%s pad:%s act:%s" % - (name, - str(shape), - str(strides), - padding, act.__name__) + (name, str(shape), str(strides), padding, act.__name__) ) self.inputs = prev_layer.outputs @@ -500,19 +459,11 @@ def __init__( # W = tf.Variable(W_init(shape=shape, **W_init_args), name='W_conv') # b = tf.Variable(b_init(shape=[shape[-1]], **b_init_args), name='b_conv') W = tf.get_variable( - name='W_conv3d', - shape=shape, - initializer=W_init, - dtype=LayersConfig.tf_dtype, - **W_init_args + name='W_conv3d', shape=shape, initializer=W_init, dtype=LayersConfig.tf_dtype, **W_init_args ) if b_init: b = tf.get_variable( - name='b_conv3d', - shape=(shape[-1]), - initializer=b_init, - dtype=LayersConfig.tf_dtype, - **b_init_args + name='b_conv3d', shape=(shape[-1]), initializer=b_init, dtype=LayersConfig.tf_dtype, **b_init_args ) self.outputs = act(tf.nn.conv3d(self.inputs, W, strides=strides, padding=padding, name=None) + b) else: @@ -579,11 +530,7 @@ def __init__( super(DeConv3dLayer, self).__init__(prev_layer=prev_layer, name=name) logging.info( "DeConv3dLayer %s: shape:%s out_shape:%s strides:%s pad:%s act:%s" % - (name, - str(shape), - str(output_shape), - str(strides), - padding, act.__name__) + (name, str(shape), str(output_shape), str(strides), padding, act.__name__) ) self.inputs = prev_layer.outputs @@ -597,11 +544,7 @@ def __init__( with tf.variable_scope(name): W = tf.get_variable( - name='W_deconv3d', - shape=shape, - initializer=W_init, - dtype=LayersConfig.tf_dtype, - **W_init_args + name='W_deconv3d', shape=shape, initializer=W_init, dtype=LayersConfig.tf_dtype, **W_init_args ) if b_init: b = tf.get_variable( @@ -617,11 +560,7 @@ def __init__( ) else: self.outputs = act( - tf.nn.conv3d_transpose(self.inputs, - W, - output_shape=output_shape, - strides=strides, - padding=padding) + tf.nn.conv3d_transpose(self.inputs, W, output_shape=output_shape, strides=strides, padding=padding) ) # self.all_layers = list(layer.all_layers) @@ -671,11 +610,7 @@ def __init__( super(UpSampling2dLayer, self).__init__(prev_layer=prev_layer, name=name) logging.info( "UpSampling2dLayer %s: is_scale:%s size:%s method:%d align_corners:%s" % - (name, - is_scale, - size, - method, - align_corners) + (name, is_scale, size, method, align_corners) ) self.inputs = prev_layer.outputs @@ -701,18 +636,11 @@ def __init__( with tf.variable_scope(name): try: self.outputs = tf.image.resize_images( - self.inputs, - size=size, - method=method, - align_corners=align_corners + self.inputs, size=size, method=method, align_corners=align_corners ) except Exception: # for TF 0.10 self.outputs = tf.image.resize_images( - self.inputs, - new_height=size[0], - new_width=size[1], - method=method, - align_corners=align_corners + self.inputs, new_height=size[0], new_width=size[1], method=method, align_corners=align_corners ) # self.all_layers = list(layer.all_layers) @@ -758,11 +686,7 @@ def __init__( super(DownSampling2dLayer, self).__init__(prev_layer=prev_layer, name=name) logging.info( "DownSampling2dLayer %s: is_scale:%s size:%s method:%d, align_corners:%s" % - (name, - is_scale, - size, - method, - align_corners) + (name, is_scale, size, method, align_corners) ) self.inputs = prev_layer.outputs @@ -786,18 +710,11 @@ def __init__( with tf.variable_scope(name): try: self.outputs = tf.image.resize_images( - self.inputs, - size=size, - method=method, - align_corners=align_corners + self.inputs, size=size, method=method, align_corners=align_corners ) except Exception: # for TF 0.10 self.outputs = tf.image.resize_images( - self.inputs, - new_height=size[0], - new_width=size[1], - method=method, - align_corners=align_corners + self.inputs, new_height=size[0], new_width=size[1], method=method, align_corners=align_corners ) # self.all_layers = list(layer.all_layers) @@ -995,11 +912,13 @@ def _tf_batch_map_offsets(inputs, offsets, grid_offset): coords = tf.tile(coords, [batch_size, 1, 1, 1, 1]) + offsets # grid_offset --> (b, h, w, n, 2) # clip out of bound - coords = tf.stack([ - tf.clip_by_value(coords[:, :, :, :, 0], 0.0, tf.cast(input_h - 1, 'float32')), - tf.clip_by_value(coords[:, :, :, :, 1], 0.0, tf.cast(input_w - 1, 'float32')) - ], - axis=-1) + coords = tf.stack( + [ + tf.clip_by_value(coords[:, :, :, :, 0], 0.0, tf.cast(input_h - 1, 'float32')), + tf.clip_by_value(coords[:, :, :, :, 1], 0.0, tf.cast(input_w - 1, 'float32')) + ], + axis=-1 + ) coords = tf.tile(coords, [channel, 1, 1, 1, 1]) mapped_vals = _tf_batch_map_coordinates(inputs, coords) @@ -1011,10 +930,7 @@ def _tf_batch_map_offsets(inputs, offsets, grid_offset): super(DeformableConv2d, self).__init__(prev_layer=prev_layer, name=name) logging.info( "DeformableConv2d %s: n_filter: %d, filter_size: %s act:%s" % - (name, - n_filter, - str(filter_size), - act.__name__) + (name, n_filter, str(filter_size), act.__name__) ) self.inputs = prev_layer.outputs @@ -1061,13 +977,7 @@ def _tf_batch_map_offsets(inputs, offsets, grid_offset): W = tf.get_variable( name='W_deformableconv2d', - shape=[ - 1, - 1, - shape[0] * shape[1], - shape[-2], - shape[-1] - ], + shape=[1, 1, shape[0] * shape[1], shape[-2], shape[-1]], initializer=W_init, dtype=LayersConfig.tf_dtype, **W_init_args @@ -1084,30 +994,12 @@ def _tf_batch_map_offsets(inputs, offsets, grid_offset): tf.reshape() self.outputs = tf.reshape( tensor=act(tf.nn.conv3d(input_deform, W, strides=[1, 1, 1, 1, 1], padding='VALID', name=None) + b), - shape=(tf.shape(self.inputs)[0], - input_h, - input_w, - shape[-1]) + shape=(tf.shape(self.inputs)[0], input_h, input_w, shape[-1]) ) else: self.outputs = tf.reshape( - tensor=act(tf.nn.conv3d( - input_deform, - W, - strides=[ - 1, - 1, - 1, - 1, - 1 - ], - padding='VALID', - name=None - )), - shape=[tf.shape(self.inputs)[0], - input_h, - input_w, - shape[-1]] + tensor=act(tf.nn.conv3d(input_deform, W, strides=[1, 1, 1, 1, 1], padding='VALID', name=None)), + shape=[tf.shape(self.inputs)[0], input_h, input_w, shape[-1]] ) # fixed @@ -1258,12 +1150,7 @@ def __init__( super(AtrousConv2dLayer, self).__init__(prev_layer=prev_layer, name=name) logging.info( "AtrousConv2dLayer %s: n_filter:%d filter_size:%s rate:%d pad:%s act:%s" % - (name, - n_filter, - filter_size, - rate, - padding, - act.__name__) + (name, n_filter, filter_size, rate, padding, act.__name__) ) self.inputs = prev_layer.outputs @@ -1278,19 +1165,11 @@ def __init__( with tf.variable_scope(name): shape = [filter_size[0], filter_size[1], int(self.inputs.get_shape()[-1]), n_filter] filters = tf.get_variable( - name='filter', - shape=shape, - initializer=W_init, - dtype=LayersConfig.tf_dtype, - **W_init_args + name='filter', shape=shape, initializer=W_init, dtype=LayersConfig.tf_dtype, **W_init_args ) if b_init: b = tf.get_variable( - name='b', - shape=(n_filter), - initializer=b_init, - dtype=LayersConfig.tf_dtype, - **b_init_args + name='b', shape=(n_filter), initializer=b_init, dtype=LayersConfig.tf_dtype, **b_init_args ) self.outputs = act(tf.nn.atrous_conv2d(self.inputs, filters, rate, padding) + b) else: @@ -1387,13 +1266,7 @@ def __init__( logging.info( "SeparableConv2dLayer %s: n_filter:%d filter_size:%s strides:%s padding:%s dilation_rate:%s depth_multiplier:%s act:%s" % ( - name, - n_filter, - filter_size, - str(strides), - padding, - str(dilation_rate), - str(depth_multiplier), + name, n_filter, filter_size, str(strides), padding, str(dilation_rate), str(depth_multiplier), act.__name__ ) ) @@ -1576,13 +1449,7 @@ def __init__( super(Conv1d, self).__init__(prev_layer=prev_layer, name=name) logging.info( "Conv1d %s: n_filter:%d filter_size:%s stride:%d pad:%s act:%s dilation_rate:%d" % - (name, - n_filter, - filter_size, - stride, - padding, - act.__name__, - dilation_rate) + (name, n_filter, filter_size, stride, padding, act.__name__, dilation_rate) ) self.inputs = prev_layer.outputs @@ -1742,12 +1609,7 @@ def __init__( if tf.__version__ > '1.5': logging.info( "Conv2d %s: n_filter:%d filter_size:%s strides:%s pad:%s act:%s" % - (self.name, - n_filter, - str(filter_size), - str(strides), - padding, - act.__name__) + (self.name, n_filter, str(filter_size), str(strides), padding, act.__name__) ) # with tf.variable_scope(name) as vs: conv2d = tf.layers.Conv2D( @@ -1788,19 +1650,12 @@ def __init__( logging.info( "Conv2d %s: shape:%s strides:%s pad:%s act:%s" % - (self.name, - str(shape), - str(strides), - padding, act.__name__) + (self.name, str(shape), str(strides), padding, act.__name__) ) with tf.variable_scope(name): W = tf.get_variable( - name='W_conv2d', - shape=shape, - initializer=W_init, - dtype=LayersConfig.tf_dtype, - **W_init_args + name='W_conv2d', shape=shape, initializer=W_init, dtype=LayersConfig.tf_dtype, **W_init_args ) if b_init: b = tf.get_variable( @@ -1875,9 +1730,7 @@ class DeConv2d(Layer): """ @deprecated_alias( - layer='prev_layer', - n_out_channel='n_filter', - end_support_version=1.9 + layer='prev_layer', n_out_channel='n_filter', end_support_version=1.9 ) # TODO remove this line for the 1.9 release def __init__( self, @@ -1898,10 +1751,7 @@ def __init__( super(DeConv2d, self).__init__(prev_layer=prev_layer, name=name) logging.info( "DeConv2d %s: n_filters:%s strides:%s pad:%s act:%s" % - (name, - str(n_filter), - str(strides), - padding, act.__name__) + (name, str(n_filter), str(strides), padding, act.__name__) ) if W_init_args is None: @@ -1998,10 +1848,7 @@ def __init__( super(DeConv3d, self).__init__(prev_layer=prev_layer, name=name) logging.info( "DeConv3d %s: n_filters:%s strides:%s pad:%s act:%s" % - (name, - str(n_filter), - str(strides), - padding, act.__name__) + (name, str(n_filter), str(strides), padding, act.__name__) ) self.inputs = prev_layer.outputs @@ -2101,10 +1948,7 @@ def __init__( super(DepthwiseConv2d, self).__init__(prev_layer=prev_layer, name=name) logging.info( "DepthwiseConv2d %s: shape:%s strides:%s pad:%s act:%s" % - (name, - str(shape), - str(strides), - padding, act.__name__) + (name, str(shape), str(strides), padding, act.__name__) ) self.inputs = prev_layer.outputs @@ -2131,11 +1975,7 @@ def __init__( with tf.variable_scope(name): W = tf.get_variable( - name='W_depthwise2d', - shape=shape, - initializer=W_init, - dtype=LayersConfig.tf_dtype, - **W_init_args + name='W_depthwise2d', shape=shape, initializer=W_init, dtype=LayersConfig.tf_dtype, **W_init_args ) # [filter_height, filter_width, in_channels, depth_multiplier] if b_init: b = tf.get_variable( @@ -2150,11 +1990,7 @@ def __init__( ) else: self.outputs = act( - tf.nn.depthwise_conv2d(self.inputs, - W, - strides=strides, - padding=padding, - rate=dilation_rate) + tf.nn.depthwise_conv2d(self.inputs, W, strides=strides, padding=padding, rate=dilation_rate) ) # self.all_layers = list(layer.all_layers) @@ -2239,12 +2075,7 @@ def __init__( super(SeparableConv2d, self).__init__(prev_layer=prev_layer, name=name) logging.info( "SeparableConv2d %s: n_filter:%d filter_size:%s filter_size:%s depth_multiplier:%d act:%s" % - (self.name, - n_filter, - str(filter_size), - str(strides), - depth_multiplier, - act.__name__) + (self.name, n_filter, str(filter_size), str(strides), depth_multiplier, act.__name__) ) self.inputs = prev_layer.outputs @@ -2331,13 +2162,7 @@ def __init__( super(GroupConv2d, self).__init__(prev_layer=prev_layer, name=name) logging.info( "GroupConv2d %s: n_filter:%d size:%s strides:%s n_group:%d pad:%s act:%s" % - (name, - n_filter, - str(filter_size), - str(strides), - n_group, - padding, - act.__name__) + (name, n_filter, str(filter_size), str(strides), n_group, padding, act.__name__) ) self.inputs = prev_layer.outputs diff --git a/tensorlayer/layers/core.py b/tensorlayer/layers/core.py index 5c8d19a64..1b049b42e 100644 --- a/tensorlayer/layers/core.py +++ b/tensorlayer/layers/core.py @@ -430,13 +430,7 @@ def print_params(self, details=True, session=None): val = p.eval(session=session) logging.info( " param {:3}: {:20} {:15} {} (mean: {:<18}, median: {:<18}, std: {:<18}) ".format( - i, - p.name, - str(val.shape), - p.dtype.name, - val.mean(), - np.median(val), - val.std() + i, p.name, str(val.shape), p.dtype.name, val.mean(), np.median(val), val.std() ) ) except Exception as e: @@ -453,10 +447,7 @@ def print_layers(self): for i, layer in enumerate(self.all_layers): # logging.info(" layer %d: %s" % (i, str(layer))) logging.info( - " layer {:3}: {:20} {:15} {}".format(i, - layer.name, - str(layer.get_shape()), - layer.dtype.name) + " layer {:3}: {:20} {:15} {}".format(i, layer.name, str(layer.get_shape()), layer.dtype.name) ) def count_params(self): @@ -992,20 +983,12 @@ def __init__( with tf.variable_scope(name): W = tf.get_variable( - name='W', - shape=(n_in, n_units), - initializer=W_init, - dtype=LayersConfig.tf_dtype, - **W_init_args + name='W', shape=(n_in, n_units), initializer=W_init, dtype=LayersConfig.tf_dtype, **W_init_args ) if b_init is not None: try: b = tf.get_variable( - name='b', - shape=(n_units), - initializer=b_init, - dtype=LayersConfig.tf_dtype, - **b_init_args + name='b', shape=(n_units), initializer=b_init, dtype=LayersConfig.tf_dtype, **b_init_args ) except Exception: # If initializer is a constant, do not specify shape. b = tf.get_variable(name='b', initializer=b_init, dtype=LayersConfig.tf_dtype, **b_init_args) @@ -1161,11 +1144,7 @@ def __init__( raise Exception("Don't support the given reconstruct activation function") self.train_op = tf.train.AdamOptimizer( - learning_rate, - beta1=0.9, - beta2=0.999, - epsilon=1e-08, - use_locking=False + learning_rate, beta1=0.9, beta2=0.999, epsilon=1e-08, use_locking=False ).minimize( self.cost, var_list=self.train_params ) @@ -1493,18 +1472,10 @@ def __init__( with tf.variable_scope(name): W = tf.get_variable( - name='W', - shape=(n_in, n_units), - initializer=W_init, - dtype=LayersConfig.tf_dtype, - **W_init_args + name='W', shape=(n_in, n_units), initializer=W_init, dtype=LayersConfig.tf_dtype, **W_init_args ) b = tf.get_variable( - name='b', - shape=(n_units), - initializer=b_init, - dtype=LayersConfig.tf_dtype, - **b_init_args + name='b', shape=(n_units), initializer=b_init, dtype=LayersConfig.tf_dtype, **b_init_args ) # self.outputs = act(tf.matmul(self.inputs, W) + b) diff --git a/tensorlayer/layers/normalization.py b/tensorlayer/layers/normalization.py index 110acf8b4..b52838098 100644 --- a/tensorlayer/layers/normalization.py +++ b/tensorlayer/layers/normalization.py @@ -51,11 +51,7 @@ def __init__( super(LocalResponseNormLayer, self).__init__(prev_layer=prev_layer, name=name) logging.info( "LocalResponseNormLayer %s: depth_radius: %s, bias: %s, alpha: %s, beta: %s" % - (name, - str(depth_radius), - str(bias), - str(alpha), - str(beta)) + (name, str(depth_radius), str(bias), str(alpha), str(beta)) ) self.inputs = prev_layer.outputs @@ -119,11 +115,7 @@ def __init__( super(BatchNormLayer, self).__init__(prev_layer=prev_layer, name=name) logging.info( "BatchNormLayer %s: decay:%f epsilon:%f act:%s is_train:%s" % - (name, - decay, - epsilon, - act.__name__, - is_train) + (name, decay, epsilon, act.__name__, is_train) ) self.inputs = prev_layer.outputs @@ -141,11 +133,7 @@ def __init__( if tf.__version__ > '0.12.1' and beta_init == tf.zeros_initializer: beta_init = beta_init() beta = tf.get_variable( - 'beta', - shape=params_shape, - initializer=beta_init, - dtype=LayersConfig.tf_dtype, - trainable=is_train + 'beta', shape=params_shape, initializer=beta_init, dtype=LayersConfig.tf_dtype, trainable=is_train ) variables.append(beta) else: @@ -169,11 +157,7 @@ def __init__( else: moving_mean_init = tf.zeros_initializer moving_mean = tf.get_variable( - 'moving_mean', - params_shape, - initializer=moving_mean_init, - dtype=LayersConfig.tf_dtype, - trainable=False + 'moving_mean', params_shape, initializer=moving_mean_init, dtype=LayersConfig.tf_dtype, trainable=False ) moving_variance = tf.get_variable( 'moving_variance', @@ -188,16 +172,10 @@ def __init__( mean, variance = tf.nn.moments(self.inputs, axis) try: # TF12 update_moving_mean = moving_averages.assign_moving_average( - moving_mean, - mean, - decay, - zero_debias=False + moving_mean, mean, decay, zero_debias=False ) # if zero_debias=True, has bias update_moving_variance = moving_averages.assign_moving_average( - moving_variance, - variance, - decay, - zero_debias=False + moving_variance, variance, decay, zero_debias=False ) # if zero_debias=True, has bias # logging.info("TF12 moving") except Exception: # TF11 @@ -214,12 +192,7 @@ def mean_var_with_update(): self.outputs = act(tf.nn.batch_normalization(self.inputs, mean, var, beta, gamma, epsilon)) else: self.outputs = act( - tf.nn.batch_normalization(self.inputs, - moving_mean, - moving_variance, - beta, - gamma, - epsilon) + tf.nn.batch_normalization(self.inputs, moving_mean, moving_variance, beta, gamma, epsilon) ) variables.extend([moving_mean, moving_variance]) diff --git a/tensorlayer/layers/pooling.py b/tensorlayer/layers/pooling.py index 5b13f3f59..95ecb48e6 100644 --- a/tensorlayer/layers/pooling.py +++ b/tensorlayer/layers/pooling.py @@ -69,10 +69,7 @@ def __init__( super(PoolLayer, self).__init__(prev_layer=prev_layer, name=name) logging.info( "PoolLayer %s: ksize:%s strides:%s padding:%s pool:%s" % - (name, - str(ksize), - str(strides), - padding, pool.__name__) + (name, str(ksize), str(strides), padding, pool.__name__) ) self.inputs = prev_layer.outputs @@ -107,30 +104,16 @@ class MaxPool1d(Layer): @deprecated_alias(net='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - filter_size=3, - strides=2, - padding='valid', - data_format='channels_last', - name='maxpool1d' + self, prev_layer, filter_size=3, strides=2, padding='valid', data_format='channels_last', name='maxpool1d' ): super(MaxPool1d, self).__init__(prev_layer=prev_layer, name=name) logging.info( - "MaxPool1d %s: filter_size:%s strides:%s padding:%s" % (name, - str(filter_size), - str(strides), - str(padding)) + "MaxPool1d %s: filter_size:%s strides:%s padding:%s" % (name, str(filter_size), str(strides), str(padding)) ) self.inputs = prev_layer.outputs # operation (customized) self.outputs = tf.layers.max_pooling1d( - self.inputs, - filter_size, - strides, - padding=padding, - data_format=data_format, - name=name + self.inputs, filter_size, strides, padding=padding, data_format=data_format, name=name ) # update layer (customized) self.all_layers.append(self.outputs) @@ -167,31 +150,17 @@ class MeanPool1d(Layer): # return net_new @deprecated_alias(net='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - filter_size=3, - strides=2, - padding='valid', - data_format='channels_last', - name='meanpool1d' + self, prev_layer, filter_size=3, strides=2, padding='valid', data_format='channels_last', name='meanpool1d' ): super(MeanPool1d, self).__init__(prev_layer=prev_layer, name=name) logging.info( "MeanPool1d %s: filter_size:%s strides:%s padding:%s" % - (name, - str(filter_size), - str(strides), - str(padding)) + (name, str(filter_size), str(strides), str(padding)) ) # operation (customized) self.outputs = tf.layers.average_pooling1d( - prev_layer.outputs, - filter_size, - strides, - padding=padding, - data_format=data_format, - name=name + prev_layer.outputs, filter_size, strides, padding=padding, data_format=data_format, name=name ) # update layer (customized) @@ -223,21 +192,13 @@ def __init__(self, prev_layer, filter_size=(3, 3), strides=(2, 2), padding='SAME super(MaxPool2d, self).__init__(prev_layer=prev_layer, name=name) logging.info( - "MaxPool2d %s: filter_size:%s strides:%s padding:%s" % (name, - str(filter_size), - str(strides), - str(padding)) + "MaxPool2d %s: filter_size:%s strides:%s padding:%s" % (name, str(filter_size), str(strides), str(padding)) ) self.inputs = prev_layer.outputs # operation (customized) if tf.__version__ > '1.5': self.outputs = tf.layers.max_pooling2d( - self.inputs, - filter_size, - strides, - padding=padding, - data_format='channels_last', - name=name + self.inputs, filter_size, strides, padding=padding, data_format='channels_last', name=name ) else: if len(strides) == 2: @@ -275,21 +236,13 @@ def __init__(self, prev_layer, filter_size=(3, 3), strides=(2, 2), padding='SAME super(MeanPool2d, self).__init__(prev_layer=prev_layer, name=name) logging.info( "MeanPool2d %s: filter_size:%s strides:%s padding:%s" % - (name, - str(filter_size), - str(strides), - str(padding)) + (name, str(filter_size), str(strides), str(padding)) ) self.inputs = prev_layer.outputs # operation (customized) if tf.__version__ > '1.5': self.outputs = tf.layers.average_pooling2d( - self.inputs, - filter_size, - strides, - padding=padding, - data_format='channels_last', - name=name + self.inputs, filter_size, strides, padding=padding, data_format='channels_last', name=name ) else: if len(strides) == 2: @@ -342,20 +295,12 @@ def __init__( ): super(MaxPool3d, self).__init__(prev_layer=prev_layer, name=name) logging.info( - "MaxPool3d %s: filter_size:%s strides:%s padding:%s" % (name, - str(filter_size), - str(strides), - str(padding)) + "MaxPool3d %s: filter_size:%s strides:%s padding:%s" % (name, str(filter_size), str(strides), str(padding)) ) # operation (customized) self.inputs = prev_layer.outputs self.outputs = tf.layers.max_pooling3d( - self.inputs, - filter_size, - strides, - padding=padding, - data_format=data_format, - name=name + self.inputs, filter_size, strides, padding=padding, data_format=data_format, name=name ) # update layer (customized) @@ -405,22 +350,14 @@ def __init__( super(MeanPool3d, self).__init__(prev_layer=prev_layer, name=name) logging.info( "MeanPool3d %s: filter_size:%s strides:%s padding:%s" % - (name, - str(filter_size), - str(strides), - str(padding)) + (name, str(filter_size), str(strides), str(padding)) ) self.inputs = prev_layer.outputs # operation (customized) self.outputs = tf.layers.average_pooling3d( - prev_layer.outputs, - filter_size, - strides, - padding=padding, - data_format=data_format, - name=name + prev_layer.outputs, filter_size, strides, padding=padding, data_format=data_format, name=name ) # update layer (customized) diff --git a/tensorlayer/layers/recurrent.py b/tensorlayer/layers/recurrent.py index 0ddac43b5..7674fee85 100644 --- a/tensorlayer/layers/recurrent.py +++ b/tensorlayer/layers/recurrent.py @@ -162,12 +162,7 @@ def __init__( logging.info( "RNNLayer %s: n_hidden:%d n_steps:%d in_dim:%d in_shape:%s cell_fn:%s " % - (self.name, - n_hidden, - n_steps, - self.inputs.get_shape().ndims, - self.inputs.get_shape(), - cell_fn.__name__) + (self.name, n_hidden, n_steps, self.inputs.get_shape().ndims, self.inputs.get_shape(), cell_fn.__name__) ) # You can get the dimension by .get_shape() or ._shape, and check the # dimension by .with_rank() as follow. @@ -360,14 +355,8 @@ def __init__( logging.info( "BiRNNLayer %s: n_hidden:%d n_steps:%d in_dim:%d in_shape:%s cell_fn:%s dropout:%s n_layer:%d " % ( - self.name, - n_hidden, - n_steps, - self.inputs.get_shape().ndims, - self.inputs.get_shape(), - cell_fn.__name__, - dropout, - n_layer + self.name, n_hidden, n_steps, self.inputs.get_shape().ndims, self.inputs.get_shape(), cell_fn.__name__, + dropout, n_layer ) ) @@ -419,10 +408,12 @@ def __init__( MultiRNNCell_fn = tf.nn.rnn_cell.MultiRNNCell if dropout: try: - self.fw_cell = MultiRNNCell_fn([cell_creator(is_last=i == n_layer - 1) for i in range(n_layer)], - state_is_tuple=True) - self.bw_cell = MultiRNNCell_fn([cell_creator(is_last=i == n_layer - 1) for i in range(n_layer)], - state_is_tuple=True) + self.fw_cell = MultiRNNCell_fn( + [cell_creator(is_last=i == n_layer - 1) for i in range(n_layer)], state_is_tuple=True + ) + self.bw_cell = MultiRNNCell_fn( + [cell_creator(is_last=i == n_layer - 1) for i in range(n_layer)], state_is_tuple=True + ) except Exception: self.fw_cell = MultiRNNCell_fn([cell_creator(is_last=i == n_layer - 1) for i in range(n_layer)]) self.bw_cell = MultiRNNCell_fn([cell_creator(is_last=i == n_layer - 1) for i in range(n_layer)]) @@ -556,14 +547,7 @@ class BasicConvLSTMCell(ConvRNNCell): """ def __init__( - self, - shape, - filter_size, - num_features, - forget_bias=1.0, - input_size=None, - state_is_tuple=False, - act=tf.nn.tanh + self, shape, filter_size, num_features, forget_bias=1.0, input_size=None, state_is_tuple=False, act=tf.nn.tanh ): """Initialize the basic Conv LSTM cell.""" # if not state_is_tuple: @@ -655,9 +639,7 @@ def _conv_linear(args, filter_size, num_features, bias, bias_start=0.0, scope=No # Now the computation. with tf.variable_scope(scope or "Conv"): matrix = tf.get_variable( - "Matrix", - [filter_size[0], filter_size[1], total_arg_size_depth, num_features], - dtype=dtype + "Matrix", [filter_size[0], filter_size[1], total_arg_size_depth, num_features], dtype=dtype ) if len(args) == 1: res = tf.nn.conv2d(args[0], matrix, strides=[1, 1, 1, 1], padding='SAME') @@ -666,10 +648,7 @@ def _conv_linear(args, filter_size, num_features, bias, bias_start=0.0, scope=No if not bias: return res bias_term = tf.get_variable( - "Bias", - [num_features], - dtype=dtype, - initializer=tf.constant_initializer(bias_start, dtype=dtype) + "Bias", [num_features], dtype=dtype, initializer=tf.constant_initializer(bias_start, dtype=dtype) ) return res + bias_term @@ -752,12 +731,7 @@ def __init__( logging.info( "ConvLSTMLayer %s: feature_map:%d, n_steps:%d, " "in_dim:%d %s, cell_fn:%s " % - (self.name, - feature_map, - n_steps, - self.inputs.get_shape().ndims, - self.inputs.get_shape(), - cell_fn.__name__) + (self.name, feature_map, n_steps, self.inputs.get_shape().ndims, self.inputs.get_shape(), cell_fn.__name__) ) # You can get the dimension by .get_shape() or ._shape, and check the # dimension by .with_rank() as follow. @@ -813,8 +787,7 @@ def __init__( # : stack more RNN layer after that # 5D Tensor [n_example/n_steps, n_steps, h, w, c] self.outputs = tf.reshape( - tf.concat(outputs, 1), - [-1, n_steps, cell_shape[0], cell_shape[1], feature_map] + tf.concat(outputs, 1), [-1, n_steps, cell_shape[0], cell_shape[1], feature_map] ) self.final_state = state @@ -1080,20 +1053,20 @@ class DynamicRNNLayer(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - cell_fn, #tf.nn.rnn_cell.LSTMCell, - cell_init_args=None, - n_hidden=256, - initializer=tf.random_uniform_initializer(-0.1, 0.1), - sequence_length=None, - initial_state=None, - dropout=None, - n_layer=1, - return_last=None, - return_seq_2d=False, - dynamic_rnn_init_args=None, - name='dyrnn', + self, + prev_layer, + cell_fn, #tf.nn.rnn_cell.LSTMCell, + cell_init_args=None, + n_hidden=256, + initializer=tf.random_uniform_initializer(-0.1, 0.1), + sequence_length=None, + initial_state=None, + dropout=None, + n_layer=1, + return_last=None, + return_seq_2d=False, + dynamic_rnn_init_args=None, + name='dyrnn', ): super(DynamicRNNLayer, self).__init__(prev_layer=prev_layer, name=name) @@ -1115,12 +1088,7 @@ def __init__( logging.info( "DynamicRNNLayer %s: n_hidden:%d, in_dim:%d in_shape:%s cell_fn:%s dropout:%s n_layer:%d" % ( - self.name, - n_hidden, - self.inputs.get_shape().ndims, - self.inputs.get_shape(), - cell_fn.__name__, - dropout, + self.name, n_hidden, self.inputs.get_shape().ndims, self.inputs.get_shape(), cell_fn.__name__, dropout, n_layer ) ) @@ -1183,8 +1151,9 @@ def __init__( if dropout: try: # cell_instance_fn=lambda: MultiRNNCell_fn([cell_instance_fn2() for _ in range(n_layer)], state_is_tuple=True) # HanSheng - self.cell = MultiRNNCell_fn([cell_creator(is_last=i == n_layer - 1) for i in range(n_layer)], - state_is_tuple=True) + self.cell = MultiRNNCell_fn( + [cell_creator(is_last=i == n_layer - 1) for i in range(n_layer)], state_is_tuple=True + ) except Exception: # when GRU # cell_instance_fn=lambda: MultiRNNCell_fn([cell_instance_fn2() for _ in range(n_layer)]) # HanSheng self.cell = MultiRNNCell_fn([cell_creator(is_last=i == n_layer - 1) for i in range(n_layer)]) @@ -1350,21 +1319,21 @@ class BiDynamicRNNLayer(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - cell_fn, #tf.nn.rnn_cell.LSTMCell, - cell_init_args=None, - n_hidden=256, - initializer=tf.random_uniform_initializer(-0.1, 0.1), - sequence_length=None, - fw_initial_state=None, - bw_initial_state=None, - dropout=None, - n_layer=1, - return_last=False, - return_seq_2d=False, - dynamic_rnn_init_args=None, - name='bi_dyrnn_layer', + self, + prev_layer, + cell_fn, #tf.nn.rnn_cell.LSTMCell, + cell_init_args=None, + n_hidden=256, + initializer=tf.random_uniform_initializer(-0.1, 0.1), + sequence_length=None, + fw_initial_state=None, + bw_initial_state=None, + dropout=None, + n_layer=1, + return_last=False, + return_seq_2d=False, + dynamic_rnn_init_args=None, + name='bi_dyrnn_layer', ): super(BiDynamicRNNLayer, self).__init__(prev_layer=prev_layer, name=name) @@ -1384,12 +1353,7 @@ def __init__( logging.info( "BiDynamicRNNLayer %s: n_hidden:%d in_dim:%d in_shape:%s cell_fn:%s dropout:%s n_layer:%d" % ( - self.name, - n_hidden, - self.inputs.get_shape().ndims, - self.inputs.get_shape(), - cell_fn.__name__, - dropout, + self.name, n_hidden, self.inputs.get_shape().ndims, self.inputs.get_shape(), cell_fn.__name__, dropout, n_layer ) ) @@ -1493,7 +1457,8 @@ def __init__( initial_state_fw=self.fw_initial_state, initial_state_bw=self.bw_initial_state, dtype=LayersConfig.tf_dtype, - **dynamic_rnn_init_args) + **dynamic_rnn_init_args + ) rnn_variables = tf.get_collection(TF_GRAPHKEYS_VARIABLES, scope=vs.name) @@ -1652,21 +1617,21 @@ class Seq2Seq(Layer): """ def __init__( - self, - net_encode_in, - net_decode_in, - cell_fn, #tf.nn.rnn_cell.LSTMCell, - cell_init_args=None, - n_hidden=256, - initializer=tf.random_uniform_initializer(-0.1, 0.1), - encode_sequence_length=None, - decode_sequence_length=None, - initial_state_encode=None, - initial_state_decode=None, - dropout=None, - n_layer=1, - return_seq_2d=False, - name='seq2seq', + self, + net_encode_in, + net_decode_in, + cell_fn, #tf.nn.rnn_cell.LSTMCell, + cell_init_args=None, + n_hidden=256, + initializer=tf.random_uniform_initializer(-0.1, 0.1), + encode_sequence_length=None, + decode_sequence_length=None, + initial_state_encode=None, + initial_state_decode=None, + dropout=None, + n_layer=1, + return_seq_2d=False, + name='seq2seq', ): super(Seq2Seq, self).__init__(prev_layer=None, name=name) @@ -1683,11 +1648,7 @@ def __init__( # self.inputs = layer.outputs logging.info( "[*] Seq2Seq %s: n_hidden:%d cell_fn:%s dropout:%s n_layer:%d" % - (self.name, - n_hidden, - cell_fn.__name__, - dropout, - n_layer) + (self.name, n_hidden, cell_fn.__name__, dropout, n_layer) ) with tf.variable_scope(name): diff --git a/tensorlayer/layers/spatial_transformer.py b/tensorlayer/layers/spatial_transformer.py index a918a0df2..a884f998e 100644 --- a/tensorlayer/layers/spatial_transformer.py +++ b/tensorlayer/layers/spatial_transformer.py @@ -135,9 +135,7 @@ def _meshgrid(height, width): # grid = np.vstack([x_t.flatten(), y_t.flatten(), ones]) x_t = tf.matmul( tf.ones(shape=tf.stack([height, 1])), - tf.transpose(tf.expand_dims(tf.linspace(-1.0, - 1.0, - width), 1), [1, 0]) + tf.transpose(tf.expand_dims(tf.linspace(-1.0, 1.0, width), 1), [1, 0]) ) y_t = tf.matmul(tf.expand_dims(tf.linspace(-1.0, 1.0, height), 1), tf.ones(shape=tf.stack([1, width]))) diff --git a/tensorlayer/layers/special_activation.py b/tensorlayer/layers/special_activation.py index 7d042193a..80b74f138 100644 --- a/tensorlayer/layers/special_activation.py +++ b/tensorlayer/layers/special_activation.py @@ -55,18 +55,14 @@ def __init__( self.inputs = prev_layer.outputs if channel_shared: - w_shape = (1, ) + w_shape = (1,) else: w_shape = int(self.inputs.get_shape()[-1]) # with tf.name_scope(name) as scope: with tf.variable_scope(name): alphas = tf.get_variable( - name='alphas', - shape=w_shape, - initializer=a_init, - dtype=LayersConfig.tf_dtype, - **a_init_args + name='alphas', shape=w_shape, initializer=a_init, dtype=LayersConfig.tf_dtype, **a_init_args ) try: # TF 1.0 self.outputs = tf.nn.relu(self.inputs) + tf.multiply(alphas, (self.inputs - tf.abs(self.inputs))) * 0.5 diff --git a/tensorlayer/models/mobilenetv1.py b/tensorlayer/models/mobilenetv1.py index b80b117c0..44659ffe9 100644 --- a/tensorlayer/models/mobilenetv1.py +++ b/tensorlayer/models/mobilenetv1.py @@ -8,14 +8,7 @@ import tensorflow as tf from .. import _logging as logging from ..layers import ( - Layer, - BatchNormLayer, - Conv2d, - DepthwiseConv2d, - FlattenLayer, - GlobalMeanPool2d, - InputLayer, - ReshapeLayer + Layer, BatchNormLayer, Conv2d, DepthwiseConv2d, FlattenLayer, GlobalMeanPool2d, InputLayer, ReshapeLayer ) from ..files import maybe_download_and_extract, assign_params, load_npz diff --git a/tensorlayer/models/vgg16.py b/tensorlayer/models/vgg16.py index c3dafc51f..8df2f394c 100644 --- a/tensorlayer/models/vgg16.py +++ b/tensorlayer/models/vgg16.py @@ -66,12 +66,7 @@ def conv_layers(net_in): name='conv1_2' ) net = PoolLayer( - net, - ksize=[1, 2, 2, 1], - strides=[1, 2, 2, 1], - padding='SAME', - pool=tf.nn.max_pool, - name='pool1' + net, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', pool=tf.nn.max_pool, name='pool1' ) # conv2 @@ -92,12 +87,7 @@ def conv_layers(net_in): name='conv2_2' ) net = PoolLayer( - net, - ksize=[1, 2, 2, 1], - strides=[1, 2, 2, 1], - padding='SAME', - pool=tf.nn.max_pool, - name='pool2' + net, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', pool=tf.nn.max_pool, name='pool2' ) # conv3 @@ -126,12 +116,7 @@ def conv_layers(net_in): name='conv3_3' ) net = PoolLayer( - net, - ksize=[1, 2, 2, 1], - strides=[1, 2, 2, 1], - padding='SAME', - pool=tf.nn.max_pool, - name='pool3' + net, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', pool=tf.nn.max_pool, name='pool3' ) # conv4 @@ -160,12 +145,7 @@ def conv_layers(net_in): name='conv4_3' ) net = PoolLayer( - net, - ksize=[1, 2, 2, 1], - strides=[1, 2, 2, 1], - padding='SAME', - pool=tf.nn.max_pool, - name='pool4' + net, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', pool=tf.nn.max_pool, name='pool4' ) # conv5 @@ -194,12 +174,7 @@ def conv_layers(net_in): name='conv5_3' ) net = PoolLayer( - net, - ksize=[1, 2, 2, 1], - strides=[1, 2, 2, 1], - padding='SAME', - pool=tf.nn.max_pool, - name='pool5' + net, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', pool=tf.nn.max_pool, name='pool5' ) return net @@ -291,10 +266,7 @@ def vgg16_simple_api(net_in, end_with): def restore_params(self, sess): logging.info("Restore pre-trained parameters") maybe_download_and_extract( - 'vgg16_weights.npz', - 'models', - 'http://www.cs.toronto.edu/~frossard/vgg16/', - expected_bytes=553436134 + 'vgg16_weights.npz', 'models', 'http://www.cs.toronto.edu/~frossard/vgg16/', expected_bytes=553436134 ) npz = np.load(os.path.join('models', 'vgg16_weights.npz')) diff --git a/tensorlayer/nlp.py b/tensorlayer/nlp.py index 642444a18..950a68093 100755 --- a/tensorlayer/nlp.py +++ b/tensorlayer/nlp.py @@ -978,12 +978,7 @@ def initialize_vocabulary(vocabulary_path): def sentence_to_token_ids( - sentence, - vocabulary, - tokenizer=None, - normalize_digits=True, - UNK_ID=3, - _DIGIT_RE=re.compile(br"\d") + sentence, vocabulary, tokenizer=None, normalize_digits=True, UNK_ID=3, _DIGIT_RE=re.compile(br"\d") ): """Convert a string to list of integers representing token-ids. @@ -1062,12 +1057,7 @@ def data_to_token_ids( if counter % 100000 == 0: logging.info(" tokenizing line %d" % counter) token_ids = sentence_to_token_ids( - line, - vocab, - tokenizer, - normalize_digits, - UNK_ID=UNK_ID, - _DIGIT_RE=_DIGIT_RE + line, vocab, tokenizer, normalize_digits, UNK_ID=UNK_ID, _DIGIT_RE=_DIGIT_RE ) tokens_file.write(" ".join([str(tok) for tok in token_ids]) + "\n") else: diff --git a/tensorlayer/prepro.py b/tensorlayer/prepro.py index 4e236628c..437e2d43c 100644 --- a/tensorlayer/prepro.py +++ b/tensorlayer/prepro.py @@ -181,12 +181,7 @@ def apply_fn(results, i, data, kwargs): threads = [] for i in range(thread_count): t = threading.Thread( - name='threading_and_return', - target=apply_fn, - args=(results, - i, - data[divs[i]:divs[i + 1]], - kwargs) + name='threading_and_return', target=apply_fn, args=(results, i, data[divs[i]:divs[i + 1]], kwargs) ) t.start() threads.append(t) @@ -204,15 +199,7 @@ def apply_fn(results, i, data, kwargs): def rotation( - x, - rg=20, - is_random=False, - row_index=0, - col_index=1, - channel_index=2, - fill_mode='nearest', - cval=0., - order=1 + x, rg=20, is_random=False, row_index=0, col_index=1, channel_index=2, fill_mode='nearest', cval=0., order=1 ): """Rotate an image randomly or non-randomly. @@ -258,15 +245,7 @@ def rotation( def rotation_multi( - x, - rg=20, - is_random=False, - row_index=0, - col_index=1, - channel_index=2, - fill_mode='nearest', - cval=0., - order=1 + x, rg=20, is_random=False, row_index=0, col_index=1, channel_index=2, fill_mode='nearest', cval=0., order=1 ): """Rotate multiple images with the same arguments, randomly or non-randomly. Usually be used for image segmentation which x=[X, Y], X and Y should be matched. @@ -566,15 +545,7 @@ def shift_multi( # shear def shear( - x, - intensity=0.1, - is_random=False, - row_index=0, - col_index=1, - channel_index=2, - fill_mode='nearest', - cval=0., - order=1 + x, intensity=0.1, is_random=False, row_index=0, col_index=1, channel_index=2, fill_mode='nearest', cval=0., order=1 ): """Shear an image randomly or non-randomly. @@ -619,15 +590,7 @@ def shear( def shear_multi( - x, - intensity=0.1, - is_random=False, - row_index=0, - col_index=1, - channel_index=2, - fill_mode='nearest', - cval=0., - order=1 + x, intensity=0.1, is_random=False, row_index=0, col_index=1, channel_index=2, fill_mode='nearest', cval=0., order=1 ): """Shear images with the same arguments, randomly or non-randomly. Usually be used for image segmentation which x=[X, Y], X and Y should be matched. @@ -1238,11 +1201,10 @@ def illumination(x, gamma=1., contrast=1., saturation=1., is_random=False): from PIL import Image, ImageEnhance if is_random: - try: - assert len(gamma) == len(contrast - ) == len(saturation) == 2, "if is_random = True, the arguments are (min, max)" - except: - raise Exception("if is_random = True, the arguments are (min, max)") + + if not (len(gamma) == len(contrast) == len(saturation) == 2): + raise AssertionError("if is_random = True, the arguments are (min, max)") + ## random change brightness # small --> brighter illum_settings = np.random.randint(0, 3) # 0-brighter, 1-darker, 2 keep normal @@ -1504,12 +1466,7 @@ def pixel_value_scale(im, val=0.9, clip=(-np.inf, np.inf), is_random=False): # normailization def samplewise_norm( - x, - rescale=None, - samplewise_center=False, - samplewise_std_normalization=False, - channel_index=2, - epsilon=1e-7 + x, rescale=None, samplewise_center=False, samplewise_std_normalization=False, channel_index=2, epsilon=1e-7 ): """Normalize an image by rescale, samplewise centering and samplewise centering in order. @@ -1845,12 +1802,7 @@ def apply_transform(x, transform_matrix, channel_index=2, fill_mode='nearest', c final_offset = transform_matrix[:2, 2] channel_images = [ ndi.interpolation.affine_transform( - x_channel, - final_affine_matrix, - final_offset, - order=order, - mode=fill_mode, - cval=cval + x_channel, final_affine_matrix, final_offset, order=order, mode=fill_mode, cval=cval ) for x_channel in x ] x = np.stack(channel_images, axis=0) @@ -1859,16 +1811,7 @@ def apply_transform(x, transform_matrix, channel_index=2, fill_mode='nearest', c def projective_transform_by_points( - x, - src, - dst, - map_args=None, - output_shape=None, - order=1, - mode='constant', - cval=0.0, - clip=True, - preserve_range=False + x, src, dst, map_args=None, output_shape=None, order=1, mode='constant', cval=0.0, clip=True, preserve_range=False ): """Projective transform by given coordinates, usually 4 coordinates. @@ -2019,10 +1962,7 @@ def find_contours(x, level=0.8, fully_connected='low', positive_orientation='low """ return skimage.measure.find_contours( - x, - level, - fully_connected=fully_connected, - positive_orientation=positive_orientation + x, level, fully_connected=fully_connected, positive_orientation=positive_orientation ) @@ -3128,9 +3068,7 @@ def pad_sequences(sequences, maxlen=None, dtype='int32', padding='post', truncat if trunc.shape[1:] != sample_shape: raise ValueError( 'Shape of sample %s of sequence at position %s is different from expected shape %s' % - (trunc.shape[1:], - idx, - sample_shape) + (trunc.shape[1:], idx, sample_shape) ) if padding == 'post': diff --git a/tensorlayer/utils.py b/tensorlayer/utils.py index 148c71fec..456062013 100644 --- a/tensorlayer/utils.py +++ b/tensorlayer/utils.py @@ -221,10 +221,7 @@ def fit( logging.info(" val acc: %f" % (val_acc / n_batch)) else: logging.info( - "Epoch %d of %d took %fs, loss %f" % (epoch + 1, - n_epoch, - time.time() - start_time, - loss_ep) + "Epoch %d of %d took %fs, loss %f" % (epoch + 1, n_epoch, time.time() - start_time, loss_ep) ) logging.info("Total training time: %fs" % (time.time() - start_time_begin)) diff --git a/tensorlayer/visualize.py b/tensorlayer/visualize.py index 98c611bc7..c459f7188 100644 --- a/tensorlayer/visualize.py +++ b/tensorlayer/visualize.py @@ -135,21 +135,14 @@ def imsave(images, size, path): return scipy.misc.imsave(path, merge(images, size)) assert len(images) <= size[0] * size[1 - ], "number of images should be equal or less than size[0] * size[1] {}".format( - len(images) - ) + ], "number of images should be equal or less than size[0] * size[1] {}".format( + len(images) + ) return imsave(images, size, image_path) def draw_boxes_and_labels_to_image( - image, - classes, - coords, - scores, - classes_list, - is_center=True, - is_rescale=True, - save_name=None + image, classes, coords, scores, classes_list, is_center=True, is_rescale=True, save_name=None ): """Draw bboxes and class labels on image. Return or save the image with bboxes, example in the docs of ``tl.prepro``. @@ -317,9 +310,7 @@ def draw_mpii_pose_to_image(image, poses, save_name='image.png'): [(12, 8), [255, 0, 255]], # right hand [(8, 13), [0, 255, 255]], [(13, 14), [100, 255, 255]], - [(14, 15), [200, - 255, - 255]] # left hand + [(14, 15), [200, 255, 255]] # left hand ] for line in lines: start, end = line[0] @@ -453,10 +444,7 @@ def CNN2d(CNN=None, second=10, saveable=True, name='cnn', fig_idx=3119362): plt.imshow(np.reshape(CNN[:, :, :, count - 1], (n_row, n_col)), cmap='gray', interpolation="nearest") elif n_color == 3: plt.imshow( - np.reshape(CNN[:, :, :, count - 1], - (n_row, n_col, n_color)), - cmap='gray', - interpolation="nearest" + np.reshape(CNN[:, :, :, count - 1], (n_row, n_col, n_color)), cmap='gray', interpolation="nearest" ) else: raise Exception("Unknown n_color") @@ -647,9 +635,7 @@ def draw_weights(W=None, second=10, saveable=True, shape=None, name='mnist', fig # if np.mean(feature) < -0.015: # condition threshold # feature = np.zeros_like(feature) plt.imshow( - np.reshape(feature, (shape[0], shape[1])), - cmap='gray', - interpolation="nearest" + np.reshape(feature, (shape[0], shape[1])), cmap='gray', interpolation="nearest" ) #, vmin=np.min(feature), vmax=np.max(feature)) # plt.title(name) # ------------------------------------------------------------ diff --git a/tests/test_layers_basic.py b/tests/test_layers_basic.py index c42952744..46fc9a434 100644 --- a/tests/test_layers_basic.py +++ b/tests/test_layers_basic.py @@ -7,6 +7,7 @@ class Layer_Basic_Test(unittest.TestCase): + @classmethod def setUpClass(cls): diff --git a/tests/test_layers_convolution.py b/tests/test_layers_convolution.py index cb6fe0b4a..d43a7f97c 100644 --- a/tests/test_layers_convolution.py +++ b/tests/test_layers_convolution.py @@ -7,6 +7,7 @@ class Layer_Convolution_Test(unittest.TestCase): + @classmethod def setUpClass(cls): ############ @@ -46,28 +47,13 @@ def setUpClass(cls): cls.n4_params = n4.all_params n5 = tl.layers.Conv2d( - nin2, - n_filter=32, - filter_size=(3, 3), - strides=(2, 2), - act=tf.nn.relu, - b_init=None, - name='conv2d_no_bias' + nin2, n_filter=32, filter_size=(3, 3), strides=(2, 2), act=tf.nn.relu, b_init=None, name='conv2d_no_bias' ) cls.shape_n5 = n5.outputs.get_shape().as_list() cls.n5_params = n5.all_params n6 = tl.layers.DeConv2dLayer( - nin2, shape=(5, - 5, - 32, - 3), output_shape=(100, - 200, - 200, - 32), strides=(1, - 2, - 2, - 1), name='deconv2dlayer' + nin2, shape=(5, 5, 32, 3), output_shape=(100, 200, 200, 32), strides=(1, 2, 2, 1), name='deconv2dlayer' ) cls.shape_n6 = n6.outputs.get_shape().as_list() @@ -75,12 +61,7 @@ def setUpClass(cls): cls.shape_n7 = n7.outputs.get_shape().as_list() n8 = tl.layers.DepthwiseConv2d( - nin2, - shape=(3, 3), - strides=(2, 2), - act=tf.nn.relu, - depth_multiplier=2, - name='depthwise' + nin2, shape=(3, 3), strides=(2, 2), act=tf.nn.relu, depth_multiplier=2, name='depthwise' ) cls.shape_n8 = n8.outputs.get_shape().as_list() @@ -89,12 +70,7 @@ def setUpClass(cls): cls.shape_n9 = n9.outputs.get_shape().as_list() n10 = tl.layers.SeparableConv2d( - nin2, - n_filter=32, - filter_size=(3, 3), - strides=(1, 1), - act=tf.nn.relu, - name='seperable1' + nin2, n_filter=32, filter_size=(3, 3), strides=(1, 1), act=tf.nn.relu, name='seperable1' ) cls.shape_n10 = n10.outputs.get_shape().as_list() cls.n10_all_layers = n10.all_layers diff --git a/tests/test_layers_core.py b/tests/test_layers_core.py index 8e90acd3a..320eaa457 100644 --- a/tests/test_layers_core.py +++ b/tests/test_layers_core.py @@ -7,6 +7,7 @@ class Layer_Core_Test(unittest.TestCase): + @classmethod def setUpClass(cls): @@ -62,7 +63,7 @@ def setUpClass(cls): # ============== EmbeddingInputlayer ============== - x4 = tf.placeholder(tf.int32, shape=(cls.batch_size, )) + x4 = tf.placeholder(tf.int32, shape=(cls.batch_size,)) net4 = tl.layers.EmbeddingInputlayer(inputs=x4, vocabulary_size=1000, embedding_size=50, name='embed') net4.print_layers() diff --git a/tests/test_layers_extend.py b/tests/test_layers_extend.py index 240612f87..6b8847744 100644 --- a/tests/test_layers_extend.py +++ b/tests/test_layers_extend.py @@ -7,6 +7,7 @@ class Layer_Extend_Test(unittest.TestCase): + @classmethod def setUpClass(cls): diff --git a/tests/test_layers_flow_control.py b/tests/test_layers_flow_control.py index 9ff900b1d..2cfce2790 100644 --- a/tests/test_layers_flow_control.py +++ b/tests/test_layers_flow_control.py @@ -7,6 +7,7 @@ class Layer_Flow_Control_Test(unittest.TestCase): + @classmethod def setUpClass(cls): x = tf.placeholder(tf.float32, shape=(None, 784), name='x') diff --git a/tests/test_layers_importer.py b/tests/test_layers_importer.py index 985a3211b..d998a4cf8 100644 --- a/tests/test_layers_importer.py +++ b/tests/test_layers_importer.py @@ -19,6 +19,7 @@ class Layer_Importer_Test(CustomTestCase): + @classmethod def setUpClass(cls): @@ -41,6 +42,7 @@ def tearDownClass(cls): tf.reset_default_graph() def test_lambda_layer(self): + def keras_block(x): x = k.layers.Dropout(0.8)(x) x = k.layers.Dense(100, activation='relu')(x) diff --git a/tests/test_layers_merge.py b/tests/test_layers_merge.py index 42c19607a..551bf9fc6 100644 --- a/tests/test_layers_merge.py +++ b/tests/test_layers_merge.py @@ -7,6 +7,7 @@ class Layer_Merge_Test(unittest.TestCase): + @classmethod def setUpClass(cls): diff --git a/tests/test_layers_normalization.py b/tests/test_layers_normalization.py index 4900f211c..79b3148d8 100644 --- a/tests/test_layers_normalization.py +++ b/tests/test_layers_normalization.py @@ -19,6 +19,7 @@ def model(x, is_train=True, reuse=False): class Layer_Normalization_Test(unittest.TestCase): + @classmethod def setUpClass(cls): diff --git a/tests/test_layers_padding.py b/tests/test_layers_padding.py index 984e388b7..29a8b0062 100644 --- a/tests/test_layers_padding.py +++ b/tests/test_layers_padding.py @@ -7,6 +7,7 @@ class Layer_Padding_Test(unittest.TestCase): + @classmethod def setUpClass(cls): diff --git a/tests/test_layers_pooling.py b/tests/test_layers_pooling.py index 2f987e7cc..c6560ea17 100644 --- a/tests/test_layers_pooling.py +++ b/tests/test_layers_pooling.py @@ -7,6 +7,7 @@ class Layer_Pooling_Test(unittest.TestCase): + @classmethod def setUpClass(cls): diff --git a/tests/test_layers_recurrent.py b/tests/test_layers_recurrent.py index 840ae11bb..17df26d26 100644 --- a/tests/test_layers_recurrent.py +++ b/tests/test_layers_recurrent.py @@ -7,6 +7,7 @@ class Layer_Recurrent_Test(unittest.TestCase): + @classmethod def setUpClass(cls): @@ -31,10 +32,7 @@ def setUpClass(cls): input_data = tf.placeholder(tf.int32, [cls.net1_batch_size, cls.num_steps]) net1 = tl.layers.EmbeddingInputlayer( - inputs=input_data, - vocabulary_size=cls.vocab_size, - embedding_size=cls.hidden_size, - name='embed' + inputs=input_data, vocabulary_size=cls.vocab_size, embedding_size=cls.hidden_size, name='embed' ) net1 = tl.layers.DropoutLayer(net1, keep=cls.keep_prob, is_fix=True, is_train=cls.is_train, name='drop1') net1 = tl.layers.RNNLayer( @@ -109,10 +107,7 @@ def setUpClass(cls): x3 = tf.placeholder(tf.int32, [cls.net3_batch_size, cls.num_steps]) net3 = tl.layers.EmbeddingInputlayer( - inputs=x3, - vocabulary_size=cls.vocab_size, - embedding_size=cls.hidden_size, - name='emb' + inputs=x3, vocabulary_size=cls.vocab_size, embedding_size=cls.hidden_size, name='emb' ) net3 = tl.layers.BiRNNLayer( net3, @@ -134,10 +129,7 @@ def setUpClass(cls): # n_layer=2 net4 = tl.layers.EmbeddingInputlayer( - inputs=x3, - vocabulary_size=cls.vocab_size, - embedding_size=cls.hidden_size, - name='emb2' + inputs=x3, vocabulary_size=cls.vocab_size, embedding_size=cls.hidden_size, name='emb2' ) net4 = tl.layers.BiRNNLayer( net4, @@ -179,10 +171,7 @@ def setUpClass(cls): input_seqs = tf.placeholder(dtype=tf.int64, shape=[cls.net5_batch_size, None], name="input") nin = tl.layers.EmbeddingInputlayer( - inputs=input_seqs, - vocabulary_size=cls.vocab_size, - embedding_size=cls.embedding_size, - name='seq_embedding' + inputs=input_seqs, vocabulary_size=cls.vocab_size, embedding_size=cls.embedding_size, name='seq_embedding' ) rnn = tl.layers.DynamicRNNLayer( @@ -209,10 +198,7 @@ def setUpClass(cls): # n_layer=3 nin = tl.layers.EmbeddingInputlayer( - inputs=input_seqs, - vocabulary_size=cls.vocab_size, - embedding_size=cls.embedding_size, - name='seq_embedding2' + inputs=input_seqs, vocabulary_size=cls.vocab_size, embedding_size=cls.embedding_size, name='seq_embedding2' ) rnn = tl.layers.DynamicRNNLayer( nin, @@ -343,18 +329,12 @@ def setUpClass(cls): with tf.variable_scope("embedding") as vs: net_encode = tl.layers.EmbeddingInputlayer( - inputs=encode_seqs, - vocabulary_size=10000, - embedding_size=200, - name='seq_embed' + inputs=encode_seqs, vocabulary_size=10000, embedding_size=200, name='seq_embed' ) vs.reuse_variables() # tl.layers.set_name_reuse(True) net_decode = tl.layers.EmbeddingInputlayer( - inputs=decode_seqs, - vocabulary_size=10000, - embedding_size=200, - name='seq_embed' + inputs=decode_seqs, vocabulary_size=10000, embedding_size=200, name='seq_embed' ) net11 = tl.layers.Seq2Seq( diff --git a/tests/test_layers_shape.py b/tests/test_layers_shape.py index 22eb87952..501946397 100644 --- a/tests/test_layers_shape.py +++ b/tests/test_layers_shape.py @@ -7,6 +7,7 @@ class Layer_Shape_Test(unittest.TestCase): + @classmethod def setUpClass(cls): diff --git a/tests/test_layers_spatial_transformer.py b/tests/test_layers_spatial_transformer.py index 835a620dc..b415b5a04 100644 --- a/tests/test_layers_spatial_transformer.py +++ b/tests/test_layers_spatial_transformer.py @@ -27,22 +27,10 @@ def model(x, is_train, reuse): s = n ## 3. Classifier n = tl.layers.Conv2d( - n, - n_filter=16, - filter_size=(3, 3), - strides=(2, 2), - act=tf.nn.relu, - padding='SAME', - name='conv1' + n, n_filter=16, filter_size=(3, 3), strides=(2, 2), act=tf.nn.relu, padding='SAME', name='conv1' ) n = tl.layers.Conv2d( - n, - n_filter=16, - filter_size=(3, 3), - strides=(2, 2), - act=tf.nn.relu, - padding='SAME', - name='conv2' + n, n_filter=16, filter_size=(3, 3), strides=(2, 2), act=tf.nn.relu, padding='SAME', name='conv2' ) n = tl.layers.FlattenLayer(n, name='flatten2') n = tl.layers.DenseLayer(n, n_units=1024, act=tf.nn.relu, name='out1') @@ -51,6 +39,7 @@ def model(x, is_train, reuse): class Layer_Spatial_Transformer_Test(CustomTestCase): + @classmethod def setUpClass(cls): cls.x = tf.placeholder(tf.float32, shape=[None, 28, 28, 1]) diff --git a/tests/test_layers_special_activation.py b/tests/test_layers_special_activation.py index 9a087c96c..7527dfb3e 100644 --- a/tests/test_layers_special_activation.py +++ b/tests/test_layers_special_activation.py @@ -7,6 +7,7 @@ class Layer_Special_Activation_Test(unittest.TestCase): + @classmethod def setUpClass(cls): diff --git a/tests/test_layers_stack.py b/tests/test_layers_stack.py index fe10113c1..5ff8cb784 100644 --- a/tests/test_layers_stack.py +++ b/tests/test_layers_stack.py @@ -7,6 +7,7 @@ class Layer_Stack_Test(unittest.TestCase): + @classmethod def setUpClass(cls): diff --git a/tests/test_layers_super_resolution.py b/tests/test_layers_super_resolution.py index b2427195d..71d2faba8 100644 --- a/tests/test_layers_super_resolution.py +++ b/tests/test_layers_super_resolution.py @@ -7,6 +7,7 @@ class Layer_Super_Resolution_Test(unittest.TestCase): + @classmethod def setUpClass(cls): t_signal = tf.placeholder('float32', [10, 100, 4], name='x') diff --git a/tests/test_layers_time_distributed.py b/tests/test_layers_time_distributed.py index 6ef6969e9..d2618c9ac 100644 --- a/tests/test_layers_time_distributed.py +++ b/tests/test_layers_time_distributed.py @@ -15,18 +15,16 @@ def model(x, is_train=True, reuse=False, name_scope="env1"): with tf.variable_scope(name_scope, reuse=reuse): net = tl.layers.InputLayer(x, name='input') net = tl.layers.TimeDistributedLayer( - net, - layer_class=tl.layers.DenseLayer, - args={ + net, layer_class=tl.layers.DenseLayer, args={ 'n_units': 50, 'name': 'dense' - }, - name='time_dense' + }, name='time_dense' ) return net class Layer_Time_Distributed_Test(CustomTestCase): + @classmethod def setUpClass(cls): diff --git a/tests/test_mnist_simple.py b/tests/test_mnist_simple.py index b1e51db30..a57e21f43 100644 --- a/tests/test_mnist_simple.py +++ b/tests/test_mnist_simple.py @@ -12,6 +12,7 @@ class Simple_MNIST_Test(CustomTestCase): + @classmethod def setUpClass(cls): @@ -88,13 +89,7 @@ def test_reuse_vgg(self): # evaluation tl.utils.test( - sess, self.network, self.acc, - X_test, - y_test, - self.x, - self.y_, - batch_size=None, - cost=self.cost + sess, self.network, self.acc, X_test, y_test, self.x, self.y_, batch_size=None, cost=self.cost ) # save the network to .npz file diff --git a/tests/test_models.py b/tests/test_models.py index 206e93807..a6dbb3a34 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -12,6 +12,7 @@ class VGG_Model_Test(CustomTestCase): + @classmethod def setUpClass(cls): diff --git a/tests/test_pydocstyle.py b/tests/test_pydocstyle.py index 0d6f0059b..bd2d6efe9 100644 --- a/tests/test_pydocstyle.py +++ b/tests/test_pydocstyle.py @@ -22,6 +22,7 @@ def lookup_error_params(code): class PyDOC_Style_Test(unittest.TestCase): + @classmethod def setUpClass(cls): diff --git a/tests/test_reuse_mlp.py b/tests/test_reuse_mlp.py index bb346eed8..7a2d9eab2 100644 --- a/tests/test_reuse_mlp.py +++ b/tests/test_reuse_mlp.py @@ -21,6 +21,7 @@ def mlp(x, is_train=True, reuse=False): class MLP_Reuse_Test(unittest.TestCase): + @classmethod def setUpClass(cls): diff --git a/tests/test_yapf_format.py b/tests/test_yapf_format.py index 2e4a47c3a..1eb6cc497 100644 --- a/tests/test_yapf_format.py +++ b/tests/test_yapf_format.py @@ -24,6 +24,7 @@ def _read_utf_8_file(filename): class YAPF_Style_Test(CustomTestCase): + @classmethod def setUpClass(cls): @@ -38,7 +39,6 @@ def test_files_format(self): code = _read_utf_8_file(file) # https://pypi.python.org/pypi/yapf/0.20.2#example-as-a-module - #diff, changed = FormatCode(code, filename=file, style_config='setup.cfg', print_diff=True) diff, changed = FormatCode(code, filename=file, style_config='.style.yapf', print_diff=True) if changed: @@ -51,7 +51,6 @@ def test_files_format(self): if self.badly_formatted_files: for filename in self.badly_formatted_files: - #str_err += 'yapf -i --style=setup.cfg %s\n' % filename str_err += 'yapf -i --style=.style.yapf %s\n' % filename str_err = "\n======================================================================================\n" \ diff --git a/tests/unittests_helper.py b/tests/unittests_helper.py index b4137c9b0..71e49ffdf 100644 --- a/tests/unittests_helper.py +++ b/tests/unittests_helper.py @@ -5,6 +5,7 @@ class CustomTestCase(unittest.TestCase): + @contextmanager def assertNotRaises(self, exc_type): try: From 4d75516a71fc80c0f0caedcbbe9e80ebc74b6315 Mon Sep 17 00:00:00 2001 From: DEKHTIARJonathan Date: Fri, 20 Apr 2018 20:35:40 +0200 Subject: [PATCH 18/22] Space Error Fix --- .style.yapf | 2 +- example/tutorial_mobilenet.py | 2 +- example/tutorial_squeezenet.py | 2 +- tensorlayer/iterate.py | 4 ++-- tensorlayer/layers/convolution.py | 2 +- tensorlayer/layers/special_activation.py | 2 +- tests/test_layers_core.py | 2 +- 7 files changed, 8 insertions(+), 8 deletions(-) diff --git a/.style.yapf b/.style.yapf index 672a9d6e7..ac93f855d 100644 --- a/.style.yapf +++ b/.style.yapf @@ -47,7 +47,7 @@ split_arguments_when_comma_terminated = False split_before_expression_after_opening_paren = False # Insert a space between the ending comma and closing bracket of a list, etc. -space_between_ending_comma_and_closing_bracket = False +space_between_ending_comma_and_closing_bracket = True # Allow splits before the dictionary value. allow_split_before_dict_value = True diff --git a/example/tutorial_mobilenet.py b/example/tutorial_mobilenet.py index 4b32dbaf4..3301cb620 100644 --- a/example/tutorial_mobilenet.py +++ b/example/tutorial_mobilenet.py @@ -55,7 +55,7 @@ def decode_predictions(preds, top=5): # keras.applications.resnet50 results = [] for pred in preds: top_indices = pred.argsort()[-top:][::-1] - result = [tuple(CLASS_INDEX[str(i)]) + (pred[i],) for i in top_indices] + result = [tuple(CLASS_INDEX[str(i)]) + (pred[i], ) for i in top_indices] result.sort(key=lambda x: x[2], reverse=True) results.append(result) return results diff --git a/example/tutorial_squeezenet.py b/example/tutorial_squeezenet.py index d60d8ae9b..238174633 100644 --- a/example/tutorial_squeezenet.py +++ b/example/tutorial_squeezenet.py @@ -32,7 +32,7 @@ def decode_predictions(preds, top=5): # keras.applications.resnet50 results = [] for pred in preds: top_indices = pred.argsort()[-top:][::-1] - result = [tuple(CLASS_INDEX[str(i)]) + (pred[i],) for i in top_indices] + result = [tuple(CLASS_INDEX[str(i)]) + (pred[i], ) for i in top_indices] result.sort(key=lambda x: x[2], reverse=True) results.append(result) return results diff --git a/tensorlayer/iterate.py b/tensorlayer/iterate.py index 67af7a322..76d756c3a 100644 --- a/tensorlayer/iterate.py +++ b/tensorlayer/iterate.py @@ -136,8 +136,8 @@ def seq_minibatches(inputs, targets, batch_size, seq_length, stride=1): end_seq_idx = start_seq_idx + seq_length seq_inputs[b_idx] = inputs[start_seq_idx:end_seq_idx] seq_targets[b_idx] = targets[start_seq_idx:end_seq_idx] - flatten_inputs = seq_inputs.reshape((-1,) + inputs.shape[1:]) - flatten_targets = seq_targets.reshape((-1,) + targets.shape[1:]) + flatten_inputs = seq_inputs.reshape((-1, ) + inputs.shape[1:]) + flatten_targets = seq_targets.reshape((-1, ) + targets.shape[1:]) yield flatten_inputs, flatten_targets diff --git a/tensorlayer/layers/convolution.py b/tensorlayer/layers/convolution.py index 386ff9cb1..3ac1c49fc 100644 --- a/tensorlayer/layers/convolution.py +++ b/tensorlayer/layers/convolution.py @@ -98,7 +98,7 @@ def __init__( name='W_conv1d', shape=shape, initializer=W_init, dtype=LayersConfig.tf_dtype, **W_init_args ) self.outputs = tf.nn.convolution( - self.inputs, W, strides=(stride,), padding=padding, dilation_rate=(dilation_rate,) + self.inputs, W, strides=(stride, ), padding=padding, dilation_rate=(dilation_rate, ) ) # 1.2 if b_init: b = tf.get_variable( diff --git a/tensorlayer/layers/special_activation.py b/tensorlayer/layers/special_activation.py index 80b74f138..cf83aeef4 100644 --- a/tensorlayer/layers/special_activation.py +++ b/tensorlayer/layers/special_activation.py @@ -55,7 +55,7 @@ def __init__( self.inputs = prev_layer.outputs if channel_shared: - w_shape = (1,) + w_shape = (1, ) else: w_shape = int(self.inputs.get_shape()[-1]) diff --git a/tests/test_layers_core.py b/tests/test_layers_core.py index 320eaa457..fab48d50a 100644 --- a/tests/test_layers_core.py +++ b/tests/test_layers_core.py @@ -63,7 +63,7 @@ def setUpClass(cls): # ============== EmbeddingInputlayer ============== - x4 = tf.placeholder(tf.int32, shape=(cls.batch_size,)) + x4 = tf.placeholder(tf.int32, shape=(cls.batch_size, )) net4 = tl.layers.EmbeddingInputlayer(inputs=x4, vocabulary_size=1000, embedding_size=50, name='embed') net4.print_layers() From 6092b6c21f7796002cf2541cc7b25474a50dfc6c Mon Sep 17 00:00:00 2001 From: DEKHTIARJonathan Date: Fri, 20 Apr 2018 20:48:40 +0200 Subject: [PATCH 19/22] Style Correction --- .style.yapf | 10 +- .../tutorial_binarynet_cifar10_tfrecord.py | 3 +- ...ial_bipedalwalker_a3c_continuous_action.py | 5 +- example/tutorial_cifar10.py | 7 +- example/tutorial_cifar10_tfrecord.py | 3 +- .../tutorial_dorefanet_cifar10_tfrecord.py | 3 +- example/tutorial_generate_text.py | 19 +- ...torial_imagenet_inceptionV3_distributed.py | 18 +- example/tutorial_imdb_fasttext.py | 6 +- example/tutorial_keras.py | 7 +- example/tutorial_mlp_dropout2.py | 7 +- example/tutorial_mnist.py | 33 +- example/tutorial_mnist_distributed.py | 17 +- example/tutorial_mnist_float16.py | 4 +- example/tutorial_mnist_simple.py | 17 +- ...tutorial_ternaryweight_cifar10_tfrecord.py | 3 +- example/tutorial_tfrecord2.py | 3 +- example/tutorial_tfrecord3.py | 24 +- example/tutorial_vgg19.py | 24 +- example/tutorial_word2vec_basic.py | 10 +- tensorlayer/cost.py | 6 +- tensorlayer/db.py | 2 +- tensorlayer/distributed.py | 54 +- tensorlayer/files.py | 30 +- tensorlayer/layers/binary.py | 268 ++++----- tensorlayer/layers/convolution.py | 534 +++++++----------- tensorlayer/layers/core.py | 203 +++---- tensorlayer/layers/extend.py | 8 +- tensorlayer/layers/importer.py | 40 +- tensorlayer/layers/merge.py | 18 +- tensorlayer/layers/normalization.py | 62 +- tensorlayer/layers/object_detection.py | 12 +- tensorlayer/layers/padding.py | 34 +- tensorlayer/layers/pooling.py | 36 +- tensorlayer/layers/recurrent.py | 217 ++++--- tensorlayer/layers/spatial_transformer.py | 10 +- tensorlayer/layers/special_activation.py | 14 +- tensorlayer/layers/stack.py | 8 +- tensorlayer/layers/time_distribution.py | 10 +- tensorlayer/models/mobilenetv1.py | 4 +- tensorlayer/models/squeezenetv1.py | 4 +- tensorlayer/nlp.py | 20 +- tensorlayer/prepro.py | 194 ++----- tensorlayer/utils.py | 27 +- tensorlayer/visualize.py | 7 +- tests/test_layers_convolution.py | 9 +- tests/test_layers_core.py | 6 +- tests/test_layers_recurrent.py | 143 ++--- tests/test_mnist_simple.py | 17 +- 49 files changed, 809 insertions(+), 1411 deletions(-) diff --git a/.style.yapf b/.style.yapf index ac93f855d..65bdef585 100644 --- a/.style.yapf +++ b/.style.yapf @@ -35,23 +35,17 @@ coalesce_brackets = False align_closing_bracket_with_visual_indent = False # Split named assignments onto individual lines. -split_before_named_assigns = True +split_before_named_assigns = False # If an argument / parameter list is going to be split, then split before the first argument. -split_before_first_argument = False +split_before_first_argument = True # Split before arguments if the argument list is terminated by a comma. split_arguments_when_comma_terminated = False -# Split after the opening paren which surrounds an expression if it doesn't fit on a single line. -split_before_expression_after_opening_paren = False - # Insert a space between the ending comma and closing bracket of a list, etc. space_between_ending_comma_and_closing_bracket = True -# Allow splits before the dictionary value. -allow_split_before_dict_value = True - # Join short lines into one line. E.g., single line if statements. join_multiple_lines = True diff --git a/example/tutorial_binarynet_cifar10_tfrecord.py b/example/tutorial_binarynet_cifar10_tfrecord.py index e23366775..dd5020150 100644 --- a/example/tutorial_binarynet_cifar10_tfrecord.py +++ b/example/tutorial_binarynet_cifar10_tfrecord.py @@ -96,8 +96,7 @@ def read_and_decode(filename, is_train=None): reader = tf.TFRecordReader() _, serialized_example = reader.read(filename_queue) features = tf.parse_single_example( - serialized_example, - features={ + serialized_example, features={ 'label': tf.FixedLenFeature([], tf.int64), 'img_raw': tf.FixedLenFeature([], tf.string), } diff --git a/example/tutorial_bipedalwalker_a3c_continuous_action.py b/example/tutorial_bipedalwalker_a3c_continuous_action.py index 23f5fa0f3..9c2aac04d 100644 --- a/example/tutorial_bipedalwalker_a3c_continuous_action.py +++ b/example/tutorial_bipedalwalker_a3c_continuous_action.py @@ -159,10 +159,7 @@ def choose_action(self, s): # run by a local def save_ckpt(self): tl.files.exists_or_mkdir(self.scope) tl.files.save_ckpt( - sess=sess, - mode_name='model.ckpt', - var_list=self.a_params + self.c_params, - save_dir=self.scope, + sess=sess, mode_name='model.ckpt', var_list=self.a_params + self.c_params, save_dir=self.scope, printable=True ) diff --git a/example/tutorial_cifar10.py b/example/tutorial_cifar10.py index bae25a679..8f1c5b80b 100644 --- a/example/tutorial_cifar10.py +++ b/example/tutorial_cifar10.py @@ -165,11 +165,8 @@ def distort_fn(x, is_train=False): batch_size = 128 train_params = network.all_params -train_op = tf.train.AdamOptimizer( - learning_rate, beta1=0.9, beta2=0.999, epsilon=1e-08, use_locking=False -).minimize( - cost, var_list=train_params -) +train_op = tf.train.AdamOptimizer(learning_rate, beta1=0.9, beta2=0.999, epsilon=1e-08, + use_locking=False).minimize(cost, var_list=train_params) tl.layers.initialize_global_variables(sess) diff --git a/example/tutorial_cifar10_tfrecord.py b/example/tutorial_cifar10_tfrecord.py index ef82ce4a3..62be4c5d9 100644 --- a/example/tutorial_cifar10_tfrecord.py +++ b/example/tutorial_cifar10_tfrecord.py @@ -97,8 +97,7 @@ def read_and_decode(filename, is_train=None): reader = tf.TFRecordReader() _, serialized_example = reader.read(filename_queue) features = tf.parse_single_example( - serialized_example, - features={ + serialized_example, features={ 'label': tf.FixedLenFeature([], tf.int64), 'img_raw': tf.FixedLenFeature([], tf.string), } diff --git a/example/tutorial_dorefanet_cifar10_tfrecord.py b/example/tutorial_dorefanet_cifar10_tfrecord.py index 113f4d40f..63b76d855 100644 --- a/example/tutorial_dorefanet_cifar10_tfrecord.py +++ b/example/tutorial_dorefanet_cifar10_tfrecord.py @@ -96,8 +96,7 @@ def read_and_decode(filename, is_train=None): reader = tf.TFRecordReader() _, serialized_example = reader.read(filename_queue) features = tf.parse_single_example( - serialized_example, - features={ + serialized_example, features={ 'label': tf.FixedLenFeature([], tf.int64), 'img_raw': tf.FixedLenFeature([], tf.string), } diff --git a/example/tutorial_generate_text.py b/example/tutorial_generate_text.py index bfab7d0ee..7ff5a5c7c 100644 --- a/example/tutorial_generate_text.py +++ b/example/tutorial_generate_text.py @@ -237,18 +237,11 @@ def inference(x, is_train, sequence_length, reuse=None): inputs=x, vocabulary_size=vocab_size, embedding_size=hidden_size, E_init=rnn_init, name='embedding' ) network = RNNLayer( - network, - cell_fn=tf.contrib.rnn.BasicLSTMCell, - cell_init_args={ + network, cell_fn=tf.contrib.rnn.BasicLSTMCell, cell_init_args={ 'forget_bias': 0.0, 'state_is_tuple': True - }, - n_hidden=hidden_size, - initializer=rnn_init, - n_steps=sequence_length, - return_last=False, - return_seq_2d=True, - name='lstm1' + }, n_hidden=hidden_size, initializer=rnn_init, n_steps=sequence_length, return_last=False, + return_seq_2d=True, name='lstm1' ) lstm1 = network network = DenseLayer( @@ -316,8 +309,7 @@ def loss_fn(outputs, targets, batch_size, sequence_length): state1 = tl.layers.initialize_rnn_state(lstm1.initial_state) for step, (x, y) in enumerate(tl.iterate.ptb_iterator(train_data, batch_size, sequence_length)): _cost, state1, _ = sess.run( - [cost, lstm1.final_state, train_op], - feed_dict={ + [cost, lstm1.final_state, train_op], feed_dict={ input_data: x, targets: y, lstm1.initial_state: state1, @@ -358,8 +350,7 @@ def loss_fn(outputs, targets, batch_size, sequence_length): for _ in range(print_length): a_id = np.asarray(a_id).reshape(1, 1) out, state1 = sess.run( - [y_soft, lstm1_test.final_state], - feed_dict={ + [y_soft, lstm1_test.final_state], feed_dict={ input_data_test: a_id, lstm1_test.initial_state: state1, } diff --git a/example/tutorial_imagenet_inceptionV3_distributed.py b/example/tutorial_imagenet_inceptionV3_distributed.py index 1009a767d..fc9495e74 100644 --- a/example/tutorial_imagenet_inceptionV3_distributed.py +++ b/example/tutorial_imagenet_inceptionV3_distributed.py @@ -160,13 +160,10 @@ def build_network(image_input, num_classes=1001, is_training=False): net_in = tl.layers.InputLayer(image_input, name='input_layer') with slim.arg_scope(inception_v3_arg_scope()): network = tl.layers.SlimNetsLayer( - prev_layer=net_in, - slim_layer=inception_v3, - slim_args={ + prev_layer=net_in, slim_layer=inception_v3, slim_args={ 'num_classes': num_classes, 'is_training': is_training - }, - name='InceptionV3' + }, name='InceptionV3' ) predictions = tf.nn.sigmoid(network.outputs, name='Predictions') @@ -380,14 +377,9 @@ def run_worker(task_spec, checkpoints_path, batch_size=32, epochs=10): # start training hooks = [StopAtStepHook(last_step=steps_per_epoch * epochs)] - with tl.distributed.DistributedSession( - task_spec=task_spec, - hooks=hooks, - checkpoint_dir=checkpoints_path, - save_summaries_secs=None, - save_summaries_steps=300, - save_checkpoint_secs=60 * 60 - ) as sess: + with tl.distributed.DistributedSession(task_spec=task_spec, hooks=hooks, checkpoint_dir=checkpoints_path, + save_summaries_secs=None, save_summaries_steps=300, + save_checkpoint_secs=60 * 60) as sess: # print network information if task_spec is None or task_spec.is_master(): network.print_params(False, session=sess) diff --git a/example/tutorial_imdb_fasttext.py b/example/tutorial_imdb_fasttext.py index 2cc2f4068..eecc6c825 100644 --- a/example/tutorial_imdb_fasttext.py +++ b/example/tutorial_imdb_fasttext.py @@ -136,8 +136,7 @@ def train_test_and_save_model(): print('Epoch %d/%d' % (epoch + 1, N_EPOCH)) for X_batch, y_batch in tl.iterate.minibatches(X_train, y_train, batch_size=BATCH_SIZE, shuffle=True): sess.run( - classifier.train_op, - feed_dict={ + classifier.train_op, feed_dict={ classifier.inputs: tl.prepro.pad_sequences(X_batch), classifier.labels: y_batch, } @@ -146,8 +145,7 @@ def train_test_and_save_model(): print(" took %.5fs" % (time.time() - start_time)) test_accuracy = sess.run( - classifier.accuracy, - feed_dict={ + classifier.accuracy, feed_dict={ classifier.inputs: tl.prepro.pad_sequences(X_test), classifier.labels: y_test, } diff --git a/example/tutorial_keras.py b/example/tutorial_keras.py index e62db3283..e3eefaee5 100644 --- a/example/tutorial_keras.py +++ b/example/tutorial_keras.py @@ -43,11 +43,8 @@ def keras_block(x): learning_rate = 0.0001 train_params = network.all_params -train_op = tf.train.AdamOptimizer( - learning_rate, beta1=0.9, beta2=0.999, epsilon=1e-08, use_locking=False -).minimize( - cost, var_list=train_params -) +train_op = tf.train.AdamOptimizer(learning_rate, beta1=0.9, beta2=0.999, epsilon=1e-08, + use_locking=False).minimize(cost, var_list=train_params) tl.layers.initialize_global_variables(sess) diff --git a/example/tutorial_mlp_dropout2.py b/example/tutorial_mlp_dropout2.py index 58817d754..0f70e2225 100644 --- a/example/tutorial_mlp_dropout2.py +++ b/example/tutorial_mlp_dropout2.py @@ -41,11 +41,8 @@ def mlp(x, is_train=True, reuse=False): # define the optimizer train_params = tl.layers.get_variables_with_name('MLP', train_only=True, printable=False) -train_op = tf.train.AdamOptimizer( - learning_rate=0.0001, beta1=0.9, beta2=0.999, epsilon=1e-08, use_locking=False -).minimize( - cost, var_list=train_params -) +train_op = tf.train.AdamOptimizer(learning_rate=0.0001, beta1=0.9, beta2=0.999, epsilon=1e-08, + use_locking=False).minimize(cost, var_list=train_params) # initialize all variables in the session tl.layers.initialize_global_variables(sess) diff --git a/example/tutorial_mnist.py b/example/tutorial_mnist.py index 43ad1fe75..174068074 100644 --- a/example/tutorial_mnist.py +++ b/example/tutorial_mnist.py @@ -193,16 +193,8 @@ def main_test_denoise_AE(model='relu'): ## pretrain print("Pre-train Layer 1") recon_layer1.pretrain( - sess, - x=x, - X_train=X_train, - X_val=X_val, - denoise_name='denoising1', - n_epoch=200, - batch_size=128, - print_freq=10, - save=True, - save_name='w1pre_' + sess, x=x, X_train=X_train, X_val=X_val, denoise_name='denoising1', n_epoch=200, batch_size=128, print_freq=10, + save=True, save_name='w1pre_' ) # You can also disable denoisong by setting denoise_name=None. # recon_layer1.pretrain(sess, x=x, X_train=X_train, X_val=X_val, @@ -274,27 +266,12 @@ def main_test_stacked_denoise_AE(model='relu'): network.print_params() print("\nPre-train Layer 1") recon_layer1.pretrain( - sess, - x=x, - X_train=X_train, - X_val=X_val, - denoise_name='denoising1', - n_epoch=100, - batch_size=128, - print_freq=10, - save=True, - save_name='w1pre_' + sess, x=x, X_train=X_train, X_val=X_val, denoise_name='denoising1', n_epoch=100, batch_size=128, print_freq=10, + save=True, save_name='w1pre_' ) print("\nPre-train Layer 2") recon_layer2.pretrain( - sess, - x=x, - X_train=X_train, - X_val=X_val, - denoise_name='denoising1', - n_epoch=100, - batch_size=128, - print_freq=10, + sess, x=x, X_train=X_train, X_val=X_val, denoise_name='denoising1', n_epoch=100, batch_size=128, print_freq=10, save=False ) print("\nAll Network Params after pre-train") diff --git a/example/tutorial_mnist_distributed.py b/example/tutorial_mnist_distributed.py index 3e1a6f607..d76f90daf 100644 --- a/example/tutorial_mnist_distributed.py +++ b/example/tutorial_mnist_distributed.py @@ -70,21 +70,8 @@ # train the network tl.utils.fit( - sess, - network, - train_op, - cost, - X_train, - y_train, - x, - y_, - acc=acc, - batch_size=500, - n_epoch=500, - print_freq=print_freq, - X_val=X_val, - y_val=y_val, - eval_train=eval_train + sess, network, train_op, cost, X_train, y_train, x, y_, acc=acc, batch_size=500, n_epoch=500, + print_freq=print_freq, X_val=X_val, y_val=y_val, eval_train=eval_train ) if task_spec.is_master(): diff --git a/example/tutorial_mnist_float16.py b/example/tutorial_mnist_float16.py index 97ea1cd31..b7b5d66cc 100644 --- a/example/tutorial_mnist_float16.py +++ b/example/tutorial_mnist_float16.py @@ -63,9 +63,7 @@ def model(x, is_train=True, reuse=False): # epsilon=1e-08, # for float32 as default epsilon=1e-4, # for float16, see https://stackoverflow.com/questions/42064941/tensorflow-float16-support-is-broken use_locking=False -).minimize( - cost, var_list=train_params -) +).minimize(cost, var_list=train_params) # initialize all variables in the session tl.layers.initialize_global_variables(sess) diff --git a/example/tutorial_mnist_simple.py b/example/tutorial_mnist_simple.py index d1f25b243..457774b68 100644 --- a/example/tutorial_mnist_simple.py +++ b/example/tutorial_mnist_simple.py @@ -45,21 +45,8 @@ # train the network tl.utils.fit( - sess, - network, - train_op, - cost, - X_train, - y_train, - x, - y_, - acc=acc, - batch_size=500, - n_epoch=500, - print_freq=5, - X_val=X_val, - y_val=y_val, - eval_train=False + sess, network, train_op, cost, X_train, y_train, x, y_, acc=acc, batch_size=500, n_epoch=500, print_freq=5, + X_val=X_val, y_val=y_val, eval_train=False ) # evaluation diff --git a/example/tutorial_ternaryweight_cifar10_tfrecord.py b/example/tutorial_ternaryweight_cifar10_tfrecord.py index 4db31c723..1f6f30357 100644 --- a/example/tutorial_ternaryweight_cifar10_tfrecord.py +++ b/example/tutorial_ternaryweight_cifar10_tfrecord.py @@ -95,8 +95,7 @@ def read_and_decode(filename, is_train=None): reader = tf.TFRecordReader() _, serialized_example = reader.read(filename_queue) features = tf.parse_single_example( - serialized_example, - features={ + serialized_example, features={ 'label': tf.FixedLenFeature([], tf.int64), 'img_raw': tf.FixedLenFeature([], tf.string), } diff --git a/example/tutorial_tfrecord2.py b/example/tutorial_tfrecord2.py index e7c410695..eb210d9da 100755 --- a/example/tutorial_tfrecord2.py +++ b/example/tutorial_tfrecord2.py @@ -65,8 +65,7 @@ def read_and_decode(filename): reader = tf.TFRecordReader() _, serialized_example = reader.read(filename_queue) features = tf.parse_single_example( - serialized_example, - features={ + serialized_example, features={ 'label': tf.FixedLenFeature([], tf.int64), 'img_raw': tf.FixedLenFeature([], tf.string), } diff --git a/example/tutorial_tfrecord3.py b/example/tutorial_tfrecord3.py index 2d5f7137e..6626476ec 100644 --- a/example/tutorial_tfrecord3.py +++ b/example/tutorial_tfrecord3.py @@ -105,11 +105,9 @@ def _bytes_feature_list(values): _, serialized_example = reader.read(filename_queue) # return the file and the name of file # features, sequence_features = tf.parse_single_example(serialized_example, # see parse_single_sequence_example for sequence example features, sequence_features = tf.parse_single_sequence_example( - serialized_example, - context_features={ + serialized_example, context_features={ 'image/img_raw': tf.FixedLenFeature([], tf.string), - }, - sequence_features={ + }, sequence_features={ "image/caption": tf.FixedLenSequenceFeature([], dtype=tf.string), "image/caption_ids": tf.FixedLenSequenceFeature([], dtype=tf.int64), } @@ -233,15 +231,8 @@ def distort_image(image, thread_id): def prefetch_input_data( - reader, - file_pattern, - is_training, - batch_size, - values_per_shard, - input_queue_capacity_factor=16, - num_reader_threads=1, - shard_queue_name="filename_queue", - value_queue_name="input_queue" + reader, file_pattern, is_training, batch_size, values_per_shard, input_queue_capacity_factor=16, + num_reader_threads=1, shard_queue_name="filename_queue", value_queue_name="input_queue" ): """Prefetches string values from disk into an input queue. @@ -281,9 +272,7 @@ def prefetch_input_data( min_queue_examples = values_per_shard * input_queue_capacity_factor capacity = min_queue_examples + 100 * batch_size values_queue = tf.RandomShuffleQueue( - capacity=capacity, - min_after_dequeue=min_queue_examples, - dtypes=[tf.string], + capacity=capacity, min_after_dequeue=min_queue_examples, dtypes=[tf.string], name="random_" + value_queue_name ) else: @@ -323,8 +312,7 @@ def prefetch_input_data( serialized_sequence_example = input_queue.dequeue() # serialized_sequence_example = tf.train.string_input_producer(["train.cat_caption"]) # don't work context, sequence = tf.parse_single_sequence_example( - serialized=serialized_sequence_example, - context_features={"image/img_raw": tf.FixedLenFeature([], dtype=tf.string)}, + serialized=serialized_sequence_example, context_features={"image/img_raw": tf.FixedLenFeature([], dtype=tf.string)}, sequence_features={ "image/caption": tf.FixedLenSequenceFeature([], dtype=tf.string), "image/caption_ids": tf.FixedLenSequenceFeature([], dtype=tf.int64), diff --git a/example/tutorial_vgg19.py b/example/tutorial_vgg19.py index f1685a6ed..f484f3dc8 100755 --- a/example/tutorial_vgg19.py +++ b/example/tutorial_vgg19.py @@ -94,13 +94,11 @@ def Vgg19(rgb): red - VGG_MEAN[2], ]) else: - bgr = tf.concat( - [ - blue - VGG_MEAN[0], - green - VGG_MEAN[1], - red - VGG_MEAN[2], - ], axis=3 - ) + bgr = tf.concat([ + blue - VGG_MEAN[0], + green - VGG_MEAN[1], + red - VGG_MEAN[2], + ], axis=3) if bgr.get_shape().as_list()[1:] != [224, 224, 3]: raise Exception("image size unmatch") # input layer @@ -212,13 +210,11 @@ def Vgg19_simple_api(rgb): red - VGG_MEAN[2], ]) else: - bgr = tf.concat( - [ - blue - VGG_MEAN[0], - green - VGG_MEAN[1], - red - VGG_MEAN[2], - ], axis=3 - ) + bgr = tf.concat([ + blue - VGG_MEAN[0], + green - VGG_MEAN[1], + red - VGG_MEAN[2], + ], axis=3) if bgr.get_shape().as_list()[1:] != [224, 224, 3]: raise Exception("image size unmatch") # input layer diff --git a/example/tutorial_word2vec_basic.py b/example/tutorial_word2vec_basic.py index 360ea7dfc..ad275c0ac 100644 --- a/example/tutorial_word2vec_basic.py +++ b/example/tutorial_word2vec_basic.py @@ -200,11 +200,8 @@ def main_word2vec_basic(): cost = emb_net.nce_cost train_params = emb_net.all_params # train_op = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost, var_list=train_params) - train_op = tf.train.AdagradOptimizer( - learning_rate, initial_accumulator_value=0.1, use_locking=False - ).minimize( - cost, var_list=train_params - ) + train_op = tf.train.AdagradOptimizer(learning_rate, initial_accumulator_value=0.1, + use_locking=False).minimize(cost, var_list=train_params) # Compute the cosine similarity between minibatch examples and all embeddings. # For simple visualization of validation set. @@ -280,8 +277,7 @@ def main_word2vec_basic(): 'count': count, 'dictionary': dictionary, 'reverse_dictionary': reverse_dictionary - }, - name=model_file_name + '.npy' + }, name=model_file_name + '.npy' ) # if step == num_steps-1: diff --git a/tensorlayer/cost.py b/tensorlayer/cost.py index c30752b39..01ba38d94 100644 --- a/tensorlayer/cost.py +++ b/tensorlayer/cost.py @@ -550,8 +550,7 @@ def li(weights): # else: standard_ops_fn = standard_ops.multiply return standard_ops_fn( - my_scale, - standard_ops.reduce_sum(standard_ops.sqrt(standard_ops.reduce_sum(tf.square(weights), 1))), + my_scale, standard_ops.reduce_sum(standard_ops.sqrt(standard_ops.reduce_sum(tf.square(weights), 1))), name=scope ) @@ -602,8 +601,7 @@ def lo(weights, name='lo_regularizer'): # else: standard_ops_fn = standard_ops.multiply return standard_ops_fn( - my_scale, - standard_ops.reduce_sum(standard_ops.sqrt(standard_ops.reduce_sum(tf.square(weights), 0))), + my_scale, standard_ops.reduce_sum(standard_ops.sqrt(standard_ops.reduce_sum(tf.square(weights), 0))), name=scope ) diff --git a/tensorlayer/db.py b/tensorlayer/db.py index 2db3ab9fa..27749459d 100644 --- a/tensorlayer/db.py +++ b/tensorlayer/db.py @@ -61,7 +61,7 @@ class TensorDB(object): """ def __init__( - self, ip='localhost', port=27017, db_name='db_name', user_name=None, password='password', studyID=None + self, ip='localhost', port=27017, db_name='db_name', user_name=None, password='password', studyID=None ): ## connect mongodb client = MongoClient(ip, port) diff --git a/tensorlayer/distributed.py b/tensorlayer/distributed.py index 88b0d7b69..d808e50d2 100644 --- a/tensorlayer/distributed.py +++ b/tensorlayer/distributed.py @@ -141,12 +141,8 @@ def use_last_worker_as_evaluator(self): raise Exception('You need more than one worker instance to use one as evaluator') return TaskSpecDef( - task_type=self.type, - index=self._index, - trial=self.trial, - ps_hosts=self.ps_hosts, - worker_hosts=self.worker_hosts[:-1], - master=self.master + task_type=self.type, index=self._index, trial=self.trial, ps_hosts=self.ps_hosts, + worker_hosts=self.worker_hosts[:-1], master=self.master ) @@ -165,38 +161,24 @@ def create_task_spec_def(): task_data = env.get('task', None) or {'type': 'master', 'index': 0} cluster_data = env.get('cluster', None) or {'ps': None, 'worker': None, 'master': None} return TaskSpecDef( - task_type=task_data['type'], - index=task_data['index'], - trial=task_data['trial'] if 'trial' in task_data else None, - ps_hosts=cluster_data['ps'], - worker_hosts=cluster_data['worker'], - master=cluster_data['master'] if 'master' in cluster_data else None + task_type=task_data['type'], index=task_data['index'], trial=task_data['trial'] if 'trial' in task_data else + None, ps_hosts=cluster_data['ps'], worker_hosts=cluster_data['worker'], master=cluster_data['master'] + if 'master' in cluster_data else None ) elif 'JOB_NAME' in os.environ: # JOB_NAME, TASK_INDEX, PS_HOSTS, WORKER_HOSTS and MASTER_HOST are used in TensorPort return TaskSpecDef( - task_type=os.environ['JOB_NAME'], - index=os.environ['TASK_INDEX'], - ps_hosts=os.environ.get('PS_HOSTS', None), - worker_hosts=os.environ.get('WORKER_HOSTS', None), - master=os.environ.get('MASTER_HOST', None) + task_type=os.environ['JOB_NAME'], index=os.environ['TASK_INDEX'], ps_hosts=os.environ.get('PS_HOSTS', None), + worker_hosts=os.environ.get('WORKER_HOSTS', None), master=os.environ.get('MASTER_HOST', None) ) else: raise Exception('You need to setup TF_CONFIG or JOB_NAME to define the task.') def create_distributed_session( - task_spec=None, - checkpoint_dir=None, - scaffold=None, - hooks=None, - chief_only_hooks=None, - save_checkpoint_secs=600, - save_summaries_steps=object(), - save_summaries_secs=object(), - config=None, - stop_grace_period_secs=120, - log_step_count_steps=100 + task_spec=None, checkpoint_dir=None, scaffold=None, hooks=None, chief_only_hooks=None, save_checkpoint_secs=600, + save_summaries_steps=object(), save_summaries_secs=object(), config=None, stop_grace_period_secs=120, + log_step_count_steps=100 ): """Creates a distributed session. @@ -282,18 +264,10 @@ def create_distributed_session( target = task_spec.target() if task_spec is not None else None is_chief = task_spec.is_master() if task_spec is not None else True return tf.train.MonitoredTrainingSession( - master=target, - is_chief=is_chief, - checkpoint_dir=checkpoint_dir, - scaffold=scaffold, - save_checkpoint_secs=save_checkpoint_secs, - save_summaries_steps=save_summaries_steps, - save_summaries_secs=save_summaries_secs, - log_step_count_steps=log_step_count_steps, - stop_grace_period_secs=stop_grace_period_secs, - config=config, - hooks=hooks, - chief_only_hooks=chief_only_hooks + master=target, is_chief=is_chief, checkpoint_dir=checkpoint_dir, scaffold=scaffold, + save_checkpoint_secs=save_checkpoint_secs, save_summaries_steps=save_summaries_steps, + save_summaries_secs=save_summaries_secs, log_step_count_steps=log_step_count_steps, + stop_grace_period_secs=stop_grace_period_secs, config=config, hooks=hooks, chief_only_hooks=chief_only_hooks ) diff --git a/tensorlayer/files.py b/tensorlayer/files.py index 3f911027c..25c38dcf4 100644 --- a/tensorlayer/files.py +++ b/tensorlayer/files.py @@ -496,15 +496,8 @@ def load_matt_mahoney_text8_dataset(path='data'): def load_imdb_dataset( - path='data', - nb_words=None, - skip_top=0, - maxlen=None, - test_split=0.2, - seed=113, - start_char=1, - oov_char=2, - index_from=3 + path='data', nb_words=None, skip_top=0, maxlen=None, test_split=0.2, seed=113, start_char=1, oov_char=2, + index_from=3 ): """Load IMDB dataset. @@ -946,9 +939,8 @@ def download_file_from_google_drive(ID, destination): def save_response_content(response, destination, chunk_size=32 * 1024): total_size = int(response.headers.get('content-length', 0)) with open(destination, "wb") as f: - for chunk in tqdm( - response.iter_content(chunk_size), total=total_size, unit='B', unit_scale=True, desc=destination - ): + for chunk in tqdm(response.iter_content(chunk_size), total=total_size, unit='B', unit_scale=True, + desc=destination): if chunk: # filter out keep-alive new chunks f.write(chunk) @@ -1404,11 +1396,8 @@ def save_joints(): # fp = open(joint_data_fn, 'w') mat = sio.loadmat(os.path.join(path, extracted_filename, "mpii_human_pose_v1_u12_1.mat")) - for _, ( - anno, train_flag - ) in enumerate( # all images - zip(mat['RELEASE']['annolist'][0, 0][0], mat['RELEASE']['img_train'][0, 0][0]) - ): + for _, (anno, train_flag) in enumerate( # all images + zip(mat['RELEASE']['annolist'][0, 0][0], mat['RELEASE']['img_train'][0, 0][0])): img_fn = anno['image']['name'][0, 0][0] train_flag = int(train_flag) @@ -1438,9 +1427,8 @@ def save_joints(): head_x2s = anno['annorect']['x2'][0] head_y2s = anno['annorect']['y2'][0] - for annopoint, head_x1, head_y1, head_x2, head_y2 in zip( - annopoints, head_x1s, head_y1s, head_x2s, head_y2s - ): + for annopoint, head_x1, head_y1, head_x2, head_y2 in zip(annopoints, head_x1s, head_y1s, head_x2s, + head_y2s): # if annopoint != []: # if len(annopoint) != 0: if annopoint.size: @@ -1794,7 +1782,7 @@ def load_and_assign_npz_dict(name='model.npz', sess=None): def save_ckpt( - sess=None, mode_name='model.ckpt', save_dir='checkpoint', var_list=None, global_step=None, printable=False + sess=None, mode_name='model.ckpt', save_dir='checkpoint', var_list=None, global_step=None, printable=False ): """Save parameters into `ckpt` file. diff --git a/tensorlayer/layers/binary.py b/tensorlayer/layers/binary.py index 6c846f3cc..ec22a5c5a 100644 --- a/tensorlayer/layers/binary.py +++ b/tensorlayer/layers/binary.py @@ -131,16 +131,16 @@ class BinaryDenseLayer(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - n_units=100, - act=tf.identity, - use_gemm=False, - W_init=tf.truncated_normal_initializer(stddev=0.1), - b_init=tf.constant_initializer(value=0.0), - W_init_args=None, - b_init_args=None, - name='binary_dense', + self, + prev_layer, + n_units=100, + act=tf.identity, + use_gemm=False, + W_init=tf.truncated_normal_initializer(stddev=0.1), + b_init=tf.constant_initializer(value=0.0), + W_init_args=None, + b_init_args=None, + name='binary_dense', ): super(BinaryDenseLayer, self).__init__(prev_layer=prev_layer, name=name) logging.info("BinaryDenseLayer %s: %d %s" % (name, n_units, act.__name__)) @@ -243,31 +243,31 @@ class BinaryConv2d(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - n_filter=32, - filter_size=(3, 3), - strides=(1, 1), - act=tf.identity, - padding='SAME', - use_gemm=False, - W_init=tf.truncated_normal_initializer(stddev=0.02), - b_init=tf.constant_initializer(value=0.0), - W_init_args=None, - b_init_args=None, - use_cudnn_on_gpu=None, - data_format=None, - # act=tf.identity, - # shape=(5, 5, 1, 100), - # strides=(1, 1, 1, 1), - # padding='SAME', - # W_init=tf.truncated_normal_initializer(stddev=0.02), - # b_init=tf.constant_initializer(value=0.0), - # W_init_args=None, - # b_init_args=None, - # use_cudnn_on_gpu=None, - # data_format=None, - name='binary_cnn2d', + self, + prev_layer, + n_filter=32, + filter_size=(3, 3), + strides=(1, 1), + act=tf.identity, + padding='SAME', + use_gemm=False, + W_init=tf.truncated_normal_initializer(stddev=0.02), + b_init=tf.constant_initializer(value=0.0), + W_init_args=None, + b_init_args=None, + use_cudnn_on_gpu=None, + data_format=None, + # act=tf.identity, + # shape=(5, 5, 1, 100), + # strides=(1, 1, 1, 1), + # padding='SAME', + # W_init=tf.truncated_normal_initializer(stddev=0.02), + # b_init=tf.constant_initializer(value=0.0), + # W_init_args=None, + # b_init_args=None, + # use_cudnn_on_gpu=None, + # data_format=None, + name='binary_cnn2d', ): super(BinaryConv2d, self).__init__(prev_layer=prev_layer, name=name) logging.info( @@ -306,22 +306,14 @@ def __init__( ) self.outputs = act( tf.nn.conv2d( - self.inputs, - W, - strides=strides, - padding=padding, - use_cudnn_on_gpu=use_cudnn_on_gpu, + self.inputs, W, strides=strides, padding=padding, use_cudnn_on_gpu=use_cudnn_on_gpu, data_format=data_format ) + b ) else: self.outputs = act( tf.nn.conv2d( - self.inputs, - W, - strides=strides, - padding=padding, - use_cudnn_on_gpu=use_cudnn_on_gpu, + self.inputs, W, strides=strides, padding=padding, use_cudnn_on_gpu=use_cudnn_on_gpu, data_format=data_format ) ) @@ -363,16 +355,16 @@ class TernaryDenseLayer(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - n_units=100, - act=tf.identity, - use_gemm=False, - W_init=tf.truncated_normal_initializer(stddev=0.1), - b_init=tf.constant_initializer(value=0.0), - W_init_args=None, - b_init_args=None, - name='ternary_dense', + self, + prev_layer, + n_units=100, + act=tf.identity, + use_gemm=False, + W_init=tf.truncated_normal_initializer(stddev=0.1), + b_init=tf.constant_initializer(value=0.0), + W_init_args=None, + b_init_args=None, + name='ternary_dense', ): super(TernaryDenseLayer, self).__init__(prev_layer=prev_layer, name=name) logging.info("TernaryDenseLayer %s: %d %s" % (name, n_units, act.__name__)) @@ -476,31 +468,31 @@ class TernaryConv2d(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - n_filter=32, - filter_size=(3, 3), - strides=(1, 1), - act=tf.identity, - padding='SAME', - use_gemm=False, - W_init=tf.truncated_normal_initializer(stddev=0.02), - b_init=tf.constant_initializer(value=0.0), - W_init_args=None, - b_init_args=None, - use_cudnn_on_gpu=None, - data_format=None, - # act=tf.identity, - # shape=(5, 5, 1, 100), - # strides=(1, 1, 1, 1), - # padding='SAME', - # W_init=tf.truncated_normal_initializer(stddev=0.02), - # b_init=tf.constant_initializer(value=0.0), - # W_init_args=None, - # b_init_args=None, - # use_cudnn_on_gpu=None, - # data_format=None, - name='ternary_cnn2d', + self, + prev_layer, + n_filter=32, + filter_size=(3, 3), + strides=(1, 1), + act=tf.identity, + padding='SAME', + use_gemm=False, + W_init=tf.truncated_normal_initializer(stddev=0.02), + b_init=tf.constant_initializer(value=0.0), + W_init_args=None, + b_init_args=None, + use_cudnn_on_gpu=None, + data_format=None, + # act=tf.identity, + # shape=(5, 5, 1, 100), + # strides=(1, 1, 1, 1), + # padding='SAME', + # W_init=tf.truncated_normal_initializer(stddev=0.02), + # b_init=tf.constant_initializer(value=0.0), + # W_init_args=None, + # b_init_args=None, + # use_cudnn_on_gpu=None, + # data_format=None, + name='ternary_cnn2d', ): super(TernaryConv2d, self).__init__(prev_layer=prev_layer, name=name) logging.info( @@ -539,22 +531,14 @@ def __init__( ) self.outputs = act( tf.nn.conv2d( - self.inputs, - W, - strides=strides, - padding=padding, - use_cudnn_on_gpu=use_cudnn_on_gpu, + self.inputs, W, strides=strides, padding=padding, use_cudnn_on_gpu=use_cudnn_on_gpu, data_format=data_format ) + b ) else: self.outputs = act( tf.nn.conv2d( - self.inputs, - W, - strides=strides, - padding=padding, - use_cudnn_on_gpu=use_cudnn_on_gpu, + self.inputs, W, strides=strides, padding=padding, use_cudnn_on_gpu=use_cudnn_on_gpu, data_format=data_format ) ) @@ -601,18 +585,18 @@ class DorefaDenseLayer(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - bitW=1, - bitA=3, - n_units=100, - act=tf.identity, - use_gemm=False, - W_init=tf.truncated_normal_initializer(stddev=0.1), - b_init=tf.constant_initializer(value=0.0), - W_init_args=None, - b_init_args=None, - name='dorefa_dense', + self, + prev_layer, + bitW=1, + bitA=3, + n_units=100, + act=tf.identity, + use_gemm=False, + W_init=tf.truncated_normal_initializer(stddev=0.1), + b_init=tf.constant_initializer(value=0.0), + W_init_args=None, + b_init_args=None, + name='dorefa_dense', ): super(DorefaDenseLayer, self).__init__(prev_layer=prev_layer, name=name) logging.info("DorefaDenseLayer %s: %d %s" % (name, n_units, act.__name__)) @@ -719,33 +703,33 @@ class DorefaConv2d(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - bitW=1, - bitA=3, - n_filter=32, - filter_size=(3, 3), - strides=(1, 1), - act=tf.identity, - padding='SAME', - use_gemm=False, - W_init=tf.truncated_normal_initializer(stddev=0.02), - b_init=tf.constant_initializer(value=0.0), - W_init_args=None, - b_init_args=None, - use_cudnn_on_gpu=None, - data_format=None, - # act=tf.identity, - # shape=(5, 5, 1, 100), - # strides=(1, 1, 1, 1), - # padding='SAME', - # W_init=tf.truncated_normal_initializer(stddev=0.02), - # b_init=tf.constant_initializer(value=0.0), - # W_init_args=None, - # b_init_args=None, - # use_cudnn_on_gpu=None, - # data_format=None, - name='dorefa_cnn2d', + self, + prev_layer, + bitW=1, + bitA=3, + n_filter=32, + filter_size=(3, 3), + strides=(1, 1), + act=tf.identity, + padding='SAME', + use_gemm=False, + W_init=tf.truncated_normal_initializer(stddev=0.02), + b_init=tf.constant_initializer(value=0.0), + W_init_args=None, + b_init_args=None, + use_cudnn_on_gpu=None, + data_format=None, + # act=tf.identity, + # shape=(5, 5, 1, 100), + # strides=(1, 1, 1, 1), + # padding='SAME', + # W_init=tf.truncated_normal_initializer(stddev=0.02), + # b_init=tf.constant_initializer(value=0.0), + # W_init_args=None, + # b_init_args=None, + # use_cudnn_on_gpu=None, + # data_format=None, + name='dorefa_cnn2d', ): super(DorefaConv2d, self).__init__(prev_layer=prev_layer, name=name) logging.info( @@ -786,22 +770,14 @@ def __init__( ) self.outputs = act( tf.nn.conv2d( - self.inputs, - W, - strides=strides, - padding=padding, - use_cudnn_on_gpu=use_cudnn_on_gpu, + self.inputs, W, strides=strides, padding=padding, use_cudnn_on_gpu=use_cudnn_on_gpu, data_format=data_format ) + b ) else: self.outputs = act( tf.nn.conv2d( - self.inputs, - W, - strides=strides, - padding=padding, - use_cudnn_on_gpu=use_cudnn_on_gpu, + self.inputs, W, strides=strides, padding=padding, use_cudnn_on_gpu=use_cudnn_on_gpu, data_format=data_format ) ) @@ -827,9 +803,9 @@ class SignLayer(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - name='sign', + self, + prev_layer, + name='sign', ): super(SignLayer, self).__init__(prev_layer=prev_layer, name=name) @@ -860,10 +836,10 @@ class ScaleLayer(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - init_scale=0.05, - name='scale', + self, + prev_layer, + init_scale=0.05, + name='scale', ): super(ScaleLayer, self).__init__(prev_layer=prev_layer, name=name) logging.info("ScaleLayer %s: init_scale: %f" % (name, init_scale)) diff --git a/tensorlayer/layers/convolution.py b/tensorlayer/layers/convolution.py index 3ac1c49fc..0d47f861b 100644 --- a/tensorlayer/layers/convolution.py +++ b/tensorlayer/layers/convolution.py @@ -64,19 +64,19 @@ class Conv1dLayer(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - act=tf.identity, - shape=(5, 1, 5), - stride=1, - dilation_rate=1, - padding='SAME', - data_format='NWC', - W_init=tf.truncated_normal_initializer(stddev=0.02), - b_init=tf.constant_initializer(value=0.0), - W_init_args=None, - b_init_args=None, - name='cnn1d', + self, + prev_layer, + act=tf.identity, + shape=(5, 1, 5), + stride=1, + dilation_rate=1, + padding='SAME', + data_format='NWC', + W_init=tf.truncated_normal_initializer(stddev=0.02), + b_init=tf.constant_initializer(value=0.0), + W_init_args=None, + b_init_args=None, + name='cnn1d', ): super(Conv1dLayer, self).__init__(prev_layer=prev_layer, name=name) logging.info( @@ -185,19 +185,19 @@ class Conv2dLayer(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - act=tf.identity, - shape=(5, 5, 1, 100), - strides=(1, 1, 1, 1), - padding='SAME', - W_init=tf.truncated_normal_initializer(stddev=0.02), - b_init=tf.constant_initializer(value=0.0), - W_init_args=None, - b_init_args=None, - use_cudnn_on_gpu=None, - data_format=None, - name='cnn_layer', + self, + prev_layer, + act=tf.identity, + shape=(5, 5, 1, 100), + strides=(1, 1, 1, 1), + padding='SAME', + W_init=tf.truncated_normal_initializer(stddev=0.02), + b_init=tf.constant_initializer(value=0.0), + W_init_args=None, + b_init_args=None, + use_cudnn_on_gpu=None, + data_format=None, + name='cnn_layer', ): super(Conv2dLayer, self).__init__(prev_layer=prev_layer, name=name) logging.info( @@ -224,22 +224,14 @@ def __init__( ) self.outputs = act( tf.nn.conv2d( - self.inputs, - W, - strides=strides, - padding=padding, - use_cudnn_on_gpu=use_cudnn_on_gpu, + self.inputs, W, strides=strides, padding=padding, use_cudnn_on_gpu=use_cudnn_on_gpu, data_format=data_format ) + b ) else: self.outputs = act( tf.nn.conv2d( - self.inputs, - W, - strides=strides, - padding=padding, - use_cudnn_on_gpu=use_cudnn_on_gpu, + self.inputs, W, strides=strides, padding=padding, use_cudnn_on_gpu=use_cudnn_on_gpu, data_format=data_format ) ) @@ -330,18 +322,18 @@ class DeConv2dLayer(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - act=tf.identity, - shape=(3, 3, 128, 256), - output_shape=(1, 256, 256, 128), - strides=(1, 2, 2, 1), - padding='SAME', - W_init=tf.truncated_normal_initializer(stddev=0.02), - b_init=tf.constant_initializer(value=0.0), - W_init_args=None, - b_init_args=None, - name='decnn2d_layer', + self, + prev_layer, + act=tf.identity, + shape=(3, 3, 128, 256), + output_shape=(1, 256, 256, 128), + strides=(1, 2, 2, 1), + padding='SAME', + W_init=tf.truncated_normal_initializer(stddev=0.02), + b_init=tf.constant_initializer(value=0.0), + W_init_args=None, + b_init_args=None, + name='decnn2d_layer', ): super(DeConv2dLayer, self).__init__(prev_layer=prev_layer, name=name) logging.info( @@ -365,11 +357,7 @@ def __init__( ) if b_init: b = tf.get_variable( - name='b_deconv2d', - shape=(shape[-2]), - initializer=b_init, - dtype=LayersConfig.tf_dtype, - **b_init_args + name='b_deconv2d', shape=(shape[-2]), initializer=b_init, dtype=LayersConfig.tf_dtype, **b_init_args ) self.outputs = act( tf.nn.conv2d_transpose(self.inputs, W, output_shape=output_shape, strides=strides, padding=padding) @@ -428,17 +416,17 @@ class Conv3dLayer(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - act=tf.identity, - shape=(2, 2, 2, 3, 32), - strides=(1, 2, 2, 2, 1), - padding='SAME', - W_init=tf.truncated_normal_initializer(stddev=0.02), - b_init=tf.constant_initializer(value=0.0), - W_init_args=None, - b_init_args=None, - name='cnn3d_layer', + self, + prev_layer, + act=tf.identity, + shape=(2, 2, 2, 3, 32), + strides=(1, 2, 2, 2, 1), + padding='SAME', + W_init=tf.truncated_normal_initializer(stddev=0.02), + b_init=tf.constant_initializer(value=0.0), + W_init_args=None, + b_init_args=None, + name='cnn3d_layer', ): super(Conv3dLayer, self).__init__(prev_layer=prev_layer, name=name) logging.info( @@ -514,18 +502,18 @@ class DeConv3dLayer(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - act=tf.identity, - shape=(2, 2, 2, 128, 256), - output_shape=(1, 12, 32, 32, 128), - strides=(1, 2, 2, 2, 1), - padding='SAME', - W_init=tf.truncated_normal_initializer(stddev=0.02), - b_init=tf.constant_initializer(value=0.0), - W_init_args=None, - b_init_args=None, - name='decnn3d_layer', + self, + prev_layer, + act=tf.identity, + shape=(2, 2, 2, 128, 256), + output_shape=(1, 12, 32, 32, 128), + strides=(1, 2, 2, 2, 1), + padding='SAME', + W_init=tf.truncated_normal_initializer(stddev=0.02), + b_init=tf.constant_initializer(value=0.0), + W_init_args=None, + b_init_args=None, + name='decnn3d_layer', ): super(DeConv3dLayer, self).__init__(prev_layer=prev_layer, name=name) logging.info( @@ -548,11 +536,7 @@ def __init__( ) if b_init: b = tf.get_variable( - name='b_deconv3d', - shape=(shape[-2]), - initializer=b_init, - dtype=LayersConfig.tf_dtype, - **b_init_args + name='b_deconv3d', shape=(shape[-2]), initializer=b_init, dtype=LayersConfig.tf_dtype, **b_init_args ) self.outputs = act( tf.nn.conv3d_transpose(self.inputs, W, output_shape=output_shape, strides=strides, padding=padding) @@ -599,13 +583,13 @@ class UpSampling2dLayer(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - size, - is_scale=True, - method=0, - align_corners=False, - name='upsample2d_layer', + self, + prev_layer, + size, + is_scale=True, + method=0, + align_corners=False, + name='upsample2d_layer', ): super(UpSampling2dLayer, self).__init__(prev_layer=prev_layer, name=name) logging.info( @@ -675,13 +659,13 @@ class DownSampling2dLayer(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - size, - is_scale=True, - method=0, - align_corners=False, - name='downsample2d_layer', + self, + prev_layer, + size, + is_scale=True, + method=0, + align_corners=False, + name='downsample2d_layer', ): super(DownSampling2dLayer, self).__init__(prev_layer=prev_layer, name=name) logging.info( @@ -773,18 +757,18 @@ class DeformableConv2d(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - offset_layer=None, - # shape=(3, 3, 1, 100), - n_filter=32, - filter_size=(3, 3), - act=tf.identity, - name='deformable_conv_2d', - W_init=tf.truncated_normal_initializer(stddev=0.02), - b_init=tf.constant_initializer(value=0.0), - W_init_args=None, - b_init_args=None + self, + prev_layer, + offset_layer=None, + # shape=(3, 3, 1, 100), + n_filter=32, + filter_size=(3, 3), + act=tf.identity, + name='deformable_conv_2d', + W_init=tf.truncated_normal_initializer(stddev=0.02), + b_init=tf.constant_initializer(value=0.0), + W_init_args=None, + b_init_args=None ): if tf.__version__ < "1.4": @@ -916,8 +900,7 @@ def _tf_batch_map_offsets(inputs, offsets, grid_offset): [ tf.clip_by_value(coords[:, :, :, :, 0], 0.0, tf.cast(input_h - 1, 'float32')), tf.clip_by_value(coords[:, :, :, :, 1], 0.0, tf.cast(input_w - 1, 'float32')) - ], - axis=-1 + ], axis=-1 ) coords = tf.tile(coords, [channel, 1, 1, 1, 1]) @@ -963,8 +946,7 @@ def _tf_batch_map_offsets(inputs, offsets, grid_offset): initial_offsets = tf.cast(initial_offsets, 'float32') grid = tf.meshgrid( tf.range(-int((shape[0] - 1) / 2.0), int(input_h - int((shape[0] - 1) / 2.0)), 1), - tf.range(-int((shape[1] - 1) / 2.0), int(input_w - int((shape[1] - 1) / 2.0)), 1), - indexing='ij' + tf.range(-int((shape[1] - 1) / 2.0), int(input_w - int((shape[1] - 1) / 2.0)), 1), indexing='ij' ) grid = tf.stack(grid, axis=-1) @@ -976,19 +958,13 @@ def _tf_batch_map_offsets(inputs, offsets, grid_offset): input_deform = _tf_batch_map_offsets(self.inputs, offset, grid_offset) W = tf.get_variable( - name='W_deformableconv2d', - shape=[1, 1, shape[0] * shape[1], shape[-2], shape[-1]], - initializer=W_init, - dtype=LayersConfig.tf_dtype, - **W_init_args + name='W_deformableconv2d', shape=[1, 1, shape[0] * shape[1], shape[-2], shape[-1]], initializer=W_init, + dtype=LayersConfig.tf_dtype, **W_init_args ) if b_init: b = tf.get_variable( - name='b_deformableconv2d', - shape=(shape[-1]), - initializer=b_init, - dtype=LayersConfig.tf_dtype, + name='b_deformableconv2d', shape=(shape[-1]), initializer=b_init, dtype=LayersConfig.tf_dtype, **b_init_args ) tf.reshape() @@ -1025,19 +1001,19 @@ def _tf_batch_map_offsets(inputs, offsets, grid_offset): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def atrous_conv1d( - prev_layer, - n_filter=32, - filter_size=2, - stride=1, - dilation=1, - act=tf.identity, - padding='SAME', - data_format='NWC', - W_init=tf.truncated_normal_initializer(stddev=0.02), - b_init=tf.constant_initializer(value=0.0), - W_init_args=None, - b_init_args=None, - name='conv1d', + prev_layer, + n_filter=32, + filter_size=2, + stride=1, + dilation=1, + act=tf.identity, + padding='SAME', + data_format='NWC', + W_init=tf.truncated_normal_initializer(stddev=0.02), + b_init=tf.constant_initializer(value=0.0), + W_init_args=None, + b_init_args=None, + name='conv1d', ): """Simplified version of :class:`AtrousConv1dLayer`. @@ -1133,18 +1109,9 @@ class AtrousConv2dLayer(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - n_filter=32, - filter_size=(3, 3), - rate=2, - act=tf.identity, - padding='SAME', - W_init=tf.truncated_normal_initializer(stddev=0.02), - b_init=tf.constant_initializer(value=0.0), - W_init_args=None, - b_init_args=None, - name='atrou2d' + self, prev_layer, n_filter=32, filter_size=(3, 3), rate=2, act=tf.identity, padding='SAME', + W_init=tf.truncated_normal_initializer(stddev=0.02), b_init=tf.constant_initializer(value=0.0), + W_init_args=None, b_init_args=None, name='atrou2d' ): super(AtrousConv2dLayer, self).__init__(prev_layer=prev_layer, name=name) @@ -1241,25 +1208,10 @@ class _SeparableConv2dLayer(Layer): # TODO @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - n_filter, - filter_size=5, - strides=(1, 1), - padding='valid', - data_format='channels_last', - dilation_rate=(1, 1), - depth_multiplier=1, - act=tf.identity, - use_bias=True, - depthwise_initializer=None, - pointwise_initializer=None, - bias_initializer=tf.zeros_initializer, - depthwise_regularizer=None, - pointwise_regularizer=None, - bias_regularizer=None, - activity_regularizer=None, - name='atrou2d' + self, prev_layer, n_filter, filter_size=5, strides=(1, 1), padding='valid', data_format='channels_last', + dilation_rate=(1, 1), depth_multiplier=1, act=tf.identity, use_bias=True, depthwise_initializer=None, + pointwise_initializer=None, bias_initializer=tf.zeros_initializer, depthwise_regularizer=None, + pointwise_regularizer=None, bias_regularizer=None, activity_regularizer=None, name='atrou2d' ): super(_SeparableConv2dLayer, self).__init__(prev_layer=prev_layer, name=name) @@ -1430,20 +1382,9 @@ class Conv1d(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - n_filter=32, - filter_size=5, - stride=1, - dilation_rate=1, - act=tf.identity, - padding='SAME', - data_format="channels_last", - W_init=tf.truncated_normal_initializer(stddev=0.02), - b_init=tf.constant_initializer(value=0.0), - W_init_args=None, - b_init_args=None, - name='conv1d' + self, prev_layer, n_filter=32, filter_size=5, stride=1, dilation_rate=1, act=tf.identity, padding='SAME', + data_format="channels_last", W_init=tf.truncated_normal_initializer(stddev=0.02), + b_init=tf.constant_initializer(value=0.0), W_init_args=None, b_init_args=None, name='conv1d' ): super(Conv1d, self).__init__(prev_layer=prev_layer, name=name) @@ -1455,17 +1396,9 @@ def __init__( self.inputs = prev_layer.outputs if tf.__version__ > '1.3': con1d = tf.layers.Conv1D( - filters=n_filter, - kernel_size=filter_size, - strides=stride, - padding=padding, - data_format=data_format, - dilation_rate=dilation_rate, - activation=act, - use_bias=(True if b_init else False), - kernel_initializer=W_init, - bias_initializer=b_init, - name=name + filters=n_filter, kernel_size=filter_size, strides=stride, padding=padding, data_format=data_format, + dilation_rate=dilation_rate, activation=act, use_bias=(True if b_init else False), + kernel_initializer=W_init, bias_initializer=b_init, name=name ) # con1d.dtype = LayersConfig.tf_dtype # unsupport, it will use the same dtype of inputs self.outputs = con1d(self.inputs) @@ -1552,21 +1485,21 @@ class Conv2d(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - n_filter=32, - filter_size=(3, 3), - strides=(1, 1), - act=tf.identity, - padding='SAME', - dilation_rate=(1, 1), - W_init=tf.truncated_normal_initializer(stddev=0.02), - b_init=tf.constant_initializer(value=0.0), - W_init_args=None, - b_init_args=None, - use_cudnn_on_gpu=None, - data_format=None, - name='conv2d', + self, + prev_layer, + n_filter=32, + filter_size=(3, 3), + strides=(1, 1), + act=tf.identity, + padding='SAME', + dilation_rate=(1, 1), + W_init=tf.truncated_normal_initializer(stddev=0.02), + b_init=tf.constant_initializer(value=0.0), + W_init_args=None, + b_init_args=None, + use_cudnn_on_gpu=None, + data_format=None, + name='conv2d', ): # if W_init_args is None: # W_init_args = {} @@ -1659,30 +1592,19 @@ def __init__( ) if b_init: b = tf.get_variable( - name='b_conv2d', - shape=(shape[-1]), - initializer=b_init, - dtype=LayersConfig.tf_dtype, + name='b_conv2d', shape=(shape[-1]), initializer=b_init, dtype=LayersConfig.tf_dtype, **b_init_args ) self.outputs = act( tf.nn.conv2d( - self.inputs, - W, - strides=strides, - padding=padding, - use_cudnn_on_gpu=use_cudnn_on_gpu, + self.inputs, W, strides=strides, padding=padding, use_cudnn_on_gpu=use_cudnn_on_gpu, data_format=data_format ) + b ) else: self.outputs = act( tf.nn.conv2d( - self.inputs, - W, - strides=strides, - padding=padding, - use_cudnn_on_gpu=use_cudnn_on_gpu, + self.inputs, W, strides=strides, padding=padding, use_cudnn_on_gpu=use_cudnn_on_gpu, data_format=data_format ) ) @@ -1733,20 +1655,20 @@ class DeConv2d(Layer): layer='prev_layer', n_out_channel='n_filter', end_support_version=1.9 ) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - n_filter=32, - filter_size=(3, 3), - out_size=(30, 30), # remove - strides=(2, 2), - padding='SAME', - batch_size=None, # remove - act=tf.identity, - W_init=tf.truncated_normal_initializer(stddev=0.02), - b_init=tf.constant_initializer(value=0.0), - W_init_args=None, # remove - b_init_args=None, # remove - name='decnn2d' + self, + prev_layer, + n_filter=32, + filter_size=(3, 3), + out_size=(30, 30), # remove + strides=(2, 2), + padding='SAME', + batch_size=None, # remove + act=tf.identity, + W_init=tf.truncated_normal_initializer(stddev=0.02), + b_init=tf.constant_initializer(value=0.0), + W_init_args=None, # remove + b_init_args=None, # remove + name='decnn2d' ): super(DeConv2d, self).__init__(prev_layer=prev_layer, name=name) logging.info( @@ -1768,14 +1690,8 @@ def __init__( self.inputs = prev_layer.outputs # scope_name = tf.get_variable_scope().name conv2d_transpose = tf.layers.Conv2DTranspose( - filters=n_filter, - kernel_size=filter_size, - strides=strides, - padding=padding, - activation=act, - kernel_initializer=W_init, - bias_initializer=b_init, - name=name + filters=n_filter, kernel_size=filter_size, strides=strides, padding=padding, activation=act, + kernel_initializer=W_init, bias_initializer=b_init, name=name ) self.outputs = conv2d_transpose(self.inputs) new_variables = conv2d_transpose.weights # new_variables = tf.get_collection(TF_GRAPHKEYS_VARIABLES, scope=vs.name) @@ -1833,16 +1749,9 @@ class DeConv3d(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - n_filter=32, - filter_size=(3, 3, 3), - strides=(2, 2, 2), - padding='SAME', - act=tf.identity, - W_init=tf.truncated_normal_initializer(stddev=0.02), - b_init=tf.constant_initializer(value=0.0), - name='decnn3d' + self, prev_layer, n_filter=32, filter_size=(3, 3, 3), strides=(2, 2, 2), padding='SAME', act=tf.identity, + W_init=tf.truncated_normal_initializer(stddev=0.02), b_init=tf.constant_initializer(value=0.0), + name='decnn3d' ): super(DeConv3d, self).__init__(prev_layer=prev_layer, name=name) @@ -1931,19 +1840,19 @@ class DepthwiseConv2d(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - shape=(3, 3), - strides=(1, 1), - act=tf.identity, - padding='SAME', - dilation_rate=(1, 1), - depth_multiplier=1, - W_init=tf.truncated_normal_initializer(stddev=0.02), - b_init=tf.constant_initializer(value=0.0), - W_init_args=None, - b_init_args=None, - name='depthwise_conv2d', + self, + prev_layer, + shape=(3, 3), + strides=(1, 1), + act=tf.identity, + padding='SAME', + dilation_rate=(1, 1), + depth_multiplier=1, + W_init=tf.truncated_normal_initializer(stddev=0.02), + b_init=tf.constant_initializer(value=0.0), + W_init_args=None, + b_init_args=None, + name='depthwise_conv2d', ): super(DepthwiseConv2d, self).__init__(prev_layer=prev_layer, name=name) logging.info( @@ -1979,11 +1888,8 @@ def __init__( ) # [filter_height, filter_width, in_channels, depth_multiplier] if b_init: b = tf.get_variable( - name='b_depthwise2d', - shape=(pre_channel * depth_multiplier), - initializer=b_init, - dtype=LayersConfig.tf_dtype, - **b_init_args + name='b_depthwise2d', shape=(pre_channel * depth_multiplier), initializer=b_init, + dtype=LayersConfig.tf_dtype, **b_init_args ) self.outputs = act( tf.nn.depthwise_conv2d(self.inputs, W, strides=strides, padding=padding, rate=dilation_rate) + b @@ -2040,32 +1946,32 @@ class SeparableConv2d(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - n_filter=100, - filter_size=(3, 3), - strides=(1, 1), - act=tf.identity, - padding='valid', - data_format='channels_last', - dilation_rate=(1, 1), - depth_multiplier=1, - # activation=None, - # use_bias=True, - depthwise_init=None, - pointwise_init=None, - b_init=tf.zeros_initializer(), - # depthwise_regularizer=None, - # pointwise_regularizer=None, - # bias_regularizer=None, - # activity_regularizer=None, - # depthwise_constraint=None, - # pointwise_constraint=None, - # W_init=tf.truncated_normal_initializer(stddev=0.1), - # b_init=tf.constant_initializer(value=0.0), - # W_init_args=None, - # b_init_args=None, - name='seperable', + self, + prev_layer, + n_filter=100, + filter_size=(3, 3), + strides=(1, 1), + act=tf.identity, + padding='valid', + data_format='channels_last', + dilation_rate=(1, 1), + depth_multiplier=1, + # activation=None, + # use_bias=True, + depthwise_init=None, + pointwise_init=None, + b_init=tf.zeros_initializer(), + # depthwise_regularizer=None, + # pointwise_regularizer=None, + # bias_regularizer=None, + # activity_regularizer=None, + # depthwise_constraint=None, + # pointwise_constraint=None, + # W_init=tf.truncated_normal_initializer(stddev=0.1), + # b_init=tf.constant_initializer(value=0.0), + # W_init_args=None, + # b_init_args=None, + name='seperable', ): # if W_init_args is None: # W_init_args = {} @@ -2144,19 +2050,19 @@ class GroupConv2d(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - n_filter=32, - filter_size=(3, 3), - strides=(2, 2), - n_group=2, - act=tf.identity, - padding='SAME', - W_init=tf.truncated_normal_initializer(stddev=0.02), - b_init=tf.constant_initializer(value=0.0), - W_init_args=None, - b_init_args=None, - name='groupconv', + self, + prev_layer, + n_filter=32, + filter_size=(3, 3), + strides=(2, 2), + n_group=2, + act=tf.identity, + padding='SAME', + W_init=tf.truncated_normal_initializer(stddev=0.02), + b_init=tf.constant_initializer(value=0.0), + W_init_args=None, + b_init_args=None, + name='groupconv', ): # Windaway super(GroupConv2d, self).__init__(prev_layer=prev_layer, name=name) @@ -2177,20 +2083,12 @@ def __init__( with tf.variable_scope(name): We = tf.get_variable( - name='W', - shape=[filter_size[0], filter_size[1], channels / n_group, n_filter], - initializer=W_init, - dtype=LayersConfig.tf_dtype, - trainable=True, - **W_init_args + name='W', shape=[filter_size[0], filter_size[1], channels / n_group, n_filter], initializer=W_init, + dtype=LayersConfig.tf_dtype, trainable=True, **W_init_args ) if b_init: bi = tf.get_variable( - name='b', - shape=n_filter, - initializer=b_init, - dtype=LayersConfig.tf_dtype, - trainable=True, + name='b', shape=n_filter, initializer=b_init, dtype=LayersConfig.tf_dtype, trainable=True, **b_init_args ) if n_group == 1: diff --git a/tensorlayer/layers/core.py b/tensorlayer/layers/core.py index 1b049b42e..0bbb7188f 100644 --- a/tensorlayer/layers/core.py +++ b/tensorlayer/layers/core.py @@ -649,20 +649,20 @@ class Word2vecEmbeddingInputlayer(Layer): """ def __init__( - self, - inputs=None, - train_labels=None, - vocabulary_size=80000, - embedding_size=200, - num_sampled=64, - nce_loss_args=None, - E_init=tf.random_uniform_initializer(minval=-1.0, maxval=1.0), - E_init_args=None, - nce_W_init=tf.truncated_normal_initializer(stddev=0.03), - nce_W_init_args=None, - nce_b_init=tf.constant_initializer(value=0.0), - nce_b_init_args=None, - name='word2vec', + self, + inputs=None, + train_labels=None, + vocabulary_size=80000, + embedding_size=200, + num_sampled=64, + nce_loss_args=None, + E_init=tf.random_uniform_initializer(minval=-1.0, maxval=1.0), + E_init_args=None, + nce_W_init=tf.truncated_normal_initializer(stddev=0.03), + nce_W_init_args=None, + nce_b_init=tf.constant_initializer(value=0.0), + nce_b_init_args=None, + name='word2vec', ): if nce_loss_args is None: nce_loss_args = {} @@ -687,26 +687,17 @@ def __init__( # row vector with 'embedding_size' values. with tf.variable_scope(name): embeddings = tf.get_variable( - name='embeddings', - shape=(vocabulary_size, embedding_size), - initializer=E_init, - dtype=LayersConfig.tf_dtype, - **E_init_args + name='embeddings', shape=(vocabulary_size, embedding_size), initializer=E_init, + dtype=LayersConfig.tf_dtype, **E_init_args ) embed = tf.nn.embedding_lookup(embeddings, self.inputs) # Construct the variables for the NCE loss (i.e. negative sampling) nce_weights = tf.get_variable( - name='nce_weights', - shape=(vocabulary_size, embedding_size), - initializer=nce_W_init, - dtype=LayersConfig.tf_dtype, - **nce_W_init_args + name='nce_weights', shape=(vocabulary_size, embedding_size), initializer=nce_W_init, + dtype=LayersConfig.tf_dtype, **nce_W_init_args ) nce_biases = tf.get_variable( - name='nce_biases', - shape=(vocabulary_size), - initializer=nce_b_init, - dtype=LayersConfig.tf_dtype, + name='nce_biases', shape=(vocabulary_size), initializer=nce_b_init, dtype=LayersConfig.tf_dtype, **nce_b_init_args ) @@ -715,13 +706,8 @@ def __init__( # each time we evaluate the loss. self.nce_cost = tf.reduce_mean( tf.nn.nce_loss( - weights=nce_weights, - biases=nce_biases, - inputs=embed, - labels=train_labels, - num_sampled=num_sampled, - num_classes=vocabulary_size, - **nce_loss_args + weights=nce_weights, biases=nce_biases, inputs=embed, labels=train_labels, num_sampled=num_sampled, + num_classes=vocabulary_size, **nce_loss_args ) ) @@ -772,13 +758,13 @@ class EmbeddingInputlayer(Layer): """ def __init__( - self, - inputs=None, - vocabulary_size=80000, - embedding_size=200, - E_init=tf.random_uniform_initializer(-0.1, 0.1), - E_init_args=None, - name='embedding', + self, + inputs=None, + vocabulary_size=80000, + embedding_size=200, + E_init=tf.random_uniform_initializer(-0.1, 0.1), + E_init_args=None, + name='embedding', ): if E_init_args is None: E_init_args = {} @@ -790,11 +776,8 @@ def __init__( with tf.variable_scope(name): embeddings = tf.get_variable( - name='embeddings', - shape=(vocabulary_size, embedding_size), - initializer=E_init, - dtype=LayersConfig.tf_dtype, - **E_init_args + name='embeddings', shape=(vocabulary_size, embedding_size), initializer=E_init, + dtype=LayersConfig.tf_dtype, **E_init_args ) embed = tf.nn.embedding_lookup(embeddings, self.inputs) @@ -843,14 +826,14 @@ class AverageEmbeddingInputlayer(Layer): """ def __init__( - self, - inputs, - vocabulary_size, - embedding_size, - pad_value=0, - embeddings_initializer=tf.random_uniform_initializer(-0.1, 0.1), - embeddings_kwargs=None, - name='average_embedding', + self, + inputs, + vocabulary_size, + embedding_size, + pad_value=0, + embeddings_initializer=tf.random_uniform_initializer(-0.1, 0.1), + embeddings_kwargs=None, + name='average_embedding', ): super(AverageEmbeddingInputlayer, self).__init__(prev_layer=None, name=name) @@ -866,9 +849,7 @@ def __init__( with tf.variable_scope(name): self.embeddings = tf.get_variable( - name='embeddings', - shape=(vocabulary_size, embedding_size), - initializer=embeddings_initializer, + name='embeddings', shape=(vocabulary_size, embedding_size), initializer=embeddings_initializer, dtype=LayersConfig.tf_dtype, **(embeddings_kwargs or {}) # **embeddings_kwargs @@ -954,15 +935,15 @@ class DenseLayer(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - n_units=100, - act=tf.identity, - W_init=tf.truncated_normal_initializer(stddev=0.1), - b_init=tf.constant_initializer(value=0.0), - W_init_args=None, - b_init_args=None, - name='dense', + self, + prev_layer, + n_units=100, + act=tf.identity, + W_init=tf.truncated_normal_initializer(stddev=0.1), + b_init=tf.constant_initializer(value=0.0), + W_init_args=None, + b_init_args=None, + name='dense', ): super(DenseLayer, self).__init__(prev_layer=prev_layer, name=name) @@ -1051,12 +1032,12 @@ class ReconLayer(DenseLayer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - x_recon=None, - n_units=784, - act=tf.nn.softplus, - name='recon', + self, + prev_layer, + x_recon=None, + n_units=784, + act=tf.nn.softplus, + name='recon', ): super(ReconLayer, self).__init__(prev_layer=prev_layer, n_units=n_units, act=act, name=name) @@ -1143,25 +1124,13 @@ def __init__( else: raise Exception("Don't support the given reconstruct activation function") - self.train_op = tf.train.AdamOptimizer( - learning_rate, beta1=0.9, beta2=0.999, epsilon=1e-08, use_locking=False - ).minimize( - self.cost, var_list=self.train_params - ) + self.train_op = tf.train.AdamOptimizer(learning_rate, beta1=0.9, beta2=0.999, epsilon=1e-08, + use_locking=False).minimize(self.cost, var_list=self.train_params) # self.train_op = tf.train.GradientDescentOptimizer(1.0).minimize(self.cost, var_list=self.train_params) def pretrain( - self, - sess, - x, - X_train, - X_val, - denoise_name=None, - n_epoch=100, - batch_size=128, - print_freq=10, - save=True, - save_name='w1pre_' + self, sess, x, X_train, X_val, denoise_name=None, n_epoch=100, batch_size=128, print_freq=10, save=True, + save_name='w1pre_' ): # ==================================================== # @@ -1210,12 +1179,8 @@ def pretrain( if save: try: visualize.draw_weights( - self.train_params[0].eval(), - second=10, - saveable=True, - shape=[28, 28], - name=save_name + str(epoch + 1), - fig_idx=2012 + self.train_params[0].eval(), second=10, saveable=True, shape=[28, 28], + name=save_name + str(epoch + 1), fig_idx=2012 ) files.save_npz([self.all_params[0]], name=save_name + str(epoch + 1) + '.npz') except Exception: @@ -1284,13 +1249,13 @@ class DropoutLayer(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - keep=0.5, - is_fix=False, - is_train=True, - seed=None, - name='dropout_layer', + self, + prev_layer, + keep=0.5, + is_fix=False, + is_train=True, + seed=None, + name='dropout_layer', ): super(DropoutLayer, self).__init__(prev_layer=prev_layer, name=name) logging.info("DropoutLayer %s: keep:%f is_fix:%s" % (name, keep, is_fix)) @@ -1369,13 +1334,13 @@ class GaussianNoiseLayer(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - mean=0.0, - stddev=1.0, - is_train=True, - seed=None, - name='gaussian_noise_layer', + self, + prev_layer, + mean=0.0, + stddev=1.0, + is_train=True, + seed=None, + name='gaussian_noise_layer', ): super(GaussianNoiseLayer, self).__init__(prev_layer=prev_layer, name=name) @@ -1444,16 +1409,16 @@ class DropconnectDenseLayer(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - keep=0.5, - n_units=100, - act=tf.identity, - W_init=tf.truncated_normal_initializer(stddev=0.1), - b_init=tf.constant_initializer(value=0.0), - W_init_args=None, - b_init_args=None, - name='dropconnect_layer', + self, + prev_layer, + keep=0.5, + n_units=100, + act=tf.identity, + W_init=tf.truncated_normal_initializer(stddev=0.1), + b_init=tf.constant_initializer(value=0.0), + W_init_args=None, + b_init_args=None, + name='dropconnect_layer', ): super(DropconnectDenseLayer, self).__init__(prev_layer=prev_layer, name=name) logging.info("DropconnectDenseLayer %s: %d %s" % (name, n_units, act.__name__)) diff --git a/tensorlayer/layers/extend.py b/tensorlayer/layers/extend.py index 87b5cbc21..8c3fa52b1 100644 --- a/tensorlayer/layers/extend.py +++ b/tensorlayer/layers/extend.py @@ -37,10 +37,10 @@ class ExpandDimsLayer(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - axis, - name='expand_dims', + self, + prev_layer, + axis, + name='expand_dims', ): super(ExpandDimsLayer, self).__init__(prev_layer=prev_layer, name=name) logging.info("ExpandDimsLayer %s: axis:%d" % (name, axis)) diff --git a/tensorlayer/layers/importer.py b/tensorlayer/layers/importer.py index dd0b73a60..632b59d99 100644 --- a/tensorlayer/layers/importer.py +++ b/tensorlayer/layers/importer.py @@ -57,11 +57,11 @@ class LambdaLayer(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - fn, - fn_args=None, - name='lambda_layer', + self, + prev_layer, + fn, + fn_args=None, + name='lambda_layer', ): super(LambdaLayer, self).__init__(prev_layer=prev_layer, name=name) @@ -111,11 +111,11 @@ class SlimNetsLayer(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - slim_layer, - slim_args=None, - name='tfslim_layer', + self, + prev_layer, + slim_layer, + slim_args=None, + name='tfslim_layer', ): super(SlimNetsLayer, self).__init__(prev_layer=prev_layer, name=name) @@ -177,11 +177,11 @@ class KerasLayer(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - keras_layer, - keras_args=None, - name='keras_layer', + self, + prev_layer, + keras_layer, + keras_args=None, + name='keras_layer', ): super(KerasLayer, self).__init__(prev_layer=prev_layer, name=name) @@ -227,11 +227,11 @@ class EstimatorLayer(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - model_fn, - args=None, - name='estimator_layer', + self, + prev_layer, + model_fn, + args=None, + name='estimator_layer', ): super(EstimatorLayer, self).__init__(prev_layer=prev_layer, name=name) logging.info("EstimatorLayer %s: %s" % (name, model_fn)) diff --git a/tensorlayer/layers/merge.py b/tensorlayer/layers/merge.py index 95fbfbeb9..9837d4b0f 100644 --- a/tensorlayer/layers/merge.py +++ b/tensorlayer/layers/merge.py @@ -50,10 +50,10 @@ class ConcatLayer(Layer): """ def __init__( - self, - layers, - concat_dim=-1, - name='concat_layer', + self, + layers, + concat_dim=-1, + name='concat_layer', ): super(ConcatLayer, self).__init__(prev_layer=layers, name=name) @@ -116,11 +116,11 @@ class ElementwiseLayer(Layer): """ def __init__( - self, - layers, - combine_fn=tf.minimum, - act=None, - name='elementwise_layer', + self, + layers, + combine_fn=tf.minimum, + act=None, + name='elementwise_layer', ): super(ElementwiseLayer, self).__init__(prev_layer=layers, name=name) diff --git a/tensorlayer/layers/normalization.py b/tensorlayer/layers/normalization.py index b52838098..da2e8be3f 100644 --- a/tensorlayer/layers/normalization.py +++ b/tensorlayer/layers/normalization.py @@ -40,13 +40,13 @@ class LocalResponseNormLayer(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - depth_radius=None, - bias=None, - alpha=None, - beta=None, - name='lrn_layer', + self, + prev_layer, + depth_radius=None, + bias=None, + alpha=None, + beta=None, + name='lrn_layer', ): super(LocalResponseNormLayer, self).__init__(prev_layer=prev_layer, name=name) logging.info( @@ -102,15 +102,15 @@ class BatchNormLayer(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - decay=0.9, - epsilon=0.00001, - act=tf.identity, - is_train=False, - beta_init=tf.zeros_initializer, - gamma_init=tf.random_normal_initializer(mean=1.0, stddev=0.002), - name='batchnorm_layer', + self, + prev_layer, + decay=0.9, + epsilon=0.00001, + act=tf.identity, + is_train=False, + beta_init=tf.zeros_initializer, + gamma_init=tf.random_normal_initializer(mean=1.0, stddev=0.002), + name='batchnorm_layer', ): super(BatchNormLayer, self).__init__(prev_layer=prev_layer, name=name) logging.info( @@ -227,11 +227,11 @@ class InstanceNormLayer(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - act=tf.identity, - epsilon=1e-5, - name='instan_norm', + self, + prev_layer, + act=tf.identity, + epsilon=1e-5, + name='instan_norm', ): super(InstanceNormLayer, self).__init__(prev_layer=prev_layer, name=name) logging.info("InstanceNormLayer %s: epsilon:%f act:%s" % (self.name, epsilon, act.__name__)) @@ -242,12 +242,10 @@ def __init__( mean, var = tf.nn.moments(self.inputs, [1, 2], keep_dims=True) scale = tf.get_variable( 'scale', [self.inputs.get_shape()[-1]], - initializer=tf.truncated_normal_initializer(mean=1.0, stddev=0.02), - dtype=LayersConfig.tf_dtype + initializer=tf.truncated_normal_initializer(mean=1.0, stddev=0.02), dtype=LayersConfig.tf_dtype ) offset = tf.get_variable( - 'offset', [self.inputs.get_shape()[-1]], - initializer=tf.constant_initializer(0.0), + 'offset', [self.inputs.get_shape()[-1]], initializer=tf.constant_initializer(0.0), dtype=LayersConfig.tf_dtype ) self.outputs = scale * tf.div(self.inputs - mean, tf.sqrt(var + epsilon)) + offset @@ -278,18 +276,8 @@ class LayerNormLayer(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - center=True, - scale=True, - act=tf.identity, - reuse=None, - variables_collections=None, - outputs_collections=None, - trainable=True, - begin_norm_axis=1, - begin_params_axis=-1, - name='layernorm' + self, prev_layer, center=True, scale=True, act=tf.identity, reuse=None, variables_collections=None, + outputs_collections=None, trainable=True, begin_norm_axis=1, begin_params_axis=-1, name='layernorm' ): super(LayerNormLayer, self).__init__(prev_layer=prev_layer, name=name) diff --git a/tensorlayer/layers/object_detection.py b/tensorlayer/layers/object_detection.py index 981d03232..2fb1a2ccf 100644 --- a/tensorlayer/layers/object_detection.py +++ b/tensorlayer/layers/object_detection.py @@ -36,12 +36,12 @@ class ROIPoolingLayer(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - rois, - pool_height=2, - pool_width=2, - name='roipooling_layer', + self, + prev_layer, + rois, + pool_height=2, + pool_width=2, + name='roipooling_layer', ): super(ROIPoolingLayer, self).__init__(prev_layer=prev_layer, name=name) logging.info("ROIPoolingLayer %s: (%d, %d)" % (name, pool_height, pool_width)) diff --git a/tensorlayer/layers/padding.py b/tensorlayer/layers/padding.py index 32d4d119c..64314c589 100644 --- a/tensorlayer/layers/padding.py +++ b/tensorlayer/layers/padding.py @@ -39,11 +39,11 @@ class PadLayer(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - padding=None, - mode='CONSTANT', - name='pad_layer', + self, + prev_layer, + padding=None, + mode='CONSTANT', + name='pad_layer', ): super(PadLayer, self).__init__(prev_layer=prev_layer, name=name) logging.info("PadLayer %s: padding:%s mode:%s" % (name, list(padding), mode)) @@ -77,10 +77,10 @@ class ZeroPad1d(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - padding, - name='zeropad1d', + self, + prev_layer, + padding, + name='zeropad1d', ): super(ZeroPad1d, self).__init__(prev_layer=prev_layer, name=name) logging.info("ZeroPad1d %s: padding:%s" % (name, str(padding))) @@ -113,10 +113,10 @@ class ZeroPad2d(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - padding, - name='zeropad2d', + self, + prev_layer, + padding, + name='zeropad2d', ): super(ZeroPad2d, self).__init__(prev_layer=prev_layer, name=name) logging.info("ZeroPad2d %s: padding:%s" % (name, str(padding))) @@ -148,10 +148,10 @@ class ZeroPad3d(Layer): """ def __init__( - self, - prev_layer, - padding, - name='zeropad3d', + self, + prev_layer, + padding, + name='zeropad3d', ): super(ZeroPad3d, self).__init__(prev_layer=prev_layer, name=name) logging.info("ZeroPad3d %s: padding:%s" % (name, str(padding))) diff --git a/tensorlayer/layers/pooling.py b/tensorlayer/layers/pooling.py index 95ecb48e6..c3475a0ae 100644 --- a/tensorlayer/layers/pooling.py +++ b/tensorlayer/layers/pooling.py @@ -58,13 +58,13 @@ class PoolLayer(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - ksize=(1, 2, 2, 1), - strides=(1, 2, 2, 1), - padding='SAME', - pool=tf.nn.max_pool, - name='pool_layer', + self, + prev_layer, + ksize=(1, 2, 2, 1), + strides=(1, 2, 2, 1), + padding='SAME', + pool=tf.nn.max_pool, + name='pool_layer', ): super(PoolLayer, self).__init__(prev_layer=prev_layer, name=name) logging.info( @@ -104,7 +104,7 @@ class MaxPool1d(Layer): @deprecated_alias(net='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, prev_layer, filter_size=3, strides=2, padding='valid', data_format='channels_last', name='maxpool1d' + self, prev_layer, filter_size=3, strides=2, padding='valid', data_format='channels_last', name='maxpool1d' ): super(MaxPool1d, self).__init__(prev_layer=prev_layer, name=name) logging.info( @@ -150,7 +150,7 @@ class MeanPool1d(Layer): # return net_new @deprecated_alias(net='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, prev_layer, filter_size=3, strides=2, padding='valid', data_format='channels_last', name='meanpool1d' + self, prev_layer, filter_size=3, strides=2, padding='valid', data_format='channels_last', name='meanpool1d' ): super(MeanPool1d, self).__init__(prev_layer=prev_layer, name=name) logging.info( @@ -285,13 +285,8 @@ class MaxPool3d(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - filter_size=(3, 3, 3), - strides=(2, 2, 2), - padding='valid', - data_format='channels_last', - name='maxpool3d' + self, prev_layer, filter_size=(3, 3, 3), strides=(2, 2, 2), padding='valid', data_format='channels_last', + name='maxpool3d' ): super(MaxPool3d, self).__init__(prev_layer=prev_layer, name=name) logging.info( @@ -338,13 +333,8 @@ class MeanPool3d(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - filter_size=(3, 3, 3), - strides=(2, 2, 2), - padding='valid', - data_format='channels_last', - name='meanpool3d' + self, prev_layer, filter_size=(3, 3, 3), strides=(2, 2, 2), padding='valid', data_format='channels_last', + name='meanpool3d' ): super(MeanPool3d, self).__init__(prev_layer=prev_layer, name=name) diff --git a/tensorlayer/layers/recurrent.py b/tensorlayer/layers/recurrent.py index 7674fee85..fb32005d9 100644 --- a/tensorlayer/layers/recurrent.py +++ b/tensorlayer/layers/recurrent.py @@ -134,17 +134,17 @@ class RNNLayer(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - cell_fn, - cell_init_args=None, - n_hidden=100, - initializer=tf.random_uniform_initializer(-0.1, 0.1), - n_steps=5, - initial_state=None, - return_last=False, - return_seq_2d=False, - name='rnn', + self, + prev_layer, + cell_fn, + cell_init_args=None, + n_hidden=100, + initializer=tf.random_uniform_initializer(-0.1, 0.1), + n_steps=5, + initial_state=None, + return_last=False, + return_seq_2d=False, + name='rnn', ): super(RNNLayer, self).__init__(prev_layer=prev_layer, name=name) @@ -324,20 +324,20 @@ class BiRNNLayer(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - cell_fn, - cell_init_args=None, - n_hidden=100, - initializer=tf.random_uniform_initializer(-0.1, 0.1), - n_steps=5, - fw_initial_state=None, - bw_initial_state=None, - dropout=None, - n_layer=1, - return_last=False, - return_seq_2d=False, - name='birnn', + self, + prev_layer, + cell_fn, + cell_init_args=None, + n_hidden=100, + initializer=tf.random_uniform_initializer(-0.1, 0.1), + n_steps=5, + fw_initial_state=None, + bw_initial_state=None, + dropout=None, + n_layer=1, + return_last=False, + return_seq_2d=False, + name='birnn', ): super(BiRNNLayer, self).__init__(prev_layer=prev_layer, name=name) @@ -547,7 +547,8 @@ class BasicConvLSTMCell(ConvRNNCell): """ def __init__( - self, shape, filter_size, num_features, forget_bias=1.0, input_size=None, state_is_tuple=False, act=tf.nn.tanh + self, shape, filter_size, num_features, forget_bias=1.0, input_size=None, state_is_tuple=False, + act=tf.nn.tanh ): """Initialize the basic Conv LSTM cell.""" # if not state_is_tuple: @@ -711,18 +712,18 @@ class ConvLSTMLayer(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - cell_shape=None, - feature_map=1, - filter_size=(3, 3), - cell_fn=BasicConvLSTMCell, - initializer=tf.random_uniform_initializer(-0.1, 0.1), - n_steps=5, - initial_state=None, - return_last=False, - return_seq_2d=False, - name='convlstm', + self, + prev_layer, + cell_shape=None, + feature_map=1, + filter_size=(3, 3), + cell_fn=BasicConvLSTMCell, + initializer=tf.random_uniform_initializer(-0.1, 0.1), + n_steps=5, + initial_state=None, + return_last=False, + return_seq_2d=False, + name='convlstm', ): super(ConvLSTMLayer, self).__init__(prev_layer=prev_layer, name=name) @@ -1053,20 +1054,20 @@ class DynamicRNNLayer(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - cell_fn, #tf.nn.rnn_cell.LSTMCell, - cell_init_args=None, - n_hidden=256, - initializer=tf.random_uniform_initializer(-0.1, 0.1), - sequence_length=None, - initial_state=None, - dropout=None, - n_layer=1, - return_last=None, - return_seq_2d=False, - dynamic_rnn_init_args=None, - name='dyrnn', + self, + prev_layer, + cell_fn, #tf.nn.rnn_cell.LSTMCell, + cell_init_args=None, + n_hidden=256, + initializer=tf.random_uniform_initializer(-0.1, 0.1), + sequence_length=None, + initial_state=None, + dropout=None, + n_layer=1, + return_last=None, + return_seq_2d=False, + dynamic_rnn_init_args=None, + name='dyrnn', ): super(DynamicRNNLayer, self).__init__(prev_layer=prev_layer, name=name) @@ -1319,21 +1320,21 @@ class BiDynamicRNNLayer(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - cell_fn, #tf.nn.rnn_cell.LSTMCell, - cell_init_args=None, - n_hidden=256, - initializer=tf.random_uniform_initializer(-0.1, 0.1), - sequence_length=None, - fw_initial_state=None, - bw_initial_state=None, - dropout=None, - n_layer=1, - return_last=False, - return_seq_2d=False, - dynamic_rnn_init_args=None, - name='bi_dyrnn_layer', + self, + prev_layer, + cell_fn, #tf.nn.rnn_cell.LSTMCell, + cell_init_args=None, + n_hidden=256, + initializer=tf.random_uniform_initializer(-0.1, 0.1), + sequence_length=None, + fw_initial_state=None, + bw_initial_state=None, + dropout=None, + n_layer=1, + return_last=False, + return_seq_2d=False, + dynamic_rnn_init_args=None, + name='bi_dyrnn_layer', ): super(BiDynamicRNNLayer, self).__init__(prev_layer=prev_layer, name=name) @@ -1436,28 +1437,18 @@ def __init__( self.bw_cell = [cell_creator() for _ in range(n_layer)] from tensorflow.contrib.rnn import stack_bidirectional_dynamic_rnn outputs, states_fw, states_bw = stack_bidirectional_dynamic_rnn( - cells_fw=self.fw_cell, - cells_bw=self.bw_cell, - inputs=self.inputs, - sequence_length=sequence_length, - initial_states_fw=self.fw_initial_state, - initial_states_bw=self.bw_initial_state, - dtype=LayersConfig.tf_dtype, - **dynamic_rnn_init_args + cells_fw=self.fw_cell, cells_bw=self.bw_cell, inputs=self.inputs, sequence_length=sequence_length, + initial_states_fw=self.fw_initial_state, initial_states_bw=self.bw_initial_state, + dtype=LayersConfig.tf_dtype, **dynamic_rnn_init_args ) else: self.fw_cell = cell_creator() self.bw_cell = cell_creator() outputs, (states_fw, states_bw) = tf.nn.bidirectional_dynamic_rnn( - cell_fw=self.fw_cell, - cell_bw=self.bw_cell, - inputs=self.inputs, - sequence_length=sequence_length, - initial_state_fw=self.fw_initial_state, - initial_state_bw=self.bw_initial_state, - dtype=LayersConfig.tf_dtype, - **dynamic_rnn_init_args + cell_fw=self.fw_cell, cell_bw=self.bw_cell, inputs=self.inputs, sequence_length=sequence_length, + initial_state_fw=self.fw_initial_state, initial_state_bw=self.bw_initial_state, + dtype=LayersConfig.tf_dtype, **dynamic_rnn_init_args ) rnn_variables = tf.get_collection(TF_GRAPHKEYS_VARIABLES, scope=vs.name) @@ -1617,21 +1608,21 @@ class Seq2Seq(Layer): """ def __init__( - self, - net_encode_in, - net_decode_in, - cell_fn, #tf.nn.rnn_cell.LSTMCell, - cell_init_args=None, - n_hidden=256, - initializer=tf.random_uniform_initializer(-0.1, 0.1), - encode_sequence_length=None, - decode_sequence_length=None, - initial_state_encode=None, - initial_state_decode=None, - dropout=None, - n_layer=1, - return_seq_2d=False, - name='seq2seq', + self, + net_encode_in, + net_decode_in, + cell_fn, #tf.nn.rnn_cell.LSTMCell, + cell_init_args=None, + n_hidden=256, + initializer=tf.random_uniform_initializer(-0.1, 0.1), + encode_sequence_length=None, + decode_sequence_length=None, + initial_state_encode=None, + initial_state_decode=None, + dropout=None, + n_layer=1, + return_seq_2d=False, + name='seq2seq', ): super(Seq2Seq, self).__init__(prev_layer=None, name=name) @@ -1655,34 +1646,18 @@ def __init__( # tl.layers.set_name_reuse(reuse) # network = InputLayer(self.inputs, name=name+'/input') network_encode = DynamicRNNLayer( - net_encode_in, - cell_fn=cell_fn, - cell_init_args=cell_init_args, - n_hidden=n_hidden, - initializer=initializer, - initial_state=initial_state_encode, - dropout=dropout, - n_layer=n_layer, - sequence_length=encode_sequence_length, - return_last=False, - return_seq_2d=True, - name='encode' + net_encode_in, cell_fn=cell_fn, cell_init_args=cell_init_args, n_hidden=n_hidden, + initializer=initializer, initial_state=initial_state_encode, dropout=dropout, n_layer=n_layer, + sequence_length=encode_sequence_length, return_last=False, return_seq_2d=True, name='encode' ) # vs.reuse_variables() # tl.layers.set_name_reuse(True) network_decode = DynamicRNNLayer( - net_decode_in, - cell_fn=cell_fn, - cell_init_args=cell_init_args, - n_hidden=n_hidden, + net_decode_in, cell_fn=cell_fn, cell_init_args=cell_init_args, n_hidden=n_hidden, initializer=initializer, - initial_state=(network_encode.final_state if initial_state_decode is None else initial_state_decode), - dropout=dropout, - n_layer=n_layer, - sequence_length=decode_sequence_length, - return_last=False, - return_seq_2d=return_seq_2d, - name='decode' + initial_state=(network_encode.final_state if initial_state_decode is None else + initial_state_decode), dropout=dropout, n_layer=n_layer, + sequence_length=decode_sequence_length, return_last=False, return_seq_2d=return_seq_2d, name='decode' ) self.outputs = network_decode.outputs diff --git a/tensorlayer/layers/spatial_transformer.py b/tensorlayer/layers/spatial_transformer.py index a884f998e..68be2f773 100644 --- a/tensorlayer/layers/spatial_transformer.py +++ b/tensorlayer/layers/spatial_transformer.py @@ -231,11 +231,11 @@ class SpatialTransformer2dAffineLayer(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - theta_layer, - out_size=None, - name='spatial_trans_2d_affine', + self, + prev_layer, + theta_layer, + out_size=None, + name='spatial_trans_2d_affine', ): super(SpatialTransformer2dAffineLayer, self).__init__(prev_layer=[prev_layer, theta_layer], name=name) diff --git a/tensorlayer/layers/special_activation.py b/tensorlayer/layers/special_activation.py index cf83aeef4..9502bd4ac 100644 --- a/tensorlayer/layers/special_activation.py +++ b/tensorlayer/layers/special_activation.py @@ -37,13 +37,13 @@ class PReluLayer(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - channel_shared=False, - a_init=tf.constant_initializer(value=0.0), - a_init_args=None, - # restore = True, - name="prelu_layer" + self, + prev_layer, + channel_shared=False, + a_init=tf.constant_initializer(value=0.0), + a_init_args=None, + # restore = True, + name="prelu_layer" ): if a_init_args is None: diff --git a/tensorlayer/layers/stack.py b/tensorlayer/layers/stack.py index 3e1b76bb2..dd9bde8be 100644 --- a/tensorlayer/layers/stack.py +++ b/tensorlayer/layers/stack.py @@ -39,10 +39,10 @@ class StackLayer(Layer): """ def __init__( - self, - layers, - axis=1, - name='stack', + self, + layers, + axis=1, + name='stack', ): super(StackLayer, self).__init__(prev_layer=layers, name=name) diff --git a/tensorlayer/layers/time_distribution.py b/tensorlayer/layers/time_distribution.py index 7aec9cfd1..fcec358e1 100644 --- a/tensorlayer/layers/time_distribution.py +++ b/tensorlayer/layers/time_distribution.py @@ -50,11 +50,11 @@ class TimeDistributedLayer(Layer): @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release def __init__( - self, - prev_layer, - layer_class=None, - args=None, - name='time_distributed', + self, + prev_layer, + layer_class=None, + args=None, + name='time_distributed', ): super(TimeDistributedLayer, self).__init__(prev_layer=prev_layer, name=name) logging.info("TimeDistributedLayer %s: layer_class:%s args:%s" % (self.name, layer_class.__name__, args)) diff --git a/tensorlayer/models/mobilenetv1.py b/tensorlayer/models/mobilenetv1.py index 44659ffe9..968b699b4 100644 --- a/tensorlayer/models/mobilenetv1.py +++ b/tensorlayer/models/mobilenetv1.py @@ -153,9 +153,7 @@ def depthwise_conv_block(cls, n, n_filter, strides=(1, 1), is_train=False, name= def restore_params(self, sess, path='models'): logging.info("Restore pre-trained parameters") maybe_download_and_extract( - 'mobilenet.npz', - path, - 'https://github.com/tensorlayer/pretrained-models/raw/master/models/', + 'mobilenet.npz', path, 'https://github.com/tensorlayer/pretrained-models/raw/master/models/', expected_bytes=25600116 ) # ls -al params = load_npz(name=os.path.join(path, 'mobilenet.npz')) diff --git a/tensorlayer/models/squeezenetv1.py b/tensorlayer/models/squeezenetv1.py index 2d30cb3b1..0884d3c47 100644 --- a/tensorlayer/models/squeezenetv1.py +++ b/tensorlayer/models/squeezenetv1.py @@ -161,9 +161,7 @@ def squeezenetv1(cls, x, end_with='output', is_train=False, reuse=None): def restore_params(self, sess, path='models'): logging.info("Restore pre-trained parameters") maybe_download_and_extract( - 'squeezenet.npz', - path, - 'https://github.com/tensorlayer/pretrained-models/raw/master/models/', + 'squeezenet.npz', path, 'https://github.com/tensorlayer/pretrained-models/raw/master/models/', expected_bytes=7405613 ) # ls -al params = load_npz(name=os.path.join(path, 'squeezenet.npz')) diff --git a/tensorlayer/nlp.py b/tensorlayer/nlp.py index 950a68093..a86e7ca2d 100755 --- a/tensorlayer/nlp.py +++ b/tensorlayer/nlp.py @@ -865,13 +865,8 @@ def basic_tokenizer(sentence, _WORD_SPLIT=re.compile(b"([.,!?\"':;)(])")): def create_vocabulary( - vocabulary_path, - data_path, - max_vocabulary_size, - tokenizer=None, - normalize_digits=True, - _DIGIT_RE=re.compile(br"\d"), - _START_VOCAB=None + vocabulary_path, data_path, max_vocabulary_size, tokenizer=None, normalize_digits=True, + _DIGIT_RE=re.compile(br"\d"), _START_VOCAB=None ): """Create vocabulary file (if it does not exist yet) from data file. @@ -978,7 +973,7 @@ def initialize_vocabulary(vocabulary_path): def sentence_to_token_ids( - sentence, vocabulary, tokenizer=None, normalize_digits=True, UNK_ID=3, _DIGIT_RE=re.compile(br"\d") + sentence, vocabulary, tokenizer=None, normalize_digits=True, UNK_ID=3, _DIGIT_RE=re.compile(br"\d") ): """Convert a string to list of integers representing token-ids. @@ -1014,13 +1009,8 @@ def sentence_to_token_ids( def data_to_token_ids( - data_path, - target_path, - vocabulary_path, - tokenizer=None, - normalize_digits=True, - UNK_ID=3, - _DIGIT_RE=re.compile(br"\d") + data_path, target_path, vocabulary_path, tokenizer=None, normalize_digits=True, UNK_ID=3, + _DIGIT_RE=re.compile(br"\d") ): """Tokenize data file and turn into token-ids using given vocabulary file. diff --git a/tensorlayer/prepro.py b/tensorlayer/prepro.py index 437e2d43c..283548b6f 100644 --- a/tensorlayer/prepro.py +++ b/tensorlayer/prepro.py @@ -199,7 +199,7 @@ def apply_fn(results, i, data, kwargs): def rotation( - x, rg=20, is_random=False, row_index=0, col_index=1, channel_index=2, fill_mode='nearest', cval=0., order=1 + x, rg=20, is_random=False, row_index=0, col_index=1, channel_index=2, fill_mode='nearest', cval=0., order=1 ): """Rotate an image randomly or non-randomly. @@ -245,7 +245,7 @@ def rotation( def rotation_multi( - x, rg=20, is_random=False, row_index=0, col_index=1, channel_index=2, fill_mode='nearest', cval=0., order=1 + x, rg=20, is_random=False, row_index=0, col_index=1, channel_index=2, fill_mode='nearest', cval=0., order=1 ): """Rotate multiple images with the same arguments, randomly or non-randomly. Usually be used for image segmentation which x=[X, Y], X and Y should be matched. @@ -449,16 +449,8 @@ def flip_axis_multi(x, axis, is_random=False): # shift def shift( - x, - wrg=0.1, - hrg=0.1, - is_random=False, - row_index=0, - col_index=1, - channel_index=2, - fill_mode='nearest', - cval=0., - order=1 + x, wrg=0.1, hrg=0.1, is_random=False, row_index=0, col_index=1, channel_index=2, fill_mode='nearest', cval=0., + order=1 ): """Shift an image randomly or non-randomly. @@ -501,16 +493,8 @@ def shift( def shift_multi( - x, - wrg=0.1, - hrg=0.1, - is_random=False, - row_index=0, - col_index=1, - channel_index=2, - fill_mode='nearest', - cval=0., - order=1 + x, wrg=0.1, hrg=0.1, is_random=False, row_index=0, col_index=1, channel_index=2, fill_mode='nearest', cval=0., + order=1 ): """Shift images with the same arguments, randomly or non-randomly. Usually be used for image segmentation which x=[X, Y], X and Y should be matched. @@ -545,7 +529,8 @@ def shift_multi( # shear def shear( - x, intensity=0.1, is_random=False, row_index=0, col_index=1, channel_index=2, fill_mode='nearest', cval=0., order=1 + x, intensity=0.1, is_random=False, row_index=0, col_index=1, channel_index=2, fill_mode='nearest', cval=0., + order=1 ): """Shear an image randomly or non-randomly. @@ -590,7 +575,8 @@ def shear( def shear_multi( - x, intensity=0.1, is_random=False, row_index=0, col_index=1, channel_index=2, fill_mode='nearest', cval=0., order=1 + x, intensity=0.1, is_random=False, row_index=0, col_index=1, channel_index=2, fill_mode='nearest', cval=0., + order=1 ): """Shear images with the same arguments, randomly or non-randomly. Usually be used for image segmentation which x=[X, Y], X and Y should be matched. @@ -623,15 +609,8 @@ def shear_multi( def shear2( - x, - shear=(0.1, 0.1), - is_random=False, - row_index=0, - col_index=1, - channel_index=2, - fill_mode='nearest', - cval=0., - order=1 + x, shear=(0.1, 0.1), is_random=False, row_index=0, col_index=1, channel_index=2, fill_mode='nearest', cval=0., + order=1 ): """Shear an image randomly or non-randomly. @@ -678,15 +657,8 @@ def shear2( def shear_multi2( - x, - shear=(0.1, 0.1), - is_random=False, - row_index=0, - col_index=1, - channel_index=2, - fill_mode='nearest', - cval=0., - order=1 + x, shear=(0.1, 0.1), is_random=False, row_index=0, col_index=1, channel_index=2, fill_mode='nearest', cval=0., + order=1 ): """Shear images with the same arguments, randomly or non-randomly. Usually be used for image segmentation which x=[X, Y], X and Y should be matched. @@ -723,18 +695,8 @@ def shear_multi2( # swirl def swirl( - x, - center=None, - strength=1, - radius=100, - rotation=0, - output_shape=None, - order=1, - mode='constant', - cval=0, - clip=True, - preserve_range=False, - is_random=False + x, center=None, strength=1, radius=100, rotation=0, output_shape=None, order=1, mode='constant', cval=0, + clip=True, preserve_range=False, is_random=False ): """Swirl an image randomly or non-randomly, see `scikit-image swirl API `__ and `example `__. @@ -796,17 +758,8 @@ def swirl( if max_v > 1: # Note: the input of this fn should be [-1, 1], rescale is required. x = x / max_v swirled = skimage.transform.swirl( - x, - center=center, - strength=strength, - radius=radius, - rotation=rotation, - output_shape=output_shape, - order=order, - mode=mode, - cval=cval, - clip=clip, - preserve_range=preserve_range + x, center=center, strength=strength, radius=radius, rotation=rotation, output_shape=output_shape, order=order, + mode=mode, cval=cval, clip=clip, preserve_range=preserve_range ) if max_v > 1: swirled = swirled * max_v @@ -814,18 +767,8 @@ def swirl( def swirl_multi( - x, - center=None, - strength=1, - radius=100, - rotation=0, - output_shape=None, - order=1, - mode='constant', - cval=0, - clip=True, - preserve_range=False, - is_random=False + x, center=None, strength=1, radius=100, rotation=0, output_shape=None, order=1, mode='constant', cval=0, + clip=True, preserve_range=False, is_random=False ): """Swirl multiple images with the same arguments, randomly or non-randomly. Usually be used for image segmentation which x=[X, Y], X and Y should be matched. @@ -859,17 +802,8 @@ def swirl_multi( if max_v > 1: # Note: the input of this fn should be [-1, 1], rescale is required. data = data / max_v swirled = skimage.transform.swirl( - data, - center=center, - strength=strength, - radius=radius, - rotation=rotation, - output_shape=output_shape, - order=order, - mode=mode, - cval=cval, - clip=clip, - preserve_range=preserve_range + data, center=center, strength=strength, radius=radius, rotation=rotation, output_shape=output_shape, + order=order, mode=mode, cval=cval, clip=clip, preserve_range=preserve_range ) if max_v > 1: swirled = swirled * max_v @@ -988,15 +922,8 @@ def elastic_transform_multi(x, alpha, sigma, mode="constant", cval=0, is_random= # zoom def zoom( - x, - zoom_range=(0.9, 1.1), - is_random=False, - row_index=0, - col_index=1, - channel_index=2, - fill_mode='nearest', - cval=0., - order=1 + x, zoom_range=(0.9, 1.1), is_random=False, row_index=0, col_index=1, channel_index=2, fill_mode='nearest', + cval=0., order=1 ): """Zoom in and out of a single image, randomly or non-randomly. @@ -1045,15 +972,8 @@ def zoom( def zoom_multi( - x, - zoom_range=(0.9, 1.1), - is_random=False, - row_index=0, - col_index=1, - channel_index=2, - fill_mode='nearest', - cval=0., - order=1 + x, zoom_range=(0.9, 1.1), is_random=False, row_index=0, col_index=1, channel_index=2, fill_mode='nearest', + cval=0., order=1 ): """Zoom in and out of images with the same arguments, randomly or non-randomly. Usually be used for image segmentation which x=[X, Y], X and Y should be matched. @@ -1466,7 +1386,7 @@ def pixel_value_scale(im, val=0.9, clip=(-np.inf, np.inf), is_random=False): # normailization def samplewise_norm( - x, rescale=None, samplewise_center=False, samplewise_std_normalization=False, channel_index=2, epsilon=1e-7 + x, rescale=None, samplewise_center=False, samplewise_std_normalization=False, channel_index=2, epsilon=1e-7 ): """Normalize an image by rescale, samplewise centering and samplewise centering in order. @@ -1811,7 +1731,8 @@ def apply_transform(x, transform_matrix, channel_index=2, fill_mode='nearest', c def projective_transform_by_points( - x, src, dst, map_args=None, output_shape=None, order=1, mode='constant', cval=0.0, clip=True, preserve_range=False + x, src, dst, map_args=None, output_shape=None, order=1, mode='constant', cval=0.0, clip=True, + preserve_range=False ): """Projective transform by given coordinates, usually 4 coordinates. @@ -1880,14 +1801,7 @@ def projective_transform_by_points( m = transform.ProjectiveTransform() m.estimate(dst, src) warped = transform.warp( - x, - m, - map_args=map_args, - output_shape=output_shape, - order=order, - mode=mode, - cval=cval, - clip=clip, + x, m, map_args=map_args, output_shape=output_shape, order=order, mode=mode, cval=cval, clip=clip, preserve_range=preserve_range ) return warped @@ -2556,16 +2470,8 @@ def obj_box_imresize(im, coords=None, size=None, interp='bicubic', mode=None, is def obj_box_crop( - im, - classes=None, - coords=None, - wrg=100, - hrg=100, - is_rescale=False, - is_center=False, - is_random=False, - thresh_wh=0.02, - thresh_wh2=12. + im, classes=None, coords=None, wrg=100, hrg=100, is_rescale=False, is_center=False, is_random=False, + thresh_wh=0.02, thresh_wh2=12. ): """Randomly or centrally crop an image, and compute the new bounding box coordinates. Objects outside the cropped image will be removed. @@ -2711,22 +2617,8 @@ def _get_coord(coord): def obj_box_shift( - im, - classes=None, - coords=None, - wrg=0.1, - hrg=0.1, - row_index=0, - col_index=1, - channel_index=2, - fill_mode='nearest', - cval=0., - order=1, - is_rescale=False, - is_center=False, - is_random=False, - thresh_wh=0.02, - thresh_wh2=12. + im, classes=None, coords=None, wrg=0.1, hrg=0.1, row_index=0, col_index=1, channel_index=2, fill_mode='nearest', + cval=0., order=1, is_rescale=False, is_center=False, is_random=False, thresh_wh=0.02, thresh_wh2=12. ): """Shift an image randomly or non-randomly, and compute the new bounding box coordinates. Objects outside the cropped image will be removed. @@ -2854,21 +2746,9 @@ def _get_coord(coord): def obj_box_zoom( - im, - classes=None, - coords=None, - zoom_range=(0.9, 1.1), - row_index=0, - col_index=1, - channel_index=2, - fill_mode='nearest', - cval=0., - order=1, - is_rescale=False, - is_center=False, - is_random=False, - thresh_wh=0.02, - thresh_wh2=12. + im, classes=None, coords=None, zoom_range=(0.9, + 1.1), row_index=0, col_index=1, channel_index=2, fill_mode='nearest', + cval=0., order=1, is_rescale=False, is_center=False, is_random=False, thresh_wh=0.02, thresh_wh2=12. ): """Zoom in and out of a single image, randomly or non-randomly, and compute the new bounding box coordinates. Objects outside the cropped image will be removed. diff --git a/tensorlayer/utils.py b/tensorlayer/utils.py index 456062013..e316ca0d7 100644 --- a/tensorlayer/utils.py +++ b/tensorlayer/utils.py @@ -33,25 +33,9 @@ def fit( - sess, - network, - train_op, - cost, - X_train, - y_train, - x, - y_, - acc=None, - batch_size=100, - n_epoch=100, - print_freq=5, - X_val=None, - y_val=None, - eval_train=True, - tensorboard=False, - tensorboard_epoch_freq=5, - tensorboard_weight_histograms=True, - tensorboard_graph_vis=True + sess, network, train_op, cost, X_train, y_train, x, y_, acc=None, batch_size=100, n_epoch=100, print_freq=5, + X_val=None, y_val=None, eval_train=True, tensorboard=False, tensorboard_epoch_freq=5, + tensorboard_weight_histograms=True, tensorboard_graph_vis=True ): """Training a given non time-series network by the given cost function, training data, batch_size, n_epoch etc. @@ -572,9 +556,8 @@ def exit_tensorflow(sess=None, port=6006): _exit() elif _platform == "darwin": logging.info('OS X: %s' % text) - subprocess.Popen( - "lsof -i tcp:" + str(port) + " | grep -v PID | awk '{print $2}' | xargs kill", shell=True - ) # kill tensorboard + subprocess.Popen("lsof -i tcp:" + str(port) + " | grep -v PID | awk '{print $2}' | xargs kill", + shell=True) # kill tensorboard elif _platform == "win32": raise NotImplementedError("this function is not supported on the Windows platform") else: diff --git a/tensorlayer/visualize.py b/tensorlayer/visualize.py index c459f7188..296980299 100644 --- a/tensorlayer/visualize.py +++ b/tensorlayer/visualize.py @@ -142,7 +142,7 @@ def imsave(images, size, path): def draw_boxes_and_labels_to_image( - image, classes, coords, scores, classes_list, is_center=True, is_rescale=True, save_name=None + image, classes, coords, scores, classes_list, is_center=True, is_rescale=True, save_name=None ): """Draw bboxes and class labels on image. Return or save the image with bboxes, example in the docs of ``tl.prepro``. @@ -634,9 +634,8 @@ def draw_weights(W=None, second=10, saveable=True, shape=None, name='mnist', fig # feature = np.zeros_like(feature) # if np.mean(feature) < -0.015: # condition threshold # feature = np.zeros_like(feature) - plt.imshow( - np.reshape(feature, (shape[0], shape[1])), cmap='gray', interpolation="nearest" - ) #, vmin=np.min(feature), vmax=np.max(feature)) + plt.imshow(np.reshape(feature, (shape[0], shape[1])), cmap='gray', + interpolation="nearest") #, vmin=np.min(feature), vmax=np.max(feature)) # plt.title(name) # ------------------------------------------------------------ # plt.imshow(np.reshape(W[:,count-1] ,(np.sqrt(size),np.sqrt(size))), cmap='gray', interpolation="nearest") diff --git a/tests/test_layers_convolution.py b/tests/test_layers_convolution.py index d43a7f97c..e49c2b046 100644 --- a/tests/test_layers_convolution.py +++ b/tests/test_layers_convolution.py @@ -31,13 +31,8 @@ def setUpClass(cls): nin2 = tl.layers.InputLayer(x2, name='in2') n3 = tl.layers.Conv2dLayer( - nin2, - act=tf.nn.relu, - shape=(5, 5, 3, 32), - strides=(1, 2, 2, 1), - padding='SAME', - W_init=tf.truncated_normal_initializer(stddev=5e-2), - b_init=tf.constant_initializer(value=0.0), + nin2, act=tf.nn.relu, shape=(5, 5, 3, 32), strides=(1, 2, 2, 1), padding='SAME', + W_init=tf.truncated_normal_initializer(stddev=5e-2), b_init=tf.constant_initializer(value=0.0), name='conv2dlayer' ) cls.shape_n3 = n3.outputs.get_shape().as_list() diff --git a/tests/test_layers_core.py b/tests/test_layers_core.py index fab48d50a..fb0cfb956 100644 --- a/tests/test_layers_core.py +++ b/tests/test_layers_core.py @@ -45,11 +45,7 @@ def setUpClass(cls): train_inputs = tf.placeholder(tf.int32, shape=cls.batch_size) train_labels = tf.placeholder(tf.int32, shape=(cls.batch_size, 1)) net3 = tl.layers.Word2vecEmbeddingInputlayer( - inputs=train_inputs, - train_labels=train_labels, - vocabulary_size=1000, - embedding_size=200, - num_sampled=64, + inputs=train_inputs, train_labels=train_labels, vocabulary_size=1000, embedding_size=200, num_sampled=64, name='word2vec' ) diff --git a/tests/test_layers_recurrent.py b/tests/test_layers_recurrent.py index 17df26d26..4cc13bd73 100644 --- a/tests/test_layers_recurrent.py +++ b/tests/test_layers_recurrent.py @@ -36,24 +36,16 @@ def setUpClass(cls): ) net1 = tl.layers.DropoutLayer(net1, keep=cls.keep_prob, is_fix=True, is_train=cls.is_train, name='drop1') net1 = tl.layers.RNNLayer( - net1, - cell_fn=tf.contrib.rnn.BasicLSTMCell, - n_hidden=cls.hidden_size, - n_steps=cls.num_steps, - return_last=False, - name='lstm1' + net1, cell_fn=tf.contrib.rnn.BasicLSTMCell, n_hidden=cls.hidden_size, n_steps=cls.num_steps, + return_last=False, name='lstm1' ) # lstm1 = net1 net1 = tl.layers.DropoutLayer(net1, keep=cls.keep_prob, is_fix=True, is_train=cls.is_train, name='drop2') net1 = tl.layers.RNNLayer( - net1, - cell_fn=tf.contrib.rnn.BasicLSTMCell, - n_hidden=cls.hidden_size, - n_steps=cls.num_steps, - return_last=True, - name='lstm2' + net1, cell_fn=tf.contrib.rnn.BasicLSTMCell, n_hidden=cls.hidden_size, n_steps=cls.num_steps, + return_last=True, name='lstm2' ) # lstm2 = net1 @@ -83,13 +75,8 @@ def setUpClass(cls): net2 = tl.layers.ReshapeLayer(net2, shape=(-1, cls.num_steps, int(net2.outputs._shape[-1]))) rnn = tl.layers.RNNLayer( - net2, - cell_fn=tf.contrib.rnn.BasicLSTMCell, - n_hidden=200, - n_steps=cls.num_steps, - return_last=False, - return_seq_2d=True, - name='rnn' + net2, cell_fn=tf.contrib.rnn.BasicLSTMCell, n_hidden=200, n_steps=cls.num_steps, return_last=False, + return_seq_2d=True, name='rnn' ) net2 = tl.layers.DenseLayer(rnn, n_units=3, name='out') @@ -110,13 +97,8 @@ def setUpClass(cls): inputs=x3, vocabulary_size=cls.vocab_size, embedding_size=cls.hidden_size, name='emb' ) net3 = tl.layers.BiRNNLayer( - net3, - cell_fn=tf.contrib.rnn.BasicLSTMCell, - n_hidden=cls.hidden_size, - n_steps=cls.num_steps, - return_last=False, - return_seq_2d=False, - name='birnn' + net3, cell_fn=tf.contrib.rnn.BasicLSTMCell, n_hidden=cls.hidden_size, n_steps=cls.num_steps, + return_last=False, return_seq_2d=False, name='birnn' ) net3.print_layers() @@ -132,14 +114,8 @@ def setUpClass(cls): inputs=x3, vocabulary_size=cls.vocab_size, embedding_size=cls.hidden_size, name='emb2' ) net4 = tl.layers.BiRNNLayer( - net4, - cell_fn=tf.contrib.rnn.BasicLSTMCell, - n_hidden=cls.hidden_size, - n_steps=cls.num_steps, - n_layer=2, - return_last=False, - return_seq_2d=False, - name='birnn2' + net4, cell_fn=tf.contrib.rnn.BasicLSTMCell, n_hidden=cls.hidden_size, n_steps=cls.num_steps, n_layer=2, + return_last=False, return_seq_2d=False, name='birnn2' ) net4.print_layers() @@ -175,14 +151,10 @@ def setUpClass(cls): ) rnn = tl.layers.DynamicRNNLayer( - nin, - cell_fn=tf.contrib.rnn.BasicLSTMCell, - n_hidden=cls.embedding_size, - dropout=(cls.keep_prob if cls.is_train else None), - sequence_length=tl.layers.retrieve_seq_length_op2(input_seqs), - return_last=False, - return_seq_2d=True, - name='dynamicrnn' + nin, cell_fn=tf.contrib.rnn.BasicLSTMCell, n_hidden=cls.embedding_size, + dropout=(cls.keep_prob + if cls.is_train else None), sequence_length=tl.layers.retrieve_seq_length_op2(input_seqs), + return_last=False, return_seq_2d=True, name='dynamicrnn' ) net5 = tl.layers.DenseLayer(rnn, n_units=cls.vocab_size, name="o") @@ -201,29 +173,18 @@ def setUpClass(cls): inputs=input_seqs, vocabulary_size=cls.vocab_size, embedding_size=cls.embedding_size, name='seq_embedding2' ) rnn = tl.layers.DynamicRNNLayer( - nin, - cell_fn=tf.contrib.rnn.BasicLSTMCell, - n_hidden=cls.embedding_size, - dropout=(cls.keep_prob if cls.is_train else None), - sequence_length=tl.layers.retrieve_seq_length_op2(input_seqs), - n_layer=3, - return_last=False, - return_seq_2d=True, - name='dynamicrnn2' + nin, cell_fn=tf.contrib.rnn.BasicLSTMCell, n_hidden=cls.embedding_size, + dropout=(cls.keep_prob + if cls.is_train else None), sequence_length=tl.layers.retrieve_seq_length_op2(input_seqs), + n_layer=3, return_last=False, return_seq_2d=True, name='dynamicrnn2' ) # net6 = tl.layers.DenseLayer(rnn, n_units=cls.vocab_size, name="o2") net6 = tl.layers.DynamicRNNLayer( - nin, - cell_fn=tf.contrib.rnn.BasicLSTMCell, - n_hidden=cls.embedding_size, - dropout=None, - sequence_length=tl.layers.retrieve_seq_length_op2(input_seqs), - n_layer=3, - return_last=False, - return_seq_2d=False, - name='dynamicrnn3' + nin, cell_fn=tf.contrib.rnn.BasicLSTMCell, n_hidden=cls.embedding_size, dropout=None, + sequence_length=tl.layers.retrieve_seq_length_op2(input_seqs), n_layer=3, return_last=False, + return_seq_2d=False, name='dynamicrnn3' ) # net6 = tl.layers.DenseLayer(rnn, n_units=vocab_size, name="o3") @@ -235,15 +196,9 @@ def setUpClass(cls): cls.net6_rnn_shape = rnn.outputs.get_shape().as_list() net7 = tl.layers.DynamicRNNLayer( - nin, - cell_fn=tf.contrib.rnn.BasicLSTMCell, - n_hidden=cls.embedding_size, - dropout=None, - sequence_length=tl.layers.retrieve_seq_length_op2(input_seqs), - n_layer=1, - return_last=True, - return_seq_2d=False, - name='dynamicrnn4' + nin, cell_fn=tf.contrib.rnn.BasicLSTMCell, n_hidden=cls.embedding_size, dropout=None, + sequence_length=tl.layers.retrieve_seq_length_op2(input_seqs), n_layer=1, return_last=True, + return_seq_2d=False, name='dynamicrnn4' ) net7.print_layers() @@ -252,15 +207,9 @@ def setUpClass(cls): cls.net7_shape = net7.outputs.get_shape().as_list() net8 = tl.layers.DynamicRNNLayer( - nin, - cell_fn=tf.contrib.rnn.BasicLSTMCell, - n_hidden=cls.embedding_size, - dropout=None, - sequence_length=tl.layers.retrieve_seq_length_op2(input_seqs), - n_layer=1, - return_last=True, - return_seq_2d=True, - name='dynamicrnn5' + nin, cell_fn=tf.contrib.rnn.BasicLSTMCell, n_hidden=cls.embedding_size, dropout=None, + sequence_length=tl.layers.retrieve_seq_length_op2(input_seqs), n_layer=1, return_last=True, + return_seq_2d=True, name='dynamicrnn5' ) net8.print_layers() @@ -271,14 +220,10 @@ def setUpClass(cls): # =============================== BiDynamic Synced input and output =============================== rnn = tl.layers.BiDynamicRNNLayer( - nin, - cell_fn=tf.contrib.rnn.BasicLSTMCell, - n_hidden=cls.embedding_size, - dropout=(cls.keep_prob if cls.is_train else None), - sequence_length=tl.layers.retrieve_seq_length_op2(input_seqs), - return_last=False, - return_seq_2d=True, - name='bidynamicrnn' + nin, cell_fn=tf.contrib.rnn.BasicLSTMCell, n_hidden=cls.embedding_size, + dropout=(cls.keep_prob + if cls.is_train else None), sequence_length=tl.layers.retrieve_seq_length_op2(input_seqs), + return_last=False, return_seq_2d=True, name='bidynamicrnn' ) net9 = tl.layers.DenseLayer(rnn, n_units=cls.vocab_size, name="o4") @@ -294,15 +239,10 @@ def setUpClass(cls): # n_layer=2 rnn = tl.layers.BiDynamicRNNLayer( - nin, - cell_fn=tf.contrib.rnn.BasicLSTMCell, - n_hidden=cls.embedding_size, - dropout=(cls.keep_prob if cls.is_train else None), - sequence_length=tl.layers.retrieve_seq_length_op2(input_seqs), - n_layer=2, - return_last=False, - return_seq_2d=True, - name='bidynamicrnn2' + nin, cell_fn=tf.contrib.rnn.BasicLSTMCell, n_hidden=cls.embedding_size, + dropout=(cls.keep_prob + if cls.is_train else None), sequence_length=tl.layers.retrieve_seq_length_op2(input_seqs), + n_layer=2, return_last=False, return_seq_2d=True, name='bidynamicrnn2' ) net10 = tl.layers.DenseLayer(rnn, n_units=cls.vocab_size, name="o5") @@ -338,18 +278,11 @@ def setUpClass(cls): ) net11 = tl.layers.Seq2Seq( - net_encode, - net_decode, - cell_fn=tf.contrib.rnn.BasicLSTMCell, - n_hidden=200, + net_encode, net_decode, cell_fn=tf.contrib.rnn.BasicLSTMCell, n_hidden=200, initializer=tf.random_uniform_initializer(-0.1, 0.1), encode_sequence_length=tl.layers.retrieve_seq_length_op2(encode_seqs), - decode_sequence_length=tl.layers.retrieve_seq_length_op2(decode_seqs), - initial_state_encode=None, - dropout=None, - n_layer=2, - return_seq_2d=True, - name='Seq2seq' + decode_sequence_length=tl.layers.retrieve_seq_length_op2(decode_seqs), initial_state_encode=None, + dropout=None, n_layer=2, return_seq_2d=True, name='Seq2seq' ) net11 = tl.layers.DenseLayer(net11, n_units=10000, act=tf.identity, name='oo') diff --git a/tests/test_mnist_simple.py b/tests/test_mnist_simple.py index a57e21f43..eec717f64 100644 --- a/tests/test_mnist_simple.py +++ b/tests/test_mnist_simple.py @@ -70,21 +70,8 @@ def test_reuse_vgg(self): # train the network tl.utils.fit( - sess, - self.network, - self.train_op, - self.cost, - X_train, - y_train, - self.x, - self.y_, - acc=self.acc, - batch_size=500, - n_epoch=1, - print_freq=1, - X_val=X_val, - y_val=y_val, - eval_train=False + sess, self.network, self.train_op, self.cost, X_train, y_train, self.x, self.y_, acc=self.acc, + batch_size=500, n_epoch=1, print_freq=1, X_val=X_val, y_val=y_val, eval_train=False ) # evaluation From 240f0df0a9eff7d3722bcc09ed819aa20a810386 Mon Sep 17 00:00:00 2001 From: DEKHTIARJonathan Date: Fri, 20 Apr 2018 21:03:46 +0200 Subject: [PATCH 20/22] Assertion Codacy Errors Corrected --- tensorlayer/prepro.py | 101 +++++++++++++++++++++++++++++++----------- 1 file changed, 75 insertions(+), 26 deletions(-) diff --git a/tensorlayer/prepro.py b/tensorlayer/prepro.py index 283548b6f..aab5e2444 100644 --- a/tensorlayer/prepro.py +++ b/tensorlayer/prepro.py @@ -308,7 +308,10 @@ def crop(x, wrg, hrg, is_random=False, row_index=0, col_index=1): """ h, w = x.shape[row_index], x.shape[col_index] - assert (h > hrg) and (w > wrg), "The size of cropping should smaller than the original image" + + if (h <= hrg) or (w <= wrg): + raise AssertionError("The size of cropping should smaller than the original image") + if is_random: h_offset = int(np.random.uniform(0, h - hrg) - 1) w_offset = int(np.random.uniform(0, w - wrg) - 1) @@ -345,7 +348,10 @@ def crop_multi(x, wrg, hrg, is_random=False, row_index=0, col_index=1): """ h, w = x[0].shape[row_index], x[0].shape[col_index] - assert (h > hrg) and (w > wrg), "The size of cropping should smaller than the original image" + + if (h <= hrg) or (w <= wrg): + raise AssertionError("The size of cropping should smaller than the original image") + if is_random: h_offset = int(np.random.uniform(0, h - hrg) - 1) w_offset = int(np.random.uniform(0, w - wrg) - 1) @@ -641,9 +647,11 @@ def shear2( - `Affine transformation `__ """ - assert len( - shear - ) == 2, "shear should be tuple of 2 floats, or you want to use tl.prepro.shear rather than tl.prepro.shear2 ?" + if len(shear) != 2: + raise AssertionError( + "shear should be tuple of 2 floats, or you want to use tl.prepro.shear rather than tl.prepro.shear2 ?" + ) + if is_random: shear[0] = np.random.uniform(-shear[0], shear[0]) shear[1] = np.random.uniform(-shear[1], shear[1]) @@ -676,9 +684,11 @@ def shear_multi2( A list of processed images. """ - assert len( - shear - ) == 2, "shear should be tuple of 2 floats, or you want to use tl.prepro.shear_multi rather than tl.prepro.shear_multi2 ?" + if len(shear) != 2: + raise AssertionError( + "shear should be tuple of 2 floats, or you want to use tl.prepro.shear_multi rather than tl.prepro.shear_multi2 ?" + ) + if is_random: shear[0] = np.random.uniform(-shear[0], shear[0]) shear[1] = np.random.uniform(-shear[1], shear[1]) @@ -744,7 +754,9 @@ def swirl( >>> x = tl.prepro.swirl(x, strength=4, radius=100) """ - assert radius != 0, Exception("Invalid radius value") + if radius == 0: + raise AssertionError("Invalid radius value") + rotation = np.pi / 180 * rotation if is_random: center_h = int(np.random.uniform(0, x.shape[0])) @@ -786,7 +798,9 @@ def swirl_multi( A list of processed images. """ - assert radius != 0, Exception("Invalid radius value") + if radius == 0: + raise AssertionError("Invalid radius value") + rotation = np.pi / 180 * rotation if is_random: center_h = int(np.random.uniform(0, x[0].shape[0])) @@ -856,7 +870,9 @@ def elastic_transform(x, alpha, sigma, mode="constant", cval=0, is_random=False) is_3d = True elif len(x.shape) == 3 and x.shape[-1] != 1: raise Exception("Only support greyscale image") - assert len(x.shape) == 2, "input should be grey-scale image" + + if len(x.shape) != 2: + raise AssertionError("input should be grey-scale image") shape = x.shape @@ -905,7 +921,9 @@ def elastic_transform_multi(x, alpha, sigma, mode="constant", cval=0, is_random= is_3d = True elif len(data.shape) == 3 and data.shape[-1] != 1: raise Exception("Only support greyscale image") - assert len(data.shape) == 2, "input should be grey-scale image" + + if len(data.shape) != 2: + raise AssertionError("input should be grey-scale image") dx = gaussian_filter((new_shape * 2 - 1), sigma, mode=mode, cval=cval) * alpha dy = gaussian_filter((new_shape * 2 - 1), sigma, mode=mode, cval=cval) * alpha @@ -2046,7 +2064,10 @@ def obj_box_coords_rescale(coords=None, shape=None): imw = imw * 1.0 coords_new = list() for coord in coords: - assert len(coord) == 4, "coordinate should be 4 values : [x, y, w, h]" + + if len(coord) != 4: + raise AssertionError("coordinate should be 4 values : [x, y, w, h]") + x = coord[0] / imw y = coord[1] / imh w = coord[2] / imw @@ -2151,7 +2172,10 @@ def obj_box_coord_centroid_to_upleft_butright(coord, to_int=False): ... [20, 30, 40, 50] """ - assert len(coord) == 4, "coordinate should be 4 values : [x, y, w, h]" + + if len(coord) != 4: + raise AssertionError("coordinate should be 4 values : [x, y, w, h]") + x_center, y_center, w, h = coord x = x_center - w / 2. y = y_center - h / 2. @@ -2183,7 +2207,8 @@ def obj_box_coord_upleft_butright_to_centroid(coord): New bounding box. """ - assert len(coord) == 4, "coordinate should be 4 values : [x1, y1, x2, y2]" + if len(coord) != 4: + raise AssertionError("coordinate should be 4 values : [x1, y1, x2, y2]") x1, y1, x2, y2 = coord w = x2 - x1 h = y2 - y1 @@ -2207,7 +2232,9 @@ def obj_box_coord_centroid_to_upleft(coord): New bounding box. """ - assert len(coord) == 4, "coordinate should be 4 values : [x, y, w, h]" + if len(coord) != 4: + raise AssertionError("coordinate should be 4 values : [x, y, w, h]") + x_center, y_center, w, h = coord x = x_center - w / 2. y = y_center - h / 2. @@ -2229,7 +2256,9 @@ def obj_box_coord_upleft_to_centroid(coord): New bounding box. """ - assert len(coord) == 4, "coordinate should be 4 values : [x, y, w, h]" + if len(coord) != 4: + raise AssertionError("coordinate should be 4 values : [x, y, w, h]") + x, y, w, h = coord x_center = x + w / 2. y_center = y + h / 2. @@ -2340,7 +2369,10 @@ def _flip(im, coords): coords_new = list() for coord in coords: - assert len(coord) == 4, "coordinate should be 4 values : [x, y, w, h]" + + if len(coord) != 4: + raise AssertionError("coordinate should be 4 values : [x, y, w, h]") + if is_rescale: if is_center: # x_center' = 1 - x @@ -2436,8 +2468,12 @@ def obj_box_imresize(im, coords=None, size=None, interp='bicubic', mode=None, is if is_rescale is False: coords_new = list() + for coord in coords: - assert len(coord) == 4, "coordinate should be 4 values : [x, y, w, h]" + + if len(coord) != 4: + raise AssertionError("coordinate should be 4 values : [x, y, w, h]") + # x' = x * (imw'/imw) x = int(coord[0] * (size[1] / imw)) # y' = y * (imh'/imh) @@ -2511,7 +2547,10 @@ def obj_box_crop( coords = [] h, w = im.shape[0], im.shape[1] - assert (h > hrg) and (w > wrg), "The size of cropping should smaller than the original image" + + if (h <= hrg) or (w <= wrg): + raise AssertionError("The size of cropping should smaller than the original image") + if is_random: h_offset = int(np.random.uniform(0, h - hrg) - 1) w_offset = int(np.random.uniform(0, w - wrg) - 1) @@ -2599,7 +2638,10 @@ def _get_coord(coord): classes_new = list() for i, _ in enumerate(coords): coord = coords[i] - assert len(coord) == 4, "coordinate should be 4 values : [x, y, w, h]" + + if len(coord) != 4: + raise AssertionError("coordinate should be 4 values : [x, y, w, h]") + if is_rescale: # for scaled coord, upscaled before process and scale back in the end. coord = obj_box_coord_scale_to_pixelunit(coord, im.shape) @@ -2658,7 +2700,10 @@ def obj_box_shift( coords = [] imh, imw = im.shape[row_index], im.shape[col_index] - assert (hrg < 1.0) and (hrg > 0.) and (wrg < 1.0) and (wrg > 0.), "shift range should be (0, 1)" + + if (hrg >= 1.0) and (hrg <= 0.) and (wrg >= 1.0) and (wrg <= 0.): + raise AssertionError("shift range should be (0, 1)") + if is_random: tx = np.random.uniform(-hrg, hrg) * imh ty = np.random.uniform(-wrg, wrg) * imw @@ -2727,8 +2772,10 @@ def _get_coord(coord): coords_new = list() classes_new = list() for i, _ in enumerate(coords): - coord = coords[i] - assert len(coord) == 4, "coordinate should be 4 values : [x, y, w, h]" + + if len(coord) != 4: + raise AssertionError("coordinate should be 4 values : [x, y, w, h]") + if is_rescale: # for scaled coord, upscaled before process and scale back in the end. coord = obj_box_coord_scale_to_pixelunit(coord, im.shape) @@ -2861,8 +2908,10 @@ def _get_coord(coord): coords_new = list() classes_new = list() for i, _ in enumerate(coords): - coord = coords[i] - assert len(coord) == 4, "coordinate should be 4 values : [x, y, w, h]" + + if len(coord) != 4: + raise AssertionError("coordinate should be 4 values : [x, y, w, h]") + if is_rescale: # for scaled coord, upscaled before process and scale back in the end. coord = obj_box_coord_scale_to_pixelunit(coord, im.shape) From 2c97f69628dab755164907741b7e467e5f5ed0eb Mon Sep 17 00:00:00 2001 From: DEKHTIARJonathan Date: Fri, 20 Apr 2018 21:08:21 +0200 Subject: [PATCH 21/22] Error Fix --- tensorlayer/prepro.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tensorlayer/prepro.py b/tensorlayer/prepro.py index aab5e2444..b293b1e17 100644 --- a/tensorlayer/prepro.py +++ b/tensorlayer/prepro.py @@ -2772,6 +2772,7 @@ def _get_coord(coord): coords_new = list() classes_new = list() for i, _ in enumerate(coords): + coord = coords[i] if len(coord) != 4: raise AssertionError("coordinate should be 4 values : [x, y, w, h]") @@ -2908,6 +2909,7 @@ def _get_coord(coord): coords_new = list() classes_new = list() for i, _ in enumerate(coords): + coord = coords[i] if len(coord) != 4: raise AssertionError("coordinate should be 4 values : [x, y, w, h]") From 9966722b8760cf0f83ca9d00a78d6c1406f149e5 Mon Sep 17 00:00:00 2001 From: DEKHTIARJonathan Date: Fri, 20 Apr 2018 21:26:12 +0200 Subject: [PATCH 22/22] Assertion Refactored --- tensorlayer/visualize.py | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/tensorlayer/visualize.py b/tensorlayer/visualize.py index 296980299..4e7f44133 100644 --- a/tensorlayer/visualize.py +++ b/tensorlayer/visualize.py @@ -134,10 +134,9 @@ def merge(images, size): def imsave(images, size, path): return scipy.misc.imsave(path, merge(images, size)) - assert len(images) <= size[0] * size[1 - ], "number of images should be equal or less than size[0] * size[1] {}".format( - len(images) - ) + if len(images) > size[0] * size[1]: + raise AssertionError("number of images should be equal or less than size[0] * size[1] {}".format(len(images))) + return imsave(images, size, image_path) @@ -182,11 +181,13 @@ def draw_boxes_and_labels_to_image( - `scikit-image `__. """ - assert len(coords) == len(classes), "number of coordinates and classes are equal" - if len(scores) > 0: - assert len(scores) == len(classes), "number of scores and classes are equal" + if len(coords) != len(classes): + raise AssertionError("number of coordinates and classes are equal") - import cv2 + if len(scores) > 0 and len(scores) != len(classes): + raise AssertionError("number of scores and classes are equal") + + import cv2 # TODO: OpenCV is not in the requirements. # don't change the original image, and avoid error https://stackoverflow.com/questions/30249053/python-opencv-drawing-errors-after-manipulating-array-with-numpy image = image.copy() @@ -554,15 +555,21 @@ def tsne_embedding(embeddings, reverse_dictionary, plot_only=500, second=5, save import matplotlib.pyplot as plt def plot_with_labels(low_dim_embs, labels, figsize=(18, 18), second=5, saveable=True, name='tsne', fig_idx=9862): - assert low_dim_embs.shape[0] >= len(labels), "More labels than embeddings" + + if low_dim_embs.shape[0] < len(labels): + raise AssertionError("More labels than embeddings") + if saveable is False: plt.ion() plt.figure(fig_idx) + plt.figure(figsize=figsize) #in inches + for i, label in enumerate(labels): x, y = low_dim_embs[i, :] plt.scatter(x, y) plt.annotate(label, xy=(x, y), xytext=(5, 2), textcoords='offset points', ha='right', va='bottom') + if saveable: plt.savefig(name + '.pdf', format='pdf') else: