| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- import torch
- import torch.nn as nn
- try:
- from .modules import ConvModule, CSPBlock
- except:
- from modules import ConvModule, CSPBlock
-
- in1k_pretrained_urls = {
- "cspdarknet53": "https://github.com/yjh0410/image_classification_pytorch/releases/download/weight/cspdarknet53_silu.pth",
- }
- # --------------------- Yolov4 backbone (CSPDarkNet-53 with SiLU) -----------------------
- class Yolov4Backbone(nn.Module):
- def __init__(self, use_pretrained: bool = False):
- super(Yolov4Backbone, self).__init__()
- self.feat_dims = [256, 512, 1024]
- self.use_pretrained = use_pretrained
- # P1
- self.layer_1 = nn.Sequential(
- ConvModule(3, 32, kernel_size=3),
- ConvModule(32, 64, kernel_size=3, stride=2),
- CSPBlock(64, 64, expand_ratio=0.5, num_blocks=1, shortcut=True)
- )
- # P2
- self.layer_2 = nn.Sequential(
- ConvModule(64, 128, kernel_size=3, stride=2),
- CSPBlock(128, 128, expand_ratio=0.5, num_blocks=2, shortcut=True)
- )
- # P3
- self.layer_3 = nn.Sequential(
- ConvModule(128, 256, kernel_size=3, stride=2),
- CSPBlock(256, 256, expand_ratio=0.5, num_blocks=8, shortcut=True)
- )
- # P4
- self.layer_4 = nn.Sequential(
- ConvModule(256, 512, kernel_size=3, stride=2),
- CSPBlock(512, 512, expand_ratio=0.5, num_blocks=8, shortcut=True)
- )
- # P5
- self.layer_5 = nn.Sequential(
- ConvModule(512, 1024, kernel_size=3, stride=2),
- CSPBlock(1024, 1024, expand_ratio=0.5, num_blocks=4, shortcut=True)
- )
- # Initialize all layers
- self.init_weights()
-
- def init_weights(self):
- """Initialize the parameters."""
- for m in self.modules():
- if isinstance(m, torch.nn.Conv2d):
- m.reset_parameters()
- # Load imagenet pretrained weight
- if self.use_pretrained:
- self.load_pretrained()
- def load_pretrained(self):
- url = in1k_pretrained_urls["cspdarknet53"]
- if url is not None:
- print('Loading backbone pretrained weight from : {}'.format(url))
- # checkpoint state dict
- checkpoint = torch.hub.load_state_dict_from_url(
- url=url, map_location="cpu", check_hash=True)
- checkpoint_state_dict = checkpoint.pop("model")
- # model state dict
- model_state_dict = self.state_dict()
- # check
- for k in list(checkpoint_state_dict.keys()):
- if k in model_state_dict:
- shape_model = tuple(model_state_dict[k].shape)
- shape_checkpoint = tuple(checkpoint_state_dict[k].shape)
- if shape_model != shape_checkpoint:
- checkpoint_state_dict.pop(k)
- else:
- checkpoint_state_dict.pop(k)
- print('Unused key: ', k)
- # load the weight
- self.load_state_dict(checkpoint_state_dict)
- else:
- print('No pretrained weight for model scale: {}.'.format(self.model_scale))
- def forward(self, x):
- c1 = self.layer_1(x)
- c2 = self.layer_2(c1)
- c3 = self.layer_3(c2)
- c4 = self.layer_4(c3)
- c5 = self.layer_5(c4)
- outputs = [c3, c4, c5]
- return outputs
- if __name__=='__main__':
- from thop import profile
- # Build backbone
- model = Yolov4Backbone(use_pretrained=True)
- # Randomly generate a input data
- x = torch.randn(2, 3, 640, 640)
- # Inference
- outputs = model(x)
- print(' - the shape of input : ', x.shape)
- for out in outputs:
- print(' - the shape of output : ', out.shape)
- x = torch.randn(1, 3, 640, 640)
- flops, params = profile(model, inputs=(x, ), verbose=False)
- print('============== FLOPs & Params ================')
- print(' - FLOPs : {:.2f} G'.format(flops / 1e9 * 2))
- print(' - Params : {:.2f} M'.format(params / 1e6))
|