def yolo2_mobilenetv3large_body(inputs, num_anchors, num_classes, alpha=1.0): """Create YOLO_V2 MobileNetV3Large model CNN body in Keras.""" mobilenetv3large = MobileNetV3Large(input_tensor=inputs, weights='imagenet', include_top=False, alpha=alpha) print('backbone layers number: {}'.format(len(mobilenetv3large.layers))) # input: 416 x 416 x 3 # mobilenetv3large.output(layer 194, final feature map): 13 x 13 x (960*alpha) # expanded_conv_14/Add(layer 191, end of block14): 13 x 13 x (160*alpha) # activation_29(layer 146, middle in block12) : 26 x 26 x (672*alpha) # expanded_conv_11/Add(layer 143, end of block11) : 26 x 26 x (112*alpha) # NOTE: activation layer name may different for TF1.x/2.x, so we # use index to fetch layer # f1: 13 x 13 x (960*alpha) f1 = mobilenetv3large.output # f2: 26 x 26 x (672*alpha) f2 = mobilenetv3large.layers[146].output f1_channel_num = int(960 * alpha) f2_channel_num = int(672 * alpha) y = yolo2_predictions((f1, f2), (f1_channel_num, f2_channel_num), num_anchors, num_classes) return Model(inputs, y)
def yolo2_xception_body(inputs, num_anchors, num_classes): """Create YOLO_V2 Xception model CNN body in Keras.""" xception = Xception(input_tensor=inputs, weights='imagenet', include_top=False) print('backbone layers number: {}'.format(len(xception.layers))) # input: 416 x 416 x 3 # xception.output: 13 x 13 x 2048 # block13_sepconv2_bn(middle in block13, layers[121]): 26 x 26 x 1024 # add_46(end of block12, layers[115]): 26 x 26 x 728 # f1: 13 x 13 x 2048 f1 = xception.output # f2: 26 x 26 x 1024 f2 = xception.layers[121].output f1_channel_num = 2048 f2_channel_num = 1024 #f1_channel_num = 1024 #f2_channel_num = 512 y = yolo2_predictions((f1, f2), (f1_channel_num, f2_channel_num), num_anchors, num_classes) return Model(inputs, y)
def yolo2_efficientnet_body(inputs, num_anchors, num_classes, level=0): ''' Create YOLO_v2 EfficientNet model CNN body in keras. # Arguments level: EfficientNet level number. by default we use basic EfficientNetB0 as backbone ''' efficientnet, feature_map_info = get_efficientnet_backbone_info(inputs, level=level) print('backbone layers number: {}'.format(len(efficientnet.layers))) f1 = efficientnet.get_layer('top_activation').output f1_channel_num = feature_map_info['f1_channel_num'] f2 = efficientnet.get_layer('block6a_expand_activation').output f2_channel_num = feature_map_info['f2_channel_num'] y = yolo2_predictions((f1, f2), (f1_channel_num, f2_channel_num), num_anchors, num_classes) return Model(inputs, y)
def yolo2_body(inputs, num_anchors, num_classes, weights_path=None): """Create YOLO_V2 model CNN body in Keras.""" darknet19 = Model(inputs, darknet19_body()(inputs)) if weights_path is not None: darknet19.load_weights(weights_path, by_name=True) print('Load weights {}.'.format(weights_path)) # input: 416 x 416 x 3 # darknet19.output : 13 x 13 x 1024 # conv13(layers[43]) : 26 x 26 x 512 print('backbone layers number: {}'.format(len(darknet19.layers))) # f1: 13 x 13 x 1024 f1 = darknet19.output # f2: 26 x 26 x 512 f2 = darknet19.layers[43].output f1_channel_num = 1024 f2_channel_num = 512 y = yolo2_predictions((f1, f2), (f1_channel_num, f2_channel_num), num_anchors, num_classes) return Model(inputs, y)
def yolo2_mobilenet_body(inputs, num_anchors, num_classes, alpha=1.0): """Create YOLO_V2 MobileNet model CNN body in Keras.""" mobilenet = MobileNet(input_tensor=inputs, weights='imagenet', include_top=False, alpha=alpha) print('backbone layers number: {}'.format(len(mobilenet.layers))) # input: 416 x 416 x 3 # mobilenet.output : 13 x 13 x (1024*alpha) # conv_pw_11_relu(layers[73]) : 26 x 26 x (512*alpha) # f1: 13 x 13 x (1024*alpha) f1 = mobilenet.output # f2: 26 x 26 x (512*alpha) f2 = mobilenet.get_layer('conv_pw_11_relu').output f1_channel_num = int(1024 * alpha) f2_channel_num = int(512 * alpha) y = yolo2_predictions((f1, f2), (f1_channel_num, f2_channel_num), num_anchors, num_classes) return Model(inputs, y)
def yolo2_mobilenetv2_body(inputs, num_anchors, num_classes, alpha=1.0): """Create YOLO_V2 MobileNetV2 model CNN body in Keras.""" mobilenetv2 = MobileNetV2(input_tensor=inputs, weights='imagenet', include_top=False, alpha=alpha) print('backbone layers number: {}'.format(len(mobilenetv2.layers))) # input: 416 x 416 x 3 # mobilenetv2.output : 13 x 13 x 1280 # block_13_expand_relu(layers[119]) : 26 x 26 x (576*alpha) # f1: 13 x 13 x 1280 f1 = mobilenetv2.output # f2: 26 x 26 x (576*alpha) f2 = mobilenetv2.get_layer('block_13_expand_relu').output f1_channel_num = 1280 f2_channel_num = int(576 * alpha) y = yolo2_predictions((f1, f2), (f1_channel_num, f2_channel_num), num_anchors, num_classes) return Model(inputs, y)
def yolo2_mobilenetv3small_body(inputs, num_anchors, num_classes, alpha=1.0): """Create YOLO_V2 MobileNetV3Small model CNN body in Keras.""" mobilenetv3small = MobileNetV3Small(input_tensor=inputs, weights='imagenet', include_top=False, alpha=alpha) print('backbone layers number: {}'.format(len(mobilenetv3small.layers))) # input: 416 x 416 x 3 # mobilenetv3small.output(layer 165, final feature map): 13 x 13 x (576*alpha) # expanded_conv_10/Add(layer 162, end of block10): 13 x 13 x (96*alpha) # activation_22(layer 117, middle in block8) : 26 x 26 x (288*alpha) # expanded_conv_7/Add(layer 114, end of block7) : 26 x 26 x (48*alpha) # NOTE: activation layer name may different for TF1.x/2.x, so we # use index to fetch layer # f1: 13 x 13 x (960*alpha) f1 = mobilenetv3small.output # f2: 26 x 26 x (672*alpha) f2 = mobilenetv3small.layers[117].output f1_channel_num = int(576*alpha) f2_channel_num = int(288*alpha) y = yolo2_predictions((f1, f2), (f1_channel_num, f2_channel_num), num_anchors, num_classes) return Model(inputs, y)