alexnet.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. # Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import paddle
  15. from paddle import ParamAttr
  16. import paddle.nn as nn
  17. import paddle.nn.functional as F
  18. from paddle.nn import Conv2D, Linear, Dropout, ReLU
  19. from paddle.nn import MaxPool2D
  20. from paddle.nn.initializer import Uniform
  21. import math
  22. __all__ = ["AlexNet"]
  23. class ConvPoolLayer(nn.Layer):
  24. def __init__(self,
  25. input_channels,
  26. output_channels,
  27. filter_size,
  28. stride,
  29. padding,
  30. stdv,
  31. groups=1,
  32. act=None,
  33. name=None):
  34. super(ConvPoolLayer, self).__init__()
  35. self.relu = ReLU() if act == "relu" else None
  36. self._conv = Conv2D(
  37. in_channels=input_channels,
  38. out_channels=output_channels,
  39. kernel_size=filter_size,
  40. stride=stride,
  41. padding=padding,
  42. groups=groups,
  43. weight_attr=ParamAttr(
  44. name=name + "_weights", initializer=Uniform(-stdv, stdv)),
  45. bias_attr=ParamAttr(
  46. name=name + "_offset", initializer=Uniform(-stdv, stdv)))
  47. self._pool = MaxPool2D(kernel_size=3, stride=2, padding=0)
  48. def forward(self, inputs):
  49. x = self._conv(inputs)
  50. if self.relu is not None:
  51. x = self.relu(x)
  52. x = self._pool(x)
  53. return x
  54. class AlexNetDY(nn.Layer):
  55. def __init__(self, class_dim=1000):
  56. super(AlexNetDY, self).__init__()
  57. stdv = 1.0 / math.sqrt(3 * 11 * 11)
  58. self._conv1 = ConvPoolLayer(
  59. 3, 64, 11, 4, 2, stdv, act="relu", name="conv1")
  60. stdv = 1.0 / math.sqrt(64 * 5 * 5)
  61. self._conv2 = ConvPoolLayer(
  62. 64, 192, 5, 1, 2, stdv, act="relu", name="conv2")
  63. stdv = 1.0 / math.sqrt(192 * 3 * 3)
  64. self._conv3 = Conv2D(
  65. 192,
  66. 384,
  67. 3,
  68. stride=1,
  69. padding=1,
  70. weight_attr=ParamAttr(
  71. name="conv3_weights", initializer=Uniform(-stdv, stdv)),
  72. bias_attr=ParamAttr(
  73. name="conv3_offset", initializer=Uniform(-stdv, stdv)))
  74. stdv = 1.0 / math.sqrt(384 * 3 * 3)
  75. self._conv4 = Conv2D(
  76. 384,
  77. 256,
  78. 3,
  79. stride=1,
  80. padding=1,
  81. weight_attr=ParamAttr(
  82. name="conv4_weights", initializer=Uniform(-stdv, stdv)),
  83. bias_attr=ParamAttr(
  84. name="conv4_offset", initializer=Uniform(-stdv, stdv)))
  85. stdv = 1.0 / math.sqrt(256 * 3 * 3)
  86. self._conv5 = ConvPoolLayer(
  87. 256, 256, 3, 1, 1, stdv, act="relu", name="conv5")
  88. stdv = 1.0 / math.sqrt(256 * 6 * 6)
  89. self._drop1 = Dropout(p=0.5, mode="downscale_in_infer")
  90. self._fc6 = Linear(
  91. in_features=256 * 6 * 6,
  92. out_features=4096,
  93. weight_attr=ParamAttr(
  94. name="fc6_weights", initializer=Uniform(-stdv, stdv)),
  95. bias_attr=ParamAttr(
  96. name="fc6_offset", initializer=Uniform(-stdv, stdv)))
  97. self._drop2 = Dropout(p=0.5, mode="downscale_in_infer")
  98. self._fc7 = Linear(
  99. in_features=4096,
  100. out_features=4096,
  101. weight_attr=ParamAttr(
  102. name="fc7_weights", initializer=Uniform(-stdv, stdv)),
  103. bias_attr=ParamAttr(
  104. name="fc7_offset", initializer=Uniform(-stdv, stdv)))
  105. self._fc8 = Linear(
  106. in_features=4096,
  107. out_features=class_dim,
  108. weight_attr=ParamAttr(
  109. name="fc8_weights", initializer=Uniform(-stdv, stdv)),
  110. bias_attr=ParamAttr(
  111. name="fc8_offset", initializer=Uniform(-stdv, stdv)))
  112. def forward(self, inputs):
  113. x = self._conv1(inputs)
  114. x = self._conv2(x)
  115. x = self._conv3(x)
  116. x = F.relu(x)
  117. x = self._conv4(x)
  118. x = F.relu(x)
  119. x = self._conv5(x)
  120. x = paddle.flatten(x, start_axis=1, stop_axis=-1)
  121. x = self._drop1(x)
  122. x = self._fc6(x)
  123. x = F.relu(x)
  124. x = self._drop2(x)
  125. x = self._fc7(x)
  126. x = F.relu(x)
  127. x = self._fc8(x)
  128. return x
  129. def AlexNet(**args):
  130. model = AlexNetDY(**args)
  131. return model