예제 #1
0
def r34(input_shape=None, channels=2, lr=1e-4, weights=None, classes=4, **kwargs):

    input_shape_resnet = (None, None, 3) if K.image_data_format() == 'channels_last' else (3, None, None)
    resnet_model = ResNet34(input_shape=input_shape_resnet, include_top=False, weights='imagenet')
    resnet_model = Model(resnet_model.input, resnet_model.get_layer('stage4_unit1_relu1').output)

    if input_shape is None:
        if K.image_data_format() == 'channels_last':
            input_shape = (None, None, channels)
        else:
            input_shape = (channels, None, None)
    main_input = Input(input_shape)
    x = Convolution2D(3, (1, 1), kernel_initializer=kernel_initializer)(main_input)
    x = resnet_model(x)
    x = GlobalAveragePooling2D(name='pool1')(x)
    main_output = Dense(classes, activation='softmax', name='predictions')(x)

    # Create model.
    model = Model(main_input, main_output, name='r34')

    if weights is not None:
        print('Load weights from', weights)
        model.load_weights(weights)

    optimizer = SGD(lr, momentum=0.95, decay=0.0005, nesterov=True)
    model.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=['accuracy'])

    return model
예제 #2
0
def uResNet34(input_size: Tuple = None,
              weights: Path = None,
              n_carotage: int = None) -> Model:
    """Definition of the model structure with a ResNet-34 decoder"""
    if K.image_data_format() == 'channels_last':
        seismic_shape = input_size + (1, )
        resnet_shape = input_size + (3, )
    else:
        seismic_shape = (1, ) + input_size
        resnet_shape = (3, ) + input_size

    # Init Resnet Unet
    input_resnet = Input(shape=resnet_shape)
    model = ResNet34(input_shape=resnet_shape,
                     include_top=False,
                     weights='imagenet')
    model(input_resnet)

    stage_2 = model.get_layer('stage2_unit1_relu1').output  # 64
    stage_3 = model.get_layer('stage3_unit1_relu1').output  # 32
    stage_4 = model.get_layer('stage4_unit1_relu1').output  # 16
    stage_5 = model.get_layer('relu1').output  # 8

    x = decoder_block(stage_5, (256, 256), stage=5)
    x = decoder_block([x, stage_4], (128, 128), stage=4)
    x = decoder_block([x, stage_3], (64, 64), stage=3)
    x = decoder_block([x, stage_2], (64, 64), stage=2)

    x = decoder_block(x, (64, 32), stage=1)
    x = Conv2D(32, (3, 3), padding='same', kernel_initializer='he_normal')(x)
    x = Activation('elu')(x)
    x = Conv2D(n_carotage, (1, 1), kernel_initializer='he_normal')(x)

    model_resnet = Model(model.get_input_at(0), x, name='uResNet34')

    # Main model
    input_seismic = Input(shape=seismic_shape)
    input_mask = Input(shape=input_size + (n_carotage, ))

    x = Conv2D(3, (1, 1))(input_seismic)
    x = model_resnet(x)
    x = Concatenate()([x, input_mask])
    model = Model([input_seismic, input_mask], x, name='main')

    if weights is not None:
        print('Load weights from', weights)
        model.load_weights(weights, by_name=True)

    optimizer = Adam()
    # wrap the Keras.Optimizer with gradient accumulation of 16 steps
    optimizer = runai.ga.keras.optimizers.Optimizer(optimizer, steps=16)
    model.compile(loss=masked_mse,
                  optimizer=optimizer,
                  metrics=[masked_correlation])

    return model
 def _get_backbone_model(self):
     backbone_model = None
     if self.backbone_name == 'resnet18':
         backbone_model = ResNet18(input_shape=self.input_shape, include_top=False, weights='imagenet')
     if self.backbone_name == 'resnet34':
         backbone_model = ResNet34(input_shape=self.input_shape, include_top=False, weights='imagenet')
     elif self.backbone_name == 'mobilenetv2':
         backbone_model = MobileNetV2(input_shape=self.input_shape, include_top=False, weights='imagenet')
     assert backbone_model is not None, f'backbone should be one of {list(_FEATURE_LAYERS.keys())[:3]}'
     return backbone_model
 def _get_backbone_model(self):
     backbone_model = None
     if self.backbone_name == 'resnet18':
         backbone_model = ResNet18(input_shape=self.input_shape, include_top=False, weights='imagenet')
     if self.backbone_name == 'resnet34':
         backbone_model = ResNet34(input_shape=self.input_shape, include_top=False, weights='imagenet')
     elif self.backbone_name == 'mobilenetv2':
         backbone_model = MobileNetV2(input_shape=self.input_shape, include_top=False, weights='imagenet')
     elif self.backbone_name == 'efficientnetb0':
         backbone_model = EfficientNetB0(input_shape=self.input_shape, include_top=False, weights='imagenet')
     elif self.backbone_name == 'efficientnetb1':
         backbone_model = EfficientNetB1(input_shape=self.input_shape, include_top=False, weights='imagenet')
     elif self.backbone_name == 'efficientnetb3':
         backbone_model = EfficientNetB3(input_shape=self.input_shape, include_top=False, weights='imagenet')
     elif self.backbone_name == 'efficientnetb5':
         backbone_model = EfficientNetB5(input_shape=self.input_shape, include_top=False, weights='imagenet')
     elif self.backbone_name == 'densenet':
         backbone_model = DenseNet121(input_shape=self.input_shape, include_top=False, weights='imagenet')
     return backbone_model
def rn34_01():
    base = ResNet34(include_top=False,
                    weights='imagenet',
                    backend=ks.backend,
                    layers=ks.layers,
                    utils=ks.utils,
                    models=ks.models)
    base.layers.pop(0)
    inp = Input(shape=(config.img_size, config.img_size, config.channel))
    x = BatchNormalization()(inp)
    base_out = base(x)
    x = GlobalAveragePooling2D()(base_out)
    x = Dense(1280, activation='relu')(x)
    x = BatchNormalization()(x)
    x = Dense(128, activation='relu')(x)
    x = BatchNormalization()(x)
    x = Dense(1)(x)
    model = Model(inputs=[inp], outputs=x)

    return model
예제 #6
0
from loss import *

#To prepare data
data_foler = train_config.data_foler
data_train = pd.read_csv(train_config.train_csv_path)
data_val = pd.read_csv(train_config.val_csv_path)

#Prepare model
import keras
from keras.applications.nasnet import NASNetLarge, NASNetMobile
from keras.applications.xception import Xception
from keras.applications.densenet import DenseNet121
from classification_models.resnet import ResNet34

model = ResNet34(weights=train_config.encoder_weights,
                 include_top=train_config.include_top,
                 input_shape=(train_config.image_size * 2,
                              train_config.image_size * 2, 3))

from keras.models import Model


def get_model(model, numclasses):
    base_model = model
    x = base_model.output
    x = keras.layers.GlobalAveragePooling2D()(x)
    x = keras.layers.Dropout(train_config.dropout_rate)(x)  #
    predictions = keras.layers.Dense(numclasses, activation='sigmoid')(x)
    model = Model(inputs=base_model.input, outputs=predictions)
    # Train top layer
    for layer in base_model.layers:
        layer.trainable = train_config.base_model_layer_trainable
예제 #7
0
파일: resnet.py 프로젝트: DableUTeeF/algea
def resnet_retinanet(num_classes,
                     backbone='resnet50',
                     inputs=None,
                     modifier=None,
                     **kwargs):
    """ Constructs a retinanet model using a resnet backbone.

    Args
        num_classes: Number of classes to predict.
        backbone: Which backbone to use (one of ('resnet50', 'resnet101', 'resnet152')).
        inputs: The inputs to the network (defaults to a Tensor of shape (None, None, 3)).
        modifier: A function handler which can modify the backbone before using it in retinanet (this can be used to freeze backbone layers for example).

    Returns
        RetinaNet model with a ResNet backbone.
    """
    # choose default input
    if inputs is None:
        if keras.backend.image_data_format() == 'channels_first':
            inputs = keras.layers.Input(shape=(3, None, None))
        else:
            inputs = keras.layers.Input(shape=(None, None, 3))

    # create the resnet backbone
    if backbone == 'resnet50':
        resnet = keras_resnet.models.ResNet50(inputs,
                                              include_top=False,
                                              freeze_bn=True)
    elif backbone == 'resnet101':
        resnet = keras_resnet.models.ResNet101(inputs,
                                               include_top=False,
                                               freeze_bn=True)
    elif backbone == 'resnet152':
        resnet = keras_resnet.models.ResNet152(inputs,
                                               include_top=False,
                                               freeze_bn=True)
    elif backbone == 'resnet36':
        resnet = ResNet34(None,
                          input_tensor=inputs,
                          weights=None,
                          include_top=False)
        layer_names = ['stage3_unit1_relu1', 'stage4_unit1_relu1', 'relu1']
        layer_outputs = [resnet.get_layer(name).output for name in layer_names]
        resnet = keras.models.Model(inputs=inputs,
                                    outputs=layer_outputs,
                                    name=resnet.name)
        return retinanet.retinanet(inputs=inputs,
                                   num_classes=num_classes,
                                   backbone_layers=resnet.outputs,
                                   **kwargs)

    elif backbone == 'resnet18':
        resnet = ResNet18(None,
                          input_tensor=inputs,
                          weights=None,
                          include_top=False)
        layer_names = ['stage3_unit1_relu1', 'stage4_unit1_relu1', 'relu1']
        layer_outputs = [resnet.get_layer(name).output for name in layer_names]
        resnet = keras.models.Model(inputs=inputs,
                                    outputs=layer_outputs,
                                    name=resnet.name)
        return retinanet.retinanet(inputs=inputs,
                                   num_classes=num_classes,
                                   backbone_layers=resnet.outputs,
                                   **kwargs)

    else:
        raise ValueError('Backbone (\'{}\') is invalid.'.format(backbone))

    # invoke modifier if given
    if modifier:
        resnet = modifier(resnet)

    # create the full model
    return retinanet.retinanet(inputs=inputs,
                               num_classes=num_classes,
                               backbone_layers=resnet.outputs[1:],
                               **kwargs)