def yolo3_spp_xception_body(inputs, num_anchors, num_classes): """Create YOLO_V3 SPP Xception model CNN body in Keras.""" xception = Xception(input_tensor=inputs, weights='imagenet', include_top=False) # input: 416 x 416 x 3 # block14_sepconv2_act: 13 x 13 x 2048 # block13_sepconv2_bn(middle in block13): 26 x 26 x 1024 # add_46(end of block12): 26 x 26 x 728 # block4_sepconv2_bn(middle in block4) : 52 x 52 x 728 # add_37(end of block3) : 52 x 52 x 256 f1 = xception.get_layer('block14_sepconv2_act').output # f1 :13 x 13 x 2048 x, y1 = make_spp_last_layers(f1, 1024, num_anchors * (num_classes + 5)) x = compose(DarknetConv2D_BN_Leaky(512, (1, 1)), UpSampling2D(2))(x) f2 = xception.get_layer('block13_sepconv2_bn').output # f2: 26 x 26 x 1024 x = Concatenate()([x, f2]) x, y2 = make_last_layers(x, 512, num_anchors * (num_classes + 5)) x = compose(DarknetConv2D_BN_Leaky(256, (1, 1)), UpSampling2D(2))(x) f3 = xception.get_layer('block4_sepconv2_bn').output # f3 : 52 x 52 x 728 x = Concatenate()([x, f3]) x, y3 = make_last_layers(x, 256, num_anchors * (num_classes + 5)) return Model(inputs=inputs, outputs=[y1, y2, y3])
def yolo3_spp_xception_body(inputs, num_anchors, num_classes): """Create YOLO_V3 SPP Xception model CNN body in Keras.""" xception = Xception(input_tensor=inputs, weights='imagenet', include_top=False) # input: 416 x 416 x 3 # block14_sepconv2_act: 13 x 13 x 2048 # block13_sepconv2_bn(middle in block13): 26 x 26 x 1024 # add_46(end of block12): 26 x 26 x 728 # block4_sepconv2_bn(middle in block4) : 52 x 52 x 728 # add_37(end of block3) : 52 x 52 x 256 # f1: 13 x 13 x 2048 f1 = xception.get_layer('block14_sepconv2_act').output # f2: 26 x 26 x 1024 f2 = xception.get_layer('block13_sepconv2_bn').output # f3: 52 x 52 x 728 f3 = xception.get_layer('block4_sepconv2_bn').output #f1_channel_num = 2048 #f2_channel_num = 1024 #f3_channel_num = 728 f1_channel_num = 1024 f2_channel_num = 512 f3_channel_num = 256 #feature map 1 head & output (13x13 for 416 input) x, y1 = make_spp_last_layers(f1, f1_channel_num // 2, num_anchors * (num_classes + 5)) #upsample fpn merge for feature map 1 & 2 x = compose(DarknetConv2D_BN_Leaky(f2_channel_num // 2, (1, 1)), UpSampling2D(2))(x) x = Concatenate()([x, f2]) #feature map 2 head & output (26x26 for 416 input) x, y2 = make_last_layers(x, f2_channel_num // 2, num_anchors * (num_classes + 5)) #upsample fpn merge for feature map 2 & 3 x = compose(DarknetConv2D_BN_Leaky(f3_channel_num // 2, (1, 1)), UpSampling2D(2))(x) x = Concatenate()([x, f3]) #feature map 3 head & output (52x52 for 416 input) x, y3 = make_last_layers(x, f3_channel_num // 2, num_anchors * (num_classes + 5)) return Model(inputs=inputs, outputs=[y1, y2, y3])
def yolo3_spp_body(inputs, num_anchors, num_classes, weights_path=None): """Create YOLO_V3 SPP model CNN body in Keras.""" darknet = Model(inputs, darknet53_body(inputs)) if weights_path is not None: darknet.load_weights(weights_path, by_name=True) print('Load weights {}.'.format(weights_path)) #x, y1 = make_last_layers(darknet.output, 512, num_anchors*(num_classes+5)) x, y1 = make_spp_last_layers(darknet.output, 512, num_anchors * (num_classes + 5)) x = compose(DarknetConv2D_BN_Leaky(256, (1, 1)), UpSampling2D(2))(x) x = Concatenate()([x, darknet.layers[152].output]) x, y2 = make_last_layers(x, 256, num_anchors * (num_classes + 5)) x = compose(DarknetConv2D_BN_Leaky(128, (1, 1)), UpSampling2D(2))(x) x = Concatenate()([x, darknet.layers[92].output]) x, y3 = make_last_layers(x, 128, num_anchors * (num_classes + 5)) return Model(inputs, [y1, y2, y3])
def yolo3_spp_body(inputs, num_anchors, num_classes, weights_path=None): """Create YOLO_V3 SPP model CNN body in Keras.""" darknet = Model(inputs, darknet53_body(inputs)) if weights_path is not None: darknet.load_weights(weights_path, by_name=True) print('Load weights {}.'.format(weights_path)) # f1: 13 x 13 x 1024 f1 = darknet.output # f2: 26 x 26 x 512 f2 = darknet.layers[152].output # f3: 52 x 52 x 256 f3 = darknet.layers[92].output f1_channel_num = 1024 f2_channel_num = 512 f3_channel_num = 256 # feature map 1 head & output (19x19 for 608 input) x, y1 = make_spp_last_layers(f1, f1_channel_num // 2, num_anchors * (num_classes + 5)) # upsample fpn merge for feature map 1 & 2 x = compose(DarknetConv2D_BN_Leaky(f2_channel_num // 2, (1, 1)), UpSampling2D(2))(x) x = Concatenate()([x, f2]) # feature map 2 head & output (38x38 for 608 input) x, y2 = make_last_layers(x, f2_channel_num // 2, num_anchors * (num_classes + 5)) # upsample fpn merge for feature map 2 & 3 x = compose(DarknetConv2D_BN_Leaky(f3_channel_num // 2, (1, 1)), UpSampling2D(2))(x) x = Concatenate()([x, f3]) # feature map 3 head & output (76x76 for 608 input) x, y3 = make_last_layers(x, f3_channel_num // 2, num_anchors * (num_classes + 5)) return Model(inputs, [y1, y2, y3])