alexnet.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. # copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve.
  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, BatchNorm, Linear, Dropout, ReLU
  19. from paddle.nn import AdaptiveAvgPool2D, MaxPool2D, AvgPool2D
  20. from paddle.nn.initializer import Uniform
  21. import math
  22. from paddlex.ppcls.utils.save_load import load_dygraph_pretrain, load_dygraph_pretrain_from_url
  23. MODEL_URLS = {
  24. "AlexNet":
  25. "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/AlexNet_pretrained.pdparams"
  26. }
  27. __all__ = list(MODEL_URLS.keys())
  28. class ConvPoolLayer(nn.Layer):
  29. def __init__(self,
  30. input_channels,
  31. output_channels,
  32. filter_size,
  33. stride,
  34. padding,
  35. stdv,
  36. groups=1,
  37. act=None,
  38. name=None):
  39. super(ConvPoolLayer, self).__init__()
  40. self.relu = ReLU() if act == "relu" else None
  41. self._conv = Conv2D(
  42. in_channels=input_channels,
  43. out_channels=output_channels,
  44. kernel_size=filter_size,
  45. stride=stride,
  46. padding=padding,
  47. groups=groups,
  48. weight_attr=ParamAttr(
  49. name=name + "_weights", initializer=Uniform(-stdv, stdv)),
  50. bias_attr=ParamAttr(
  51. name=name + "_offset", initializer=Uniform(-stdv, stdv)))
  52. self._pool = MaxPool2D(kernel_size=3, stride=2, padding=0)
  53. def forward(self, inputs):
  54. x = self._conv(inputs)
  55. if self.relu is not None:
  56. x = self.relu(x)
  57. x = self._pool(x)
  58. return x
  59. class AlexNetDY(nn.Layer):
  60. def __init__(self, class_num=1000):
  61. super(AlexNetDY, self).__init__()
  62. stdv = 1.0 / math.sqrt(3 * 11 * 11)
  63. self._conv1 = ConvPoolLayer(
  64. 3, 64, 11, 4, 2, stdv, act="relu", name="conv1")
  65. stdv = 1.0 / math.sqrt(64 * 5 * 5)
  66. self._conv2 = ConvPoolLayer(
  67. 64, 192, 5, 1, 2, stdv, act="relu", name="conv2")
  68. stdv = 1.0 / math.sqrt(192 * 3 * 3)
  69. self._conv3 = Conv2D(
  70. 192,
  71. 384,
  72. 3,
  73. stride=1,
  74. padding=1,
  75. weight_attr=ParamAttr(
  76. name="conv3_weights", initializer=Uniform(-stdv, stdv)),
  77. bias_attr=ParamAttr(
  78. name="conv3_offset", initializer=Uniform(-stdv, stdv)))
  79. stdv = 1.0 / math.sqrt(384 * 3 * 3)
  80. self._conv4 = Conv2D(
  81. 384,
  82. 256,
  83. 3,
  84. stride=1,
  85. padding=1,
  86. weight_attr=ParamAttr(
  87. name="conv4_weights", initializer=Uniform(-stdv, stdv)),
  88. bias_attr=ParamAttr(
  89. name="conv4_offset", initializer=Uniform(-stdv, stdv)))
  90. stdv = 1.0 / math.sqrt(256 * 3 * 3)
  91. self._conv5 = ConvPoolLayer(
  92. 256, 256, 3, 1, 1, stdv, act="relu", name="conv5")
  93. stdv = 1.0 / math.sqrt(256 * 6 * 6)
  94. self._drop1 = Dropout(p=0.5, mode="downscale_in_infer")
  95. self._fc6 = Linear(
  96. in_features=256 * 6 * 6,
  97. out_features=4096,
  98. weight_attr=ParamAttr(
  99. name="fc6_weights", initializer=Uniform(-stdv, stdv)),
  100. bias_attr=ParamAttr(
  101. name="fc6_offset", initializer=Uniform(-stdv, stdv)))
  102. self._drop2 = Dropout(p=0.5, mode="downscale_in_infer")
  103. self._fc7 = Linear(
  104. in_features=4096,
  105. out_features=4096,
  106. weight_attr=ParamAttr(
  107. name="fc7_weights", initializer=Uniform(-stdv, stdv)),
  108. bias_attr=ParamAttr(
  109. name="fc7_offset", initializer=Uniform(-stdv, stdv)))
  110. self._fc8 = Linear(
  111. in_features=4096,
  112. out_features=class_num,
  113. weight_attr=ParamAttr(
  114. name="fc8_weights", initializer=Uniform(-stdv, stdv)),
  115. bias_attr=ParamAttr(
  116. name="fc8_offset", initializer=Uniform(-stdv, stdv)))
  117. def forward(self, inputs):
  118. x = self._conv1(inputs)
  119. x = self._conv2(x)
  120. x = self._conv3(x)
  121. x = F.relu(x)
  122. x = self._conv4(x)
  123. x = F.relu(x)
  124. x = self._conv5(x)
  125. x = paddle.flatten(x, start_axis=1, stop_axis=-1)
  126. x = self._drop1(x)
  127. x = self._fc6(x)
  128. x = F.relu(x)
  129. x = self._drop2(x)
  130. x = self._fc7(x)
  131. x = F.relu(x)
  132. x = self._fc8(x)
  133. return x
  134. def _load_pretrained(pretrained, model, model_url, use_ssld=False):
  135. if pretrained is False:
  136. pass
  137. elif pretrained is True:
  138. load_dygraph_pretrain_from_url(model, model_url, use_ssld=use_ssld)
  139. elif isinstance(pretrained, str):
  140. load_dygraph_pretrain(model, pretrained)
  141. else:
  142. raise RuntimeError(
  143. "pretrained type is not available. Please use `string` or `boolean` type."
  144. )
  145. def AlexNet(pretrained=False, use_ssld=False, **kwargs):
  146. model = AlexNetDY(**kwargs)
  147. _load_pretrained(
  148. pretrained, model, MODEL_URLS["AlexNet"], use_ssld=use_ssld)
  149. return model