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.
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:
%load_ext autoreload
%aimport lucid
%aimport -tensorflow
%aimport -numpy
%aimport -sklearn
%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:
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.
import numpy as np
image = np.random.normal(loc=.5, scale=.1, size=(200,200))
# 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)
%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.
%autoreload
array = load("../tests/fixtures/noise.jpeg")
print(array.dtype, array.shape)
show(array, domain=None)
array
(dtype('float64'), (5, 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.]])
show(load("https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png"))
from lucid.optvis import objectives, param, transform, render
from lucid.modelzoo.vision_models import InceptionV1
model = InceptionV1()
model.load_graphdef()
_ = render.render_vis(model, "mixed3b_pre_relu:470", thresholds=(32, 256, 1024))
32 416.72797
256 713.85126
1024 765.27924
import os
repr(os.urandom(16))
"'\\x0f\\xb8\\xaa\\xc7\\xaf\\x9b\\xff\\xd5i\\xe3Z\\xed:IC\\t'"
import numpy as np
from lucid.misc.channel_reducer import ChannelReducer
array = np.zeros((100,100,10), dtype=np.float32)
for d in range(array.shape[-1]):
array[:,:,d] = np.eye(100,100)
array += 0.1 * np.random.uniform(size=array.shape)
reducer = ChannelReducer(3, reduction_alg='PCA')
reducer.fit(array)
PCA(copy=True, iterated_power='auto', n_components=3, random_state=None, svd_solver='auto', tol=0.0, whiten=False)
reducer._reducer.components_
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)
reduced = reducer.transform(array)
show(np.dsplit(reduced, reduced.shape[-1]))
from lucid.misc.io import load, save, show
show(np.dsplit(array, array.shape[-1]))