Alexnet.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import torch
  2. import torch.nn as nn
  3. import torch.nn.functional as F
  4. class Alexnet(nn.Module):
  5. def __init__(self, input_channels, output_num, input_size):
  6. super().__init__()
  7. self.features = nn.Sequential(
  8. nn.Conv2d(in_channels=input_channels, out_channels=64, kernel_size=3, stride=2, padding=1),
  9. nn.BatchNorm2d(64), # 批量归一化层
  10. nn.MaxPool2d(kernel_size=2),
  11. nn.ReLU(inplace=True),
  12. nn.Conv2d(in_channels=64, out_channels=192, kernel_size=3, padding=1),
  13. nn.BatchNorm2d(192), # 批量归一化层
  14. nn.MaxPool2d(kernel_size=2),
  15. nn.ReLU(inplace=True),
  16. nn.Conv2d(in_channels=192, out_channels=384, kernel_size=3, padding=1),
  17. nn.BatchNorm2d(384), # 批量归一化层
  18. nn.ReLU(inplace=True),
  19. nn.Conv2d(in_channels=384, out_channels=256, kernel_size=3, padding=1),
  20. nn.BatchNorm2d(256), # 批量归一化层
  21. nn.ReLU(inplace=True),
  22. nn.Conv2d(in_channels=256, out_channels=256, kernel_size=3, padding=1),
  23. nn.BatchNorm2d(256), # 批量归一化层
  24. nn.MaxPool2d(kernel_size=2),
  25. nn.ReLU(inplace=True),
  26. )
  27. self.input_size = input_size
  28. self._init_classifier(output_num)
  29. def _init_classifier(self, output_num):
  30. with torch.no_grad():
  31. # Forward a dummy input through the feature extractor part of the network
  32. dummy_input = torch.zeros(1, 3, self.input_size, self.input_size)
  33. features_size = self.features(dummy_input).numel()
  34. self.classifier = nn.Sequential(
  35. nn.Dropout(0.5),
  36. nn.Linear(features_size, 1000),
  37. nn.ReLU(inplace=True),
  38. nn.Dropout(0.5),
  39. nn.Linear(1000, 256),
  40. nn.ReLU(inplace=True),
  41. nn.Linear(256, output_num)
  42. )
  43. def forward(self, x):
  44. x = self.features(x)
  45. x = x.view(x.size(0), -1)
  46. x = self.classifier(x)
  47. return x
  48. if __name__ == '__main__':
  49. import argparse
  50. parser = argparse.ArgumentParser(description='AlexNet Implementation')
  51. parser.add_argument('--input_channels', default=3, type=int)
  52. parser.add_argument('--output_num', default=10, type=int)
  53. parser.add_argument('--input_size', default=32, type=int)
  54. args = parser.parse_args()
  55. model = Alexnet(args.input_channels, args.output_num, args.input_size)
  56. tensor = torch.rand(1, args.input_channels, args.input_size, args.input_size)
  57. pred = model(tensor)
  58. print(model)
  59. print("Predictions shape:", pred.shape)