diff --git a/docs/index.rst b/docs/index.rst index 8322e2ae5..b7cdf4618 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -51,13 +51,9 @@ method, this part of the documentation is for you. modules/rein modules/files modules/visualize - modules/ops modules/activation modules/distributed -.. - modules/db - Command-line Reference ---------------------- diff --git a/docs/modules/activation.rst b/docs/modules/activation.rst old mode 100755 new mode 100644 diff --git a/docs/modules/cost.rst b/docs/modules/cost.rst old mode 100755 new mode 100644 diff --git a/docs/modules/distributed.rst b/docs/modules/distributed.rst old mode 100755 new mode 100644 diff --git a/docs/modules/files.rst b/docs/modules/files.rst old mode 100755 new mode 100644 diff --git a/docs/modules/iterate.rst b/docs/modules/iterate.rst old mode 100755 new mode 100644 diff --git a/docs/modules/layers.rst b/docs/modules/layers.rst old mode 100755 new mode 100644 diff --git a/docs/modules/nlp.rst b/docs/modules/nlp.rst old mode 100755 new mode 100644 diff --git a/docs/modules/ops.rst b/docs/modules/ops.rst deleted file mode 100755 index e158b7751..000000000 --- a/docs/modules/ops.rst +++ /dev/null @@ -1,43 +0,0 @@ -API - Operation System -======================= - -Operation system, more functions can be found in `TensorFlow API `_. - -.. automodule:: tensorlayer.ops - -.. autosummary:: - - exit_tf - open_tb - clear_all - set_gpu_fraction - get_site_packages_directory - empty_trash - -TensorFlow functions ---------------------------- - -Close TF session and associated processes -^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -.. autofunction:: exit_tf - -Open TensorBoard -^^^^^^^^^^^^^^^^^^^ -.. autofunction:: open_tb - -Delete placeholder -^^^^^^^^^^^^^^^^^^^^^^^^ -.. autofunction:: clear_all - -GPU functions ---------------------------- -.. autofunction:: set_gpu_fraction - - -Site packages information ----------------------------- -.. autofunction:: get_site_packages_directory - -Trash -------- -.. autofunction:: empty_trash diff --git a/docs/modules/prepro.rst b/docs/modules/prepro.rst old mode 100755 new mode 100644 diff --git a/docs/modules/rein.rst b/docs/modules/rein.rst old mode 100755 new mode 100644 diff --git a/docs/modules/utils.rst b/docs/modules/utils.rst old mode 100755 new mode 100644 index 501820b72..2a93c26b4 --- a/docs/modules/utils.rst +++ b/docs/modules/utils.rst @@ -56,3 +56,19 @@ Convert list of string to dictionary Flatten a list ^^^^^^^^^^^^^^^^^^^ .. autofunction:: flatten_list + +Close TF session and associated processes +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +.. autofunction:: exit_tensorflow + +Open TensorBoard +^^^^^^^^^^^^^^^^^^^ +.. autofunction:: open_tensorboard + +Clear TensorFlow placeholder +^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +.. autofunction:: clear_all_placeholder_variables + +Set GPU functions +--------------------------- +.. autofunction:: set_gpu_fraction \ No newline at end of file diff --git a/docs/modules/visualize.rst b/docs/modules/visualize.rst old mode 100755 new mode 100644 diff --git a/docs/user/development.rst b/docs/user/development.rst old mode 100755 new mode 100644 diff --git a/docs/user/example.rst b/docs/user/example.rst old mode 100755 new mode 100644 diff --git a/docs/user/installation.rst b/docs/user/installation.rst old mode 100755 new mode 100644 diff --git a/docs/user/more.rst b/docs/user/more.rst old mode 100755 new mode 100644 index b60290c2f..84b42dc9e --- a/docs/user/more.rst +++ b/docs/user/more.rst @@ -69,6 +69,15 @@ After you get the variable list, you can define your optimizer like that so as t train_op = tf.train.AdamOptimizer(0.001).minimize(cost, var_list= train_params) +Logging +------- + +TensorLayer adopts the `Python logging module `__ +to log running information. +The logging module would print logs to the console in default. +If you want to configure the logging module, +you shall follow its `manual `__. + Visualization -------------- diff --git a/docs/user/tutorial.rst b/docs/user/tutorial.rst old mode 100755 new mode 100644 diff --git a/example/tutorial_mnist_distributed.py b/example/tutorial_mnist_distributed.py index bc7d40e42..584375190 100644 --- a/example/tutorial_mnist_distributed.py +++ b/example/tutorial_mnist_distributed.py @@ -14,9 +14,6 @@ import tensorflow as tf import tensorlayer as tl -# set buffer mode to _IOLBF for stdout -tl.ops.setlinebuf() - # load environment for distributed training task_spec = tl.distributed.TaskSpec() task_spec.create_server() diff --git a/tensorlayer/__init__.py b/tensorlayer/__init__.py index b76ccc261..000ed8dfa 100644 --- a/tensorlayer/__init__.py +++ b/tensorlayer/__init__.py @@ -12,7 +12,6 @@ from . import files from . import iterate from . import layers -from . import ops from . import utils from . import visualize from . import prepro diff --git a/tensorlayer/layers/time_distribution.py b/tensorlayer/layers/time_distribution.py index 296ac5249..943259b60 100644 --- a/tensorlayer/layers/time_distribution.py +++ b/tensorlayer/layers/time_distribution.py @@ -1,6 +1,5 @@ # -*- coding: utf-8 -*- -from .. import ops from .core import * @@ -62,14 +61,13 @@ def __init__( timestep = input_shape[1] x = tf.unstack(self.inputs, axis=1) - with ops.suppress_stdout(): - for i in range(0, timestep): - with tf.variable_scope(name, reuse=(set_keep['name_reuse'] if i == 0 else True)) as vs: - set_name_reuse((set_keep['name_reuse'] if i == 0 else True)) - net = layer_class(InputLayer(x[i], name=args['name'] + str(i)), **args) - # net = layer_class(InputLayer(x[i], name="input_"+args['name']), **args) - x[i] = net.outputs - variables = tf.get_collection(TF_GRAPHKEYS_VARIABLES, scope=vs.name) + for i in range(0, timestep): + with tf.variable_scope(name, reuse=(set_keep['name_reuse'] if i == 0 else True)) as vs: + set_name_reuse((set_keep['name_reuse'] if i == 0 else True)) + net = layer_class(InputLayer(x[i], name=args['name'] + str(i)), **args) + # net = layer_class(InputLayer(x[i], name="input_"+args['name']), **args) + x[i] = net.outputs + variables = tf.get_collection(TF_GRAPHKEYS_VARIABLES, scope=vs.name) self.outputs = tf.stack(x, axis=1, name=name) diff --git a/tensorlayer/ops.py b/tensorlayer/ops.py deleted file mode 100644 index f677880e1..000000000 --- a/tensorlayer/ops.py +++ /dev/null @@ -1,274 +0,0 @@ -# -*- coding: utf-8 -*- - -import os -import subprocess -import sys -from contextlib import contextmanager -from sys import exit as _exit -from sys import platform as _platform - -import tensorflow as tf -import tensorlayer as tl - -from . import _logging as logging - - -def exit_tf(sess=None, port=6006): - """Close TensorFlow session, TensorBoard and Nvidia-process if available. - - Parameters - ---------- - sess : Session - TensorFlow Session. - tb_port : int - TensorBoard port you want to close, `6006` as default. - - """ - text = "[TL] Close tensorboard and nvidia-process if available" - text2 = "[TL] Close tensorboard and nvidia-process not yet supported by this function (tl.ops.exit_tf) on " - if sess != None: - sess.close() - # import time - # time.sleep(2) - if _platform == "linux" or _platform == "linux2": - logging.info('linux: %s' % text) - os.system('nvidia-smi') - os.system('fuser ' + port + '/tcp -k') # kill tensorboard 6006 - os.system("nvidia-smi | grep python |awk '{print $3}'|xargs kill") # kill all nvidia-smi python process - _exit() - elif _platform == "darwin": - logging.info('OS X: %s' % text) - subprocess.Popen("lsof -i tcp:" + str(port) + " | grep -v PID | awk '{print $2}' | xargs kill", shell=True) # kill tensorboard - elif _platform == "win32": - logging.info(text2 + "Windows") - # TODO - else: - logging.info(text2 + _platform) - - -def open_tb(logdir='/tmp/tensorflow', port=6006): - """Open Tensorboard. - - Parameters - ---------- - logdir : str - Directory where your tensorboard logs are saved - port : int - TensorBoard port you want to open, 6006 is tensorboard default - - """ - text = "[TL] Open tensorboard, go to localhost:" + str(port) + " to access" - text2 = " not yet supported by this function (tl.ops.open_tb)" - - if not tl.files.exists_or_mkdir(logdir, verbose=False): - logging.info("[TL] Log reportory was created at %s" % logdir) - - if _platform == "linux" or _platform == "linux2": - logging.info('linux %s' % text2) - # TODO - elif _platform == "darwin": - logging.info('OS X: %s' % text) - subprocess.Popen( - sys.prefix + " | python -m tensorflow.tensorboard --logdir=" + logdir + " --port=" + str(port), - shell=True) # open tensorboard in localhost:6006/ or whatever port you chose - elif _platform == "win32": - logging.info('Windows%s' % text2) - # TODO - else: - logging.info(_platform + text2) - - -def clear_all(printable=True): - """Clears all the placeholder variables of keep prob, - including keeping probabilities of all dropout, denoising, dropconnect etc. - - Parameters - ---------- - printable : boolean - If True, print all deleted variables. - - """ - logging.info('clear all .....................................') - gl = globals().copy() - for var in gl: - if var[0] == '_': continue - if 'func' in str(globals()[var]): continue - if 'module' in str(globals()[var]): continue - if 'class' in str(globals()[var]): continue - - if printable: - logging.info(" clear_all ------- %s" % str(globals()[var])) - - del globals()[var] - - -# def clear_all2(vars, printable=True): -# """ -# The :function:`clear_all()` Clears all the placeholder variables of keep prob, -# including keeping probabilities of all dropout, denoising, dropconnect -# Parameters -# ---------- -# printable : if True, print all deleted variables. -# """ -# logging.info('clear all .....................................') -# for var in vars: -# if var[0] == '_': continue -# if 'func' in str(var): continue -# if 'module' in str(var): continue -# if 'class' in str(var): continue -# -# if printable: -# logging.info(" clear_all ------- %s" % str(var)) -# -# del var - - -def set_gpu_fraction(sess=None, gpu_fraction=0.3): - """Set the GPU memory fraction for the application. - - Parameters - ---------- - sess : Session - TensorFlow Session. - gpu_fraction : float - Fraction of GPU memory, (0 ~ 1] - - References - ---------- - - `TensorFlow using GPU `__ - - """ - logging.info("[TL]: GPU MEM Fraction %f" % gpu_fraction) - gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=gpu_fraction) - sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)) - return sess - - -# def setlinebuf(): -# """Set buffer mode to _IOLBF for stdout. -# When running in container, or other environments where stdout is redirected, -# the default buffer behavior will seriously delay the message written by `print`. -# -# TODO: this method should be called automatically by default. -# -# References -# ----------- -# - ``__ -# - ``__ -# - `man setlinebuf `__ -# """ -# sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 1) - - -def disable_print(): - """Disable console output, ``tl.ops.suppress_stdout`` is recommended. - - Examples - --------- - >>> print("You can see me") - >>> tl.ops.disable_print() - >>> print("You can't see me") - >>> tl.ops.enable_print() - >>> print("You can see me") - - """ - # sys.stdout = os.devnull # this one kill the process - sys.stdout = None - sys.stderr = os.devnull - - -def enable_print(): - """Enable console output. - - see ``tl.ops.disable_print()`` - - """ - sys.stdout = sys.__stdout__ - sys.stderr = sys.__stderr__ - - -# class temporary_disable_print: -# """Temporarily disable console output. -# -# Examples -# --------- -# >>> print("You can see me") -# >>> with tl.ops.temporary_disable_print() as t: -# >>> print("You can't see me") -# >>> print("You can see me") -# """ -# def __init__(self): -# pass -# def __enter__(self): -# sys.stdout = None -# sys.stderr = os.devnull -# def __exit__(self, type, value, traceback): -# sys.stdout = sys.__stdout__ -# sys.stderr = sys.__stderr__ -# return isinstance(value, TypeError) - - -@contextmanager -def suppress_stdout(): - """Temporarily disable console output. - - Examples - --------- - >>> print("You can see me") - >>> with tl.ops.suppress_stdout(): - >>> print("You can't see me") - >>> print("You can see me") - - References - ----------- - - `Stack Overflow `__ - - """ - with open(os.devnull, "w") as devnull: - old_stdout = sys.stdout - sys.stdout = devnull - try: - yield - finally: - sys.stdout = old_stdout - - -def get_site_packages_directory(): - """Print and return the site-packages directory. - - Examples - --------- - >>> loc = tl.ops.get_site_packages_directory() - - """ - import site - try: - loc = site.getsitepackages() - logging.info("[TL] tl.ops : site-packages in %s " % loc) - return loc - except: - logging.info("[TL] tl.ops : Cannot find package dir from virtual environment") - return False - - -def empty_trash(): - """Empty trash folder.""" - text = "[TL] Empty the trash" - if _platform == "linux" or _platform == "linux2": - logging.info('linux: %s' % text) - os.system("rm -rf ~/.local/share/Trash/*") - elif _platform == "darwin": - logging.info('OS X: %s' % text) - os.system("sudo rm -rf ~/.Trash/*") - elif _platform == "win32": - logging.info('Windows: %s' % text) - try: - os.system("rd /s c:\$Recycle.Bin") # Windows 7 or Server 2008 - except: - pass - try: - os.system("rd /s c:\recycler") # Windows XP, Vista, or Server 2003 - except: - pass - else: - logging.info(_platform) diff --git a/tensorlayer/utils.py b/tensorlayer/utils.py index 84d9b1f4a..00e69e435 100644 --- a/tensorlayer/utils.py +++ b/tensorlayer/utils.py @@ -1,6 +1,12 @@ # -*- coding: utf-8 -*- +import os import random +import subprocess +import sys import time +from contextlib import contextmanager +from sys import exit as _exit +from sys import platform as _platform import numpy as np import tensorflow as tf @@ -29,7 +35,7 @@ def fit(sess, tensorboard_epoch_freq=5, tensorboard_weight_histograms=True, tensorboard_graph_vis=True): - """Traing a given non time-series network by the given cost function, training data, batch_size, n_epoch etc. + """Training a given non time-series network by the given cost function, training data, batch_size, n_epoch etc. Parameters ---------- @@ -501,3 +507,111 @@ def list_string_to_dict(string): for idx, c in enumerate(string): dictionary.update({c: idx}) return dictionary + + +def exit_tensorflow(sess=None, port=6006): + """Close TensorFlow session, TensorBoard and Nvidia-process if available. + + Parameters + ---------- + sess : Session + TensorFlow Session. + tb_port : int + TensorBoard port you want to close, `6006` as default. + + """ + text = "[TL] Close tensorboard and nvidia-process if available" + text2 = "[TL] Close tensorboard and nvidia-process not yet supported by this function (tl.ops.exit_tf) on " + if sess != None: + sess.close() + # import time + # time.sleep(2) + if _platform == "linux" or _platform == "linux2": + logging.info('linux: %s' % text) + os.system('nvidia-smi') + os.system('fuser ' + port + '/tcp -k') # kill tensorboard 6006 + os.system("nvidia-smi | grep python |awk '{print $3}'|xargs kill") # kill all nvidia-smi python process + _exit() + elif _platform == "darwin": + logging.info('OS X: %s' % text) + subprocess.Popen("lsof -i tcp:" + str(port) + " | grep -v PID | awk '{print $2}' | xargs kill", shell=True) # kill tensorboard + elif _platform == "win32": + logging.info(text2 + "Windows") + # TODO + else: + logging.info(text2 + _platform) + + +def open_tensorboard(log_dir='/tmp/tensorflow', port=6006): + """Open Tensorboard. + + Parameters + ---------- + log_dir : str + Directory where your tensorboard logs are saved + port : int + TensorBoard port you want to open, 6006 is tensorboard default + + """ + text = "[TL] Open tensorboard, go to localhost:" + str(port) + " to access" + text2 = " not yet supported by this function (tl.ops.open_tb)" + + if not tl.files.exists_or_mkdir(log_dir, verbose=False): + logging.info("[TL] Log reportory was created at %s" % log_dir) + + if _platform == "linux" or _platform == "linux2": + logging.info('linux %s' % text2) + # TODO + elif _platform == "darwin": + logging.info('OS X: %s' % text) + subprocess.Popen( + sys.prefix + " | python -m tensorflow.tensorboard --logdir=" + log_dir + " --port=" + str(port), + shell=True) # open tensorboard in localhost:6006/ or whatever port you chose + elif _platform == "win32": + logging.info('Windows%s' % text2) + # TODO + else: + logging.info(_platform + text2) + + +def clear_all_placeholder_variables(printable=True): + """Clears all the placeholder variables of keep prob, + including keeping probabilities of all dropout, denoising, dropconnect etc. + + Parameters + ---------- + printable : boolean + If True, print all deleted variables. + + """ + logging.info('clear all .....................................') + gl = globals().copy() + for var in gl: + if var[0] == '_': continue + if 'func' in str(globals()[var]): continue + if 'module' in str(globals()[var]): continue + if 'class' in str(globals()[var]): continue + + if printable: + logging.info(" clear_all ------- %s" % str(globals()[var])) + + del globals()[var] + + +def set_gpu_fraction(gpu_fraction=0.3): + """Set the GPU memory fraction for the application. + + Parameters + ---------- + gpu_fraction : float + Fraction of GPU memory, (0 ~ 1] + + References + ---------- + - `TensorFlow using GPU `__ + + """ + logging.info("[TL]: GPU MEM Fraction %f" % gpu_fraction) + gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=gpu_fraction) + sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)) + return sess