Exemplo n.º 1
0
def yolo_body(inputs, num_anchors, num_classes):
    """Create YOLO_V2 model CNN body in Keras."""
    darknet = Model(inputs, darknet_body()(inputs))
    conv20 = compose(DarknetConv2D_BN_Leaky(1024, (3, 3)),
                     DarknetConv2D_BN_Leaky(1024, (3, 3)))(darknet.output)

    conv13 = darknet.layers[43].output
    conv21 = DarknetConv2D_BN_Leaky(64, (1, 1))(conv13)
    # TODO: Allow Keras Lambda to use func arguments for output_shape?
    conv21_reshaped = Lambda(space_to_depth_x2,
                             output_shape=space_to_depth_x2_output_shape,
                             name='space_to_depth')(conv21)

    x = concatenate([conv21_reshaped, conv20])
    x = DarknetConv2D_BN_Leaky(1024, (3, 3))(x)
    x = DarknetConv2D(num_anchors * (num_classes + 5), (1, 1))(x)
    return Model(inputs, x)
Exemplo n.º 2
0
def yolo_body(inputs, num_anchors, num_classes):
    darknet = Model(inputs, darknet_body()(inputs))
    conv20 = compose(
        DarknetConv2D_BN_Leaky(1024, (3, 3)),
        DarknetConv2D_BN_Leaky(1024, (3, 3))
    )(darknet.output)

    conv13 = darknet.layers[43].output
    conv21 = DarknetConv2D_BN_Leaky(64, (1, 1))(conv13)

    conv21_reshaped = Lambda(
        space_to_depth_x2,
        output_shape=space_to_depth_X2_output_shape,
        name='space_to_depth'
    )(conv21)

    x = concatenate([conv21_reshaped, conv20])
    x = DarknetConv2D_BN_Leaky(1024, (3, 3))(x)
    x = DarknetConv2D(num_anchors * (num_classes + 5), (1, 1))(x)

    return Model(inputs, x)
inputs = Input(shape=input_shape)
x = Cropping2D(cropping=((CROP_TOP, CROP_BOTTOM), (0, 0)),
               input_shape=input_shape)(inputs)
x = GaussianNoise(0.5)(x)
x = DarknetConv2D_BN_Leaky(32, (3, 3))(x)
x = MaxPooling2D()(x)
x = DarknetConv2D_BN_Leaky(64, (3, 3))(x)
x = MaxPooling2D()(x)
x = bottleneck_block(128, 64)(x)
x = MaxPooling2D()(x)
x = bottleneck_block(256, 128)(x)
x = MaxPooling2D()(x)
x = bottleneck_x2_block(512, 256)(x)
x = MaxPooling2D()(x)
x = bottleneck_x2_block(1024, 512)(x)
x = DarknetConv2D(1000, (1, 1), activation='softmax')(x)
x = GlobalAveragePooling2D()(x)
x = Dense(3, activation='softmax')(x)
model = Model(inputs, x)

#model = load_model("models/weights.358-3.57-0.64.hdf5")

# In[86]:

#model=darknet()
sgd = optimizers.SGD(lr=0.00001, decay=1e-6, momentum=0.9, nesterov=True)
adam = optimizers.Adam(lr=0.00001, decay=1e-6)

model.compile(loss='categorical_crossentropy',
              optimizer=adam,
              metrics=['accuracy'])