def darknet_yolo_body(inputs, num_anchors, num_classes): """Create YOLO_V3 model CNN body in Keras.""" if not hasattr(inputs, '_keras_history'): inputs = tf.keras.layers.Input(tensor=inputs) darknet = darknet_body(inputs, include_top=False) x, y1 = make_last_layers(darknet.output, 512, num_anchors * (num_classes + 5)) x = compose(DarknetConv2D_BN_Leaky(256, (1, 1)), tf.keras.layers.UpSampling2D(2))(x) x = tf.keras.layers.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)), tf.keras.layers.UpSampling2D(2))(x) x = tf.keras.layers.Concatenate()([x, darknet.layers[92].output]) x, y3 = make_last_layers(x, 128, num_anchors * (num_classes + 5)) y1 = tf.keras.layers.Lambda(lambda y: tf.reshape(y, [ -1, tf.shape(y)[1], tf.shape(y)[2], num_anchors, num_classes + 5 ]), name='y1')(y1) y2 = tf.keras.layers.Lambda(lambda y: tf.reshape(y, [ -1, tf.shape(y)[1], tf.shape(y)[2], num_anchors, num_classes + 5 ]), name='y2')(y2) y3 = tf.keras.layers.Lambda(lambda y: tf.reshape(y, [ -1, tf.shape(y)[1], tf.shape(y)[2], num_anchors, num_classes + 5 ]), name='y3')(y3) return AdvLossModel(inputs, [y1, y2, y3])
def make_last_layers(x, num_filters, out_filters): '''6 Conv2D_BN_Leaky layers followed by a Conv2D_linear layer''' x = compose(DarknetConv2D_BN_Leaky(num_filters, (1, 1)), DarknetConv2D_BN_Leaky(num_filters * 2, (3, 3)), DarknetConv2D_BN_Leaky(num_filters, (1, 1)), DarknetConv2D_BN_Leaky(num_filters * 2, (3, 3)), DarknetConv2D_BN_Leaky(num_filters, (1, 1)))(x) y = compose(DarknetConv2D_BN_Leaky(num_filters * 2, (3, 3)), DarknetConv2D(out_filters, (1, 1)))(x) return x, y