How remove filter from layers and initialize custom filter weights to layers

I have to remove filter from layers.
VGG16 model, here are two layer
model = models.vgg16(pretrained=True)
print(model)
print(model.features[2].weight.shape) # features[2].weight has 64 filters
torch.Size([64, 64, 3, 3])
print(model.features[5].weight.shape) # features[5].weight has 128 filters
torch.Size([128, 64, 3, 3])

Is there any way (function or method) to remove filter#53 from model.features[2].weight and filter#109 from features[5].weight.

Also:
How to initialize model.features[5].weight of VGG16 with custom filter weights of same shape.

model = models.vgg16(pretrained=True)
print(model)
print(model.features[5].weight.shape)
torch.Size([128, 64, 3, 3])

Thanks

You can remove features by doing torch.cat([a[:53], a[54:]]).

For initialization you can directly operate on model.features[5].weight tensor or pass the tensor to some method of torch.nn.init to initialize it.

1 Like

Thank you very much Kushajveer Singh

1 Like