def resnet50v2(input_shape: Tuple[int, int, int], output_shape: Tuple[int, ...], weights: str='imagenet', include_top: bool=False) -> Model:

    base_model = ResNet50V2(weights=weights, include_top=include_top, input_tensor=Input(shape=input_shape))
    if include_top:
        return base_model
    else:
        # Construct the head of the model that will be placed on top of the base model
        num_classes = output_shape[0]
        head_model=base_model.output
        head_model=AveragePooling2D(pool_size=(4, 4))(head_model)
        head_model=Flatten(name='flatten')(head_model)
        head_model=Dense(64, activation='relu')(head_model)
        head_model=Dropout(0.5)(head_model)

        if num_classes > 2:
            activation = 'softmax'
        else:
            activation = 'sigmoid'

        head_model=Dense(num_classes, activation=activation)(head_model)

        # Place the head Fully Connected model on top of the base model (actual model)
        model=Model(base_model.input, head_model)

        # Loop over all layers in the base model and freeze them so they won't be updated during the first training process
        for layer in base_model.layers:
            layer.trainable=False

        return model
Esempio n. 2
0
def loader(input_shape,
           num_outputs,
           output_activation="log_softmax",
           channel_dropout_rate=0):
    inputs = Input(shape=input_shape, name="input")
    x = inputs
    if channel_dropout_rate > 0:
        x = Dropout(rate=channel_dropout_rate,
                    noise_shape=(None, 1, input_shape[1]),
                    name="channel_dropout")(x)
    x = Reshape((input_shape[0] or -1, input_shape[1], 1),
                name="reshape_to_image")(x)
    resnet = ResNet50V2(include_top=False, weights=None, input_tensor=x)
    rows, cols, channels = resnet.output.shape[1:]
    x = Reshape((rows or -1, cols * channels),
                name="flatten_channels")(resnet.output)
    x = FrameLayer(512, 5, 1, name="frame1")(x)
    x = FrameLayer(512, 3, 2, name="frame2")(x)
    x = FrameLayer(512, 3, 3, name="frame3")(x)
    x = FrameLayer(512, 1, 1, name="frame4")(x)
    x = FrameLayer(1500, 1, 1, name="frame5")(x)
    x = GlobalMeanStddevPooling1D(name="stats_pooling")(x)
    x = SegmentLayer(512, name="segment1")(x)
    x = SegmentLayer(512, name="segment2")(x)
    outputs = Dense(num_outputs, name="output", activation=None)(x)
    if output_activation:
        outputs = Activation(getattr(tf.nn, output_activation),
                             name=str(output_activation))(outputs)
    return Model(inputs=inputs, outputs=outputs, name="resnet50-x-vector")
Esempio n. 3
0
def loader(input_shape,
           num_outputs,
           core="resnet50_v2",
           output_activation="softmax"):
    # Normalize and regularize by adding Gaussian noise to input (during training only)
    inputs = Input(shape=input_shape, name="input")
    x = inputs
    x = GaussianNoise(stddev=0.01, name="input_noise")(x)
    x = Dropout(0.2,
                noise_shape=(None, 1, input_shape[1]),
                name="channel_dropout")(x)
    x = Reshape((input_shape[0] or -1, input_shape[1], 1),
                name="reshape_to_image")(x)
    # Connect untrained Resnet50 or MobileNet architecture without inputs and outputs
    if core == "mobilenet_v2":
        convnet = MobileNetV2(include_top=False, weights=None, input_tensor=x)
    elif core == "resnet50_v2":
        convnet = ResNet50V2(include_top=False, weights=None, input_tensor=x)
    # Embedding layer with timesteps
    rows, cols, channels = convnet.output.shape[1:]
    x = Reshape((rows or -1, cols * channels),
                name="flatten_channels")(convnet.output)
    x = Dense(128, activation="sigmoid", name="embedding")(x)
    x = BatchNormalization(name="embedding_bn")(x)
    # Pooling and output
    x = GlobalAveragePooling1D(name="timesteps_pooling")(x)
    outputs = Dense(num_outputs, activation=None, name="output")(x)
    if output_activation:
        outputs = Activation(getattr(tf.nn, output_activation),
                             name=str(output_activation))(outputs)
    return Model(inputs=inputs,
                 outputs=outputs,
                 name="{}_extractor".format(core))
Esempio n. 4
0
def DAFE(input_shape, n_classes):

    # ResNet50 backbone (bottom-up)
    resnet = ResNet50V2(weights='imagenet', include_top=False,
                        input_shape=input_shape)

    conv2 = resnet.get_layer('pool1_pool').output
    conv3 = resnet.get_layer('conv2_block3_out').output
    conv4 = resnet.get_layer('conv3_block4_out').output
    conv5 = resnet.get_layer('conv4_block6_out').output

    # DAFE blocks (top-down)
    x = sau_block([conv5, conv4], 512, name='sau1')
    x = cas_block(x, [256, 512], name='cas1')
    x = layers.Add(name='dafe1_out')([x, conv4])

    x = sau_block([x, conv3], 256, name='sau2')
    x = cas_block(x, [64, 256], name='cas2')
    x = layers.Add(name='dafe2_out')([x, conv3])

    x = sau_block([x, conv2], 64, name='sau3')
    x = cas_block(x, [32, 64], name='cas3')
    x = layers.Add(name='dafe3_out')([x, conv2])

    # Classifier
    x = layers.Conv2D(n_classes, (1, 1), padding='same', activation='linear',
                      name='conv_classifier')(x)

    return tf.keras.models.Model(inputs=resnet.input, outputs=x, name='dafe')
def Resnet(input_shape=(384, 384, 3)):
    resnet = ResNet50V2(input_shape=input_shape, weights='imagenet', include_top=False)
    out_f1 = resnet.get_layer('conv2_block3_1_relu').output
    out_f2 = resnet.get_layer('conv3_block4_1_relu').output
    out_f3 = resnet.get_layer('conv4_block6_1_relu').output
    model = KM.Model(resnet.inputs, [out_f1, out_f2, out_f3])
    return model
Esempio n. 6
0
def loader(input_shape,
           num_outputs,
           core="resnet50_v2",
           embedding_dim=512,
           output_activation="log_softmax",
           channel_dropout_rate=0):
    inputs = Input(shape=input_shape, name="input")
    x = inputs
    if channel_dropout_rate > 0:
        x = Dropout(rate=channel_dropout_rate,
                    noise_shape=(None, 1, input_shape[1]),
                    name="channel_dropout")(x)
    x = Reshape((input_shape[0] or -1, input_shape[1], 1),
                name="reshape_to_image")(x)
    # Connect untrained Resnet50 or MobileNet architecture without inputs and outputs
    if core == "mobilenet_v2":
        convnet = MobileNetV2(include_top=False, weights=None, input_tensor=x)
    elif core == "resnet50_v2":
        convnet = ResNet50V2(include_top=False, weights=None, input_tensor=x)
    # Embedding layer with timesteps
    rows, cols, channels = convnet.output.shape[1:]
    x = Reshape((rows or -1, cols * channels),
                name="flatten_channels")(convnet.output)
    x = Dense(embedding_dim, activation=None, name="embedding")(x)
    x = Activation("relu", name="embedding_relu")(x)
    x = BatchNormalization(name="embedding_bn")(x)
    # Pooling and output
    x = GlobalAveragePooling1D(name="timesteps_pooling")(x)
    outputs = Dense(num_outputs, activation=None, name="output")(x)
    if output_activation:
        outputs = Activation(getattr(tf.nn, output_activation),
                             name=str(output_activation))(outputs)
    return Model(inputs=inputs,
                 outputs=outputs,
                 name="{}_extractor".format(core))
Esempio n. 7
0
def transfer_learning(input_shape):
    base_model = ResNet50V2(include_top=False, input_shape=(224, 224, 3), pooling='avg', weights='imagenet')

    # Freeze the layers except the last 12 layers (which contains few sets of Conv layers and batch normalization
    # layers)
    count_layers = 0
    for layer in base_model.layers[:-12]:
        layer.trainable = False
        count_layers = count_layers + 1
    print(count_layers, "Number of layers in Resnet50")

    # Check the trainable status of the individual layers
    for layer in base_model.layers:
        print(layer, layer.trainable)
    base_model.summary()

    # Keras input layer
    inputs = keras.Input(shape=(256, 256, 3))

    # preprocessing layer to resize image to 224*224, as Resnet input layer accepts 224,224,3
    r_input = keras.layers.experimental.preprocessing.Resizing(224, 224)(inputs)

    out = base_model(r_input)

    out = keras.layers.Dense(16, activation=tf.nn.relu,kernel_regularizer=keras.regularizers.l1(0.0001))(out)

    out = keras.layers.Dropout(0.6)(out)

    out = keras.layers.Dense(2, activation=tf.nn.softmax)(out)

    model = keras.Model(inputs, out)

    # Model Summary
    model.summary()
    return model
def define_model(architecture, input_shape=(128, 128, 3), n_classes=4):
    if architecture == 'ResNet50V2':
        base_model = ResNet50V2(weights='imagenet',
                                include_top=False,
                                input_shape=input_shape)
    elif architecture == 'VGG16':
        base_model = VGG16(weights='imagenet',
                           include_top=False,
                           input_shape=input_shape)
    elif architecture == 'VGG19':
        base_model = VGG19(weights='imagenet',
                           include_top=False,
                           input_shape=input_shape)
    elif architecture == 'DenseNet121':
        base_model = DenseNet121(weights='imagenet',
                                 include_top=False,
                                 input_shape=input_shape)

    x = base_model.output
    x = GlobalAveragePooling2D()(x)
    x = Dense(512, activation='relu')(x)
    predictions = Dense(n_classes, activation='softmax')(x)

    model = Model(inputs=base_model.input, outputs=predictions)
    return model
Esempio n. 9
0
def build_model(classes=2):
    inputs = Input(shape=(IMAGE_SIZE, IMAGE_SIZE, 3))
    x = preprocess_input(inputs)
    x = ResNet50V2(weights=None, classes=classes)(x)
    model = Model(inputs=inputs, outputs=x)
    model.compile(loss='categorical_crossentropy', metrics=['accuracy'])
    return model
 def __init__(self, model_name=None):
     if model_name == 'Xception':
         base_model = Xception(weights='imagenet')
         self.preprocess_input = xception.preprocess_input
     elif model_name == 'VGG19':
         base_model = VGG19(weights='imagenet')
         self.preprocess_input = vgg19.preprocess_input
     elif model_name == 'ResNet50':
         base_model = ResNet50(weights='imagenet')
         self.preprocess_input = resnet.preprocess_input
     elif model_name == 'ResNet101':
         base_model = ResNet101(weights='imagenet')
         self.preprocess_input = resnet.preprocess_input
     elif model_name == 'ResNet152':
         base_model = ResNet152(weights='imagenet')
         self.preprocess_input = resnet.preprocess_input
     elif model_name == 'ResNet50V2':
         base_model = ResNet50V2(weights='imagenet')
         self.preprocess_input = resnet_v2.preprocess_input
     elif model_name == 'ResNet101V2':
         base_model = ResNet101V2(weights='imagenet')
         self.preprocess_input = resnet_v2.preprocess_input
     elif model_name == 'ResNet152V2':
         base_model = ResNet152V2(weights='imagenet')
         self.preprocess_input = resnet_v2.preprocess_input
     elif model_name == 'InceptionV3':
         base_model = InceptionV3(weights='imagenet')
         self.preprocess_input = inception_v3.preprocess_input
     elif model_name == 'InceptionResNetV2':
         base_model = InceptionResNetV2(weights='imagenet')
         self.preprocess_input = inception_resnet_v2.preprocess_input
     elif model_name == 'DenseNet121':
         base_model = DenseNet121(weights='imagenet')
         self.preprocess_input = densenet.preprocess_input
     elif model_name == 'DenseNet169':
         base_model = DenseNet169(weights='imagenet')
         self.preprocess_input = densenet.preprocess_input
     elif model_name == 'DenseNet201':
         base_model = DenseNet201(weights='imagenet')
         self.preprocess_input = densenet.preprocess_input
     elif model_name == 'NASNetLarge':
         base_model = NASNetLarge(weights='imagenet')
         self.preprocess_input = nasnet.preprocess_input
     elif model_name == 'NASNetMobile':
         base_model = NASNetMobile(weights='imagenet')
         self.preprocess_input = nasnet.preprocess_input
     elif model_name == 'MobileNet':
         base_model = MobileNet(weights='imagenet')
         self.preprocess_input = mobilenet.preprocess_input
     elif model_name == 'MobileNetV2':
         base_model = MobileNetV2(weights='imagenet')
         self.preprocess_input = mobilenet_v2.preprocess_input
     else:
         base_model = VGG16(weights='imagenet')
         self.preprocess_input = vgg16.preprocess_input
     self.model = Model(inputs=base_model.input,
                        outputs=base_model.layers[-2].output)
    def build_model(self, weights, includeTop, inputShape):
        baseModel = ResNet50V2(weights=weights,
                               include_top=includeTop,
                               input_shape=inputShape)

        model = Model(inputs=baseModel.input,
                      outputs=baseModel.get_layer('avg_pool').output)

        return model
Esempio n. 12
0
 def __init__(self,
              optimizer=Adam(),
              loss=categorical_crossentropy,
              metrics=[top_k_categorical_accuracy]):
     super().__init__(optimizer=optimizer,
                      loss=loss,
                      metrics=metrics,
                      MODEL_NAME=RESNET_NAME)
     self.resnet = ResNet50V2()
     self.resnet.layers[-1].activation = tf.keras.activations.linear
Esempio n. 13
0
    def __init__(self, network='resnet', num_classes=31):
        super(Classifier, self).__init__()
        self.num_classes = num_classes
        if network == 'resnet':
            self.feature_extractor = ResNet50V2(weights='imagenet', include_top=False, input_shape=(224, 224, 3),
                                                pooling='avg')
        else:
            raise NotImplementedError(f"Network {network} is not implemented yet.")

        self.label_predictor = Dense(self.num_classes)
Esempio n. 14
0
def load_VanilaResNet50():
    resnet = ResNet50V2(include_top=False, pooling="avg", weights='imagenet')
    for layer in resnet.layers:
        layer.trainable = False

    logits = Dense(2)(resnet.layers[-1].output)
    output = Activation('softmax')(logits)
    model = Model(resnet.input, output)
    model.load_weights("{}/resnet50best.hdf5".format(MODEL_DIR))

    return model
Esempio n. 15
0
def resnet50V2Model():
    baseModel = ResNet50V2(
        weights=None,
        include_top=False,
        input_shape=(32, 32, 3),
    )
    model_input = Input(shape=(32, 32, 3))
    x = baseModel(model_input)
    x = GlobalAveragePooling2D()(x)
    model_output = Dense(10, activation="softmax")(x)
    model = Model(inputs=model_input, outputs=model_output)
    return model
Esempio n. 16
0
    def __call__(self):
        resnet = ResNet50V2(include_top=False,
                            pooling="avg",
                            weights='imagenet')
        for layer in resnet.layers:
            layer.trainable = False

        logits = Dense(2)(resnet.layers[-1].output)
        output = Activation('softmax')(logits)
        model = Model(resnet.input, output)

        return model
def cifar_model(x_test, model_name='MobileNetV2', num_classes=10):
    # Load model from tensorlfow
    include_top = False
    weights = 'imagenet'
    input_tensor = Input(x_test.shape[1:], dtype='float16')
    input_shape = x_test.shape[1:]
    pooling = None
    classes = num_classes

    if model_name == 'MobileNetV2':
        KerasModel = MobileNetV2(include_top=include_top,
                                 weights=weights,
                                 input_tensor=input_tensor,
                                 input_shape=input_shape,
                                 pooling=pooling,
                                 classes=classes)

    elif model_name == 'ResNet50V2':
        KerasModel = ResNet50V2(include_top=include_top,
                                weights=weights,
                                input_tensor=input_tensor,
                                input_shape=input_shape,
                                pooling=pooling,
                                classifier_activation='softmax',
                                classes=classes)

    elif model_name == 'ResNet50':
        KerasModel = ResNet50(include_top=include_top,
                              weights=weights,
                              input_tensor=input_tensor,
                              input_shape=input_shape,
                              pooling=pooling,
                              classifier_activation='softmax',
                              classes=classes)

    elif model_name == 'DenseNet121':
        KerasModel = DenseNet121(include_top=include_top,
                                 weights=weights,
                                 input_tensor=input_tensor,
                                 input_shape=input_shape,
                                 pooling=pooling,
                                 classes=classes)
    inputs = KerasModel.input
    output = KerasModel.output
    x = GlobalAveragePooling2D()(output)
    x = Flatten()(x)
    x = Dense(num_classes, kernel_initializer='he_normal')(x)
    outputs = Softmax(dtype='float32')(x)

    # Instantiate model.
    model = Model(inputs=inputs, outputs=outputs)
    return model
Esempio n. 18
0
def transfer_resnet50v2():
    resnet50v2 = ResNet50V2(include_top=False,weights='imagenet',input_shape=(160,160,3))
    resnet50v2_preprocess = tf.keras.applications.resnet50.preprocess_input

    inputs = tf.keras.Input(shape=(160,160,3))
    x = resnet50v2_preprocess(inputs)
    x = resnet50v2(inputs,training=False)
    x = tf.keras.layers.GlobalAveragePooling2D()(x)
    outputs = tf.keras.layers.Dense(units=10,activation='sigmoid')(x)
    custom_resnet50v2 = tf.keras.Model(inputs,outputs)
    custom_resnet50v2.summary()

    return custom_resnet50v2
Esempio n. 19
0
    def __init__(self, n_classes=2):
        resnet = ResNet50V2(include_top=False,
                            pooling="avg",
                            weights='imagenet')
        for layer in resnet.layers:
            layer.trainable = False

        fc1 = Dense(100)(resnet.layers[-1].output)
        fc2 = Dense(100)(fc1)
        logits = Dense(2)(fc2)
        output = Activation('softmax')(logits)
        model = Model(resnet.input, output)

        return model
Esempio n. 20
0
    def loadModel(self, name, output_layer=None):

        model = None

        name = name.lower()

        self._model_name = name

        model = ResNet50V2(weights='imagenet', include_top=False)
        self._last_dim = 7
        model = Model(inputs=model.inputs,
                      outputs=model.layers[output_layer].output)

        self._model = model
Esempio n. 21
0
 def __get_bottleneck(self, ):
     if self.net == "mobilenet":
         bottleneck = MobileNetV2(input_shape=self.input_shape, include_top=False, weights='imagenet')
     elif self.net == "resnet":
         bottleneck = ResNet50V2(input_shape=self.input_shape, include_top=False, weights='imagenet')
     elif self.net == "vgg16":
         bottleneck = VGG16(include_top=False, weights='imagenet', input_shape=self.input_shape)
     elif self.net == "inception":
         bottleneck = InceptionNetwork(
             input_shape=self.input_shape,
             emd_size=self.emd_size,
             weights=self.kwargs.get('weights', None)
         )
     return bottleneck
Esempio n. 22
0
def frozen_resnet(input_size, n_classes):
    model_ = ResNet50V2(include_top=False, input_tensor=Input(shape=input_size))
    # include_top = False : flatten() layer 전 층까지만 가져다 쓴다
    # include_top = True : flatten() layer 까지 다 가져 와서 쓴다

    for layer in model_.layers:
        layer.trainable = False # frozen 상태로 가중치만 가져온다
    # x = Flatten(input_shape=model_.output_shape[1:])(model_.layers[-1].output)
    x = Flatten()(model_.layers[-1].output)
    x = Dense(n_classes, activation='softmax')(x)
    # output layer, n_classes : 클래스 개수(라벨의 개수)
    frozen_model = Model(model_.input, x)
    # 함수형 모델이므로 마지막에 input과 output을 Model()에 넣어준다

    return frozen_model
Esempio n. 23
0
    def __get_bottleneck(self, ):
        if self.net == "mobilenet":
            bottleneck = MobileNetV2(input_shape=self.input_shape,
                                     include_top=False,
                                     weights='imagenet')
        elif self.net == "resnet":
            bottleneck = ResNet50V2(input_shape=self.input_shape,
                                    include_top=False,
                                    weights='imagenet')
        elif self.net == "vgg16":
            bottleneck = VGG16(include_top=False,
                               weights='imagenet',
                               input_shape=self.input_shape)

        return bottleneck
Esempio n. 24
0
def resnet50V2_build_model():
    w_init = glorot_normal()
    b_init = Zeros()
    res_net50v2 = ResNet50V2(input_shape=(512, 512, 3),
                             include_top=False,
                             weights="imagenet",
                             pooling="avg")

    for layer in res_net50v2.layers:
        layer.trainable = False

    model = Sequential()
    model.add(res_net50v2)
    model.add(Flatten())
    model.add(Dropout(0.2))
    model.add(
        Dense(276,
              activation='relu',
              kernel_initializer=w_init,
              bias_initializer=b_init,
              kernel_regularizer='l2'))
    model.add(BatchNormalization())
    model.add(
        Dense(3,
              activation='softmax',
              kernel_initializer=w_init,
              bias_initializer=b_init,
              kernel_regularizer='l2'))
    model.summary()

    optimizer = tf.keras.optimizers.Adam(
        learning_rate=0.00001,
        beta_1=0.9,
        beta_2=0.999,
        epsilon=1e-08,
        amsgrad=False,
        name="Adam",
    )

    loss = tf.keras.losses.CategoricalCrossentropy(
        from_logits=False,
        label_smoothing=0.05,
        reduction="auto",
        name="categorical_crossentropy",
    )
    model.compile(optimizer=optimizer, loss=loss, metrics=['accuracy'])

    return model
def get_encoder_model(name, in_shape, pooling):
    if name == "InceptionV3":
        model = InceptionV3(include_top=False,
                            input_shape=in_shape,
                            weights=None,
                            pooling=pooling)
    elif name == "ResNet50":
        model = ResNet50(include_top=False,
                         input_shape=in_shape,
                         weights=None,
                         pooling=pooling)
    elif name == "ResNet50V2":
        model = ResNet50V2(include_top=False,
                           input_shape=in_shape,
                           weights=None,
                           pooling=pooling)
    elif name == "ResNet101":
        model = ResNet101(include_top=False,
                          input_shape=in_shape,
                          weights=None,
                          pooling=pooling)
    elif name == "ResNet101V2":
        model = ResNet101V2(include_top=False,
                            input_shape=in_shape,
                            weights=None,
                            pooling=pooling)
    elif name == "ResNet152":
        model = ResNet152(include_top=False,
                          input_shape=in_shape,
                          weights=None,
                          pooling=pooling)
    elif name == "InceptionResNetV2":
        model = InceptionResNetV2(include_top=False,
                                  input_shape=in_shape,
                                  weights=None,
                                  pooling=pooling)
    elif name == "DenseNet121":
        model = DenseNet121(include_top=False,
                            input_shape=in_shape,
                            weights=None,
                            pooling=pooling)
    else:
        raise ValueError("model " + name + " not found")

    return model
Esempio n. 26
0
    def _build_network(self, input_tensor, is_training):
        """
        https://github.com/bgshih/crnn/blob/master/model/crnn_demo/config.lua
        :return:
        """
        net = tf.add(input_tensor, (-128.0))
        net = tf.multiply(net, (1 / 128.0))
        resnet = ResNet50V2(weights="imagenet",
                            input_tensor=net,
                            input_shape=(300, 40, 3),
                            include_top=False)
        for layer in resnet.layers:
            layer.trainable = False
        # resnet = resnet.layers[-37].output

        net = tf.squeeze(resnet.layers[-34].output, axis=1)
        # net = Flatten()(net)
        print(net)
        self.model = net
    def define_model(self):
        if config.BASE_MODEL == 'ResNet50V2':
            # Pre-trained model with MobileNetV2
            base_model = ResNet50V2(input_shape=config.IMG_SHAPE,
                                    include_top=False,
                                    weights='imagenet')
            print("Number of layers in the base model: ",
                  len(base_model.layers))
            head_model = base_model
            for layers in base_model.layers[:self.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.2)(head_model)
            prediction_layer = tf.keras.layers.Dense(
                len(config.CLASS_NAMES), activation='softmax')(head_model)
            model = tf.keras.Model(inputs=base_model.input,
                                   outputs=prediction_layer)

        if config.BASE_MODEL == 'InceptionV3':
            base_model = InceptionV3(input_shape=config.IMG_SHAPE,
                                     include_top=False,
                                     weights='imagenet')
            print("Number of layers in the base model: ",
                  len(base_model.layers))
            head_model = base_model
            for layers in base_model.layers[:self.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(config.CLASS_NAMES), activation='softmax')(head_model)
            model = tf.keras.Model(inputs=base_model.input,
                                   outputs=prediction_layer)
        return model
Esempio n. 28
0
def get_model(transfer_layer):
    model = ResNet50V2(weights="imagenet",
                       include_top=False,
                       input_shape=(224, 224, 3))
    for layer in model.layers:
        layer.trainable = False

    X = model.get_layer(transfer_layer).output
    X = tf.keras.layers.Conv2D(64, 1)(X)
    x = tf.keras.layers.BatchNormalization()(X)
    X = tf.keras.layers.Activation("relu")(X)
    X = tf.keras.layers.GlobalAveragePooling2D()(X)
    X = tf.keras.layers.Dense(32, activation='relu')(X)
    X = tf.keras.layers.Dense(classes, activation='sigmoid')(X)
    new_model = tf.keras.Model(inputs=model.input, outputs=X)

    Input_Layer = tf.keras.layers.Input(shape=(224, 224, 3))
    In = tf.keras.layers.BatchNormalization()(Input_Layer)
    out = new_model(In)
    new_model = tf.keras.Model(inputs=Input_Layer, outputs=out)
    return new_model
Esempio n. 29
0
    def __init__(self, n_classes, name='resnet50', weights='imagenet'):
        """
        :param n_classes: int, number of classes (units in the last layer)
        :param name: string, name of the model
        :param weights: one of 'imagenet', or path to the pretrained weights to load
        """
        super().__init__(name=name)
        self._n_classes = n_classes
        self._transfer_learning = weights is not None

        if weights is not None and weights != 'imagenet':  # weights for the whole model
            load_whole = True
            weights_resnet = None
        else:
            load_whole = False
            weights_resnet = weights

        self._feature_extractor = Sequential([
            Rescaling(1. / 127.5, offset=-1),
            ResNet50V2(include_top=False,
                       weights=weights_resnet,
                       input_shape=self.image_shape)
        ],
                                             name='feature_extractor')

        self._classifier = Sequential(
            [GlobalAveragePooling2D(),
             Dense(n_classes, activation='softmax')],
            name='classifier')

        if load_whole:
            self.load_weights(weights)

        # required for summary()
        inputs = Input(shape=self.image_shape)
        outputs = self.call(inputs)
        super().__init__(name=name, inputs=inputs, outputs=outputs)
        self.build(input_shape=(None, self.image_shape[0], self.image_shape[1],
                                self.image_shape[2]))
Esempio n. 30
0
    def __init__(self, weights_init, model_architecture='vgg16'):

        self.weights_init = weights_init
        if model_architecture == 'vgg16':
            self.model = VGG16(weights=self.weights_init, include_top=False)
            self.bridge_list = [2, 5, 9, 13, 17]

        elif model_architecture == 'vgg19':
            self.model = VGG19(weights=self.weights_init, include_top=False)
            self.bridge_list = [2, 5, 10, 15, 20]

        elif model_architecture == 'resnet50':
            self.model = ResNet50(weights=self.weights_init, include_top=False)
            self.bridge_list = [4, 38, 80, 142, -1]

        elif model_architecture == 'resnet50v2':
            self.model = ResNet50V2(weights=self.weights_init,
                                    include_top=False)
            self.bridge_list = [2, 27, 62, 108, -1]

        elif model_architecture == 'resnet101':
            self.model = ResNet101(weghts=self.weights_init, include_top=False)
            self.bridge_list = [4, 38, 80, 312, -1]

        elif model_architecture == 'resnet101v2':
            self.model = ResNet101V2(weights=self.weights_init,
                                     include_top=False)
            self.bridge_list = [2, 27, 62, 328, -1]

        elif model_architecture == 'resnet152':
            self.model = ResNet152(weights=self.weights_init,
                                   include_top=False)
            self.bridge_list = [4, 38, 120, 482, -1]

        elif model_architecture == 'resnet152v2':
            self.model = ResNet152V2(weights=self.weights_init,
                                     include_top=False)
            self.bridge_list = [2, 27, 117, 515, -1]