Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

vgg.py 1.6 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
  1. '''VGG11/13/16/19 in Pytorch. Adapted from https://github.com/pytorch/vision/blob/master/torchvision/models/vgg.py'''
  2. import torch
  3. import torch.nn as nn
  4. from super_gradients.training.models.sg_module import SgModule
  5. cfg = {
  6. 'VGG11': [64, 'M', 128, 'M', 256, 256, 'M', 512, 512, 'M', 512, 512, 'M'],
  7. 'VGG13': [64, 64, 'M', 128, 128, 'M', 256, 256, 'M', 512, 512, 'M', 512, 512, 'M'],
  8. 'VGG16': [64, 64, 'M', 128, 128, 'M', 256, 256, 256, 'M', 512, 512, 512, 'M', 512, 512, 512, 'M'],
  9. 'VGG19': [64, 64, 'M', 128, 128, 'M', 256, 256, 256, 256, 'M', 512, 512, 512, 512, 'M', 512, 512, 512, 512, 'M'],
  10. }
  11. class VGG(SgModule):
  12. def __init__(self, vgg_name):
  13. super(VGG, self).__init__()
  14. self.features = self._make_layers(cfg[vgg_name])
  15. self.classifier = nn.Linear(512, 10)
  16. def forward(self, x):
  17. out = self.features(x)
  18. out = out.view(out.size(0), -1)
  19. out = self.classifier(out)
  20. return out
  21. def _make_layers(self, cfg):
  22. layers = []
  23. in_channels = 3
  24. for x in cfg:
  25. if x == 'M':
  26. layers += [nn.MaxPool2d(kernel_size=2, stride=2)]
  27. else:
  28. layers += [nn.Conv2d(in_channels, x, kernel_size=3, padding=1),
  29. nn.BatchNorm2d(x),
  30. nn.ReLU(inplace=True)]
  31. in_channels = x
  32. layers += [nn.AvgPool2d(kernel_size=1, stride=1)]
  33. return nn.Sequential(*layers)
  34. def test():
  35. net = VGG('VGG11')
  36. x = torch.randn(2, 3, 32, 32)
  37. y = net(x)
  38. print(y.size())
  39. # test()
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...