<jupyter_start><jupyter_text>CNN Filter Visulization   1. Load the required Libraries and Download the Model<jupyter_code>import tensorflow as tf 
import math
import numpy
import matplotlib.pyplot as plt

print('Tensorflow version', tf.__version__)
model = tf.keras.applications.vgg16.VGG16(
    include_top = False, # Final Fully connected layers in CNN not inclued in the model
    weights ='imagenet', # CNN model trained on the given weights
    input_shape =(96,96,3) # 96 rows and columns and 3 channels

)

model.summary()<jupyter_output>Downloading data from https://storage.googleapis.com/tensorflow/keras-applications/vgg16/vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5
58892288/58889256 [==============================] - 1s 0us/step
58900480/58889256 [==============================] - 1s 0us/step
Model: "vgg16"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 input_1 (InputLayer)        [(None, 96, 96, 3)]       0         
                                                                 
 block1_conv1 (Conv2D)       (None, 96, 96, 64)        1792      
                                                                 
 block1_conv2 (Conv2D)       (None, 96, 96, 64)        36928     
                                                                 
 block1_pool (MaxPooling2D)  (None, 48, 48, 64)        0         
                                                        [...]<jupyter_text>2. Create the Submodel<jupyter_code>def get_submodel(name):
  return tf.keras.models.Model(
      model.input, 
      model.get_layer(name).output)

get_submodel('block2_conv1').summary()<jupyter_output>Model: "model"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 input_1 (InputLayer)        [(None, 96, 96, 3)]       0         
                                                                 
 block1_conv1 (Conv2D)       (None, 96, 96, 64)        1792      
                                                                 
 block1_conv2 (Conv2D)       (None, 96, 96, 64)        36928     
                                                                 
 block1_pool (MaxPooling2D)  (None, 48, 48, 64)        0         
                                                                 
 block2_conv1 (Conv2D)       (None, 48, 48, 128)       73856     
                                                                 
=================================================================
Total params: 112,576
Trainable params: 112,576
Non-trainable[...]<jupyter_text>3. Generate the random Image and use it for visuliszation<jupyter_code>def create_image():
  return tf.random.uniform((96,96,3))

def plot(image, title ='Random Generated Image'):
  image = image -tf.math.reduce_min(image)
  image = image / tf.math.reduce_max(image) #scaling the image for values between 0 & 1
  plt.imshow(image)
  plt.xticks([])
  plt.yticks([])
  plt.title(title)
  plt.show()

image =create_image()
plot(image)<jupyter_output><empty_output><jupyter_text>4. Create the Model Algorithm used for the Submodel<jupyter_code>def filter_visulise(layer_name, filter_index =None, max_iters = 500):
  submodel =get_submodel(layer_name)
  num_filters = submodel.output.shape[-1]

  if filter_index is None:
    filter_index =tf.experimental.numpy.random.randint(0,num_filters -1)
  assert num_filters > filter_index, 'Filer_index is out of bonds'
  
  # Show loss as given loss_step 
  image =create_image()
  loss_step =int(max_iters / 50)
  
  # Gradient Ascent 
  for i in range(0,max_iters):
    with tf.GradientTape() as tape:
      tape.watch(image)
      out = submodel(tf.expand_dims(image,axis=0))[:,:,:,filter_index]
      loss = tf.math.reduce_mean(out)
    gradient = tape.gradient(loss,image)
    gradient =tf.math.l2_normalize(gradient)
    image = image + gradient * 0.1 

    if (i + 1) % loss_step == 0:
      print(f'Iteration: {i + 1}, Loss : {loss.numpy():4f}')


  plot(image, f'{layer_name},{filter_index}')<jupyter_output><empty_output><jupyter_text>5. Run the Model and Visulize the Results Try running on different layer names to see what happens<jupyter_code>print([layer.name for layer in model.layers if 'conv' in layer.name])
layer_name = 'block1_conv1' #param [''block1_conv1','block1_conv2', 'block2_conv1', 'block2_conv2', 'block3_conv1', 'block3_conv2', 'block3_conv3', 'block4_conv1', 'block4_conv2', 'block5_conv1', 'block5_conv2', 'block5_conv3']
filter_visulise(layer_name)
layer_name = 'block1_conv2' #param [''block1_conv1','block1_conv2', 'block2_conv1', 'block2_conv2', 'block3_conv1', 'block3_conv2', 'block3_conv3', 'block4_conv1', 'block4_conv2', 'block5_conv1', 'block5_conv2', 'block5_conv3']
filter_visulise(layer_name)
layer_name = 'block3_conv1' #param [''block1_conv1','block1_conv2', 'block2_conv1', 'block2_conv2', 'block3_conv1', 'block3_conv2', 'block3_conv3', 'block4_conv1', 'block4_conv2', 'block5_conv1', 'block5_conv2', 'block5_conv3']
filter_visulise(layer_name)
layer_name = 'block3_conv3' #param [''block1_conv1','block1_conv2', 'block2_conv1', 'block2_conv2', 'block3_conv1', 'block3_conv2', 'block3_conv3', 'block4_conv1', 'block4_conv2', 'block5_conv1', 'block5_conv2', 'block5_conv3']
filter_visulise(layer_name)<jupyter_output>Iteration: 10, Loss : 1.371170
Iteration: 20, Loss : 2.904049
Iteration: 30, Loss : 4.546204
Iteration: 40, Loss : 6.241271
Iteration: 50, Loss : 7.900932
Iteration: 60, Loss : 9.531157
Iteration: 70, Loss : 11.131031
Iteration: 80, Loss : 12.666299
Iteration: 90, Loss : 14.156609
Iteration: 100, Loss : 15.607747
Iteration: 110, Loss : 16.980059
Iteration: 120, Loss : 18.269550
Iteration: 130, Loss : 19.538673
Iteration: 140, Loss : 20.772875
Iteration: 150, Loss : 21.961794
Iteration: 160, Loss : 23.109056
Iteration: 170, Loss : 24.210558
Iteration: 180, Loss : 25.272829
Iteration: 190, Loss : 26.309799
Iteration: 200, Loss : 27.302435
Iteration: 210, Loss : 28.250610
Iteration: 220, Loss : 29.168566
Iteration: 230, Loss : 30.056051
Iteration: 240, Loss : 30.916056
Iteration: 250, Loss : 31.769701
Iteration: 260, Loss : 32.612785
Iteration: 270, Loss : 33.431961
Iteration: 280, Loss : 34.234585
Iteration: 290, Loss : 35.032578
Iteration: 300, Loss : 35.817524
Iteration: 310, Loss : 36[...]
{'fraction_non_alphanumeric': 0.1462663587374904, 'fraction_numerical': 0.09591993841416474, 'mean_word_length': 2.9877225291589933, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 19, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '161375', 'license': 'Apache-2.0', 'n_tokens_mistral': 2473, 'n_tokens_neox': 2061, 'n_words': 487, 'path': 'CNN_Filter_Visulization.ipynb', 'repo_name': 'vijitVM/CNN-Filter-Visulization', 'size': '6495'}