Esempio n. 1
0
    def create_model(self):
        super().create_model()

        encoder_model = densenet.DenseNet169(include_top=False,
                                             weights='imagenet',
                                             pooling=None)
        input_image = encoder_model.input
        # input_image = layers.Input(batch_shape=(self.BATCH_SIZE, *self.MODEL_INPUT_IMAGE_SHAPE), name='input_1')
        # encoder_model = densenet.DenseNet169(include_top=False, weights='imagenet',
        #                                      input_tensor=input_image,
        #                                      # input_shape=self.MODEL_INPUT_IMAGE_SHAPE,
        #                                      pooling=None)

        input_male = layers.Input(shape=(1, ), name='input_male')
        # input_male = layers.Input(batch_shape=(self.BATCH_SIZE, 1), name='input_male')

        x_image = encoder_model.output
        x_image = layers.GlobalAveragePooling2D(
            name='encoder_pooling')(x_image)

        # x_image = layers.Reshape(target_shape=(-1, 1,))(x_image)
        # x_image = layers.Flatten()(x_image)
        x_image = layers.Reshape(target_shape=(-1, ))(x_image)

        x_male = layers.Dense(32, activation='relu')(input_male)

        x = layers.concatenate([x_image, x_male], axis=-1)
        x = layers.Dense(1000, activation='relu')(x)
        x = layers.Dense(1000, activation='relu')(x)
        output_age = layers.Dense(1, activation='linear', name='output_age')(x)

        self.model = keras.models.Model(inputs=[input_image, input_male],
                                        outputs=output_age)
        self.model.summary(line_length=150)
Esempio n. 2
0
    def build_base_model(self, inputs, blocks=None):
        inputs = keras.layers.Lambda(
            lambda x: keras_densenet.preprocess_input(x))(inputs)

        if self.backbone_name == 'densenet121':
            densenet = keras_densenet.DenseNet121(include_top=False,
                                                  weights='imagenet',
                                                  input_tensor=inputs)
        elif self.backbone_name == 'densenet169':
            densenet = keras_densenet.DenseNet169(include_top=False,
                                                  input_tensor=inputs,
                                                  weights='imagenet')
        elif self.backbone_name == 'densenet201':
            densenet = keras_densenet.DenseNet201(include_top=False,
                                                  input_tensor=inputs,
                                                  weights='imagenet')
        elif self.backbone_name == 'densenet':
            if blocks is None:
                raise ValueError(
                    'blocks must be specified to use custom densenet backbone')

            densenet = keras_densenet.DenseNet(blocks=blocks,
                                               include_top=False,
                                               input_tensor=inputs,
                                               weights='imagenet')
        else:
            raise ValueError("Backbone '{}' not recognized.".format(
                self.backbone_name))

        return densenet
	def dense_net169(self, percent2retrain):
		'Returns a Densenet169 architecture NN'
		dense_model = densenet.DenseNet169(input_shape=self.input_dim,
		                                   weights='imagenet',
		                                   include_top=False)
		# freeze base layers
		if percent2retrain < 1:
			for layer in dense_model.layers[:-int(len(dense_model.layers)*percent2retrain)]: layer.trainable = False

		# add classification top layer
		model = Sequential()
		model.add(dense_model)
		model.add(Flatten())
		model.add(Dense(512, activation='relu'))
		model.add(Dropout(0.5))
		model.add(Dense(self.n_classes, activation='sigmoid'))
		return model
Esempio n. 4
0
def DenseNet3D(input_shape, growth_rate=32, block_config=(6, 12, 24, 16),
               num_init_features=64, bn_size=4, drop_rate=0, num_classes=5):
    r"""Densenet-BC model class, based on
    `"Densely Connected Convolutional Networks" <https://arxiv.org/pdf/1608.06993.pdf>`
    Args:
        growth_rate (int) - how many filters to add each layer (`k` in paper)
        block_config (list of 4 ints) - how many layers in each pooling block
        num_init_features (int) - the number of filters to learn in the first convolution layer
        bn_size (int) - multiplicative factor for number of bottle neck layers
          (i.e. bn_size * k features in the bottleneck layer)
        drop_rate (float) - dropout rate after each dense layer
        num_classes (int) - number of classification classes
    """
    #-----------------------------------------------------------------
    inp_2d = (Input(shape=(224,224,3), name='2d_input'))
    batch_densenet = densenet.DenseNet169(include_top=False, input_shape=(224,224,3), input_tensor=inp_2d, weights='imagenet')
    
    for layer in batch_densenet.layers:
        layer.trainable = False

    # Configure the 2D CNN to take batches of pictures
    inp_2d_batch = (Input(shape=input_shape, name='2d_input_batch'))
    batch_densenet = TimeDistributed(batch_densenet)(inp_2d_batch)
    batch_densenet = Model(inputs=inp_2d_batch, outputs=batch_densenet)
    #-----------------------------------------------------------------

    # inp_3d = (Input(shape=input_shape, name='3d_input'))
    t3d = T3D169(include_top=False, input_shape=input_shape)

    #--------------from 2d densenet model-----------------
    x = GlobalAveragePooling3D(name='avg_pool_t3d')(t3d.output)
    y = GlobalAveragePooling3D(name='avg_pool_densnet3d')(batch_densenet.output)

    #-----------------------------------------------------
    x = keras.layers.concatenate([x,y])
    x = Dropout(0.65)(x)
    x = Dense(512, activation='relu')(x)
    x = Dropout(0.5)(x)
    x = Dense(128, activation='relu')(x)
    x = Dropout(0.35)(x)
    out = Dense(num_classes, activation='softmax')(x)

    model = Model(inputs=[inp_2d_batch, t3d.input], outputs=[out])
    # model.summary()

    return model
def get_model():

    num_classes = 10
    input_shape = (MODELS[MODEL]['size'], MODELS[MODEL]['size'], 3)
    #preprocess = imagenet_utils.preprocess_input

    input_image = Input(shape=input_shape)

    if MODEL == "densenet121":
        base_model = densenet.DenseNet121(include_top=False,
                                          pooling=None,
                                          weights='imagenet',
                                          input_shape=input_shape)
    elif MODEL == "densenet169":
        base_model = densenet.DenseNet169(include_top=False,
                                          pooling=None,
                                          weights='imagenet',
                                          input_shape=input_shape)
    elif MODEL == "densenet201":
        base_model = densenet.DenseNet201(include_top=False,
                                          pooling=None,
                                          weights='imagenet',
                                          input_shape=input_shape)
    elif MODEL == "inceptionresnet":
        base_model = inception_resnet_v2.InceptionResNetV2(
            include_top=False,
            pooling=None,
            weights='imagenet',
            input_shape=input_shape)
    elif MODEL == "inception":
        base_model = inception_v3.InceptionV3(include_top=False,
                                              pooling=None,
                                              weights='imagenet',
                                              input_shape=input_shape)
    elif MODEL == "mobilenet":
        base_model = mobilenet.MobileNet(include_top=False,
                                         pooling=None,
                                         weights='imagenet',
                                         input_shape=input_shape)
    elif MODEL == "resnet":
        base_model = resnet50.ResNet50(include_top=False,
                                       pooling=None,
                                       weights='imagenet',
                                       input_shape=input_shape)
    elif MODEL == "vgg16":
        base_model = vgg16.VGG16(include_top=False,
                                 pooling=None,
                                 weights='imagenet',
                                 input_shape=input_shape)
    elif MODEL == "vgg19":
        base_model = vgg19.VGG19(include_top=False,
                                 pooling=None,
                                 weights='imagenet',
                                 input_shape=input_shape)
    else:
        print("Bad model type:", MODEL)
        sys.exit(-1)

    x = input_image
    x = base_model(x)
    x = Reshape((-1, ))(x)
    #x = Dropout(rate=?)(x)
    x = Dense(512, activation='relu', name='fc1')(x)
    x = Dropout(0.3, name='dropout_fc1')(x)
    x = Dense(128, activation='relu', name='fc2')(x)
    x = Dropout(0.3, name='dropout_fc2')(x)
    prediction = Dense(nclass, activation="softmax", name="predictions")(x)

    # this is the model we will train
    my_model = Model(inputs=(input_image), outputs=prediction)

    # compile the model (should be done *after* setting layers to non-trainable)
    opt = optimizers.Adam(lr=1e-4)
    my_model.compile(optimizer=opt,
                     loss='categorical_crossentropy',
                     metrics=['acc'])

    my_model.summary()
    return my_model
Esempio n. 6
0
def DenseNet3D(input_shape, growth_rate=32, block_config=(6, 12, 24, 16),
               num_init_features=64, bn_size=4, drop_rate=0, num_classes=5):
    r"""Densenet-BC model class, based on
    `"Densely Connected Convolutional Networks" <https://arxiv.org/pdf/1608.06993.pdf>`
    Args:
        growth_rate (int) - how many filters to add each layer (`k` in paper)
        block_config (list of 4 ints) - how many layers in each pooling block
        num_init_features (int) - the number of filters to learn in the first convolution layer
        bn_size (int) - multiplicative factor for number of bottle neck layers
          (i.e. bn_size * k features in the bottleneck layer)
        drop_rate (float) - dropout rate after each dense layer
        num_classes (int) - number of classification classes
    """
    #-----------------------------------------------------------------
    inp_2d = (Input(shape=(224,224,3), name='2d_input'))
    pretrained_densenet = densenet.DenseNet169(include_top=False, input_shape=(224,224,3), input_tensor=inp_2d, weights='imagenet')
    for layer in pretrained_densenet.layers:
        layer.trainable = False
    #-----------------------------------------------------------------

    # First convolution-----------------------
    inp_3d = (Input(shape=input_shape, name='3d_input'))


    # need to check padding
    x = (Conv3D(num_init_features, kernel_size=(3, 7, 7),
                strides=2, padding='same', use_bias=False))(inp_3d)

    x = BatchNormalization()(x)
    x = Activation('relu')(x)

    # need to check padding
    x = MaxPooling3D(pool_size=(3, 3, 3), strides=(
        2, 2, 2), padding='valid')(x)

    # Each denseblock
    num_features = num_init_features
    for i, num_layers in enumerate(block_config):
        # print('Pass', i)
        x = _DenseBlock(x, num_layers=num_layers,
                        bn_size=bn_size, growth_rate=growth_rate, drop_rate=drop_rate)

        num_features = num_features + num_layers * growth_rate

        if i != len(block_config) - 1:
            # print('Not Last layer, so adding Temporal Transition Layer')

            x = _TTL(x)
            # num_features = 128*3

            x = _Transition(x, num_output_features=num_features)
            num_features = num_features

    # Final batch norm
    x = BatchNormalization()(x)

    x = Activation('relu')(x)
    x = AveragePooling3D(pool_size=(1, 7, 7))(x)
    x = Flatten(name='flatten_3d')(x)
    x = Dense(1024, activation='relu')(x)
    #--------------fron 2d densenet model-----------------
    y = GlobalAveragePooling2D(name='avg_pool_densnet2d')(pretrained_densenet.output)
    y = Dense(1024, activation='relu')(y)

    #-----------------------------------------------------
    x = keras.layers.concatenate([x,y])
    x = Dropout(0.65)(x)
    x = Dense(512, activation='relu')(x)
    x = Dropout(0.5)(x)
    x = Dense(128, activation='relu')(x)
    x = Dropout(0.35)(x)
    out = Dense(num_classes, activation='softmax')(x)

    model = Model(inputs=[inp_2d, inp_3d], outputs=[out])
    model.summary()

    return model
            )  # decode the results into a list of tuples (class, description, probability)
            print(decode_prediction
                  )  # change top to x to get x highest predictions
            df.at[i, prediction_likelihood[2 * x]] = decode_prediction[0][0][
                1]  # save description
            df.at[i,
                  prediction_likelihood[2 * x + 1]] = decode_prediction[0][0][
                      2]  # save probability
            print(trueLabels[x])


# initialize models

resnet_model = resnet50.ResNet50(weights='imagenet')
vgg_model = vgg16.VGG16(weights='imagenet')
densenet_model = densenet.DenseNet169(weights='imagenet')
mobile_model = mobilenet.MobileNet(weights='imagenet')

modelEnsemble = [resnet_model, vgg_model, densenet_model]
# list of used models: resnet_model, vgg_model, mobile_model,  densenet_model
modelEnsemleArt = [resnet50, vgg16, densenet]
# list of model nets: resnet50, vgg16, mobilenet,   densenet
model_names = ['resnet50', 'vgg16', 'densenet']
# list of model Names: 'resnet50', 'vgg16', 'mobilenet','densenet'

data = []  # images to classify
trueLabels = []  # labels of images
extend = 'likelihood'  # name for dataframe columns to store likelihood
prediction_likelihood = [
]  # store prediction and likelihood [house, 0.10% , room 0.2% ...]
Esempio n. 8
0
def get_models(model_name, NUM_CLASSES):

    #region Normal Inceptionv3,Xception,InceptionResnetV2
    if model_name == 'InceptionV3':
        model = InceptionV3(include_top=True,
                            input_shape=(299, 299, 3),
                            weights=None,
                            classes=NUM_CLASSES)
        IMAGE_SIZE = 299
        BATCH_SIZE_TRAIN = 32
        # BATCH_SIZE_TRAIN = 64  # 增加GPU之后
    elif model_name == 'InceptionV4':
        from LIBS.Neural_Networks.classification import inception_v4
        model = inception_v4.create_inception_v4(nb_classes=NUM_CLASSES,
                                                 load_weights=False)
        IMAGE_SIZE = 299
        BATCH_SIZE_TRAIN = 16
    elif model_name == 'Xception':
        model = Xception(include_top=True,
                         input_shape=(299, 299, 3),
                         weights=None,
                         classes=NUM_CLASSES)
        IMAGE_SIZE = 299
        BATCH_SIZE_TRAIN = 28  # 增加GPU之后
    elif model_name == 'InceptionResNetV2':
        # following keras implemantation don't use dropout
        # 标准的不用dropout,收敛快了许多, train准确率提高
        model = InceptionResNetV2(include_top=True,
                                  input_shape=(299, 299, 3),
                                  weights=None,
                                  classes=NUM_CLASSES)
        IMAGE_SIZE = 299
        BATCH_SIZE_TRAIN = 32  # 增加GPU之后
    elif model_name == 'MobileNetV2':  #Total params: 2,261,827
        # model = MobileNetV2(include_top=True, input_shape=(256, 256, 3), weights=None, classes=NUM_CLASSES)
        model = MobileNetV2(include_top=True,
                            input_shape=(224, 224, 3),
                            weights=None,
                            classes=NUM_CLASSES)
        IMAGE_SIZE = 224
        BATCH_SIZE_TRAIN = 64
    elif model_name == 'My_Xception':
        model = my_xception.Xception(classes=NUM_CLASSES)

        IMAGE_SIZE = 299
        BATCH_SIZE_TRAIN = 32  # 增加GPU之后
    elif model_name == 'DRN_A18':  #params:20,631,682
        from LIBS.Neural_Networks.classification import my_DRN_A
        model = my_DRN_A.DRN_A_Builder.build_DRN_A_18(input_shape=(256, 256,
                                                                   3),
                                                      num_classes=NUM_CLASSES)

        IMAGE_SIZE = 256
        BATCH_SIZE_TRAIN = 32
    elif model_name == 'DRN_A34':
        from LIBS.Neural_Networks.classification import my_DRN_A_1
        # model = my_DRN_A.DRN_A_Builder.build_DRN_A_34(input_shape=(256, 256, 3), num_classes=NUM_CLASSES)
        model = my_DRN_A_1.DRN_A_Builder.build_DRN_A_34(
            input_shape=(256, 256, 3), num_classes=NUM_CLASSES)

        IMAGE_SIZE = 256
        BATCH_SIZE_TRAIN = 32
    elif model_name == 'DRN_A50':
        from LIBS.Neural_Networks.classification import my_DRN_A
        model = my_DRN_A.DRN_A_Builder.build_DRN_A_50(input_shape=(256, 256,
                                                                   3),
                                                      num_classes=NUM_CLASSES)

        IMAGE_SIZE = 256
        BATCH_SIZE_TRAIN = 32
    elif model_name == 'DRN_A101':
        from LIBS.Neural_Networks.classification import my_DRN_A
        model = my_DRN_A.DRN_A_Builder.build_DRN_A_101(input_shape=(256, 256,
                                                                    3),
                                                       num_classes=NUM_CLASSES)

        IMAGE_SIZE = 256
        BATCH_SIZE_TRAIN = 32
    elif model_name == 'DRN_C26':  #params:20,631,682
        from LIBS.Neural_Networks.classification import my_DRN_C
        model = my_DRN_C.DRN_C_Builder.build_DRN_C_26(input_shape=(224, 224,
                                                                   3),
                                                      num_classes=NUM_CLASSES)

        IMAGE_SIZE = 224
        BATCH_SIZE_TRAIN = 32
    elif model_name == 'DRN_C42':  # params:30,750,978
        from LIBS.Neural_Networks.classification import my_DRN_C
        model = my_DRN_C.DRN_C_Builder.build_DRN_C_42(input_shape=(224, 224,
                                                                   3),
                                                      num_classes=NUM_CLASSES)

        IMAGE_SIZE = 224
        BATCH_SIZE_TRAIN = 32
    elif model_name == 'DPN92':  #DPN92,DPN98,DPN107,DPN137
        from LIBS.Neural_Networks.classification import my_dpn
        model = my_dpn.DPN92(input_shape=(224, 224, 3),
                             weights=None,
                             classes=NUM_CLASSES)
        IMAGE_SIZE = 224
        BATCH_SIZE_TRAIN = 16  # GTX1080 ti 32 exhausted
    elif model_name == 'DPN98':
        from LIBS.Neural_Networks.classification import my_dpn
        model = my_dpn.DPN92(input_shape=(224, 224, 3),
                             weights=None,
                             classes=NUM_CLASSES)
        IMAGE_SIZE = 224
        BATCH_SIZE_TRAIN = 16  # GTX1080 ti 32 exhausted
    elif model_name == 'DPN107':
        from LIBS.Neural_Networks.classification import my_dpn
        model = my_dpn.DPN107(input_shape=(224, 224, 3),
                              weights=None,
                              classes=NUM_CLASSES)
        IMAGE_SIZE = 224
        BATCH_SIZE_TRAIN = 16  # GTX1080 ti 32 exhausted
    elif model_name == 'DPN137':
        from LIBS.Neural_Networks.classification import my_dpn
        model = my_dpn.DPN137(input_shape=(224, 224, 3),
                              weights=None,
                              classes=NUM_CLASSES)
        IMAGE_SIZE = 224
        BATCH_SIZE_TRAIN = 16  # GTX1080 ti 32 exhausted
    elif model_name == 'DenseNet121':  #121,169,201
        model = densenet.DenseNet121(input_shape=(224, 224, 3),
                                     weights=None,
                                     classes=NUM_CLASSES)
        IMAGE_SIZE = 224
        BATCH_SIZE_TRAIN = 32
    elif model_name == 'DenseNet169':
        model = densenet.DenseNet169(input_shape=(224, 224, 3),
                                     weights=None,
                                     classes=NUM_CLASSES)
        IMAGE_SIZE = 224
        BATCH_SIZE_TRAIN = 32
    elif model_name == 'DenseNet201':
        model = densenet.DenseNet201(input_shape=(224, 224, 3),
                                     weights=None,
                                     classes=NUM_CLASSES)
        IMAGE_SIZE = 224
        BATCH_SIZE_TRAIN = 16
    elif model_name == 'NasnetMobile':
        model = nasnet.NASNetMobile(input_shape=(224, 224, 3),
                                    weights=None,
                                    classes=NUM_CLASSES)
        IMAGE_SIZE = 224
        BATCH_SIZE_TRAIN = 32
    elif model_name == 'my_Mnasnet':
        from LIBS.Neural_Networks.classification import my_Mnasnet
        model = my_Mnasnet.MnasNet(input_shape=(224, 224, 3),
                                   classes=NUM_CLASSES)
        IMAGE_SIZE = 224
        BATCH_SIZE_TRAIN = 64
    elif model_name == "Mnasnet":
        from LIBS.Neural_Networks.classification import my_Mnasnet
        model = my_Mnasnet.MnasNet(classes=NUM_CLASSES,
                                   input_shape=(224, 224, 3))
        IMAGE_SIZE = 224
        BATCH_SIZE_TRAIN = 64
    elif model_name == 'NasnetMedium':  #Trainable params: 22,695,624
        model = nasnet.NASNet(
            input_shape=(299, 299, 3),
            penultimate_filters=1920,
            num_blocks=7,
            stem_block_filters=80,  # Large:96, Mobile:32
            classes=NUM_CLASSES,
            default_size=299)
        IMAGE_SIZE = 299
        BATCH_SIZE_TRAIN = 16
    elif model_name == 'NasnetLarge':
        model = nasnet.NASNetLarge(input_shape=(331, 331, 3),
                                   weights=None,
                                   classes=NUM_CLASSES)
        IMAGE_SIZE = 331
        BATCH_SIZE_TRAIN = 8
    #endregion

    #region All kinds of  ResNet, ResNeXt
    # 7*32=224, 256, 288, 320, 352, 384
    elif model_name == 'ResNet50':
        from LIBS.Neural_Networks.classification import my_resnet
        model = my_resnet.ResnetBuilder.build_resnet_50((256, 256, 3),
                                                        NUM_CLASSES)
        IMAGE_SIZE = 256
        BATCH_SIZE_TRAIN = 32
    elif model_name == 'ResNet50_288':
        from LIBS.Neural_Networks.classification import my_resnet
        model = my_resnet.ResnetBuilder.build_resnet_50((288, 288, 3),
                                                        NUM_CLASSES)
        IMAGE_SIZE = 288
        BATCH_SIZE_TRAIN = 32
    elif model_name == 'ResNet101':
        from LIBS.Neural_Networks.classification import my_resnet
        model = my_resnet.ResnetBuilder.build_resnet_101((256, 256, 3),
                                                         NUM_CLASSES)
        IMAGE_SIZE = 256
        BATCH_SIZE_TRAIN = 32
    elif model_name == 'ResNet448':
        from LIBS.Neural_Networks.classification import my_resnet
        # model = my_resnet.ResnetBuilder.build_resnet_mymodel_34_64_5((448, 448, 3), NUM_CLASSES)
        model = my_resnet.ResnetBuilder.build_resnet_448_1((448, 448, 3),
                                                           NUM_CLASSES)
        IMAGE_SIZE = 448
        BATCH_SIZE_TRAIN = 32  # 增加GPU之后
    elif model_name == 'ResNext448':
        from LIBS.Neural_Networks.classification import resNeXt
        model = resNeXt.my_ResNext(input_shape=(448, 448, 3),
                                   classes=NUM_CLASSES)
        IMAGE_SIZE = 448
        BATCH_SIZE_TRAIN = 16  # 增加GPU之后

    #endregion

    #region multi label (Inceptionv3, Xception, InceptionResnetV2)
    elif model_name == 'Multi_label_InceptionV3':
        base_model = InceptionV3(include_top=False, weights=None)
        model = add_multilabels_top(base_model, NUM_CLASSES)

        IMAGE_SIZE = 299
        BATCH_SIZE_TRAIN = 32
    elif model_name == 'Multi_label_SE_InceptionV3':
        from LIBS.Neural_Networks.classification import my_se_inception_v3
        base_model = my_se_inception_v3.SE_InceptionV3((299, 299, 3),
                                                       include_top=False)
        model = add_multilabels_top(base_model, NUM_CLASSES)

        IMAGE_SIZE = 299
        BATCH_SIZE_TRAIN = 32
    elif model_name == 'Multi_label_Xception':
        base_model = Xception(include_top=False, weights=None)
        model = add_multilabels_top(base_model, NUM_CLASSES)

        IMAGE_SIZE = 299
        BATCH_SIZE_TRAIN = 32  # 增加GPU之后
    elif model_name == 'Multi_label_my_Xception':
        from LIBS.Neural_Networks.classification import my_xception
        model = my_xception.Xception(classes=NUM_CLASSES, multi_labels=True)

        IMAGE_SIZE = 299
        BATCH_SIZE_TRAIN = 32  # 增加GPU之后

    elif model_name == 'Multi_label_InceptionResNetV2':
        base_model = InceptionResNetV2(include_top=False, weights=None)
        model = add_multilabels_top(base_model, NUM_CLASSES)

        IMAGE_SIZE = 299
        BATCH_SIZE_TRAIN = 32

    elif model_name == 'Multi_label_DRN_A_Xception':
        from LIBS.Neural_Networks import DRN_A_Xception_Builder
        base_model = DRN_A_Xception_Builder.build_DRN_A_xception(
            input_shape=(288, 288, 3), num_classes=29, include_top=False)

        model = add_multilabels_top(base_model, NUM_CLASSES)
        IMAGE_SIZE = 288
        BATCH_SIZE_TRAIN = 32

    elif model_name == 'Multi_NasnetMedium':
        base_model = nasnet.NASNet(
            input_shape=(299, 299, 3),
            penultimate_filters=1920,
            num_blocks=7,
            stem_block_filters=80,  # Large:96, Mobile:32
            classes=NUM_CLASSES,
            default_size=299,
            include_top=False)
        model = add_multilabels_top(base_model, NUM_CLASSES)

        IMAGE_SIZE = 299
        BATCH_SIZE_TRAIN = 16
    elif model_name == 'Multi_DRN_A18':
        from LIBS.Neural_Networks.classification import my_DRN_A
        #original number of parameters, resnet34 :21.8M, resnet50:25.6M
        base_model = my_DRN_A.DRN_A_Builder.build_DRN_A_18(
            input_shape=(288, 288, 3),
            num_classes=NUM_CLASSES,
            include_top=False)

        model = add_multilabels_top(base_model, NUM_CLASSES)
        IMAGE_SIZE = 288
        BATCH_SIZE_TRAIN = 32
    elif model_name == 'Multi_DRN_A34':
        from LIBS.Neural_Networks.classification import my_DRN_A
        base_model = my_DRN_A.DRN_A_Builder.build_DRN_A_34(
            input_shape=(288, 288, 3),
            num_classes=NUM_CLASSES,
            include_top=False)

        model = add_multilabels_top(base_model, NUM_CLASSES)
        IMAGE_SIZE = 288
        BATCH_SIZE_TRAIN = 32
    elif model_name == 'Multi_DRN_A50':
        from LIBS.Neural_Networks.classification import my_DRN_A
        base_model = my_DRN_A.DRN_A_Builder.build_DRN_A_50(
            input_shape=(288, 288, 3),
            num_classes=NUM_CLASSES,
            include_top=False)

        model = add_multilabels_top(base_model, NUM_CLASSES)
        IMAGE_SIZE = 288
        BATCH_SIZE_TRAIN = 32
    elif model_name == 'Multi_DRN_C26':  #params:20,631,682
        from LIBS.Neural_Networks.classification import my_DRN_C
        base_model = my_DRN_C.DRN_C_Builder.build_DRN_C_26(
            input_shape=(288, 288, 3),
            num_classes=NUM_CLASSES,
            include_top=False)

        model = add_multilabels_top(base_model, NUM_CLASSES)
        IMAGE_SIZE = 288
        BATCH_SIZE_TRAIN = 32
    elif model_name == 'Multi_DRN_C42':  # params:30,750,978
        from LIBS.Neural_Networks.classification import my_DRN_C
        base_model = my_DRN_C.DRN_C_Builder.build_DRN_C_42(
            input_shape=(288, 288, 3),
            num_classes=NUM_CLASSES,
            include_top=False)

        model = add_multilabels_top(base_model, NUM_CLASSES)
        IMAGE_SIZE = 288
        BATCH_SIZE_TRAIN = 32

    elif model_name == 'Multi_DRN_C58':
        from LIBS.Neural_Networks.classification import my_DRN_C
        base_model = my_DRN_C.DRN_C_Builder.build_DRN_C_58(
            input_shape=(288, 288, 3),
            num_classes=NUM_CLASSES,
            include_top=False)

        model = add_multilabels_top(base_model, NUM_CLASSES)
        IMAGE_SIZE = 288
        BATCH_SIZE_TRAIN = 32
    # endregion

    #region SE Net(Se_InceptionV3, Se_InceptionResNetV2等)
    elif model_name == 'Se_InceptionV3':
        from LIBS.Neural_Networks.classification import my_se_inception_v3
        model = my_se_inception_v3.SE_InceptionV3((299, 299, 3),
                                                  classes=NUM_CLASSES)
        IMAGE_SIZE = 299
        BATCH_SIZE_TRAIN = 48

    elif model_name == 'Se_InceptionResNetV2':
        from LIBS.Neural_Networks.classification import my_se_inception_resnet_v2
        model = my_se_inception_resnet_v2.SE_InceptionResNetV2(
            (299, 299, 3), classes=NUM_CLASSES)
        IMAGE_SIZE = 299
        BATCH_SIZE_TRAIN = 32
    elif model_name == 'Se_Resnext':
        from LIBS.Neural_Networks import my_se_resnext
        model = my_se_resnext.SEResNextImageNet((299, 299, 3),
                                                classes=NUM_CLASSES)
        IMAGE_SIZE = 299
        BATCH_SIZE_TRAIN = 24
    elif model_name == 'Se_Resnet50':
        from LIBS.Neural_Networks import my_se_resnet
        model = my_se_resnet.SEResNet50((299, 299, 3), classes=NUM_CLASSES)
        IMAGE_SIZE = 299
        BATCH_SIZE_TRAIN = 32
    #endregion
    '''other models
    #region multi label SE_NET
    elif model_name == 'Multi_label_Se_InceptionV3':
        from Neural_Networks import my_se_inception_v3
        base_model = my_se_inception_v3.SE_InceptionV3((299, 299, 3), include_top=False,
                                                       classes=29, weights=None)

        x = base_model.output
        from keras.layers import GlobalAveragePooling2D, Dense
        from keras.models import Model
        x = GlobalAveragePooling2D()(x)
        predictions = Dense(NUM_CLASSES, activation='sigmoid')(x)

        model = Model(inputs=base_model.input, outputs=predictions)
        IMAGE_SIZE = 299
        BATCH_SIZE_TRAIN = 48
    elif model_name == 'Multi_label_Se_InceptionResNetV2':
        from Neural_Networks import my_se_inception_resnet_v2
        base_model = my_se_inception_resnet_v2.SE_InceptionResNetV2((299, 299, 3), include_top=False,
                                                        classes=29, weights=None)

        x = base_model.output
        from keras.layers import GlobalAveragePooling2D, Dense
        from keras.models import Model
        x = GlobalAveragePooling2D()(x)
        predictions = Dense(NUM_CLASSES, activation='sigmoid')(x)

        model = Model(inputs=base_model.input, outputs=predictions)
        IMAGE_SIZE = 299
        BATCH_SIZE_TRAIN = 24
        BATCH_SIZE_TRAIN = 32  # 增加GPU之后

    #endregion

    '''

    return model, IMAGE_SIZE, BATCH_SIZE_TRAIN
Esempio n. 9
0
 def __init__(self, model_name, transfer=False, input_shape=None):
     '''
     Load pre-trained model.
     
     model_name: one of available models.
     transfer: whether to transfer learning. if True, exclude the fully-connected layer at
         the top of the network. default False
     input_shape: : Transfer learning input shape. If None, default input shape. Default None. https://keras.io/applications/
     '''
     self.name = model_name
     # vgg16
     if model_name == 'vgg16':
         import keras.applications.vgg16 as vgg16
         self.lib = vgg16
         if transfer:
             if input_shape == None:
                 self.input_size = (224, 224)
                 input_shape = (self.input_size[0], self.input_size[1], 3)
             else:
                 self.input_size = (input_shape[0], input_shape[1])
             self.model = vgg16.VGG16(include_top=(not transfer), input_shape=input_shape)
         else:
             self.input_size = (224, 224)
             self.model = vgg16.VGG16(include_top=(not transfer))
     # vgg 19
     elif model_name == 'vgg19':
         import keras.applications.vgg19 as vgg19
         self.lib = vgg19
         if transfer:
             if input_shape == None:
                 self.input_size = (224, 224)
                 input_shape = (self.input_size[0], self.input_size[1], 3)
             else:
                 self.input_size = (input_shape[0], input_shape[1])
             self.model = vgg19.VGG19(include_top=(not transfer), input_shape=input_shape)
         else:
             self.input_size = (224, 224)
             self.model = vgg19.VGG19(include_top=(not transfer))
     # resnet50
     elif model_name == 'resnet50':
         import keras.applications.resnet50 as resnet50
         self.lib = resnet50
         if transfer:
             if input_shape == None:
                 self.input_size = (224, 224)
                 input_shape = (self.input_size[0], self.input_size[1], 3)
             else:
                 self.input_size = (input_shape[0], input_shape[1])
             self.model = resnet50.ResNet50(include_top=(not transfer), input_shape=input_shape)
         else:
             self.input_size = (224, 224)
             self.model = resnet50.ResNet50(include_top=(not transfer))
     # xception
     elif model_name == 'xception':
         import keras.applications.xception as xception
         self.lib = xception
         if transfer:
             if input_shape == None:
                 self.input_size = (299, 299)
                 input_shape = (self.input_size[0], self.input_size[1], 3)
             else:
                 self.input_size = (input_shape[0], input_shape[1])
             self.model = xception.Xception(include_top=(not transfer), input_shape=input_shape)
         else:
             self.input_size = (299, 299)
             self.model = xception.Xception(include_top=(not transfer))
     # densenet121
     elif model_name == 'densenet121':
         import keras.applications.densenet as densenet
         self.lib = densenet
         if transfer:
             if input_shape == None:
                 self.input_size = (224, 224)
                 input_shape = (self.input_size[0], self.input_size[1], 3)
             else:
                 self.input_size = (input_shape[0], input_shape[1])
             self.model = densenet.DenseNet121(include_top=(not transfer), input_shape=input_shape)
         else:
             self.input_size = (224, 224)
             self.model = densenet.DenseNet121(include_top=(not transfer))
     # densenet169
     elif model_name == 'densenet169':
         import keras.applications.densenet as densenet
         self.lib = densenet
         if transfer:
             if input_shape == None:
                 self.input_size = (224, 224)
                 input_shape = (self.input_size[0], self.input_size[1], 3)
             else:
                 self.input_size = (input_shape[0], input_shape[1])
             self.model = densenet.DenseNet169(include_top=(not transfer), input_shape=input_shape)
         else:
             self.input_size = (224, 224)
             self.model = densenet.DenseNet169(include_top=(not transfer))
     # densenet201
     elif model_name == 'densenet201':
         import keras.applications.densenet as densenet
         self.lib = densenet
         if transfer:
             if input_shape == None:
                 self.input_size = (224, 224)
                 input_shape = (self.input_size[0], self.input_size[1], 3)
             else:
                 self.input_size = (input_shape[0], input_shape[1])
             self.model = densenet.DenseNet201(include_top=(not transfer), input_shape=input_shape)
         else:
             self.input_size = (224, 224)
             self.model = densenet.DenseNet201(include_top=(not transfer))
     # inceptionResnetV2
     elif model_name == 'inception_resnet_v2':
         import keras.applications.inception_resnet_v2 as inception_resnet_v2
         self.lib = inception_resnet_v2
         if transfer:
             if input_shape == None:
                 self.input_size = (299, 299)
                 input_shape = (self.input_size[0], self.input_size[1], 3)
             else:
                 self.input_size = (input_shape[0], input_shape[1])
             self.model = self.lib.InceptionResNetV2(include_top=(not transfer), input_shape=input_shape)
         else:
             self.input_size = (299, 299)
             self.model = self.lib.InceptionResNetV2(include_top=(not transfer))
     # inceptionV3
     elif model_name == 'inception_v3':
         import keras.applications.inception_v3 as inception_v3
         self.lib = inception_v3
         if transfer:
             if input_shape == None:
                 self.input_size = (299, 299)
                 input_shape = (self.input_size[0], self.input_size[1], 3)
             else:
                 self.input_size = (input_shape[0], input_shape[1])
             self.model = self.lib.InceptionV3(include_top=(not transfer), input_shape=input_shape)
         else:
             self.input_size = (299, 299)
             self.model = self.lib.InceptionV3(include_top=(not transfer))
     # nasnet mobile
     elif model_name == 'nasnet_mobile':
         import keras.applications.nasnet as nasnet
         self.lib = nasnet
         if transfer:
             if input_shape == None:
                 self.input_size = (224, 224)
                 input_shape = (self.input_size[0], self.input_size[1], 3)
             else:
                 self.input_size = (input_shape[0], input_shape[1])
             self.model = self.lib.NASNetMobile(include_top=(not transfer), input_shape=input_shape)
         else:
             self.input_size = (224, 224)
             self.model = self.lib.NASNetMobile(include_top=(not transfer))
     # nasnet large
     elif model_name == 'nasnet_large':
         import keras.applications.nasnet as nasnet
         self.lib = nasnet
         if transfer:
             if input_shape == None:
                 self.input_size = (331, 331)
                 input_shape = (self.input_size[0], self.input_size[1], 3)
             else:
                 self.input_size = (input_shape[0], input_shape[1])
             self.model = self.lib.NASNetLarge(include_top=(not transfer), input_shape=input_shape)
         else:
             self.input_size = (331, 331)
             self.model = self.lib.NASNetLarge(include_top=(not transfer))
     # mobilenet
     elif model_name == 'mobilenet':
         import keras.applications.mobilenet as mobilenet
         self.lib = mobilenet
         if transfer:
             if input_shape == None:
                 self.input_size = (224, 224)
                 input_shape = (self.input_size[0], self.input_size[1], 3)
             else:
                 self.input_size = (input_shape[0], input_shape[1])
             self.model = self.lib.MobileNet(include_top=(not transfer), input_shape=input_shape)
         else:
             self.input_size = (224, 224)
             self.model = self.lib.MobileNet(include_top=(not transfer))
     # mobilenet v2
     elif model_name == 'mobilenet_v2':
         import keras.applications.mobilenet_v2 as mobilenet_v2
         self.lib = mobilenet_v2
         if transfer:
             if input_shape == None:
                 self.input_size = (224, 224)
                 input_shape = (self.input_size[0], self.input_size[1], 3)
             else:
                 self.input_size = (input_shape[0], input_shape[1])
             self.model = self.lib.MobileNetV2(include_top=(not transfer), input_shape=input_shape)
         else:
             self.input_size = (224, 224)
             self.model = self.lib.MobileNetV2(include_top=(not transfer))
Esempio n. 10
0
def get_model_and_data(mode, model_name):
    if model_name == 'vgg16':
        model = vgg16.VGG16(weights='imagenet', include_top=True)
        preprocess_func = partial(vgg16.preprocess_input, mode='tf')
        target_size = (224, 224)

    elif model_name == 'vgg19':
        model = vgg19.VGG19(weights='imagenet', include_top=True)
        preprocess_func = partial(vgg19.preprocess_input, mode='tf')
        target_size = (224, 224)

    elif model_name == 'resnet50':
        model = resnet50.ResNet50(weights='imagenet', include_top=True)
        preprocess_func = partial(resnet50.preprocess_input, mode='tf')
        target_size = (224, 224)

    elif model_name == 'inception_v3':
        model = inception_v3.InceptionV3(weights='imagenet', include_top=True)
        preprocess_func = inception_v3.preprocess_input
        target_size = (299, 299)

    elif model_name == 'inception_resnet_v2':
        model = inception_resnet_v2.InceptionResNetV2(weights='imagenet',
                                                      include_top=True)
        preprocess_func = inception_resnet_v2.preprocess_input
        target_size = (299, 299)

    elif model_name == 'xception':
        model = xception.Xception(weights='imagenet', include_top=True)
        preprocess_func = xception.preprocess_input
        target_size = (299, 299)

    elif model_name == 'mobilenet':
        model = mobilenet.MobileNet(weights='imagenet', include_top=True)
        preprocess_func = mobilenet.preprocess_input
        target_size = (224, 224)

    elif model_name.startswith('densenet'):
        model_type = int(model_name[len('densenet'):])

        if model_type == 121:
            model = densenet.DenseNet121(weights='imagenet', include_top=True)
        elif model_type == 169:
            model = densenet.DenseNet169(weights='imagenet', include_top=True)
        elif model_type == 201:
            model = densenet.DenseNet201(weights='imagenet', include_top=True)
        else:
            raise ValueError(
                f'Got incorrect DenseNet model type ({model_type}).')

        preprocess_func = densenet.preprocess_input
        target_size = (224, 224)

    else:
        raise ValueError(f'Got unknown NN model ({model_name}).')

    if mode == 'train':
        input_dir = cfg.TRAIN_IMAGES_DIR

    elif mode == 'test':
        input_dir = cfg.TEST_IMAGES_DIR
    else:
        raise ValueError(f'Got unknown proc mode ({mode}).')

    output_file = cfg.NN_IMAGE_FEATURES[model_name][mode]['memmap']

    return model, input_dir, output_file, preprocess_func, target_size
Esempio n. 11
0
def get_model_and_data(mode, model_name):
    if model_name == "vgg16":
        model = vgg16.VGG16(weights="imagenet", include_top=True)
        preprocess_func = partial(vgg16.preprocess_input, mode="tf")
        target_size = (224, 224)

    elif model_name == "vgg19":
        model = vgg19.VGG19(weights="imagenet", include_top=True)
        preprocess_func = partial(vgg19.preprocess_input, mode="tf")
        target_size = (224, 224)

    elif model_name == "resnet50":
        model = resnet50.ResNet50(weights="imagenet", include_top=True)
        preprocess_func = partial(resnet50.preprocess_input, mode="tf")
        target_size = (224, 224)

    elif model_name == "inception_v3":
        model = inception_v3.InceptionV3(weights="imagenet", include_top=True)
        preprocess_func = inception_v3.preprocess_input
        target_size = (299, 299)

    elif model_name == "inception_resnet_v2":
        model = inception_resnet_v2.InceptionResNetV2(weights="imagenet",
                                                      include_top=True)
        preprocess_func = inception_resnet_v2.preprocess_input
        target_size = (299, 299)

    elif model_name == "xception":
        model = xception.Xception(weights="imagenet", include_top=True)
        preprocess_func = xception.preprocess_input
        target_size = (299, 299)

    elif model_name == "mobilenet":
        model = mobilenet.MobileNet(weights="imagenet", include_top=True)
        preprocess_func = mobilenet.preprocess_input
        target_size = (224, 224)

    elif model_name.startswith("densenet"):
        model_type = int(model_name[len("densenet"):])

        if model_type == 121:
            model = densenet.DenseNet121(weights="imagenet", include_top=True)
        elif model_type == 169:
            model = densenet.DenseNet169(weights="imagenet", include_top=True)
        elif model_type == 201:
            model = densenet.DenseNet201(weights="imagenet", include_top=True)
        else:
            raise ValueError(
                f"Got incorrect DenseNet model type ({model_type}).")

        preprocess_func = densenet.preprocess_input
        target_size = (224, 224)

    else:
        raise ValueError(f"Got unknown NN model ({model_name}).")

    if mode == "train":
        input_dir = cfg.TRAIN_IMAGES_DIR

    elif mode == "test":
        input_dir = cfg.TEST_IMAGES_DIR
    else:
        raise ValueError(f"Got unknown proc mode ({mode}).")

    output_file = cfg.NN_IMAGE_FEATURES[model_name][mode]["memmap"]

    return model, input_dir, output_file, preprocess_func, target_size
Esempio n. 12
0
  else:
    image_test = np.append(image_test, [img_norm], axis=0);

if K.image_data_format() == 'channels_first':
    image_test = image_test.reshape(image_test.shape[0], 3, img_rows, img_cols)
else:
    image_test = image_test.reshape(image_test.shape[0], img_rows, img_cols, 3)

#print(image_test)

image_test = image_test.astype('float32')
image_test /= 255
print('image_test shape:', image_test.shape)
print(image_test.shape[0], 'test samples')

base_model = densenet.DenseNet169(weights='imagenet', input_shape=input_shape, include_top=False)
x = base_model.output
x = GlobalAveragePooling2D()(x)
x = Dropout(0.5)(x)
x = Dense(1024, activation='relu')(x)
x = Dropout(0.5)(x)
predictions = Dense(12, activation='softmax')(x)
model = Model(inputs=base_model.input, outputs=predictions)

model.load_weights("DenseNet169.hdf5")
predict_results = model.predict(image_test);

base_model3 = densenet.DenseNet201(weights='imagenet', input_shape=input_shape, include_top=False)
x3 = base_model3.output
x3 = GlobalAveragePooling2D()(x3)
x3 = Dropout(0.5)(x3)