Logo elodees  elodees

A caring AI for a better world













Only alphabetic characters accented or not as well as the space are accepted

Logo IA




Activation maximization





No account yet ?

Sign up to access all content




Tested in Anaconda and Python 3.7

import tensorflow as tf
tf.enable_eager_execution()
import numpy as np
import matplotlib.pyplot as plt
 
np.random.seed(500)
 
model = tf.keras.applications.vgg16.VGG16(include_top=True, weights="imagenet")
 
model.summary()
 
# trick to get optimal visualizations: swap softmax with identity/linear
model.get_layer("predictions").activation = None
 
random_image = np.zeros((224, 224, 3)).astype(np.float32)
plt.title("Random Image")
plt.imshow(random_image)
plt.show()
 
random_image = np.expand_dims(random_image, axis=0)  # reshape it to (1,224,224,3)
 
def get_gradients(model, image, class_index):
    image_tensor = tf.convert_to_tensor(image, dtype="float32")
    with tf.GradientTape() as tape:
      # The visualization is very sensitive to the regularization parameter!
        regularizer_l2 = tf.keras.regularizers.l2(l=0.01)
        tape.watch(image_tensor)
        output = model(image_tensor)
        loss = tf.reduce_mean(output[:, class_index] - regularizer_l2(image_tensor))
    grads = tape.gradient(loss, image_tensor)
    return grads
 
step_size = 1
epochs = 1000
class_index = 20  # ouzel
progbar = tf.keras.utils.Progbar(epochs)
for i in range(epochs):
    grads = get_gradients(model, random_image, class_index=class_index)
    random_image += grads * step_size   # + is gradient ascent
    progbar.update(i+1)
 
def deprocess_image(x):
    """Utility function to convert a tensor into a valid image
    """
    x = np.squeeze(x.numpy(), axis=0)
    x -= x.mean()
    x /= (x.std() + 1e-5)
    x *= 0.1
    x += 0.5
    x = np.clip(x, 0, 1)
    x *= 255
    x = np.clip(x, 0, 255).astype('uint8')
    return x
 
activation_maximization = deprocess_image(random_image)
 
plt.figure(figsize=(20,20))
plt.imshow(activation_maximization)
plt.show()
 
plt.imsave('Results/activation_maximization.jpg', activation_maximization)
 


Visualizing-and-Understanding-CNNs

License: MITLicenseMIT  Copyright (c) 2020 Abhijit Balaji


GitHub



Free image provided by pexel.com












Welcome, my name is Eric Soupet and I am the administrator of the site elodees.com. elodees.com is a state of the art of Artificial Intelligence and aims to be collaborative, you can now offer content such as articles, events, tutorials, ... so don't hesitate !

Platform images credit : Pixabay - Pixabay License | Pexels - Pexels License