Ejemplo n.º 1
0
def get_resnet_model(save_path, model_res=1024, image_size=256, depth=2, size=0, activation='elu', loss='logcosh', optimizer='adam'):
    # Build model
    if os.path.exists(save_path):
        print('Loading model')
        return load_model(save_path)

    print('Building model')
    model_scale = int(2*(math.log(model_res,2)-1)) # For example, 1024 -> 18

    if size <= 0:
        from keras.applications.resnet50 import ResNet50
        resnet = ResNet50(include_top=False, pooling=None, weights='imagenet', input_shape=(image_size, image_size, 3))
    else:
        from keras_applications.resnet_v2 import ResNet50V2, ResNet101V2, ResNet152V2
    if size == 1:
        resnet = ResNet50V2(include_top=False, pooling=None, weights='imagenet', input_shape=(image_size, image_size, 3), backend = keras.backend, layers = keras.layers, models = keras.models, utils = keras.utils)
    if size == 2:
        resnet = ResNet101V2(include_top=False, pooling=None, weights='imagenet', input_shape=(image_size, image_size, 3), backend = keras.backend, layers = keras.layers, models = keras.models, utils = keras.utils)
    if size >= 3:
        resnet = ResNet152V2(include_top=False, pooling=None, weights='imagenet', input_shape=(image_size, image_size, 3), backend = keras.backend, layers = keras.layers, models = keras.models, utils = keras.utils)

    layer_size = model_scale*8*8*8
    if is_square(layer_size): # work out layer dimensions
        layer_l = int(math.sqrt(layer_size)+0.5)
        layer_r = layer_l
    else:
        layer_m = math.log(math.sqrt(layer_size),2)
        layer_l = 2**math.ceil(layer_m)
        layer_r = layer_size // layer_l
    layer_l = int(layer_l)
    layer_r = int(layer_r)

    x_init = None
    inp = Input(shape=(image_size, image_size, 3))
    x = resnet(inp)

    if (depth < 0):
        depth = 1

    if (size <= 1):
        if (size <= 0):
            x = Conv2D(model_scale*8, 1, activation=activation)(x) # scale down
            x = Reshape((layer_r, layer_l))(x)
        else:
            x = Conv2D(model_scale*8*4, 1, activation=activation)(x) # scale down a little
            x = Reshape((layer_r*2, layer_l*2))(x)
    else:
        if (size == 2):
            x = Conv2D(1024, 1, activation=activation)(x) # scale down a bit
            x = Reshape((256, 256))(x)
        else:
            x = Reshape((256, 512))(x) # all weights used

    while (depth > 0): # See https://github.com/OliverRichter/TreeConnect/blob/master/cifar.py - TreeConnect inspired layers instead of dense layers.
        x = LocallyConnected1D(layer_r, 1, activation=activation)(x)
        x = Permute((2, 1))(x)
        x = LocallyConnected1D(layer_l, 1, activation=activation)(x)
        x = Permute((2, 1))(x)
        if x_init is not None:
            x = Add()([x, x_init])   # add skip connection
        x_init = x
        depth-=1

    x = Reshape((model_scale, 512))(x) # train against all dlatent values
    model = Model(inputs=inp,outputs=x)
    model.compile(loss=loss, metrics=[], optimizer=optimizer) # By default: adam optimizer, logcosh used for loss.
    return model
Ejemplo n.º 2
0
def load_backbone(backbone_type="resnet50",
                  backbone_outputs=('C3', 'C4', 'C5', 'P6', 'P7'),
                  num_features=256):
    global BACKBONE_LAYERS
    inputs = Input((None, None, 3), name='images')
    if backbone_type.lower() == 'resnet50':
        preprocess = BackBonePreProcess(rgb=False,
                                        mean_shift=True,
                                        normalize=0)(inputs)
        model = ResNet50(input_tensor=preprocess, include_top=False)
    elif backbone_type.lower() == 'resnet50v2':
        preprocess = BackBonePreProcess(rgb=True, mean_shift=True,
                                        normalize=2)(inputs)
        resnet50v2, _ = Classifiers.get('resnet50v2')
        model = resnet50v2(input_tensor=preprocess,
                           include_top=False,
                           weights='imagenet')
    elif backbone_type.lower() == "resnet101v2":
        preprocess = BackBonePreProcess(rgb=True,
                                        mean_shift=False,
                                        normalize=2)(inputs)
        model = ResNet101V2(input_tensor=preprocess,
                            include_top=False,
                            backend=tf.keras.backend,
                            layers=tf.keras.layers,
                            models=tf.keras.models,
                            utils=tf.keras.utils)
    elif backbone_type.lower() == 'resnext50':
        preprocess = BackBonePreProcess(rgb=True, mean_shift=True,
                                        normalize=2)(inputs)
        model = ResNeXt50(input_tensor=preprocess, include_top=False)
    elif backbone_type.lower() == "seresnet50":
        preprocess = BackBonePreProcess(rgb=True, mean_shift=True,
                                        normalize=3)(inputs)
        seresnet50, _ = Classifiers.get('seresnet50')
        model = seresnet50(input_tensor=preprocess,
                           original_input=inputs,
                           include_top=False,
                           weights='imagenet')
    elif backbone_type.lower() == "seresnet34":
        preprocess = BackBonePreProcess(rgb=True,
                                        mean_shift=False,
                                        normalize=0)(inputs)
        seresnet34, _ = Classifiers.get('seresnet34')
        model = seresnet34(input_tensor=preprocess,
                           original_input=inputs,
                           include_top=False,
                           weights='imagenet')
    elif backbone_type.lower() == "seresnext50":
        preprocess = BackBonePreProcess(rgb=True, mean_shift=True,
                                        normalize=3)(inputs)
        seresnext50, _ = Classifiers.get('seresnext50')
        model = seresnext50(input_tensor=preprocess,
                            original_input=inputs,
                            include_top=False,
                            weights='imagenet')
    elif backbone_type.lower() == "vgg16":
        preprocess = BackBonePreProcess(rgb=False,
                                        mean_shift=True,
                                        normalize=0)(inputs)
        model = VGG16(input_tensor=preprocess, include_top=False)
    elif backbone_type.lower() == "mobilenet":
        preprocess = BackBonePreProcess(rgb=False,
                                        mean_shift=False,
                                        normalize=2)(inputs)
        model = MobileNet(input_tensor=preprocess,
                          include_top=False,
                          alpha=1.0)
    elif backbone_type.lower() == 'efficientnetb2':
        preprocess = BackBonePreProcess(rgb=True, mean_shift=True,
                                        normalize=3)(inputs)
        model = efn.EfficientNetB2(input_tensor=preprocess,
                                   include_top=False,
                                   weights='imagenet')
    elif backbone_type.lower() == 'efficientnetb3':
        preprocess = BackBonePreProcess(rgb=True, mean_shift=True,
                                        normalize=3)(inputs)
        model = efn.EfficientNetB3(input_tensor=preprocess,
                                   include_top=False,
                                   weights='imagenet')
    elif backbone_type.lower() == 'efficientnetb4':
        preprocess = BackBonePreProcess(rgb=True, mean_shift=True,
                                        normalize=3)(inputs)
        model = efn.EfficientNetB4(input_tensor=preprocess,
                                   include_top=False,
                                   weights='imagenet')
    else:
        raise NotImplementedError(
            f"backbone_type은 {BACKBONE_LAYERS.keys()} 중에서 하나가 되어야 합니다.")
    model.trainable = False

    # Block Layer 가져오기
    features = []
    for key, layer_name in BACKBONE_LAYERS[backbone_type.lower()].items():
        if key in backbone_outputs:
            layer_tensor = model.get_layer(layer_name).output
            features.append(Identity(name=key)(layer_tensor))

    if backbone_type.lower() == "mobilenet":
        # Extra Layer for Feature Extracting
        Z6 = ZeroPadding2D(((0, 1), (0, 1)),
                           name=f'P6_zeropadding')(features[-1])
        P6 = Conv2D(num_features, (3, 3),
                    strides=(2, 2),
                    padding='valid',
                    activation='relu',
                    name=f'P6_conv')(Z6)
        if 'P6' in backbone_outputs:
            features.append(Identity(name='P6')(P6))
        G6 = GroupNormalization(name=f'P6_norm')(P6)
        Z7 = ZeroPadding2D(((0, 1), (0, 1)), name=f'P7_zeropadding')(G6)
        P7 = Conv2D(num_features, (3, 3),
                    strides=(2, 2),
                    padding='valid',
                    activation='relu',
                    name=f'P7_conv')(Z7)
        if 'P7' in backbone_outputs:
            features.append(Identity(name=f'P7')(P7))
    else:
        P6 = Conv2D(num_features, (3, 3),
                    strides=(2, 2),
                    padding='same',
                    activation='relu',
                    name=f'P6_conv')(features[-1])
        if 'P6' in backbone_outputs:
            features.append(Identity(name=f'P6')(P6))
        G6 = GroupNormalization(name=f'P6_norm')(P6)
        P7 = Conv2D(num_features, (3, 3),
                    strides=(2, 2),
                    padding='same',
                    activation='relu',
                    name=f'P7_conv')(G6)
        if 'P7' in backbone_outputs:
            features.append(Identity(name=f'P7')(P7))

    return Model(inputs, features, name=backbone_type)