Beispiel #1
0
def tiny_yolo4lite_resnet50v2_body(inputs,
                                   num_anchors,
                                   num_classes,
                                   use_spp=True):
    '''Create Tiny YOLO_v4 Lite ResNet50V2 model CNN body in keras.'''
    resnet50v2 = ResNet50V2(input_tensor=inputs,
                            weights='imagenet',
                            include_top=False)
    print('backbone layers number: {}'.format(len(resnet50v2.layers)))

    # input: 416 x 416 x 3
    # post_relu: 13 x 13 x 2048
    # conv4_block5_out: 26 x 26 x 1024
    # conv3_block3_out: 52 x 52 x 512

    # f1 :13 x 13 x 2048
    f1 = resnet50v2.get_layer('post_relu').output
    # f2: 26 x 26 x 1024
    f2 = resnet50v2.get_layer('conv4_block5_out').output

    f1_channel_num = 1024
    f2_channel_num = 512

    y1, y2 = tiny_yolo4lite_predictions(
        (f1, f2), (f1_channel_num, f2_channel_num), num_anchors, num_classes,
        use_spp)

    return Model(inputs, [y1, y2])
Beispiel #2
0
    def __init__(self, img_w: int, img_h: int, channels: int):
        '''

        Args:
            img_w: Pixel width for input images
            img_h: Pixel height for input images
            channels: number of channels for input images
        '''
        self.img_w = img_w
        self.img_h = img_h
        self.channels = channels

        self.resnet50v2 = ResNet50V2(weights='imagenet',
                                     include_top=False,
                                     input_tensor=layers.Input(
                                         shape=(self.img_h, self.img_w, 3),
                                         name='input_sentinel'))
        # Load part of the VGG without the top layers into 'pretrained' model
        self.pretrained_model = models.Model(
            inputs=self.resnet50v2.input,
            outputs=self.resnet50v2.get_layer('conv5_block3_out').output)
        self.config = self.pretrained_model.get_config()

        self.hs_inputs = Input(shape=(self.img_h, self.img_w, self.channels),
                               name='input')
Beispiel #3
0
def model_builder(hp):
    model_type = hp.Choice('model_type', ['ResNet50V2'])

    if model_type == 'ResNet50V2':
        base_model = ResNet50V2(input_shape=(
            IMG_WIDTH, IMG_HEIGHT, 3), include_top=False, weights='imagenet')
        head_model = base_model
        for layers in base_model.layers[:45]:
            layers.trainable = True
        head_model = head_model.output
        head_model = tf.keras.layers.GlobalMaxPooling2D()(head_model)
        head_model = tf.keras.layers.Flatten(name="Flatten")(head_model)
        hp_units = hp.Int('units', min_value=1300, max_value=1750, step=150)
        head_model = tf.keras.layers.Dense(
            hp_units, activation='relu')(head_model)
        head_model = tf.keras.layers.Dropout(0.3)(head_model)
        prediction_layer = tf.keras.layers.Dense(
            len(classes), activation='relu')(head_model)
        model = tf.keras.Model(inputs=base_model.input,
                               outputs=prediction_layer)
        hp_learning_rate = hp.Choice(
            'learning_rate', values=[1e-2, 1e-3, 1e-4])
        model.compile(optimizer=keras.optimizers.Adam(learning_rate=hp_learning_rate),
                      loss=tf.keras.losses.categorical_crossentropy,
                      metrics=['accuracy'])

    return model
Beispiel #4
0
def define_model(n_layers=45, BASE_MODEL='ResNet50V2'):
    if BASE_MODEL == 'ResNet50V2':
        # Pre-trained model with MobileNetV2
        base_model = ResNet50V2(input_shape=(
            IMG_WIDTH, IMG_HEIGHT, 3), include_top=False, weights='imagenet')
        head_model = base_model
        for layers in base_model.layers[:n_layers]:
            layers.trainable = True
        head_model = head_model.output
        head_model = tf.keras.layers.GlobalMaxPooling2D()(head_model)
        head_model = tf.keras.layers.Flatten(name="Flatten")(head_model)
        head_model = tf.keras.layers.Dense(1600, activation='relu')(head_model)
        head_model = tf.keras.layers.Dropout(0.2)(head_model)
        prediction_layer = tf.keras.layers.Dense(
            len(classes), activation='softmax')(head_model)
        model = tf.keras.Model(inputs=base_model.input,
                               outputs=prediction_layer)

    if BASE_MODEL == 'InceptionV3':
        base_model = InceptionV3(input_shape=(
            IMG_WIDTH, IMG_HEIGHT, 3), include_top=False, weights='imagenet')
        head_model = base_model
        for layers in base_model.layers[:n_layers]:
            layers.trainable = False

        head_model = head_model.output
        head_model = tf.keras.layers.GlobalMaxPooling2D()(head_model)
        head_model = tf.keras.layers.Flatten(name="Flatten")(head_model)
        head_model = tf.keras.layers.Dense(1024, activation='relu')(head_model)
        head_model = tf.keras.layers.Dropout(0.5)(head_model)
        prediction_layer = tf.keras.layers.Dense(
            len(classes), activation='softmax')(head_model)
        model = tf.keras.Model(inputs=base_model.input,
                               outputs=prediction_layer)
    return model
    def __init__(self, args, encoder_shape=(224, 224, 3)):
        super().__init__(name="Generator")
        self._args = args

        # Create the ResNet50V2 Encoder
        self._encoder = ResNet50V2(include_top=False,
                                   weights="imagenet",
                                   input_shape=encoder_shape,
                                   pooling="avg")

        # Build the Regressor that will perform IEF
        self._dense_1 = layers.Dense(1024, activation="relu")
        self._dense_2 = layers.Dense(1024, activation="relu")
        self._dropout_1 = layers.Dropout(0.5)
        self._dropout_2 = layers.Dropout(0.5)
        small_xavier = tf.initializers.VarianceScaling(.01,
                                                       mode='fan_avg',
                                                       distribution='uniform')
        self._reg_out = layers.Dense(85, kernel_initializer=small_xavier)

        # Load the initial mean theta params and create
        # a trainable variable for it
        self._mean_theta = tf.Variable(load_mean_theta(self._args),
                                       name='mean_theta',
                                       trainable=True)
Beispiel #6
0
def yolo4_resnet50v2_body(inputs, num_anchors, num_classes):
    """Create YOLO_V4 ResNet50V2 model CNN body in Keras."""
    resnet50v2 = ResNet50V2(input_tensor=inputs,
                            weights='imagenet',
                            include_top=False)
    print('backbone layers number: {}'.format(len(resnet50v2.layers)))

    # input: 416 x 416 x 3
    # post_relu: 13 x 13 x 2048
    # conv4_block5_out: 26 x 26 x 1024
    # conv3_block3_out: 52 x 52 x 512

    # f1 :13 x 13 x 2048
    f1 = resnet50v2.get_layer('post_relu').output
    # f2: 26 x 26 x 1024
    f2 = resnet50v2.get_layer('conv4_block5_out').output
    # f3 : 52 x 52 x 512
    f3 = resnet50v2.get_layer('conv3_block3_out').output

    f1_channel_num = 1024
    f2_channel_num = 512
    f3_channel_num = 256

    y1, y2, y3 = yolo4_predictions(
        (f1, f2, f3), (f1_channel_num, f2_channel_num, f3_channel_num),
        num_anchors, num_classes)

    return Model(inputs=inputs, outputs=[y1, y2, y3])
Beispiel #7
0
def build_COVIDNet(num_classes=3, flatten=True, checkpoint='',args=None):
    
    if args.model == 'resnet50v2':
        base_model = ResNet50V2(include_top=False, weights='imagenet', input_shape=(args.img_size, args.img_size, 3))
        x = base_model.output
    
    if args.model =='mobilenetv2':
        base_model = MobileNetV2(include_top=False, weights='imagenet', input_shape=(args.img_size, args.img_size, 3))
        x = base_model.output
    
    if args.model == 'custom':
        base_model = covidnet(input_tensor=None, input_shape=(args.img_size, args.img_size, 3), classes=3)
        x = base_model.output
        
    if args.model == 'EfficientNet':
        import efficientnet.tfkeras as efn
        base_model = efn.EfficientNetB4(weights=None, include_top=True, input_shape=(args.img_size, args.img_size, 3), classes=3)
        x = base_model.output
    
    
    if flatten:
        x = Flatten()(x)
    else:
        # x = GlobalAveragePooling2D()(x)
        x = GlobalMaxPool2D()(x)
    
    if args.datapipeline == 'covidx':
        x = Dense(1024, activation='relu',kernel_regularizer=tf.keras.regularizers.l2(0.0001))(x)
    x = Dense(256, activation='relu',kernel_regularizer=tf.keras.regularizers.l2(0.0001))(x)
    # x = Dropout(0.2)(x)
    predictions = Dense(num_classes, activation='softmax',name=f'FC_{num_classes}')(x)
    model = Model(inputs=base_model.input, outputs=predictions)
    if len(checkpoint):
        model.load_weights(checkpoint)
    return model
Beispiel #8
0
def define_model(num):

    if num == 0:
        convbase = MobileNetV2(
            weights='imagenet', include_top=False, input_shape=(150, 150, 3))
        print("Creating MobileNet layer!")
    elif num == 1:
        convbase = ResNet50V2(weights='imagenet',
                              include_top=False, input_shape=(150, 150, 3))
        print("Creating ResNet layer!")
    elif num == 2:
        convbase = VGG16(
            weights='imagenet', include_top=False, input_shape=(150, 150, 3))
        print("Creating VGG16 layer!")
    else:
        print("Out of range! Something's wrong")

    convbase.trainable = False  # Preserve weights of pre-trained conv layers
    model = tf.keras.Sequential()
    model.add(convbase)
    model.add(layers.Flatten())
    model.add(layers.Dense(256, activation='relu'))
    model.add(layers.Dense(10, activation='softmax'))
    # compile model
    model.compile(loss='categorical_crossentropy',
                  optimizer=RMSprop(lr=2e-5),
                  metrics=['accuracy'])

    return model
Beispiel #9
0
def prepare_model(labels):
    base_model = ResNet50V2(include_top=False,
                            weights='imagenet',
                            input_shape=(200, 200, 3),
                            pooling='avg')

    x = base_model.output
    x = Dense(1024, activation='relu')(x)
    x = Dense(256, activation='relu')(x)
    x = Dense(64, activation='relu')(x)
    x = Dropout(0.2)(x)
    predictions = Dense(len(labels), activation='sigmoid')(x)

    model = Model(inputs=base_model.input, outputs=predictions)

    # first: train only the top layers (which were randomly initialized)
    for layer in base_model.layers:
        layer.trainable = False

    # compile the model (should be done *after* setting layers to non-trainable)
    model.compile(optimizer='adam',
                  loss='binary_crossentropy',
                  metrics=['accuracy'])

    return model
Beispiel #10
0
 def cp_net(input):
     resnet = ResNet50V2(False,
                         None,
                         input_tensor=input,
                         input_shape=input_shape,
                         pooling="avg")
     out = Dense(n_features)(resnet.output)
     return out
Beispiel #11
0
def resnet50v2(model_config,
               input_shape,
               metrics,
               n_classes=2,
               output_bias=None,
               gpus=1):
    '''
    Defines a model based on a pretrained ResNet50V2 for multiclass X-ray classification.
    :param model_config: A dictionary of parameters associated with the model architecture
    :param input_shape: The shape of the model input
    :param metrics: Metrics to track model's performance
    :return: a Keras Model object with the architecture defined in this method
    '''

    # Set hyperparameters
    nodes_dense0 = model_config['NODES_DENSE0']
    lr = model_config['LR']
    dropout = model_config['DROPOUT']
    l2_lambda = model_config['L2_LAMBDA']
    if model_config['OPTIMIZER'] == 'adam':
        optimizer = Adam(learning_rate=lr)
    elif model_config['OPTIMIZER'] == 'sgd':
        optimizer = SGD(learning_rate=lr)
    else:
        optimizer = Adam(learning_rate=lr)  # For now, Adam is default option

    # Set output bias
    if output_bias is not None:
        output_bias = Constant(output_bias)
    print("MODEL CONFIG: ", model_config)

    # Start with pretrained ResNet50V2
    X_input = Input(input_shape, name='input_img')
    base_model = ResNet50V2(include_top=False,
                            weights='imagenet',
                            input_shape=input_shape,
                            input_tensor=X_input)
    X = base_model.output

    # Add custom top
    X = GlobalAveragePooling2D()(X)
    X = Dropout(dropout)(X)
    X = Dense(nodes_dense0,
              kernel_initializer='he_uniform',
              activity_regularizer=l2(l2_lambda))(X)
    X = LeakyReLU()(X)
    X = Dense(n_classes, bias_initializer=output_bias)(X)
    Y = Activation('softmax', dtype='float32', name='output')(X)

    # Set model loss function, optimizer, metrics.
    model = Model(inputs=X_input, outputs=Y)
    model.summary()
    if gpus >= 2:
        model = multi_gpu_model(model, gpus=gpus)
    model.compile(loss='categorical_crossentropy',
                  optimizer=optimizer,
                  metrics=metrics)
    return model
Beispiel #12
0
def frozen_resnet(input_size, n_classes, local_weights="/resnets/resnet50v2_notop.h5"):
    if local_weights and path.exists(local_weights):
        print(f'Using {local_weights} as local weights.')
        model_ = ResNet50V2(
            include_top=False,
            input_tensor=Input(shape=input_size),
            weights=local_weights)
    else:
        print(
            f'Could not find local weights {local_weights} for ResNet. Using remote weights.')
        model_ = ResNet50V2(
            include_top=False,
            input_tensor=Input(shape=input_size))
    for layer in model_.layers:
        layer.trainable = False
    x = Flatten(input_shape=model_.output_shape[1:])(model_.layers[-1].output)
    x = Dense(n_classes, activation='softmax')(x)
    frozen_model = Model(model_.input, x)

    return frozen_model
Beispiel #13
0
    def resNet(self, nclasses=3, summary=True):
        res = ResNet50V2(weights='imagenet',
                         include_top=False,
                         input_shape=(self.__xSize, self.__ySize, 3))
        res.trainable = False
        input_l = layers.Input(shape=(self.__xSize, self.__ySize, 3))
        x = res(input_l, training=False)
        predict = self.__addTop(x, nclasses)
        self.__model = tf.keras.Model(input_l, predict)
        if summary:
            self.__model.summary()

        self.__compileModel()
Beispiel #14
0
def define_model():
    model = ResNet50V2(include_top=False, input_shape=(240, 320, 3))

    x = model.output
    avgpool = GlobalAveragePooling2D()(x)
    output = Dense(1, activation='sigmoid')(avgpool)

    model = Model(inputs=model.inputs, outputs=output)
    opt = SGD(lr=0.001, momentum=0.9)
    model.compile(optimizer=opt,
                  loss='binary_crossentropy',
                  metrics=['accuracy'])
    return model
Beispiel #15
0
    def __init__(self):
        super(Generator, self).__init__(name='generator')
        self.config = Config()

        self.enc_shape = self.config.ENCODER_INPUT_SHAPE
        self.resnet50V2 = ResNet50V2(include_top=False,
                                     weights='imagenet',
                                     input_shape=self.enc_shape,
                                     pooling='avg')
        self._set_resnet_arg_scope()

        self.regressor = Regressor()
        self.smpl = Smpl()
Beispiel #16
0
def model(img_width=224, img_height=224):
    model_resnet50_conv = ResNet50V2(include_top=False,
                                     input_shape=(img_width, img_height, 3))
    for layer in model_resnet50_conv.layers[:154]:
        layer.trainable = False
    model_resnet50_conv.trainable = False

    x = GlobalAveragePooling2D(name="GAP")(model_resnet50_conv.output)
    x = Dense(2, activation="sigmoid", name="predictions")(x)

    model = Model(inputs=model_resnet50_conv.inputs, outputs=x)

    return model
 def __init__(self, data_shape=(224, 224, 3), resnet_version=1, resnet_layer_number=50, num_classes=1000):
   super(ResNet, self).__init__()
   
   weights = None
   if num_classes == 1000 and data_shape == (224, 224, 3):
     weights = 'imagenet'
     
   self.resnet_version = resnet_version
   
   self.data_augmentation = keras.Sequential(
     [
       layers.experimental.preprocessing.RandomFlip(
         "horizontal", 
         input_shape=data_shape),
       layers.experimental.preprocessing.RandomRotation(0.1),
       layers.experimental.preprocessing.RandomZoom(0.1),
     ]
   )
   
   self.rescaling = layers.experimental.preprocessing.Rescaling(1./255)
   
   def preprocess_input(x, data_format=None):
     from tensorflow.keras.applications import imagenet_utils
     return imagenet_utils.preprocess_input(
     x, data_format=data_format, mode='tf')
     #return x
     
   self.preprocess_input = preprocess_input
   
   if resnet_layer_number == 18:
     if resnet_version == 1:
       self.resnet = ResNet18(category_num=num_classes)
     else:
       self.resnet = ResNet18V2(category_num=num_classes)
   elif resnet_layer_number == 50:
     if resnet_version == 1:
       self.resnet = ResNet50(weights=weights, input_shape=data_shape, classes=num_classes)
     else:
       self.resnet = ResNet50V2(weights=weights, input_shape=data_shape, classes=num_classes)
   elif resnet_layer_number == 101:
     if resnet_version == 1:
       self.resnet = ResNet101(weights=weights, input_shape=data_shape, classes=num_classes)
     else:
       self.resnet = ResNet101V2(weights=weights, input_shape=data_shape, classes=num_classes)
   elif resnet_layer_number == 152:
     if resnet_version == 1:
       self.resnet = ResNet152(weights=weights, input_shape=data_shape, classes=num_classes)
     else:
       self.resnet = ResNet152V2(weights=weights, input_shape=data_shape, classes=num_classes)
     
   self.build((None,) + data_shape)
def create_image_resnet50v2_model(NUM_CLASS,
                                  isPreTrained=False,
                                  pathToResNet50V2ModelWeights=None):
    resnet50v2 = ResNet50V2(weights='imagenet', include_top=False)
    last_layer = resnet50v2.output
    x = GlobalAveragePooling2D()(last_layer)
    x = Dense(768, activation='relu', name='img_dense_768')(x)
    out = Dense(NUM_CLASS, activation='softmax', name='img_output_layer')(x)
    resnet50v2 = Model(inputs=resnet50v2.input, outputs=out, name="ResNet50V2")

    if not isPreTrained:
        return resnet50v2
    else:
        resnet50v2.load_weights(pathToResNet50V2ModelWeights)
        return resnet50v2, 2
Beispiel #19
0
def load_network(model_path):
    if '.h5' in model_path:
        model = tf.keras.models.load_model(model_path)
    else:
        if 'resnet50v2' in model_path:
            model = ResNet50V2(weights='imagenet')
        elif 'mobilenet' in model_path:
            model = MobileNet(weights='imagenet')
        elif 'mobilenetv2' in model_path:
            model = MobileNetV2(weights='imagenet')
        elif 'vgg19' in model_path:
            model = VGG19(weights='imagenet')
        else:
            raise ValueError('Invalid model name : {}'.format(model_path))
    return model
Beispiel #20
0
def ResNet50V2_pretrain_model():
    modelPre = ResNet50V2(weights='imagenet',
                          include_top=False,
                          input_shape=config.input_shape,
                          classes=config.category_num)
    model = tf.keras.Sequential()
    model.add(modelPre)
    model.add(tf.keras.layers.GlobalAveragePooling2D())
    model.add(
        tf.keras.layers.Dense(config.category_num,
                              name='fully_connected',
                              activation='softmax',
                              use_bias=False))

    return model
Beispiel #21
0
def build_resnet50V2(NUM_CLASS):
    resnet50v2 = ResNet50V2(weights='imagenet', include_top=False)
    #model.summary()
    last_layer = resnet50v2.output
    x = GlobalAveragePooling2D()(last_layer)
    # a softmax layer for 2 classes
    x = Dense(768, activation='relu', name='img_dense_768')(x)
    out = Dense(NUM_CLASS, activation='softmax', name='output_layer')(x)
    resnet50v2 = Model(inputs=resnet50v2.input, outputs=out)
    plot_model(resnet50v2,
               to_file='resnet50v2_inputs.png',
               show_shapes=True,
               dpi=600,
               expand_nested=False)
    return resnet50v2, 7
Beispiel #22
0
    def load_model(self):
        FACTOR = 0.70
        HEIGHT = 137
        WIDTH = 236
        HEIGHT_NEW = int(HEIGHT * FACTOR)
        WIDTH_NEW = int(WIDTH * FACTOR)

        base_model = ResNet50V2(include_top=False,
                                weights='imagenet',
                                input_tensor=None,
                                input_shape=(HEIGHT_NEW, WIDTH_NEW, 3),
                                pooling=None,
                                classes=1000)
        # base_model.trainable=False
        x = base_model.output
        x = layers.GlobalAveragePooling2D()(x)

        grapheme_root = layers.Dense(168, activation='softmax', name='root')(x)
        vowel_diacritic = layers.Dense(11, activation='softmax',
                                       name='vowel')(x)
        consonant_diacritic = layers.Dense(7,
                                           activation='softmax',
                                           name='consonant')(x)

        model = Model(
            inputs=base_model.input,
            outputs=[grapheme_root, vowel_diacritic, consonant_diacritic])
        for layer in base_model.layers:
            layer.trainable = True
        model.compile(optimizer='adam',
                      loss={
                          'root': 'categorical_crossentropy',
                          'vowel': 'categorical_crossentropy',
                          'consonant': 'categorical_crossentropy'
                      },
                      loss_weights={
                          'root': 0.5,
                          'vowel': 0.25,
                          'consonant': 0.25
                      },
                      metrics={
                          'root': 'accuracy',
                          'vowel': 'accuracy',
                          'consonant': 'accuracy'
                      })
        # print(model.summary())

        return model
Beispiel #23
0
def build_COVIDNet(num_classes=3, flatten=True, checkpoint=''):
    base_model = ResNet50V2(include_top=False,
                            weights='imagenet',
                            input_shape=(224, 224, 3))
    x = base_model.output
    if flatten:
        x = Flatten()(x)
    else:
        x = GlobalAveragePooling2D()(x)
    x = Dense(1024, activation='relu')(x)
    x = Dense(256, activation='relu')(x)
    predictions = Dense(num_classes, activation='softmax')(x)
    model = Model(inputs=base_model.input, outputs=predictions)
    if len(checkpoint):
        model.load_weights(checkpoint)
    return model
def get_resnet_model():
    model = ResNet50V2(weights='imagenet', include_top=False)
    # for i, layer in enumerate(model.layers):
    #     print(i, layer.name)
    layer_indices = [9, 36, 82, 150, 185]  # activation indices ~ scale 0.015
    layer_indices = [37, 83, 151]  # max_pool indices ~ scale 0.018
    outputs = None
    for idx in layer_indices:
        features = model.layers[idx].output
        features = Flatten()(features)
        if (idx == layer_indices[0]):
            outputs = features
        else:
            outputs = concatenate([outputs, features])
    resnet_model = Model(model.inputs, outputs)
    return resnet_model
def inject_net(name="resnet", input_tensor=None):
    '''
    Picks One of the Pretrained Models (or untrained Mods on it)
    and returns the respective mergeable Endpoints
    '''
    if name == "resnet":

        resnet = ResNet50V2(include_top=False,
                            weights=None,
                            pooling=None,
                            input_tensor=input_tensor)

        ep_layers = []
        for layer in resnet.layers:
            if '_pool' in layer.name:
                ep_layers.append(layer.output)

        ep1, ep2, ep3, ep4 = ep_layers

        return ep1, ep2, ep3, ep4

    elif name == "vgg16":

        vgg = tf.keras.applications.VGG16(include_top=False,
                                          weights='imagenet',
                                          input_tensor=input_tensor,
                                          pooling=None)

        ep1 = vgg.get_layer('block2_pool').output
        ep2 = vgg.get_layer('block3_pool').output
        ep3 = vgg.get_layer('block4_pool').output
        ep4 = vgg.get_layer('block5_pool').output

        return ep1, ep2, ep3, ep4

    elif name == 'resnet_mod':

        resnet_mod = get_resnet_mod(input_tensor=input_tensor)

        ep1 = resnet_mod.get_layer('ep1').output
        ep2 = resnet_mod.get_layer('ep2').output
        ep3 = resnet_mod.get_layer('ep3').output
        ep4 = resnet_mod.get_layer('ep4').output

        print(ep1.shape, ep2.shape, ep3.shape, ep4.shape)

        return ep1, ep2, ep3, ep4
Beispiel #26
0
def buildKerasAppModel(img_height, img_width, img_channel, num_classes):
    AppModel = ResNet50V2(include_top=False, pooling='avg', weights='imagenet')

    x = AppModel.layers[-1].output
    x = Flatten()(x)
    x = BatchNormalization()(x)
    x = Dropout(0.5)(x)

    output_layer = Dense(num_classes, activation='softmax', name='softmax')(x)
    model = Model(inputs=AppModel.input, outputs=output_layer)
    model.compile(
        loss=
        'sparse_categorical_crossentropy',  #categorical_crossentropy / sparse_categorical_crossentropy
        optimizer=Adam(lr=1e-5),
        metrics=['accuracy'])

    model.summary()
    return model
Beispiel #27
0
 def build_network(cls):
     import ssl
     ssl._create_default_https_context = ssl._create_unverified_context
     base_model = ResNet50V2(include_top=False,
                             weights='imagenet',
                             input_shape=(224, 224, 3))
     x = base_model.output
     if cls.flatten:
         x = Flatten()(x)
     else:
         x = GlobalAveragePooling2D()(x)
     x = Dense(1024, activation='relu')(x)
     x = Dense(256, activation='relu')(x)
     predictions = Dense(cls.num_classes, activation='softmax')(x)
     model = Model(inputs=base_model.input, outputs=predictions)
     if len(cls.checkpoint):
         model.load_weights(checkpoint)
     return model
Beispiel #28
0
def createTextResNet50v2Maximum(max_seq_len, bert_ckpt_file, bert_config_file,
                                NUM_CLASS):
    with GFile(bert_config_file, "r") as reader:
        bc = StockBertConfig.from_json_string(reader.read())
        bert_params = map_stock_config_to_params(bc)
        bert_params.adapter_size = None
        bert_layer = BertModelLayer.from_params(bert_params, name="bert")

    bert_in = Input(shape=(max_seq_len, ),
                    dtype='int32',
                    name="input_ids_bert")
    bert_inter = bert_layer(bert_in)
    cls_out = Lambda(lambda seq: seq[:, 0, :])(bert_inter)
    cls_out = Dropout(0.5)(cls_out)
    bert_out = Dense(units=768, activation="tanh")(cls_out)  # 768 before
    load_stock_weights(bert_layer, bert_ckpt_file)

    # image models:
    resNet50v2 = ResNet50V2(weights='imagenet', include_top=False)
    res_out = resNet50v2.output
    res_out = GlobalAveragePooling2D()(res_out)
    res_out = Dropout(0.5)(res_out)
    res_out = Dense(2048)(res_out)
    res_out = Dropout(0.5)(res_out)
    res_out = Dense(768)(res_out)
    merge = Maximum()([res_out, bert_out])
    # restliche Layer
    x = Dense(2048)(merge)
    x = Dropout(0.5)(x)
    x = Dense(1024)(x)
    x = Dropout(0.5)(x)
    x = Dense(512)(x)
    x = Dropout(0.5)(x)
    output = Dense(NUM_CLASS, activation='softmax', name='output_layer')(x)
    model = Model(inputs=[resNet50v2.input, bert_in], outputs=output)
    plot_model(model,
               to_file='multiple_inputs_text.png',
               show_shapes=True,
               dpi=600,
               expand_nested=False)

    return model, 14
Beispiel #29
0
def _resnet50_attention_cnn_backbone(input_layer):
    """ResNet50V2 convolution part of CRNN model(Only Attention use).

    Parameters:
        input_layer:
            Keras Layer, model input.

    Returns:
        Keras Layer.
    """
    layer = ResNet50V2(include_top=False, weights='imagenet')(input_layer)

    # The down sampling factor of ResNet50 is 28.
    # The shape of the ResNet50 output layer is (None, img_width / 28, img_height / 28, 2048).

    layer = Reshape(target_shape=(-1, 512), name='Reshape')(layer)
    layer = Dense(units=256, activation='relu', name='Dense1')(layer)
    layer = Dropout(rate=0.25, name='Dropout')(layer)

    return layer
def ResNet50_model(input_shape=(128, 128, 3)):
    optimizer = SGD(lr=0.01)

    X_input = Input(input_shape, name='input_img')
    base_model = ResNet50V2(include_top=False,
                            weights='imagenet',
                            input_shape=input_shape,
                            input_tensor=X_input)
    X = base_model.output

    X = GlobalAveragePooling2D()(X)
    X = BatchNormalization()(X)
    X = Dropout(0.2)(X)
    X = Dense(512, kernel_initializer='he_uniform')(X)
    X = LeakyReLU()(X)
    X = Dense(4)(X)
    Y = Activation('softmax', dtype='float32', name='output')(X)

    model = Model(inputs=X_input, outputs=Y)
    model.summary()
    model.compile(loss='categorical_crossentropy',
                  optimizer=optimizer,
                  metrics=['accuracy'])
    return model