Pruning Pretrained Neural Networks

61 Views Asked by At

I try to remove the neurons that was under the required threshold not to do them 0 but to delete it from the VGG neural network.

I did the follow:

import tensorflow as tf

Load the VGG16 model

model = tf.keras.applications.VGG16(weights='imagenet')

Display the model summary

model.summary()

import numpy as np

Access the layers of your model

layers = model.layers

Loop through the layers and print the number of weights for each layer

for layer in layers:

if hasattr(layer, 'weights'):

  if len(layer.get_weights()) != 0:

    weights, biases = layer.get_weights()

    weight_count = len(weights.flatten())

    median_weight = np.median(weights)     # Calculate the mean weight value for the layer

    # Prune weights that are below the median

    weights[weights < median_weight] = 0 

    print("weights", weight_count)

    

    zero_indices = np.where(weights == 0)[0]

    print("zero into each layer", len(zero_indices))

    # Set the pruned weights back to the layer

    layer.set_weights([weights, biases])

Print a summary of the model architecture

model.summary()

But the summary is the same. I want to delete the nodes not to do them 0, but I don't find a function to do that. I only found that i must create from the beginning the desired Neural Network with the pruned shapes of the layers.

Is it the only solution?

0

There are 0 best solutions below