def __init__(self, c1, c2, k=3, s=1, g=1, e=1.0, shortcut=False): # ch_in, ch_out, kernel, stride, groups, expansion, shortcut super(CrossConv, self).__init__() c_ = int(c2 * e) # hidden channels self.cv1 = Conv(c1, c_, (1, k), (1, s)) self.cv2 = Conv(c_, c2, (k, 1), (s, 1), g=g) self.add = shortcut and c1 == c2
def __init__(self, c1, c2, k=1, s=1, g=1, act=True): # ch_in, ch_out, kernel, stride, groups super(GhostConv, self).__init__() c_ = c2 // 2 # hidden channels self.cv1 = Conv(c1, c_, k, s, None, g, act) self.cv2 = Conv(c_, c_, 5, 1, None, c_, act)
def __init__(self, c1, c2, k=3, s=1): # ch_in, ch_out, kernel, stride super(GhostBottleneck, self).__init__() c_ = c2 // 2 self.conv = nn.Sequential( GhostConv(c1, c_, 1, 1), # pw DWConv(c_, c_, k, s, act=False) if s == 2 else nn.Identity(), # dw GhostConv(c_, c2, 1, 1, act=False)) # pw-linear self.shortcut = nn.Sequential(DWConv( c1, c1, k, s, act=False), Conv( c1, c2, 1, 1, act=False)) if s == 2 else nn.Identity()
def __init__(self, P3_size=512, P4_size=256, P5_size=512, inner_p3=256, inner_p4=512, inner_p5=1024, version='L'): super(PAN, self).__init__() self.version = str(version) gains = {'s': {'gd': 0.33, 'gw': 0.5}, 'm': {'gd': 0.67, 'gw': 0.75}, 'l': {'gd': 1, 'gw': 1}, 'x': {'gd': 1.33, 'gw': 1.25}} if self.version.lower() in gains: # only for yolov5 self.gd = gains[self.version.lower()]['gd'] # depth gain self.gw = gains[self.version.lower()]['gw'] # width gain else: self.gd = 1 self.gw = 1 self.channels_out = { 'inner_p3': inner_p3, 'inner_p4': inner_p4, 'inner_p5': inner_p5 } self.re_channels_out() self.P3_size = P3_size self.P4_size = P4_size self.P5_size = P5_size self.inner_p3 = self.channels_out['inner_p3'] self.inner_p4 = self.channels_out['inner_p4'] self.inner_p5 = self.channels_out['inner_p5'] self.P3 = C3(self.P3_size, self.inner_p3, self.get_depth(3), False) self.convP3 = Conv(self.inner_p3, self.inner_p3, 3, 2) self.P4 = C3(self.P4_size + self.inner_p3, self.inner_p4, self.get_depth(3), False) self.convP4 = Conv(self.inner_p4, self.inner_p4, 3, 2) self.P5 = C3(self.inner_p4 + P5_size, self.inner_p5, self.get_depth(3), False) self.concat = Concat() self.out_shape = (self.inner_p3, self.inner_p4, self.inner_p5) print("PAN input channel size: P3 {}, P4 {}, P5 {}".format(self.P3_size, self.P4_size, self.P5_size)) print("PAN output channel size: PP3 {}, PP4 {}, PP5 {}".format(self.inner_p3, self.inner_p4, self.inner_p5))
def __init__(self, C3_size=256, C4_size=512, C5_size=512, inner_p4=512, outer_p4=256, version='S'): super(PyramidFeatures, self).__init__() self.C3_size = C3_size self.C4_size = C4_size self.C5_size = C5_size self.version = version gains = {'s': {'gd': 0.33, 'gw': 0.5}, 'm': {'gd': 0.67, 'gw': 0.75}, 'l': {'gd': 1, 'gw': 1}, 'x': {'gd': 1.33, 'gw': 1.25}} if self.version.lower() in gains: # only for yolov5 self.gd = gains[self.version.lower()]['gd'] # depth gain self.gw = gains[self.version.lower()]['gw'] # width gain else: self.gd = 0.33 self.gw = 0.5 self.channels_out = { 'inner_p4': inner_p4, 'outer_p4': outer_p4, } self.re_channels_out() self.concat = Concat() self.P5_upsampled = nn.Upsample(scale_factor=2, mode='nearest') self.P4_1 = C3(self.C5_size + self.C4_size, self.channels_out['inner_p4'], self.get_depth(3), False) self.P4_upsampled = nn.Upsample(scale_factor=2, mode='nearest') self.P4_2 = Conv(self.channels_out['inner_p4'], self.channels_out['outer_p4'], 1, 1) self.out_shape = {'P3_size': self.C3_size + self.channels_out['outer_p4'], 'P4_size': self.channels_out['outer_p4'], 'P5_size': self.C5_size} print("FPN input channel size: C3 {}, C4 {}, C5 {}".format(self.C3_size, self.C4_size, self.C5_size)) print("FPN output channel size: P3 {}, P4 {}, P5 {}".format(self.C3_size + self.channels_out['outer_p4'], self.channels_out['outer_p4'], self.C5_size))
def __init__(self, focus=True, version='L', with_C3TR=False): super(YOLOv5, self).__init__() self.version = version self.with_focus = focus self.with_c3tr = with_C3TR gains = { 's': { 'gd': 0.33, 'gw': 0.5 }, 'm': { 'gd': 0.67, 'gw': 0.75 }, 'l': { 'gd': 1, 'gw': 1 }, 'x': { 'gd': 1.33, 'gw': 1.25 } } self.gd = gains[self.version.lower()]['gd'] # depth gain self.gw = gains[self.version.lower()]['gw'] # width gain self.channels_out = { 'stage1': 64, 'stage2_1': 128, 'stage2_2': 128, 'stage3_1': 256, 'stage3_2': 256, 'stage4_1': 512, 'stage4_2': 512, 'stage5': 1024, 'spp': 1024, 'csp1': 1024, 'conv1': 512 } self.re_channels_out() if self.with_focus: self.stage1 = Focus(3, self.channels_out['stage1']) else: self.stage1 = Conv(3, self.channels_out['stage1'], 3, 2) # for latest yolov5, you can change BottleneckCSP to C3 self.stage2_1 = Conv(self.channels_out['stage1'], self.channels_out['stage2_1'], k=3, s=2) self.stage2_2 = C3(self.channels_out['stage2_1'], self.channels_out['stage2_2'], self.get_depth(3)) self.stage3_1 = Conv(self.channels_out['stage2_2'], self.channels_out['stage3_1'], 3, 2) self.stage3_2 = C3(self.channels_out['stage3_1'], self.channels_out['stage3_2'], self.get_depth(9)) self.stage4_1 = Conv(self.channels_out['stage3_2'], self.channels_out['stage4_1'], 3, 2) self.stage4_2 = C3(self.channels_out['stage4_1'], self.channels_out['stage4_2'], self.get_depth(9)) self.stage5 = Conv(self.channels_out['stage4_2'], self.channels_out['stage5'], 3, 2) self.spp = SPP(self.channels_out['stage5'], self.channels_out['spp'], [5, 9, 13]) if self.with_c3tr: self.c3tr = C3TR(self.channels_out['spp'], self.channels_out['csp1'], self.get_depth(3), False) else: self.csp1 = C3(self.channels_out['spp'], self.channels_out['csp1'], self.get_depth(3), False) self.conv1 = Conv(self.channels_out['csp1'], self.channels_out['conv1'], 1, 1) self.out_shape = { 'C3_size': self.channels_out['stage3_2'], 'C4_size': self.channels_out['stage4_2'], 'C5_size': self.channels_out['conv1'] } print("backbone output channel: C3 {}, C4 {}, C5 {}".format( self.channels_out['stage3_2'], self.channels_out['stage4_2'], self.channels_out['conv1']))