Beispiel #1
0
def get_model(use_model) -> Model:
    if use_model == 'MobileNetV2':
        conv_model: Model = MobileNetV2(weights='imagenet', include_top=False, input_shape=IMG_SHAPE)

    else:
        conv_model: Model = ResNet50(weights='imagenet', include_top=False, input_shape=IMG_SHAPE)

    layer: Layer
    for layer in conv_model.layers:
        layer.trainable = False

    image_a = Input(IMG_SHAPE)
    image_b = Input(IMG_SHAPE)

    branch_a = conv_model(image_a)
    branch_b = conv_model(image_b)

    merged_layers = concatenate([branch_a, branch_b])
    merged_layers = GlobalAveragePooling2D()(merged_layers)

    merged_layers = Dense(256, activation='relu')(merged_layers)
    merged_layers = Dropout(0.1)(merged_layers)

    merged_layers = Dense(256, activation='relu')(merged_layers)
    merged_layers = Dropout(0.0)(merged_layers)

    output = Dense(1, kernel_initializer='normal', activation='linear')(merged_layers)
    model = Model(inputs=[image_a, image_b], outputs=output)

    model.compile(optimizer=tf.keras.optimizers.Adam(0.00100),
                  loss='mse',
                  metrics=[loss_in_fact])

    return model
Beispiel #2
0
def mobilenetv2_yolo_body(inputs, num_anchors, num_classes, alpha=1.0):
    # input: 416 x 416 x 3
    # conv_pw_13_relu :13 x 13 x 1024
    # conv_pw_11_relu :26 x 26 x 512
    # conv_pw_5_relu : 52 x 52 x 256
    mobilenetv2 = MobileNetV2(input_tensor=inputs, include_top=False, weights='imagenet')
    x, y1 = make_last_layers_mobilenet(mobilenetv2.output, 17, 512, num_anchors * (num_classes + 5))
    x = Conv2D(256, kernel_size=1, padding='same', use_bias=False, name='block_20_conv')(x)
    x = BatchNormalization(momentum=0.9, name='block_20_BN')(x)
    x = ReLU(6., name='block_20_relu6')(x)
    x = UpSampling2D(2)(x)
    x = Concatenate()([x, MobilenetConv2D(mobilenetv2.get_layer('block_12_project_BN').output, (1, 1), alpha, 384)])

    x, y2 = make_last_layers_mobilenet(x, 21, 256, num_anchors * (num_classes + 5))
    x = Conv2D(128, kernel_size=1, padding='same', use_bias=False, name='block_24_conv')(x)
    x = BatchNormalization(momentum=0.9, name='block_24_BN')(x)
    x = ReLU(6., name='block_24_relu6')(x)
    x = UpSampling2D(2)(x)
    x = Concatenate()([x, MobilenetConv2D(mobilenetv2.get_layer('block_5_project_BN').output, (1, 1), alpha, 128)])
    x, y3 = make_last_layers_mobilenet(x, 25, 128, num_anchors * (num_classes + 5))

    # y1 = Lambda(lambda y: tf.reshape(y, [-1, tf.shape(y)[1],tf.shape(y)[2], num_anchors, num_classes + 5]),name='y1')(y1)
    # y2 = Lambda(lambda y: tf.reshape(y, [-1, tf.shape(y)[1],tf.shape(y)[2], num_anchors, num_classes + 5]),name='y2')(y2)
    # y3 = Lambda(lambda y: tf.reshape(y, [-1, tf.shape(y)[1],tf.shape(y)[2], num_anchors, num_classes + 5]),name='y3')(y3)
    return Model(inputs, [y1, y2, y3])
Beispiel #3
0
def GetModel():
    base_model = MobileNetV2(input_shape=(224, 224, 3),
                             weights='imagenet',
                             include_top=False,
                             pooling='max')
    for layer in base_model.layers:
        layer.trainable = False

    x = base_model.output
    x = Dropout(0.6)(x)
    x = Dense(embedding_dim)(x)
    x = Lambda(lambda x: K.l2_normalize(x, axis=1))(x)
    embedding_model = Model(base_model.input, x, name='embedding')

    input_shape = (image_size, image_size, 3)
    anchor_input = Input(input_shape, name='anchor_input')
    positive_input = Input(input_shape, name='positive_input')
    negative_input = Input(input_shape, name='negative_input')
    anchor_embedding = embedding_model(anchor_input)
    positive_embedding = embedding_model(positive_input)
    negative_embedding = embedding_model(negative_input)

    inputs = [anchor_input, positive_input, negative_input]
    outputs = [anchor_embedding, positive_embedding, negative_embedding]

    triplet_model = Model(inputs, outputs)
    triplet_model.add_loss(K.mean(triplet_loss(outputs)))

    return embedding_model, triplet_model
Beispiel #4
0
def siamese_encoder(input_shape):
    """Creates and returns model of encoder part of Siamese network."""

    model = MobileNetV2(include_top=False,
                        pooling="max",
                        weights="imagenet",
                        input_shape=input_shape)
    return model
    def create_model(self, num_outputs):
        print('[Dronet] Starting dronet with MobileNetV2')

        self.mobile = MobileNetV2(include_top=self.include_top,
                                  weights=None,
                                  classes=num_outputs)

        print("self.mobile.summary()======", self.mobile.summary())

        print('[Dronet] Done with dronet with MobileNetV2')
Beispiel #6
0
def get_mobilev2_model(classes=2):
    def preprocess_input(img):
        img = img / 128.
        img = img - 1.
        return img.astype(np.float32)

    def decode_img(img):
        img = img + 1.
        img = img * 128.
        return img.astype(np.uint8)

    base_model = MobileNetV2(include_top=False, input_shape=(224, 224, 3))
    x = base_model.output
    x = GlobalAveragePooling2D()(x)
    pre = Dense(classes, activation='softmax')(x)
    model = Model(inputs=base_model.input, outputs=pre)
    model.summary()
    # 冻结这些层就无法训练
    # 迁移学习,用训练好的权重,重写全连接层再进行训练
    for layer in base_model.layers:
        layer.trainable = False

    ckpt = './ckpt/mobilev2.h5'
    checkpoint = ModelCheckpoint(filepath=ckpt)
    tensorboard = './log/mobilev2'
    tensorboard = TensorBoard(log_dir=tensorboard)
    if os.path.exists(ckpt):
        model.load_weights(ckpt)
        print('load done')
    else:
        plot_model(model, to_file='mobilev2.png')

    sgd = SGD(lr=1e-2, decay=1e-6, momentum=0.9, nesterov=True)
    model.compile(optimizer=sgd,
                  loss='binary_crossentropy',
                  metrics=['accuracy'])
    return model, checkpoint, tensorboard, preprocess_input, decode_img
Beispiel #7
0
def siamese_network(input_shape):
    """Creates and returns model of encoder part of Siamese network."""

    left_input = Input(input_shape)
    right_input = Input(input_shape)

    model = MobileNetV2(include_top=False,
                        pooling="max",
                        weights="imagenet",
                        input_shape=input_shape)

    model.trainable = False

    x = Dense(1280)(model.output)
    x = Activation('relu')(x)
    x = Dropout(0.2)(x)
    x = Dense(1280)(x)
    x = Activation('relu')(x)
    x = Dropout(0.2)(x)

    model = Model(model.input, x)

    encoded_l = model(left_input)
    encoded_r = model(right_input)

    distance = Lambda(euclidean_distance, output_shape=eucl_dist_output_shape)(
        [encoded_l, encoded_r])

    siamese_net = Model(inputs=[left_input, right_input], outputs=distance)

    siamese_net.compile(loss=contrastive_loss,
                        optimizer='rmsprop',
                        metrics=[accuracy])

    siamese_net.summary()

    return siamese_net
Beispiel #8
0
def construct_model():
    conv_base = MobileNetV2(input_shape=None,
                            include_top=False,
                            weights='imagenet',
                            pooling='max')

    conv_features = conv_base.output

    finger_status = layers.Dense(3, activation='softmax',
                                 name="finger_status")(
                                     conv_features)  # none, closed, open
    finger_positions = layers.Dense(4, activation='relu', name="finger_pos")(
        conv_features)  # y1, x1, y2, x2
    face_status = layers.Dense(1, activation='sigmoid', name="face_status")(
        conv_features)  # none, present
    face_position = layers.Dense(4, activation='relu', name="face_pos")(
        conv_features)  # y1, x1, y2, x2

    final_outputs = [
        finger_status, finger_positions, face_status, face_position
    ]
    model = models.Model(inputs=conv_base.input, outputs=final_outputs)

    return model
Beispiel #9
0
def DeepLabV3(config: ModelConfig):
    """ Build DeepLabv3+ model

    :param config: ModelConfiguration Class,
                   Reference - `models.config` module
    :return: Keras Model
    """
    # 전처리 부분 구성하기
    inputs = Input(shape=config.INPUT_SHAPE)

    # BackBone Network 구성하기
    if config.BACKBONE == 'MobileNetV2':
        from tensorflow.python.keras.applications import MobileNetV2
        preprocess = MeanShift()(inputs)  # (0,255) -> (-1, 1)
        base = MobileNetV2(input_tensor=preprocess,
                           alpha=config.BACKBONE_ALPHA,
                           include_top=False)
        skip_input = (base.get_layer(
            config.BACKBONE_LOW_FEATURE_MAP_NAME).output)
        aspp_input = (base.get_layer(
            config.BACKBONE_HIGH_FEATURE_MAP_NAME).output)
    else:
        raise ValueError(f"{config.BACKBONE} is not implemented")

    # ASPP Module Network 구성하기
    encoded_fmap = aspp_module(aspp_input,
                               num_features=config.ASPP_NUM_FEATURES,
                               atrous_rate=config.ASPP_ATROUS_RATE,
                               USE_GROUPNORM=config.USE_GROUPNORM,
                               GROUPS=config.GROUPS)

    # Decoder Network 구성하기
    logits = deeplab_decoder(
        encoded_fmap,
        skip_input,
        num_depth=config.DECODER_NUM_DEPTH,
        num_features=config.DECODER_NUM_FEATURES,
        num_skip_features=config.DECODER_NUM_SKIP_FEATURES,
        use_separable_conv=config.DECODER_USE_SEPARABLE_CONV,
        USE_GROUPNORM=config.USE_GROUPNORM,
        GROUPS=config.GROUPS)

    outputs = []
    if config.SEPARATE_BG_CLASSIFIER:
        # BackGround Classifier 분리
        bg_pred = Conv2D(1, (1, 1), activation='sigmoid')(logits)
        bg_pred = ResizeLike(name='bg_prediction')([bg_pred, base.input])
        outputs.append(bg_pred)

    if config.SEPARATE_CRACK_CLASSIFIER:
        # Label Classifier와 Crack Classifier를 분리
        label_pred = Conv2D(config.NUM_CLASSES, (1, 1),
                            activation='softmax')(logits)
        label_pred = ResizeLike(name='label_prediction')(
            [label_pred, base.input])

        abnormal_pred = Conv2D(1, (1, 1), activation='sigmoid')(logits)
        abnormal_pred = ResizeLike(name='crack_prediction')(
            [abnormal_pred, base.input])
        outputs.extend([label_pred, abnormal_pred])
    else:
        # Label Classifier만을 둚
        prediction = Conv2D(config.NUM_CLASSES, (1, 1), name='logits')(logits)
        prediction = ResizeLike(name='label_prediction')(
            [prediction, base.input])
        outputs.append(prediction)

    # Model 선언
    model = Model(inputs, outputs, name='deeplabv3')

    # Freeze the BackBone Network
    if config.BACKBONE_FREEZE:
        freeze_flag = True
        for layer in model.layers:
            if freeze_flag:
                layer.trainable = False
            else:
                layer.trainable = True

            if layer.name == config.BACKBONE_HIGH_FEATURE_MAP_NAME:
                # model train the weights After BACKBONE Network
                freeze_flag = False

    return model
                                                  stratify=labels,
                                                  random_state=42)

# construct the training image generator for data augmentation
aug = ImageDataGenerator(rotation_range=20,
                         zoom_range=0.15,
                         width_shift_range=0.2,
                         height_shift_range=0.2,
                         shear_range=0.15,
                         horizontal_flip=True,
                         fill_mode="nearest")

# load the MobileNetV2 network, ensuring the head FC layer sets are
# left off
baseModel = MobileNetV2(weights="imagenet",
                        include_top=False,
                        input_tensor=Input(shape=(224, 224, 3)))

# construct the head of the model that will be placed on top of the
# the base model
headModel = baseModel.output
headModel = AveragePooling2D(pool_size=(7, 7))(headModel)
headModel = Flatten(name="flatten")(headModel)
headModel = Dense(128, activation="relu")(headModel)
headModel = Dropout(0.5)(headModel)
headModel = Dense(2, activation="softmax")(headModel)

# place the head FC model on top of the base model (this will become
# the actual model we will train)
model = Model(inputs=baseModel.input, outputs=headModel)
Beispiel #11
0
"""
we set up a sequential model that we can add layers to
"""
my_new_model = Sequential()
"""
first we add all of pre-trained  model
we've written include_top=False, this is how specify that we want to exlude
the layer that makes prediction into the thousands of categories used in the ImageNet competition
we set the weights to be 'ImageNet' to specify that we use the pre-traind model on ImageNet
pooling equals average says that if we had extra channels in our tensor at the end of this step
we want to collapse them to 1d tensor by taking an average across channels
now we have a pre-trained model that creates the layer before the last layer
that we saw in the slides
"""
my_new_model.add(
    MobileNetV2(weights='imagenet', include_top=False, pooling='avg'))
"""
we add a dense layer to make predictions,
we specify the number of nodes in this layer which in this case is
the number of classes,
then we want to apply the softmax function to turn it into probabilities 
"""
my_new_model.add(Dense(
    num_classes,
    activation='softmax',
))
"""
we tell tensor flow not to train the first layer which is the  pre-trained model
because that's the model that was already pre-trained with the ImageNet data
"""
my_new_model.layers[0].trainable = False