예제 #1
0
def test_xception_variable_input_channels():
    input_shape = (1, None, None) if K.image_data_format() == 'channels_first' else (None, None, 1)
    model = applications.Xception(weights=None, include_top=False, input_shape=input_shape)
    assert model.output_shape == (None, None, None, 2048)

    input_shape = (4, None, None) if K.image_data_format() == 'channels_first' else (None, None, 4)
    model = applications.Xception(weights=None, include_top=False, input_shape=input_shape)
    assert model.output_shape == (None, None, None, 2048)
예제 #2
0
    def existing_model(base_model, base_model_layer, l):
        if base_model == 'VGG16':
            base_model = applications.VGG16(
                weights="imagenet",
                include_top=False,
                input_shape=(constants.IMAGE_HEIGHT, constants.IMAGE_WIDTH, 3))
        elif base_model == 'ResNet50':
            base_model = applications.ResNet50(
                weights="imagenet",
                include_top=False,
                input_shape=(constants.IMAGE_HEIGHT, constants.IMAGE_WIDTH, 3))
        elif base_model == 'Xception':
            base_model = applications.Xception(
                weights="imagenet",
                include_top=False,
                input_shape=(constants.IMAGE_HEIGHT, constants.IMAGE_WIDTH, 3))
        else:
            raise "Unrecognized existing model {}".format(base_model)

        if base_model_layer == -1:
            x = base_model.layers[-1].output
        else:
            for layer in base_model.layers:
                layer.trainable = False
            x = base_model.layers[base_model_layer].output

        x = Flatten()(x)
        x = Dense(512,
                  activation="relu",
                  kernel_regularizer=regularizers.l2(l))(x)
        x = Dense(128,
                  activation="relu",
                  kernel_regularizer=regularizers.l2(l))(x)
        x = Dense(2, activation='sigmoid')(x)
        return Model(input=base_model.input, output=x)
예제 #3
0
def callModel(model_picked = 'vgg16'):
    '''function returns the model picked based on input
    Input choices:
        'vgg16'     - VGG16
        'vgg19'     - VGG19
        'res50'     - ResNet50
        'xception'  - Xception
        'inception' - InceptionV3
        'monet'     - MobileNetV2
    '''
    #The models have a series of convolutional layers and then they have dense(deeply connected layers)
    #include_top = False only gets the convo layers and ignores the dense layer
    #imagenet is a huge image dataset on which the models are trained. if weights ='imagenet' means the weights are acquired from that.
    if model_picked == 'vgg16':
        model = applications.VGG16(include_top=False, weights='imagenet')
    elif model_picked =='vgg19':
        model = applications.VGG19(include_top=False, weights='imagenet')
    elif model_picked == 'res50':
        model = applications.ResNet50(include_top=False, weights='imagenet')
    elif model_picked == 'xception':
        model = applications.Xception(include_top=False, weights='imagenet')
    elif model_picked == 'inception':
        model = applications.InceptionV3(include_top=False, weights='imagenet')
    elif model_picked == 'monet':
        model = applications.MobileNetV2(include_top=False, weights='imagenet',
        input_shape=(224,224,3))
    return model
예제 #4
0
def build_xception_classifier(input_shape, num_classes, l2_coeff=0.01):
    xception = applications.Xception(include_top=False,
                                     weights=None,
                                     input_shape=(input_shape[0],
                                                  input_shape[1], 3))
    xception.trainable = True

    inputs = Input(shape=(input_shape[0], input_shape[1], 3), name='in1')

    x = xception(inputs)
    x = GlobalAveragePooling2D()(x)
    x = BatchNormalization()(x)
    x = Activation('relu')(x)
    x = Dense(1024, kernel_regularizer=regularizers.l2(l2_coeff))(x)
    x = BatchNormalization()(x)
    x = Activation('relu')(x)
    x = Dropout(0.5)(x)
    x = Dense(512, kernel_regularizer=regularizers.l2(l2_coeff))(x)
    x = BatchNormalization()(x)
    x = Activation('relu')(x)
    x = Dropout(0.5)(x)
    x = Dense(num_classes)(x)
    x = Activation('sigmoid')(x)

    model = Model(inputs, x, name='xception_based_classifier')

    return model
예제 #5
0
파일: utils.py 프로젝트: koaning/wallholla
def get_pretrained_model(model, img_size):
    possible_names = ["vgg16", "vgg19", "mobilenet", "xception"]
    if model not in possible_names:
        raise ValueError(f"model needs to be either {possible_names}")
    logger.debug(f"backend for model is {model}")
    width, height = img_size
    if K.image_data_format() == 'channels_first':
        input_shape = (3, width, height)
    else:
        input_shape = (width, height, 3)
    logger.debug(
        f"backend suggests that we have the following input shape: {input_shape}"
    )
    models = {
        "vgg16":
        applications.VGG16(input_shape=input_shape,
                           include_top=False,
                           weights='imagenet'),
        "vgg19":
        applications.VGG19(input_shape=input_shape,
                           include_top=False,
                           weights='imagenet'),
        "mobilenet":
        applications.MobileNet(input_shape=input_shape,
                               include_top=False,
                               weights='imagenet'),
        "xception":
        applications.Xception(input_shape=input_shape,
                              include_top=False,
                              weights='imagenet')
    }
    return models[model]
예제 #6
0
def save_bottlebeck_features():
    datagen = ImageDataGenerator(rescale=1. / 255)

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

    generator = datagen.flow_from_directory(train_data_dir,
                                            target_size=(img_width,
                                                         img_height),
                                            batch_size=batch_size,
                                            class_mode='categorical',
                                            shuffle=False)
    bottleneck_features_train = model.predict_generator(
        generator, nb_train_samples // batch_size)
    np.save(open('xception_bottleneck_features_train.npy', 'wb'),
            bottleneck_features_train)

    generator = datagen.flow_from_directory(validation_data_dir,
                                            target_size=(img_width,
                                                         img_height),
                                            batch_size=batch_size,
                                            class_mode='categorical',
                                            shuffle=False)
    bottleneck_features_validation = model.predict_generator(
        generator, nb_validation_samples // batch_size)
    np.save(open('xception_bottleneck_features_validation.npy', 'wb'),
            bottleneck_features_validation)

    class_dictionary = generator.class_indices
    print(class_dictionary)
예제 #7
0
def callModel(model_picked='vgg16'):
    '''function returns the model picked based on input
    Input choices:
        'vgg16'     - VGG16
        'vgg19'     - VGG19
        'res50'     - ResNet50
        'xception'  - Xception
        'inception' - InceptionV3
        'monet'     - MobileNetV2
    '''
    if model_picked == 'vgg16':
        model = applications.VGG16(include_top=False, weights='imagenet')
    elif model_picked == 'vgg19':
        model = applications.VGG19(include_top=False, weights='imagenet')
    elif model_picked == 'res50':
        model = applications.ResNet50(include_top=False, weights='imagenet')
    elif model_picked == 'xception':
        model = applications.Xception(include_top=False, weights='imagenet')
    elif model_picked == 'inception':
        model = applications.InceptionV3(include_top=False, weights='imagenet')
    elif model_picked == 'monet':
        model = applications.MobileNetV2(include_top=False,
                                         weights='imagenet',
                                         input_shape=(224, 224, 3))
    return model
def model_define(modeltype, inputshape):

    if modeltype == 'define':
        model = customize_mode()
        print('Model: define !')
    elif modeltype == 'EfficientNetB3':
        model = efn.EfficientNetB3(include_top=False,
                                   weights='imagenet',
                                   input_tensor=None,
                                   input_shape=inputshape,
                                   pooling=None)
        freeze_layers(model)
        print('Model: EfficientNetB3, weights loaded!')
    elif modeltype == 'ResNet50':
        model = applications.ResNet50(include_top=False,
                                      weights='imagenet',
                                      input_shape=inputshape,
                                      pooling='avg')
        freeze_layers(model)
        print('Model: ResNet50, weights loaded!')
    elif modeltype == 'Xception':
        model = applications.Xception(include_top=False,
                                      weights='imagenet',
                                      input_shape=inputshape)
        freeze_layers(model)
        print('Model: Xception, weights loaded!')
    else:
        pass

    return model
예제 #9
0
def Xception(shape, num_classes, last_activation):
    base_model = applications.Xception(weights='imagenet', include_top=False, input_shape=shape)
    x = base_model.output
    x = GlobalAveragePooling2D()(x)
    x = Dense(num_classes, activation=last_activation)(x)
    model = Model(base_model.input, x)
    
    return model
예제 #10
0
def train_pretrained_model(model_name='xception',
                           hidden_layers=2,
                           hidden_neurons=512,
                           loss_function='binary_crossentropy',
                           top_epochs=100,
                           batch_size=16,
                           optimizer=Adagrad(lr=.0001),
                           patience=3,
                           validation_split=.1,
                           dropout=.2):

    config = tf.ConfigProto()
    set_session(tf.Session(config=config))

    # load training data
    train_X = np.load(os.path.join('training_data', 'train_X.npy'))
    train_Y = np.load(os.path.join('training_data', 'train_Y.npy'))
    classes = len(train_Y[0])
    checkpoint_path = os.path.join(
        'checkpoints', model_name + '{epoch:02d}-{val_loss:.3f}.mod')

    # xception
    base_model = applications.Xception(include_top=False,
                                       weights='imagenet',
                                       input_shape=(299, 299, 3))

    # don't train base layers
    for layer in base_model.layers:
        layer.trainable = False

    # add new top model
    x = base_model.output
    x = Flatten()(x)
    for i in range(0, hidden_layers):
        x = Dense(hidden_neurons,
                  activation='relu',
                  kernel_initializer='glorot_uniform')(x)
        x = Dropout(dropout)(x)
    predictions = Dense(classes, activation='sigmoid')(x)

    model = Model(inputs=base_model.inputs, outputs=predictions)
    model.summary()

    # train new top layers
    model.compile(optimizer=optimizer, metrics=[], loss=loss_function)

    callbacks = [
        EarlyStopping(monitor='val_loss', min_delta=0.001, patience=patience),
        ModelCheckpoint(checkpoint_path, period=5)
    ]
    model.fit(train_X,
              train_Y,
              epochs=top_epochs,
              batch_size=batch_size,
              validation_split=validation_split,
              callbacks=callbacks)
    save_path = os.path.join('checkpoints', model_name + '_trained')
    model.save(save_path)
예제 #11
0
def models_factory(model_type, image_size):

    if model_type == "vgg16":
        base_model = applications.VGG16(weights='imagenet',
                                        include_top=False,
                                        input_shape=(image_size[0],
                                                     image_size[1], 3))
    elif model_type == "vgg19":
        base_model = applications.VGG19(weights='imagenet',
                                        include_top=False,
                                        input_shape=(image_size[0],
                                                     image_size[1], 3))
    elif model_type == "resnet50":
        base_model = applications.ResNet50(weights='imagenet',
                                           include_top=False,
                                           input_shape=(image_size[0],
                                                        image_size[1], 3))
    elif model_type == "inceptionv3":
        base_model = applications.InceptionV3(weights='imagenet',
                                              include_top=False,
                                              input_shape=(image_size[0],
                                                           image_size[1], 3))
    elif model_type == "xception":
        base_model = applications.Xception(weights='imagenet',
                                           include_top=False,
                                           input_shape=(image_size[0],
                                                        image_size[1], 3))
    elif model_type == "mobilenet":
        base_model = applications.MobileNet(weights='imagenet',
                                            include_top=False,
                                            input_shape=(image_size[0],
                                                         image_size[1], 3))
    elif model_type == "inceptionresnetv2":
        base_model = applications.InceptionResNetV2(weights='imagenet',
                                                    include_top=False,
                                                    input_shape=(image_size[0],
                                                                 image_size[1],
                                                                 3))
    elif model_type == "nasnet":
        base_model = applications.nasnet.NASNetLarge(
            weights='imagenet',
            include_top=False,
            input_shape=(image_size[0], image_size[1], 3))

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

    top_model = Sequential()
    top_model.add(Flatten(input_shape=base_model.output_shape[1:]))
    top_model.add(
        Dense(1024, kernel_initializer='glorot_uniform', activation='relu'))
    top_model.add(
        Dense(1024, kernel_initializer='glorot_uniform', activation='relu'))
    top_model.add(Dense(1, activation='sigmoid'))
    model = Model(input=base_model.input, output=top_model(base_model.output))

    return model, base_model
예제 #12
0
def Shared_Xception():
    xception_base = applications.Xception(weights= None,include_top = False)

    left_input = Input(shape=(250,250,3))
    right_input = Input(shape=(250,250,3))
    left_features = xception_base(left_input)
    right_input = xception_base(right_input)

    merged_features = layers.concatenate([left_features,right_input],axis=-1)
예제 #13
0
def Xception(input_shape = (256, 256, 3), weights = 'imagenet', num_classes = 5):
	model_origin = applications.Xception(include_top = False, weights = weights, input_shape = input_shape)
	for layer in model_origin.layers:
		layer.trainable = True

	#layer_name = 'block13_sepconv2_bn'
	p = GlobalAveragePooling2D()(model_origin.output)
	o = Dense(num_classes, activation = 'softmax')(p)
	model = Model(inputs = model_origin.input, output = [o])
	return model
예제 #14
0
파일: features.py 프로젝트: mme/vergeml
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
예제 #15
0
def create_Xception(image_size, num_class):
    resnet_conv = applications.Xception(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
예제 #16
0
파일: keras.py 프로젝트: kmader/tfdeploy
    def test_big_models(self):
        """
        A test for bigger commonly used pretrained models (for this we skip the weights)
        :return: 
        """
        kapp_kwargs = dict(
            input_shape=(99, 99, 3),
            weights=None,
            include_top=False  # so we can use different sizes
        )
        test_models = []

        test_models += [('Resnet50', kapps.ResNet50(**kapp_kwargs))]
        test_models += [('InceptionV3', kapps.InceptionV3(**kapp_kwargs))]
        test_models += [('VGG19', kapps.VGG19(**kapp_kwargs))]
        test_models += [('Xception', kapps.Xception(**kapp_kwargs))]

        for i, (model_name, cur_keras_model) in enumerate(test_models):

            model_layers = ','.join(
                map(lambda x: x.name, cur_keras_model.layers))
            out_path = "%04d.pkl" % i
            try:
                c_model_pkl = KerasTestCase.export_keras_model(
                    cur_keras_model, out_path, model_name=model_layers)
            except td.UnknownOperationException as uoe:
                print('Model {}: {}'.format(i, model_layers),
                      'could not be serialized', uoe)
                bad_layer_count = sum([
                    us_layer in model_layers for us_layer in UNSUPPORTED_LAYERS
                ])
                self.assertGreater(
                    bad_layer_count, 0,
                    "Model contains no unsupported layers {}, "
                    "Unsupported Layers:{}".format(model_layers,
                                                   UNSUPPORTED_LAYERS))
                continue
            except tf.errors.RESOURCE_EXHAUSTED:
                # many of the bigger models take up quite a bit of GPU memory
                print('Model {} with #{} layers is too big for memory'.format(
                    model_name, len(cur_keras_model.layers)))

            result = KerasTestCase.deploy_model(
                c_model_pkl, np.random.uniform(0, 1, size=(299, 299, 3)))
            self.assertIsNotNone(result, "Result should not be empty")
            self.assertEqual(
                len(result.shape), 4,
                "Output should be 4D Tensor: {}".format(result.shape))
            os.remove(c_model_pkl['path'])
예제 #17
0
 def getModel(self):
     # Gets Xception model. The classification layer of the model is sliced off by setting include_top=False. New classification layer is added as the model has typically 1000 classes
     base_model = applications.Xception(weights="imagenet",
                                        include_top=False,
                                        input_shape=(img_width, img_height,
                                                     3))
     x = base_model.output
     x = Flatten()(x)
     x = Dense(1024, activation="relu")(x)
     predictions = Dense(2, activation="softmax")(x)
     for layer in base_model.layers:
         layer.trainable = False
     model = Model(input=base_model.input, output=predictions)
     model.summary()
     return model
예제 #18
0
def get_model(name='VGG19'):
    if name=='VGG16':
        return applications.VGG16(),(224, 224)
    elif name=='VGG19':
        return applications.VGG19(),(224, 224)
    elif name=='ResNet50':
        return applications.ResNet50(),(224, 224)
    elif name=='InceptionV3':
        return applications.InceptionV3(),(299,299)
    elif name=='MobileNet':
        return applications.MobileNet(),(224, 224) # any>32*32
    elif name=='Xception':
        return applications.Xception(),(299,299)
    else:
        return None, None
예제 #19
0
def getModel():
    xception_base = applications.Xception(
        weights=None, include_top=False)  # 使用图像处理基础模型Xception,不包括顶部

    left_input = Input(shape=(250, 250, 3))
    right_input = Input(shape=(250, 250, 3))

    left_features = xception_base(left_input)  # 将模型xception_base当作层来使用
    right_features = xception_base(right_input)

    merged_features = layers.concatenate([left_features, right_features],
                                         axis=-1)

    model = models.Model([left_input, right_input], merged_features)

    return model
예제 #20
0
def bottom_layers_builder(originalSize,resizeFactor):
	"""
	[email protected]
	"""
	img_size = originalSize*resizeFactor

	if k.image_data_format() == 'channels_first':
		input_shape = (3, img_size, img_size)
	else:
		input_shape = (img_size, img_size, 3)

	#model = applications.InceptionV3(weights = "imagenet", include_top=False, input_shape = (img_size, img_size, 3))
	model = applications.Xception(weights = "imagenet", include_top=False, input_shape = input_shape)

	for layer in model.layers :
		layer.trainable = False
	return model
예제 #21
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
예제 #22
0
파일: train.py 프로젝트: yangliu2/webscrap
def get_base_model(model_type):
    input_tensor = Input(shape=(img_width,img_height,3))
    input_shape = shape=(img_width,img_height,3)
    if(model_type == 'vgg16'):       
        # build the VGG16 network
        #NOTE: VGG16 code needs the base model downloaded or else it will try to download it
        preferred_size = (224,224)
        return applications.VGG16(include_top=False, weights='imagenet',input_tensor=input_tensor)
    elif(model_type == 'inception'):
        preferred_size = (299,299)
        if (preferred_size_matches_scale()):
            return applications.InceptionV3(include_top=False, weights='imagenet')
        else:
            return applications.InceptionV3(include_top=False, weights='imagenet', input_shape=input_shape)
    elif(model_type == 'xception'):
        print('Using Xception')
        return applications.Xception(include_top=False, weights='imagenet', input_shape=input_shape)
    elif(model_type == 'resnet50'):
        preferred_size = (224,224)
        return applications.ResNet50(include_top=False, weights='imagenet')
def get_xception_model(num_classes, input_shape):
    model = applications.Xception(weights="imagenet",
                                  include_top=False,
                                  input_shape=input_shape)

    # # Freeze the layers which you don't want to train. Here I am freezing the first 5 layers.
    # for layer in model.layers[:5]:
    #     layer.trainable = False
    #
    # # Adding custom Layers
    x = model.output
    # x = Flatten()(x)
    # x = Dense(512, activation="relu")(x)
    # x = Dropout(0.5)(x)
    # x = Dense(512, activation="relu")(x)
    predictions = Dense(num_classes, activation="softmax")(x)

    # creating the final model
    model_final = Model(inputs=model.input, output=predictions)
    model_final.summary()
    return model_final
예제 #24
0
def pretrained_model(arch_name, input_shape=(224, 224, 3)):
    """
         返回在 ImageNet 训练好的网络模型

         args:
             arch: str, 模型名字, 
                        eg: 'xception'
             input_shape, tuple, 模型输入维度, 
                        eg: (224, 224, 3)

         return:
              model, keras model, 一个没有被freeze的网络模型

    """
    arch_name = arch_name.lower()
    arch_set = {'xception', 'vgg16', 'vgg19', 'resnet50', 'inception_v3'}
    if arch_name == 'xception':
        model = applications.Xception(include_top=True,
                                      weights='imagenet',
                                      input_shape=input_shape)
    elif arch_name == 'vgg16':
        model = applications.VGG16(include_top=True,
                                   weights='imagenet',
                                   input_shape=input_shape)
    elif arch_name == 'vgg19':
        model = applications.VGG19(include_top=True,
                                   weights='imagenet',
                                   input_shape=input_shape)
    elif arch_name == 'resnet50':
        model = applications.ResNet50(include_top=True,
                                      weights='imagenet',
                                      input_shape=input_shape)
    elif arch_name == 'inception_v3':
        model = applications.InceptionV3(include_top=True,
                                         weights='imagenet',
                                         input_shape=input_shape)
    else:
        raise ValueError('只能使用以下网络结构 \n{}\n'.format(arch_set))
    build_model(model)
    return model
def createXception(img_rows=299, img_cols=299, channel=3, num_classes=25):
    base_model = applications.Xception(input_shape=(img_rows, img_cols,
                                                    channel),
                                       weights='imagenet',
                                       include_top=False)
    out = base_model.output
    out = GlobalAveragePooling2D()(out)
    out = Dropout(0.5)(out)
    pred_out = Dense(num_classes, activation='softmax')(out)

    # add your top layer block to your base model
    model = Model(base_model.input, pred_out)

    model.compile(loss='categorical_crossentropy',
                  optimizer=optimizers.SGD(lr=1e-3,
                                           momentum=0.9,
                                           decay=1e-6,
                                           nesterov=True),
                  metrics=['accuracy'])

    model.summary()
    return model
예제 #26
0
    def create_model(self):
        base_model = applications.Xception(weights='imagenet',
                                           include_top=False,
                                           input_shape=(self.ROWS, self.COLS,
                                                        3))

        for layer in base_model.layers[:5]:
            layer.trainable = False

        add_model = Sequential()
        add_model.add(base_model)
        add_model.add(Flatten())
        add_model.add(Dropout(0.2))
        add_model.add(Dense(1024, activation='relu'))
        add_model.add(Dropout(0.3))
        add_model.add(Dense(1024, activation='relu'))
        add_model.add(Dense(2, activation='softmax'))

        self.model = add_model

        self.model.compile(loss='categorical_crossentropy',
                           optimizer=optimizers.SGD(lr=1e-3, momentum=0.9),
                           metrics=['accuracy'])
        self.model.summary()
예제 #27
0
def test_xception_pooling():
    model = applications.Xception(weights=None,
                                  include_top=False,
                                  pooling='avg')
    assert model.output_shape == (None, 2048)
예제 #28
0
def test_xception_notop():
    model = applications.Xception(weights=None, include_top=False)
    assert model.output_shape == (None, None, None, 2048)
예제 #29
0
def test_xception():
    model = applications.Xception(weights=None)
    assert model.output_shape == (None, 1000)
""" Test case for Keras """
from __future__ import absolute_import

import numpy as np
import keras.applications as models
from perceptron.models.classification.keras import KerasModel
from perceptron.utils.image import imagenet_example
from perceptron.benchmarks.additive_noise import AdditiveGaussianNoiseMetric
from perceptron.utils.criteria.classification import TopKMisclassification
from perceptron.utils.tools import plot_image
from perceptron.utils.tools import bcolors

# instantiate the model from keras applications
xception = models.Xception(weights='imagenet')

# initialize the KerasModel
# keras xception has input bound (0, 1)
mean = np.array([0.485, 0.456, 0.406]).reshape((1, 1, 3))
std = np.array([0.229, 0.224, 0.225]).reshape((1, 1, 3))
kmodel = KerasModel(xception, bounds=(0, 1), preprocessing=(mean, std))

# get source image and label
# the model Xception expects values in [0, 1] with shape (299, 299), and channles_last
image, _ = imagenet_example(shape=(299, 299), data_format='channels_last')
image /= 255.0
label = np.argmax(kmodel.predictions(image))

metric = AdditiveGaussianNoiseMetric(kmodel,
                                     criterion=TopKMisclassification(10))

print(bcolors.BOLD + 'Process start' + bcolors.ENDC)