Skip to content

Fully convolutional version of all vgg models via constructor argument #184

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

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
16 changes: 14 additions & 2 deletions torchvision/models/vgg.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@

class VGG(nn.Module):

def __init__(self, features, num_classes=1000):
def __init__(self, features, num_classes=1000, fully_conv=False):
super(VGG, self).__init__()
self.features = features
self.fully_conv = fully_conv
self.classifier = nn.Sequential(
nn.Linear(512 * 7 * 7, 4096),
nn.ReLU(True),
Expand All @@ -35,11 +36,22 @@ def __init__(self, features, num_classes=1000):
nn.Dropout(),
nn.Linear(4096, num_classes),
)
if fully_conv:
self.classifier = nn.Sequential(
nn.Conv2d(512, 4096, 7, 1, 3),
nn.ReLU(True),
nn.Dropout(),
nn.Conv2d(4096, 4096, 1),
nn.ReLU(True),
nn.Dropout(),
nn.Conv2d(4096, num_classes, 1)
)
self._initialize_weights()

def forward(self, x):
x = self.features(x)
x = x.view(x.size(0), -1)
if not self.fully_conv:
x = x.view(x.size(0), -1)
x = self.classifier(x)
return x

Expand Down