cornerModel.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. # Reference : Taken from https://github.com/kuangliu/pytorch-cifar
  2. # License
  3. # MIT License
  4. #
  5. # Copyright (c) 2017 liukuang
  6. #
  7. # Permission is hereby granted, free of charge, to any person obtaining a copy
  8. # of this software and associated documentation files (the "Software"), to deal
  9. # in the Software without restriction, including without limitation the rights
  10. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. # copies of the Software, and to permit persons to whom the Software is
  12. # furnished to do so, subject to the following conditions:
  13. #
  14. # The above copyright notice and this permission notice shall be included in all
  15. # copies or substantial portions of the Software.
  16. #
  17. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. # SOFTWARE.
  24. '''MobileNet in PyTorch.
  25. See the paper "MobileNets: Efficient Convolutional Neural Networks for Mobile Vision Applications"
  26. for more details.
  27. '''
  28. import torch
  29. import torch.nn as nn
  30. import torch.nn.functional as F
  31. class Block(nn.Module):
  32. '''Depthwise conv + Pointwise conv'''
  33. def __init__(self, in_planes, out_planes, stride=1):
  34. super(Block, self).__init__()
  35. self.conv1 = nn.Conv2d(in_planes, in_planes, kernel_size=3, stride=stride, padding=1, groups=in_planes, bias=False)
  36. self.bn1 = nn.BatchNorm2d(in_planes)
  37. self.conv2 = nn.Conv2d(in_planes, out_planes, kernel_size=1, stride=1, padding=0, bias=False)
  38. self.bn2 = nn.BatchNorm2d(out_planes)
  39. def forward(self, x):
  40. out = F.relu(self.bn1(self.conv1(x)))
  41. out = F.relu(self.bn2(self.conv2(out)))
  42. return out
  43. class MobileNet(nn.Module):
  44. # (128,2) means conv planes=128, conv stride=2, by default conv stride=1
  45. cfg = [64, (128,2), 128, (256,2), 256, (512,2), 512, 512, 512, 512, 512, (1024,2), 1024]
  46. def __init__(self, num_classes=10):
  47. super(MobileNet, self).__init__()
  48. self.conv1 = nn.Conv2d(3, 32, kernel_size=3, stride=1, padding=1, bias=False)
  49. self.bn1 = nn.BatchNorm2d(32)
  50. self.layers = self._make_layers(in_planes=32)
  51. self.linear = nn.Linear(1024, num_classes)
  52. def _make_layers(self, in_planes):
  53. layers = []
  54. for x in self.cfg:
  55. out_planes = x if isinstance(x, int) else x[0]
  56. stride = 1 if isinstance(x, int) else x[1]
  57. layers.append(Block(in_planes, out_planes, stride))
  58. in_planes = out_planes
  59. return nn.Sequential(*layers)
  60. def forward(self, x, pretrain=False):
  61. out = F.relu(self.bn1(self.conv1(x)))
  62. out = self.layers(out)
  63. out = F.avg_pool2d(out, 2)
  64. out = out.view(out.size(0), -1)
  65. out = self.linear(out)
  66. return out