models.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. # torch
  2. import torch
  3. import torch.nn as nn
  4. import torch.functional as F
  5. # other
  6. import numpy as np
  7. # internal
  8. from utils.helpers import *
  9. cfg = {
  10. 'VGG11': [64, 'M', 128, 'M', 256, 256, 'M', 512, 512, 'M', 512, 512, 'M'],
  11. 'VGG13': [64, 64, 'M', 128, 128, 'M', 256, 256, 'M', 512, 512, 'M', 512, 512, 'M'],
  12. 'VGG16': [64, 64, 'M', 128, 128, 'M', 256, 256, 256, 'M', 512, 512, 512, 'M', 512, 512, 512, 'M'],
  13. 'VGG19': [64, 64, 'M', 128, 128, 'M', 256, 256, 256, 256, 'M', 512, 512, 512, 512, 'M', 512, 512, 512, 512, 'M'],
  14. }
  15. class VGG(nn.Module):
  16. def __init__(self, vgg_name='VGG19'):
  17. super(VGG, self).__init__()
  18. self.features = self._make_layers(cfg[vgg_name])
  19. self.classifier = self._make_classifier()
  20. def forward(self, x):
  21. x = self.features(x)
  22. x = x.view(x.size(0), -1)
  23. x = self.classifier(x)
  24. return x
  25. def _make_layers(self, cfg):
  26. layers = []
  27. in_channels = 3
  28. for x in cfg:
  29. if x == 'M':
  30. layers += [nn.MaxPool2d(kernel_size=2, stride=2)]
  31. else:
  32. layers += [nn.Conv2d(in_channels, x, kernel_size=3, padding=1),
  33. nn.BatchNorm2d(x),
  34. nn.ReLU(inplace=True)]
  35. in_channels = x
  36. return nn.Sequential(*layers)
  37. def _make_classifier(self):
  38. layers = []
  39. layers += [nn.Linear(512,512),
  40. nn.BatchNorm1d(512),
  41. nn.ReLU(True),
  42. nn.Linear(512,10)]
  43. return nn.Sequential(*layers)
  44. class VGG_eval(nn.Module):
  45. '''
  46. This version is used for the attack
  47. '''
  48. def __init__(self, mu, sigma, vgg_name='VGG19'):
  49. super(VGG_eval, self).__init__()
  50. self.features = self._make_layers(cfg[vgg_name])
  51. self.classifier = self._make_classifier()
  52. self.sigma = torch.tensor(sigma)
  53. self.mu = torch.tensor(mu)
  54. def forward(self, x):
  55. x = (x*self.sigma)+self.mu
  56. x = self.features(x)
  57. x = x.view(x.size(0), -1)
  58. x = self.classifier(x)
  59. return x
  60. def _make_layers(self, cfg):
  61. layers = []
  62. in_channels = 3
  63. for x in cfg:
  64. if x == 'M':
  65. layers += [nn.MaxPool2d(kernel_size=2, stride=2)]
  66. else:
  67. layers += [nn.Conv2d(in_channels, x, kernel_size=3, padding=1),
  68. nn.BatchNorm2d(x),
  69. nn.ReLU(inplace=True)]
  70. in_channels = x
  71. return nn.Sequential(*layers)
  72. def _make_classifier(self):
  73. layers = []
  74. layers += [nn.Linear(512,512),
  75. nn.BatchNorm1d(512),
  76. nn.ReLU(True),
  77. nn.Linear(512,10)]
  78. return nn.Sequential(*layers)
  79. class rob_VGG(nn.Module):
  80. '''
  81. Robust version of VGG net above.
  82. Inputs:
  83. k - truncation parameter
  84. '''
  85. def __init__(self, k, vgg_name='VGG19'):
  86. super(rob_VGG, self).__init__()
  87. self.k = k
  88. self.features = self._make_layers(cfg[vgg_name])
  89. self.classifier = self._make_classifier()
  90. def forward(self, x):
  91. # apply truncation first
  92. x = trunc(x,self.k)
  93. x = self.features(x)
  94. x = x.view(x.size(0), -1)
  95. x = self.classifier(x)
  96. return x
  97. def _make_layers(self, cfg):
  98. layers = []
  99. in_channels = 3
  100. for x in cfg:
  101. if x == 'M':
  102. layers += [nn.MaxPool2d(kernel_size=2, stride=2)]
  103. else:
  104. layers += [nn.Conv2d(in_channels, x, kernel_size=3, padding=1),
  105. nn.BatchNorm2d(x),
  106. nn.ReLU(inplace=True)]
  107. in_channels = x
  108. return nn.Sequential(*layers)
  109. def _make_classifier(self):
  110. layers = []
  111. layers += [nn.Linear(512,512),
  112. nn.BatchNorm1d(512),
  113. nn.ReLU(True),
  114. nn.Linear(512,10)]
  115. return nn.Sequential(*layers)
  116. class rob_VGG_eval(nn.Module):
  117. '''
  118. This version is used for the attack
  119. '''
  120. def __init__(self, k, mu, sigma, vgg_name='VGG19'):
  121. super(rob_VGG_eval, self).__init__()
  122. self.k = k
  123. self.features = self._make_layers(cfg[vgg_name])
  124. self.classifier = self._make_classifier()
  125. self.sigma = torch.tensor(sigma)
  126. self.mu = torch.tensor(mu)
  127. def forward(self, x):
  128. x = (x*self.sigma)+self.mu
  129. x = trunc(x,self.k)
  130. x = self.features(x)
  131. x = x.view(x.size(0), -1)
  132. x = self.classifier(x)
  133. return x
  134. def _make_layers(self, cfg):
  135. layers = []
  136. in_channels = 3
  137. for x in cfg:
  138. if x == 'M':
  139. layers += [nn.MaxPool2d(kernel_size=2, stride=2)]
  140. else:
  141. layers += [nn.Conv2d(in_channels, x, kernel_size=3, padding=1),
  142. nn.BatchNorm2d(x),
  143. nn.ReLU(inplace=True)]
  144. in_channels = x
  145. return nn.Sequential(*layers)
  146. def _make_classifier(self):
  147. layers = []
  148. layers += [nn.Linear(512,512),
  149. nn.BatchNorm1d(512),
  150. nn.ReLU(True),
  151. nn.Linear(512,10)]
  152. return nn.Sequential(*layers)