Spaces:
Runtime error
Runtime error
Update nets/yolo.py
Browse files- nets/yolo.py +3 -95
nets/yolo.py
CHANGED
|
@@ -1,20 +1,9 @@
|
|
| 1 |
import torch
|
| 2 |
import torch.nn as nn
|
| 3 |
from torch.nn import functional as F
|
| 4 |
-
from nets.CSPdarknet import darknet
|
| 5 |
from nets.CSPdarknet_tiny import darknet_tiny
|
| 6 |
-
from nets.mobilenetv2 import mobilenet_v2
|
| 7 |
-
from nets.shufflenet_v2 import shufflenet_v2
|
| 8 |
-
from nets.ghostnet import ghostnet
|
| 9 |
-
from nets.attention import cbam_block, eca_block, se_block, CA_Block
|
| 10 |
-
attention_block = [se_block, cbam_block, eca_block, CA_Block]
|
| 11 |
|
| 12 |
|
| 13 |
-
|
| 14 |
-
#-------------------------------------------------#
|
| 15 |
-
# 卷积块 -> 卷积 + 标准化 + 激活函数
|
| 16 |
-
# Conv2d + BatchNormalization + LeakyReLU
|
| 17 |
-
#-------------------------------------------------#
|
| 18 |
class BasicConv(nn.Module):
|
| 19 |
def __init__(self, in_channels, out_channels, kernel_size, stride=1):
|
| 20 |
super(BasicConv, self).__init__()
|
|
@@ -29,9 +18,6 @@ class BasicConv(nn.Module):
|
|
| 29 |
x = self.activation(x)
|
| 30 |
return x
|
| 31 |
|
| 32 |
-
#---------------------------------------------------#
|
| 33 |
-
# 卷积 + 上采样
|
| 34 |
-
#---------------------------------------------------#
|
| 35 |
class Upsample(nn.Module):
|
| 36 |
def __init__(self, in_channels, out_channels):
|
| 37 |
super(Upsample, self).__init__()
|
|
@@ -45,9 +31,6 @@ class Upsample(nn.Module):
|
|
| 45 |
x = self.upsample(x)
|
| 46 |
return x
|
| 47 |
|
| 48 |
-
#---------------------------------------------------#
|
| 49 |
-
# 最后获得yolov4的输出
|
| 50 |
-
#---------------------------------------------------#
|
| 51 |
def yolo_head(filters_list, in_filters):
|
| 52 |
m = nn.Sequential(
|
| 53 |
BasicConv(in_filters, filters_list[0], 3),
|
|
@@ -128,26 +111,9 @@ class CARAFE(nn.Module):
|
|
| 128 |
# yolo_body--MSFNet
|
| 129 |
#---------------------------------------------------#
|
| 130 |
class YoloBody(nn.Module):
|
| 131 |
-
def __init__(self, anchors_mask, num_classes, phi=0, backbone ='', pretrained=False):
|
| 132 |
super(YoloBody, self).__init__()
|
| 133 |
-
|
| 134 |
-
self.phi = phi
|
| 135 |
-
|
| 136 |
-
if backbone == 'cspdarknet':
|
| 137 |
-
self.backbone = darknet(pretrained)
|
| 138 |
-
self.conv_for_P5 = BasicConv(512,256,1)
|
| 139 |
-
self.yolo_headP5 = yolo_head([512, len(anchors_mask[0]) * (5 + num_classes)],256)
|
| 140 |
-
self.upsample_1 = Upsample(256,128)
|
| 141 |
-
self.conv1 = BasicConv(256,128,1)
|
| 142 |
-
self.upsample_2 = CARAFE(128)
|
| 143 |
-
self.yolo_headP4 = yolo_head([256, len(anchors_mask[1]) * (5 + num_classes)],384)
|
| 144 |
-
|
| 145 |
-
if 1 <= self.phi and self.phi <= 4:
|
| 146 |
-
self.feat1_att = attention_block[self.phi - 1](256)
|
| 147 |
-
self.feat2_att = attention_block[self.phi - 1](512)
|
| 148 |
-
self.upsample_att = attention_block[self.phi - 1](128)
|
| 149 |
-
|
| 150 |
-
elif backbone == 'tiny':
|
| 151 |
self.backbone = darknet_tiny(pretrained)
|
| 152 |
self.conv_for_P5 = BasicConv(512,256,1)
|
| 153 |
self.yolo_headP5 = yolo_head([512, len(anchors_mask[0]) * (5 + num_classes)],256)
|
|
@@ -156,67 +122,9 @@ class YoloBody(nn.Module):
|
|
| 156 |
self.upsample_2 = CARAFE(128)
|
| 157 |
self.yolo_headP4 = yolo_head([256, len(anchors_mask[1]) * (5 + num_classes)],384)
|
| 158 |
|
| 159 |
-
if 1 <= self.phi and self.phi <= 4:
|
| 160 |
-
self.feat1_att = attention_block[self.phi - 1](256)
|
| 161 |
-
self.feat2_att = attention_block[self.phi - 1](512)
|
| 162 |
-
self.upsample_att = attention_block[self.phi - 1](128)
|
| 163 |
-
|
| 164 |
-
elif backbone == 'mobilenetv2':
|
| 165 |
-
self.backbone = mobilenet_v2(pretrained)
|
| 166 |
-
self.conv_for_P5 = BasicConv(320,256,1)
|
| 167 |
-
self.yolo_headP5 = yolo_head([512, len(anchors_mask[0]) * (5 + num_classes)],256)
|
| 168 |
-
self.upsample_1 = Upsample(256,128)
|
| 169 |
-
self.conv1 = BasicConv(256,128,1)
|
| 170 |
-
self.upsample_2 = CARAFE(128)
|
| 171 |
-
self.yolo_headP4 = yolo_head([256, len(anchors_mask[1]) * (5 + num_classes)],224)
|
| 172 |
-
|
| 173 |
-
if 1 <= self.phi and self.phi <= 4:
|
| 174 |
-
self.feat1_att = attention_block[self.phi - 1](256)
|
| 175 |
-
self.feat2_att = attention_block[self.phi - 1](512)
|
| 176 |
-
self.upsample_att = attention_block[self.phi - 1](128)
|
| 177 |
-
|
| 178 |
-
elif backbone == 'shufflenetv2':
|
| 179 |
-
self.backbone = shufflenet_v2()
|
| 180 |
-
self.conv_for_P5 = BasicConv(1024,256,1)
|
| 181 |
-
self.yolo_headP5 = yolo_head([512, len(anchors_mask[0]) * (5 + num_classes)],256)
|
| 182 |
-
self.upsample_1 = Upsample(256,128)
|
| 183 |
-
self.conv1 = BasicConv(256,128,1)
|
| 184 |
-
self.upsample_2 = CARAFE(128)
|
| 185 |
-
self.yolo_headP4 = yolo_head([256, len(anchors_mask[1]) * (5 + num_classes)],592)
|
| 186 |
-
|
| 187 |
-
if 1 <= self.phi and self.phi <= 4:
|
| 188 |
-
self.feat1_att = attention_block[self.phi - 1](256)
|
| 189 |
-
self.feat2_att = attention_block[self.phi - 1](512)
|
| 190 |
-
self.upsample_att = attention_block[self.phi - 1](128)
|
| 191 |
-
|
| 192 |
-
elif backbone == 'ghostnet':
|
| 193 |
-
self.backbone = ghostnet()
|
| 194 |
-
self.conv_for_P5 = BasicConv(160,256,1)
|
| 195 |
-
self.yolo_headP5 = yolo_head([512, len(anchors_mask[0]) * (5 + num_classes)],256)
|
| 196 |
-
self.upsample_1 = Upsample(256,128)
|
| 197 |
-
self.conv1 = BasicConv(256,128,1)
|
| 198 |
-
self.upsample_2 = CARAFE(128)
|
| 199 |
-
self.yolo_headP4 = yolo_head([256, len(anchors_mask[1]) * (5 + num_classes)],240)
|
| 200 |
-
|
| 201 |
-
if 1 <= self.phi and self.phi <= 4:
|
| 202 |
-
self.feat1_att = attention_block[self.phi - 1](256)
|
| 203 |
-
self.feat2_att = attention_block[self.phi - 1](512)
|
| 204 |
-
self.upsample_att = attention_block[self.phi - 1](128)
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
def forward(self, x):
|
| 209 |
|
| 210 |
-
#---------------------------------------------------#
|
| 211 |
-
# 生成CSPdarknet53_tiny的主干模型
|
| 212 |
-
# feat1的shape为26,26,256
|
| 213 |
-
# feat2的shape为13,13,512
|
| 214 |
-
#---------------------------------------------------#
|
| 215 |
feat1, feat2 = self.backbone(x)
|
| 216 |
-
if 1 <= self.phi and self.phi <= 4:
|
| 217 |
-
feat1 = self.feat1_att(feat1)
|
| 218 |
-
feat2 = self.feat2_att(feat2)
|
| 219 |
-
|
| 220 |
# 13,13,512 -> 13,13,256
|
| 221 |
P5 = self.conv_for_P5(feat2)
|
| 222 |
# 13,13,256 -> 13,13,512 -> 13,13,255
|
|
@@ -225,7 +133,7 @@ class YoloBody(nn.Module):
|
|
| 225 |
P6 = self.conv_for_P5(feat2)
|
| 226 |
P6_Upsample = self.upsample_1(P6)
|
| 227 |
|
| 228 |
-
# 13,13,256 -> 13,13,128 -> 26,26,128
|
| 229 |
P5 = self.conv1(P5)
|
| 230 |
P5_Upsample = self.upsample_2(P5)
|
| 231 |
|
|
|
|
| 1 |
import torch
|
| 2 |
import torch.nn as nn
|
| 3 |
from torch.nn import functional as F
|
|
|
|
| 4 |
from nets.CSPdarknet_tiny import darknet_tiny
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
class BasicConv(nn.Module):
|
| 8 |
def __init__(self, in_channels, out_channels, kernel_size, stride=1):
|
| 9 |
super(BasicConv, self).__init__()
|
|
|
|
| 18 |
x = self.activation(x)
|
| 19 |
return x
|
| 20 |
|
|
|
|
|
|
|
|
|
|
| 21 |
class Upsample(nn.Module):
|
| 22 |
def __init__(self, in_channels, out_channels):
|
| 23 |
super(Upsample, self).__init__()
|
|
|
|
| 31 |
x = self.upsample(x)
|
| 32 |
return x
|
| 33 |
|
|
|
|
|
|
|
|
|
|
| 34 |
def yolo_head(filters_list, in_filters):
|
| 35 |
m = nn.Sequential(
|
| 36 |
BasicConv(in_filters, filters_list[0], 3),
|
|
|
|
| 111 |
# yolo_body--MSFNet
|
| 112 |
#---------------------------------------------------#
|
| 113 |
class YoloBody(nn.Module):
|
| 114 |
+
def __init__(self, anchors_mask, num_classes, phi=0, backbone ='tiny', pretrained=False):
|
| 115 |
super(YoloBody, self).__init__()
|
| 116 |
+
if backbone == 'tiny':
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
self.backbone = darknet_tiny(pretrained)
|
| 118 |
self.conv_for_P5 = BasicConv(512,256,1)
|
| 119 |
self.yolo_headP5 = yolo_head([512, len(anchors_mask[0]) * (5 + num_classes)],256)
|
|
|
|
| 122 |
self.upsample_2 = CARAFE(128)
|
| 123 |
self.yolo_headP4 = yolo_head([256, len(anchors_mask[1]) * (5 + num_classes)],384)
|
| 124 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
def forward(self, x):
|
| 126 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
feat1, feat2 = self.backbone(x)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
# 13,13,512 -> 13,13,256
|
| 129 |
P5 = self.conv_for_P5(feat2)
|
| 130 |
# 13,13,256 -> 13,13,512 -> 13,13,255
|
|
|
|
| 133 |
P6 = self.conv_for_P5(feat2)
|
| 134 |
P6_Upsample = self.upsample_1(P6)
|
| 135 |
|
| 136 |
+
# 13,13,256 -> 13,13,128 -> 26,26,128
|
| 137 |
P5 = self.conv1(P5)
|
| 138 |
P5_Upsample = self.upsample_2(P5)
|
| 139 |
|