|
|
@@ -3,7 +3,7 @@ import torch
|
|
|
import torch.nn as nn
|
|
|
import torch.nn.functional as F
|
|
|
|
|
|
-from .yolov7_basic import BasicConv, ELANLayer, MDown
|
|
|
+from .yolov7_basic import BasicConv, ELANLayerFPN, MDown
|
|
|
|
|
|
|
|
|
# PaFPN-ELAN (YOLOv7's)
|
|
|
@@ -12,6 +12,7 @@ class Yolov7PaFPN(nn.Module):
|
|
|
super(Yolov7PaFPN, self).__init__()
|
|
|
# ----------------------------- Basic parameters -----------------------------
|
|
|
self.in_dims = in_dims
|
|
|
+ self.out_dims = [round(256*cfg.width), round(512*cfg.width), round(1024*cfg.width)]
|
|
|
c3, c4, c5 = in_dims
|
|
|
|
|
|
# ----------------------------- Top-down FPN -----------------------------
|
|
|
@@ -20,50 +21,54 @@ class Yolov7PaFPN(nn.Module):
|
|
|
kernel_size=1, act_type=cfg.fpn_act, norm_type=cfg.fpn_norm)
|
|
|
self.reduce_layer_2 = BasicConv(c4, round(256*cfg.width),
|
|
|
kernel_size=1, act_type=cfg.fpn_act, norm_type=cfg.fpn_norm)
|
|
|
- self.top_down_layer_1 = ELANLayer(in_dim = round(256*cfg.width) + round(256*cfg.width),
|
|
|
- out_dim = round(256*cfg.width),
|
|
|
- expansion = 0.5,
|
|
|
- num_blocks = round(3*cfg.depth),
|
|
|
- act_type = cfg.fpn_act,
|
|
|
- norm_type = cfg.fpn_norm,
|
|
|
- depthwise = cfg.fpn_depthwise,
|
|
|
- )
|
|
|
+ self.top_down_layer_1 = ELANLayerFPN(in_dim = round(256*cfg.width) + round(256*cfg.width),
|
|
|
+ out_dim = round(256*cfg.width),
|
|
|
+ expansions = cfg.fpn_expansions,
|
|
|
+ branch_width = cfg.fpn_block_bw,
|
|
|
+ branch_depth = cfg.fpn_block_dw,
|
|
|
+ act_type = cfg.fpn_act,
|
|
|
+ norm_type = cfg.fpn_norm,
|
|
|
+ depthwise = cfg.fpn_depthwise,
|
|
|
+ )
|
|
|
## P4 -> P3
|
|
|
self.reduce_layer_3 = BasicConv(round(256*cfg.width), round(128*cfg.width),
|
|
|
kernel_size=1, act_type=cfg.fpn_act, norm_type=cfg.fpn_norm)
|
|
|
self.reduce_layer_4 = BasicConv(c3, round(128*cfg.width),
|
|
|
kernel_size=1, act_type=cfg.fpn_act, norm_type=cfg.fpn_norm)
|
|
|
- self.top_down_layer_2 = ELANLayer(in_dim = round(128*cfg.width) + round(128*cfg.width),
|
|
|
- out_dim = round(128*cfg.width),
|
|
|
- expansion = 0.5,
|
|
|
- num_blocks = round(3*cfg.depth),
|
|
|
- act_type = cfg.fpn_act,
|
|
|
- norm_type = cfg.fpn_norm,
|
|
|
- depthwise = cfg.fpn_depthwise,
|
|
|
- )
|
|
|
+ self.top_down_layer_2 = ELANLayerFPN(in_dim = round(128*cfg.width) + round(128*cfg.width),
|
|
|
+ out_dim = round(128*cfg.width),
|
|
|
+ expansions = cfg.fpn_expansions,
|
|
|
+ branch_width = cfg.fpn_block_bw,
|
|
|
+ branch_depth = cfg.fpn_block_dw,
|
|
|
+ act_type = cfg.fpn_act,
|
|
|
+ norm_type = cfg.fpn_norm,
|
|
|
+ depthwise = cfg.fpn_depthwise,
|
|
|
+ )
|
|
|
# ----------------------------- Bottom-up FPN -----------------------------
|
|
|
## P3 -> P4
|
|
|
self.downsample_layer_1 = MDown(round(128*cfg.width), round(256*cfg.width),
|
|
|
act_type=cfg.fpn_act, norm_type=cfg.fpn_norm)
|
|
|
- self.bottom_up_layer_1 = ELANLayer(in_dim = round(256*cfg.width) + round(256*cfg.width),
|
|
|
- out_dim = round(256*cfg.width),
|
|
|
- expansion = 0.5,
|
|
|
- num_blocks = round(3*cfg.depth),
|
|
|
- act_type = cfg.fpn_act,
|
|
|
- norm_type = cfg.fpn_norm,
|
|
|
- depthwise = cfg.fpn_depthwise,
|
|
|
- )
|
|
|
+ self.bottom_up_layer_1 = ELANLayerFPN(in_dim = round(256*cfg.width) + round(256*cfg.width),
|
|
|
+ out_dim = round(256*cfg.width),
|
|
|
+ expansions = cfg.fpn_expansions,
|
|
|
+ branch_width = cfg.fpn_block_bw,
|
|
|
+ branch_depth = cfg.fpn_block_dw,
|
|
|
+ act_type = cfg.fpn_act,
|
|
|
+ norm_type = cfg.fpn_norm,
|
|
|
+ depthwise = cfg.fpn_depthwise,
|
|
|
+ )
|
|
|
## P4 -> P5
|
|
|
self.downsample_layer_2 = MDown(round(256*cfg.width), round(512*cfg.width),
|
|
|
act_type=cfg.fpn_act, norm_type=cfg.fpn_norm)
|
|
|
- self.bottom_up_layer_2 = ELANLayer(in_dim = round(512*cfg.width) + c5,
|
|
|
- out_dim = round(512*cfg.width),
|
|
|
- expansion = 0.5,
|
|
|
- num_blocks = round(3*cfg.depth),
|
|
|
- act_type = cfg.fpn_act,
|
|
|
- norm_type = cfg.fpn_norm,
|
|
|
- depthwise = cfg.fpn_depthwise,
|
|
|
- )
|
|
|
+ self.bottom_up_layer_2 = ELANLayerFPN(in_dim = round(512*cfg.width) + c5,
|
|
|
+ out_dim = round(512*cfg.width),
|
|
|
+ expansions = cfg.fpn_expansions,
|
|
|
+ branch_width = cfg.fpn_block_bw,
|
|
|
+ branch_depth = cfg.fpn_block_dw,
|
|
|
+ act_type = cfg.fpn_act,
|
|
|
+ norm_type = cfg.fpn_norm,
|
|
|
+ depthwise = cfg.fpn_depthwise,
|
|
|
+ )
|
|
|
|
|
|
# ----------------------------- Head conv layers -----------------------------
|
|
|
## Head convs
|
|
|
@@ -77,15 +82,6 @@ class Yolov7PaFPN(nn.Module):
|
|
|
kernel_size=3, padding=1, stride=1,
|
|
|
act_type=cfg.fpn_act, norm_type=cfg.fpn_norm, depthwise=cfg.fpn_depthwise)
|
|
|
|
|
|
- # ---------------------- Yolov5's output projection ----------------------
|
|
|
- self.out_layers = nn.ModuleList([
|
|
|
- BasicConv(in_dim, round(cfg.head_dim*cfg.width), kernel_size=1,
|
|
|
- act_type=cfg.fpn_act, norm_type=cfg.fpn_norm)
|
|
|
- for in_dim in [round(256*cfg.width), round(512*cfg.width), round(1024*cfg.width)]
|
|
|
- ])
|
|
|
- self.out_dims = [round(cfg.head_dim*cfg.width)] * 3
|
|
|
-
|
|
|
-
|
|
|
def forward(self, features):
|
|
|
c3, c4, c5 = features
|
|
|
|
|
|
@@ -112,10 +108,5 @@ class Yolov7PaFPN(nn.Module):
|
|
|
p5 = self.bottom_up_layer_2(p5)
|
|
|
|
|
|
out_feats = [self.head_conv_1(p3), self.head_conv_2(p4), self.head_conv_3(p5)]
|
|
|
-
|
|
|
- # output proj layers
|
|
|
- out_feats_proj = []
|
|
|
- for feat, layer in zip(out_feats, self.out_layers):
|
|
|
- out_feats_proj.append(layer(feat))
|
|
|
|
|
|
- return out_feats_proj
|
|
|
+ return out_feats
|