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.

92 lines
3.2 KiB

  1. import torch.nn as nn
  2. import torchvision.transforms as transforms
  3. from .binarized_modules import BinarizeLinear,BinarizeConv2d
  4. __all__ = ['alexnet_binary']
  5. class AlexNetOWT_BN(nn.Module):
  6. def __init__(self, num_classes=1000):
  7. super(AlexNetOWT_BN, self).__init__()
  8. self.ratioInfl=3
  9. self.features = nn.Sequential(
  10. BinarizeConv2d(3, int(64*self.ratioInfl), kernel_size=11, stride=4, padding=2),
  11. nn.MaxPool2d(kernel_size=3, stride=2),
  12. nn.BatchNorm2d(int(64*self.ratioInfl)),
  13. nn.Hardtanh(inplace=True),
  14. BinarizeConv2d(int(64*self.ratioInfl), int(192*self.ratioInfl), kernel_size=5, padding=2),
  15. nn.MaxPool2d(kernel_size=3, stride=2),
  16. nn.BatchNorm2d(int(192*self.ratioInfl)),
  17. nn.Hardtanh(inplace=True),
  18. BinarizeConv2d(int(192*self.ratioInfl), int(384*self.ratioInfl), kernel_size=3, padding=1),
  19. nn.BatchNorm2d(int(384*self.ratioInfl)),
  20. nn.Hardtanh(inplace=True),
  21. BinarizeConv2d(int(384*self.ratioInfl), int(256*self.ratioInfl), kernel_size=3, padding=1),
  22. nn.BatchNorm2d(int(256*self.ratioInfl)),
  23. nn.Hardtanh(inplace=True),
  24. BinarizeConv2d(int(256*self.ratioInfl), 256, kernel_size=3, padding=1),
  25. nn.MaxPool2d(kernel_size=3, stride=2),
  26. nn.BatchNorm2d(256),
  27. nn.Hardtanh(inplace=True)
  28. )
  29. self.classifier = nn.Sequential(
  30. BinarizeLinear(256 * 6 * 6, 4096),
  31. nn.BatchNorm1d(4096),
  32. nn.Hardtanh(inplace=True),
  33. #nn.Dropout(0.5),
  34. BinarizeLinear(4096, 4096),
  35. nn.BatchNorm1d(4096),
  36. nn.Hardtanh(inplace=True),
  37. #nn.Dropout(0.5),
  38. BinarizeLinear(4096, num_classes),
  39. nn.BatchNorm1d(1000),
  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. self.regime = {
  51. 0: {'optimizer': 'Adam', 'lr': 5e-3},
  52. 20: {'lr': 1e-3},
  53. 30: {'lr': 5e-4},
  54. 35: {'lr': 1e-4},
  55. 40: {'lr': 1e-5}
  56. }
  57. normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
  58. std=[0.229, 0.224, 0.225])
  59. self.input_transform = {
  60. 'train': transforms.Compose([
  61. transforms.Scale(256),
  62. transforms.RandomCrop(224),
  63. transforms.RandomHorizontalFlip(),
  64. transforms.ToTensor(),
  65. normalize
  66. ]),
  67. 'eval': transforms.Compose([
  68. transforms.Scale(256),
  69. transforms.CenterCrop(224),
  70. transforms.ToTensor(),
  71. normalize
  72. ])
  73. }
  74. def forward(self, x):
  75. x = self.features(x)
  76. x = x.view(-1, 256 * 6 * 6)
  77. x = self.classifier(x)
  78. return x
  79. def alexnet_binary(**kwargs):
  80. num_classes = kwargs.get( 'num_classes', 1000)
  81. return AlexNetOWT_BN(num_classes)