Skip to content
This repository was archived by the owner on Apr 10, 2024. It is now read-only.
2 changes: 1 addition & 1 deletion lucid/optvis/param/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.
# ==============================================================================

from lucid.optvis.param.images import image
from lucid.optvis.param.images import image, grayscale_image_rgb
from lucid.optvis.param.lowres import lowres_tensor
from lucid.optvis.param.color import to_valid_rgb
from lucid.optvis.param.spatial import naive, fft_image, laplacian_pyramid
Expand Down
1 change: 1 addition & 0 deletions lucid/optvis/param/color.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,4 @@ def to_valid_rgb(t, decorrelate=False, sigmoid=True):
return tf.nn.sigmoid(t)
else:
return constrain_L_inf(2*t-1)/2 + 0.5

24 changes: 16 additions & 8 deletions lucid/optvis/param/images.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,23 @@
from lucid.optvis.param.spatial import pixel_image, fft_image


def image(w, h=None, batch=None, sd=None, decorrelate=True, fft=True, alpha=False):
def image(w, h=None, batch=None, sd=None, decorrelate=True, fft=True, alpha=False, channels=None):
h = h or w
batch = batch or 1
channels = 4 if alpha else 3
shape = [batch, w, h, channels]
ch = channels or (4 if alpha else 3)
shape = [batch, h, w, ch]
param_f = fft_image if fft else pixel_image
t = param_f(shape, sd=sd)
rgb = to_valid_rgb(t[..., :3], decorrelate=decorrelate, sigmoid=True)
if alpha:
a = tf.nn.sigmoid(t[..., 3:])
return tf.concat([rgb, a], -1)
return rgb
if channels:
output = tf.nn.sigmoid(t)
else:
output = to_valid_rgb(t[..., :3], decorrelate=decorrelate, sigmoid=True)
if alpha:
a = tf.nn.sigmoid(t[..., 3:])
return tf.concat([rgb, a], -1)
return output

def grayscale_image_rgb(*args, **kwargs):
"""Takes same arguments as image"""
output = image(*args, channels=1, **kwargs)
return tf.tile(output, (1,1,1,3))
297 changes: 297 additions & 0 deletions notebooks/feature-visualization/any_number_channels.ipynb

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/misc/io/test_reading.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

path = "./tests/fixtures/string.txt"
string = u"The quick brown fox jumps over the lazy 🐕"
io.open(path, 'w').write(string)
io.open(path, 'w', encoding="utf-8").write(string)


def test_read_txt_file():
Expand Down
31 changes: 31 additions & 0 deletions tests/optvis/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,34 @@ def test_integration(decorrelate, fft):
start_image, end_image = rendering

assert (start_image != end_image).any()

def arbitrary_channels_to_rgb(*args, **kwargs):
"""Arbitrary parametrization for testing"""
channels = kwargs.pop('channels', None) or 10
full_im = param.image(*args, channels=channels, **kwargs)
r = tf.reduce_mean(full_im[...,:channels//3]**2, axis=-1)
g = tf.reduce_mean(full_im[...,channels//3:2*channels//3]**2, axis=-1)
b = tf.reduce_mean(full_im[...,2*channels//3:]**2, axis=-1)
return tf.stack([r,g,b], axis=-1)

@pytest.mark.slow
def test_integration_any_channels():
inceptionv1 = InceptionV1()
objectives_f = [objectives.deepdream("mixed4a_pre_relu"),
objectives.channel("mixed4a_pre_relu", 360),
objectives.neuron("mixed3a", 177)]
params_f = [lambda: param.grayscale_image_rgb(128),
lambda: arbitrary_channels_to_rgb(128, channels=10)]
for objective_f in objectives_f:
for param_f in params_f:
rendering = render.render_vis(
inceptionv1,
objective_f,
param_f,
verbose=False,
thresholds=(0, 64),
use_fixed_seed=True,
)
start_image, end_image = rendering

assert (start_image != end_image).any()