From 152b2f24ca5a313d9e297fd43e88a27677a4da57 Mon Sep 17 00:00:00 2001 From: Luo Mai Date: Fri, 23 Feb 2018 15:33:35 +0800 Subject: [PATCH 01/10] fix pylint default argument issues. --- tensorlayer/db.py | 63 ++++++---- tensorlayer/files.py | 44 ++++--- tensorlayer/layers/convolution.py | 143 +++++++++++++++------- tensorlayer/layers/core.py | 40 ++++-- tensorlayer/layers/importer.py | 26 ++-- tensorlayer/layers/recurrent.py | 15 ++- tensorlayer/layers/spatial_transformer.py | 7 +- tensorlayer/layers/special_activation.py | 5 +- tensorlayer/layers/time_distribution.py | 4 +- tensorlayer/nlp.py | 20 ++- tensorlayer/prepro.py | 95 ++++++++++---- tensorlayer/visualize.py | 16 +-- 12 files changed, 332 insertions(+), 146 deletions(-) diff --git a/tensorlayer/db.py b/tensorlayer/db.py index 15ff35b1a..c634017cf 100644 --- a/tensorlayer/db.py +++ b/tensorlayer/db.py @@ -12,8 +12,6 @@ import uuid from datetime import datetime -import numpy as np - import gridfs from pymongo import MongoClient @@ -96,7 +94,7 @@ def __serialization(self, ps): def __deserialization(self, ps): return pickle.loads(ps) - def save_params(self, params=[], args={}): #, file_name='parameters'): + def save_params(self, params=None, args=None): #, file_name='parameters'): """ Save parameters into MongoDB Buckets, and save the file ID into Params Collections. Parameters @@ -108,6 +106,10 @@ def save_params(self, params=[], args={}): #, file_name='parameters'): --------- f_id : the Buckets ID of the parameters. """ + if params is None: + params = [] + if args is None: + args = {} self.__autofill(args) s = time.time() f_id = self.paramsfs.put(self.__serialization(params)) #, file_name=file_name) @@ -118,7 +120,7 @@ def save_params(self, params=[], args={}): #, file_name='parameters'): return f_id @AutoFill - def find_one_params(self, args={}, sort=None): + def find_one_params(self, args=None, sort=None): """ Find one parameter from MongoDB Buckets. Parameters @@ -131,7 +133,8 @@ def find_one_params(self, args={}, sort=None): params : the parameters, return False if nothing found. f_id : the Buckets ID of the parameters, return False if nothing found. """ - + if args is None: + args = {} s = time.time() # print(args) d = self.db.Params.find_one(filter=args, sort=sort) @@ -149,7 +152,7 @@ def find_one_params(self, args={}, sort=None): return False, False @AutoFill - def find_all_params(self, args={}): + def find_all_params(self, args=None): """ Find all parameter from MongoDB Buckets Parameters @@ -161,7 +164,8 @@ def find_all_params(self, args={}): params : the parameters, return False if nothing found. """ - + if args is None: + args = {} s = time.time() pc = self.db.Params.find(args) @@ -179,14 +183,15 @@ def find_all_params(self, args={}): return params @AutoFill - def del_params(self, args={}): + def del_params(self, args=None): """ Delete params in MongoDB uckets. Parameters ----------- args : dictionary, find items to delete, leave it empty to delete all parameters. """ - + if args is None: + args = {} pc = self.db.Params.find(args) f_id_list = pc.distinct('f_id') # remove from Buckets @@ -208,7 +213,7 @@ def _print_dict(self, args): ## =========================== LOG =================================== ## @AutoFill - def train_log(self, args={}): + def train_log(self, args=None): """Save the training log. Parameters @@ -219,26 +224,28 @@ def train_log(self, args={}): --------- >>> db.train_log(time=time.time(), {'loss': loss, 'acc': acc}) """ - + if args is None: + args = {} _result = self.db.TrainLog.insert_one(args) _log = self._print_dict(args) #print("[TensorDB] TrainLog: " +_log) return _result @AutoFill - def del_train_log(self, args={}): + def del_train_log(self, args=None): """ Delete train log. Parameters ----------- args : dictionary, find items to delete, leave it empty to delete all log. """ - + if args is None: + args = {} self.db.TrainLog.delete_many(args) print("[TensorDB] Delete TrainLog SUCCESS") @AutoFill - def valid_log(self, args={}): + def valid_log(self, args=None): """Save the validating log. Parameters @@ -249,7 +256,8 @@ def valid_log(self, args={}): --------- >>> db.valid_log(time=time.time(), {'loss': loss, 'acc': acc}) """ - + if args is None: + args = {} _result = self.db.ValidLog.insert_one(args) # _log = "".join(str(key) + ": " + str(value) for key, value in args.items()) _log = self._print_dict(args) @@ -257,18 +265,20 @@ def valid_log(self, args={}): return _result @AutoFill - def del_valid_log(self, args={}): + def del_valid_log(self, args=None): """ Delete validation log. Parameters ----------- args : dictionary, find items to delete, leave it empty to delete all log. """ + if args is None: + args = {} self.db.ValidLog.delete_many(args) print("[TensorDB] Delete ValidLog SUCCESS") @AutoFill - def test_log(self, args={}): + def test_log(self, args=None): """Save the testing log. Parameters @@ -279,7 +289,8 @@ def test_log(self, args={}): --------- >>> db.test_log(time=time.time(), {'loss': loss, 'acc': acc}) """ - + if args is None: + args = {} _result = self.db.TestLog.insert_one(args) # _log = "".join(str(key) + str(value) for key, value in args.items()) _log = self._print_dict(args) @@ -307,7 +318,10 @@ def save_model_architecture(self, s, args={}): self.db.march.insert_one(args) @AutoFill - def load_model_architecture(self, args={}): + def load_model_architecture(self, args=None): + + if args is None: + args = {} d = self.db.march.find_one(args) if d is not None: @@ -329,7 +343,7 @@ def load_model_architecture(self, args={}): return False, False @AutoFill - def save_job(self, script=None, args={}): + def save_job(self, script=None, args=None): """Save the job. Parameters @@ -348,6 +362,10 @@ def save_job(self, script=None, args={}): >>> import _your_script ... running your script """ + + if args is None: + args = {} + self.__autofill(args) if script is not None: _script = open(script, 'rb').read() @@ -359,7 +377,7 @@ def save_job(self, script=None, args={}): return _result @AutoFill - def find_one_job(self, args={}): + def find_one_job(self, args=None): """ Find one job from MongoDB Job Collections. Parameters @@ -371,6 +389,9 @@ def find_one_job(self, args={}): dictionary : contains all meta data and script. """ + if args is None: + args = {} + temp = self.db.Job.find_one(args) if temp is not None: diff --git a/tensorlayer/files.py b/tensorlayer/files.py index f04016850..657a55be0 100644 --- a/tensorlayer/files.py +++ b/tensorlayer/files.py @@ -196,7 +196,7 @@ def unpickle(file): y_train = np.array(y_train) - if plotable == True: + if plotable: logging.info('\nCIFAR-10') import matplotlib.pyplot as plt fig = plt.figure(1) @@ -1133,7 +1133,7 @@ def convert_annotation(file_name): ## Load and save network list npz -def save_npz(save_list=[], name='model.npz', sess=None): +def save_npz(save_list=None, name='model.npz', sess=None): """Input parameters and the file name, save parameters into .npz file. Use tl.utils.load_npz() to restore. Parameters @@ -1170,6 +1170,9 @@ def save_npz(save_list=[], name='model.npz', sess=None): """ ## save params into a list + if save_list is None: + save_list = [] + save_list_var = [] if sess: save_list_var = sess.run(save_list) @@ -1291,8 +1294,10 @@ def load_and_assign_npz(sess=None, name=None, network=None): - See ``tl.files.save_npz`` """ - assert network is not None - assert sess is not None + if network is None: + raise ValueError("network is None.") + if sess is None: + raise ValueError("session is None.") if not os.path.exists(name): logging.info("[!] Load {} failed!".format(name)) return False @@ -1303,8 +1308,7 @@ def load_and_assign_npz(sess=None, name=None, network=None): return network -## Load and save network dict npz -def save_npz_dict(save_list=[], name='model.npz', sess=None): +def save_npz_dict(save_list=None, name='model.npz', sess=None): """Input parameters and the file name, save parameters as a dictionary into .npz file. Use ``tl.files.load_and_assign_npz_dict()`` to restore. @@ -1319,7 +1323,11 @@ def save_npz_dict(save_list=[], name='model.npz', sess=None): TensorFlow Session. """ - assert sess is not None + if sess is None: + raise ValueError("session is None.") + if save_list is None: + save_list = [] + save_list_names = [tensor.name for tensor in save_list] save_list_var = sess.run(save_list) save_var_dict = {save_list_names[idx]: val for idx, val in enumerate(save_list_var)} @@ -1342,7 +1350,9 @@ def load_and_assign_npz_dict(name='model.npz', sess=None): TensorFlow Session. """ - assert sess is not None + if sess is None: + raise ValueError("session is None.") + if not os.path.exists(name): logging.info("[!] Load {} failed!".format(name)) return False @@ -1424,8 +1434,7 @@ def load_and_assign_npz_dict(name='model.npz', sess=None): # return saved_list_var -## Load and save network ckpt -def save_ckpt(sess=None, mode_name='model.ckpt', save_dir='checkpoint', var_list=[], 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 @@ -1448,7 +1457,11 @@ def save_ckpt(sess=None, mode_name='model.ckpt', save_dir='checkpoint', var_list load_ckpt """ - assert sess is not None + if sess is None: + raise ValueError("session is None.") + if var_list is None: + var_list = [] + ckpt_file = os.path.join(save_dir, mode_name) if var_list == []: var_list = tf.global_variables() @@ -1463,7 +1476,7 @@ def save_ckpt(sess=None, mode_name='model.ckpt', save_dir='checkpoint', var_list saver.save(sess, ckpt_file, global_step=global_step) -def load_ckpt(sess=None, mode_name='model.ckpt', save_dir='checkpoint', var_list=[], is_latest=True, printable=False): +def load_ckpt(sess=None, mode_name='model.ckpt', save_dir='checkpoint', var_list=None, is_latest=True, printable=False): """Load parameters from `ckpt` file. Parameters @@ -1500,7 +1513,10 @@ def load_ckpt(sess=None, mode_name='model.ckpt', save_dir='checkpoint', var_list >>> tl.files.load_ckpt(sess=sess, mode_name='model.ckpt', var_list=net.all_params, save_dir='model', is_latest=False, printable=True) """ - assert sess is not None + if sess is None: + raise ValueError("session is None.") + if var_list is None: + var_list = [] if is_latest: ckpt_file = tf.train.latest_checkpoint(save_dir) @@ -1524,7 +1540,6 @@ def load_ckpt(sess=None, mode_name='model.ckpt', save_dir='checkpoint', var_list logging.info("[*] load ckpt fail ...") -## Load and save variables def save_any_to_npy(save_dict={}, name='file.npy'): """Save variables to `.npy` file. @@ -1560,7 +1575,6 @@ def load_npy_to_any(path='', name='file.npy'): exit() -## Folder functions def file_exists(filepath): """Check whether a file exists by given file path.""" return os.path.isfile(filepath) diff --git a/tensorlayer/layers/convolution.py b/tensorlayer/layers/convolution.py index d7b3432d8..3fe01e6b8 100644 --- a/tensorlayer/layers/convolution.py +++ b/tensorlayer/layers/convolution.py @@ -49,14 +49,19 @@ def __init__( data_format='NWC', W_init=tf.truncated_normal_initializer(stddev=0.02), b_init=tf.constant_initializer(value=0.0), - W_init_args={}, - b_init_args={}, + W_init_args=None, + b_init_args=None, name='cnn1d', ): - Layer.__init__(self, name=name) - self.inputs = layer.outputs if act is None: act = tf.identity + if W_init_args is None: + W_init_args = {} + if b_init_args is None: + b_init_args = {} + + Layer.__init__(self, name=name) + self.inputs = layer.outputs logging.info("Conv1dLayer %s: shape:%s stride:%s pad:%s act:%s" % (self.name, str(shape), str(stride), padding, act.__name__)) with tf.variable_scope(name): # as vs: @@ -158,12 +163,17 @@ def __init__( padding='SAME', W_init=tf.truncated_normal_initializer(stddev=0.02), b_init=tf.constant_initializer(value=0.0), - W_init_args={}, - b_init_args={}, + W_init_args=None, + b_init_args=None, use_cudnn_on_gpu=None, data_format=None, name='cnn_layer', ): + if W_init_args is None: + W_init_args = {} + if b_init_args is None: + b_init_args = {} + Layer.__init__(self, name=name) self.inputs = layer.outputs if act is None: @@ -276,10 +286,15 @@ def __init__( padding='SAME', W_init=tf.truncated_normal_initializer(stddev=0.02), b_init=tf.constant_initializer(value=0.0), - W_init_args={}, - b_init_args={}, + W_init_args=None, + b_init_args=None, name='decnn2d_layer', ): + if W_init_args is None: + W_init_args = {} + if b_init_args is None: + b_init_args = {} + Layer.__init__(self, name=name) self.inputs = layer.outputs if act is None: @@ -344,10 +359,15 @@ def __init__( padding='SAME', W_init=tf.truncated_normal_initializer(stddev=0.02), b_init=tf.constant_initializer(value=0.0), - W_init_args={}, - b_init_args={}, + W_init_args=None, + b_init_args=None, name='cnn3d_layer', ): + if W_init_args is None: + W_init_args = {} + if b_init_args is None: + b_init_args = {} + Layer.__init__(self, name=name) self.inputs = layer.outputs if act is None: @@ -411,10 +431,15 @@ def __init__( padding='SAME', W_init=tf.truncated_normal_initializer(stddev=0.02), b_init=tf.constant_initializer(value=0.0), - W_init_args={}, - b_init_args={}, + W_init_args=None, + b_init_args=None, name='decnn3d_layer', ): + if W_init_args is None: + W_init_args = {} + if b_init_args is None: + b_init_args = {} + Layer.__init__(self, name=name) self.inputs = layer.outputs if act is None: @@ -555,24 +580,6 @@ def __init__( self.all_layers.extend([self.outputs]) -class DeformableConv2dLayer(Layer): - """The :class:`DeformableConv2dLayer` class is a 2D - `Deformable Convolutional Networks `__. - """ - - def __init__(self, - layer, - act=tf.identity, - offset_layer=None, - shape=(3, 3, 1, 100), - name='deformable_conv_2d_layer', - W_init=tf.truncated_normal_initializer(stddev=0.02), - b_init=tf.constant_initializer(value=0.0), - W_init_args={}, - b_init_args={}): - raise Exception("deprecated, use DeformableConv2d instead") - - class DeformableConv2d(Layer): """The :class:`DeformableConv2d` class is a 2D `Deformable Convolutional Networks `__. @@ -637,11 +644,16 @@ def __init__( name='deformable_conv_2d', W_init=tf.truncated_normal_initializer(stddev=0.02), b_init=tf.constant_initializer(value=0.0), - W_init_args={}, - b_init_args={}): + W_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__) + if W_init_args is None: + W_init_args = {} + if b_init_args is None: + b_init_args = {} + def _to_bc_h_w(x, x_shape): """(b, h, w, c) -> (b*c, h, w)""" x = tf.transpose(x, [0, 3, 1, 2]) @@ -857,8 +869,8 @@ def atrous_conv1d( data_format='NWC', W_init=tf.truncated_normal_initializer(stddev=0.02), b_init=tf.constant_initializer(value=0.0), - W_init_args={}, - b_init_args={}, + W_init_args=None, + b_init_args=None, name='conv1d', ): """Simplified version of :class:`AtrousConv1dLayer`. @@ -898,6 +910,12 @@ def atrous_conv1d( A :class:`AtrousConv1dLayer` object """ + + if W_init_args is None: + W_init_args = {} + if b_init_args is None: + b_init_args = {} + return Conv1dLayer( layer=layer, act=act, @@ -956,9 +974,14 @@ def __init__(self, padding='SAME', W_init=tf.truncated_normal_initializer(stddev=0.02), b_init=tf.constant_initializer(value=0.0), - W_init_args={}, - b_init_args={}, + W_init_args=None, + b_init_args=None, name='atrou2d'): + if W_init_args is None: + W_init_args = {} + if b_init_args is None: + b_init_args = {} + Layer.__init__(self, name=name) self.inputs = layer.outputs if act is None: @@ -1058,7 +1081,8 @@ def __init__(self, name='atrou2d'): Layer.__init__(self, name=name) self.inputs = layer.outputs - assert tf.__version__ > "0.12.1", "This layer only supports for TF 1.0+" + if tf.__version__ > "0.12.1": + raise Exception("This layer only supports for TF 1.0+") bias_initializer = bias_initializer() @@ -1173,8 +1197,8 @@ def conv1d( data_format="NWC", W_init=tf.truncated_normal_initializer(stddev=0.02), b_init=tf.constant_initializer(value=0.0), - W_init_args={}, - b_init_args={}, + W_init_args=None, + b_init_args=None, name='conv1d', ): """Simplified version of :class:`Conv1dLayer`. @@ -1231,6 +1255,12 @@ def conv1d( >>> n = DenseLayer(n, 2, tf.identity, name='o') """ + + if W_init_args is None: + W_init_args = {} + if b_init_args is None: + b_init_args = {} + return Conv1dLayer( layer=layer, act=act, @@ -1256,8 +1286,8 @@ def conv2d( padding='SAME', W_init=tf.truncated_normal_initializer(stddev=0.02), b_init=tf.constant_initializer(value=0.0), - W_init_args={}, - b_init_args={}, + W_init_args=None, + b_init_args=None, use_cudnn_on_gpu=None, data_format=None, name='conv2d', @@ -1310,7 +1340,15 @@ def conv2d( >>> net = MaxPool2d(net, (2, 2), name='pool2') """ - assert len(strides) == 2, "len(strides) should be 2, Conv2d and Conv2dLayer are different." + + if W_init_args is None: + W_init_args = {} + if b_init_args is None: + b_init_args = {} + + if len(strides) != 2: + raise ValueError("len(strides) should be 2, Conv2d and Conv2dLayer are different.") + try: pre_channel = int(layer.outputs.get_shape()[-1]) except: # if pre_channel is ?, it happens when using Spatial Transformer Net @@ -1341,8 +1379,8 @@ def deconv2d(layer, act=tf.identity, W_init=tf.truncated_normal_initializer(stddev=0.02), b_init=tf.constant_initializer(value=0.0), - W_init_args={}, - b_init_args={}, + W_init_args=None, + b_init_args=None, name='decnn2d'): """Simplified version of :class:`DeConv2dLayer`. @@ -1382,9 +1420,14 @@ def deconv2d(layer, A :class:`DeConv2dLayer` object. """ + if W_init_args is None: + W_init_args = {} + if b_init_args is None: + b_init_args = {} if act is None: act = tf.identity - assert len(strides) == 2, "len(strides) should be 2, DeConv2d and DeConv2dLayer are different." + if len(strides) != 2: + raise ValueError("len(strides) should be 2, DeConv2d and DeConv2dLayer are different.") if tf.__version__ > '1.3': logging.info("DeConv2d %s: n_filters:%s strides:%s pad:%s act:%s" % (name, str(n_filter), str(strides), padding, act.__name__)) inputs = layer.outputs @@ -1555,10 +1598,15 @@ def __init__( padding='SAME', W_init=tf.truncated_normal_initializer(stddev=0.02), b_init=tf.constant_initializer(value=0.0), - W_init_args={}, - b_init_args={}, + W_init_args=None, + b_init_args=None, name='depthwise_conv2d', ): + if W_init_args is None: + W_init_args = {} + if b_init_args is None: + b_init_args = {} + Layer.__init__(self, name=name) self.inputs = layer.outputs @@ -1604,3 +1652,4 @@ def __init__( Conv1d = conv1d Conv2d = conv2d DeConv2d = deconv2d +DeformableConv2dLayer = DeformableConv2d diff --git a/tensorlayer/layers/core.py b/tensorlayer/layers/core.py index 213335df9..dab02e70c 100644 --- a/tensorlayer/layers/core.py +++ b/tensorlayer/layers/core.py @@ -589,15 +589,24 @@ def __init__( vocabulary_size=80000, embedding_size=200, num_sampled=64, - nce_loss_args={}, + nce_loss_args=None, E_init=tf.random_uniform_initializer(minval=-1.0, maxval=1.0), - E_init_args={}, + E_init_args=None, nce_W_init=tf.truncated_normal_initializer(stddev=0.03), - nce_W_init_args={}, + nce_W_init_args=None, nce_b_init=tf.constant_initializer(value=0.0), - nce_b_init_args={}, + nce_b_init_args=None, name='word2vec', ): + if nce_loss_args is None: + nce_loss_args = {} + if E_init_args is None: + E_init_args = {} + if nce_W_init_args is None: + nce_W_init_args = {} + if nce_b_init_args is None: + nce_b_init_args = {} + Layer.__init__(self, name=name) self.inputs = inputs logging.info("Word2vecEmbeddingInputlayer %s: (%d, %d)" % (self.name, vocabulary_size, embedding_size)) @@ -682,9 +691,12 @@ def __init__( vocabulary_size=80000, embedding_size=200, E_init=tf.random_uniform_initializer(-0.1, 0.1), - E_init_args={}, + E_init_args=None, name='embedding', ): + if E_init_args is None: + E_init_args = {} + Layer.__init__(self, name=name) self.inputs = inputs logging.info("EmbeddingInputlayer %s: (%d, %d)" % (self.name, vocabulary_size, embedding_size)) @@ -844,10 +856,15 @@ def __init__( act=tf.identity, W_init=tf.truncated_normal_initializer(stddev=0.1), b_init=tf.constant_initializer(value=0.0), - W_init_args={}, - b_init_args={}, + W_init_args=None, + b_init_args=None, name='dense', ): + if W_init_args is None: + W_init_args = {} + if b_init_args is None: + b_init_args = {} + Layer.__init__(self, name=name) self.inputs = layer.outputs if self.inputs.get_shape().ndims != 2: @@ -1283,10 +1300,15 @@ def __init__( act=tf.identity, W_init=tf.truncated_normal_initializer(stddev=0.1), b_init=tf.constant_initializer(value=0.0), - W_init_args={}, - b_init_args={}, + W_init_args=None, + b_init_args=None, name='dropconnect_layer', ): + if W_init_args is None: + W_init_args = {} + if b_init_args is None: + b_init_args = {} + Layer.__init__(self, name=name) self.inputs = layer.outputs if self.inputs.get_shape().ndims != 2: diff --git a/tensorlayer/layers/importer.py b/tensorlayer/layers/importer.py index 9e108ea7b..87711aef9 100644 --- a/tensorlayer/layers/importer.py +++ b/tensorlayer/layers/importer.py @@ -93,12 +93,15 @@ def __init__( self, layer, slim_layer, - slim_args={}, + slim_args=None, name='tfslim_layer', ): + if slim_layer is None: + raise ValueError("slim layer is None") + if slim_args is None: + slim_args = {} + Layer.__init__(self, name=name) - assert slim_layer is not None - assert slim_args is not None self.inputs = layer.outputs logging.info("SlimNetsLayer %s: %s" % (self.name, slim_layer.__name__)) @@ -152,12 +155,15 @@ def __init__( self, layer, keras_layer, - keras_args={}, + keras_args=None, name='keras_layer', ): + if layer is None: + raise ValueError("layer is None") + if keras_args is None: + keras_args = {} + Layer.__init__(self, name=name) - assert layer is not None - assert keras_layer is not None self.inputs = layer.outputs logging.info("KerasLayer %s: %s" % (self.name, keras_layer)) logging.info("This API will be removed, please use LambdaLayer instead.") @@ -194,12 +200,14 @@ def __init__( self, layer, model_fn, - args={}, + args=None, name='estimator_layer', ): + if model_fn is None: + raise ValueError('model fn is None') + if args is None: + args = {} Layer.__init__(self, name=name) - assert layer is not None - assert model_fn is not None self.inputs = layer.outputs logging.info("EstimatorLayer %s: %s" % (self.name, model_fn)) logging.info("This API will be removed, please use LambdaLayer instead.") diff --git a/tensorlayer/layers/recurrent.py b/tensorlayer/layers/recurrent.py index 0b0ca7157..db9e8d291 100644 --- a/tensorlayer/layers/recurrent.py +++ b/tensorlayer/layers/recurrent.py @@ -129,7 +129,7 @@ def __init__( self, layer, cell_fn, - cell_init_args={}, + cell_init_args=None, n_hidden=100, initializer=tf.random_uniform_initializer(-0.1, 0.1), n_steps=5, @@ -138,6 +138,9 @@ def __init__( return_seq_2d=False, name='rnn_layer', ): + if cell_init_args is None: + cell_init_args = {} + Layer.__init__(self, name=name) if cell_fn is None: raise Exception("Please put in cell_fn") @@ -1010,9 +1013,12 @@ def __init__( n_layer=1, return_last=False, return_seq_2d=False, - dynamic_rnn_init_args={}, + dynamic_rnn_init_args=None, name='dyrnn', ): + if dynamic_rnn_init_args is None: + dynamic_rnn_init_args = {} + Layer.__init__(self, name=name) if cell_fn is None: raise Exception("Please put in cell_fn") @@ -1250,9 +1256,12 @@ def __init__( n_layer=1, return_last=False, return_seq_2d=False, - dynamic_rnn_init_args={}, + dynamic_rnn_init_args=None, name='bi_dyrnn_layer', ): + if dynamic_rnn_init_args is None: + dynamic_rnn_init_args = {} + Layer.__init__(self, name=name) if cell_fn is None: raise Exception("Please put in cell_fn") diff --git a/tensorlayer/layers/spatial_transformer.py b/tensorlayer/layers/spatial_transformer.py index 0f5414fda..e3af5789d 100644 --- a/tensorlayer/layers/spatial_transformer.py +++ b/tensorlayer/layers/spatial_transformer.py @@ -207,7 +207,7 @@ class SpatialTransformer2dAffineLayer(Layer): theta_layer : :class:`Layer` The localisation network. - We will use a :class:`DenseLayer` to make the theta size to [batch, 6], value range to [0, 1] (via tanh). - out_size : tuple of int + out_size : tuple of int or None The size of the output of the network (height, width), the feature maps will be resized by this. name : str A unique layer name. @@ -223,9 +223,12 @@ def __init__( self, layer=None, theta_layer=None, - out_size=[40, 40], + out_size=None, name='sapatial_trans_2d_affine', ): + if out_size is None: + out_size = [40, 40] + Layer.__init__(self, name=name) self.inputs = layer.outputs self.theta_layer = theta_layer diff --git a/tensorlayer/layers/special_activation.py b/tensorlayer/layers/special_activation.py index 1edcfd27a..16a83c6e2 100644 --- a/tensorlayer/layers/special_activation.py +++ b/tensorlayer/layers/special_activation.py @@ -31,9 +31,12 @@ def __init__( layer, channel_shared=False, a_init=tf.constant_initializer(value=0.0), - a_init_args={}, + a_init_args=None, # restore = True, name="prelu_layer"): + if a_init_args is None: + a_init_args = {} + Layer.__init__(self, name=name) self.inputs = layer.outputs logging.info("PReluLayer %s: channel_shared:%s" % (self.name, channel_shared)) diff --git a/tensorlayer/layers/time_distribution.py b/tensorlayer/layers/time_distribution.py index 943259b60..c8e292941 100644 --- a/tensorlayer/layers/time_distribution.py +++ b/tensorlayer/layers/time_distribution.py @@ -43,9 +43,11 @@ def __init__( self, layer, layer_class=None, - args={}, + args=None, name='time_distributed', ): + if args is None: + args = {} Layer.__init__(self, name=name) self.inputs = layer.outputs 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 2ddfc7bb5..16d8cf9ee 100755 --- a/tensorlayer/nlp.py +++ b/tensorlayer/nlp.py @@ -136,7 +136,7 @@ def sample(a=[], temperature=1.0): return np.argmax(np.random.multinomial(1, b, 1)) -def sample_top(a=[], top_k=10): +def sample_top(a=None, top_k=10): """Sample from ``top_k`` probabilities. Parameters @@ -147,6 +147,9 @@ def sample_top(a=[], top_k=10): Number of candidates to be considered. """ + if a is None: + a = [] + idx = np.argpartition(a, -top_k)[-top_k:] probs = a[idx] # logging.info("new %f" % probs) @@ -441,7 +444,7 @@ def simple_read_words(filename="nietzsche.txt"): return words -def read_words(filename="nietzsche.txt", replace=['\n', '']): +def read_words(filename="nietzsche.txt", replace=None): """Read list format context from a file. For customized read_words method, see ``tutorial_generate_text.py``. @@ -463,6 +466,9 @@ def read_words(filename="nietzsche.txt", replace=['\n', '']): - `tensorflow.models.rnn.ptb.reader `__ """ + if replace is None: + replace = ['\n', ''] + with tf.gfile.GFile(filename, "r") as f: try: # python 3.4 or older context_list = f.read().replace(*replace).split() @@ -473,7 +479,7 @@ def read_words(filename="nietzsche.txt", replace=['\n', '']): return context_list -def read_analogies_file(eval_file='questions-words.txt', word2id={}): +def read_analogies_file(eval_file='questions-words.txt', word2id=None): """Reads through an analogy question file, return its id format. Parameters @@ -518,6 +524,9 @@ def read_analogies_file(eval_file='questions-words.txt', word2id={}): ... [ 1216 4309 140 312]] """ + if word2id is None: + word2id = {} + questions = [] questions_skipped = 0 with open(eval_file, "rb") as analogy_f: @@ -745,7 +754,7 @@ def word_ids_to_words(data, id_to_word): return [id_to_word[i] for i in data] -def save_vocab(count=[], name='vocab.txt'): +def save_vocab(count=None, name='vocab.txt'): """Save the vocabulary to a file so the model can be reloaded. Parameters @@ -772,6 +781,9 @@ def save_vocab(count=[], name='vocab.txt'): ... to 316376 """ + if count is None: + count = [] + pwd = os.getcwd() vocabulary_size = len(count) with open(os.path.join(pwd, name), "w") as f: diff --git a/tensorlayer/prepro.py b/tensorlayer/prepro.py index 04af4c328..cb56d88d0 100644 --- a/tensorlayer/prepro.py +++ b/tensorlayer/prepro.py @@ -1229,7 +1229,7 @@ def adjust_hue(im, hout=0.66, is_offset=True, is_clip=True, is_random=False): # pass -def imresize(x, size=[100, 100], interp='bicubic', mode=None): +def imresize(x, size=None, interp='bicubic', mode=None): """Resize an image by given output size and method. Warning, this function will rescale the value to [0, 255]. @@ -1238,7 +1238,7 @@ def imresize(x, size=[100, 100], interp='bicubic', mode=None): ----------- x : numpy.array An image with dimension of [row, col, channel] (default). - size : list of 2 int + size : list of 2 int or None For height and width. interp : str Interpolation method for re-sizing (`nearest`, `lanczos`, `bilinear`, `bicubic` (default) or `cubic`). @@ -1255,6 +1255,9 @@ def imresize(x, size=[100, 100], interp='bicubic', mode=None): - `scipy.misc.imresize `__ """ + if size is None: + size = [100, 100] + if x.shape[-1] == 1: # greyscale x = scipy.misc.imresize(x[:, :, 0], size, interp=interp, mode=mode) @@ -1649,7 +1652,7 @@ def apply_transform(x, transform_matrix, channel_index=2, fill_mode='nearest', c return x -def projective_transform_by_points(x, src, dst, map_args={}, 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 `__. @@ -1662,7 +1665,7 @@ def projective_transform_by_points(x, src, dst, map_args={}, output_shape=None, The original coordinates, usually 4 coordinates of (width, height). dst : list or numpy The coordinates after transformation, the number of coordinates is the same with src. - map_args : dictionary + map_args : dictionary or None Keyword arguments passed to inverse map. output_shape : tuple of 2 int Shape of the output image generated. By default the shape of the input image is preserved. Note that, even for multi-band images, only rows and columns need to be specified. @@ -1703,6 +1706,8 @@ def projective_transform_by_points(x, src, dst, map_args={}, output_shape=None, - `scikit-image : examples `__ """ + if map_args is None: + map_args = {} if type(src) is list: # convert to numpy src = np.array(src) if type(dst) is list: @@ -1907,14 +1912,14 @@ def erosion(x, radius=3): return x -def obj_box_coords_rescale(coords=[], shape=[100, 200]): +def obj_box_coords_rescale(coords=None, shape=None): """Scale down a list of coordinates from pixel unit to the ratio of image size i.e. in the range of [0, 1]. Parameters ------------ - coords : list of list for 4 int + coords : list of list of 4 ints or None For coordinates of more than one images .e.g.[[x, y, w, h], [x, y, w, h], ...]. - shape : list of 2 int + shape : list of 2 int or None 【height, width]. Returns @@ -1941,6 +1946,11 @@ def obj_box_coords_rescale(coords=[], shape=[100, 200]): New coordinates. """ + if coords is None: + coords = [] + if shape is None: + shape = [100, 200] + imh, imw = shape[0], shape[1] imh = imh * 1.0 # * 1.0 for python2 : force division to be float point imw = imw * 1.0 @@ -1955,15 +1965,15 @@ def obj_box_coords_rescale(coords=[], shape=[100, 200]): return coords_new -def obj_box_coord_rescale(coord=[], shape=[100, 200]): +def obj_box_coord_rescale(coord=None, shape=None): """Scale down one coordinates from pixel unit to the ratio of image size i.e. in the range of [0, 1]. It is the reverse process of ``obj_box_coord_scale_to_pixelunit``. Parameters ------------ - coords : list of 4 int + coords : list of 4 int or None One coordinates of one image e.g. [x, y, w, h]. - shape : list of 2 int + shape : list of 2 int or None For [height, width]. Returns @@ -1977,10 +1987,15 @@ def obj_box_coord_rescale(coord=[], shape=[100, 200]): ... [0.3, 0.4, 0.5, 0.5] """ + if coord is None: + coord = [] + if shape is None: + shape = [100, 200] + return obj_box_coords_rescale(coords=[coord], shape=shape)[0] -def obj_box_coord_scale_to_pixelunit(coord, shape=(100, 100)): +def obj_box_coord_scale_to_pixelunit(coord, shape=None): """Convert one coordinate [x, y, w (or x2), h (or y2)] in ratio format to image coordinate format. It is the reverse process of ``obj_box_coord_rescale``. @@ -1988,7 +2003,7 @@ def obj_box_coord_scale_to_pixelunit(coord, shape=(100, 100)): ----------- coord : list of 4 float One coordinate of one image [x, y, w (or x2), h (or y2)] in ratio format, i.e value range [0~1]. - shape : tuple of 2 + shape : tuple of 2 or None For [height, width]. Returns @@ -2002,6 +2017,9 @@ def obj_box_coord_scale_to_pixelunit(coord, shape=(100, 100)): ... [40, 30, 100, 70] """ + if shape is None: + shape = [100, 100] + imh, imw = shape[0:2] x = int(coord[0] * imw) x2 = int(coord[2] * imw) @@ -2183,14 +2201,14 @@ def parse_darknet_ann_list_to_cls_box(annotations): return class_list, bbox_list -def obj_box_left_right_flip(im, coords=[], is_rescale=False, is_center=False, is_random=False): +def obj_box_left_right_flip(im, coords=None, is_rescale=False, is_center=False, is_random=False): """Left-right flip the image and coordinates for object detection. Parameters ---------- im : numpy.array An image with dimension of [row, col, channel] (default). - coords : list of list of 4 int/float + coords : list of list of 4 int/float or None Coordinates [[x, y, w, h], [x, y, w, h], ...]. is_rescale : boolean Set to True, if the input coordinates are rescaled to [0, 1]. Default is False. @@ -2224,6 +2242,9 @@ def obj_box_left_right_flip(im, coords=[], is_rescale=False, is_center=False, is """ + if coords is None: + coords = [] + def _flip(im, coords): im = flip_axis(im, axis=1, is_random=False) coords_new = list() @@ -2273,14 +2294,14 @@ def _flip(im, coords): # exit() -def obj_box_imresize(im, coords=[], size=[100, 100], interp='bicubic', mode=None, is_rescale=False): +def obj_box_imresize(im, coords=None, size=None, interp='bicubic', mode=None, is_rescale=False): """Resize an image, and compute the new bounding box coordinates. Parameters ------------- im : numpy.array An image with dimension of [row, col, channel] (default). - coords : list of list of 4 int/float + coords : list of list of 4 int/float or None Coordinates [[x, y, w, h], [x, y, w, h], ...] size interp and mode : args See ``tl.prepro.imresize``. @@ -2311,6 +2332,11 @@ def obj_box_imresize(im, coords=[], size=[100, 100], interp='bicubic', mode=None ... [[0.2, 0.4, 0.3, 0.3]] (160, 200, 3) """ + if coords is None: + coords = [] + if size is None: + size = [100, 100] + imh, imw = im.shape[0:2] imh = imh * 1.0 # * 1.0 for python2 : force division to be float point imw = imw * 1.0 @@ -2351,7 +2377,7 @@ def obj_box_imresize(im, coords=[], size=[100, 100], interp='bicubic', mode=None # exit() -def obj_box_crop(im, classes=[], coords=[], 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. @@ -2359,9 +2385,9 @@ def obj_box_crop(im, classes=[], coords=[], wrg=100, hrg=100, is_rescale=False, ----------- im : numpy.array An image with dimension of [row, col, channel] (default). - classes : list of int + classes : list of int or None Class IDs. - coords : list of list of 4 int/float + coords : list of list of 4 int/float or None Coordinates [[x, y, w, h], [x, y, w, h], ...] wrg hrg and is_random : args See ``tl.prepro.crop``. @@ -2384,6 +2410,11 @@ def obj_box_crop(im, classes=[], coords=[], wrg=100, hrg=100, is_rescale=False, A list of new bounding boxes. """ + if classes is None: + classes = [] + if coords is None: + 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 is_random: @@ -2490,8 +2521,8 @@ def _get_coord(coord): def obj_box_shift(im, - classes=[], - coords=[], + classes=None, + coords=None, wrg=0.1, hrg=0.1, row_index=0, @@ -2512,9 +2543,9 @@ def obj_box_shift(im, ----------- im : numpy.array An image with dimension of [row, col, channel] (default). - classes : list of int + classes : list of int or None Class IDs. - coords : list of list of 4 int/float + coords : list of list of 4 int/float or None Coordinates [[x, y, w, h], [x, y, w, h], ...] wrg, hrg row_index col_index channel_index is_random fill_mode cval and order : see ``tl.prepro.shift``. is_rescale : boolean @@ -2537,6 +2568,11 @@ def obj_box_shift(im, A list of new bounding boxes. """ + if classes is None: + classes = [] + if coords is None: + 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 is_random: @@ -2625,8 +2661,8 @@ def _get_coord(coord): def obj_box_zoom(im, - classes=[], - coords=[], + classes=None, + coords=None, zoom_range=(0.9, 1.1), row_index=0, col_index=1, @@ -2646,9 +2682,9 @@ def obj_box_zoom(im, ----------- im : numpy.array An image with dimension of [row, col, channel] (default). - classes : list of int + classes : list of int or None Class IDs. - coords : list of list of 4 int/float + coords : list of list of 4 int/float or None Coordinates [[x, y, w, h], [x, y, w, h], ...]. zoom_range row_index col_index channel_index is_random fill_mode cval and order : see ``tl.prepro.zoom``. is_rescale : boolean @@ -2670,6 +2706,11 @@ def obj_box_zoom(im, A list of new bounding boxes. """ + if classes is None: + classes = [] + if coords is None: + coords = [] + if len(zoom_range) != 2: raise Exception('zoom_range should be a tuple or list of two floats. ' 'Received arg: ', zoom_range) if is_random: diff --git a/tensorlayer/visualize.py b/tensorlayer/visualize.py index 12452a062..5142cf6d4 100644 --- a/tensorlayer/visualize.py +++ b/tensorlayer/visualize.py @@ -2,15 +2,14 @@ import os -import matplotlib import numpy as np -# save/read image(s) -import scipy.misc +import scipy.misc # save/read image(s) from . import _logging as logging from . import prepro # Uncomment the following line if you got: _tkinter.TclError: no display name and no $DISPLAY environment variable +# import matplotlib # matplotlib.use('Agg') @@ -124,7 +123,7 @@ def imsave(images, size, path): 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 @@ -438,7 +437,7 @@ def plot_with_labels(low_dim_embs, labels, figsize=(18, 18), second=5, saveable= logging.info("Please install sklearn and matplotlib to visualize embeddings.") -def draw_weights(W=None, second=10, saveable=True, shape=[28, 28], name='mnist', fig_idx=2396512): +def draw_weights(W=None, second=10, saveable=True, shape=None, name='mnist', fig_idx=2396512): """Visualize every columns of the weight matrix to a group of Greyscale img. Parameters @@ -449,7 +448,7 @@ def draw_weights(W=None, second=10, saveable=True, shape=[28, 28], name='mnist', The display second(s) for the image(s), if saveable is False. saveable : boolean Save or plot the figure. - shape : a list with 2 int + shape : a list with 2 int or None The shape of feature image, MNIST is [28, 80]. name : a string A name to save the image, if saveable is True. @@ -461,6 +460,9 @@ def draw_weights(W=None, second=10, saveable=True, shape=[28, 28], name='mnist', >>> tl.visualize.draw_weights(network.all_params[0].eval(), second=10, saveable=True, name='weight_of_1st_layer', fig_idx=2012) """ + if shape is None: + shape = [28, 28] + import matplotlib.pyplot as plt if saveable is False: plt.ion() @@ -498,4 +500,4 @@ def draw_weights(W=None, second=10, saveable=True, shape=[28, 28], name='mnist', plt.savefig(name + '.pdf', format='pdf') else: plt.draw() - plt.pause(second) \ No newline at end of file + plt.pause(second) From bd871f214ce22f5669e3c27885ae16372b90f5f5 Mon Sep 17 00:00:00 2001 From: Luo Mai Date: Fri, 23 Feb 2018 15:39:29 +0800 Subject: [PATCH 02/10] minor fix --- tensorlayer/layers/time_distribution.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tensorlayer/layers/time_distribution.py b/tensorlayer/layers/time_distribution.py index c8e292941..40336f3a7 100644 --- a/tensorlayer/layers/time_distribution.py +++ b/tensorlayer/layers/time_distribution.py @@ -48,13 +48,13 @@ def __init__( ): if args is None: args = {} + if not isinstance(args, dict): + raise TypeError("'args' must be a dict.") + Layer.__init__(self, name=name) self.inputs = layer.outputs logging.info("TimeDistributedLayer %s: layer_class:%s args:%s" % (self.name, layer_class.__name__, args)) - if not args: args = dict() - assert isinstance(args, dict), "'args' must be a dict." - if not isinstance(self.inputs, tf.Tensor): self.inputs = tf.transpose(tf.stack(self.inputs), [1, 0, 2]) From 95d4d93f42b669998fe08c5216bd9c45c5c9ee3c Mon Sep 17 00:00:00 2001 From: Luo Mai Date: Fri, 23 Feb 2018 18:11:55 +0800 Subject: [PATCH 03/10] fix exception too broad issue. --- example/tutorial_cifar10_tfrecord.py | 4 ++-- example/tutorial_inceptionV3_tfslim.py | 2 +- tensorlayer/db.py | 2 +- tensorlayer/files.py | 2 +- tensorlayer/layers/core.py | 8 ++++---- tensorlayer/layers/extend.py | 2 +- tensorlayer/layers/flow_control.py | 2 +- tensorlayer/layers/merge.py | 2 +- tensorlayer/layers/recurrent.py | 14 +++++++------- tensorlayer/nlp.py | 2 +- 10 files changed, 20 insertions(+), 20 deletions(-) diff --git a/example/tutorial_cifar10_tfrecord.py b/example/tutorial_cifar10_tfrecord.py index 66a9e0d51..6df316217 100644 --- a/example/tutorial_cifar10_tfrecord.py +++ b/example/tutorial_cifar10_tfrecord.py @@ -121,7 +121,7 @@ def read_and_decode(filename, is_train=None): # 5. Subtract off the mean and divide by the variance of the pixels. try: # TF 0.12+ img = tf.image.per_image_standardization(img) - except: # earlier TF versions + except Exception: # earlier TF versions img = tf.image.per_image_whitening(img) elif is_train == False: @@ -130,7 +130,7 @@ def read_and_decode(filename, is_train=None): # 2. Subtract off the mean and divide by the variance of the pixels. try: # TF 0.12+ img = tf.image.per_image_standardization(img) - except: # earlier TF versions + except Exception: # earlier TF versions img = tf.image.per_image_whitening(img) elif is_train == None: img = img diff --git a/example/tutorial_inceptionV3_tfslim.py b/example/tutorial_inceptionV3_tfslim.py index c90d15048..dd5e58901 100644 --- a/example/tutorial_inceptionV3_tfslim.py +++ b/example/tutorial_inceptionV3_tfslim.py @@ -123,7 +123,7 @@ def print_prob(prob): exit() try: # TF12+ saver.restore(sess, "./inception_v3.ckpt") -except: # TF11 +except Exception: # TF11 saver.restore(sess, "inception_v3.ckpt") print("Model Restored") diff --git a/tensorlayer/db.py b/tensorlayer/db.py index c634017cf..a2cfb82b7 100644 --- a/tensorlayer/db.py +++ b/tensorlayer/db.py @@ -148,7 +148,7 @@ def find_one_params(self, args=None, sort=None): params = self.__deserialization(self.paramsfs.get(f_id).read()) print("[TensorDB] Find one params SUCCESS, {} took: {}s".format(args, round(time.time() - s, 2))) return params, f_id - except: + except Exception: return False, False @AutoFill diff --git a/tensorlayer/files.py b/tensorlayer/files.py index 657a55be0..5f61ecefb 100644 --- a/tensorlayer/files.py +++ b/tensorlayer/files.py @@ -1180,7 +1180,7 @@ def save_npz(save_list=None, name='model.npz', sess=None): try: for k, value in enumerate(save_list): save_list_var.append(value.eval()) - except: + 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)") np.savez(name, params=save_list_var) save_list_var = None diff --git a/tensorlayer/layers/core.py b/tensorlayer/layers/core.py index dab02e70c..2d4e50e93 100644 --- a/tensorlayer/layers/core.py +++ b/tensorlayer/layers/core.py @@ -22,7 +22,7 @@ try: # For TF12 and later TF_GRAPHKEYS_VARIABLES = tf.GraphKeys.GLOBAL_VARIABLES -except: # For TF11 and before +except Exception: # For TF11 and before TF_GRAPHKEYS_VARIABLES = tf.GraphKeys.VARIABLES @@ -152,7 +152,7 @@ def initialize_rnn_state(state, feed_dict=None): """ try: # TF1.0 LSTMStateTuple = tf.contrib.rnn.LSTMStateTuple - except: + except Exception: LSTMStateTuple = tf.nn.rnn_cell.LSTMStateTuple if isinstance(state, LSTMStateTuple): @@ -183,7 +183,7 @@ def print_all_variables(train_only=False): else: try: # TF1.0+ t_vars = tf.global_variables() - except: # TF0.12 + except Exception: # TF0.12 t_vars = tf.all_variables() logging.info(" [*] printing global variables") for idx, v in enumerate(t_vars): @@ -998,7 +998,7 @@ def __init__( 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)))) - except: # TF0.12 + 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(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)) ) diff --git a/tensorlayer/layers/extend.py b/tensorlayer/layers/extend.py index f05852912..ca3cb2baa 100644 --- a/tensorlayer/layers/extend.py +++ b/tensorlayer/layers/extend.py @@ -32,7 +32,7 @@ def __init__( with tf.variable_scope(name) as vs: try: # TF12 TF1.0 self.outputs = tf.expand_dims(self.inputs, axis=axis) - except: # TF11 + except Exception: # TF11 self.outputs = tf.expand_dims(self.inputs, dim=axis) self.all_layers = list(layer.all_layers) self.all_params = list(layer.all_params) diff --git a/tensorlayer/layers/flow_control.py b/tensorlayer/layers/flow_control.py index 5ab1b0170..e8fb4dcd2 100644 --- a/tensorlayer/layers/flow_control.py +++ b/tensorlayer/layers/flow_control.py @@ -61,7 +61,7 @@ def __init__(self, layers, name='mux_layer'): self.inputs.append(l.outputs) try: # TF1.0 all_inputs = tf.stack(self.inputs, name=name) # pack means concat a list of tensor in a new dim # 1.2 - except: + except Exception: all_inputs = tf.pack(self.inputs, name=name) # pack means concat a list of tensor in a new dim # 1.2 logging.info("MultiplexerLayer %s: n_inputs:%d" % (self.name, self.n_inputs)) diff --git a/tensorlayer/layers/merge.py b/tensorlayer/layers/merge.py index bf13ac6f2..40a9c113f 100644 --- a/tensorlayer/layers/merge.py +++ b/tensorlayer/layers/merge.py @@ -52,7 +52,7 @@ def __init__( self.inputs.append(l.outputs) try: # TF1.0 self.outputs = tf.concat(self.inputs, concat_dim, name=name) - except: # TF0.12 + except Exception: # TF0.12 self.outputs = tf.concat(concat_dim, self.inputs, name=name) logging.info("ConcatLayer %s: axis: %d" % (self.name, concat_dim)) diff --git a/tensorlayer/layers/recurrent.py b/tensorlayer/layers/recurrent.py index db9e8d291..6dd0d8f81 100644 --- a/tensorlayer/layers/recurrent.py +++ b/tensorlayer/layers/recurrent.py @@ -1268,7 +1268,7 @@ def __init__( if 'GRU' in cell_fn.__name__: try: cell_init_args.pop('state_is_tuple') - except: + except Exception: pass self.inputs = layer.outputs @@ -1278,7 +1278,7 @@ def __init__( # Input dimension should be rank 3 [batch_size, n_steps(max), n_features] try: self.inputs.get_shape().with_rank(3) - except: + except Exception: raise Exception("RNN : Input dimension should be rank 3 : [batch_size, n_steps(max), n_features]") # Get the batch_size @@ -1308,7 +1308,7 @@ def __init__( raise Exception("Invalid dropout type (must be a 2-D tuple of " "float)") try: DropoutWrapper_fn = tf.contrib.rnn.DropoutWrapper - except: + except Exception: DropoutWrapper_fn = tf.nn.rnn_cell.DropoutWrapper # cell_instance_fn1=cell_instance_fn # HanSheng @@ -1337,7 +1337,7 @@ 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)) - except: # TF0.12 + except Exception: # TF0.12 sequence_length = retrieve_seq_length_op(self.inputs if isinstance(self.inputs, tf.Tensor) else tf.pack(self.inputs)) if n_layer > 1: @@ -1373,7 +1373,7 @@ def __init__( # Manage the outputs try: # TF1.0 outputs = tf.concat(outputs, 2) - except: # TF0.12 + except Exception: # TF0.12 outputs = tf.concat(2, outputs) if return_last: # [batch_size, 2 * n_hidden] @@ -1386,7 +1386,7 @@ def __init__( # 2D Tensor [n_example, 2 * n_hidden] try: # TF1.0 self.outputs = tf.reshape(tf.concat(outputs, 1), [-1, 2 * n_hidden]) - except: # TF0.12 + except Exception: # TF0.12 self.outputs = tf.reshape(tf.concat(1, outputs), [-1, 2 * n_hidden]) else: # : @@ -1546,7 +1546,7 @@ def __init__( if 'GRU' in cell_fn.__name__: try: cell_init_args.pop('state_is_tuple') - except: + except Exception: pass # 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)) diff --git a/tensorlayer/nlp.py b/tensorlayer/nlp.py index 16d8cf9ee..6d9efe0fb 100755 --- a/tensorlayer/nlp.py +++ b/tensorlayer/nlp.py @@ -1048,7 +1048,7 @@ def moses_multi_bleu(hypotheses, references, lowercase=False): try: 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: # pylint: disable=W0702 + except Exception: # pylint: disable=W0702 tf.logging.info("Unable to fetch multi-bleu.perl script, using local.") metrics_dir = os.path.dirname(os.path.realpath(__file__)) bin_dir = os.path.abspath(os.path.join(metrics_dir, "..", "..", "bin")) From 8137691b113428b0684d137a5c55cb976a45407e Mon Sep 17 00:00:00 2001 From: Luo Mai Date: Fri, 23 Feb 2018 18:22:45 +0800 Subject: [PATCH 04/10] fix useless string statement issues. --- example/tutorial_vgg16.py | 36 +++++++++++--------- example/tutorial_word2vec_basic.py | 47 ++++++++++++-------------- tensorlayer/db.py | 1 - tensorlayer/layers/super_resolution.py | 15 ++++---- tensorlayer/prepro.py | 7 ++-- 5 files changed, 51 insertions(+), 55 deletions(-) diff --git a/example/tutorial_vgg16.py b/example/tutorial_vgg16.py index 695d9cf95..dda37025e 100644 --- a/example/tutorial_vgg16.py +++ b/example/tutorial_vgg16.py @@ -37,10 +37,6 @@ """ import os -import sys -import time -import numpy as np -import tensorflow as tf import tensorlayer as tl from scipy.misc import imread, imresize from tensorlayer.layers import * @@ -60,7 +56,8 @@ def conv_layers(net_in): """ mean = tf.constant([123.68, 116.779, 103.939], dtype=tf.float32, shape=[1, 1, 1, 3], name='img_mean') net_in.outputs = net_in.outputs - mean - """ conv1 """ + + # conv1 network = Conv2dLayer( net_in, act=tf.nn.relu, @@ -76,7 +73,8 @@ def conv_layers(net_in): 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 """ + + # conv2 network = Conv2dLayer( network, act=tf.nn.relu, @@ -92,7 +90,8 @@ def conv_layers(net_in): 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 """ + + # conv3 network = Conv2dLayer( network, act=tf.nn.relu, @@ -115,7 +114,8 @@ def conv_layers(net_in): 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') - """ conv4 """ + + # conv4 network = Conv2dLayer( network, act=tf.nn.relu, @@ -138,7 +138,8 @@ def conv_layers(net_in): 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') - """ conv5 """ + + # conv5 network = Conv2dLayer( network, act=tf.nn.relu, @@ -173,25 +174,30 @@ def conv_layers_simple_api(net_in): """ mean = tf.constant([123.68, 116.779, 103.939], dtype=tf.float32, shape=[1, 1, 1, 3], name='img_mean') net_in.outputs = net_in.outputs - mean - """ conv1 """ + + # 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 = MaxPool2d(network, filter_size=(2, 2), strides=(2, 2), padding='SAME', name='pool1') - """ conv2 """ + + # 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 = MaxPool2d(network, filter_size=(2, 2), strides=(2, 2), padding='SAME', name='pool2') - """ conv3 """ + + # 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 = MaxPool2d(network, filter_size=(2, 2), strides=(2, 2), padding='SAME', name='pool3') - """ conv4 """ + + # 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 = MaxPool2d(network, filter_size=(2, 2), strides=(2, 2), padding='SAME', name='pool4') - """ conv5 """ + + # 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') @@ -249,5 +255,3 @@ def fc_layers(net): preds = (np.argsort(prob)[::-1])[0:5] for p in preds: print(class_names[p], prob[p]) - -# diff --git a/example/tutorial_word2vec_basic.py b/example/tutorial_word2vec_basic.py index 79654847e..923bc7955 100644 --- a/example/tutorial_word2vec_basic.py +++ b/example/tutorial_word2vec_basic.py @@ -37,10 +37,6 @@ """ -import collections -import math -import os -import random import time import numpy as np @@ -56,10 +52,9 @@ def main_word2vec_basic(): # sess = tf.InteractiveSession() sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) - """ Step 1: Download the data, read the context into a list of strings. - Set hyperparameters. - """ + # Step 1: Download the data, read the context into a list of strings. + # Set hyperparameters. words = tl.files.load_matt_mahoney_text8_dataset() data_size = len(words) print(data_size) # 17005207 @@ -126,8 +121,8 @@ def main_word2vec_basic(): print('%d Steps in a Epoch, total Epochs %d' % (int(data_size / batch_size), n_epoch)) print(' learning_rate: %f' % learning_rate) print(' batch_size: %d' % batch_size) - """ Step 2: Build the dictionary and replace rare words with 'UNK' token. - """ + + # Step 2: Build the dictionary and replace rare words with 'UNK' token. print() if resume: print("Load existing data and dictionaries" + "!" * 10) @@ -146,10 +141,10 @@ def main_word2vec_basic(): ]) # [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. - """ + + # Step 3: Function to generate a training batch for the Skip-Gram model. print() - 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]]) @@ -157,10 +152,10 @@ def main_word2vec_basic(): 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]]) - # exit() - """ Step 4: Build a Skip-Gram model. - """ + + # Step 4: Build a Skip-Gram model. print() + # We pick a random validation set to sample nearest neighbors. Here we limit the # validation samples to the words that have a low numeric ID, which by # construction are also the most frequent. @@ -208,9 +203,10 @@ def main_word2vec_basic(): similarity = tf.matmul(valid_embed, normalized_embeddings, transpose_b=True) # multiply all valid word vector with all word vector. # transpose_b=True, normalized_embeddings is transposed before multiplication. - """ Step 5: Start training. - """ + + # Step 5: Start training. print() + tl.layers.initialize_global_variables(sess) if resume: print("Load existing model" + "!" * 10) @@ -229,7 +225,7 @@ def main_word2vec_basic(): average_loss = 0 step = 0 print_freq = 2000 - while (step < num_steps): + 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) @@ -279,16 +275,17 @@ def main_word2vec_basic(): # learning_rate = float(input("Input new learning rate: ")) # train_op = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost) step += 1 - """ Step 6: Visualize the normalized embedding matrix by t-SNE. - """ + + # Step 6: Visualize the normalized embedding matrix by t-SNE. 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') - """ Step 7: Evaluate by analogy questions. - see tensorflow/models/embedding/word2vec_optimized.py - """ + + # Step 7: Evaluate by analogy questions. see tensorflow/models/embedding/word2vec_optimized.py print() - # from tensorflow/models/embedding/word2vec.py + + # from tensorflow/models/embedding/word2vec.py analogy_questions = tl.nlp.read_analogies_file( \ eval_file='questions-words.txt', word2id=dictionary) # The eval feeds three vectors of word ids for a, b, c, each of @@ -352,5 +349,3 @@ def predict(analogy): if __name__ == '__main__': main_word2vec_basic() - -# diff --git a/tensorlayer/db.py b/tensorlayer/db.py index a2cfb82b7..d34c1b225 100644 --- a/tensorlayer/db.py +++ b/tensorlayer/db.py @@ -335,7 +335,6 @@ def load_model_architecture(self, args=None): return False, False try: archs = self.archfs.get(fid).read() - '''print("[TensorDB] Find one params SUCCESS, {} took: {}s".format(args, round(time.time()-s, 2)))''' return archs, fid except Exception as e: print("exception") diff --git a/tensorlayer/layers/super_resolution.py b/tensorlayer/layers/super_resolution.py index 2dd914099..2ce06ef47 100644 --- a/tensorlayer/layers/super_resolution.py +++ b/tensorlayer/layers/super_resolution.py @@ -72,14 +72,13 @@ def subpixel_conv2d(net, scale=2, n_out_channel=None, act=tf.identity, name='sub def _PS(X, r, n_out_channels): if n_out_channels >= 1: assert int(X.get_shape()[-1]) == (r**2) * n_out_channels, _err_log - """ - bsize, a, b, c = X.get_shape().as_list() - bsize = tf.shape(X)[0] # Handling Dimension(None) type for undefined batch dim - Xs=tf.split(X,r,3) #b*h*w*r*r - Xr=tf.concat(Xs,2) #b*h*(r*w)*r - X=tf.reshape(Xr,(bsize,r*a,r*b,n_out_channel)) # b*(r*h)*(r*w)*c - - """ + + # bsize, a, b, c = X.get_shape().as_list() + # bsize = tf.shape(X)[0] # Handling Dimension(None) type for undefined batch dim + # Xs=tf.split(X,r,3) #b*h*w*r*r + # Xr=tf.concat(Xs,2) #b*h*(r*w)*r + # X=tf.reshape(Xr,(bsize,r*a,r*b,n_out_channel)) # b*(r*h)*(r*w)*c + X = tf.depth_to_space(X, r) else: logging.info(_err_log) diff --git a/tensorlayer/prepro.py b/tensorlayer/prepro.py index cb56d88d0..8aacc8be9 100644 --- a/tensorlayer/prepro.py +++ b/tensorlayer/prepro.py @@ -2738,7 +2738,7 @@ def _get_coord(coord): if is_center: coord = obj_box_coord_centroid_to_upleft(coord) - ##======= pixel unit format and upleft, w, h ==========## + # ======= pixel unit format and upleft, w, h ========== x = (coord[0] - im.shape[1] / 2) / zy + im.shape[1] / 2 # only change this y = (coord[1] - im.shape[0] / 2) / zx + im.shape[0] / 2 # only change this w = coord[2] / zy # only change this @@ -2776,7 +2776,7 @@ def _get_coord(coord): coord = [x, y, w, h] - ## convert back if input format is center. + # convert back if input format is center. if is_center: coord = obj_box_coord_upleft_to_centroid(coord) @@ -2788,7 +2788,7 @@ def _get_coord(coord): coord = coords[i] assert len(coord) == 4, "coordinate should be 4 values : [x, y, w, h]" if is_rescale: - """ for scaled coord, upscaled before process and scale back in the end. """ + # for scaled coord, upscaled before process and scale back in the end. coord = obj_box_coord_scale_to_pixelunit(coord, im.shape) coord = _get_coord(coord) if coord is not None: @@ -2803,7 +2803,6 @@ def _get_coord(coord): return im_new, classes_new, coords_new -## Sequence def pad_sequences(sequences, maxlen=None, dtype='int32', padding='post', truncating='pre', value=0.): """Pads each sequence to the same length: the length of the longest sequence. From d645db9d20ca0f5c40a02edfca956a0e39a5d1ee Mon Sep 17 00:00:00 2001 From: Luo Mai Date: Fri, 23 Feb 2018 20:03:59 +0800 Subject: [PATCH 05/10] convert range(len() to enumerate() --- tensorlayer/files.py | 89 +++++++++++++------------------------ tensorlayer/layers/stack.py | 2 +- tensorlayer/prepro.py | 6 +-- tensorlayer/visualize.py | 2 +- 4 files changed, 35 insertions(+), 64 deletions(-) diff --git a/tensorlayer/files.py b/tensorlayer/files.py index 5f61ecefb..712fedcc9 100644 --- a/tensorlayer/files.py +++ b/tensorlayer/files.py @@ -570,22 +570,24 @@ def load_flickr25k_dataset(tag='sky', path="data", n_threads=50, printable=False filename = 'mirflickr25k.zip' url = 'http://press.liacs.nl/mirflickr/mirflickr25k/' - ## download dataset + + # download dataset if folder_exists(path + "/mirflickr") is False: logging.info("[*] Flickr25k is nonexistent in {}".format(path)) maybe_download_and_extract(filename, path, url, extract=True) del_file(path + '/' + filename) - ## return images by the given tag. + + # return images by the given tag. # 1. image path list folder_imgs = path + "/mirflickr" path_imgs = load_file_list(path=folder_imgs, regx='\\.jpg', printable=False) path_imgs.sort(key=natural_keys) - # logging.info(path_imgs[0:10]) + # 2. tag path list folder_tags = path + "/mirflickr/meta/tags" path_tags = load_file_list(path=folder_tags, regx='\\.txt', printable=False) path_tags.sort(key=natural_keys) - # logging.info(path_tags[0:10]) + # 3. select images if tag is None: logging.info("[Flickr25k] reading all images") @@ -643,7 +645,8 @@ def load_flickr1M_dataset(tag='sky', size=10, path="data", n_threads=50, printab ] tag_zip = 'tags.zip' url = 'http://press.liacs.nl/mirflickr/mirflickr1m/' - ## download dataset + + # download dataset for image_zip in images_zip[0:size]: image_folder = image_zip.split(".")[0] # logging.info(path+"/"+image_folder) @@ -655,7 +658,8 @@ def load_flickr1M_dataset(tag='sky', size=10, path="data", n_threads=50, printab os.system("mv {} {}".format(path + '/images', path + '/' + image_folder)) else: logging.info("[Flickr1M] {} exists in {}".format(image_folder, path)) - ## download tag + + # download tag if folder_exists(path + "/tags") is False: logging.info("[Flickr1M] tag files is nonexistent in {}".format(path)) maybe_download_and_extract(tag_zip, path, url, extract=True) @@ -663,42 +667,37 @@ def load_flickr1M_dataset(tag='sky', size=10, path="data", n_threads=50, printab else: logging.info("[Flickr1M] tags exists in {}".format(path)) - ## 1. image path list + # 1. image path list images_list = [] images_folder_list = [] for i in range(0, size): - # images_folder_list += load_folder_list(path=path + '/images%d' % i) images_folder_list += load_folder_list(path=os.path.join(path, 'images%d' % i)) images_folder_list.sort(key=lambda s: int(s.split('/')[-1])) # folder/images/ddd - # logging.info(images_folder_list) - # exit() + for folder in images_folder_list[0:size * 10]: tmp = load_file_list(path=folder, regx='\\.jpg', printable=False) tmp.sort(key=lambda s: int(s.split('.')[-2])) # ddd.jpg - # logging.info(tmp[0::570]) images_list.extend([folder + '/' + x for x in tmp]) - # logging.info('IM', len(images_list), images_list[0::6000]) - ## 2. tag path list + + # 2. tag path list tag_list = [] tag_folder_list = load_folder_list(path + "/tags") tag_folder_list.sort(key=lambda s: int(s.split('/')[-1])) # folder/images/ddd for folder in tag_folder_list[0:size * 10]: - # logging.info(folder) tmp = load_file_list(path=folder, regx='\\.txt', printable=False) tmp.sort(key=lambda s: int(s.split('.')[-2])) # ddd.txt tmp = [folder + '/' + s for s in tmp] tag_list += tmp - # logging.info('T', len(tag_list), tag_list[0::6000]) - # exit() - ## 3. select images + + # 3. select images logging.info("[Flickr1M] searching tag: {}".format(tag)) select_images_list = [] - for idx in range(0, len(tag_list)): + for idx, _val in enumerate(tag_list): tags = read_file(tag_list[idx]).split('\n') if tag in tags: select_images_list.append(images_list[idx]) - # logging.info(idx, tags, tag_list[idx], images_list[idx]) + logging.info("[Flickr1M] reading images with tag: {}".format(tag)) images = visualize.read_images(select_images_list, '', n_threads=n_threads, printable=printable) return images @@ -737,7 +736,7 @@ def load_image_from_folder(path): im_test_B = load_image_from_folder(os.path.join(path, filename, "testB")) def if_2d_to_3d(images): # [h, w] --> [h, w, 3] - for i in range(len(images)): + for i, _v in enumerate(images): if len(images[i].shape) == 2: images[i] = images[i][:, :, np.newaxis] images[i] = np.tile(images[i], (1, 1, 3)) @@ -821,7 +820,7 @@ def load_celebA_dataset(path='data'): os.rename(os.path.join(path, zip_dir), image_path) data_files = load_file_list(path=image_path, regx='\\.jpg', printable=False) - for i in range(len(data_files)): + for i, _v in enumerate(data_files): data_files[i] = os.path.join(image_path, data_files[i]) return data_files @@ -930,7 +929,6 @@ def _recursive_parse_xml_to_dict(xml): from lxml import etree # pip install lxml import xml.etree.ElementTree as ET - ## if dataset == "2012": url = "http://host.robots.ox.ac.uk/pascal/VOC/voc2012/" tar_filename = "VOCtrainval_11-May-2012.tar" @@ -967,7 +965,7 @@ def _recursive_parse_xml_to_dict(xml): else: raise Exception("Please set the dataset aug to 2012, 2012test or 2007.") - ##======== download dataset + # download dataset if dataset != "2012test": from sys import platform as _platform if folder_exists(os.path.join(path, extracted_filename)) is False: @@ -990,7 +988,7 @@ def _recursive_parse_xml_to_dict(xml): else: os.system("mv {}/VOCdevkit/VOC2007 {}/VOC2007test".format(path, path)) del_folder(os.path.join(path, 'VOCdevkit')) - ##======== object classes(labels) NOTE: YOU CAN CUSTOMIZE THIS LIST + # 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" @@ -1005,7 +1003,7 @@ def _recursive_parse_xml_to_dict(xml): classes_dict = utils.list_string_to_dict(classes) logging.info("[VOC] object classes {}".format(classes_dict)) - ##======== 1. image path list + # 1. image path list # folder_imgs = path+"/"+extracted_filename+"/JPEGImages/" folder_imgs = os.path.join(path, extracted_filename, "JPEGImages") imgs_file_list = load_file_list(path=folder_imgs, regx='\\.jpg', printable=False) @@ -1033,7 +1031,7 @@ def _recursive_parse_xml_to_dict(xml): else: imgs_semseg_file_list = [] imgs_insseg_file_list = [] - ##======== 4. annotations for bounding box and object class + # 4. annotations for bounding box and object class # 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) @@ -1053,7 +1051,7 @@ def _recursive_parse_xml_to_dict(xml): imgs_file_list = imgs_file_list_new logging.info("[VOC] keep %d images" % len(imgs_file_list_new)) - ##======== parse XML annotations + # parse XML annotations def convert(size, box): dw = 1. / size[0] dh = 1. / size[1] @@ -1065,7 +1063,7 @@ def convert(size, box): w = w * dw y = y * dh h = h * dh - return (x, y, w, h) + return x, y, w, h def convert_annotation(file_name): """Given VOC2012 XML Annotations, returns number of objects and info.""" @@ -1078,8 +1076,6 @@ def convert_annotation(file_name): h = int(size.find('height').text) n_objs = 0 - # logging.info(file_name, w, h, size) - # exit() for obj in root.iter('object'): if dataset != "2012test": difficult = obj.find('difficult').text @@ -1094,7 +1090,7 @@ def convert_annotation(file_name): 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)) 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' n_objs += 1 if cls in "person": @@ -1117,7 +1113,6 @@ def convert_annotation(file_name): objs_info_list = [] # Darknet Format list of string objs_info_dicts = {} for idx, ann_file in enumerate(imgs_ann_file_list): - # logging.info(ann_file) n_objs, objs_info = convert_annotation(ann_file) n_objs_list.append(n_objs) objs_info_list.append(objs_info) @@ -1132,7 +1127,6 @@ def convert_annotation(file_name): n_objs_list, objs_info_list, objs_info_dicts -## Load and save network list npz def save_npz(save_list=None, name='model.npz', sess=None): """Input parameters and the file name, save parameters into .npz file. Use tl.utils.load_npz() to restore. @@ -1169,7 +1163,6 @@ def save_npz(save_list=None, name='model.npz', sess=None): - `Saving dictionary using numpy `__ """ - ## save params into a list if save_list is None: save_list = [] @@ -1187,13 +1180,6 @@ def save_npz(save_list=None, name='model.npz', sess=None): del save_list_var logging.info("[*] %s saved" % name) - ## save params into a dictionary - # rename_dict = {} - # for k, value in enumerate(save_dict): - # rename_dict.update({'param'+str(k) : value.eval()}) - # np.savez(name, **rename_dict) - # logging.info('Model is saved to: %s' % name) - def load_npz(path='', name='model.npz'): """Load the parameters of a Model saved by tl.files.save_npz(). @@ -1219,23 +1205,8 @@ def load_npz(path='', name='model.npz'): - `Saving dictionary using numpy `__ """ - ## if save_npz save params into a dictionary - # d = np.load( path+name ) - # params = [] - # logging.info('Load Model') - # for key, val in sorted( d.items() ): - # params.append(val) - # logging.info('Loading %s, %s' % (key, str(val.shape))) - # return params - ## if save_npz save params into a list d = np.load(path + name) - # for val in sorted( d.items() ): - # params = val - # return params return d['params'] - # logging.info(d.items()[0][1]['params']) - # exit() - # return d.items()[0][1]['params'] def assign_params(sess, params, network): @@ -1523,7 +1494,7 @@ def load_ckpt(sess=None, mode_name='model.ckpt', save_dir='checkpoint', var_list else: ckpt_file = os.path.join(save_dir, mode_name) - if var_list == []: + if not var_list: var_list = tf.global_variables() logging.info("[*] load %s n_params: %d" % (ckpt_file, len(var_list))) @@ -1565,12 +1536,12 @@ def load_npy_to_any(path='', name='file.npy'): file_path = os.path.join(path, name) try: npy = np.load(file_path).item() - except: + except Exception: npy = np.load(file_path) finally: try: return npy - except: + except Exception: logging.info("[!] Fail to load %s" % file_path) exit() diff --git a/tensorlayer/layers/stack.py b/tensorlayer/layers/stack.py index 3480bf445..1ec94ddfb 100644 --- a/tensorlayer/layers/stack.py +++ b/tensorlayer/layers/stack.py @@ -80,7 +80,7 @@ def unstack_layer(layer, num=None, axis=0, name='unstack'): else: whole_name = name - for i in range(len(outputs)): + for i, _v in enumerate(outputs): n = Layer(None, name=whole_name + str(i)) n.outputs = outputs[i] n.all_layers = list(layer.all_layers) diff --git a/tensorlayer/prepro.py b/tensorlayer/prepro.py index 8aacc8be9..68d85b8a5 100644 --- a/tensorlayer/prepro.py +++ b/tensorlayer/prepro.py @@ -2165,7 +2165,7 @@ def parse_darknet_ann_str_to_list(annotations): for a in annotations: a = a.split() if len(a) == 5: - for i in range(len(a)): + for i, _v in enumerate(a): if i == 0: a[i] = int(a[i]) else: @@ -3069,8 +3069,8 @@ def sequences_add_end_id_after_pad(sequences, end_id=888, pad_id=0): # sequences_out[i].append(pad_id) # # pad -- > end # max_len = 0 - for i in range(len(sequences)): - for j in range(len(sequences[i])): + for i, v in enumerate(sequences): + for j, _v2 in enumerate(v): if sequences[i][j] == pad_id: sequences_out[i][j] = end_id # if j > max_len: diff --git a/tensorlayer/visualize.py b/tensorlayer/visualize.py index 5142cf6d4..ff19ee822 100644 --- a/tensorlayer/visualize.py +++ b/tensorlayer/visualize.py @@ -174,7 +174,7 @@ def draw_boxes_and_labels_to_image(image, classes, coords, scores, classes_list, imh, imw = image.shape[0:2] thick = int((imh + imw) // 430) - for i in range(len(coords)): + for i, _v in enumerate(coords): if is_center: x, y, x2, y2 = prepro.obj_box_coord_centroid_to_upleft_butright(coords[i]) else: From e382a7fdc1d408fc97e87690315691cf5be1ed28 Mon Sep 17 00:00:00 2001 From: Luo Mai Date: Fri, 23 Feb 2018 20:06:51 +0800 Subject: [PATCH 06/10] fix whitespace tailing issue --- tensorlayer/cli/train.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/tensorlayer/cli/train.py b/tensorlayer/cli/train.py index 3df923f37..24744a479 100755 --- a/tensorlayer/cli/train.py +++ b/tensorlayer/cli/train.py @@ -7,17 +7,17 @@ (Alpha release - usage might change later) The tensorlayer.cli.train module provides the ``tl train`` subcommand. -It helps the user bootstrap a TensorFlow/TensorLayer program for distributed training +It helps the user bootstrap a TensorFlow/TensorLayer program for distributed training using multiple GPU cards or CPUs on a computer. -You need to first setup the `CUDA_VISIBLE_DEVICES `_ +You need to first setup the `CUDA_VISIBLE_DEVICES `_ to tell ``tl train`` which GPUs are available. If the CUDA_VISIBLE_DEVICES is not given, -``tl train`` would try best to discover all available GPUs. +``tl train`` would try best to discover all available GPUs. In distribute training, each TensorFlow program needs a TF_CONFIG environment variable to describe -the cluster. It also needs a master daemon to +the cluster. It also needs a master daemon to monitor all trainers. ``tl train`` is responsible -for automatically managing these two tasks. +for automatically managing these two tasks. Usage ----- @@ -25,7 +25,7 @@ tl train [-h] [-p NUM_PSS] [-c CPU_TRAINERS] [args [args ...]] .. code-block:: bash - + # example of using GPU 0 and 1 for training mnist CUDA_VISIBLE_DEVICES="0,1" tl train example/tutorial_mnist_distributed.py @@ -56,13 +56,13 @@ ----- A parallel training program would require multiple parameter servers to help parallel trainers to exchange intermediate gradients. -The best number of parameter servers is often proportional to the +The best number of parameter servers is often proportional to the size of your model as well as the number of CPUs available. You can control the number of parameter servers using the ``-p`` parameter. If you have a single computer with massive CPUs, you can use the ``-c`` parameter to enable CPU-only parallel training. -The reason we are not supporting GPU-CPU co-training is because GPU and +The reason we are not supporting GPU-CPU co-training is because GPU and CPU are running at different speeds. Using them together in training would incur stragglers. From 6d5b199c791e608a99fa3e313624e2c4c7bba736 Mon Sep 17 00:00:00 2001 From: Luo Mai Date: Fri, 23 Feb 2018 20:31:42 +0800 Subject: [PATCH 07/10] fix many pylint issues. --- ...ial_bipedalwalker_a3c_continuous_action.py | 2 +- ...torial_imagenet_inceptionV3_distributed.py | 4 +- example/tutorial_vgg16.py | 2 +- tensorlayer/db.py | 4 +- tensorlayer/files.py | 58 +----------------- tensorlayer/layers/convolution.py | 12 ++-- tensorlayer/layers/core.py | 20 ++++--- tensorlayer/layers/extend.py | 2 +- tensorlayer/layers/normalization.py | 3 +- tensorlayer/layers/recurrent.py | 60 ++++++++++--------- tensorlayer/layers/spatial_transformer.py | 6 +- tensorlayer/layers/special_activation.py | 2 +- tensorlayer/visualize.py | 17 +++--- 13 files changed, 69 insertions(+), 123 deletions(-) diff --git a/example/tutorial_bipedalwalker_a3c_continuous_action.py b/example/tutorial_bipedalwalker_a3c_continuous_action.py index 9b9c88b22..ae3f66736 100644 --- a/example/tutorial_bipedalwalker_a3c_continuous_action.py +++ b/example/tutorial_bipedalwalker_a3c_continuous_action.py @@ -210,7 +210,7 @@ def work(self): self.AC.v_target: buffer_v_target, } # update gradients on global network - test = self.AC.update_global(feed_dict) + self.AC.update_global(feed_dict) buffer_s, buffer_a, buffer_r = [], [], [] # update local network from global network diff --git a/example/tutorial_imagenet_inceptionV3_distributed.py b/example/tutorial_imagenet_inceptionV3_distributed.py index 4cb8b7fd3..7a3ceae08 100644 --- a/example/tutorial_imagenet_inceptionV3_distributed.py +++ b/example/tutorial_imagenet_inceptionV3_distributed.py @@ -288,12 +288,12 @@ def calculate_metrics(predicted_batch, real_batch, threshold=0.5, is_training=Fa def run_evaluator(task_spec, checkpoints_path, batch_size=32): with tf.Graph().as_default(): # load dataset - images_input, one_hot_classes, num_classes, dataset_size = \ + images_input, one_hot_classes, num_classes, _dataset_size = \ load_data(file=VAL_FILE, task_spec=task_spec, batch_size=batch_size, epochs=1) - network, predictions = build_network(images_input, num_classes=num_classes, is_training=False) + _network, predictions = build_network(images_input, num_classes=num_classes, is_training=False) saver = tf.train.Saver() # metrics metrics_init_ops, _, metrics_ops = \ diff --git a/example/tutorial_vgg16.py b/example/tutorial_vgg16.py index dda37025e..9a33aa693 100644 --- a/example/tutorial_vgg16.py +++ b/example/tutorial_vgg16.py @@ -48,7 +48,7 @@ def conv_layers(net_in): - with tf.name_scope('preprocess') as scope: + with tf.name_scope('preprocess'): """ Notice that we include a preprocessing layer that takes the RGB image with pixels values in the range of 0-255 and subtracts the mean image diff --git a/tensorlayer/db.py b/tensorlayer/db.py index d34c1b225..6b0b6c3fe 100644 --- a/tensorlayer/db.py +++ b/tensorlayer/db.py @@ -407,8 +407,8 @@ def find_one_job(self, args=None): def push_job(self, margs, wargs, dargs, epoch): - ms, mid = self.load_model_architecture(margs) - weight, wid = self.find_one_params(wargs) + _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} self.__autofill(args) self.db.JOBS.insert_one(args) diff --git a/tensorlayer/files.py b/tensorlayer/files.py index 712fedcc9..4b7ccbbd1 100644 --- a/tensorlayer/files.py +++ b/tensorlayer/files.py @@ -1171,8 +1171,8 @@ def save_npz(save_list=None, name='model.npz', sess=None): save_list_var = sess.run(save_list) else: try: - for k, value in enumerate(save_list): - save_list_var.append(value.eval()) + for _k, v in enumerate(save_list): + save_list_var.append(v.eval()) 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)") np.savez(name, params=save_list_var) @@ -1351,60 +1351,6 @@ def load_and_assign_npz_dict(name='model.npz', sess=None): logging.info("[*] Model restored from npz_dict %s" % name) -# def save_npz_dict(save_list=[], name='model.npz', sess=None): -# """Input parameters and the file name, save parameters as a dictionary into .npz file. Use tl.utils.load_npz_dict() to restore. -# -# Parameters -# ---------- -# save_list : a list -# Parameters want to be saved. -# name : a string or None -# The name of the .npz file. -# sess : None or Session -# -# Notes -# ----- -# This function tries to avoid a potential broadcasting error raised by numpy. -# -# """ -# ## save params into a list -# save_list_var = [] -# if sess: -# save_list_var = sess.run(save_list) -# else: -# try: -# for k, value in enumerate(save_list): -# save_list_var.append(value.eval()) -# except: -# logging.info(" Fail to save model, Hint: pass the session into this function, save_npz_dict(network.all_params, name='model.npz', sess=sess)") -# save_var_dict = {str(idx):val for idx, val in enumerate(save_list_var)} -# np.savez(name, **save_var_dict) -# save_list_var = None -# save_var_dict = None -# del save_list_var -# del save_var_dict -# logging.info("[*] %s saved" % name) -# -# def load_npz_dict(path='', name='model.npz'): -# """Load the parameters of a Model saved by tl.files.save_npz_dict(). -# -# Parameters -# ---------- -# path : a string -# Folder path to .npz file. -# name : a string or None -# The name of the .npz file. -# -# Returns -# -------- -# params : list -# A list of parameters in order. -# """ -# d = np.load( path+name ) -# saved_list_var = [val[1] for val in sorted(d.items(), key=lambda tup: int(tup[0]))] -# return saved_list_var - - 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. diff --git a/tensorlayer/layers/convolution.py b/tensorlayer/layers/convolution.py index 3fe01e6b8..748939716 100644 --- a/tensorlayer/layers/convolution.py +++ b/tensorlayer/layers/convolution.py @@ -180,7 +180,7 @@ def __init__( act = tf.identity logging.info("Conv2dLayer %s: shape:%s strides:%s pad:%s act:%s" % (self.name, str(shape), str(strides), padding, act.__name__)) - with tf.variable_scope(name) as vs: + with tf.variable_scope(name): W = tf.get_variable(name='W_conv2d', shape=shape, initializer=W_init, dtype=D_TYPE, **W_init_args) if b_init: b = tf.get_variable(name='b_conv2d', shape=(shape[-1]), initializer=b_init, dtype=D_TYPE, **b_init_args) @@ -511,7 +511,7 @@ def __init__( with tf.variable_scope(name) as vs: try: self.outputs = tf.image.resize_images(self.inputs, size=size, method=method, align_corners=align_corners) - except: # for TF 0.10 + 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.all_layers = list(layer.all_layers) @@ -571,7 +571,7 @@ def __init__( with tf.variable_scope(name) as vs: try: self.outputs = tf.image.resize_images(self.inputs, size=size, method=method, align_corners=align_corners) - except: # for TF 0.10 + 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.all_layers = list(layer.all_layers) @@ -792,7 +792,7 @@ def _tf_batch_map_offsets(inputs, offsets, grid_offset): try: pre_channel = int(layer.outputs.get_shape()[-1]) - except: # if pre_channel is ?, it happens when using Spatial Transformer Net + except Exception: # if pre_channel is ?, it happens when using Spatial Transformer Net pre_channel = 1 logging.info("[warnings] unknow input channels, set to 1") shape = (filter_size[0], filter_size[1], pre_channel, n_filter) @@ -1351,7 +1351,7 @@ def conv2d( try: pre_channel = int(layer.outputs.get_shape()[-1]) - except: # if pre_channel is ?, it happens when using Spatial Transformer Net + except Exception: # if pre_channel is ?, it happens when using Spatial Transformer Net pre_channel = 1 logging.info("[warnings] unknow input channels, set to 1") return Conv2dLayer( @@ -1616,7 +1616,7 @@ def __init__( logging.info("DepthwiseConv2d %s: shape:%s strides:%s pad:%s act:%s" % (self.name, str(shape), str(strides), padding, act.__name__)) try: pre_channel = int(layer.outputs.get_shape()[-1]) - except: # if pre_channel is ?, it happens when using Spatial Transformer Net + except Exception: # if pre_channel is ?, it happens when using Spatial Transformer Net pre_channel = 1 logging.info("[warnings] unknow input channels, set to 1") diff --git a/tensorlayer/layers/core.py b/tensorlayer/layers/core.py index 2d4e50e93..e251bf7a1 100644 --- a/tensorlayer/layers/core.py +++ b/tensorlayer/layers/core.py @@ -221,7 +221,7 @@ def get_variables_with_name(name=None, train_only=True, printable=False): else: try: # TF1.0+ t_vars = tf.global_variables() - except: # TF0.12 + except Exception: # TF0.12 t_vars = tf.all_variables() d_vars = [var for var in t_vars if name in var.name] @@ -418,13 +418,13 @@ def print_layers(self): def count_params(self): """Return the number of parameters in the network""" n_params = 0 - for i, p in enumerate(self.all_params): + for _i, p in enumerate(self.all_params): n = 1 # for s in p.eval().shape: for s in p.get_shape(): try: s = int(s) - except: + except Exception: s = 1 if s: n = n * s @@ -610,6 +610,7 @@ def __init__( Layer.__init__(self, name=name) self.inputs = inputs logging.info("Word2vecEmbeddingInputlayer %s: (%d, %d)" % (self.name, vocabulary_size, embedding_size)) + # Look up embeddings for inputs. # Note: a row of 'embeddings' is the vector representation of a word. # for the sake of speed, it is better to slice the embedding matrix @@ -617,7 +618,7 @@ def __init__( # multiply by the embedding matrix. # embed is the outputs of the hidden layer (embedding layer), it is a # row vector with 'embedding_size' values. - with tf.variable_scope(name) as vs: + with tf.variable_scope(name): embeddings = tf.get_variable(name='embeddings', shape=(vocabulary_size, embedding_size), initializer=E_init, dtype=D_TYPE, **E_init_args) embed = tf.nn.embedding_lookup(embeddings, self.inputs) # Construct the variables for the NCE loss (i.e. negative sampling) @@ -878,7 +879,7 @@ def __init__( if b_init is not None: try: b = tf.get_variable(name='b', shape=(n_units), initializer=b_init, dtype=D_TYPE, **b_init_args) - except: # If initializer is a constant, do not specify shape. + except Exception: # If initializer is a constant, do not specify shape. b = tf.get_variable(name='b', initializer=b_init, dtype=D_TYPE, **b_init_args) self.outputs = act(tf.matmul(self.inputs, W) + b) else: @@ -982,10 +983,11 @@ def __init__( L2_w = tf.contrib.layers.l2_regularizer(lambda_l2_w)(self.train_params[0]) \ + tf.contrib.layers.l2_regularizer(lambda_l2_w)(self.train_params[2]) # faster than the code below # L2_w = lambda_l2_w * tf.reduce_mean(tf.square(self.train_params[0])) + lambda_l2_w * tf.reduce_mean( tf.square(self.train_params[2])) + # DropNeuro - P_o = cost.lo_regularizer(0.03)( - self.train_params[0]) # + cost.lo_regularizer(0.5)(self.train_params[2]) # : if add lo on decoder, no neuron will be broken - P_i = cost.li_regularizer(0.03)(self.train_params[0]) # + cost.li_regularizer(0.001)(self.train_params[2]) + # P_o = cost.lo_regularizer(0.03)( + # self.train_params[0]) # + cost.lo_regularizer(0.5)(self.train_params[2]) # : if add lo on decoder, no neuron will be broken + # P_i = cost.li_regularizer(0.03)(self.train_params[0]) # + cost.li_regularizer(0.001)(self.train_params[2]) # L1 of activation outputs activation_out = self.all_layers[-2] @@ -1082,7 +1084,7 @@ def pretrain(self, sess, x, X_train, X_val, denoise_name=None, n_epoch=100, batc visualize.draw_weights( 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: + except Exception: raise Exception( "You should change the visualize.W() in ReconLayer.pretrain(), if you want to save the feature images for different dataset") diff --git a/tensorlayer/layers/extend.py b/tensorlayer/layers/extend.py index ca3cb2baa..e9a96fee9 100644 --- a/tensorlayer/layers/extend.py +++ b/tensorlayer/layers/extend.py @@ -29,7 +29,7 @@ def __init__( self.inputs = layer.outputs logging.info("ExpandDimsLayer %s: axis:%d" % (self.name, axis)) - with tf.variable_scope(name) as vs: + with tf.variable_scope(name): try: # TF12 TF1.0 self.outputs = tf.expand_dims(self.inputs, axis=axis) except Exception: # TF11 diff --git a/tensorlayer/layers/normalization.py b/tensorlayer/layers/normalization.py index 0c0594d7b..1c328998a 100644 --- a/tensorlayer/layers/normalization.py +++ b/tensorlayer/layers/normalization.py @@ -99,9 +99,8 @@ def __init__( params_shape = x_shape[-1:] from tensorflow.python.training import moving_averages - from tensorflow.python.ops import control_flow_ops - with tf.variable_scope(name) as vs: + with tf.variable_scope(name): axis = list(range(len(x_shape) - 1)) # 1. beta, gamma diff --git a/tensorlayer/layers/recurrent.py b/tensorlayer/layers/recurrent.py index 6dd0d8f81..366ada8f5 100644 --- a/tensorlayer/layers/recurrent.py +++ b/tensorlayer/layers/recurrent.py @@ -147,7 +147,7 @@ def __init__( if 'GRU' in cell_fn.__name__: try: cell_init_args.pop('state_is_tuple') - except: + except Exception: pass self.inputs = layer.outputs @@ -162,7 +162,7 @@ def __init__( # Input dimension should be rank 3 [batch_size, n_steps(max), n_features] try: self.inputs.get_shape().with_rank(3) - except: + except Exception: raise Exception("RNN : Input dimension should be rank 3 : [batch_size, n_steps, n_features]") # is_reshape : boolean (deprecate) @@ -223,7 +223,7 @@ def __init__( # 2D Tensor [n_example, n_hidden] try: # TF1.0 self.outputs = tf.reshape(tf.concat(outputs, 1), [-1, n_hidden]) - except: # TF0.12 + except Exception: # TF0.12 self.outputs = tf.reshape(tf.concat(1, outputs), [-1, n_hidden]) else: @@ -231,7 +231,7 @@ def __init__( # 3D Tensor [n_example/n_steps, n_steps, n_hidden] try: # TF1.0 self.outputs = tf.reshape(tf.concat(outputs, 1), [-1, n_steps, n_hidden]) - except: # TF0.12 + except Exception: # TF0.12 self.outputs = tf.reshape(tf.concat(1, outputs), [-1, n_steps, n_hidden]) self.final_state = state @@ -337,7 +337,7 @@ def __init__( if 'GRU' in cell_fn.__name__: try: cell_init_args.pop('state_is_tuple') - except: + except Exception: pass self.inputs = layer.outputs @@ -360,7 +360,7 @@ def __init__( # Input dimension should be rank 3 [batch_size, n_steps(max), n_features] try: self.inputs.get_shape().with_rank(3) - except: + except Exception: raise Exception("RNN : Input dimension should be rank 3 : [batch_size, n_steps, n_features]") with tf.variable_scope(name, initializer=initializer) as vs: @@ -376,7 +376,7 @@ def __init__( raise Exception("Invalid dropout type (must be a 2-D tuple of " "float)") try: # TF 1.0 DropoutWrapper_fn = tf.contrib.rnn.DropoutWrapper - except: + except Exception: DropoutWrapper_fn = tf.nn.rnn_cell.DropoutWrapper cell_creator = lambda: DropoutWrapper_fn(rnn_creator(), input_keep_prob=in_keep_prob, output_keep_prob=1.0) # out_keep_prob) else: @@ -388,13 +388,13 @@ def __init__( if n_layer > 1: try: # TF1.0 MultiRNNCell_fn = tf.contrib.rnn.MultiRNNCell - except: + except Exception: MultiRNNCell_fn = tf.nn.rnn_cell.MultiRNNCell try: self.fw_cell = MultiRNNCell_fn([cell_creator() for _ in range(n_layer)], state_is_tuple=True) self.bw_cell = MultiRNNCell_fn([cell_creator() for _ in range(n_layer)], state_is_tuple=True) - except: + except Exception: self.fw_cell = MultiRNNCell_fn([cell_creator() for _ in range(n_layer)]) self.bw_cell = MultiRNNCell_fn([cell_creator() for _ in range(n_layer)]) @@ -411,12 +411,12 @@ def __init__( # Feedforward to MultiRNNCell try: # TF1.0 list_rnn_inputs = tf.unstack(self.inputs, axis=1) - except: # TF0.12 + except Exception: # TF0.12 list_rnn_inputs = tf.unpack(self.inputs, axis=1) try: # TF1.0 bidirectional_rnn_fn = tf.contrib.rnn.static_bidirectional_rnn - except: + except Exception: bidirectional_rnn_fn = tf.nn.bidirectional_rnn outputs, fw_state, bw_state = bidirectional_rnn_fn( # outputs, fw_state, bw_state = tf.contrib.rnn.static_bidirectional_rnn( cell_fw=self.fw_cell, @@ -434,7 +434,7 @@ def __init__( # 2D Tensor [n_example, n_hidden] try: # TF1.0 self.outputs = tf.reshape(tf.concat(outputs, 1), [-1, n_hidden * 2]) - except: # TF0.12 + except Exception: # TF0.12 self.outputs = tf.reshape(tf.concat(1, outputs), [-1, n_hidden * 2]) else: # : stack more RNN layer after that @@ -442,7 +442,7 @@ def __init__( try: # TF1.0 self.outputs = tf.reshape(tf.concat(outputs, 1), [-1, n_steps, n_hidden * 2]) - except: # TF0.12 + except Exception: # TF0.12 self.outputs = tf.reshape(tf.concat(1, outputs), [-1, n_steps, n_hidden * 2]) self.fw_final_state = fw_state self.bw_final_state = bw_state @@ -697,7 +697,7 @@ def __init__( # Input dimension should be rank 5 [batch_size, n_steps(max), h, w, c] try: self.inputs.get_shape().with_rank(5) - except: + except Exception: 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] @@ -913,7 +913,7 @@ class DynamicRNNLayer(Layer): A TensorFlow core RNN cell - See `RNN Cells in TensorFlow `__ - Note TF1.0+ and TF1.0- are different - cell_init_args : dictionary + cell_init_args : dictionary or None The arguments for the cell function. n_hidden : int The number of hidden units in the layer. @@ -932,7 +932,7 @@ class DynamicRNNLayer(Layer): - If one int, input and output keep probability are the same. n_layer : int The number of RNN layers, default is 1. - return_last : boolean + return_last : boolean or None Whether return last output or all outputs in each step. - If True, return the last output, "Sequence input and single output" - If False, return all outputs, "Synced sequence input and output" @@ -1004,20 +1004,24 @@ def __init__( self, layer, cell_fn, #tf.nn.rnn_cell.LSTMCell, - cell_init_args={'state_is_tuple': True}, + 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=False, + return_last=None, return_seq_2d=False, dynamic_rnn_init_args=None, name='dyrnn', ): if dynamic_rnn_init_args is None: dynamic_rnn_init_args = {} + if cell_init_args is None: + cell_init_args = {'state_is_tuple': True} + if return_last is None: + return_last = True Layer.__init__(self, name=name) if cell_fn is None: @@ -1025,7 +1029,7 @@ def __init__( if 'GRU' in cell_fn.__name__: try: cell_init_args.pop('state_is_tuple') - except: + except Exception: pass self.inputs = layer.outputs @@ -1035,7 +1039,7 @@ def __init__( # Input dimension should be rank 3 [batch_size, n_steps(max), n_features] try: self.inputs.get_shape().with_rank(3) - except: + except Exception: raise Exception("RNN : Input dimension should be rank 3 : [batch_size, n_steps(max), n_features]") # Get the batch_size @@ -1064,7 +1068,7 @@ def __init__( raise Exception("Invalid dropout type (must be a 2-D tuple of " "float)") try: # TF1.0 DropoutWrapper_fn = tf.contrib.rnn.DropoutWrapper - except: + except Exception: DropoutWrapper_fn = tf.nn.rnn_cell.DropoutWrapper # cell_instance_fn1=cell_instance_fn # HanSheng @@ -1080,14 +1084,14 @@ def __init__( if n_layer > 1: try: MultiRNNCell_fn = tf.contrib.rnn.MultiRNNCell - except: + except Exception: MultiRNNCell_fn = tf.nn.rnn_cell.MultiRNNCell # cell_instance_fn2=cell_instance_fn # HanSheng 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() for _ in range(n_layer)], state_is_tuple=True) - except: # when GRU + 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() for _ in range(n_layer)]) @@ -1103,7 +1107,7 @@ 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)) - except: # TF0.12 + except Exception: # TF0.12 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 @@ -1133,7 +1137,7 @@ def __init__( # 2D Tensor [n_example, n_hidden] try: # TF1.0 self.outputs = tf.reshape(tf.concat(outputs, 1), [-1, n_hidden]) - except: # TF0.12 + except Exception: # TF0.12 self.outputs = tf.reshape(tf.concat(1, outputs), [-1, n_hidden]) else: # : @@ -1143,7 +1147,7 @@ def __init__( try: # TF1.0 self.outputs = tf.reshape(tf.concat(outputs, 1), [batch_size, max_length, n_hidden]) - except: # TF0.12 + except Exception: # TF0.12 self.outputs = tf.reshape(tf.concat(1, outputs), [batch_size, max_length, n_hidden]) # self.outputs = tf.reshape(tf.concat(1, outputs), [-1, max_length, n_hidden]) @@ -1395,7 +1399,7 @@ def __init__( batch_size = tf.shape(outputs)[0] try: # TF1.0 self.outputs = tf.reshape(tf.concat(outputs, 1), [batch_size, max_length, 2 * n_hidden]) - except: # TF0.12 + except Exception: # TF0.12 self.outputs = tf.reshape(tf.concat(1, outputs), [batch_size, max_length, 2 * n_hidden]) # self.outputs = tf.reshape(tf.concat(1, outputs), [-1, max_length, 2 * n_hidden]) @@ -1551,7 +1555,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)) - with tf.variable_scope(name) as vs: #, reuse=reuse): + with tf.variable_scope(name): # tl.layers.set_name_reuse(reuse) # network = InputLayer(self.inputs, name=name+'/input') network_encode = DynamicRNNLayer( diff --git a/tensorlayer/layers/spatial_transformer.py b/tensorlayer/layers/spatial_transformer.py index e3af5789d..afdd4b91b 100644 --- a/tensorlayer/layers/spatial_transformer.py +++ b/tensorlayer/layers/spatial_transformer.py @@ -135,15 +135,11 @@ def _meshgrid(height, width): def _transform(theta, input_dim, out_size): with tf.variable_scope('_transform'): num_batch = tf.shape(input_dim)[0] - height = tf.shape(input_dim)[1] - width = tf.shape(input_dim)[2] num_channels = tf.shape(input_dim)[3] theta = tf.reshape(theta, (-1, 2, 3)) theta = tf.cast(theta, 'float32') # grid of (x_t, y_t, 1), eq (1) in ref [1] - height_f = tf.cast(height, 'float32') - width_f = tf.cast(width, 'float32') out_height = out_size[0] out_width = out_size[1] grid = _meshgrid(out_height, out_width) @@ -260,7 +256,7 @@ def __init__( else: from tensorflow.python.ops import array_ops batch_size = array_ops.shape(self.inputs)[0] - size = self.inputs.get_shape().as_list() + n_channels = self.inputs.get_shape().as_list()[-1] # logging.info(self.outputs) self.outputs = tf.reshape(self.outputs, shape=[batch_size, out_size[0], out_size[1], n_channels]) diff --git a/tensorlayer/layers/special_activation.py b/tensorlayer/layers/special_activation.py index 16a83c6e2..576f353e1 100644 --- a/tensorlayer/layers/special_activation.py +++ b/tensorlayer/layers/special_activation.py @@ -46,7 +46,7 @@ def __init__( w_shape = int(self.inputs.get_shape()[-1]) # with tf.name_scope(name) as scope: - with tf.variable_scope(name) as vs: + with tf.variable_scope(name): alphas = tf.get_variable(name='alphas', shape=w_shape, initializer=a_init, dtype=D_TYPE, **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/visualize.py b/tensorlayer/visualize.py index ff19ee822..3cec6225b 100644 --- a/tensorlayer/visualize.py +++ b/tensorlayer/visualize.py @@ -76,7 +76,7 @@ def save_image(image, image_path=''): """ try: # RGB scipy.misc.imsave(image_path, image) - except: # Greyscale + except Exception: # Greyscale scipy.misc.imsave(image_path, image[:, :, 0]) @@ -235,7 +235,7 @@ def frame(I=None, second=5, saveable=True, name='frame', cmap=None, fig_idx=1283 import matplotlib.pyplot as plt if saveable is False: plt.ion() - fig = plt.figure(fig_idx) # show all feature images + plt.figure(fig_idx) # show all feature images if len(I.shape) and I.shape[-1] == 1: # (10,10,1) --> (10,10) I = I[:, :, 0] @@ -285,8 +285,8 @@ def CNN2d(CNN=None, second=10, saveable=True, name='cnn', fig_idx=3119362): plt.ion() # active mode fig = plt.figure(fig_idx) count = 1 - for ir in range(1, row + 1): - for ic in range(1, col + 1): + for _ir in range(1, row + 1): + for _ic in range(1, col + 1): if count > n_mask: break a = fig.add_subplot(col, row, count) @@ -353,7 +353,7 @@ def images2d(images=None, second=10, saveable=True, name='images', dtype=None, f for ic in range(1, col + 1): if count > n_mask: break - a = fig.add_subplot(col, row, count) + fig.add_subplot(col, row, count) # logging.info(images[:,:,:,count-1].shape, n_row, n_col) # (5, 1, 32) 5 5 # plt.imshow( # np.reshape(images[count-1,:,:,:], (n_row, n_col)), @@ -467,17 +467,16 @@ def draw_weights(W=None, second=10, saveable=True, shape=None, name='mnist', fig if saveable is False: plt.ion() fig = plt.figure(fig_idx) # show all feature images - size = W.shape[0] n_units = W.shape[1] num_r = int(np.sqrt(n_units)) # 每行显示的个数 若25个hidden unit -> 每行显示5个 num_c = int(np.ceil(n_units / num_r)) count = int(1) - for row in range(1, num_r + 1): - for col in range(1, num_c + 1): + for _row in range(1, num_r + 1): + for _col in range(1, num_c + 1): if count > n_units: break - a = fig.add_subplot(num_r, num_c, count) + fig.add_subplot(num_r, num_c, count) # ------------------------------------------------------------ # plt.imshow(np.reshape(W[:,count-1],(28,28)), cmap='gray') # ------------------------------------------------------------ From dd8e778bed30b7d10d35bdf02e14eb952ddc87cf Mon Sep 17 00:00:00 2001 From: Luo Mai Date: Fri, 23 Feb 2018 20:38:13 +0800 Subject: [PATCH 08/10] add warning message to failed pop. --- tensorlayer/_logging.py | 4 ++++ tensorlayer/layers/recurrent.py | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/tensorlayer/_logging.py b/tensorlayer/_logging.py index 6e8f11da2..09a4a0543 100644 --- a/tensorlayer/_logging.py +++ b/tensorlayer/_logging.py @@ -5,3 +5,7 @@ def info(fmt, *args): logging.info(fmt, *args) + + +def warning(fmt, *args): + logging.warning(fmt, *args) \ No newline at end of file diff --git a/tensorlayer/layers/recurrent.py b/tensorlayer/layers/recurrent.py index 366ada8f5..040c0a370 100644 --- a/tensorlayer/layers/recurrent.py +++ b/tensorlayer/layers/recurrent.py @@ -148,7 +148,7 @@ def __init__( try: cell_init_args.pop('state_is_tuple') except Exception: - pass + logging.warning('pop state_is_tuple fails.') self.inputs = layer.outputs From 3c450db4d25715079ea32b3ff85da37b587149ec Mon Sep 17 00:00:00 2001 From: Luo Mai Date: Fri, 23 Feb 2018 20:41:32 +0800 Subject: [PATCH 09/10] address guo's comment --- tensorlayer/files.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tensorlayer/files.py b/tensorlayer/files.py index 4b7ccbbd1..b866f0dbc 100644 --- a/tensorlayer/files.py +++ b/tensorlayer/files.py @@ -1160,7 +1160,7 @@ def save_npz(save_list=None, name='model.npz', sess=None): References ---------- - - `Saving dictionary using numpy `__ + `Saving dictionary using numpy `__ """ if save_list is None: @@ -1171,8 +1171,7 @@ def save_npz(save_list=None, name='model.npz', sess=None): save_list_var = sess.run(save_list) else: try: - for _k, v in enumerate(save_list): - save_list_var.append(v.eval()) + 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)") np.savez(name, params=save_list_var) From e5629cae30bd2978af7e0c2714294db5bb52ee5f Mon Sep 17 00:00:00 2001 From: Luo Mai Date: Fri, 23 Feb 2018 20:43:23 +0800 Subject: [PATCH 10/10] fix try except pass issue. --- tensorlayer/layers/recurrent.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tensorlayer/layers/recurrent.py b/tensorlayer/layers/recurrent.py index 040c0a370..e16dc0985 100644 --- a/tensorlayer/layers/recurrent.py +++ b/tensorlayer/layers/recurrent.py @@ -338,7 +338,7 @@ def __init__( try: cell_init_args.pop('state_is_tuple') except Exception: - pass + logging.warning("pop state_is_tuple fails.") self.inputs = layer.outputs @@ -1030,7 +1030,7 @@ def __init__( try: cell_init_args.pop('state_is_tuple') except Exception: - pass + logging.warning("pop state_is_tuple fails.") self.inputs = layer.outputs logging.info("DynamicRNNLayer %s: n_hidden:%d, in_dim:%d in_shape:%s cell_fn:%s dropout:%s n_layer:%d" % @@ -1273,7 +1273,7 @@ def __init__( try: cell_init_args.pop('state_is_tuple') except Exception: - pass + logging.warning("pop state_is_tuple fails.") self.inputs = layer.outputs logging.info("BiDynamicRNNLayer %s: n_hidden:%d in_dim:%d in_shape:%s cell_fn:%s dropout:%s n_layer:%d" % @@ -1551,7 +1551,7 @@ def __init__( try: cell_init_args.pop('state_is_tuple') except Exception: - pass + 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))