Last active
July 18, 2019 08:56
-
-
Save RaphaelMeudec/7c2875d184a732aed35ae21a6365ada1 to your computer and use it in GitHub Desktop.
Visualize outputs of activations with Tensorflow 2.0
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import tensorflow as tf | |
layers_name = ['activation_1'] | |
IMAGE_PATH = './cat.jpg' | |
# Model to examine | |
model = tf.keras.applications.resnet50.ResNet50(weights='imagenet', include_top=True) | |
# Image to pass as input | |
img = tf.keras.preprocessing.image.load_img(IMAGE_PATH, target_size=(224, 224)) | |
img = tf.keras.preprocessing.image.img_to_array(img) | |
# Get the outputs of layers we want to inspect | |
outputs = [ | |
layer.output for layer in model.layers | |
if layer.name in layers_name | |
] | |
# Create a connection between the input and those target outputs | |
activations_model = tf.keras.models.Model(model.inputs, outputs=outputs) | |
activations_model.compile(optimizer='adam', loss='categorical_crossentropy') | |
# Get their outputs | |
activations_1 = activations_model.predict(np.array([img])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment