コード例 #1
0
def yolo3_spp_xception_body(inputs, num_anchors, num_classes):
    """Create YOLO_V3 SPP Xception model CNN body in Keras."""
    xception = Xception(input_tensor=inputs,
                        weights='imagenet',
                        include_top=False)

    # input: 416 x 416 x 3
    # block14_sepconv2_act: 13 x 13 x 2048
    # block13_sepconv2_bn(middle in block13): 26 x 26 x 1024
    # add_46(end of block12): 26 x 26 x 728
    # block4_sepconv2_bn(middle in block4) : 52 x 52 x 728
    # add_37(end of block3) : 52 x 52 x 256

    f1 = xception.get_layer('block14_sepconv2_act').output
    # f1 :13 x 13 x 2048
    x, y1 = make_spp_last_layers(f1, 1024, num_anchors * (num_classes + 5))

    x = compose(DarknetConv2D_BN_Leaky(512, (1, 1)), UpSampling2D(2))(x)

    f2 = xception.get_layer('block13_sepconv2_bn').output
    # f2: 26 x 26 x 1024
    x = Concatenate()([x, f2])

    x, y2 = make_last_layers(x, 512, num_anchors * (num_classes + 5))

    x = compose(DarknetConv2D_BN_Leaky(256, (1, 1)), UpSampling2D(2))(x)

    f3 = xception.get_layer('block4_sepconv2_bn').output
    # f3 : 52 x 52 x 728
    x = Concatenate()([x, f3])
    x, y3 = make_last_layers(x, 256, num_anchors * (num_classes + 5))

    return Model(inputs=inputs, outputs=[y1, y2, y3])
コード例 #2
0
def keras_applications_xception(inputs):
    from tensorflow.keras.applications.xception import Xception

    base_model = Xception(input_tensor=inputs,
                          weights='imagenet',
                          include_top=False)

    tf.keras.utils.plot_model(base_model, 'model.png')

    for layer in base_model.layers:
        print(layer.name, layer.output)

    outputs = {
        '4x':
        tf.keras.layers.ZeroPadding2D(
            ((0, 1), (0, 1)))(base_model.get_layer('add').output),
        '8x':
        base_model.get_layer('add_1').output,
        '16x':
        base_model.get_layer('add_10').output,
        '32x':
        base_model.get_layer('block14_sepconv2_act').output
    }

    return outputs
コード例 #3
0
def tiny_yolo3_xception_body(inputs, num_anchors, num_classes):
    '''Create Tiny YOLO_v3 Xception model CNN body in keras.'''
    xception = Xception(input_tensor=inputs,
                        weights='imagenet',
                        include_top=False)

    # input: 416 x 416 x 3
    # block14_sepconv2_act: 13 x 13 x 2048
    # block13_sepconv2_bn(middle in block13): 26 x 26 x 1024
    # add_46(end of block12): 26 x 26 x 728
    # block4_sepconv2_bn(middle in block4) : 52 x 52 x 728
    # add_37(end of block3) : 52 x 52 x 256

    x1 = xception.get_layer('block13_sepconv2_bn').output
    # x1 :26 x 26 x 1024
    x2 = xception.get_layer('block14_sepconv2_act').output
    # x2 :13 x 13 x 2048
    x2 = DarknetConv2D_BN_Leaky(1024, (1, 1))(x2)

    y1 = compose(
        DarknetConv2D_BN_Leaky(2048, (3, 3)),
        #Depthwise_Separable_Conv2D_BN_Leaky(filters=2048, kernel_size=(3, 3), block_id_str='14'),
        DarknetConv2D(num_anchors * (num_classes + 5), (1, 1)))(x2)

    x2 = compose(DarknetConv2D_BN_Leaky(512, (1, 1)), UpSampling2D(2))(x2)
    y2 = compose(
        Concatenate(),
        DarknetConv2D_BN_Leaky(1024, (3, 3)),
        #Depthwise_Separable_Conv2D_BN_Leaky(filters=1024, kernel_size=(3, 3), block_id_str='15'),
        DarknetConv2D(num_anchors * (num_classes + 5), (1, 1)))([x2, x1])

    return Model(inputs, [y1, y2])
コード例 #4
0
def tiny_yolo3lite_xception_body(inputs, num_anchors, num_classes):
    '''Create Tiny YOLO_v3 Lite Xception model CNN body in keras.'''
    xception = Xception(input_tensor=inputs,
                        weights='imagenet',
                        include_top=False)

    # input: 416 x 416 x 3
    # block14_sepconv2_act: 13 x 13 x 2048
    # block13_sepconv2_bn(middle in block13): 26 x 26 x 1024
    # add_46(end of block12): 26 x 26 x 728
    # block4_sepconv2_bn(middle in block4) : 52 x 52 x 728
    # add_37(end of block3) : 52 x 52 x 256

    # f1 :13 x 13 x 2048
    f1 = xception.get_layer('block14_sepconv2_act').output
    # f2 :26 x 26 x 1024
    f2 = xception.get_layer('block13_sepconv2_bn').output

    f1_channel_num = 2048
    f2_channel_num = 1024
    #f1_channel_num = 1024
    #f2_channel_num = 512

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

    return Model(inputs, [y1, y2])
コード例 #5
0
def yolo3lite_xception_body(inputs, num_anchors, num_classes):
    '''Create YOLO_v3 Lite Xception model CNN body in keras.'''
    xception = Xception(input_tensor=inputs, weights='imagenet', include_top=False)
    print('backbone layers number: {}'.format(len(xception.layers)))

    # input: 416 x 416 x 3
    # block14_sepconv2_act: 13 x 13 x 2048
    # block13_sepconv2_bn(middle in block13): 26 x 26 x 1024
    # add_46(end of block12): 26 x 26 x 728
    # block4_sepconv2_bn(middle in block4) : 52 x 52 x 728
    # add_37(end of block3) : 52 x 52 x 256

    # f1: 13 x 13 x 2048
    f1 = xception.get_layer('block14_sepconv2_act').output
    # f2: 26 x 26 x 1024
    f2 = xception.get_layer('block13_sepconv2_bn').output
    # f3: 52 x 52 x 728
    f3 = xception.get_layer('block4_sepconv2_bn').output

    #f1_channel_num = 2048
    #f2_channel_num = 1024
    #f3_channel_num = 728
    f1_channel_num = 1024
    f2_channel_num = 512
    f3_channel_num = 256

    y1, y2, y3 = yolo3lite_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])
コード例 #6
0
def yolo3_spp_xception_body(inputs, num_anchors, num_classes):
    """Create YOLO_V3 SPP Xception model CNN body in Keras."""
    xception = Xception(input_tensor=inputs,
                        weights='imagenet',
                        include_top=False)

    # input: 416 x 416 x 3
    # block14_sepconv2_act: 13 x 13 x 2048
    # block13_sepconv2_bn(middle in block13): 26 x 26 x 1024
    # add_46(end of block12): 26 x 26 x 728
    # block4_sepconv2_bn(middle in block4) : 52 x 52 x 728
    # add_37(end of block3) : 52 x 52 x 256

    # f1: 13 x 13 x 2048
    f1 = xception.get_layer('block14_sepconv2_act').output
    # f2: 26 x 26 x 1024
    f2 = xception.get_layer('block13_sepconv2_bn').output
    # f3: 52 x 52 x 728
    f3 = xception.get_layer('block4_sepconv2_bn').output

    #f1_channel_num = 2048
    #f2_channel_num = 1024
    #f3_channel_num = 728
    f1_channel_num = 1024
    f2_channel_num = 512
    f3_channel_num = 256

    y1, y2, y3 = yolo3_predictions(
        (f1, f2, f3), (f1_channel_num, f2_channel_num, f3_channel_num),
        num_anchors,
        num_classes,
        use_spp=True)

    return Model(inputs=inputs, outputs=[y1, y2, y3])
コード例 #7
0
def yolo3lite_xception_body(inputs, num_anchors, num_classes):
    '''Create YOLO_v3 Lite Xception model CNN body in keras.'''
    xception = Xception(input_tensor=inputs,
                        weights='imagenet',
                        include_top=False)

    # input: 416 x 416 x 3
    # block14_sepconv2_act: 13 x 13 x 2048
    # block13_sepconv2_bn(middle in block13): 26 x 26 x 1024
    # add_46(end of block12): 26 x 26 x 728
    # block4_sepconv2_bn(middle in block4) : 52 x 52 x 728
    # add_37(end of block3) : 52 x 52 x 256

    # f1: 13 x 13 x 2048
    f1 = xception.get_layer('block14_sepconv2_act').output
    # f2: 26 x 26 x 1024
    f2 = xception.get_layer('block13_sepconv2_bn').output
    # f3: 52 x 52 x 728
    f3 = xception.get_layer('block4_sepconv2_bn').output

    #f1_channel_num = 2048
    #f2_channel_num = 1024
    #f3_channel_num = 728
    f1_channel_num = 1024
    f2_channel_num = 512
    f3_channel_num = 256

    #feature map 1 head & output (13x13 for 416 input)
    x, y1 = make_depthwise_separable_last_layers(f1,
                                                 f1_channel_num // 2,
                                                 num_anchors *
                                                 (num_classes + 5),
                                                 block_id_str='14')

    #upsample fpn merge for feature map 1 & 2
    x = compose(DarknetConv2D_BN_Leaky(f2_channel_num // 2, (1, 1)),
                UpSampling2D(2))(x)
    x = Concatenate()([x, f2])

    #feature map 2 head & output (26x26 for 416 input)
    x, y2 = make_depthwise_separable_last_layers(x,
                                                 f2_channel_num // 2,
                                                 num_anchors *
                                                 (num_classes + 5),
                                                 block_id_str='15')

    #upsample fpn merge for feature map 2 & 3
    x = compose(DarknetConv2D_BN_Leaky(f3_channel_num // 2, (1, 1)),
                UpSampling2D(2))(x)
    x = Concatenate()([x, f3])

    #feature map 3 head & output (52x52 for 416 input)
    x, y3 = make_depthwise_separable_last_layers(x,
                                                 f3_channel_num // 2,
                                                 num_anchors *
                                                 (num_classes + 5),
                                                 block_id_str='16')

    return Model(inputs=inputs, outputs=[y1, y2, y3])
コード例 #8
0
def tiny_yolo3lite_xception_body(inputs, num_anchors, num_classes):
    '''Create Tiny YOLO_v3 Lite Xception model CNN body in keras.'''
    xception = Xception(input_tensor=inputs,
                        weights='imagenet',
                        include_top=False)

    # input: 416 x 416 x 3
    # block14_sepconv2_act: 13 x 13 x 2048
    # block13_sepconv2_bn(middle in block13): 26 x 26 x 1024
    # add_46(end of block12): 26 x 26 x 728
    # block4_sepconv2_bn(middle in block4) : 52 x 52 x 728
    # add_37(end of block3) : 52 x 52 x 256

    # f1 :13 x 13 x 2048
    f1 = xception.get_layer('block14_sepconv2_act').output
    # f2 :26 x 26 x 1024
    f2 = xception.get_layer('block13_sepconv2_bn').output

    f1_channel_num = 2048
    f2_channel_num = 1024
    #f1_channel_num = 1024
    #f2_channel_num = 512

    #feature map 1 transform
    x1 = DarknetConv2D_BN_Leaky(f1_channel_num // 2, (1, 1))(f1)

    #feature map 1 output (13x13 for 416 input)
    y1 = compose(
        #DarknetConv2D_BN_Leaky(f1_channel_num, (3,3)),
        Depthwise_Separable_Conv2D_BN_Leaky(filters=f1_channel_num,
                                            kernel_size=(3, 3),
                                            block_id_str='14'),
        DarknetConv2D(num_anchors * (num_classes + 5), (1, 1)))(x1)

    #upsample fpn merge for feature map 1 & 2
    x2 = compose(DarknetConv2D_BN_Leaky(f2_channel_num // 2, (1, 1)),
                 UpSampling2D(2))(x1)

    #feature map 2 output (26x26 for 416 input)
    y2 = compose(
        Concatenate(),
        #DarknetConv2D_BN_Leaky(f2_channel_num, (3,3)),
        Depthwise_Separable_Conv2D_BN_Leaky(filters=f2_channel_num,
                                            kernel_size=(3, 3),
                                            block_id_str='15'),
        DarknetConv2D(num_anchors * (num_classes + 5), (1, 1)))([x2, f2])

    return Model(inputs, [y1, y2])
コード例 #9
0
def load_cnn_model():
    """
    Loads the Xception pre-trained CNN model
    :return: Xception model
    """
    base_model = Xception(weights='imagenet', include_top=True)

    # get the feature outputs of second-to-last layer (final FC layer)
    outputs = base_model.get_layer('avg_pool').output

    cnn_model = Model(inputs=base_model.input, outputs=outputs)

    return cnn_model
コード例 #10
0
def create_model():
    # Image shape
    image_input = Input(shape=(160, 120, 3))

    # Set weights to none. Remove dense layers of model
    model = Xception(input_tensor=image_input, include_top=False, weights=None)

    # Add custom fully connected layers
    last_layer = model.get_layer('block14_sepconv2_act').output
    x = GlobalAveragePooling2D()(last_layer)
    x = Dense(512, activation='relu', name='fc1')(x)
    x = Dense(128, activation='relu', name='fc2')(x)
    out = Dense(NUM_CLASSES, activation='linear', name='output')(x)

    return Model(image_input, out)
    
if not os.path.exists('results'):
    os.makedirs('results')
            
for i in range(1,50) :
    
    print ("[STATUS] start time - {}".format(datetime.datetime.now().strftime("%H:%M")))
    
    
    image_input = Input(shape=(224, 224, 3))
    
    base_model = Xception(input_tensor=image_input, include_top=False,weights='imagenet')
    
    
    
    last_layer = base_model.get_layer('block14_sepconv2_act').output
    x = Flatten(name='flatten')(last_layer) 
    x = Dense(128, activation='relu', name='fc1')(x)
    x = Dense(128, activation='relu', name='fc2')(x)
    out = Dense(9 , activation='softmax', name='output')(x)
    custom_Xception_model = Model(image_input, out)
    
    # freeze all the layers except the dense layers
    for layer in custom_Xception_model.layers[:2]:
    	layer.trainable = False
    
    train_datagen = ImageDataGenerator(
            rescale=1./255,
            shear_range=0.2,
            zoom_range=0.2,
            horizontal_flip=True)
コード例 #12
0
ファイル: features.py プロジェクト: DIDSR/ThresholdoutAUC
    def _get_ImageNet_trained_model(self, model):
        """Initialize an ImageNet pre-trained model

        After the chosen convolution neural network (see `model` for options)
        is loaded with the ImageNet pre-trained weights from
        `tensorflow.keras.applications`, the last fully connected layer(s) is
        removed to turn the classification model into a feature extractor.

        Parameters
        ----------
        model : str
            Name of the pre-trained deep learning model to be used for feature
            extraction. Can be one of "ResNet50", "DenseNet121", "DenseNet201",
            "VGG16", "VGG19", "MobileNet", "MobileNetV2", "Xception",
            "InceptionV3".

        Returns
        -------
        feature_extractor
            A Keras model object.
        image_size : tuple
            Image size as (height, width).
        preprocess_input
            A preprocessing function - the `preprocess_input` function from
            `tensorflow.keras.applications` that corresponds to the chosen `model`.
        """
        if model == "ResNet50":
            from tensorflow.keras.applications.resnet50 import ResNet50, preprocess_input

            image_size = (224, 224)
            res = ResNet50(input_shape=image_size + (3, ),
                           weights="imagenet",
                           include_top=True)
            feature_extractor = tensorflow.keras.models.Model(
                inputs=res.input, outputs=res.get_layer("avg_pool").output)
        elif model == "DenseNet121":
            from tensorflow.keras.applications.densenet import DenseNet121, preprocess_input

            image_size = (224, 224)
            dense121 = DenseNet121(input_shape=image_size + (3, ),
                                   weights="imagenet",
                                   include_top=True)
            feature_extractor = tensorflow.keras.models.Model(
                inputs=dense121.input,
                outputs=dense121.get_layer("avg_pool").output)
        elif model == "DenseNet201":
            from tensorflow.keras.applications.densenet import DenseNet201, preprocess_input

            image_size = (224, 224)
            dense201 = DenseNet201(input_shape=image_size + (3, ),
                                   weights="imagenet",
                                   include_top=True)
            feature_extractor = tensorflow.keras.models.Model(
                inputs=dense201.input,
                outputs=dense201.get_layer("avg_pool").output)
        elif model == "VGG16":
            from tensorflow.keras.applications.vgg16 import VGG16, preprocess_input

            image_size = (224, 224)
            vgg16 = VGG16(input_shape=image_size + (3, ),
                          weights="imagenet",
                          include_top=True)
            feature_extractor = tensorflow.keras.models.Model(
                inputs=vgg16.input, outputs=vgg16.get_layer("fc2").output)
        elif model == "VGG19":
            from tensorflow.keras.applications.vgg19 import VGG19, preprocess_input

            image_size = (224, 224)
            vgg19 = VGG19(input_shape=image_size + (3, ),
                          weights="imagenet",
                          include_top=True)
            feature_extractor = tensorflow.keras.models.Model(
                inputs=vgg19.input, outputs=vgg19.get_layer("fc2").output)
        elif model == "MobileNet":
            from tensorflow.keras.applications.mobilenet import MobileNet, preprocess_input

            image_size = (224, 224)
            mobile = MobileNet(input_shape=image_size + (3, ),
                               weights="imagenet",
                               include_top=True)
            feature_extractor = tensorflow.keras.models.Model(
                inputs=mobile.input, outputs=mobile.get_layer(index=-6).output)
            # the output layer in the above is 'global_average_pooling2d', but we use the equivalent 'index' due to a bug in tensorflow
        elif model == "MobileNetV2":
            from tensorflow.keras.applications.mobilenet_v2 import (
                MobileNetV2,
                preprocess_input,
            )

            image_size = (224, 224)
            mobilev2 = MobileNetV2(input_shape=image_size + (3, ),
                                   weights="imagenet",
                                   include_top=True)
            feature_extractor = tensorflow.keras.models.Model(
                inputs=mobilev2.input,
                outputs=mobilev2.get_layer(index=-2).output)
            # output layer in the above is 'global_average_pooling2d', but we use the equivalent 'index' due to a bug in tensorflow
        elif model == "Xception":
            from tensorflow.keras.applications.xception import Xception, preprocess_input

            image_size = (299, 299)
            xception = Xception(input_shape=image_size + (3, ),
                                weights="imagenet",
                                include_top=True)
            feature_extractor = tensorflow.keras.models.Model(
                inputs=xception.input,
                outputs=xception.get_layer("avg_pool").output)
        elif model == "InceptionV3":
            from tensorflow.keras.applications.inception_v3 import (
                InceptionV3,
                preprocess_input,
            )

            image_size = (299, 299)
            inception = InceptionV3(input_shape=image_size + (3, ),
                                    weights="imagenet",
                                    include_top=True)
            feature_extractor = tensorflow.keras.models.Model(
                inputs=inception.input,
                outputs=inception.get_layer("avg_pool").output)
        else:
            raise NotImplementedError("please specify a model!")

        return feature_extractor, image_size, preprocess_input