Skip to content

Instantly share code, notes, and snippets.

@victor-shepardson
Created September 25, 2017 17:52
Show Gist options
  • Save victor-shepardson/972020c12f37007cc816c59217d2aa60 to your computer and use it in GitHub Desktop.
Save victor-shepardson/972020c12f37007cc816c59217d2aa60 to your computer and use it in GitHub Desktop.
dynamic RGB image figure in bokeh+jupyter for monitoring GAN training, etc. import or paste into cell.
try:
from bokeh.io import push_notebook, output_notebook, show
from bokeh.plotting import figure
output_notebook()
def dynamic_image_figure(w,h):
"""create an RGB image figure in current cell and return an update function for it"""
def im2bokeh(img):
img = (img*255).astype(np.uint8)
img = np.dstack([img, np.ones(img.shape[:2], np.uint8) * 255])
img = np.squeeze(img.view(np.uint32))
return img
p = figure(plot_width=w, plot_height=h, x_range=(0,1), y_range=(0,1), tools='')
p.xaxis.visible = False
p.yaxis.visible = False
r = p.image_rgba([im2bokeh(np.zeros((w,h,3)))],0,0,1,1)
show(p, notebook_handle=True)
def update(img):
r.data_source.data['image'][0] = im2bokeh(img)
push_notebook()
return update
except Exception:
print('install bokeh for dynamic plots')
def dynamic_image_figure(*args):
return lambda x: None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment