--- title: SparsifyCallback keywords: fastai sidebar: home_sidebar summary: "Use the sparsifier in fastai Callback system" description: "Use the sparsifier in fastai Callback system" nb_path: "nbs/02_sparsify_callback.ipynb" ---
{% raw %}
{% endraw %} {% raw %}
 
{% endraw %} {% raw %}
{% endraw %} {% raw %}
path = untar_data(URLs.PETS)
files = get_image_files(path/"images")

def label_func(f): return f[0].isupper()
{% endraw %} {% raw %}
dls = ImageDataLoaders.from_name_func(path, files, label_func, item_tfms=Resize(64))
{% endraw %} {% raw %}

class SparsifyCallback[source]

SparsifyCallback(end_sparsity, granularity, method, criteria, sched_func, start_sparsity=0, start_epoch=0, end_epoch=None, lth=False, rewind_epoch=0, reset_end=False, model=None, round_to=None, layer_type=Conv2d) :: Callback

Basic class handling tweaks of the training loop by changing a Learner in various events

{% endraw %} {% raw %}
{% endraw %}

The most important part of our Callback happens in before_batch. There, we first compute the sparsity of our network according to our schedule and then we remove the parameters accordingly.

{% raw %}
learn = cnn_learner(dls, resnet18, metrics=accuracy)
learn.unfreeze()
{% endraw %} {% raw %}
learn.fit_one_cycle(3)
epoch train_loss valid_loss accuracy time
0 0.646404 0.579887 0.828146 00:09
1 0.384775 0.310144 0.872801 00:09
2 0.213229 0.212824 0.912720 00:08
{% endraw %}

Let's now try adding some sparsity in our model

{% raw %}
learn = cnn_learner(dls, resnet18, metrics=accuracy)
learn.unfreeze()
{% endraw %}

The SparsifyCallback requires a new argument compared to the Sparsifier. Indeed, we need to know the pruning schedule that we should follow during training in order to prune the parameters accordingly.

You can use any scheduling function already available in fastai or come up with your own ! For more information about the pruning schedules, take a look at the Schedules section.

{% raw %}
sp_cb = SparsifyCallback(end_sparsity=50, granularity='weight', method='local', criteria=large_final, sched_func=sched_cos)
{% endraw %} {% raw %}
learn.fit_one_cycle(3, cbs=sp_cb)
Pruning of weight until a sparsity of [50]%
Saving Weights at epoch 0
epoch train_loss valid_loss accuracy time
0 0.626793 0.787221 0.795670 00:10
1 0.378027 0.252783 0.888363 00:10
2 0.214151 0.201621 0.912043 00:10
Sparsity at the end of epoch 0: [12.5]%
Sparsity at the end of epoch 1: [37.5]%
Sparsity at the end of epoch 2: [50.0]%
Final Sparsity: [50.0]%
Sparsity in Conv2d 2: 50.00%
Sparsity in Conv2d 8: 50.00%
Sparsity in Conv2d 11: 50.00%
Sparsity in Conv2d 14: 50.00%
Sparsity in Conv2d 17: 50.00%
Sparsity in Conv2d 21: 50.00%
Sparsity in Conv2d 24: 50.00%
Sparsity in Conv2d 27: 50.00%
Sparsity in Conv2d 30: 50.00%
Sparsity in Conv2d 33: 50.00%
Sparsity in Conv2d 37: 50.00%
Sparsity in Conv2d 40: 50.00%
Sparsity in Conv2d 43: 50.00%
Sparsity in Conv2d 46: 50.00%
Sparsity in Conv2d 49: 50.00%
Sparsity in Conv2d 53: 50.00%
Sparsity in Conv2d 56: 50.00%
Sparsity in Conv2d 59: 50.00%
Sparsity in Conv2d 62: 50.00%
Sparsity in Conv2d 65: 50.00%
{% endraw %}

Surprisinlgy, our network that is composed of $50 \%$ of zeroes performs reasonnably well when compared to our plain and dense network.

The SparsifyCallback also accepts a list of sparsities, corresponding to each layer of layer_type to be pruned. Below, we show how to prune only the intermediate layers of ResNet-18.

{% raw %}
learn = cnn_learner(dls, resnet18, metrics=accuracy)
learn.unfreeze()
{% endraw %} {% raw %}
sparsities = [0, 0, 0, 0, 0, 0, 50, 50, 50, 50, 50, 50, 50, 50, 0, 0, 0, 0, 0, 0]
{% endraw %} {% raw %}
sp_cb = SparsifyCallback(end_sparsity=sparsities, granularity='weight', method='local', criteria=large_final, sched_func=sched_cos)
{% endraw %} {% raw %}
learn.fit_one_cycle(4, cbs=sp_cb)
Pruning of weight until a sparsity of [0, 0, 0, 0, 0, 0, 50, 50, 50, 50, 50, 50, 50, 50, 0, 0, 0, 0, 0, 0]%
Saving Weights at epoch 0
epoch train_loss valid_loss accuracy time
0 0.627289 0.399411 0.831529 00:09
1 0.365587 0.264053 0.893775 00:09
2 0.212597 0.193413 0.917456 00:09
3 0.120238 0.178505 0.933694 00:10
Sparsity at the end of epoch 0: [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 7.32, 7.32, 7.32, 7.32, 7.32, 7.32, 7.32, 7.32, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]%
Sparsity at the end of epoch 1: [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 25.0, 25.0, 25.0, 25.0, 25.0, 25.0, 25.0, 25.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]%
Sparsity at the end of epoch 2: [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 42.68, 42.68, 42.68, 42.68, 42.68, 42.68, 42.68, 42.68, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]%
Sparsity at the end of epoch 3: [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 50.0, 50.0, 50.0, 50.0, 50.0, 50.0, 50.0, 50.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]%
Final Sparsity: [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 50.0, 50.0, 50.0, 50.0, 50.0, 50.0, 50.0, 50.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]%
Sparsity in Conv2d 2: 0.00%
Sparsity in Conv2d 8: 0.00%
Sparsity in Conv2d 11: 0.00%
Sparsity in Conv2d 14: 0.00%
Sparsity in Conv2d 17: 0.00%
Sparsity in Conv2d 21: 0.00%
Sparsity in Conv2d 24: 50.00%
Sparsity in Conv2d 27: 50.00%
Sparsity in Conv2d 30: 50.00%
Sparsity in Conv2d 33: 50.00%
Sparsity in Conv2d 37: 50.00%
Sparsity in Conv2d 40: 50.00%
Sparsity in Conv2d 43: 50.00%
Sparsity in Conv2d 46: 50.00%
Sparsity in Conv2d 49: 0.00%
Sparsity in Conv2d 53: 0.00%
Sparsity in Conv2d 56: 0.00%
Sparsity in Conv2d 59: 0.00%
Sparsity in Conv2d 62: 0.00%
Sparsity in Conv2d 65: 0.00%
{% endraw %}

On top of that, the SparsifyCallbackcan also take many optionnal arguments:

  • start_sparsity: the sparsity that the schedule will use as a starting point (default to 0)
  • start_epoch: the epoch at which the schedule will start pruning (default to 0)
  • end_epoch: the epoch at which the schedule will stop pruning (default to the training epochs passed in fit)
  • lth: whether training using the Lottery Ticket Hypothesis, i.e. reset the weights to their original value at each pruning step (more information in the Lottery Ticket Hypothesis section)
  • rewind_epoch: the epoch used as a reference for the Lottery Ticket Hypothesis with Rewinding (default to 0)
  • reset_end: whether you want to reset the weights to their original values after training (pruning masks are still applied)
  • model: pass a model or a part of the model if you don't want to apply pruning on the whole model trained.
  • layer_type: specify the type of layer that you want to apply pruning to (default to nn.Conv2d)

For example, we correctly pruned the convolution layers of our model, but we could imagine pruning the Linear Layers of even only the BatchNorm ones !