alexnet.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import paddle
  2. from paddle import ParamAttr
  3. import paddle.nn as nn
  4. import paddle.nn.functional as F
  5. from paddle.nn import Conv2D, BatchNorm, Linear, Dropout, ReLU
  6. from paddle.nn import AdaptiveAvgPool2D, MaxPool2D, AvgPool2D
  7. from paddle.nn.initializer import Uniform
  8. import math
  9. __all__ = ["AlexNet"]
  10. class ConvPoolLayer(nn.Layer):
  11. def __init__(self,
  12. input_channels,
  13. output_channels,
  14. filter_size,
  15. stride,
  16. padding,
  17. stdv,
  18. groups=1,
  19. act=None,
  20. name=None):
  21. super(ConvPoolLayer, self).__init__()
  22. self.relu = ReLU() if act == "relu" else None
  23. self._conv = Conv2D(
  24. in_channels=input_channels,
  25. out_channels=output_channels,
  26. kernel_size=filter_size,
  27. stride=stride,
  28. padding=padding,
  29. groups=groups,
  30. weight_attr=ParamAttr(
  31. name=name + "_weights", initializer=Uniform(-stdv, stdv)),
  32. bias_attr=ParamAttr(
  33. name=name + "_offset", initializer=Uniform(-stdv, stdv)))
  34. self._pool = MaxPool2D(kernel_size=3, stride=2, padding=0)
  35. def forward(self, inputs):
  36. x = self._conv(inputs)
  37. if self.relu is not None:
  38. x = self.relu(x)
  39. x = self._pool(x)
  40. return x
  41. class AlexNetDY(nn.Layer):
  42. def __init__(self, class_dim=1000):
  43. super(AlexNetDY, self).__init__()
  44. stdv = 1.0 / math.sqrt(3 * 11 * 11)
  45. self._conv1 = ConvPoolLayer(
  46. 3, 64, 11, 4, 2, stdv, act="relu", name="conv1")
  47. stdv = 1.0 / math.sqrt(64 * 5 * 5)
  48. self._conv2 = ConvPoolLayer(
  49. 64, 192, 5, 1, 2, stdv, act="relu", name="conv2")
  50. stdv = 1.0 / math.sqrt(192 * 3 * 3)
  51. self._conv3 = Conv2D(
  52. 192,
  53. 384,
  54. 3,
  55. stride=1,
  56. padding=1,
  57. weight_attr=ParamAttr(
  58. name="conv3_weights", initializer=Uniform(-stdv, stdv)),
  59. bias_attr=ParamAttr(
  60. name="conv3_offset", initializer=Uniform(-stdv, stdv)))
  61. stdv = 1.0 / math.sqrt(384 * 3 * 3)
  62. self._conv4 = Conv2D(
  63. 384,
  64. 256,
  65. 3,
  66. stride=1,
  67. padding=1,
  68. weight_attr=ParamAttr(
  69. name="conv4_weights", initializer=Uniform(-stdv, stdv)),
  70. bias_attr=ParamAttr(
  71. name="conv4_offset", initializer=Uniform(-stdv, stdv)))
  72. stdv = 1.0 / math.sqrt(256 * 3 * 3)
  73. self._conv5 = ConvPoolLayer(
  74. 256, 256, 3, 1, 1, stdv, act="relu", name="conv5")
  75. stdv = 1.0 / math.sqrt(256 * 6 * 6)
  76. self._drop1 = Dropout(p=0.5, mode="downscale_in_infer")
  77. self._fc6 = Linear(
  78. in_features=256 * 6 * 6,
  79. out_features=4096,
  80. weight_attr=ParamAttr(
  81. name="fc6_weights", initializer=Uniform(-stdv, stdv)),
  82. bias_attr=ParamAttr(
  83. name="fc6_offset", initializer=Uniform(-stdv, stdv)))
  84. self._drop2 = Dropout(p=0.5, mode="downscale_in_infer")
  85. self._fc7 = Linear(
  86. in_features=4096,
  87. out_features=4096,
  88. weight_attr=ParamAttr(
  89. name="fc7_weights", initializer=Uniform(-stdv, stdv)),
  90. bias_attr=ParamAttr(
  91. name="fc7_offset", initializer=Uniform(-stdv, stdv)))
  92. self._fc8 = Linear(
  93. in_features=4096,
  94. out_features=class_dim,
  95. weight_attr=ParamAttr(
  96. name="fc8_weights", initializer=Uniform(-stdv, stdv)),
  97. bias_attr=ParamAttr(
  98. name="fc8_offset", initializer=Uniform(-stdv, stdv)))
  99. def forward(self, inputs):
  100. x = self._conv1(inputs)
  101. x = self._conv2(x)
  102. x = self._conv3(x)
  103. x = F.relu(x)
  104. x = self._conv4(x)
  105. x = F.relu(x)
  106. x = self._conv5(x)
  107. x = paddle.flatten(x, start_axis=1, stop_axis=-1)
  108. x = self._drop1(x)
  109. x = self._fc6(x)
  110. x = F.relu(x)
  111. x = self._drop2(x)
  112. x = self._fc7(x)
  113. x = F.relu(x)
  114. x = self._fc8(x)
  115. return x
  116. def AlexNet(**args):
  117. model = AlexNetDY(**args)
  118. return model