<jupyter_start><jupyter_text>Import libraries<jupyter_code>from keras.applications import VGG16
from keras import models
from google.colab import drive
from keras.preprocessing import image
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf<jupyter_output><empty_output><jupyter_text>Loading Data<jupyter_code>drive.mount('/content/drive/', force_remount=False)
img_path = '/content/drive/My Drive/Colab Notebooks/datasets/cats_dogs_small/train/cats/cat.1.jpg'
img = image.load_img(img_path,target_size=(224,224))
img_tensor = image.img_to_array(img)
img_tensor.shape
img_tensor = np.expand_dims(img_tensor, axis=0)
img_tensor.shape
img_tensor /= img_tensor.max()
plt.imshow(img_tensor[0])
plt.show()
plt.matshow(img_tensor[0,:,:,0])<jupyter_output><empty_output><jupyter_text>Call a training model (VGG16)<jupyter_code>model = VGG16()
model.summary()
model.layers
len(model.layers)
model.get_layer(index=0)
for layer in model.layers:
  if 'conv' not in layer.name:
    continue
  filters, biases = layer.get_weights()
  print("The {} has filter with dimension : {}".format(layer.name, filters.shape))
for layer in model.layers:
  if 'block1_conv1' not in layer.name:
    continue
  filters, biases = layer.get_weights()
  print(filters[0][0][0])
layer_outputs = [layer.output for layer in model.layers]
layer_outputs
activation_model = models.Model(inputs=model.input, outputs=layer_outputs)
activation_model
activations = activation_model.predict(img_tensor)
first_layer_activation = activations[0]
first_layer_activation.shape
for ind in range(len(activations)):
  activation = activations[ind]
  print(activation.shape)<jupyter_output>(1, 224, 224, 3)
(1, 224, 224, 64)
(1, 224, 224, 64)
(1, 112, 112, 64)
(1, 112, 112, 128)
(1, 112, 112, 128)
(1, 56, 56, 128)
(1, 56, 56, 256)
(1, 56, 56, 256)
(1, 56, 56, 256)
(1, 28, 28, 256)
(1, 28, 28, 512)
(1, 28, 28, 512)
(1, 28, 28, 512)
(1, 14, 14, 512)
(1, 14, 14, 512)
(1, 14, 14, 512)
(1, 14, 14, 512)
(1, 7, 7, 512)
(1, 25088)
(1, 4096)
(1, 4096)
(1, 1000)<jupyter_text>Visualizating the activations<jupyter_code>def plot_layer(activation, i_layer):
  #fig, a = plt.subplots(1,3)
  layer_activation = activation[i_layer]
  plt.figure()
  #for i in range(3):
    #plt.subplot(1,3,i)
  plt.matshow(layer_activation[0,:,:,0], cmap='viridis', fignum=1)
  plt.matshow(layer_activation[0,:,:,1], cmap='viridis', fignum=2)
  plt.matshow(layer_activation[0,:,:,2], cmap='viridis', fignum=3)
plot_layer(activations, 0)
def plot_layer1(activation, i_layer):
  plt.figure()
  layer_activation = activation[i_layer]
  for i in range(3):
    plt.subplot(1, 3, i+1)
    data = layer_activation[0,:,:,i]
    plt.imshow(data)
plot_layer1(activations, 1)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1513493800145879, 'fraction_numerical': 0.08205689277899343, 'mean_word_length': 6.237467018469657, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 0, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '75378', 'license': 'MIT', 'n_tokens_mistral': 1270, 'n_tokens_neox': 1080, 'n_words': 242, 'path': 'Visualizing_intermediate_convnet_vgg16_outputs.ipynb', 'repo_name': 'Phanttan/DeepLearning-Miscellanious', 'size': '2742'}