Skip to content

Commit adc1aff

Browse files
committed
Fix #188 : Issues with ops.exit_tf, and add ops.open_tb()
ops.exit_tf() : changes to avoid throwing exceptions (see issue #188), added “port” as argument, some work still needed (for windows). Also, it might be better to separate the functions performed by this into subcomponent Ops.open_tb() : self explanatory but only works on Mac for now => if anyone can implement and test on linux and windows, please do
1 parent efded8e commit adc1aff

File tree

3 files changed

+52
-10
lines changed

3 files changed

+52
-10
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ tensorlayer.egg-info
88
data/.DS_Store
99
*.pyc
1010
*.gz
11+
.spyproject/

docs/modules/ops.rst

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,14 @@ Operation system, more functions can be found in `TensorFlow API <https://www.te
1919
TensorFlow functions
2020
---------------------------
2121

22-
Kill nvidia process
23-
^^^^^^^^^^^^^^^^^^^^^^^
22+
Close TF session and associated processes
23+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2424
.. autofunction:: exit_tf
2525

26+
Open tensorboard
27+
^^^^^^^^^^^^^^^^^^^
28+
.. autofunction:: open_tb
29+
2630
Delete placeholder
2731
^^^^^^^^^^^^^^^^^^^^^^^^
2832
.. autofunction:: clear_all

tensorlayer/ops.py

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,73 @@
55

66

77
import tensorflow as tf
8+
import tensorlayer as tl
89
import os
10+
import subprocess
911
import sys
1012
from sys import platform as _platform
13+
from sys import exit as _exit
1114

1215

13-
def exit_tf(sess=None):
14-
"""Close tensorboard and nvidia-process if available
16+
def exit_tf(sess=None, port=6006):
17+
"""Close tensorflow session, tensorboard and nvidia-process if available
1518
1619
Parameters
1720
----------
1821
sess : a session instance of TensorFlow
1922
TensorFlow session
23+
tb_port : an integer
24+
TensorBoard port you want to close, 6006 is tensorboard default
2025
"""
2126
text = "[tl] Close tensorboard and nvidia-process if available"
22-
sess.close()
27+
text2 = "[tl] Close tensorboard and nvidia-process not yet supported by this function (tl.ops.exit_tf) on "
28+
if sess != None:
29+
sess.close()
2330
# import time
2431
# time.sleep(2)
2532
if _platform == "linux" or _platform == "linux2":
2633
print('linux: %s' % text)
2734
os.system('nvidia-smi')
28-
os.system('fuser 6006/tcp -k') # kill tensorboard 6006
35+
os.system('fuser '+ port +'/tcp -k') # kill tensorboard 6006
2936
os.system("nvidia-smi | grep python |awk '{print $3}'|xargs kill") # kill all nvidia-smi python process
37+
_exit()
3038
elif _platform == "darwin":
3139
print('OS X: %s' % text)
32-
os.system("lsof -i tcp:6006 | grep -v PID | awk '{print $2}' | xargs kill") # kill tensorboard 6006
40+
subprocess.Popen("lsof -i tcp:"+ str(port) +" | grep -v PID | awk '{print $2}' | xargs kill", shell=True) # kill tensorboard
3341
elif _platform == "win32":
34-
print('Windows: %s' % text)
42+
print(text2 + "Windows")
43+
# TODO
3544
else:
36-
print(_platform)
37-
exit()
45+
print(text2 + _platform)
46+
47+
def open_tb(logdir='/tmp/tensorflow', port=6006):
48+
"""Open tensorboard
49+
50+
Parameters
51+
----------
52+
logdir : a string
53+
Directory where your tensorboard logs are saved
54+
port : an integer
55+
TensorBoard port you want to open, 6006 is tensorboard default
56+
"""
57+
58+
text = "[tl] Open tensorboard, go to localhost:" + str(port) + " to access"
59+
text2 = " not yet supported by this function (tl.ops.open_tb)"
60+
61+
if not tl.files.exists_or_mkdir(logdir, verbose=False):
62+
print("[tl] Log reportory was created at %s" % logdir)
63+
64+
if _platform == "linux" or _platform == "linux2":
65+
print('linux %s' % text2)
66+
# TODO
67+
elif _platform == "darwin":
68+
print('OS X: %s' % text)
69+
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
70+
elif _platform == "win32":
71+
print('Windows%s' % text2)
72+
# TODO
73+
else:
74+
print(_platform + text2)
3875

3976
def clear_all(printable=True):
4077
"""Clears all the placeholder variables of keep prob,

0 commit comments

Comments
 (0)