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.
This notebook explores combining feature visualization with some similar ideas to Raghu et al's SVCCA paper.
In [0]:
!pip install --upgrade git+https://github.com/tensorflow/lucid.git
In [0]:
import numpy as np
import tensorflow as tf
import lucid.modelzoo.vision_models as models
import lucid.optvis.objectives as objectives
import lucid.optvis.param as param
import lucid.optvis.render as render
from lucid.misc.io.showing import _image_url, _display_html
import lucid.scratch.pretty_graphs.visualizations as vis
from lucid.misc.io import show, load
from lucid.recipes.caricature import feature_inversion
import lucid.misc.io.showing
In [0]:
_Cs = {}
def C(layer1, layer2=None):
if layer2 is None:
layer2 = layer1
if (layer1, layer2) not in _Cs:
act1, act2 = layer1.activations, layer2.activations
_Cs[(layer1, layer2)] = np.matmul(act1.T, act2) / len(act1)
return _Cs[(layer1, layer2)]
_C_i_chols = {}
def C_i_chol(layer):
if layer not in _C_i_chols:
_C_i_chols[layer] = np.linalg.cholesky(np.linalg.inv(C(layer)))
return _C_i_chols[layer]
In [0]:
InceptionV1 = models.InceptionV1()
InceptionV1.load_graphdef()
InceptionV2 = models.InceptionV2_slim()
InceptionV2.load_graphdef()
AlexNet = models.AlexNet()
AlexNet.load_graphdef()
In [0]:
def make_caricature_tooltips(model, img, layers, n_steps=128, cossim_pow=0.0):
"""Create tooltips for layers of the form:
> layer name
> [caricature image]
Returns dictionary of the form {layer_name: html fragment} for use in
visualizations as a collection of tooltip.
"""
result = feature_inversion(img, model, layers, n_steps=n_steps,
cossim_pow=cossim_pow, verbose=False)
tooltips = {}
for layer, caricature in zip (layers, result[1:]):
data_url = _image_url(caricature)
tooltips[layer] = "<div>%s</div><img src=\"%s\"></img>" % (layer, data_url)
return tooltips
In [0]:
img = load("https://storage.googleapis.com/lucid-static/building-blocks/examples/dog_cat.png")
In [0]:
layers = [layer.name for layer in InceptionV1.layers if 'conv' in layer.tags and (layer.name.startswith('conv') or layer.name.startswith('mixed'))]
tooltips = make_caricature_tooltips(InceptionV1, img, layers)
In [0]:
vis.vpad(40)
vis.display_model(InceptionV1)
vis.display_tooltips(".background", "data-group-tf-name", tooltips)
vis.display_tooltips(".node", "data-tf-name", tooltips)
vis.vpad(240)
In [7]:
rendered = vis.complete_render_model_graph(InceptionV1)
all_layers = [n.name for n in rendered["node_boxes"].keys() if n.op not in ["Placeholder", "Softmax"]]
N = 16
all_tooltips = {}
for n in range(0, len(all_layers), N):
print ".",
pres_layers = all_layers[n : n+N]
pres_tooltips = make_caricature_tooltips(InceptionV1, img, pres_layers, cossim_pow=1.0)
all_tooltips.update(pres_tooltips)
In [8]:
vis.vpad(40)
vis._display_html("<style>.background, .tooltip {pointer-events: none;} </style>")
vis.display_model(InceptionV1)
vis.display_tooltips(".node", "data-tf-name", all_tooltips)
vis.vpad(240)