Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how Pruning the last conv layer affects the first linear layer of the classifier #36

Open
Saharkakavand opened this issue Jan 9, 2020 · 5 comments

Comments

@Saharkakavand
Copy link

Saharkakavand commented Jan 9, 2020

I trained the vgg and saved the model as pth file. then I load it for pruning some filters of it.
the last conv after pruning is not 512 anymore, some filters are gone.
how Pruning the last conv layer affects the first linear layer of the classifier which is (512 7 7, 4096).
how can I prune the input weights of classifier according to the last conv layer.

@Eric-mingjie
Copy link
Owner

how Pruning the last conv layer affects the first linear layer of the classifier which is (512 7 7, 4096).

It changed the input dimension of the first fc layer.

how can I prune the input weights of classifier according to the last conv layer.

The input weights of classifier does not change with the last conv layer.

@Saharkakavand
Copy link
Author

how Pruning the last conv layer affects the first linear layer of the classifier which is (512 7 7, 4096).

It changed the input dimension of the first fc layer.

how can I prune the input weights of classifier according to the last conv layer.

The input weights of classifier does not change with the last conv layer.

nn.Conv2d(512, 512, 3, padding=1), is changed to nn.Conv2d(450, 412, 3, padding=1)
how this one will change: nn.Linear(in_features=512 * 7 * 7, out_features=4096, bias=True),

class VGGOWN(nn.Module):

def __init__(self):
    super(VGGOWN, self).__init__()
    self.features = nn.Sequential(
        nn.Conv2d(3, 64, 3, padding=1),
        nn.ReLU(inplace=True),
        nn.Conv2d(64, 64, 3, padding=1),
        nn.ReLU(inplace=True),
        nn.MaxPool2d(2, stride=2, ceil_mode=True),  # 1/2

        # conv2
        nn.Conv2d(64, 128, 3, padding=1),
        nn.ReLU(inplace=True),
        nn.Conv2d(128, 128, 3, padding=1),
        nn.ReLU(inplace=True),
        nn.MaxPool2d(2, stride=2, ceil_mode=True),  # 1/4

        # conv3
        nn.Conv2d(128, 256, 3, padding=1),
        nn.ReLU(inplace=True),
        nn.Conv2d(256, 256, 3, padding=1),
        nn.ReLU(inplace=True),
        nn.Conv2d(256, 256, 3, padding=1),
        nn.ReLU(inplace=True),
        nn.MaxPool2d(2, stride=2, ceil_mode=True), # 1/8

        # conv4
        nn.Conv2d(256, 512, 3, padding=1),
        nn.ReLU(inplace=True),
        nn.Conv2d(512, 512, 3, padding=1),
        nn.ReLU(inplace=True),
        nn.Conv2d(512, 512, 3, padding=1),
        nn.ReLU(inplace=True),
        nn.MaxPool2d(2, stride=2, ceil_mode=True),  # 1/16

        # conv5
        nn.Conv2d(512, 512, 3, padding=1),
        nn.ReLU(inplace=True),
        nn.Conv2d(512, 512, 3, padding=1),
        nn.ReLU(inplace=True),
        nn.Conv2d(512, 512, 3, padding=1),
        nn.ReLU(inplace=True),
        nn.MaxPool2d(2, stride=2, ceil_mode=True) # 1/32
    )
    self.avgpool = nn.AdaptiveAvgPool2d((7, 7))

    self.classifier = nn.Sequential(

        nn.Linear(in_features=512 * 7 * 7, out_features=4096, bias=True),
        nn.ReLU(inplace=True),
        nn.Dropout(p=0.5),
        nn.Linear(in_features=4096, out_features=4096, bias=True),
        nn.ReLU(inplace=True),
        nn.Dropout(p=0.5),
        nn.Linear(in_features=4096, out_features=1000, bias=True)
    )

def forward(self, *input):
    x = self.features(input)
    #print(x.size)
    x = self.avgpool(x)
    #print(x.size)
    x = x.view(x.size(0), -1)
    #print(x.size)
    y = self.classifier(x)
    return y

thanks

@Eric-mingjie
Copy link
Owner

Should be nn.Linear(in_features=450 * 7 * 7, out_features=4096, bias=True) now if i understand the definition of nn.conv2d correctly.

@Saharkakavand
Copy link
Author

when I load the pretrained state-dict it has already (25088, 4096) weights in linear layer how can I know which one should be prun?
I know which filters I pruned in prev layer I dont know how conv layer weights is mapped to next layer

@Eric-mingjie
Copy link
Owner

Please try to understand vggprune.py in detail. Then you will know.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants