In [0]:
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
In [0]:
# Install Lucid
!pip install --quiet lucid==0.0.5
#!pip install --quiet --upgrade-strategy=only-if-needed git+https://github.com/tensorflow/lucid.git
In [0]:
# Imports
import numpy as np
import tensorflow as tf
import lucid.modelzoo.vision_models as models
from lucid.misc.io import show
import lucid.optvis.objectives as objectives
import lucid.optvis.param as param
import lucid.optvis.render as render
import lucid.optvis.transform as transform
In [0]:
import scipy.ndimage as nd
In [0]:
# Let's import a model from the Lucid modelzoo!
model = models.InceptionV1()
model.load_graphdef()
In [0]:
def hidden_opt(model, layer, obj, W, require_pos=False):
with tf.Graph().as_default(), tf.Session() as sess:
t_input = tf.placeholder("float32", [1, None, None, 3])
T = render.import_model(model, t_input, t_input)
loss = obj(T)
t_layer = T(layer)
vecs = 0.1*np.random.randn(1, W, W, t_layer.shape[-1])
t_layer_grad = tf.gradients(loss, t_layer)[0]
for i in range(1000):
if i % 250 == 0: print loss.eval({t_layer: vecs}),
vecs += 0.1 * t_layer_grad.eval({t_layer: vecs})
if require_pos:
vecs = np.maximum(0, vecs)
print ""
return vecs
In [0]:
def decomposed_vis(layer, obj, W=8, W2=32):
vecs = hidden_opt(model, layer, obj, W=W)
vecs_flat = vecs.reshape([-1, vecs.shape[-1]])
mags = np.sqrt((vecs[0]**2).sum(2))
param_f = lambda: param.image(W2, batch=W*W)
obj = objectives.Objective.sum([
objectives.direction(layer, vec, batch=n)
for n, vec in enumerate(vecs_flat)
])
imgs = render.render_vis(model, obj, param_f, thresholds=(512,))
imgs_ = np.pad(imgs[0].reshape([W,W,W2,W2,3]), [(0,0), (0,0), (0,1), (0,1), (0,0)], mode='constant', constant_values=(1,))
img_grid = np.hstack(np.hstack(imgs_))
mags_grid = np.hstack(np.hstack(
np.pad(
nd.zoom(mags.reshape([W,W,1,1])/mags.max(), [1, 1, W2, W2], order=0)[..., None]
, [(0,0), (0,0), (0,1), (0,1), (0,0)], mode='constant', constant_values=(1,))
))
#show(mags_grid)
#show(img_grid)
show(img_grid * mags_grid + 0.8*(1-mags_grid) )
In [8]:
for node in model.graph_def.node:
if not "/" in node.name and not "_" in node.name:
print node.name
In [7]:
obj = objectives.neuron("mixed4c_pre_relu", 447)
render.render_vis(model, obj, lambda: param.image(100))
decomposed_vis("mixed4b", obj, W=5, W2=48)
In [9]:
obj = objectives.neuron("mixed4c_pre_relu", 447)
render.render_vis(model, obj, lambda: param.image(100))
decomposed_vis("mixed4a", obj, W=8, W2=48)
In [10]:
obj = objectives.neuron("mixed4c_pre_relu", 447)
render.render_vis(model, obj, lambda: param.image(100))
decomposed_vis("mixed3a", obj, W=14, W2=32)
In [47]:
|fobj = objectives.neuron("mixed4d_pre_relu", 426)
render.render_vis(model, obj, lambda: param.image(100))
decomposed_vis("mixed4c", obj, W=5, W2=48)
In [53]:
obj = objectives.neuron("mixed4b_pre_relu", 368)
render.render_vis(model, obj, lambda: param.image(100))
decomposed_vis("mixed4a", obj, W=5, W2=48)
In [54]:
obj = objectives.neuron("mixed4b_pre_relu", 369)
render.render_vis(model, obj, lambda: param.image(100))
decomposed_vis("mixed4a", obj, W=5, W2=48)
In [9]:
obj = objectives.neuron("mixed4b_pre_relu", 396)
render.render_vis(model, obj, lambda: param.image(100))
decomposed_vis("mixed4a", obj, W=5, W2=70)
In [10]:
obj = objectives.neuron("mixed4b_pre_relu", 402)
render.render_vis(model, obj, lambda: param.image(100))
decomposed_vis("mixed4a", obj, W=5, W2=70)
In [11]:
obj = objectives.neuron("mixed4b_pre_relu", 403)
render.render_vis(model, obj, lambda: param.image(100))
decomposed_vis("mixed4a", obj, W=5, W2=70)
In [12]:
obj = objectives.neuron("mixed4b_pre_relu", 405)
render.render_vis(model, obj, lambda: param.image(100))
decomposed_vis("mixed4a", obj, W=5, W2=70)
In [21]:
obj = objectives.neuron("mixed4b_pre_relu", 409)
render.render_vis(model, obj, lambda: param.image(100))
decomposed_vis("mixed4a", obj, W=5, W2=50)
In [20]:
obj = objectives.neuron("mixed4b_pre_relu", 414)
render.render_vis(model, obj, lambda: param.image(100))
decomposed_vis("mixed4a", obj, W=5, W2=50)
In [19]:
obj = objectives.neuron("mixed4b_pre_relu", 418)
render.render_vis(model, obj, lambda: param.image(100))
decomposed_vis("mixed4a", obj, W=5, W2=50)
In [22]:
obj = objectives.neuron("mixed4b_pre_relu", 418)
render.render_vis(model, obj, lambda: param.image(100))
decomposed_vis("mixed4a", obj, W=5, W2=50)
In [23]:
obj = objectives.neuron("mixed4b_pre_relu", 429)
render.render_vis(model, obj, lambda: param.image(100))
decomposed_vis("mixed4a", obj, W=5, W2=50)
In [24]:
obj = objectives.neuron("mixed4b_pre_relu", 437)
render.render_vis(model, obj, lambda: param.image(100))
decomposed_vis("mixed4a", obj, W=5, W2=50)
In [7]:
obj = objectives.neuron("mixed4b_pre_relu", 450)
render.render_vis(model, obj, lambda: param.image(100))
decomposed_vis("mixed4a", obj, W=5, W2=50)
In [8]:
obj = objectives.neuron("mixed4c_pre_relu", 409)
render.render_vis(model, obj, lambda: param.image(100))
decomposed_vis("mixed4b", obj, W=5, W2=70)
In [9]:
obj = objectives.neuron("mixed4c_pre_relu", 418)
render.render_vis(model, obj, lambda: param.image(100))
decomposed_vis("mixed4b", obj, W=5, W2=70)
In [18]:
obj = objectives.neuron("mixed4b_pre_relu", 462)
render.render_vis(model, obj, lambda: param.image(100))
decomposed_vis("mixed4a", obj, W=5, W2=70)
In [57]:
obj = objectives.neuron("mixed4b_pre_relu", 373)
render.render_vis(model, obj, lambda: param.image(100))
decomposed_vis("mixed4a", obj, W=5, W2=70)
In [55]:
obj = objectives.neuron("mixed4b_pre_relu", 373)
render.render_vis(model, obj, lambda: param.image(100))
decomposed_vis("mixed4a", obj, W=5, W2=48)
In [52]:
obj = objectives.neuron("mixed4d_pre_relu", 426)
render.render_vis(model, obj, lambda: param.image(100))
decomposed_vis("mixed4a", obj, W=10, W2=48)
In [50]:
obj = objectives.neuron("mixed4a_pre_relu", 368)
render.render_vis(model, obj, lambda: param.image(100))
decomposed_vis("maxpool4", obj, W=5, W2=38)
In [48]:
obj = objectives.neuron("mixed4a_pre_relu", 368)
render.render_vis(model, obj, lambda: param.image(100))
decomposed_vis("maxpool4", obj, W=5, W2=32)
In [11]:
obj = objectives.neuron("mixed4a_pre_relu", 460)
render.render_vis(model, obj, lambda: param.image(100))
decomposed_vis("maxpool4", obj, W=5, W2=32)
In [10]:
obj = objectives.neuron("mixed4a_pre_relu", 475)
render.render_vis(model, obj, lambda: param.image(100))
decomposed_vis("maxpool4", obj, W=5, W2=32)
In [8]:
obj = objectives.neuron("mixed4a_pre_relu", 455)
render.render_vis(model, obj, lambda: param.image(100))
decomposed_vis("maxpool4", obj, W=5, W2=32)
In [7]:
obj = objectives.neuron("mixed4a_pre_relu", 443)
render.render_vis(model, obj, lambda: param.image(100))
decomposed_vis("maxpool4", obj, W=5, W2=32)
In [14]:
obj = objectives.neuron("mixed4a_pre_relu", 426)
render.render_vis(model, obj, lambda: param.image(100))
decomposed_vis("maxpool4", obj, W=5, W2=32)
In [42]:
obj = objectives.neuron("mixed4a_pre_relu", 371)
render.render_vis(model, obj, lambda: param.image(100))
decomposed_vis("maxpool4", obj, W=5, W2=32)
In [49]:
obj = objectives.neuron("mixed4a_pre_relu", 372)
render.render_vis(model, obj, lambda: param.image(100))
decomposed_vis("maxpool4", obj, W=5, W2=32)
In [35]:
obj = objectives.neuron("mixed4a_pre_relu", 374)
render.render_vis(model, obj, lambda: param.image(100))
decomposed_vis("maxpool4", obj, W=5, W2=32)
In [43]:
obj = objectives.neuron("mixed4a_pre_relu", 375)
render.render_vis(model, obj, lambda: param.image(100))
decomposed_vis("maxpool4", obj, W=5, W2=32)
In [36]:
obj = objectives.neuron("mixed4a_pre_relu", 383)
render.render_vis(model, obj, lambda: param.image(100))
decomposed_vis("maxpool4", obj, W=5, W2=32)
In [37]:
obj = objectives.neuron("mixed4a_pre_relu", 386)
render.render_vis(model, obj, lambda: param.image(100))
decomposed_vis("maxpool4", obj, W=5, W2=32)
In [44]:
obj = objectives.neuron("mixed4a_pre_relu", 389)
render.render_vis(model, obj, lambda: param.image(100))
decomposed_vis("maxpool4", obj, W=5, W2=38)
In [38]:
obj = objectives.neuron("mixed4a_pre_relu", 389)
render.render_vis(model, obj, lambda: param.image(100))
decomposed_vis("maxpool4", obj, W=5, W2=32)
In [39]:
obj = objectives.neuron("mixed4a_pre_relu", 391)
render.render_vis(model, obj, lambda: param.image(100))
decomposed_vis("maxpool4", obj, W=5, W2=38)
In [40]:
obj = objectives.neuron("mixed4a_pre_relu", 391)
render.render_vis(model, obj, lambda: param.image(100))
decomposed_vis("maxpool4", obj, W=5, W2=32)
In [59]:
obj = objectives.neuron("mixed4a_pre_relu", 397)
render.render_vis(model, obj, lambda: param.image(100))
decomposed_vis("maxpool4", obj, W=5, W2=32)
In [18]:
obj = objectives.neuron("mixed4a_pre_relu", 409)
render.render_vis(model, obj, lambda: param.image(100))
decomposed_vis("maxpool4", obj, W=5, W2=32)
In [22]:
obj = objectives.neuron("mixed4a_pre_relu", 408)
render.render_vis(model, obj, lambda: param.image(100))
decomposed_vis("maxpool4", obj, W=5, W2=38)
In [19]:
obj = objectives.neuron("mixed4a_pre_relu", 408)
render.render_vis(model, obj, lambda: param.image(100))
decomposed_vis("maxpool4", obj, W=5, W2=32)
In [45]:
obj = objectives.neuron("mixed4a_pre_relu", 402)
render.render_vis(model, obj, lambda: param.image(100))
decomposed_vis("maxpool4", obj, W=5, W2=32)
In [46]:
obj = objectives.neuron("mixed4a_pre_relu", 423)
render.render_vis(model, obj, lambda: param.image(100))
decomposed_vis("maxpool4", obj, W=5, W2=32)
In [17]:
obj = objectives.neuron("mixed4a_pre_relu", 417)
render.render_vis(model, obj, lambda: param.image(100))
decomposed_vis("maxpool4", obj, W=5, W2=32)
In [9]:
_ = render.render_vis(model, "mixed4a_pre_relu:476")
In [41]:
obj = objectives.neuron("mixed3a_pre_relu", 188)
render.render_vis(model, obj, lambda: param.image(80))
decomposed_vis("maxpool1", obj, W=5, W2=32)
In [34]:
obj = objectives.neuron("mixed3a_pre_relu", 188)
render.render_vis(model, obj, lambda: param.image(80))
decomposed_vis("maxpool1", obj, W=5, W2=32)
In [19]:
obj = objectives.neuron("mixed3b_pre_relu", 318)
decomposed_vis("mixed3a", obj, W=5, W2=48)
In [58]:
obj = objectives.neuron("mixed3b_pre_relu", 342)
render.render_vis(model, obj, lambda: param.image(80))
decomposed_vis("mixed3a", obj, W=5, W2=32)
In [17]:
obj = objectives.neuron("mixed3b_pre_relu", 316)
decomposed_vis("mixed3a", obj, W=5, W2=48)
In [15]:
obj = objectives.neuron("mixed3b_pre_relu", 320)
render.render_vis(model, obj, lambda: param.image(80))
decomposed_vis("mixed3a", obj, W=5, W2=32)
In [14]:
obj = objectives.neuron("mixed3b_pre_relu", 339)
render.render_vis(model, obj, lambda: param.image(80))
decomposed_vis("mixed3a", obj, W=5, W2=32)
In [8]:
obj = objectives.neuron("mixed3b_pre_relu", 367)
decomposed_vis("mixed3a", obj, W=5, W2=48)
In [17]:
obj = objectives.neuron("mixed3b_pre_relu", 344)
render.render_vis(model, obj, lambda: param.image(80))
decomposed_vis("mixed3a", obj, W=5, W2=32)
In [23]:
obj = objectives.neuron("mixed3a_pre_relu", 174)
render.render_vis(model, obj, lambda: param.image(80))
decomposed_vis("maxpool1", obj, W=5, W2=32)
In [12]:
obj = objectives.neuron("mixed3a_pre_relu", 149)
decomposed_vis("maxpool1", obj, W=5, W2=48)
In [11]:
obj = objectives.neuron("mixed3a_pre_relu", 177)
decomposed_vis("maxpool1", obj, W=5, W2=48)
In [10]:
obj = objectives.neuron("mixed4b_pre_relu", 175)
decomposed_vis("mixed4a", obj, W=5, W2=48)
In [18]:
obj = objectives.neuron("mixed4b_pre_relu", 176)
render.render_vis(model, obj, lambda: param.image(80))
decomposed_vis("mixed4a", obj, W=3, W2=42)
In [16]:
obj = objectives.neuron("mixed4b_pre_relu", 235)
decomposed_vis("mixed4a", obj, W=5, W2=32)
In [19]:
obj = objectives.neuron("mixed4b_pre_relu", 237)
decomposed_vis("mixed4a", obj, W=5, W2=32)
In [17]:
obj = objectives.neuron("mixed4c_pre_relu", 8)
decomposed_vis("mixed4b", obj, W=3, W2=32)
In [18]:
obj = objectives.neuron("mixed4c_pre_relu", 8)
decomposed_vis("mixed4a", obj, W=5, W2=32)
In [26]:
obj = objectives.neuron("mixed4a_pre_relu", 36)
decomposed_vis("maxpool4", obj, W=5, W2=32)
In [12]:
obj = objectives.neuron("mixed4a_pre_relu", 36)
decomposed_vis("mixed3a", obj, W=10, W2=18)
In [13]:
obj = objectives.neuron("mixed4a_pre_relu", 36)
decomposed_vis("mixed3b", obj, W=10, W2=18)
In [8]:
obj = objectives.channel("mixed4a_pre_relu", 36)
decomposed_vis("mixed3a", obj, W=10, W2=18)
In [9]:
obj = objectives.channel("mixed4a_pre_relu", 11)
decomposed_vis("mixed3a", obj, W=10, W2=16)
In [19]:
obj = objectives.channel("mixed4a_pre_relu", 11)
decomposed_vis("mixed3a", obj, W=10, W2=18)
In [10]:
obj = objectives.neuron("mixed4a_pre_relu", 11)
decomposed_vis("mixed3a", obj, W=10, W2=18)
In [11]:
obj = objectives.neuron("mixed4a_pre_relu", 11)
decomposed_vis("conv2d2", obj, W=4*5, W2=18)
In [14]:
obj = objectives.neuron("mixed4a_pre_relu", 476)
decomposed_vis("mixed3b", obj, W=8, W2=32)
In [34]:
obj = objectives.neuron("mixed4a_pre_relu", 476)
decomposed_vis("mixed3b", obj, W=8, W2=32)
In [36]:
obj = objectives.neuron("mixed4a_pre_relu", 11)
decomposed_vis("mixed3a", obj, W=10, W2=32)
In [37]:
obj = objectives.neuron("mixed4a_pre_relu", 11)
decomposed_vis("conv2d2", obj, W=20, W2=16)
In [11]:
obj = objectives.channel("mixed4a_pre_relu", 11)
decomposed_vis("mixed3a", obj, W=10, W2=32)
In [13]:
obj = objectives.channel("mixed4a_pre_relu", 11)
decomposed_vis("mixed3a", obj, W=10, W2=18)
In [14]:
obj = objectives.channel("mixed4a_pre_relu", 11)
decomposed_vis("mixed3a", obj, W=10, W2=16)
In [41]:
obj = objectives.channel("mixed4a_pre_relu", 1)
decomposed_vis("mixed3b", obj, W=10, W2=32)
In [40]:
obj = objectives.channel("mixed4a_pre_relu", 1)
decomposed_vis("mixed3a", obj, W=10, W2=32)
In [49]:
obj = objectives.channel("mixed4a_pre_relu", 1)
decomposed_vis("mixed3a", obj, W=10, W2=32)
In [48]:
obj = objectives.channel("mixed3a_pre_relu", 120)
decomposed_vis("conv2d2", obj, W=10, W2=24)
In [10]:
layer = "mixed3b"
obj = objectives.neuron("mixed4a_pre_relu", 476)
vecs = hidden_opt(model, layer, obj, W=8)
param_f = lambda: param.image(32, batch=8*8)
obj = objectives.Objective.sum([
objectives.direction(layer, vec, batch=n)
for n, vec in enumerate(vecs.reshape([-1, vecs.shape[-1]]))
])
imgs = render.render_vis(model, obj, param_f)
show(np.hstack(np.hstack(imgs[0].reshape([8,8,32,32,3]))))
In [9]:
layer = "mixed3a"
obj = objectives.neuron("mixed4a_pre_relu", 476)
vecs = hidden_opt(model, layer, obj, W=8)
param_f = lambda: param.image(32, batch=8*8)
obj = objectives.Objective.sum([
objectives.direction(layer, vec, batch=n)
for n, vec in enumerate(vecs.reshape([-1, vecs.shape[-1]]))
])
imgs = render.render_vis(model, obj, param_f)
show(np.hstack(np.hstack(imgs[0].reshape([8,8,32,32,3]))))
In [0]: