Skip to content

Commit cb33980

Browse files
zsdonghaoluomai
authored andcommitted
Fix codacy issues (continue) (#349)
* fixed rein.py | try Exception * fixed deformable conv2d bug (#346) * improve docs * fixed bug of deformable conv2d * 1) try exception with Exception / 2) use enumerate instead of range(l… (#347) * 1) try exception with Exception / 2) use enumerate instead of range(len()) * remove unused import in tfrecord3.py example * try Exception in tfrecord3.py
1 parent e14a898 commit cb33980

File tree

4 files changed

+16
-14
lines changed

4 files changed

+16
-14
lines changed

example/tutorial_tfrecord3.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,12 @@
1515
1616
"""
1717

18-
import io
1918
import json
2019
import os
21-
import time
2220
import numpy as np
2321
import tensorflow as tf
2422
import tensorlayer as tl
2523
from PIL import Image
26-
from tensorlayer.layers import set_keep
2724

2825

2926
def _int64_feature(value):
@@ -326,7 +323,7 @@ def prefetch_input_data(reader,
326323
try:
327324
# for TensorFlow 0.11
328325
img = tf.image.resize_images(img, size=(resize_height, resize_width), method=tf.image.ResizeMethod.BILINEAR)
329-
except:
326+
except Exception:
330327
# for TensorFlow 0.10
331328
img = tf.image.resize_images(img, new_height=resize_height, new_width=resize_width, method=tf.image.ResizeMethod.BILINEAR)
332329
# Crop to final dimensions.

tensorlayer/layers/convolution.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -834,9 +834,9 @@ def _tf_batch_map_offsets(inputs, offsets, grid_offset):
834834
offset_params = [osparam for osparam in offset_layer.all_params if osparam not in layer.all_params]
835835
offset_layers = [oslayer for oslayer in offset_layer.all_layers if oslayer not in layer.all_layers]
836836

837-
self.all_params.extend(offset_params)
838-
self.all_layers.extend(offset_layers)
839-
self.all_drop.update(offset_layer.all_drop)
837+
self.all_params.extend(list(offset_params))
838+
self.all_layers.extend(list(offset_layers))
839+
self.all_drop.update(dict(offset_layer.all_drop))
840840

841841
# this layer
842842
self.all_layers.extend([self.outputs])

tensorlayer/prepro.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,10 @@ def apply_fn(results, i, data, kwargs):
100100
if thread_count is None:
101101
results = [None] * len(data)
102102
threads = []
103-
for i in range(len(data)):
104-
t = threading.Thread(name='threading_and_return', target=apply_fn, args=(results, i, data[i], kwargs))
103+
# for i in range(len(data)):
104+
# t = threading.Thread(name='threading_and_return', target=apply_fn, args=(results, i, data[i], kwargs))
105+
for i, d in enumerate(data):
106+
t = threading.Thread(name='threading_and_return', target=apply_fn, args=(results, i, d, kwargs))
105107
t.start()
106108
threads.append(t)
107109
else:
@@ -120,7 +122,7 @@ def apply_fn(results, i, data, kwargs):
120122
if thread_count is None:
121123
try:
122124
return np.asarray(results)
123-
except:
125+
except Exception:
124126
return results
125127
else:
126128
return np.concatenate(results)

tensorlayer/rein.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from six.moves import xrange
77

88

9-
def discount_episode_rewards(rewards=[], gamma=0.99, mode=0):
9+
def discount_episode_rewards(rewards=None, gamma=0.99, mode=0):
1010
"""Take 1D float array of rewards and compute discounted rewards for an
1111
episode. When encount a non-zero value, consider as the end a of an episode.
1212
@@ -40,6 +40,8 @@ def discount_episode_rewards(rewards=[], gamma=0.99, mode=0):
4040
... 1.49048996 1.65610003 0.72899997 0.81 0.89999998 1. ]
4141
4242
"""
43+
if rewards is None:
44+
raise Exception("rewards should be a list")
4345
discounted_r = np.zeros_like(rewards, dtype=np.float32)
4446
running_add = 0
4547
for t in reversed(xrange(0, rewards.size)):
@@ -84,13 +86,13 @@ def cross_entropy_reward_loss(logits, actions, rewards, name=None):
8486
"""
8587
try: # TF 1.0+
8688
cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(labels=actions, logits=logits, name=name)
87-
except:
89+
except Exception:
8890
cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, targets=actions)
8991
# cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits, actions)
9092

9193
try: ## TF1.0+
9294
loss = tf.reduce_sum(tf.multiply(cross_entropy, rewards))
93-
except: ## TF0.12
95+
except Exception: ## TF0.12
9496
loss = tf.reduce_sum(tf.mul(cross_entropy, rewards)) # element-wise mul
9597
return loss
9698

@@ -153,5 +155,6 @@ def choice_action_by_probs(probs=[0.5, 0.5], action_list=None):
153155
n_action = len(probs)
154156
action_list = np.arange(n_action)
155157
else:
156-
assert len(action_list) == len(probs), "Number of actions should equal to number of probabilities."
158+
if len(action_list) != len(probs):
159+
raise Exception("number of actions should equal to number of probabilities.")
157160
return np.random.choice(action_list, p=probs)

0 commit comments

Comments
 (0)