googlenet.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import torch
  2. import torch.nn as nn
  3. import torch.nn.functional as F
  4. # 定义GoogLeNet模型
  5. class InceptionModule(nn.Module):
  6. def __init__(self, in_channels, out1, out2_in, out2, out3_in, out3, out4):
  7. super(InceptionModule, self).__init__()
  8. self.branch1 = nn.Sequential(
  9. nn.Conv2d(in_channels, out1, kernel_size=1),
  10. )
  11. self.branch2 = nn.Sequential(
  12. nn.Conv2d(in_channels, out2_in, kernel_size=1),
  13. nn.Conv2d(out2_in, out2, kernel_size=3, padding=1),
  14. )
  15. self.branch3 = nn.Sequential(
  16. nn.Conv2d(in_channels, out3_in, kernel_size=1),
  17. nn.Conv2d(out3_in, out3, kernel_size=5, padding=2),
  18. )
  19. self.branch4 = nn.Sequential(
  20. nn.MaxPool2d(kernel_size=3, stride=1, padding=1),
  21. nn.Conv2d(in_channels, out4, kernel_size=1),
  22. )
  23. def forward(self, x):
  24. out1 = self.branch1(x)
  25. out2 = self.branch2(x)
  26. out3 = self.branch3(x)
  27. out4 = self.branch4(x)
  28. return torch.cat([out1, out2, out3, out4], 1)
  29. class GoogLeNet(nn.Module):
  30. def __init__(self, num_classes=10):
  31. super(GoogLeNet, self).__init__()
  32. self.conv1 = nn.Sequential(
  33. nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3),
  34. nn.ReLU(inplace=True),
  35. nn.MaxPool2d(kernel_size=3, stride=2, padding=1),
  36. )
  37. self.conv2 = nn.Sequential(
  38. nn.Conv2d(64, 64, kernel_size=1),
  39. nn.ReLU(inplace=True),
  40. nn.Conv2d(64, 192, kernel_size=3, padding=1),
  41. nn.ReLU(inplace=True),
  42. nn.MaxPool2d(kernel_size=3, stride=2, padding=1),
  43. )
  44. self.inception3a = InceptionModule(192, 64, 96, 128, 16, 32, 32)
  45. self.inception3b = InceptionModule(256, 128, 128, 192, 32, 96, 64)
  46. self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
  47. self.inception4a = InceptionModule(480, 192, 96, 208, 16, 48, 64)
  48. self.inception4b = InceptionModule(512, 160, 112, 224, 24, 64, 64)
  49. self.inception4c = InceptionModule(512, 128, 128, 256, 24, 64, 64)
  50. self.inception4d = InceptionModule(512, 112, 144, 288, 32, 64, 64)
  51. self.inception4e = InceptionModule(528, 256, 160, 320, 32, 128, 128)
  52. self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
  53. self.inception5a = InceptionModule(832, 256, 160, 320, 32, 128, 128)
  54. self.inception5b = InceptionModule(832, 384, 192, 384, 48, 128, 128)
  55. self.avgpool = nn.AdaptiveAvgPool2d((1, 1))
  56. self.dropout = nn.Dropout(0.4)
  57. self.fc = nn.Linear(1024, num_classes)
  58. def forward(self, x):
  59. x = self.conv1(x)
  60. x = self.conv2(x)
  61. x = self.inception3a(x)
  62. x = self.inception3b(x)
  63. x = self.maxpool(x)
  64. x = self.inception4a(x)
  65. x = self.inception4b(x)
  66. x = self.inception4c(x)
  67. x = self.inception4d(x)
  68. x = self.inception4e(x)
  69. x = self.maxpool(x)
  70. x = self.inception5a(x)
  71. x = self.inception5b(x)
  72. x = self.avgpool(x)
  73. x = x.view(x.size(0), -1)
  74. x = self.dropout(x)
  75. x = self.fc(x)
  76. return x