<jupyter_start><jupyter_text>5.4 Visualizing what convnets learn * Visualizing intermediate convnet outputs (intermediate activations)—Useful forunderstanding how successive convnet layers transform their input, and for getting a first idea of the meaning of individual convnet filters.* Visualizing convnets filters—Useful for understanding precisely what visual pattern or concept each filter in a convnet is receptive to.* Visualizing heatmaps of class activation in an image—Useful for understandingwhich parts of an image were identified  5.4.1 Visualizing intermediate activations<jupyter_code>import tensorflow as tf
tf.compat.v1.disable_eager_execution()
from tensorflow.keras.models import load_model
model = load_model('/cats_and_dogs_small_1.h5')
model.summary()<jupyter_output><empty_output><jupyter_text>Listing 5.25 Preprocessing a single image<jupyter_code>img_path = '/cat.1.jpg'
from tensorflow.keras.preprocessing import image
import numpy as np
img = image.load_img(img_path, target_size=(150, 150))
img_tensor = image.img_to_array(img)
img_tensor = np.expand_dims(img_tensor, axis=0)
img_tensor /= 255.
# <1> Its shape is (1, 150, 150, 3)
print(img_tensor.shape)<jupyter_output><empty_output><jupyter_text>Listing 5.26 Displaying the test picture<jupyter_code>import matplotlib.pyplot as plt
plt.imshow(img_tensor[0])
plt.show()<jupyter_output><empty_output><jupyter_text>Listing 5.27 Instantiating a model from an input tensor and a list of output tensors<jupyter_code>from tensorflow.keras import models
layer_outputs = [layer.output for layer in model.layers[:8]]
activation_model = models.Model(inputs=model.input, outputs=layer_outputs)<jupyter_output><empty_output><jupyter_text>Listing 5.28 Running the model in predict mode import tensorflow as tfconfig = tf.compat.v1.ConfigProto()config.gpu_options.allow_growth=Truesess = tf.compat.v1.Session(config=config)print("Num GPUs Available: ", len(tf.config.experimental.list_physical_devices('GPU')))tf.debugging.set_log_device_placement(True)import tensorflow as tfconfig = tf.compat.v1.ConfigProto()sess = tf.compat.v1.Session(config=config) import tensorflow as tfphysical_devices = tf.config.experimental.list_physical_devices('GPU')if len(physical_devices) > 0:    tf.config.experimental.set_memory_growth(physical_devices[0], True)<jupyter_code>activations = activation_model.predict(img_tensor)

first_layer_activation = activations[0]
print(first_layer_activation.shape)
activation_model<jupyter_output><empty_output><jupyter_text>Listing 5.29 Visualizing the fourth channel<jupyter_code>import matplotlib.pyplot as plt
plt.matshow(first_layer_activation[0, :, :, 4], cmap='viridis')<jupyter_output><empty_output><jupyter_text>Listing 5.30 Visualizing the seventh channel<jupyter_code>plt.matshow(first_layer_activation[0, :, :, 7], cmap='viridis')<jupyter_output><empty_output><jupyter_text>Listing 5.31 Visualizing every channel in every intermediate activation<jupyter_code>layer_names = []
for layer in model.layers[:8]:
    layer_names.append(layer.name)
images_per_row = 16
for layer_name, layer_activation in zip(layer_names, activations):
    n_features = layer_activation.shape[-1]
    size = layer_activation.shape[1]
    n_cols = n_features // images_per_row
    display_grid = np.zeros((size * n_cols, images_per_row * size))
    for col in range(n_cols):
        for row in range(images_per_row):
            channel_image = layer_activation[0,:, :,
                                             col * images_per_row + row]
            channel_image -= channel_image.mean()
            channel_image /= channel_image.std()
            channel_image *= 64
            channel_image += 128
            channel_image = np.clip(channel_image, 0, 255).astype('uint8')
            display_grid[col * size : (col + 1) * size,
                         row * size : (row + 1) * size] = channel_image
    scale = 1. / size
    plt.figure(figsize=(scale * display_grid.shape[1],
    scale * display_grid.shape[0]))
    plt.title(layer_name)
    plt.grid(False)
    plt.imshow(display_grid, aspect='auto', cmap='viridis')<jupyter_output><empty_output><jupyter_text>Listing 5.32 Defining the loss tensor for filter visualization<jupyter_code>from tensorflow.keras.applications import VGG16
from tensorflow.keras import backend as K

model = VGG16(weights='imagenet',
            include_top=False)

layer_name = 'block3_conv1'
filter_index = 0

layer_output = model.get_layer(layer_name).output
loss = K.mean(layer_output[:, :, :, filter_index])
import tensorflow as tf
tf.compat.v1.disable_eager_execution()<jupyter_output><empty_output><jupyter_text>Listing 5.33 Obtaining the gradient of the loss with regard to the input<jupyter_code>grads = K.gradients(loss, model.input)[0]
grads<jupyter_output><empty_output><jupyter_text>**Listing 5.34 Gradient-normalization **<jupyter_code>grads /= (K.sqrt(K.mean(K.square(grads))) + 1e-5)
grads<jupyter_output><empty_output><jupyter_text>**Listing 5.35 Fetching Numpy output values given Numpy input values**<jupyter_code>iterate = K.function([model.input], [loss, grads])
import numpy as np
loss_value, grads_value = iterate([np.zeros((1, 150, 150, 3))])<jupyter_output><empty_output><jupyter_text>**Listing 5.36 Loss maximization via stochastic gradient descent**<jupyter_code>input_img_data = np.random.random((1, 150, 150, 3)) * 20 + 128.
step = 1.
for i in range(40):
  loss_value, grads_value = iterate([input_img_data])
  input_img_data += grads_value * step<jupyter_output><empty_output><jupyter_text>**Listing 5.37 Utility function to convert a tensor into a valid image**<jupyter_code>def deprocess_image(x):
  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<jupyter_output><empty_output><jupyter_text>**Listing 5.38 Function to generate filter visualizations**<jupyter_code>def generate_pattern(layer_name, filter_index, size=150):
  layer_output = model.get_layer(layer_name).output
  loss = K.mean(layer_output[:, :, :, filter_index])
  grads = K.gradients(loss, model.input)[0]
  grads /= (K.sqrt(K.mean(K.square(grads))) + 1e-5)
  iterate = K.function([model.input], [loss, grads])
  input_img_data = np.random.random((1, size, size, 3)) * 20 + 128.
  step = 1.
  for i in range(40):
    loss_value, grads_value = iterate([input_img_data])
    input_img_data += grads_value * step
  img = input_img_data[0]
  return deprocess_image(img)
plt.imshow(generate_pattern('block3_conv1', 0))<jupyter_output><empty_output><jupyter_text>**Listing 5.39 Generating a grid of all filter response patterns in a layer**<jupyter_code>layer_name = 'block1_conv1'
size = 64
margin = 5
results = np.zeros((8 * size+7* margin, 8 * size+7* margin, 3))

for i in range(8):
  for j in range(8):
    filter_img = generate_pattern(layer_name, i + (j * 8), size=size)
    horizontal_start = i * size + i * margin
    horizontal_end = horizontal_start + size
    vertical_start = j * size + j * margin
    vertical_end = vertical_start + size
    results[horizontal_start: horizontal_end,
            vertical_start: vertical_end, :] = filter_img
plt.figure(figsize=(20, 20))
plt.imshow(results)<jupyter_output><empty_output><jupyter_text>**5.4.3 Visualizing heatmaps of class activation**<jupyter_code>from keras.applications.vgg16 import VGG16
model = VGG16(weights='imagenet')

img_path = '/content/abc.jpg'
img = image.load_img(img_path, target_size=(224, 224))
from keras.preprocessing import image
from keras.applications.vgg16 import preprocess_input, decode_predictions
import numpy as np
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
 preds = model.predict(x)
 print('Predicted:', decode_predictions(preds, top=3)[0])
np.argmax(preds[0])<jupyter_output><empty_output><jupyter_text>**Listing 5.42 Setting up the Grad-CAM algorithm**<jupyter_code>african_e66lephant_output = model.output[:, 386]
last_conv_layer = model.get_layer('block5_conv3')

grads = K.gradients(african_e66lephant_output, last_conv_layer.output)[0]
pooled_grads = K.mean(grads, axis=(0, 1, 2))
iterate = K.function([model.input],
                     [pooled_grads, last_conv_layer.output[0]])

pooled_grads_value, conv_layer_output_value = iterate([x])
for i in range(512):
  conv_layer_output_value[:, :, i] *= pooled_grads_value[i]
heatmap = np.mean(conv_layer_output_value, axis=-1)<jupyter_output><empty_output><jupyter_text>**Listing 5.43 Heatmap post-processing**<jupyter_code>heatmap = np.maximum(heatmap, 0)
heatmap /= np.max(heatmap)
plt.matshow(heatmap)<jupyter_output><empty_output><jupyter_text>**Listing 5.44 Superimposing the heatmap with the original picture**<jupyter_code>import cv2
img = cv2.imread(img_path)
heatmap = cv2.resize(heatmap, (img.shape[1], img.shape[0]))
heatmap = np.uint8(255 * heatmap)
heatmap = cv2.applyColorMap(heatmap, cv2.COLORMAP_JET)
superimposed_img = heatmap * 0.4 + img
cv2.imwrite('/content/ab.jpg', superimposed_img)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11782309909111062, 'fraction_numerical': 0.02859676346708047, 'mean_word_length': 5.7486910994764395, 'pattern_counts': {'":': 0, '<': 82, '<?xml version=': 0, '>': 83, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '20', 'dir': 'jupyter-structured-clean-dedup', 'id': '159541', 'license': 'MIT', 'n_tokens_mistral': 3318, 'n_tokens_neox': 3098, 'n_words': 645, 'path': 'Q3/DeepLearning Part -2/Chapter5/5.4 (Visualizing Convnets).ipynb', 'repo_name': 'qasim1020/PIAIC-Artificial-Intelligence', 'size': '9022'}