def caricature(img, model, layer, n_steps=512, cossim_pow=0.0, verbose=True): if isinstance(layer, str): layers = [layer] elif isinstance(layer, (tuple, list)): layers = layer else: raise TypeError("layer must be str, tuple or list") with tf.Graph().as_default(), tf.Session() as sess: img = resize(img, model.image_shape[:2]) objective = objectives.Objective.sum([ 1.0 * dot_compare(layer, cossim_pow=cossim_pow, batch=i+1) for i, layer in enumerate(layers) ]) t_input = tf.placeholder(tf.float32, img.shape) param_f = param.image(img.shape[0], decorrelate=True, fft=True, alpha=False, batch=len(layers)) param_f = tf.concat([t_input[None], param_f], 0) transforms = transform.standard_transforms + [transform.crop_or_pad_to(*model.image_shape[:2])] T = render.make_vis_T(model, objective, param_f, transforms=transforms) loss, vis_op, t_image = T("loss"), T("vis_op"), T("input") tf.global_variables_initializer().run() for i in range(n_steps): _ = sess.run([vis_op], {t_input: img}) result = t_image.eval(feed_dict={t_input: img}) if verbose: lucid.misc.io.showing.images(result[1:], layers) return result
def test_resize_noop(image): """FYI: this essentially tests the `if original_size == target_size: return image` part of `resize()`'s implementation. Without it, machine precision makes the following assert no longer true, but I thought it was an imoprtant property to have. """ resized = resize(image, image.shape[-3:-1]) assert np.all(resized == image)
def image_activations(model, image, layer_names=None): if layer_names is None: layer_names = [ layer["name"] for layer in model.layers if "conv" in layer.tags ] resized_image = resize(image, model.image_shape[:2]) with tf.Graph().as_default() as graph, tf.Session() as sess: image_t = tf.placeholder_with_default(resized_image, shape=model.image_shape) model.import_graph(image_t, scope="import") layer_ts = {} for layer_name in layer_names: name = layer_name if layer_name.endswith( ":0") else layer_name + ":0" layer_t = graph.get_tensor_by_name("import/{}".format(name))[0] layer_ts[layer_name] = layer_t activations = sess.run(layer_ts) return activations
def test_resize(image): size = (3, 3) resized = resize(image, size) assert resized.shape[-3:-1] == size