You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
2.2 KiB

  1. import torch.nn as nn
  2. import torchvision.transforms as transforms
  3. class AlexNetOWT_BN(nn.Module):
  4. def __init__(self, num_classes=1000):
  5. super(AlexNetOWT_BN, self).__init__()
  6. self.features = nn.Sequential(
  7. nn.Conv2d(3, 128, kernel_size=3, stride=1, padding=1,
  8. bias=False),
  9. nn.BatchNorm2d(128),
  10. nn.ReLU(inplace=True),
  11. nn.Conv2d(128, 128, kernel_size=3, padding=1, bias=False),
  12. nn.MaxPool2d(kernel_size=2, stride=2),
  13. nn.ReLU(inplace=True),
  14. nn.BatchNorm2d(128),
  15. nn.Conv2d(128, 256, kernel_size=3, padding=1, bias=False),
  16. nn.ReLU(inplace=True),
  17. nn.BatchNorm2d(256),
  18. nn.Conv2d(256, 256, kernel_size=3, padding=1, bias=False),
  19. nn.MaxPool2d(kernel_size=2, stride=2),
  20. nn.ReLU(inplace=True),
  21. nn.BatchNorm2d(256),
  22. nn.Conv2d(256, 512, kernel_size=3, padding=1, bias=False),
  23. nn.ReLU(inplace=True),
  24. nn.BatchNorm2d(512),
  25. nn.Conv2d(512, 512, kernel_size=3, padding=1, bias=False),
  26. nn.MaxPool2d(kernel_size=2, stride=2),
  27. nn.ReLU(inplace=True),
  28. nn.BatchNorm2d(512),
  29. )
  30. self.classifier = nn.Sequential(
  31. nn.Linear(512 * 4 * 4, 1024, bias=False),
  32. nn.BatchNorm1d(1024),
  33. nn.ReLU(inplace=True),
  34. nn.Dropout(0.5),
  35. nn.Linear(1024, 1024, bias=False),
  36. nn.BatchNorm1d(1024),
  37. nn.ReLU(inplace=True),
  38. nn.Dropout(0.5),
  39. nn.Linear(1024, num_classes)
  40. nn.LogSoftMax()
  41. )
  42. self.regime = {
  43. 0: {'optimizer': 'SGD', 'lr': 1e-2,
  44. 'weight_decay': 5e-4, 'momentum': 0.9},
  45. 10: {'lr': 5e-3},
  46. 15: {'lr': 1e-3, 'weight_decay': 0},
  47. 20: {'lr': 5e-4},
  48. 25: {'lr': 1e-4}
  49. }
  50. def forward(self, x):
  51. x = self.features(x)
  52. x = x.view(-1, 512 * 4 * 4)
  53. x = self.classifier(x)
  54. return x
  55. def model(**kwargs):
  56. num_classes = kwargs.get( 'num_classes', 1000)
  57. return AlexNetOWT_BN(num_classes)