def yolo_body(inputs, num_anchors, num_classes): #---------------------------------------------------# # 生成CSPdarknet53_tiny的主干模型 # feat1的shape为26,26,256 # feat2的shape为13,13,512 #---------------------------------------------------# feat1, feat2 = darknet_body(inputs) # 13,13,512 -> 13,13,256 P5 = DarknetConv2D_BN_Leaky(256, (1, 1))(feat2) # 13,13,256 -> 13,13,512 -> 13,13,255 P5_output = DarknetConv2D_BN_Leaky(512, (3, 3))(P5) P5_output = DarknetConv2D(num_anchors * (num_classes + 5), (1, 1))(P5_output) # 13,13,256 -> 13,13,128 -> 26,26,128 P5_upsample = compose(DarknetConv2D_BN_Leaky(128, (1, 1)), UpSampling2D(2))(P5) # 26,26,256 + 26,26,128 -> 26,26,384 P4 = Concatenate()([P5_upsample, feat1]) # 26,26,384 -> 26,26,256 -> 26,26,255 P4_output = DarknetConv2D_BN_Leaky(256, (3, 3))(P4) P4_output = DarknetConv2D(num_anchors * (num_classes + 5), (1, 1))(P4_output) return Model(inputs, [P5_output, P4_output])
def yolo_body(input_shape, anchors_mask, num_classes, phi=0): inputs = Input(input_shape) #---------------------------------------------------# # 生成CSPdarknet53_tiny的主干模型 # feat1的shape为26,26,256 # feat2的shape为13,13,512 #---------------------------------------------------# feat1, feat2 = darknet_body(inputs) if phi >= 1 and phi <= 4: feat1 = attention[phi - 1](feat1, name='feat1') feat2 = attention[phi - 1](feat2, name='feat2') # 13,13,512 -> 13,13,256 P5 = DarknetConv2D_BN_Leaky(256, (1, 1))(feat2) # 13,13,256 -> 13,13,512 -> 13,13,255 P5_output = DarknetConv2D_BN_Leaky(512, (3, 3))(P5) P5_output = DarknetConv2D( len(anchors_mask[0]) * (num_classes + 5), (1, 1))(P5_output) # 13,13,256 -> 13,13,128 -> 26,26,128 P5_upsample = compose(DarknetConv2D_BN_Leaky(128, (1, 1)), UpSampling2D(2))(P5) if phi >= 1 and phi <= 4: P5_upsample = attention[phi - 1](P5_upsample, name='P5_upsample') # 26,26,256 + 26,26,128 -> 26,26,384 P4 = Concatenate()([P5_upsample, feat1]) # 26,26,384 -> 26,26,256 -> 26,26,255 P4_output = DarknetConv2D_BN_Leaky(256, (3, 3))(P4) P4_output = DarknetConv2D( len(anchors_mask[1]) * (num_classes + 5), (1, 1))(P4_output) return Model(inputs, [P5_output, P4_output])
def yolo_body(inputs, num_anchors, num_classes): # 生成darknet53的主干模型 # 首先我们会获取到两个有效特征层 # feat1 26x26x256 # feat2 13x13x512 feat1, feat2 = darknet_body(inputs) # 13x13x512 -> 13x13x256 P5 = DarknetConv2D_BN_Leaky(256, (1, 1))(feat2) P5_output = DarknetConv2D_BN_Leaky(512, (3, 3))(P5) P5_output = DarknetConv2D(num_anchors * (num_classes + 5), (1, 1))(P5_output) # Conv+UpSampling2D 13x13x256 -> 26x26x128 P5_upsample = compose(DarknetConv2D_BN_Leaky(128, (1, 1)), UpSampling2D(2))(P5) # 26x26x(128+256) 26x26x384 P4 = Concatenate()([feat1, P5_upsample]) P4_output = DarknetConv2D_BN_Leaky(256, (3, 3))(P4) P4_output = DarknetConv2D(num_anchors * (num_classes + 5), (1, 1))(P4_output) return Model(inputs, [P5_output, P4_output])
def yolo_body(inputs, num_anchors, num_classes): #---------------------------------------------------# # 生成CSPdarknet53_tiny的主干模型 # feat1的shape为26,26,256 # feat2的shape为13,13,512 #---------------------------------------------------# feat1, feat2 = darknet_body(inputs) P5 = DarknetConv2D_BN_Leaky(256, (1,1))(feat2) P5 = DarknetConv2D_BN_Leaky(512, (3,3))(P5) P5 = DarknetConv2D_BN_Leaky(256, (1,1))(P5) # 使用了SPP结构,即不同尺度的最大池化后堆叠。 maxpool1 = MaxPooling2D(pool_size=(13,13), strides=(1,1), padding='same')(P5) maxpool2 = MaxPooling2D(pool_size=(9,9), strides=(1,1), padding='same')(P5) maxpool3 = MaxPooling2D(pool_size=(5,5), strides=(1,1), padding='same')(P5) P5 = Concatenate()([maxpool1, maxpool2, P5]) P5 = DarknetConv2D_BN_Leaky(256, (1,1))(P5) P5 = DarknetConv2D_BN_Leaky(512, (3,3))(P5) P5 = DarknetConv2D_BN_Leaky(256, (1,1))(P5) P5 = DarknetConv2D_BN_Leaky(512, (3,3))(P5) P5 = DarknetConv2D_BN_Leaky(256, (1,1))(P5) # 13,13,512 -> 13,13,256 -> 26,26,256 P5_upsample = compose(DarknetConv2D_BN_Leaky(256, (1,1)), UpSampling2D(2))(P5) # 26,26,512 -> 26,26,256 P4 = DarknetConv2D_BN_Leaky(256, (1,1))(feat1) P4 = DarknetConv2D_BN_Leaky(512, (3,3))(P4) P4 = DarknetConv2D_BN_Leaky(256, (1,1))(P4) maxpool1 = MaxPooling2D(pool_size=(13,13), strides=(1,1), padding='same')(P4) maxpool2 = MaxPooling2D(pool_size=(9,9), strides=(1,1), padding='same')(P4) P4 = Concatenate()([maxpool1, maxpool2, P4]) P4 = DarknetConv2D_BN_Leaky(256, (1,1))(P4) P4 = DarknetConv2D_BN_Leaky(512, (3,3))(P4) # 26,26,256 + 26,26,256 -> 26,26,512 P4 = Concatenate()([P4, P5_upsample]) #---------------------------------------------------# # 第二个特征层 # y2=(batch_size,26,26,3,85) #---------------------------------------------------# P4_output = DarknetConv2D_BN_Leaky(512, (3,3))(P4) P4_output = DarknetConv2D(num_anchors*(num_classes+5), (1,1))(P4_output) #---------------------------------------------------# # 第一个特征层 # y1=(batch_size,13,13,3,85) #---------------------------------------------------# P5_output = DarknetConv2D_BN_Leaky(512, (3,3))(P5) P5_output = DarknetConv2D(num_anchors*(num_classes+5), (1,1))(P5_output) return Model(inputs, [P5_output, P4_output])
def yolo_body(inputs, num_anchors, num_classes): # 生成darknet53的主干模型 feat1,feat2 = darknet_body(inputs) # 第一个特征层 # y1=(batch_size,13,13,3,85) P5 = DarknetConv2D_BN_Leaky(256, (1,1))(feat2) P5_output = DarknetConv2D_BN_Leaky(512, (3,3))(P5) P5_output = DarknetConv2D(num_anchors*(num_classes+5), (1,1))(P5_output) P5_upsample = compose(DarknetConv2D_BN_Leaky(128, (1,1)), UpSampling2D(2))(P5) P4 = Concatenate()([feat1, P5_upsample]) P4_output = DarknetConv2D_BN_Leaky(256, (3,3))(P4) P4_output = DarknetConv2D(num_anchors*(num_classes+5), (1,1))(P4_output) return Model(inputs, [P5_output, P4_output])