From 2ac731a999c3ad0a5286766785e33501d128c531 Mon Sep 17 00:00:00 2001 From: Luo Mai Date: Sat, 24 Feb 2018 15:49:56 +0800 Subject: [PATCH 1/2] fix pulint issues --- tensorlayer/layers/convolution.py | 8 ++++---- tensorlayer/layers/core.py | 4 ++-- tensorlayer/layers/recurrent.py | 9 ++++++--- tensorlayer/prepro.py | 4 ++-- 4 files changed, 14 insertions(+), 11 deletions(-) diff --git a/tensorlayer/layers/convolution.py b/tensorlayer/layers/convolution.py index ce14ac931..7d806b155 100644 --- a/tensorlayer/layers/convolution.py +++ b/tensorlayer/layers/convolution.py @@ -568,7 +568,7 @@ def __init__( else: raise Exception("Donot support shape %s" % self.inputs.get_shape()) logging.info("DownSampling2dLayer %s: is_scale:%s size:%s method:%d, align_corners:%s" % (name, is_scale, size, method, align_corners)) - with tf.variable_scope(name) as vs: + with tf.variable_scope(name): try: self.outputs = tf.image.resize_images(self.inputs, size=size, method=method, align_corners=align_corners) except Exception: # for TF 0.10 @@ -797,7 +797,7 @@ def _tf_batch_map_offsets(inputs, offsets, grid_offset): logging.info("[warnings] unknow input channels, set to 1") shape = (filter_size[0], filter_size[1], pre_channel, n_filter) - with tf.variable_scope(name) as vs: + with tf.variable_scope(name): offset = self.offset_layer.outputs assert offset.get_shape()[-1] == 2 * shape[0] * shape[1] @@ -987,7 +987,7 @@ def __init__(self, if act is None: act = tf.identity logging.info("AtrousConv2dLayer %s: n_filter:%d filter_size:%s rate:%d pad:%s act:%s" % (self.name, n_filter, filter_size, rate, padding, act.__name__)) - with tf.variable_scope(name) as vs: + with tf.variable_scope(name): shape = [filter_size[0], filter_size[1], int(self.inputs.get_shape()[-1]), n_filter] filters = tf.get_variable(name='filter', shape=shape, initializer=W_init, dtype=D_TYPE, **W_init_args) if b_init: @@ -1627,7 +1627,7 @@ def __init__( assert len(strides) == 4, "len(strides) should be 4." - with tf.variable_scope(name) as vs: + with tf.variable_scope(name): W = tf.get_variable( name='W_sepconv2d', shape=shape, initializer=W_init, dtype=D_TYPE, **W_init_args) # [filter_height, filter_width, in_channels, channel_multiplier] diff --git a/tensorlayer/layers/core.py b/tensorlayer/layers/core.py index 3bcf70173..523104f5d 100644 --- a/tensorlayer/layers/core.py +++ b/tensorlayer/layers/core.py @@ -1323,10 +1323,10 @@ def __init__( self.n_units = n_units logging.info("DropconnectDenseLayer %s: %d %s" % (self.name, self.n_units, act.__name__)) - with tf.variable_scope(name) as vs: + with tf.variable_scope(name): W = tf.get_variable(name='W', shape=(n_in, n_units), initializer=W_init, dtype=D_TYPE, **W_init_args) b = tf.get_variable(name='b', shape=(n_units), initializer=b_init, dtype=D_TYPE, **b_init_args) - self.outputs = act(tf.matmul(self.inputs, W) + b) #, name=name) # 1.2 + self.outputs = act(tf.matmul(self.inputs, W) + b) set_keep[name] = tf.placeholder(tf.float32) W_dropcon = tf.nn.dropout(W, set_keep[name]) diff --git a/tensorlayer/layers/recurrent.py b/tensorlayer/layers/recurrent.py index 5269aff54..c985921bf 100644 --- a/tensorlayer/layers/recurrent.py +++ b/tensorlayer/layers/recurrent.py @@ -1062,7 +1062,7 @@ def __init__( # Apply dropout if dropout: - if type(dropout) in [tuple, list]: + if isinstance(dropout, (tuple, list)): in_keep_prob = dropout[0] out_keep_prob = dropout[1] elif isinstance(dropout, float): @@ -1445,7 +1445,7 @@ class Seq2Seq(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 initializer. n_hidden : int The number of hidden units in the layer. @@ -1540,7 +1540,7 @@ def __init__( net_encode_in, net_decode_in, 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), encode_sequence_length=None, @@ -1552,6 +1552,9 @@ def __init__( return_seq_2d=False, name='seq2seq', ): + if cell_init_args is None: + cell_init_args = {'state_is_tuple': True} + Layer.__init__(self, name=name) if cell_fn is None: raise Exception("Please put in cell_fn") diff --git a/tensorlayer/prepro.py b/tensorlayer/prepro.py index af4d5d423..157075520 100644 --- a/tensorlayer/prepro.py +++ b/tensorlayer/prepro.py @@ -2651,7 +2651,7 @@ def _get_coord(coord): coords_new = list() classes_new = list() - for i in range(len(coords)): + for i, _ in enumerate(coords): coord = coords[i] assert len(coord) == 4, "coordinate should be 4 values : [x, y, w, h]" if is_rescale: @@ -3010,7 +3010,7 @@ def sequences_add_start_id(sequences, start_id=0, remove_last=False): """ sequences_out = [[] for _ in range(len(sequences))] #[[]] * len(sequences) - for i in range(len(sequences)): + for i, _ in enumerate(sequences): if remove_last: sequences_out[i] = [start_id] + sequences[i][:-1] else: From 30866906106ae3ce26062952ac435f068a22c7c7 Mon Sep 17 00:00:00 2001 From: Luo Mai Date: Sat, 24 Feb 2018 17:44:26 +0800 Subject: [PATCH 2/2] fix more issues. --- tensorlayer/layers/convolution.py | 2 +- tensorlayer/layers/recurrent.py | 2 +- tensorlayer/prepro.py | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tensorlayer/layers/convolution.py b/tensorlayer/layers/convolution.py index 7d806b155..3697de4fb 100644 --- a/tensorlayer/layers/convolution.py +++ b/tensorlayer/layers/convolution.py @@ -508,7 +508,7 @@ def __init__( else: raise Exception("Donot support shape %s" % self.inputs.get_shape()) logging.info("UpSampling2dLayer %s: is_scale:%s size:%s method:%d align_corners:%s" % (name, is_scale, size, method, align_corners)) - with tf.variable_scope(name) as vs: + with tf.variable_scope(name): try: self.outputs = tf.image.resize_images(self.inputs, size=size, method=method, align_corners=align_corners) except Exception: # for TF 0.10 diff --git a/tensorlayer/layers/recurrent.py b/tensorlayer/layers/recurrent.py index c985921bf..3204b8e1b 100644 --- a/tensorlayer/layers/recurrent.py +++ b/tensorlayer/layers/recurrent.py @@ -1311,7 +1311,7 @@ def __init__( # Apply dropout if dropout: - if type(dropout) in [tuple, list]: + if isinstance(dropout, (tuple, list)): in_keep_prob = dropout[0] out_keep_prob = dropout[1] elif isinstance(dropout, float): diff --git a/tensorlayer/prepro.py b/tensorlayer/prepro.py index 157075520..02b87abf3 100644 --- a/tensorlayer/prepro.py +++ b/tensorlayer/prepro.py @@ -2794,7 +2794,7 @@ def _get_coord(coord): coords_new = list() classes_new = list() - for i in range(len(coords)): + for i, _ in enumerate(coords): coord = coords[i] assert len(coord) == 4, "coordinate should be 4 values : [x, y, w, h]" if is_rescale: @@ -3041,7 +3041,7 @@ def sequences_add_end_id(sequences, end_id=888): """ sequences_out = [[] for _ in range(len(sequences))] #[[]] * len(sequences) - for i in range(len(sequences)): + for i, _ in enumerate(sequences): sequences_out[i] = sequences[i] + [end_id] return sequences_out