Pytorch Global Pruning is not reducing the size of the model

WhatsintheName: <p>I am trying to Prune my Deep Learning model via Global Pruning. The original UnPruned model is about 77.5 MB. However after pruning, when I am saving the model, the size of the model is the same as the original. Can anyone help me with this issue?</p>
<p>Below is the Pruning code:-</p>
<p>import torch.nn.utils.prune as prune</p>
<p>parameters_to_prune = (<br>
(model.encoder[0], ‘weight’),<br>
(model.up_conv1[0], ‘weight’),<br>
(model.up_conv2[0], ‘weight’),<br>
(model.up_conv3[0], ‘weight’),<br>
)<br>
print(parameters_to_prune)</p>
<p>prune.global_unstructured(<br>
parameters_to_prune,<br>
pruning_method=prune.L1Unstructured,<br>
amount=0.2,<br>
)</p>
<p>print(<br>
“Sparsity in Encoder.weight: {:.2f}%”.format(<br>
100. * float(torch.sum(model.encoder[0].weight == 0))<br>
/ float(model.encoder[0].weight.nelement())<br>
)<br>
)<br>
print(<br>
“Sparsity in up_conv1.weight: {:.2f}%”.format(<br>
100. * float(torch.sum(model.up_conv1[0].weight == 0))<br>
/ float(model.up_conv1[0].weight.nelement())<br>
)<br>
)<br>
print(<br>
“Sparsity in up_conv2.weight: {:.2f}%”.format(<br>
100. * float(torch.sum(model.up_conv2[0].weight == 0))<br>
/ float(model.up_conv2[0].weight.nelement())<br>
)<br>
)<br>
print(<br>
“Sparsity in up_conv3.weight: {:.2f}%”.format(<br>
100. * float(torch.sum(model.up_conv3[0].weight == 0))<br>
/ float(model.up_conv3[0].weight.nelement())<br>
)<br>
)</p>
<p>print(<br>
“Global sparsity: {:.2f}%”.format(<br>
100. * float(<br>
torch.sum(model.encoder[0].weight == 0)<br>
+ torch.sum(model.up_conv1[0].weight == 0)<br>
+ torch.sum(model.up_conv2[0].weight == 0)<br>
+ torch.sum(model.up_conv3[0].weight == 0)<br>
)<br>
/ float(<br>
model.encoder[0].weight.nelement()<br>
+ model.up_conv1[0].weight.nelement()<br>
+ model.up_conv2[0].weight.nelement()<br>
+ model.up_conv3[0].weight.nelement()<br>
)<br>
)<br>
)</p>
<h1>Setting Pruning to Permanent</h1>
<p>prune.remove(model.encoder[0], “weight”)<br>
prune.remove(model.up_conv1[0], “weight”)<br>
prune.remove(model.up_conv2[0], “weight”)<br>
prune.remove(model.up_conv3[0], “weight”)</p>
<h1>Saving the model</h1>
<p>PATH = “C:\PrunedNet.pt”<br>
torch.save(model.state_dict(), PATH)</p>

zhucheng725: <p>The prune method in pytorch is used for set matrix for zero if the weight of conv are not useful. So the size of the .pt model  also is  the same because the structure is the same.</p>

WhatsintheName: <p>Thank you for your response. Are you aware of any methods, by which I can actually reduce the model size?</p>

Ahmad_Khan: <p>save it in .tar format</p>

ube: <aside class="quote no-group" data-username="WhatsintheName" data-post="1" data-topic="109428">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/w/e95f7d/48.png" class="avatar"> WhatsintheName:</div>
<blockquote>
<p>However after</p>
</blockquote>
</aside>
<p>Use knowledge distillation instead if you are after actual memory footprint reduction.Here is the <a href="https://arxiv.org/abs/1503.02531?context=cs" rel="noopener nofollow ugc">paper</a>  and a sample code <a href="https://github.com/SforAiDl/KD_Lib" rel="noopener nofollow ugc">Sample code</a></p>