Exemple #1
0
def build_model(input_shape,
                pre_trained_weights,
                num_classes,
                dropout=False,
                model='resnet'):
    if model == 'densenet121':
        base_model = applications.DenseNet121(weights=pre_trained_weights,
                                              include_top=False,
                                              input_shape=input_shape)
    elif model == 'vgg16':
        base_model = applications.VGG16(weights=pre_trained_weights,
                                        include_top=False,
                                        input_shape=input_shape)
    else:
        base_model = applications.ResNet50(weights=pre_trained_weights,
                                           include_top=False,
                                           input_shape=input_shape)
    if pre_trained_weights == 'imagenet':
        for layer in base_model.layers:
            layer.trainable = False
    x = base_model.output
    x = GlobalAveragePooling2D()(x)
    if dropout:
        x = Dropout(0.3)(x)
    # let's add a fully-connected layer
    x = Dense(1024, activation='relu')(x)
    # and a logistic layer -- let's say we have 200 classes
    predictions = Dense(num_classes, activation='softmax')(x)
    # this is the model we will train
    model = Model(inputs=base_model.input, outputs=predictions)
    return model
def raw_model(lr=0.001, shape=(64, 64, 3)):
    inputs = layers.Input(shape=shape)
    base = applications.DenseNet121(input_tensor=inputs, weights='imagenet', include_top=False)
    for i, layer in enumerate(base.layers):
        print(i, layer.name)

    # for layer in base.layers[:-7]:
    #    layer.trainable = False
    # for layer in base.layers[-7:]:
    #    layer.trainable = True
    output = layers.GlobalMaxPooling2D()(base.output)
    output = layers.Dropout(0.5)(output)
    output = layers.Dense(230, activation='softmax')(output)
    model = Model(inputs=inputs, outputs=output)
    opti = optimizers.Adam(lr=lr, beta_1=0.9, beta_2=0.999, epsilon=1e-08)
    model.compile(optimizer=opti, loss=losses.mean_squared_error, metrics=[metrics.categorical_accuracy])

    for i, layer in enumerate(model.layers):
        print(i, layer.name)

    output = layers.GlobalMaxPooling2D()(base.output)
    output = layers.Dense(1024, activation='sigmoid')(output)
    middle_layer_model = Model(inputs=model.input, outputs=output)

    return model, middle_layer_model
def base_network(network='InceptionV3'):
    if network == 'InceptionV3':
        base_model = applications.InceptionV3(weights='imagenet',
                                              include_top=False)
    elif network == 'VGG16':
        base_model = applications.VGG16(weights='imagenet', include_top=False)
    elif network == 'VGG19':
        base_model = applications.VGG19(weights='imagenet', include_top=False)
    elif network == 'ResNet50':
        base_model = applications.ResNet50(weights='imagenet',
                                           include_top=False)
    elif network == 'InceptionResNetV2':
        base_model = applications.InceptionResNetV2(weights='imagenet',
                                                    include_top=False)
    elif network == 'MobileNet':
        base_model = applications.MobileNet(weights='imagenet',
                                            include_top=False)
    elif network == 'DenseNet121':
        base_model = applications.DenseNet121(weights='imagenet',
                                              include_top=False)
    else:
        print('Wrong Model selected.')
        return None

    return base_model
def save_bottlebeck_features():
    train_datagen = ImageDataGenerator(rescale=1. / 255)

    test_datagen = ImageDataGenerator(rescale=1. / 255)

    # build the DenseNet121 network
    model = applications.DenseNet121(include_top=False, weights='imagenet')

    train_generator = train_datagen.flow_from_directory(
        train_data_dir,
        target_size=(img_width, img_height),
        batch_size=batch_size,
        class_mode=None,
        shuffle=False)
    bottleneck_features_train = model.predict_generator(
        train_generator, aug_factor * nb_classes * nb_train_samples // batch_size)
    train_labels = np.tile(np.repeat(np.arange(nb_classes), nb_train_samples), aug_factor)
    np.savez(bn_train_path, data=bottleneck_features_train, label=train_labels)

    test_generator = test_datagen.flow_from_directory(
        validation_data_dir,
        target_size=(img_width, img_height),
        batch_size=batch_size,
        class_mode=None,
        shuffle=False)
    bottleneck_features_validation = model.predict_generator(
        test_generator, nb_classes * nb_validation_samples // batch_size)
    validation_labels = np.repeat(np.arange(nb_classes), nb_validation_samples)
    np.savez(bn_validation_path, data=bottleneck_features_validation, label=validation_labels)
Exemple #5
0
def get_imagenet_architecture(architecture, variant, size, alpha, output_layer, include_top=False, weights='imagenet'):
    from keras import applications, Model

    if include_top:
        assert output_layer == 'last'

    if size == 'auto':
        size = get_image_size(architecture, variant, size)

    shape = (size, size, 3)

    if architecture == 'densenet':
        if variant == 'auto':
            variant = 'densenet-121'
        if variant == 'densenet-121':
            model = applications.DenseNet121(weights=weights, include_top=include_top, input_shape=shape)
        elif variant == 'densenet-169':
            model = applications.DenseNet169(weights=weights, include_top=include_top, input_shape=shape)
        elif variant == 'densenet-201':
            model = applications.DenseNet201(weights=weights, include_top=include_top, input_shape=shape)
    elif architecture == 'inception-resnet-v2':
        model = applications.InceptionResNetV2(weights=weights, include_top=include_top, input_shape=shape)
    elif architecture == 'mobilenet':
        model = applications.MobileNet(weights=weights, include_top=include_top, input_shape=shape, alpha=alpha)
    elif architecture == 'mobilenet-v2':
        model = applications.MobileNetV2(weights=weights, include_top=include_top, input_shape=shape, alpha=alpha)
    elif architecture == 'nasnet':
        if variant == 'auto':
            variant = 'large'
        if variant == 'large':
            model = applications.NASNetLarge(weights=weights, include_top=include_top, input_shape=shape)
        else:
            model = applications.NASNetMobile(weights=weights, include_top=include_top, input_shape=shape)
    elif architecture == 'resnet-50':
        model = applications.ResNet50(weights=weights, include_top=include_top, input_shape=shape)
    elif architecture == 'vgg-16':
        model = applications.VGG16(weights=weights, include_top=include_top, input_shape=shape)
    elif architecture == 'vgg-19':
        model = applications.VGG19(weights=weights, include_top=include_top, input_shape=shape)
    elif architecture == 'xception':
        model = applications.Xception(weights=weights, include_top=include_top, input_shape=shape)
    elif architecture == 'inception-v3':
        model = applications.InceptionV3(weights=weights, include_top=include_top, input_shape=shape)

    if output_layer != 'last':
        try:
            if isinstance(output_layer, int):
                layer = model.layers[output_layer]
            else:
                layer = model.get_layer(output_layer)
        except Exception:
            raise VergeMLError('layer not found: {}'.format(output_layer))
        model = Model(inputs=model.input, outputs=layer.output)

    return model
def create_desnet121():
    base = applications.DenseNet121(include_top=False,
                                    weights=None,
                                    input_shape=(112, 112, 3),
                                    pooling='max')

    x = base.output
    x = Dropout(0.2)(x)

    output = Dense(1, activation='linear')(x)
    return Model(inputs=base.input, outputs=output)
Exemple #7
0
def densenet121(shape, classes):
    """ DenseNet121 """
    initial_model = app.DenseNet121(include_top=False,
                                    weights='imagenet',
                                    pooling='avg',
                                    input_shape=shape)
    last = initial_model.output
    preds = l.Dense(classes, activation='softmax')(last)
    model = keras.models.Model(initial_model.input, preds)
    model.name = "densenet121"
    return model
Exemple #8
0
def DenseNet121_finetune(input_shape=(256, 256, 3), num_classes=5):
    # Defaultly all layers are trainable
    model_DenseNet = applications.DenseNet121(include_top=False,
                                              weights='imagenet',
                                              input_shape=input_shape)
    for layer in model_DenseNet.layers:  # or for layer in model_VGG.layers[:25]:
        layer.trainable = True  ##True or False

    p = GlobalAveragePooling2D()(model_DenseNet.output)
    o = Dense(num_classes, activation='softmax')(p)
    model = Model(inputs=model_DenseNet.input, outputs=[o])
    return model
Exemple #9
0
def create_DenseNet121(image_size, num_class):
    resnet_conv = applications.DenseNet121(weights='imagenet',
                                           include_top=False,
                                           input_shape=(image_size, image_size,
                                                        3))

    model = models.Sequential()
    model.add(resnet_conv)
    model.add(layers.GlobalAveragePooling2D())
    model.add(layers.Dense(num_class, activation='softmax'))
    return model
    pass
Exemple #10
0
def model_app(arch, input_tensor):
    """Loads the appropriate convolutional neural network (CNN) model
      Args:
        arch: String key for model to be loaded.
        input_tensor: Keras tensor to use as image input for the model.
      Returns:
        model: The specified Keras Model instance with ImageNet weights loaded and without the top classification layer.
      """
    # function that loads the appropriate model
    if arch == 'Xception':
        model = applications.Xception(weights='imagenet', include_top=False, input_tensor=input_tensor)
        print('Xception loaded')
    elif arch == 'VGG16':
        model = applications.VGG16(weights='imagenet', include_top=False, input_tensor=input_tensor)
        print('VGG16 loaded')
    elif arch == 'VGG19':
        model = applications.VGG19(weights='imagenet', include_top=False, input_tensor=input_tensor)
        print('VGG19 loaded')
    elif arch == 'ResNet50':
        model = applications.ResNet50(weights='imagenet', include_top=False, input_tensor=input_tensor)
        print('ResNet50 loaded')
    elif arch == 'InceptionV3':
        model = applications.InceptionV3(weights='imagenet', include_top=False, input_tensor=input_tensor)
        print('InceptionV3 loaded')
    elif arch == 'InceptionResNetV2':
        model = applications.InceptionResNetV2(weights='imagenet', include_top=False, input_tensor=input_tensor)
        print('InceptionResNetV2 loaded')
    elif arch == 'MobileNet':
        model = applications.MobileNet(input_shape=(224, 224, 3), weights='imagenet', include_top=False,
                                       input_tensor=input_tensor)
        print('MobileNet loaded')
    elif arch == 'DenseNet121':
        model = applications.DenseNet121(weights='imagenet', include_top=False, input_tensor=input_tensor)
        print('DenseNet121 loaded')
    elif arch == 'NASNetLarge':
        model = applications.NASNetLarge(weights='imagenet', include_top=False, input_tensor=input_tensor)
        print('NASNetLarge loaded')
    elif arch == 'MobileNetV2':
        model = applications.MobileNetV2(input_shape=(224, 224, 3), weights='imagenet', include_top=False,
                                         input_tensor=input_tensor)
        print('MobileNetV2 loaded')
    else:
        print('Invalid model selected')
        model = False

    return model
if cnn == 'inceptionv3':
    weights = os.path.join(
        base_dir, 'weights',
        'inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5')
    model = applications.InceptionV3(weights=weights,
                                     include_top=False,
                                     input_shape=(img_width, img_height, 3))
    layer_freeze = 249

if cnn == 'densenet121':
    weights = os.path.join(
        base_dir, 'weights',
        'densenet121_weights_tf_dim_ordering_tf_kernels_notop.h5')
    model = applications.DenseNet121(weights=weights,
                                     include_top=False,
                                     input_shape=(img_width, img_height, 3))
    layer_freeze = 313

# Now add additional layers for fine-tuning, regularization, dropout, Softmax, etc.
# Freeze early layers (up to layer specified in layer_freeze) while allowing deeper layers
# to remain trainable for fine-tuning
for layer in model.layers[:layer_freeze]:
    layer.trainable = False

x = model.output
x = Flatten()(x)

# L1/L2 regularization
if reg:
    x = Dense(1024,
                                       input_shape=(256, 256, 3))
elif args.base_model == 'inceptionv3':
    base_model = applications.InceptionV3(weights='imagenet',
                                          include_top=False,
                                          input_shape=(256, 256, 3))
elif args.base_model == 'inception_resnetv2':
    base_model = applications.InceptionResNetV2(weights='imagenet',
                                                include_top=False,
                                                input_shape=(256, 256, 3))
elif args.base_model == 'xception':
    base_model = applications.Xception(weights='imagenet',
                                       include_top=False,
                                       input_shape=(256, 256, 3))
elif args.base_model == 'densenet121':
    base_model = applications.DenseNet121(weights='imagenet',
                                          include_top=False,
                                          input_shape=(256, 256, 3))
elif args.base_model == 'densenet169':
    base_model = applications.DenseNet169(weights='imagenet',
                                          include_top=False,
                                          input_shape=(256, 256, 3))
elif args.base_model == 'densenet201':
    base_model = applications.DenseNet201(weights='imagenet',
                                          include_top=False,
                                          input_shape=(256, 256, 3))
elif args.base_model == 'nasnetmobile':
    base_model = applications.NASNetMobile(weights='imagenet',
                                           include_top=False,
                                           input_shape=(256, 256, 3))
elif args.base_model == 'nasnetlarge':
    base_model = applications.NASNetLarge(weights='imagenet',
Exemple #13
0
def create_two_branch_model(existing='', is_twohundred=False, is_halffeatures=True, channels=4):
        

    if len(existing) == 0:
        print('Loading base model (DenseNet)..')

        def crop(dimension, start, end):
            # Crops (or slices) a Tensor on a given dimension from start to end
            # example : to crop tensor x[:, :, 5:10]
            # call slice(2, 5, 10) as you want to crop on the second dimension
            def func(x):
                if dimension == 0:
                    return x[start: end]
                if dimension == 1:
                    return x[:, start: end]
                if dimension == 2:
                    return x[:, :, start: end]
                if dimension == 3:
                    return x[:, :, :, start: end]
                if dimension == 4:
                    return x[:, :, :, :, start: end]
            return Lambda(func)

        input_rgbd = Input(shape=(None, None, channels), name='input_rgbd')
        input_rgb = crop(3, 0, 3)(input_rgbd)
        input_sparse = crop(3, 3, channels)(input_rgbd)
        #base_model_sz_input = Conv2D(3, (3,3), padding='same')(input_sparse)

        # Encoder Layers
        if is_twohundred:
            base_model = applications.DenseNet201(input_shape=(None, None, 3), include_top=False, weights='imagenet', input_tensor=input_rgb)
            #base_model_sz = applications.DenseNet201(input_shape=(None, None, 3), include_top=False, weights='imagenet', input_tensor=concatenate([input_sparse, input_sparse, input_sparse], axis=-1))
            base_model_sz = applications.DenseNet201(input_shape=(None, None, channels-3), include_top=False, weights=None, input_tensor=input_sparse)
        else:
            base_model = applications.DenseNet169(input_shape=(None, None, 3), include_top=False, weights='imagenet', input_tensor=input_rgb)
            #base_model_sz = applications.DenseNet169(input_shape=(None, None, 3), include_top=False, weights='imagenet', input_tensor=concatenate([input_sparse, input_sparse, input_sparse], axis=-1))
            base_model_sz = applications.DenseNet121(input_shape=(None, None, channels-3), include_top=False, weights=None, input_tensor=input_sparse)
            pretrained = applications.DenseNet121(input_shape=(None, None, 3), include_top=False, weights='imagenet')
        for layer in pretrained.layers:
            print(layer.name)
            if layer.get_weights() != []:  # Skip input, pooling and no weights layers
                target_layer = base_model_sz.get_layer(name=layer.name)
                if layer.name != 'conv1/conv': # Initialize imagenet weights in all layers except the first conv1 layer where the channels do not match
                    target_layer.set_weights(layer.get_weights())

        print('Base model loaded.')

        # Layer freezing?
        for layer in base_model.layers: 
            layer.trainable = True
        for layer in base_model_sz.layers: 
            layer.trainable = True
            layer.name = layer.name + str("_sz")

        # Starting point for decoder
        encoder_output = concatenate([base_model.output, base_model_sz.output], axis=-1)
        base_model_output_shape = encoder_output.shape

        #base_model_output_shape = base_model.layers[-1].output.shape
        #base_model_output_shape_sz = base_model_sz.layers[-1].output.shape


        # Starting number of decoder filters
        if is_halffeatures:
            decode_filters = int(int(base_model_output_shape[-1])/2)
        else:
            decode_filters = int(base_model_output_shape[-1])

        # Define upsampling layer
        def upproject(tensor, filters, name, concat_with):
            up_i = BilinearUpSampling2D((2, 2), name=name+'_upsampling2d')(tensor)
            up_i = Concatenate(name=name+'_concat')([up_i, base_model.get_layer(concat_with).output, base_model_sz.get_layer(concat_with+str("_sz")).output]) # Skip connection
            up_i = Conv2D(filters=filters, kernel_size=3, strides=1, padding='same', name=name+'_convA')(up_i)
            up_i = LeakyReLU(alpha=0.2)(up_i)
            up_i = Conv2D(filters=filters, kernel_size=3, strides=1, padding='same', name=name+'_convB')(up_i)
            up_i = LeakyReLU(alpha=0.2)(up_i)
            return up_i

        # Decoder Layers
        decoder = Conv2D(filters=decode_filters, kernel_size=1, padding='same', input_shape=base_model_output_shape, name='conv2')(encoder_output)
        decoder = upproject(decoder, int(decode_filters/2), 'up1', concat_with='pool3_pool')
        decoder = upproject(decoder, int(decode_filters/4), 'up2', concat_with='pool2_pool')
        decoder = upproject(decoder, int(decode_filters/8), 'up3', concat_with='pool1')
        decoder = upproject(decoder, int(decode_filters/16), 'up4', concat_with='conv1/relu')
        if False: decoder = upproject(decoder, int(decode_filters/32), 'up5', concat_with='input_1')

        # Extract depths (final layer)
        conv3 = Conv2D(filters=1, kernel_size=3, strides=1, padding='same', name='conv3')(decoder)

        # Create the model
        model = Model(inputs=input_rgbd, outputs=conv3)
    else:
        # Load model from file
        if not existing.endswith('.h5'):
            sys.exit('Please provide a correct model file when using [existing] argument.')
        custom_objects = {'BilinearUpSampling2D': BilinearUpSampling2D, 'depth_loss_function': depth_loss_function}
        model = load_model(existing, custom_objects=custom_objects, compile=False)
        print('\nExisting model loaded.\n')

    print('Model created.')
    
    return model
Exemple #14
0
def mySpatialModel(model_name,
                   spatial_size,
                   nb_classes,
                   channels,
                   weights_path=None):

    input_tensor = Input(shape=(channels, spatial_size, spatial_size))
    input_shape = (channels, spatial_size, spatial_size)
    base_model = None
    predictions = None
    data_dim = 1024
    if model_name == 'ResNet50':

        input_tensor = Input(shape=(spatial_size, spatial_size, channels))
        input_shape = (spatial_size, spatial_size, channels)

        base_model = kerasApp.ResNet50(include_top=False,
                                       input_tensor=input_tensor,
                                       input_shape=input_shape,
                                       weights=weights_path,
                                       classes=nb_classes,
                                       pooling=None)
        x = base_model.output
        # 添加自己的全链接分类层 method 1
        #x = Flatten()(x)
        #predictions = Dense(nb_classes, activation='softmax')(x)
        #method 2
        x = GlobalAveragePooling2D()(x)
        x = Dense(1024, activation='relu')(x)
        predictions = Dense(nb_classes, activation='softmax')(x)
        model = Model(inputs=base_model.input, outputs=predictions)
    elif model_name == 'VGG16':
        input_tensor = Input(shape=(spatial_size, spatial_size, channels))
        input_shape = (spatial_size, spatial_size, channels)
        base_model = kerasApp.VGG16(include_top=False,
                                    input_tensor=input_tensor,
                                    input_shape=input_shape,
                                    weights=weights_path,
                                    classes=nb_classes,
                                    pooling=None)
        x = base_model.output
        x = GlobalAveragePooling2D()(
            x)  # add a global spatial average pooling layer
        x = Dense(1024,
                  activation='relu')(x)  # let's add a fully-connected layer
        predictions = Dense(nb_classes,
                            activation='softmax')(x)  # and a logistic layer
        model = Model(inputs=base_model.input, outputs=predictions)
    elif model_name == 'VGG19':
        base_model = kerasApp.VGG19(include_top=False,
                                    input_tensor=input_tensor,
                                    input_shape=input_shape,
                                    weights=weights_path,
                                    classes=2,
                                    pooling=None)

        x = base_model.output
        # 添加自己的全链接分类层
        x = GlobalAveragePooling2D()(x)
        x = Dense(1024, activation='relu')(x)
        predictions = Dense(nb_classes, activation='softmax')(x)
        model = Model(inputs=base_model.input, outputs=predictions)

    elif model_name == 'InceptionV3':
        input_tensor = Input(shape=(spatial_size, spatial_size, channels))
        input_shape = (spatial_size, spatial_size, channels)
        base_model = kerasApp.InceptionV3(weights=weights_path,
                                          include_top=False,
                                          pooling=None,
                                          input_shape=input_shape,
                                          classes=nb_classes)

        x = base_model.output
        # 添加自己的全链接分类层
        x = GlobalAveragePooling2D()(x)
        x = Dense(1024, activation='relu')(x)
        predictions = Dense(nb_classes, activation='softmax')(x)
        model = Model(inputs=base_model.input, outputs=predictions)
    elif model_name == 'InceptionResNetV2':
        input_tensor = Input(shape=(spatial_size, spatial_size, channels))
        input_shape = (
            spatial_size,
            spatial_size,
            channels,
        )
        base_model = kerasApp.InceptionResNetV2(weights=weights_path,
                                                include_top=False,
                                                pooling=None,
                                                input_shape=input_shape,
                                                classes=nb_classes)

        x = base_model.output
        # 添加自己的全链接分类层
        x = GlobalAveragePooling2D()(x)
        data_dim = 1536
        predictions = Dense(nb_classes, activation='softmax')(x)
        model = Model(inputs=base_model.input, outputs=predictions)
    elif model_name == 'Xception':
        input_shape_xception = (spatial_size, spatial_size, channels)

        base_model = kerasApp.Xception(weights=weights_path,
                                       include_top=False,
                                       pooling="avg",
                                       input_shape=input_shape_xception,
                                       classes=nb_classes)
        x = base_model.output
        predictions = Dense(nb_classes, activation='softmax')(x)
        model = Model(inputs=base_model.input, outputs=predictions)

    elif model_name == 'DenseNet121':
        base_model = kerasApp.DenseNet121(weights=weights_path,
                                          include_top=False,
                                          pooling=None,
                                          input_shape=input_shape,
                                          classes=nb_classes)

        x = base_model.output
        # 添加自己的全链接分类层
        x = GlobalAveragePooling2D()(x)

        predictions = Dense(nb_classes, activation='softmax')(x)
        model = Model(inputs=base_model.input, outputs=predictions)
    elif model_name == 'DenseNet169':
        base_model = kerasApp.DenseNet169(weights=weights_path,
                                          include_top=False,
                                          pooling=None,
                                          input_shape=input_shape,
                                          classes=nb_classes)

        x = base_model.output
        # 添加自己的全链接分类层
        x = GlobalAveragePooling2D()(x)

        predictions = Dense(nb_classes, activation='softmax')(x)
        model = Model(inputs=base_model.input, outputs=predictions)
    elif model_name == 'DenseNet201':
        base_model = kerasApp.DenseNet201(weights=weights_path,
                                          include_top=False,
                                          pooling=None,
                                          input_shape=input_shape,
                                          classes=nb_classes)

        x = base_model.output
        # 添加自己的全链接分类层
        x = GlobalAveragePooling2D()(x)
        predictions = Dense(nb_classes, activation='softmax')(x)
        model = Model(inputs=base_model.input, outputs=predictions)
    elif model_name == 'MobileNet':
        base_model = kerasApp.MobileNet(weights=weights_path,
                                        include_top=False,
                                        pooling=None,
                                        input_shape=input_shape,
                                        classes=nb_classes)
        x = base_model.output
        # 添加自己的全链接分类层
        x = GlobalAveragePooling2D()(x)
        x = Dense(1024, activation='relu')(x)
        x = Dense(1024, activation='relu')(x)
        x = Dense(512, activation='relu')(x)
        data_dim = 512
        predictions = Dense(nb_classes, activation='softmax')(x)
        model = Model(inputs=base_model.input, outputs=predictions)
    else:
        print("this model--[" + model_name + "]-- doesnt exist!")

    # 冻结base_model所有层,这样就可以正确获得bottleneck特征
    for layer in base_model.layers:
        layer.trainable = True
    # 训练模型
    model = Model(inputs=base_model.input, outputs=predictions)

    print('-------------当前base_model模型[' + model_name +
          "]-------------------\n")
    print('base_model层数目:' + str(len(base_model.layers)))
    print('model模型层数目:' + str(len(model.layers)))
    featureLayer = model.layers[len(model.layers) - 2]
    print(featureLayer.output_shape)
    print("data_dim:" + str(featureLayer.output_shape[1]))
    print("---------------------------------------------\n")

    #sgd = SGD(lr=lr, decay=decay, momentum=momentum, nesterov=True)

    # 绘制模型
    #if plot_model:
    #	plot_model(model, to_file=model_name+'.png', show_shapes=True)
    return model
Exemple #15
0
def create_model(existing=''):
    
    if len(existing)== 0 :
        #print('Loading Encoder(DenseNet)')
        encoder=applications.DenseNet121(input_shape=(None,None,3), include_top=False)
        #encoder=applications.DenseNet169(input_shape=(None,None,3), include_top=False)

        encoder_output_shape=encoder.layers[-1].output.shape
        
        for layer in encoder.layers : 
            layer.trainable = True
        
        decode_filters = int(int(encoder_output_shape[-1])/2)
        def upproject(tensor, filters, name, concat_with):
            up_i = BilinearUpSampling2D((2, 2), name=name+'_upsampling2d')(tensor)
            up_i = Concatenate(name=name+'_concat')([up_i, encoder.get_layer(concat_with).output]) # Skip connection
            up_i = Conv2D(filters=filters, kernel_size=3, strides=1, padding='same', name=name+'_convA')(up_i)
            up_i = LeakyReLU(alpha=0.2)(up_i)
            up_i = Conv2D(filters=filters, kernel_size=3, strides=1, padding='same', name=name+'_convB')(up_i)
            up_i = LeakyReLU(alpha=0.2)(up_i)
            return up_i

        # Decoder Layers for Albedo Map
        
        decoder = Conv2D(filters=decode_filters, kernel_size=1, padding='same', input_shape=encoder_output_shape, name='am_conv2')(encoder.output)

        decoder = upproject(decoder, int(decode_filters/2), 'am_up1', concat_with='pool3_pool')
        decoder = upproject(decoder, int(decode_filters/4), 'am_up2', concat_with='pool2_pool')
        decoder = upproject(decoder, int(decode_filters/8), 'am_up3', concat_with='pool1')
        decoder = upproject(decoder, int(decode_filters/16), 'am_up4', concat_with='conv1/relu')
        decoder = upproject(decoder, int(decode_filters/32), 'am_up5', concat_with='input_1')

        # Extract albedo (final layer)
        am_conv3 = Conv2D(filters=3, kernel_size=3, strides=1, padding='same', name='am_conv3')(decoder)
        
        # Decoder Layers for Shading Map

        decoder = Conv2D(filters=decode_filters, kernel_size=1, padding='same', input_shape=encoder_output_shape, name='sm_conv2')(encoder.output)

        decoder = upproject(decoder, int(decode_filters/2), 'sm_up1', concat_with='pool3_pool')
        decoder = upproject(decoder, int(decode_filters/4), 'sm_up2', concat_with='pool2_pool')
        decoder = upproject(decoder, int(decode_filters/8), 'sm_up3', concat_with='pool1')
        decoder = upproject(decoder, int(decode_filters/16), 'sm_up4', concat_with='conv1/relu')
        decoder = upproject(decoder, int(decode_filters/32), 'sm_up5', concat_with='input_1')

        # Extract shading (final layer)
        sm_conv3 = Conv2D(filters=3, kernel_size=3, strides=1, padding='same', name='sm_conv3')(decoder)

        # Create the model
        model = Model(inputs=encoder.input, outputs=[am_conv3,sm_conv3])
        

    else:
        # Load model from file
        if not existing.endswith('.h5'):
            sys.exit('Please provide a correct model file when using [existing] argument.')
        custom_objects = {'BilinearUpSampling2D': BilinearUpSampling2D, 'albedo_loss_function': albedo_loss_function, 'shading_loss_function': shading_loss_function }
        model = load_model(existing, custom_objects=custom_objects)
        print('\nExisting model loaded.\n')

    print('Model created.')

    return model
    
Exemple #16
0
def create_model(existing='', is_twohundred=False, is_halffeatures=True):

    if len(existing) == 0:
        print('Loading base model (DenseNet)..')

        # Encoder Layers
        if is_twohundred:
            base_model = applications.DenseNet201(input_shape=(None, None, 3),
                                                  include_top=False)
        else:
            # base_model = applications.DenseNet169(input_shape=(None, None, 3), include_top=False)
            base_model = applications.DenseNet121(input_shape=(None, None, 3),
                                                  include_top=False)

        print('Base model loaded.')

        # Starting point for decoder
        base_model_output_shape = base_model.layers[-1].output.shape

        # Layer freezing?
        for layer in base_model.layers:
            layer.trainable = True

        # Starting number of decoder filters
        if is_halffeatures:
            decode_filters = int(int(base_model_output_shape[-1]) / 2)
        else:
            decode_filters = int(base_model_output_shape[-1])

        # Define upsampling layer
        def upproject(tensor, filters, name, concat_with):
            up_i = BilinearUpSampling2D((2, 2),
                                        name=name + '_upsampling2d')(tensor)
            up_i = Concatenate(name=name + '_concat')(
                [up_i,
                 base_model.get_layer(concat_with).output])  # Skip connection
            up_i = Conv2D(filters=filters,
                          kernel_size=3,
                          strides=1,
                          padding='same',
                          name=name + '_convA')(up_i)
            up_i = LeakyReLU(alpha=0.2)(up_i)
            up_i = Conv2D(filters=filters,
                          kernel_size=3,
                          strides=1,
                          padding='same',
                          name=name + '_convB')(up_i)
            up_i = LeakyReLU(alpha=0.2)(up_i)
            return up_i

        # Decoder Layers
        decoder = Conv2D(filters=decode_filters,
                         kernel_size=1,
                         padding='same',
                         input_shape=base_model_output_shape,
                         name='conv2')(base_model.output)

        decoder = upproject(decoder,
                            int(decode_filters / 2),
                            'up1',
                            concat_with='pool3_pool')
        decoder = upproject(decoder,
                            int(decode_filters / 4),
                            'up2',
                            concat_with='pool2_pool')
        decoder = upproject(decoder,
                            int(decode_filters / 8),
                            'up3',
                            concat_with='pool1')
        decoder = upproject(decoder,
                            int(decode_filters / 16),
                            'up4',
                            concat_with='conv1/relu')
        if False:
            decoder = upproject(decoder,
                                int(decode_filters / 32),
                                'up5',
                                concat_with='input_1')

        # Extract depths (final layer)
        conv3 = Conv2D(filters=1,
                       kernel_size=3,
                       strides=1,
                       padding='same',
                       name='conv3')(decoder)

        # Create the model
        model = Model(inputs=base_model.input, outputs=conv3)
    else:
        # Load model from file
        if not existing.endswith('.h5'):
            sys.exit(
                'Please provide a correct model file when using [existing] argument.'
            )
        custom_objects = {
            'BilinearUpSampling2D': BilinearUpSampling2D,
            'depth_loss_function': depth_loss_function
        }
        model = load_model(existing, custom_objects=custom_objects)
        print('\nExisting model loaded.\n')

    print('Model created.')

    return model
Exemple #17
0
def mySpatialModelChannelTest(model_name,spatial_size, nb_classes, channels, channel_first=True, weights_path=None,
				   lr=0.005, decay=1e-6, momentum=0.9,plot_model=True):

	input_tensor = Input(shape=(channels, spatial_size, spatial_size))
	input_shape = (channels, spatial_size, spatial_size)
	base_model=None
	predictions=None
	data_dim=1024

	base_model = kerasApp.ResNet50(include_top=False, input_tensor=input_tensor, input_shape=input_shape,
									   weights=None, classes=nb_classes, pooling=None)
	x = base_model.output
	x = GlobalAveragePooling2D()(x)
	x = Dense(1024, activation='relu')(x)
	predictions = Dense(nb_classes, activation='softmax')(x)
	# 训练模型
	model = Model(inputs=base_model.input, outputs=predictions)
	print_shape(model,model_name)


	base_model = kerasApp.VGG16(include_top=False, input_tensor=input_tensor, input_shape=input_shape,
								   weights=None, classes=nb_classes, pooling=None)
	x = base_model.output
	# 添加自己的全链接分类层
	x = GlobalAveragePooling2D()(x)  # add a global spatial average pooling layer
	x = Dense(1024, activation='relu')(x)  # let's add a fully-connected layer
	predictions = Dense(nb_classes, activation='softmax')(x)
	# 训练模型
	model = Model(inputs=base_model.input, outputs=predictions)
	print_shape(model, model_name)

	base_model = kerasApp.VGG19(include_top=False, input_tensor=input_tensor, input_shape=input_shape,
								weights=None, classes=2, pooling='avg')
	print_shape(base_model, model_name)
	base_model = kerasApp.InceptionV3(weights=None, include_top=False, pooling=None,
							 input_shape=input_shape, classes=nb_classes)
	print_shape(base_model, model_name)
	base_model = kerasApp.InceptionResNetV2(weights=None, include_top=False, pooling=None,
							 input_shape=input_shape, classes=nb_classes)
	x = base_model.output
	# 添加自己的全链接分类层
	x = GlobalAveragePooling2D()(x)
	predictions = Dense(nb_classes, activation='softmax')(x)
	# 训练模型
	model = Model(inputs=base_model.input, outputs=predictions)
	print_shape(model, model_name)
	#channel last
	input_tensor_Xception = Input(shape=( spatial_size, spatial_size,channels))
	input_shape__Xception = (spatial_size, spatial_size,channels)
	base_model = kerasApp.Xception(weights=None, include_top=False, pooling=None,
											input_shape=input_shape__Xception, classes=nb_classes)
	print_shape(base_model, model_name)

	base_model = kerasApp.DenseNet121(weights=None, include_top=False, pooling=None,
											input_shape=input_shape, classes=nb_classes)
	print_shape(base_model, model_name)

	base_model = kerasApp.DenseNet169(weights=None, include_top=False, pooling=None,
											input_shape=input_shape, classes=nb_classes)

	print_shape(base_model, model_name)

	base_model = kerasApp.DenseNet201(weights=None, include_top=False, pooling=None,
											input_shape=input_shape, classes=nb_classes)

	print_shape(base_model, model_name)
	input_shape = (channels, spatial_size, spatial_size)

	base_model = kerasApp.MobileNet(weights=None, include_top=False, pooling=None,
												  input_shape=input_shape, classes=nb_classes)