<jupyter_start><jupyter_text>Activation Grids- This notebook does not do any visualizations, it just creates the act grids for each layer and saves them<jupyter_code>import numpy as np
import tensorflow as tf


from sklearn.preprocessing import normalize
import numpy as np

import matplotlib
import matplotlib.pyplot as plt

from PIL import Image
import math
import requests
from io import BytesIO


import lucid.modelzoo.vision_models as models
import lucid.optvis.render as render
from lucid.misc.io import show, load
from lucid.misc.io.showing import _image_url
import lucid.scratch.web.svelte as lucid_svelte

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
LAYERS = { 'conv1': 'Conv2D',
           'conv1_relu':'Relu',
           'pool1': 'MaxPool',
           'conv2': 'Conv2D_1',
           'conv2_relu': 'Relu_1',
           'pool2': 'MaxPool_1',
           'conv3': 'Conv2D_2',
           'conv3_relu': 'Relu_2',
           'pool3': 'MaxPool_2',
         }

from lucid.modelzoo.vision_base import Model

class DLA(Model):
    model_path = 'https://storage.googleapis.com/dla_protobuff/full_model_8_13.pb'
    image_shape = [1, 400]
    image_value_range = [0, 1]
    input_name = 'x'
def resize_input(image_file):
    img = Image.open(image_file)
    img = img.resize((400,267), Image.ANTIALIAS)
    img.save(image_file)
    
def run(data, start, layer, hide=False):
    flux = np.load(data)
    flux_test = flux[start:start+400]
    flux_test = flux_test.reshape(1, 400, 1, 1)
    flux_graph = flux_test.reshape(400)
    fig = plt.figure(frameon=False);
    ax = plt.Axes(fig, [0, 0, 1, 1]);
    ax.set_axis_off();
    fig.add_axes(ax);
    ax.plot(flux_graph, 'black');
    ax.set(xlim=(0, 400));
    file_save = 'flux_' + str(start) + '.png'
    fig.savefig(file_save);
    plt.close(fig)
    resize_input(file_save)
    acts = render_act_grid(layer, flux_test, file_save)
    return acts
model = DLA()

def render_act_grid(layer, test_input):
    input_1d = test_input # Actual 1-Dimensional test input
    
    model_layer = LAYERS[layer]
    
    # Compute the activations
    with tf.Graph().as_default(), tf.Session() as sess:
        t_input = tf.placeholder(tf.float32, shape=[1, 400, 1, 1])
        T = render.import_model(model, t_input, t_input)
        acts = T(model_layer).eval(feed_dict={t_input: input_1d, 'import/keep_prob:0': .98})[0]
    acts_flat = acts.reshape([-1] + [acts.shape[2]])
    
    # Render an image for each activation vector
    param_f = lambda: param.image(400, h=1, channels=1, batch = acts.shape[0])
    obj = objectives.Objective.sum(
        [objectives.direction(model_layer, v, batch=n)
         for n,v in enumerate(acts_flat)])
    
    thresholds = (128,)
    vis_imgs = render.render_vis(model, obj, param_f, thresholds=thresholds, transforms=[], verbose=False)[-1]
    vis_imgs = vis_imgs.reshape(acts.shape[0], 400)
    
    return vis_imgs
        
# Specify the 400 length flux input
flux = np.load("../data/spectra/flux0.npy")
flux = flux[0:400]
flux = flux.reshape(1, 400, 1, 1)
file_save = 'flux0_'
# Specify the 400 length flux input
flux = np.load("../data/spectra/flux.npy")
flux = flux[700:1100]
flux = flux.reshape(1, 400, 1, 1)
file_save = 'flux700_'
for layer in LAYERS:
    print(layer + "...")
    imgs = render_act_grid(layer, flux)
    img_flat = imgs.flatten()
    out = file_save + layer + ".npy"
    np.save(out, img_flat)
# Specify the 400 length flux input
flux = np.load("../data/spectra/flux.npy")
flux = flux[350:750]
flux = flux.reshape(1, 400, 1, 1)
file_save = 'flux350_'
for layer in LAYERS:
    print(layer + "...")
    imgs = render_act_grid(layer, flux)
    img_flat = imgs.flatten()
    out = file_save + layer + ".npy"
    np.save(out, img_flat)
# Specify the 400 length flux input
flux = np.load("../data/spectra/flux1.npy")
flux = flux[1000:1400]
flux = flux.reshape(1, 400, 1, 1)
file_save = 'flux1_'
for layer in LAYERS:
    print(layer + "...")
    imgs = render_act_grid(layer, flux)
    img_flat = imgs.flatten()
    out = file_save + layer + ".npy"
    np.save(out, img_flat)<jupyter_output>conv1...
conv1_relu...
WARNING:tensorflow:From /home/sam/lucid/lucid/misc/redirected_relu_grad.py:95: add_dispatch_support.<locals>.wrapper (from tensorflow.python.ops.array_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Use tf.where in 2.0, which has the same broadcast rule as np.where
{'fraction_non_alphanumeric': 0.12735951399435885, 'fraction_numerical': 0.03493165545671512, 'mean_word_length': 3.5329400196656833, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '435258', 'license': 'MIT', 'n_tokens_mistral': 1806, 'n_tokens_neox': 1667, 'n_words': 407, 'path': 'lucid_work/notebooks/activation_grid.ipynb', 'repo_name': 'JianiDing/dla_cnn', 'size': '4609'}