Skip to content

Instantly share code, notes, and snippets.

@m-rath
Last active May 7, 2021 09:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save m-rath/aff5f88acf1e5f6abeb1b6594cd0053f to your computer and use it in GitHub Desktop.
Save m-rath/aff5f88acf1e5f6abeb1b6594cd0053f to your computer and use it in GitHub Desktop.
# From a Kaggle notebook, run the following blocks as separate cells
#--------------------------------------------------------------------------
# [1] Load libraries
#--------------------------------------------------------------------------
import os, multiprocessing, datetime
import numpy as np
import tensorflow as tf
from tensorflow import keras
from tensorboard import *
from functools import partial
#--------------------------------------------------------------------------
# [2] Install the plug-in
#--------------------------------------------------------------------------
pip install -U tensorboard-plugin-profile
#--------------------------------------------------------------------------
# [3] Download ngrok to tunnel the tensorboard port to an external port
#--------------------------------------------------------------------------
!wget https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-amd64.zip
!unzip ngrok-stable-linux-amd64.zip
#--------------------------------------------------------------------------
# [4] Clear logs from previous runs
#--------------------------------------------------------------------------
!rm -rf ./logs/
!mkdir ./logs/
#--------------------------------------------------------------------------
# [5] More set up
#--------------------------------------------------------------------------
pool = multiprocessing.Pool(processes = 10)
results_of_processes = [pool.apply_async(os.system, args=(cmd, ), callback = None )
for cmd in [
f"tensorboard --logdir ./logs/ --host 0.0.0.0 --port 6006 &",
"./ngrok http 6006 &"
]]
#--------------------------------------------------------------------------
# [6] Get the URL for access to our Tensorboard
#--------------------------------------------------------------------------
! curl -s http://localhost:4040/api/tunnels | python3 -c \
"import sys, json; print(json.load(sys.stdin)['tunnels'][0]['public_url'])"
#--------------------------------------------------------------------------
# [7A] EITHER USE CALLBACK IN FIT
#--------------------------------------------------------------------------
log_dir = "logs/fit/" + datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
tensorboard_callback = tf.keras.callbacks.TensorBoard(log_dir=log_dir,
histogram_freq=1,
profile_batch=(101,110))
# history = cnn.fit(train_dataset,
# steps_per_epoch=STEPS_PER_EPOCH,
# epochs=EPOCHS,
# validation_data=valid_dataset,
# validation_steps=VALID_STEPS)
# callbacks=[tensorboard_callback])
#--------------------------------------------------------------------------
# [7B] OR USE TRACER ON INDIVIDUAL FUNCTION CALL
#--------------------------------------------------------------------------
logdir = "logs/func/" + datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
writer = tf.summary.create_file_writer(logdir)
# #CV_COLOR_MASK on CPU
# tf.summary.trace_on(graph=True, profiler=True) #this works for profiler, but not graph
# rac_masked = cv_color_mask(racoon_tensor)
# with writer.as_default():
tf.summary.trace_export(name="cv_color_mask CPU",step=0,profiler_outdir=logdir)
# #wrapped CV_COLOR_MASK on CPU
# tf.summary.trace_on(graph=True, profiler=True) #this works for profiler, but not graph
# rac_masked = tf.py_function(func=cv_color_mask, inp=[racoon_tensor], Tout=tf.float32)
# with writer.as_default():
# tf.summary.trace_export(name="cv_color_mask CPU",step=0,profiler_outdir=logdir)
# #TF_COLOR_MASK on CPU
# tf.summary.trace_on(graph=True, profiler=True)
# rac_masked = tf_color_mask(racoon_tensor)
# with writer.as_default():
# tf.summary.trace_export(name="tf_color_mask CPU",step=0,profiler_outdir=logdir)
# #decorated TF_COLOR_MASK on CPU
# tf_color_mask = tf.function(tf_color_mask)
# tf.summary.trace_on(graph=True, profiler=True)
# rac_masked = tf_color_mask(racoon_tensor)
# with writer.as_default():
# tf.summary.trace_export(name="Decorated tf_color_mask",step=0,profiler_outdir=logdir)
# #wrapped CV_COLOR_MASK on GPU
# tf.summary.trace_on(graph=True, profiler=True) #this works for profiler, but not graph
# with tf.device("/device:GPU:0"):
# rac_masked = tf.py_function(func=cv_color_mask, inp=[racoon_tensor], Tout=tf.float32)
# with writer.as_default():
# tf.summary.trace_export(name="cv_color_mask GPU",step=0,profiler_outdir=logdir)
# #TF_COLOR_MASK on GPU
# tf.summary.trace_on(graph=True, profiler=True)
# with tf.device("/device:GPU:0"):
# rac_masked = tf_color_mask(racoon_tensor)
# with writer.as_default():
# tf.summary.trace_export(name="tf_color_mask GPU",step=0,profiler_outdir=logdir)
# #decorated TF_COLOR_MASK on GPU
# tf_color_mask = tf.function(tf_color_mask)
# tf.summary.trace_on(graph=True, profiler=True)
# with tf.device("/device:GPU:0"):
# rac_masked = tf_color_mask(racoon_tensor)
# with writer.as_default():
# tf.summary.trace_export(name="Decorated tf_color_mask GPU",step=0,profiler_outdir=logdir)
#--------------------------------------------------------------------------
# Much thanks to https://www.kaggle.com/shivam1600/tensorboard-on-kaggle
#--------------------------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment