<jupyter_start><jupyter_text>Filter Visualization with lucid<jupyter_code>import keras.backend as K
from keras.applications.vgg16 import VGG16

import matplotlib.pyplot as plt
%matplotlib inline
K.clear_session()
print("load model")
model = VGG16(input_shape=(224,224,3),weights="imagenet")
from tensorflow.python.framework import graph_util
from tensorflow.python.framework import graph_io

#https://qiita.com/cvusk/items/a2a0a11815de491cf3e5
# load tensorflow and keras backend
ksess = K.get_session()
print(ksess)

# transform keras model to tensorflow graph
# the output will be json-like format
K.set_learning_phase(0)
graph = ksess.graph
kgraph = graph.as_graph_def()

# define output node
num_output = 1
prefix = "output"
pred = [None]*num_output
outputName = [None]*num_output
for i in range(num_output):
    outputName[i] = prefix + str(i)
    pred[i] = tf.identity(model.get_output_at(i), name=outputName[i])
print('output name: ', outputName)

# convert variables in the model graph to constants
constant_graph = graph_util.convert_variables_to_constants(ksess, ksess.graph.as_graph_def(), outputName)

# save the model in .pb and .txt
output_dir = "./"
output_graph_name = "keras2tf.pb"
output_text_name = "keras2tf.txt"
graph_io.write_graph(constant_graph, output_dir, output_graph_name, as_text=False)
graph_io.write_graph(constant_graph, output_dir, output_text_name, as_text=True)
print('saved graph .pb at: {0}\nsaved graph .txt at: {1}'.format(
        os.path.join(output_dir, output_graph_name),
        os.path.join(output_dir, output_text_name)))
def load_graph(model_file):
    graph = tf.Graph()
    graph_def = tf.GraphDef()

    with open(model_file, "rb") as f:
        graph_def.ParseFromString(f.read())
    with graph.as_default():
        tf.import_graph_def(graph_def)
    return graph
#model = load_graph(os.path.join(output_dir, "keras2tf.pb"))
#opers = model.get_operations()
from lucid.modelzoo.vision_base import Model
class tfmodel(Model):
    model_path = 'VGG16.pb.modelzoo'
    #labels_path = 'keras2tf.txt'
    image_shape = [224,224,3]
    image_value_range = (0, 255)
    input_name = 'input_1'
model = tfmodel()
model.load_graphdef()
[print(n.name) for n in tf.get_default_graph().as_graph_def().node]
#cannot visualize after flatten or globalaverage pooling layer. lucid problem.
obj = objectives.channel("predictions/BiasAdd", 0)
#obj = objectives.channel("block5_conv3/BiasAdd", 10)
#param_f = lambda: param.image(224)

images = render_vis(model, obj,relu_gradient_override=False)
print(a[0][0].shape)
plt.imshow(images[0][0])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11981566820276497, 'fraction_numerical': 0.022657450076804916, 'mean_word_length': 5.662404092071611, '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': '256696', 'license': 'Apache-2.0', 'n_tokens_mistral': 957, 'n_tokens_neox': 923, 'n_words': 197, 'path': 'kwb/.ipynb_checkpoints/1_filter_activation_with_lucid-checkpoint.ipynb', 'repo_name': 'totti0223/keraswhitebox', 'size': '2604'}