def yolo_body(inputs, num_anchors, num_classes): #net, endpoint = inception_v2.inception_v2(inputs) mobilenet = MobileNetV2(input_tensor=inputs, weights='imagenet', include_top=False) # input: 416 x 416 x 3 # conv_pw_13_relu :13 x 13 x 1024 # conv_pw_11_relu :26 x 26 x 512 # conv_pw_5_relu : 52 x 52 x 256 f1 = mobilenet.get_layer('out_relu').output # f1 :13 x 13 x 1024 x, y1 = make_last_layers(f1, 512, num_anchors * (num_classes + 5)) x = compose(DarknetConv2D_BN_Leaky(256, (1, 1)), UpSampling2D(2))(x) f2 = mobilenet.get_layer('block_13_expand_relu').output # f2: 26 x 26 x 512 x = Concatenate()([x, f2]) x, y2 = make_last_layers(x, 256, num_anchors * (num_classes + 5)) x = compose(DarknetConv2D_BN_Leaky(128, (1, 1)), UpSampling2D(2))(x) f3 = mobilenet.get_layer('block_6_expand_relu').output # f3 : 52 x 52 x 256 x = Concatenate()([x, f3]) x, y3 = make_last_layers(x, 128, num_anchors * (num_classes + 5)) return Model(inputs=inputs, outputs=[y1, y2, y3])
def yolo_body(inputs, num_anchors, num_classes): #net, endpoint = inception_v2.inception_v2(inputs) densenet = DenseNet121( input_tensor=inputs, weights='imagenet') #include top can be added but will not change much # input: 416 x 416 x 3 # conv_pw_13_relu :13 x 13 x 1024 # conv_pw_11_relu :26 x 26 x 1024 # conv_pw_5_relu : 52 x 52 x 512 f1 = densenet.get_layer('relu').output # f1 :13 x 13 x 1024 x, y1 = make_last_layers(f1, 512, num_anchors * (num_classes + 5)) x = compose(DarknetConv2D_BN_Leaky(256, (1, 1)), UpSampling2D(2))(x) f2 = densenet.get_layer('pool4_relu').output # f2: 26 x 26 x 1024 // 512 x = Concatenate()([x, f2]) x, y2 = make_last_layers(x, 256, num_anchors * (num_classes + 5)) x = compose(DarknetConv2D_BN_Leaky(128, (1, 1)), UpSampling2D(2))(x) f3 = densenet.get_layer('pool3_relu').output # f3 : 52 x 52 x 512 // 256 x = Concatenate()([x, f3]) x, y3 = make_last_layers(x, 128, num_anchors * (num_classes + 5)) return Model(inputs=inputs, outputs=[y1, y2, y3])
def resblock_body(x, num_filters, num_blocks): '''A series of resblocks starting with a downsampling Convolution2D''' # Darknet uses left and top padding instead of 'same' mode x = ZeroPadding2D(((1, 0), (1, 0)))(x) x = DarknetConv2D_BN_Leaky(num_filters, (3, 3), strides=(2, 2))(x) for i in range(num_blocks): y = compose(DarknetConv2D_BN_Leaky(num_filters // 2, (1, 1)), DarknetConv2D_BN_Leaky(num_filters, (3, 3)))(x) x = Add()([x, y]) return x
def yolo_body(inputs, num_anchors, num_classes): """Create YOLO_V3 model CNN body in Keras.""" darknet = Model(inputs, darknet_body(inputs)) x, y1 = make_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 darknet_body(x): '''Darknent body having 52 Convolution2D layers''' x = DarknetConv2D_BN_Leaky(32, (3, 3))(x) x = resblock_body(x, 64, 1) x = resblock_body(x, 128, 2) x = resblock_body(x, 256, 8) x = resblock_body(x, 512, 8) x = resblock_body(x, 1024, 4) return x
def tiny_yolo_body(inputs, num_anchors, num_classes): '''Create Tiny YOLO_v3 model CNN body in keras.''' x1 = compose( DarknetConv2D_BN_Leaky(16, (3, 3)), MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='same'), DarknetConv2D_BN_Leaky(32, (3, 3)), MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='same'), DarknetConv2D_BN_Leaky(64, (3, 3)), MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='same'), DarknetConv2D_BN_Leaky(128, (3, 3)), MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='same'), DarknetConv2D_BN_Leaky(256, (3, 3)))(inputs) x2 = compose( MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='same'), DarknetConv2D_BN_Leaky(512, (3, 3)), MaxPooling2D(pool_size=(2, 2), strides=(1, 1), padding='same'), DarknetConv2D_BN_Leaky(1024, (3, 3)), DarknetConv2D_BN_Leaky(256, (1, 1)))(x1) y1 = compose(DarknetConv2D_BN_Leaky(512, (3, 3)), DarknetConv2D(num_anchors * (num_classes + 5), (1, 1)))(x2) x2 = compose(DarknetConv2D_BN_Leaky(128, (1, 1)), UpSampling2D(2))(x2) y2 = compose(Concatenate(), DarknetConv2D_BN_Leaky(256, (3, 3)), DarknetConv2D(num_anchors * (num_classes + 5), (1, 1)))([x2, x1]) return Model(inputs, [y1, y2])