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:

import lucid

However, we will use autoreload so we can quickly iterate on local code changes:

Enable autoreload


In [6]:
%load_ext autoreload

In [7]:
%aimport lucid
%aimport -tensorflow
%aimport -numpy
%aimport -sklearn

In [8]:
%aimport


Modules to reload:
lucid

Modules to skip:
numpy sklearn tensorflow

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


Lucid was loaded from /Users/ludwigschubert/Code/deepviz/lucid.

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]:
%autoreload

from lucid.misc.io import load, save, show

image = load('../tests/fixtures/noise.jpeg')

print(image.dtype)
show(image)


DEBUG:lucid.misc.io.loading:Using inferred loader 'img' due to passed file extension '.png'.
float64
DEBUG:lucid.misc.io.showing:Show is assuming rank 2 or 3 tensor to be an image.
DEBUG:lucid.misc.io.serialize_array:Converting inexact array by scaling by 255.00.

In [10]:
%autoreload

array = load("../tests/fixtures/noise.jpeg")
print(array.dtype, array.shape)
show(array, domain=None)
array


(dtype('float64'), (5, 10))
Out[10]:
array([[ 70., 180., 110., 104.,  66., 147., 100., 130., 112., 116.],
       [112., 165., 154., 106., 175., 129., 152., 111., 144., 167.],
       [135., 139.,  73., 133., 139.,  89., 120., 132.,  98., 115.],
       [106., 142.,  79.,  93., 132., 161., 175., 137., 139., 118.],
       [200., 129., 115., 130., 125., 116.,  95., 109., 117., 179.]])

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))


32 416.72797
256 713.85126
1024 765.27924

In [12]:
import os
repr(os.urandom(16))


Out[12]:
"'\\x0f\\xb8\\xaa\\xc7\\xaf\\x9b\\xff\\xd5i\\xe3Z\\xed:IC\\t'"

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)


Out[107]:
PCA(copy=True, iterated_power='auto', n_components=3, random_state=None,
  svd_solver='auto', tol=0.0, whiten=False)

In [108]:
reducer._reducer.components_


Out[108]:
array([[ 0.31404045,  0.3159232 ,  0.31751755,  0.31607628,  0.31686693,
         0.31543642,  0.31630793,  0.31591657,  0.3158789 ,  0.31829447],
       [-0.16002858,  0.50028497, -0.12999336,  0.5603771 , -0.16221319,
         0.17761908, -0.4573163 , -0.34593767,  0.06547735, -0.04716743],
       [-0.4286834 ,  0.11218928, -0.51907194,  0.2326237 , -0.03332246,
         0.12981284,  0.4749903 ,  0.30447853, -0.36753035,  0.09344025]],
      dtype=float32)

In [109]:
reduced = reducer.transform(array)

In [110]:
show(np.dsplit(reduced, reduced.shape[-1]))


0
1
2

In [105]:
from lucid.misc.io import load, save, show

show(np.dsplit(array, array.shape[-1]))


0
1
2
3
4
5
6
7
8
9