#!/usr/bin/env python # coding: utf-8 # # Lucid local development notebook # # This notebook allows you to use your development version of the lucid codebase. # It is meant to only be used during development of lucid, not when using it. # # Instead, see "usage_example.ipynb" in this folder for an example of how to use lucid in a notebook. # ## Setup # # ### Add local package to search path # In[5]: import os import sys module_path = os.path.abspath(os.path.join('..')) if module_path not in sys.path: sys.path.append(module_path) # Now we should be able to import it directly, just like if we installed it using `pip`: # # ```python # import lucid # ``` # # However, we will use `autoreload` so we can quickly iterate on local code changes: # # ### Enable autoreload # In[6]: get_ipython().run_line_magic('load_ext', 'autoreload') # In[7]: get_ipython().run_line_magic('aimport', 'lucid') get_ipython().run_line_magic('aimport', '-tensorflow') get_ipython().run_line_magic('aimport', '-numpy') get_ipython().run_line_magic('aimport', '-sklearn') # In[8]: get_ipython().run_line_magic('aimport', '') # Let's check that we're actually seeing the local version, not a package installed in site-packages. # Th next cell should show the path at which you cloned the lucid repo, not a system path: # In[9]: module_path = lucid.__path__[0] print("Lucid was loaded from {}.".format(module_path)) assert os.path.abspath("..") in module_path del module_path # ## Example usage # In[6]: import numpy as np # In[7]: image = np.random.normal(loc=.5, scale=.1, size=(200,200)) # In[15]: # this is how you set log levels globally: import logging logging.getLogger('lucid').setLevel(logging.DEBUG) # or per module: # logging.getLogger('lucid.misc.io').setLevel(logging.INFO) # In[16]: get_ipython().run_line_magic('autoreload', '') from lucid.misc.io import load, save, show image = load('../tests/fixtures/noise.jpeg') print(image.dtype) show(image) # In[10]: get_ipython().run_line_magic('autoreload', '') array = load("../tests/fixtures/noise.jpeg") print(array.dtype, array.shape) show(array, domain=None) array # In[11]: show(load("https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png")) # ## Optvis Usage # In[12]: from lucid.optvis import objectives, param, transform, render from lucid.modelzoo.vision_models import InceptionV1 # In[13]: model = InceptionV1() model.load_graphdef() # In[14]: _ = render.render_vis(model, "mixed3b_pre_relu:470", thresholds=(32, 256, 1024)) # In[12]: import os repr(os.urandom(16)) # In[12]: import numpy as np from lucid.misc.channel_reducer import ChannelReducer # In[98]: array = np.zeros((100,100,10), dtype=np.float32) for d in range(array.shape[-1]): array[:,:,d] = np.eye(100,100) # In[104]: array += 0.1 * np.random.uniform(size=array.shape) # In[106]: reducer = ChannelReducer(3, reduction_alg='PCA') # In[107]: reducer.fit(array) # In[108]: reducer._reducer.components_ # In[109]: reduced = reducer.transform(array) # In[110]: show(np.dsplit(reduced, reduced.shape[-1])) # In[105]: from lucid.misc.io import load, save, show show(np.dsplit(array, array.shape[-1]))