You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

blocks.py 3.4 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. # Copyright 2020 Huawei Technologies Co., Ltd
  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. # ============================================================================
  15. import mindspore.nn as nn
  16. import mindspore.ops.operations as P
  17. class ShuffleV2Block(nn.Cell):
  18. def __init__(self, inp, oup, mid_channels, *, ksize, stride):
  19. super(ShuffleV2Block, self).__init__()
  20. self.stride = stride
  21. ##assert stride in [1, 2]
  22. self.mid_channels = mid_channels
  23. self.ksize = ksize
  24. pad = ksize // 2
  25. self.pad = pad
  26. self.inp = inp
  27. outputs = oup - inp
  28. branch_main = [
  29. # pw
  30. nn.Conv2d(in_channels=inp, out_channels=mid_channels, kernel_size=1, stride=1,
  31. pad_mode='pad', padding=0, has_bias=False),
  32. nn.BatchNorm2d(num_features=mid_channels, momentum=0.9),
  33. nn.ReLU(),
  34. # dw
  35. nn.Conv2d(in_channels=mid_channels, out_channels=mid_channels, kernel_size=ksize, stride=stride,
  36. pad_mode='pad', padding=pad, group=mid_channels, has_bias=False),
  37. nn.BatchNorm2d(num_features=mid_channels, momentum=0.9),
  38. # pw-linear
  39. nn.Conv2d(in_channels=mid_channels, out_channels=outputs, kernel_size=1, stride=1,
  40. pad_mode='pad', padding=0, has_bias=False),
  41. nn.BatchNorm2d(num_features=outputs, momentum=0.9),
  42. nn.ReLU(),
  43. ]
  44. self.branch_main = nn.SequentialCell(branch_main)
  45. if stride == 2:
  46. branch_proj = [
  47. # dw
  48. nn.Conv2d(in_channels=inp, out_channels=inp, kernel_size=ksize, stride=stride,
  49. pad_mode='pad', padding=pad, group=inp, has_bias=False),
  50. nn.BatchNorm2d(num_features=inp, momentum=0.9),
  51. # pw-linear
  52. nn.Conv2d(in_channels=inp, out_channels=inp, kernel_size=1, stride=1,
  53. pad_mode='pad', padding=0, has_bias=False),
  54. nn.BatchNorm2d(num_features=inp, momentum=0.9),
  55. nn.ReLU(),
  56. ]
  57. self.branch_proj = nn.SequentialCell(branch_proj)
  58. else:
  59. self.branch_proj = None
  60. def construct(self, old_x):
  61. if self.stride == 1:
  62. x_proj, x = self.channel_shuffle(old_x)
  63. return P.Concat(1)((x_proj, self.branch_main(x)))
  64. if self.stride == 2:
  65. x_proj = old_x
  66. x = old_x
  67. return P.Concat(1)((self.branch_proj(x_proj), self.branch_main(x)))
  68. return None
  69. def channel_shuffle(self, x):
  70. batchsize, num_channels, height, width = P.Shape()(x)
  71. ##assert (num_channels % 4 == 0)
  72. x = P.Reshape()(x, (batchsize * num_channels // 2, 2, height * width,))
  73. x = P.Transpose()(x, (1, 0, 2,))
  74. x = P.Reshape()(x, (2, -1, num_channels // 2, height, width,))
  75. return x[0], x[1]