darknet19.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import torch
  2. import torch.nn as nn
  3. # ImageNet pretrained weight
  4. pretrained_urls = {
  5. "darknet19": "https://github.com/yjh0410/image_classification_pytorch/releases/download/weight/darknet19.pth",
  6. }
  7. # --------------------- Basic Module -----------------------
  8. class ConvModule(nn.Module):
  9. def __init__(self,
  10. in_channels: int,
  11. out_channels: int,
  12. kernel_size: int,
  13. padding: int = 0,
  14. stride: int = 1,
  15. dilation: int = 1,
  16. ):
  17. super(ConvModule, self).__init__()
  18. self.convs = nn.Sequential(
  19. nn.Conv2d(in_channels, out_channels, kernel_size, padding=padding, stride=stride, dilation=dilation),
  20. nn.BatchNorm2d(out_channels),
  21. nn.LeakyReLU(0.1, inplace=True)
  22. )
  23. def forward(self, x):
  24. return self.convs(x)
  25. class DarkNet19(nn.Module):
  26. def __init__(self, use_pretrained=False):
  27. super(DarkNet19, self).__init__()
  28. # output : stride = 2, c = 32
  29. self.conv_1 = nn.Sequential(
  30. ConvModule(3, 32, kernel_size=3, padding=1),
  31. nn.MaxPool2d(kernel_size=(2, 2), stride=2),
  32. )
  33. # output : stride = 4, c = 64
  34. self.conv_2 = nn.Sequential(
  35. ConvModule(32, 64, kernel_size=3, padding=1),
  36. nn.MaxPool2d(kernel_size=(2, 2), stride=2)
  37. )
  38. # output : stride = 8, c = 128
  39. self.conv_3 = nn.Sequential(
  40. ConvModule(64, 128, kernel_size=3, padding=1),
  41. ConvModule(128, 64, 1),
  42. ConvModule(64, 128, kernel_size=3, padding=1),
  43. nn.MaxPool2d(kernel_size=(2, 2), stride=2)
  44. )
  45. # output : stride = 8, c = 256
  46. self.conv_4 = nn.Sequential(
  47. ConvModule(128, 256, kernel_size=3, padding=1),
  48. ConvModule(256, 128, 1),
  49. ConvModule(128, 256, kernel_size=3, padding=1),
  50. )
  51. # output : stride = 16, c = 512
  52. self.maxpool_4 = nn.MaxPool2d((2, 2), 2)
  53. self.conv_5 = nn.Sequential(
  54. ConvModule(256, 512, kernel_size=3, padding=1),
  55. ConvModule(512, 256, 1),
  56. ConvModule(256, 512, kernel_size=3, padding=1),
  57. ConvModule(512, 256, 1),
  58. ConvModule(256, 512, kernel_size=3, padding=1),
  59. )
  60. # output : stride = 32, c = 1024
  61. self.maxpool_5 = nn.MaxPool2d((2, 2), 2)
  62. self.conv_6 = nn.Sequential(
  63. ConvModule(512, 1024, kernel_size=3, padding=1),
  64. ConvModule(1024, 512, 1),
  65. ConvModule(512, 1024, kernel_size=3, padding=1),
  66. ConvModule(1024, 512, 1),
  67. ConvModule(512, 1024, kernel_size=3, padding=1)
  68. )
  69. if use_pretrained:
  70. self.load_pretrained()
  71. def load_pretrained(self):
  72. url = pretrained_urls["darknet19"]
  73. if url is not None:
  74. print(' - Loading backbone pretrained weight from : {}'.format(url))
  75. # checkpoint state dict
  76. checkpoint_state_dict = torch.hub.load_state_dict_from_url(
  77. url=url, map_location="cpu", check_hash=True)
  78. # model state dict
  79. model_state_dict = self.state_dict()
  80. # check
  81. for k in list(checkpoint_state_dict.keys()):
  82. if k in model_state_dict:
  83. shape_model = tuple(model_state_dict[k].shape)
  84. shape_checkpoint = tuple(checkpoint_state_dict[k].shape)
  85. if shape_model != shape_checkpoint:
  86. checkpoint_state_dict.pop(k)
  87. else:
  88. checkpoint_state_dict.pop(k)
  89. print('Unused key: ', k)
  90. # load the weight
  91. self.load_state_dict(checkpoint_state_dict)
  92. else:
  93. print(' - No pretrained weight for darknet-19.')
  94. def forward(self, x):
  95. c1 = self.conv_1(x) # c1
  96. c2 = self.conv_2(c1) # c2
  97. c3 = self.conv_3(c2) # c3
  98. c3 = self.conv_4(c3) # c3
  99. c4 = self.conv_5(self.maxpool_4(c3)) # c4
  100. c5 = self.conv_6(self.maxpool_5(c4)) # c5
  101. return c5
  102. if __name__ == '__main__':
  103. from thop import profile
  104. # Build model
  105. model = DarkNet19(use_pretrained=True)
  106. # Randomly generate a input data
  107. x = torch.randn(2, 3, 640, 640)
  108. # Inference
  109. output = model(x)
  110. print(' - the shape of input : ', x.shape)
  111. print(' - the shape of output : ', output.shape)
  112. x = torch.randn(1, 3, 640, 640)
  113. flops, params = profile(model, inputs=(x, ), verbose=False)
  114. print('============== FLOPs & Params ================')
  115. print(' - FLOPs : {:.2f} G'.format(flops / 1e9 * 2))
  116. print(' - Params : {:.2f} M'.format(params / 1e6))