예제 #1
0
    def classification_model(self,
                             num_dense_layers=2,
                             num_dense_units=256,
                             dropout_rate=0.2,
                             num_classes=7,
                             pooling='avg',
                             name='default_resnet_classification_model',
                             input_shape = 224,
                             **kwargs):
        """ Returns a classifier model using the correct backbone.
        """
        if K.image_data_format() == 'channels_last':
            input_shape = (input_shape, input_shape, 3)
        else:
            input_shape = (3, input_shape, input_shape)
        
        t = keras.layers.Input(shape = input_shape)
        #input shape is processed in base_model
        base_model = self.build_base_model(input_tensor=t)
        x = base_model.ouput

        #output is softmax output of last dense layer
        output = default_classification_model(input_tensor=x,
                                              input_shape=base_model.ouput_shape[1:],
                                              num_classes=num_classes,
                                              num_dense_layers=num_dense_layers,
                                              num_dense_units=num_dense_units,
                                              dropout_rate=dropout_rate,
                                              pooling=pooling, 
                                              kernel_regularizer='L2'                            
                                              )
        model = 
    def classification_model(self,
                             num_dense_layers=0,
                             num_dense_units=0,
                             dropout_rate=0.2,
                             num_classes=1000,
                             pooling='avg',
                             name='default_resnet_classification_model',
                             input_shape=224,
                             **kwargs):
        """ Returns a classifier model using the correct backbone.
        """
        if K.image_data_format() == 'channels_last':
            input_shape = (input_shape, input_shape, 3)
        else:
            input_shape = (3, input_shape, input_shape)

        input = keras.layers.Input(shape=input_shape)
        base_model = self.build_base_model(input_tensor=input)
        x = base_model.ouput

        output = default_classification_model(
            input_tensor=x,
            input_shape=base_model.ouput_shape[1:],
            num_classes=num_classes,
            num_dense_layers=num_dense_layers,
            num_dense_units=num_dense_units,
            dropout_rate=dropout_rate,
            pooling=pooling,
        )
    def classification_model(self,
                             num_dense_layers=2,
                             num_dense_units=256,
                             dropout_rate=0.2,
                             num_classes=7,
                             pooling='avg',
                             name='default_resnet_classification_model',
                             input_shape = 256,
                             save_to = "",
                             lr=1e-4
                             **kwargs):
        """ Returns a classifier model using the correct backbone.
        """
        if isinstance(input_shape, int):
            if K.image_data_format() == 'channels_last':
                input_shape = (input_shape, input_shape, 3)
            else:
                input_shape = (3, input_shape, input_shape)

        else :
                
        t = keras.layers.Input(shape = input_shape)
        #input shape is processed in base_model
        base_model = self.build_base_model(input_tensor=t)
        x = base_model.ouput

        #output is softmax output of last dense layer
        output = default_classification_model(input_tensor=x,
                                              input_shape=base_model.ouput_shape[1:],
                                              num_classes=num_classes,
                                              num_dense_layers=num_dense_layers,
                                              num_dense_units=num_dense_units,
                                              dropout_rate=dropout_rate,
                                              pooling=pooling, 
                                              kernel_regularizer='L2'                            
                                              )
        model = keras.models.Model(inputs = t, outputs=output, name=name)
        if save_to:
            save_model_to_run(model, save_to)

        compile_model(model=model,
                    num_classes=num_classes,
                    metrics='acc',
                    loss='ce',
                    lr=lr)
        
        return model     

    def validate(self):
        """ Checks whether the backbone string is correct.
        """
        allowed_backbones = ['resnet50', ]

        if self.backbone_name not in allowed_backbones:
            raise ValueError('Backbone (\'{}\') not in allowed backbones ({}).'.format(self.backbone_name,
                                                                                       allowed_backbones))
class ResNetBackbone(Backbone):
    def __init__(self, backbone_name='resnet50'):
        super(ResNetBackbone, self).__init__(backbone_name)
        self.custom_objects['keras_resnet50'] = keras_resnet50

    def build_base_model(self, inputs, **kwarg):
        # create the vgg backbone
        if self.backbone_name == 'resnet50':
            inputs = keras.layers.Lambda(lambda x: keras_resnet50.preprocess_input(x))(inputs)
            resnet = keras_resnet50.ResNet50(input_tensor=inputs,
                                             include_top=False,
                                             weights=None)
        else:
            raise ValueError("Backbone '{}' not recognized.".format(self.backbone_name))

        return resnet

    def classification_model(self,
                             num_dense_layers=2,
                             num_dense_units=256,
                             dropout_rate=0.2,
                             num_classes=7,
                             pooling='avg',
                             name='default_resnet_classification_model',
                             input_shape = 224,
                             save_to
                             **kwargs):
        """ Returns a classifier model using the correct backbone.
        """
        if K.image_data_format() == 'channels_last':
            input_shape = (input_shape, input_shape, 3)
        else:
            input_shape = (3, input_shape, input_shape)
        
        t = keras.layers.Input(shape = input_shape)
        #input shape is processed in base_model
        base_model = self.build_base_model(input_tensor=t)
        x = base_model.ouput

        #output is softmax output of last dense layer
        output = default_classification_model(input_tensor=x,
                                              input_shape=base_model.ouput_shape[1:],
                                              num_classes=num_classes,
                                              num_dense_layers=num_dense_layers,
                                              num_dense_units=num_dense_units,
                                              dropout_rate=dropout_rate,
                                              pooling=pooling, 
                                              kernel_regularizer='L2'                            
                                              )
        model = keras.models.Model(inputs = t, outputs=output, name=name)
    def classification_model(self,
                             num_dense_layers=2,
                             num_dense_units=256,
                             dropout_rate=0.2,
                             num_classes=7,
                             pooling='avg',
                             name='default_resnet_classification_model',
                             input_shape = 224,
                             save_to = "",
                             lr=1e-4
                             **kwargs):
        """ Returns a classifier model using the correct backbone.
        """
        if K.image_data_format() == 'channels_last':
            input_shape = (input_shape, input_shape, 3)
        else:
            input_shape = (3, input_shape, input_shape)
        
        t = keras.layers.Input(shape = input_shape)
        #input shape is processed in base_model
        base_model = self.build_base_model(input_tensor=t)
        x = base_model.ouput

        #output is softmax output of last dense layer
        output = default_classification_model(input_tensor=x,
                                              input_shape=base_model.ouput_shape[1:],
                                              num_classes=num_classes,
                                              num_dense_layers=num_dense_layers,
                                              num_dense_units=num_dense_units,
                                              dropout_rate=dropout_rate,
                                              pooling=pooling, 
                                              kernel_regularizer='L2'                            
                                              )
        model = keras.models.Model(inputs = t, outputs=output, name=name)
        if save_to:
            save_model_to_run(model, save_to)

        compile_model(model=model,
                    num_classes=num_classes,
                    metrics='acc',
                    loss='ce',
                    lr=lr)
        
        return class Mixin(models.Model):
        
            
        
            class Meta:
                abstract = True
예제 #6
0
    def classification_model(
        self,
        input_shape=None,
        input_padding=None,
        submodel=None,
        num_classes=7,
        num_dense_layers=2,
        num_dense_units=256,
        dropout_rate=0.,
        pooling=None,
        use_output_activation=True,
        kernel_regularizer=None,
        use_activation=True,
        include_top=True,
        name='default_classification_model',
        print_model_summary=False,
        plot_model_summary=False,
        load_from=None,
        load_model_from=None,
        load_weights_from=None,
        save_to=None,
        lr=1e-5,
    ):
        """ Returns a classifier model using the correct backbone.
        """
        import keras
        from keras import backend as K
        from models.submodels.classification import default_classification_model
        from misc_utils.model_utils import plot_model
        from misc_utils.model_utils import load_model_from_run
        from misc_utils.model_utils import save_model_to_run
        from misc_utils.model_utils import load_model_weights_from
        from misc_utils.print_utils import on_aws

        if load_from:
            model = load_model_from_run(self.backbone_name, load_from,
                                        load_from)
        elif load_model_from:
            model = load_model_from_run(self.backbone_name, load_model_from,
                                        load_weights_from)
        else:
            if K.image_data_format() == 'channels_last':
                input_shape = (224, 224,
                               3) if input_shape is None else input_shape
            else:
                input_shape = (3, 224,
                               224) if input_shape is None else input_shape

            inputs = keras.layers.Input(shape=input_shape)

            x = inputs
            if input_padding is not None:
                x = keras.layers.ZeroPadding2D(padding=input_padding)(x)

            base_model = self.build_base_model(inputs=x,
                                               **self.backbone_options)
            x = base_model.output

            if submodel is None:
                outputs = default_classification_model(
                    input_tensor=x,
                    input_shape=base_model.output_shape[1:],
                    num_classes=num_classes,
                    num_dense_layers=num_dense_layers,
                    num_dense_units=num_dense_units,
                    dropout_rate=dropout_rate,
                    pooling=pooling,
                    use_output_activation=use_activation,
                    kernel_regularizer=kernel_regularizer)
            else:
                outputs = submodel(x)

            model = keras.models.Model(inputs=inputs,
                                       outputs=outputs,
                                       name=name)

            if load_weights_from:
                load_model_weights_from(model,
                                        load_weights_from,
                                        skip_mismatch=False)

        if print_model_summary:
            model.summary()

        if plot_model_summary and not on_aws():
            plot_model(save_to_dir=save_to, model=model, name=name)

        if save_to:
            save_model_to_run(model, save_to)

        compile_model(model=model,
                      num_classes=num_classes,
                      metrics='acc',
                      loss='ce',
                      lr=lr)

        return model