コード例 #1
0
def create_efficientnetb0(height,
                          width,
                          pretrained: bool,
                          mode: XModelMode = XModelMode.SIMPLE):
    shape = (height, width, 3)

    efficientnet.layers.BatchNormalization = BatchNormalization
    base_model = efficientnet.EfficientNetB0(input_shape=shape,
                                             include_top=False,
                                             weights=None)

    if pretrained:
        base_model.load_weights(load_weights("imagenet",
                                             model_name="efficientnetb0"),
                                by_name=True,
                                skip_mismatch=True)
        print("\033[31m", "Imagenet model loaded", "\033[0m")
    else:
        print("\033[31m", "Imagenet model not loaded", "\033[0m")

    inputs = tf.keras.Input(shape=shape, name="input")
    base_model, features = upsample(
        base_model,
        inputs,
        [
            "block2b_activation", "block3b_activation", "block5c_activation",
            "top_activation"
        ],  # 144, 240, 672, 1280
        # ["block2b_add", "block3b_add", "block5c_add", "block6d_add"], # 24, 40, 112, 192
        # resnet50: 256, 512, 1024, 2048
        mode,
    )
    return base_model, inputs, features
コード例 #2
0
def create_efficientnetb2(height, width, pretrained: bool, mode: XModelMode = XModelMode.SIMPLE):
    shape = (height, width, 3)

    tf.keras.layers.BatchNormalization = BatchNormalization
    base_model = EfficientNetB2(input_shape=shape, include_top=False, weights="imagenet" if pretrained else None)

    inputs = tf.keras.Input(shape=shape, name="input")
    base_model, features = upsample(
        base_model, inputs, ["block2c_add", "block3c_add", "block5d_add", "block6e_add"], mode
    )
    return base_model, inputs, features
コード例 #3
0
def create_efficientnetb0(height, width, pretrained: bool, mode: XModelMode = XModelMode.SIMPLE):
    shape = (height, width, 3)

    tf.keras.layers.BatchNormalization = BatchNormalization
    base_model = EfficientNetB0(input_shape=shape, include_top=False, weights="imagenet" if pretrained else None)

    inputs = tf.keras.Input(shape=shape, name="input")
    base_model, features = upsample(
        base_model,
        inputs,
        ["block2b_activation", "block3b_activation", "block5c_activation", "top_activation"],  # 144, 240, 672, 1280
        # ["block2b_add", "block3b_add", "block5c_add", "block6d_add"], # 24, 40, 112, 192
        # resnet50: 256, 512, 1024, 2048
        mode,
    )
    return base_model, inputs, features
コード例 #4
0
def create_efficientnetb2(height,
                          width,
                          pretrained: bool,
                          mode: XModelMode = XModelMode.SIMPLE):
    shape = (height, width, 3)
    base_model = efficientnet.EfficientNetB2(input_shape=shape,
                                             include_top=False,
                                             weights=None)

    if pretrained:
        base_model.load_weights(load_weights("imagenet",
                                             model_name="efficientnetb2"),
                                by_name=True,
                                skip_mismatch=True)

    inputs = tf.keras.Input(shape=shape, name="input")
    base_model, features = upsample(
        base_model, inputs,
        ["block2c_add", "block3c_add", "block5d_add", "block6e_add"], mode)
    return base_model, inputs, features
コード例 #5
0
def create_resnet_18(height,
                     width,
                     pretrained: bool,
                     mode: XModelMode = XModelMode.SIMPLE):
    shape = (height, width, 3)

    ResNet18, preprocess_input = Classifiers.get("resnet18")
    weights = "imagenet" if pretrained else None
    base_model = ResNet18(input_shape=shape,
                          weights=weights,
                          include_top=False)
    base_model = set_regularization(
        base_model, kernel_regularizer=tf.keras.regularizers.l2(L2_REG))

    inputs = tf.keras.Input(shape=shape, name="input")
    base_model, features = upsample(base_model, inputs, [
        "stage2_unit1_relu1", "stage3_unit1_relu1", "stage4_unit1_relu1",
        "relu1"
    ], mode)

    return base_model, inputs, features
コード例 #6
0
def create_resnet(height,
                  width,
                  pretrained: bool,
                  mode: XModelMode = XModelMode.SIMPLE):
    shape = (height, width, 3)

    tf.keras.layers.BatchNormalization = BatchNormalization
    base_model = tf.keras.applications.resnet50.ResNet50(
        input_shape=shape,
        include_top=False,
        weights="imagenet" if pretrained else None,
        layers=tf.keras.layers)

    inputs = tf.keras.Input(shape=shape, name="input")
    x = PreprocessCaffeLayer()(inputs)

    base_model, features = upsample(base_model, x, [
        "conv2_block3_out", "conv3_block3_out", "conv4_block3_out",
        "conv5_block3_out"
    ], mode)

    return base_model, inputs, features