示例#1
0
def buildin_models(name,
                   dropout=1,
                   emb_shape=512,
                   input_shape=(112, 112, 3),
                   output_layer="GDC",
                   bn_momentum=0.99,
                   bn_epsilon=0.001,
                   add_pointwise_conv=False,
                   **kwargs):
    name_lower = name.lower()
    """ Basic model """
    if name_lower == "mobilenet":
        xx = keras.applications.MobileNet(input_shape=input_shape,
                                          include_top=False,
                                          weights="imagenet",
                                          **kwargs)
    elif name_lower == "mobilenetv2":
        xx = keras.applications.MobileNetV2(input_shape=input_shape,
                                            include_top=False,
                                            weights="imagenet",
                                            **kwargs)
    elif name_lower == "r34" or name_lower == "r50" or name_lower == "r100" or name_lower == "r101":
        from backbones import resnet  # MXNet insightface version resnet

        model_name = "ResNet" + name_lower[1:]
        model_class = getattr(resnet, model_name)
        xx = model_class(input_shape=input_shape,
                         include_top=False,
                         weights=None,
                         **kwargs)
    elif name_lower.startswith("resnet"):  # keras.applications.ResNetxxx
        if name_lower.endswith("v2"):
            model_name = "ResNet" + name_lower[len("resnet"):-2] + "V2"
        else:
            model_name = "ResNet" + name_lower[len("resnet"):]
        model_class = getattr(keras.applications, model_name)
        xx = model_class(weights="imagenet",
                         include_top=False,
                         input_shape=input_shape,
                         **kwargs)
    elif name_lower.startswith("efficientnet"):
        # import tensorflow.keras.applications.efficientnet as efficientnet
        from backbones import efficientnet

        model_name = "EfficientNet" + name_lower[-2:].upper()
        model_class = getattr(efficientnet, model_name)
        xx = model_class(weights="imagenet",
                         include_top=False,
                         input_shape=input_shape,
                         **kwargs)  # or weights='imagenet'
    elif name_lower.startswith("se_resnext"):
        from keras_squeeze_excite_network import se_resnext

        if name_lower.endswith("101"):  # se_resnext101
            depth = [3, 4, 23, 3]
        else:  # se_resnext50
            depth = [3, 4, 6, 3]
        xx = se_resnext.SEResNextImageNet(weights="imagenet",
                                          input_shape=input_shape,
                                          include_top=False,
                                          depth=depth)
    elif name_lower.startswith("resnest"):
        from backbones import resnest

        if name_lower == "resnest50":
            xx = resnest.ResNest50(input_shape=input_shape)
        else:
            xx = resnest.ResNest101(input_shape=input_shape)
    elif name_lower.startswith("mobilenetv3"):
        from backbones import mobilenet_v3
        model_class = mobilenet_v3.MobileNetV3Small if "small" in name_lower else mobilenet_v3.MobileNetV3Large
        # from tensorflow.keras.layers.experimental.preprocessing import Rescaling
        # model_class = keras.applications.MobileNetV3Small if "small" in name_lower else keras.applications.MobileNetV3Large
        xx = model_class(input_shape=input_shape,
                         include_top=False,
                         weights="imagenet")
        # xx = keras.models.clone_model(xx, clone_function=lambda layer: Rescaling(1.) if isinstance(layer, Rescaling) else layer)
    elif "mobilefacenet" in name_lower or "mobile_facenet" in name_lower:
        from backbones import mobile_facenet

        use_se = True if "se" in name_lower else False
        xx = mobile_facenet.mobile_facenet(input_shape=input_shape,
                                           include_top=False,
                                           name=name,
                                           use_se=use_se)
    elif name_lower == "ghostnet":
        from backbones import ghost_model

        xx = ghost_model.GhostNet(input_shape=input_shape,
                                  include_top=False,
                                  width=1.3)
    elif hasattr(keras.applications, name):
        model_class = getattr(keras.applications, name)
        xx = model_class(weights="imagenet",
                         include_top=False,
                         input_shape=input_shape)
    else:
        return None
    xx.trainable = True

    inputs = xx.inputs[0]
    nn = xx.outputs[0]

    if add_pointwise_conv:  # Model using `pointwise_conv + GDC` / `pointwise_conv + E` is smaller than `E`
        nn = keras.layers.Conv2D(512, 1, use_bias=False, padding="same")(nn)
        nn = keras.layers.BatchNormalization(momentum=bn_momentum,
                                             epsilon=bn_epsilon)(nn)
        nn = keras.layers.PReLU(shared_axes=[1, 2])(nn)

    if output_layer == "E":
        """ Fully Connected """
        nn = keras.layers.BatchNormalization(momentum=bn_momentum,
                                             epsilon=bn_epsilon)(nn)
        if dropout > 0 and dropout < 1:
            nn = keras.layers.Dropout(dropout)(nn)
        nn = keras.layers.Flatten()(nn)
        nn = keras.layers.Dense(emb_shape,
                                activation=None,
                                use_bias=True,
                                kernel_initializer="glorot_normal")(nn)
    else:
        """ GDC """
        nn = keras.layers.DepthwiseConv2D(int(nn.shape[1]),
                                          depth_multiplier=1,
                                          use_bias=False)(nn)
        nn = keras.layers.BatchNormalization(momentum=bn_momentum,
                                             epsilon=bn_epsilon)(nn)
        if dropout > 0 and dropout < 1:
            nn = keras.layers.Dropout(dropout)(nn)
        nn = keras.layers.Conv2D(emb_shape,
                                 1,
                                 use_bias=True,
                                 activation=None,
                                 kernel_initializer="glorot_normal")(nn)
        nn = keras.layers.Flatten()(nn)
        # nn = keras.layers.Dense(emb_shape, activation=None, use_bias=True, kernel_initializer="glorot_normal")(nn)

    # `fix_gamma=True` in MXNet means `scale=False` in Keras
    embedding = keras.layers.BatchNormalization(momentum=bn_momentum,
                                                epsilon=bn_epsilon,
                                                name="embedding",
                                                scale=False)(nn)
    basic_model = keras.models.Model(inputs, embedding, name=xx.name)
    return basic_model
示例#2
0
def buildin_models(name,
                   dropout=1,
                   emb_shape=512,
                   input_shape=(112, 112, 3),
                   **kwargs):
    name = name.lower()
    """ Basic model """
    if name == "mobilenet":
        xx = keras.applications.MobileNet(input_shape=input_shape,
                                          include_top=False,
                                          weights=None,
                                          **kwargs)
    elif name == "mobilenetv2":
        xx = keras.applications.MobileNetV2(input_shape=input_shape,
                                            include_top=False,
                                            weights=None,
                                            **kwargs)
    elif name == "resnet50v2":
        xx = keras.applications.ResNet50V2(input_shape=input_shape,
                                           include_top=False,
                                           weights="imagenet",
                                           **kwargs)
    elif name == "resnet101v2":
        xx = keras.applications.ResNet101V2(input_shape=input_shape,
                                            include_top=False,
                                            weights="imagenet",
                                            **kwargs)
    elif name == "nasnetmobile":
        xx = keras.applications.NASNetMobile(input_shape=input_shape,
                                             include_top=False,
                                             weights=None,
                                             **kwargs)
    elif name.startswith("efficientnet"):
        if "-dev" in tf.__version__:
            # import tensorflow.keras.applications.efficientnet as efntf
            import efficientnet.tfkeras as efntf
        else:
            import efficientnet.tfkeras as efntf

        if name[-2] == "b":
            compound_scale = int(name[-1])
            models = [
                efntf.EfficientNetB0,
                efntf.EfficientNetB1,
                efntf.EfficientNetB2,
                efntf.EfficientNetB3,
                efntf.EfficientNetB4,
                efntf.EfficientNetB5,
                efntf.EfficientNetB6,
                efntf.EfficientNetB7,
            ]
            model = models[compound_scale]
        else:
            model = efntf.EfficientNetL2
        xx = model(weights="noisy-student",
                   include_top=False,
                   input_shape=input_shape)  # or weights='imagenet'
    elif name.startswith("se_resnext"):
        from keras_squeeze_excite_network import se_resnext

        if name.endswith("101"):  # se_resnext101
            depth = [3, 4, 23, 3]
        else:  # se_resnext50
            depth = [3, 4, 6, 3]
        xx = se_resnext.SEResNextImageNet(weights="imagenet",
                                          input_shape=input_shape,
                                          include_top=False,
                                          depth=depth)
    elif name.startswith("resnest"):
        from backbones import resnest

        if name == "resnest50":
            xx = resnest.ResNest50(input_shape=input_shape)
        else:
            xx = resnest.ResNest101(input_shape=input_shape)
    elif name.startswith("mobilenetv3"):
        from backbones import mobilenetv3

        size = 'small' if 'small' in name else 'large'
        xx = mobilenetv3.MobilenetV3(input_shape=input_shape,
                                     include_top=False,
                                     size=size)
    elif "mobilefacenet" in name or "mobile_facenet" in name:
        from backbones import mobile_facenet

        use_se = True if 'se' in name else False
        xx = mobile_facenet.mobile_facenet(input_shape=input_shape,
                                           include_top=False,
                                           name=name,
                                           use_se=use_se)
    else:
        return None
    # xx = keras.models.load_model('checkpoints/mobilnet_v1_basic_922667.h5', compile=False)
    xx.trainable = True

    inputs = xx.inputs[0]
    nn = xx.outputs[0]
    # nn = keras.layers.Conv2D(emb_shape, xx.output_shape[1], use_bias=False)(nn)
    """ GDC """
    nn = keras.layers.Conv2D(512, 1, use_bias=False)(nn)
    nn = keras.layers.BatchNormalization()(nn)
    nn = keras.layers.PReLU(shared_axes=[1, 2])(nn)
    nn = keras.layers.DepthwiseConv2D(int(nn.shape[1]),
                                      depth_multiplier=1,
                                      use_bias=False)(nn)
    nn = keras.layers.BatchNormalization()(nn)
    nn = keras.layers.Conv2D(emb_shape, 1, use_bias=False, activation=None)(nn)
    if dropout > 0 and dropout < 1:
        nn = keras.layers.Dropout(dropout)(nn)
    nn = keras.layers.Flatten()(nn)
    # nn = keras.layers.Dense(emb_shape, activation=None, use_bias=False, kernel_initializer="glorot_normal")(nn)
    embedding = keras.layers.BatchNormalization(name="embedding")(nn)
    # norm_emb = layers.Lambda(tf.nn.l2_normalize, name='norm_embedding', arguments={'axis': 1})(embedding)
    basic_model = keras.models.Model(inputs, embedding, name=xx.name)
    return basic_model
示例#3
0
def buildin_models(name,
                   dropout=1,
                   emb_shape=512,
                   input_shape=(112, 112, 3),
                   output_layer="GDC",
                   bn_momentum=0.99,
                   bn_epsilon=0.001,
                   **kwargs):
    name = name.lower()
    """ Basic model """
    if name == "mobilenet":
        xx = keras.applications.MobileNet(input_shape=input_shape,
                                          include_top=False,
                                          weights="imagenet",
                                          **kwargs)
    elif name == "mobilenetv2":
        xx = keras.applications.MobileNetV2(input_shape=input_shape,
                                            include_top=False,
                                            weights="imagenet",
                                            **kwargs)
    elif name == "resnet34":
        from backbones import resnet

        xx = resnet.ResNet34(input_shape=input_shape,
                             include_top=False,
                             weights=None,
                             **kwargs)
    elif name == "r50":
        from backbones import resnet

        xx = resnet.ResNet50(input_shape=input_shape,
                             include_top=False,
                             weights=None,
                             **kwargs)
    elif name == "resnet50":
        xx = keras.applications.ResNet50(input_shape=input_shape,
                                         include_top=False,
                                         weights="imagenet",
                                         **kwargs)
    elif name == "resnet50v2":
        xx = keras.applications.ResNet50V2(input_shape=input_shape,
                                           include_top=False,
                                           weights="imagenet",
                                           **kwargs)
    elif name == "resnet101":
        # xx = ResNet101(input_shape=input_shape, include_top=False, weights=None, **kwargs)
        xx = keras.applications.ResNet101(input_shape=input_shape,
                                          include_top=False,
                                          weights="imagenet",
                                          **kwargs)
    elif name == "resnet101v2":
        xx = keras.applications.ResNet101V2(input_shape=input_shape,
                                            include_top=False,
                                            weights="imagenet",
                                            **kwargs)
    elif name == "nasnetmobile":
        xx = keras.applications.NASNetMobile(input_shape=input_shape,
                                             include_top=False,
                                             weights=None,
                                             **kwargs)
    elif name.startswith("efficientnet"):
        if "-dev" in tf.__version__:
            # import tensorflow.keras.applications.efficientnet as efficientnet
            from backbones import efficientnet
        else:
            # import efficientnet.tfkeras as efficientnet
            from backbones import efficientnet

        if name[-2] == "b":
            compound_scale = int(name[-1])
            models = [
                efficientnet.EfficientNetB0,
                efficientnet.EfficientNetB1,
                efficientnet.EfficientNetB2,
                efficientnet.EfficientNetB3,
                efficientnet.EfficientNetB4,
                efficientnet.EfficientNetB5,
                efficientnet.EfficientNetB6,
                efficientnet.EfficientNetB7,
            ]
            model = models[compound_scale]
        else:
            model = efficientnet.EfficientNetL2
        xx = model(weights="imagenet",
                   include_top=False,
                   input_shape=input_shape)  # or weights='imagenet'
    elif name.startswith("se_resnext"):
        from keras_squeeze_excite_network import se_resnext

        if name.endswith("101"):  # se_resnext101
            depth = [3, 4, 23, 3]
        else:  # se_resnext50
            depth = [3, 4, 6, 3]
        xx = se_resnext.SEResNextImageNet(weights="imagenet",
                                          input_shape=input_shape,
                                          include_top=False,
                                          depth=depth)
    elif name.startswith("resnest"):
        from backbones import resnest

        if name == "resnest50":
            xx = resnest.ResNest50(input_shape=input_shape)
        else:
            xx = resnest.ResNest101(input_shape=input_shape)
    elif name.startswith("mobilenetv3"):
        from backbones import mobilenet_v3

        # size = "small" if "small" in name else "large"
        # xx = mobilenetv3.MobilenetV3(input_shape=input_shape, include_top=False, size=size)
        if "small" in name:
            xx = mobilenet_v3.MobileNetV3Small(input_shape=input_shape,
                                               include_top=False,
                                               weights="imagenet")
        else:
            xx = mobilenet_v3.MobileNetV3Large(input_shape=input_shape,
                                               include_top=False,
                                               weights="imagenet")
    elif "mobilefacenet" in name or "mobile_facenet" in name:
        from backbones import mobile_facenet

        use_se = True if "se" in name else False
        xx = mobile_facenet.mobile_facenet(input_shape=input_shape,
                                           include_top=False,
                                           name=name,
                                           use_se=use_se)
    else:
        return None
    # xx = keras.models.load_model('checkpoints/mobilnet_v1_basic_922667.h5', compile=False)
    xx.trainable = True

    inputs = xx.inputs[0]
    nn = xx.outputs[0]
    # nn = keras.layers.Conv2D(emb_shape, xx.output_shape[1], use_bias=False)(nn)

    if output_layer == "E":
        """ Fully Connected """
        nn = keras.layers.BatchNormalization(momentum=bn_momentum,
                                             epsilon=bn_epsilon)(nn)
        if dropout > 0 and dropout < 1:
            nn = keras.layers.Dropout(dropout)(nn)
        nn = keras.layers.Flatten()(nn)
        nn = keras.layers.Dense(emb_shape,
                                activation=None,
                                use_bias=True,
                                kernel_initializer="glorot_normal")(nn)
    else:
        """ GDC """
        # nn = keras.layers.Conv2D(512, 1, use_bias=False)(nn)
        # nn = keras.layers.BatchNormalization(momentum=bn_momentum, epsilon=bn_epsilon)(nn)
        # nn = keras.layers.PReLU(shared_axes=[1, 2])(nn)
        nn = keras.layers.DepthwiseConv2D(int(nn.shape[1]),
                                          depth_multiplier=1,
                                          use_bias=False)(nn)
        nn = keras.layers.BatchNormalization(momentum=bn_momentum,
                                             epsilon=bn_epsilon)(nn)
        if dropout > 0 and dropout < 1:
            nn = keras.layers.Dropout(dropout)(nn)
        nn = keras.layers.Conv2D(emb_shape,
                                 1,
                                 use_bias=False,
                                 activation=None,
                                 kernel_initializer="glorot_normal")(nn)
        nn = keras.layers.Flatten()(nn)
        # nn = keras.layers.Dense(emb_shape, activation=None, use_bias=True, kernel_initializer="glorot_normal")(nn)
    # `fix_gamma=True` in MXNet means `scale=False` in Keras
    embedding = keras.layers.BatchNormalization(momentum=bn_momentum,
                                                epsilon=bn_epsilon,
                                                name="embedding",
                                                scale=False)(nn)
    basic_model = keras.models.Model(inputs, embedding, name=xx.name)
    return basic_model
示例#4
0
def __init_model_from_name__(name, input_shape=(112, 112, 3), weights="imagenet", **kwargs):
    name_lower = name.lower()
    """ Basic model """
    if name_lower == "mobilenet":
        xx = keras.applications.MobileNet(input_shape=input_shape, include_top=False, weights=weights, **kwargs)
    elif name_lower == "mobilenet_m1":
        from backbones import mobilenet

        xx = mobilenet.MobileNet(input_shape=input_shape, include_top=False, weights=None, **kwargs)
    elif name_lower == "mobilenetv2":
        xx = keras.applications.MobileNetV2(input_shape=input_shape, include_top=False, weights=weights, **kwargs)
    elif name_lower == "r34" or name_lower == "r50" or name_lower == "r100" or name_lower == "r101":
        from backbones import resnet  # MXNet insightface version resnet

        model_name = "ResNet" + name_lower[1:]
        model_class = getattr(resnet, model_name)
        xx = model_class(input_shape=input_shape, include_top=False, weights=None, **kwargs)
    elif name_lower.startswith("resnet"):  # keras.applications.ResNetxxx
        if name_lower.endswith("v2"):
            model_name = "ResNet" + name_lower[len("resnet") : -2] + "V2"
        else:
            model_name = "ResNet" + name_lower[len("resnet") :]
        model_class = getattr(keras.applications, model_name)
        xx = model_class(weights=weights, include_top=False, input_shape=input_shape, **kwargs)
    elif name_lower.startswith("efficientnet"):
        # import tensorflow.keras.applications.efficientnet as efficientnet
        from backbones import efficientnet

        model_name = "EfficientNet" + name_lower[-2:].upper()
        model_class = getattr(efficientnet, model_name)
        xx = model_class(weights=weights, include_top=False, input_shape=input_shape, **kwargs)  # or weights='imagenet'
    elif name_lower.startswith("se_resnext"):
        from keras_squeeze_excite_network import se_resnext

        if name_lower.endswith("101"):  # se_resnext101
            depth = [3, 4, 23, 3]
        else:  # se_resnext50
            depth = [3, 4, 6, 3]
        xx = se_resnext.SEResNextImageNet(weights=weights, input_shape=input_shape, include_top=False, depth=depth)
    elif name_lower.startswith("resnest"):
        from backbones import resnest

        if name_lower == "resnest50":
            xx = resnest.ResNest50(input_shape=input_shape)
        else:
            xx = resnest.ResNest101(input_shape=input_shape)
    elif name_lower.startswith("mobilenetv3"):
        from backbones import mobilenet_v3

        # from backbones import mobilenetv3 as mobilenet_v3
        model_class = mobilenet_v3.MobileNetV3Small if "small" in name_lower else mobilenet_v3.MobileNetV3Large
        # from tensorflow.keras.layers.experimental.preprocessing import Rescaling
        # model_class = keras.applications.MobileNetV3Small if "small" in name_lower else keras.applications.MobileNetV3Large
        xx = model_class(input_shape=input_shape, include_top=False, weights=weights)
        # xx = keras.models.clone_model(xx, clone_function=lambda layer: Rescaling(1.) if isinstance(layer, Rescaling) else layer)
    elif "mobilefacenet" in name_lower or "mobile_facenet" in name_lower:
        from backbones import mobile_facenet

        use_se = True if "se" in name_lower else False
        xx = mobile_facenet.mobile_facenet(input_shape=input_shape, include_top=False, name=name, use_se=use_se)
    elif name_lower == "ghostnet":
        from backbones import ghost_model

        xx = ghost_model.GhostNet(input_shape=input_shape, include_top=False, width=1.3, **kwargs)
    elif name_lower.startswith("botnet"):
        from backbones import botnet

        model_name = "BotNet" + name_lower[len("botnet") :]
        model_class = getattr(botnet, model_name)
        xx = model_class(include_top=False, input_shape=input_shape, strides=1, **kwargs)
    elif hasattr(keras.applications, name):
        model_class = getattr(keras.applications, name)
        xx = model_class(weights=weights, include_top=False, input_shape=input_shape, **kwargs)
    else:
        return None
    xx.trainable = True
    return xx