def pan_down(backbone_output_layers): output_layers = [] pan_down_output_layer = scaled_yolov4_csp_block( backbone_output_layers[-1], backbone_output_layers[-1].shape[-1] // 2, 3, type='spp') output_layers.append(pan_down_output_layer) for layer_index in range(2, len(backbone_output_layers) + 1): pan_down_output_layer = conv2d_bn_mish( pan_down_output_layer, pan_down_output_layer.shape[-1] // 2, (1, 1)) pan_down_output_layer = tf.keras.layers.UpSampling2D( size=(2, 2))(pan_down_output_layer) backbone_output_layer = conv2d_bn_mish( backbone_output_layers[-layer_index], backbone_output_layers[-layer_index].shape[3] // 2, (1, 1)) pan_down_output_layer = tf.keras.layers.Concatenate()( [backbone_output_layer, pan_down_output_layer]) pan_down_output_layer = scaled_yolov4_csp_block( pan_down_output_layer, pan_down_output_layer.shape[-1] // 2, 3, type='head') output_layers.append(pan_down_output_layer) return output_layers
def head_up(head_down_output_layers): output_layers = [] head_up_output_layers = head_down_output_layers[-1] output_layers.append(head_up_output_layers) head_up_block_index = 0 for layer_index in range(2, len(head_down_output_layers) + 1): if layer_index >= 4: head_up_out_channel = head_up_output_layers.shape[-1] else: head_up_out_channel = head_up_output_layers.shape[-1] * 2 head_up_output_layers = tf.keras.layers.ZeroPadding2D( ((1, 0), (1, 0)))(head_up_output_layers) head_up_output_layers = conv2d_bn_mish( head_up_output_layers, head_up_out_channel, (3, 3), strides=(2, 2), padding='valid', name="head_up_block_{}_0".format(head_up_block_index)) head_up_output_layers = tf.keras.layers.Concatenate()( [head_up_output_layers, head_down_output_layers[-layer_index]]) head_up_output_layers = scaled_yolov4_csp_block( head_up_output_layers, head_up_output_layers.shape[-1] // 2, 3, type='head', name="head_up_block_{}_1".format(head_up_block_index)) head_up_block_index += 1 output_layers.append(head_up_output_layers) return output_layers
def scaled_yolov4_csp_darknet53(x, mode='p5'): darknet53_filters = [64 * 2**i for i in range(5)] if mode == 'p5': loop_nums = [1, 3, 15, 15, 7] elif mode == 'p6': loop_nums = [1, 3, 15, 15, 7, 7] darknet53_filters += [1024] elif mode == 'p7': loop_nums = [1, 3, 15, 15, 7, 7, 7] darknet53_filters += [1024] * 2 x = conv2d_bn_mish(x, 32, (3, 3), name="first_block") output_layers = [] for block_index in range(len(loop_nums)): x = tf.keras.layers.ZeroPadding2D(((1, 0), (1, 0)))(x) x = conv2d_bn_mish(x, darknet53_filters[block_index], (3, 3), strides=(2, 2), padding='valid', name="backbone_block_{}_0".format(block_index)) x = scaled_yolov4_csp_block( x, darknet53_filters[block_index], loop_nums[block_index], type="backbone", name="backbone_block_{}_1".format(block_index)) output_layers.append(x) return output_layers[2:]
def head_down(backbone_output_layers): output_layers = [] head_down_output_layer = scaled_yolov4_csp_block( backbone_output_layers[-1], backbone_output_layers[-1].shape[-1] // 2, 3, type='spp', name='spp') output_layers.append(head_down_output_layer) head_down_block_index = 0 for layer_index in range(2, len(backbone_output_layers) + 1): if layer_index < len(backbone_output_layers) - 1: spp_out_channel = head_down_output_layer.shape[-1] else: spp_out_channel = head_down_output_layer.shape[-1] // 2 head_down_output_layer = conv2d_bn_mish( head_down_output_layer, spp_out_channel, (1, 1), name="head_down_block_{}_0".format(head_down_block_index)) head_down_output_layer = tf.keras.layers.UpSampling2D( size=(2, 2))(head_down_output_layer) backbone_output_layer = conv2d_bn_mish( backbone_output_layers[-layer_index], backbone_output_layers[-layer_index].shape[3] // 2, (1, 1), name="head_down_block_{}_1".format(head_down_block_index)) head_down_output_layer = tf.keras.layers.Concatenate()( [backbone_output_layer, head_down_output_layer]) head_down_output_layer = scaled_yolov4_csp_block( head_down_output_layer, head_down_output_layer.shape[-1] // 2, 3, type='head', name="head_down_block_{}_2".format(head_down_block_index)) head_down_block_index += 1 output_layers.append(head_down_output_layer) return output_layers
def pan_up(pan_down_output_layers): output_layers = [] pan_up_output_layers = pan_down_output_layers[-1] output_layers.append(pan_up_output_layers) for layer_index in range(2, len(pan_down_output_layers) + 1): pan_up_output_layers = tf.keras.layers.ZeroPadding2D( ((1, 0), (1, 0)))(pan_up_output_layers) pan_up_output_layers = conv2d_bn_mish(pan_up_output_layers, pan_up_output_layers.shape[-1] * 2, (3, 3), strides=(2, 2), padding='valid') pan_up_output_layers = tf.keras.layers.Concatenate()( [pan_up_output_layers, pan_down_output_layers[-layer_index]]) pan_down_output_layer = scaled_yolov4_csp_block( pan_up_output_layers, pan_up_output_layers.shape[-1] // 2, 3, type='head') output_layers.append(pan_down_output_layer) return output_layers