| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- import torch
- import torch.nn as nn
- try:
- from .yolov8_basic import Conv
- except:
- from yolov8_basic import Conv
- # Spatial Pyramid Pooling - Fast (SPPF) layer for YOLOv5 by Glenn Jocher
- class SPPF(nn.Module):
- def __init__(self, in_dim, out_dim, expand_ratio=0.5, pooling_size=5, act_type='', norm_type=''):
- super().__init__()
- inter_dim = int(in_dim * expand_ratio)
- self.out_dim = out_dim
- self.cv1 = Conv(in_dim, inter_dim, k=1, act_type=act_type, norm_type=norm_type)
- self.cv2 = Conv(inter_dim * 4, out_dim, k=1, act_type=act_type, norm_type=norm_type)
- self.m = nn.MaxPool2d(kernel_size=pooling_size, stride=1, padding=pooling_size // 2)
- def forward(self, x):
- x = self.cv1(x)
- y1 = self.m(x)
- y2 = self.m(y1)
- return self.cv2(torch.cat((x, y1, y2, self.m(y2)), 1))
- # SPPF block with CSP module
- class SPPFBlockCSP(nn.Module):
- """
- CSP Spatial Pyramid Pooling Block
- """
- def __init__(self,
- in_dim,
- out_dim,
- expand_ratio=0.5,
- pooling_size=5,
- act_type='lrelu',
- norm_type='BN',
- depthwise=False
- ):
- super(SPPFBlockCSP, self).__init__()
- inter_dim = int(in_dim * expand_ratio)
- self.out_dim = out_dim
- self.cv1 = Conv(in_dim, inter_dim, k=1, act_type=act_type, norm_type=norm_type)
- self.cv2 = Conv(in_dim, inter_dim, k=1, act_type=act_type, norm_type=norm_type)
- self.m = nn.Sequential(
- Conv(inter_dim, inter_dim, k=3, p=1,
- act_type=act_type, norm_type=norm_type,
- depthwise=depthwise),
- SPPF(inter_dim,
- inter_dim,
- expand_ratio=1.0,
- pooling_size=pooling_size,
- act_type=act_type,
- norm_type=norm_type),
- Conv(inter_dim, inter_dim, k=3, p=1,
- act_type=act_type, norm_type=norm_type,
- depthwise=depthwise)
- )
- self.cv3 = Conv(inter_dim * 2, self.out_dim, k=1, act_type=act_type, norm_type=norm_type)
-
- def forward(self, x):
- x1 = self.cv1(x)
- x2 = self.cv2(x)
- x3 = self.m(x2)
- y = self.cv3(torch.cat([x1, x3], dim=1))
- return y
- def build_neck(cfg, in_dim, out_dim):
- model = cfg['neck']
- print('==============================')
- print('Neck: {}'.format(model))
- # build neck
- if model == 'sppf':
- neck = SPPF(
- in_dim=in_dim,
- out_dim=out_dim,
- expand_ratio=cfg['expand_ratio'],
- pooling_size=cfg['pooling_size'],
- act_type=cfg['neck_act'],
- norm_type=cfg['neck_norm']
- )
- elif model == 'sppf_block_csp':
- neck = SPPFBlockCSP(
- in_dim=in_dim,
- out_dim=out_dim,
- expand_ratio=cfg['expand_ratio'],
- pooling_size=cfg['pooling_size'],
- act_type=cfg['neck_act'],
- norm_type=cfg['neck_norm'],
- depthwise=cfg['neck_depthwise']
- )
- return neck
-
|