Пример #1
0
def test_mobilenet_variable_input_channels():
    input_shape = (1, None, None) if K.image_data_format() == 'channels_first' else (None, None, 1)
    model = applications.MobileNet(weights=None, include_top=False, input_shape=input_shape)
    assert model.output_shape == (None, None, None, 1024)

    input_shape = (4, None, None) if K.image_data_format() == 'channels_first' else (None, None, 4)
    model = applications.MobileNet(weights=None, include_top=False, input_shape=input_shape)
    assert model.output_shape == (None, None, None, 1024)
Пример #2
0
def test_mobilenet_image_size():
    valid_image_sizes = [128, 160, 192, 224]
    for size in valid_image_sizes:
        input_shape = (size, size, 3) if K.image_data_format() == 'channels_last' else (3, size, size)
        model = applications.MobileNet(input_shape=input_shape, weights='imagenet', include_top=True)
        assert model.input_shape == (None,) + input_shape

    invalid_image_shape = (112, 112, 3) if K.image_data_format() == 'channels_last' else (3, 112, 112)
    with pytest.raises(ValueError):
        model = applications.MobileNet(input_shape=invalid_image_shape, weights='imagenet', include_top=True)
Пример #3
0
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
Пример #4
0
 def test_04_plain_text_script(self):
     model = applications.MobileNet(weights='imagenet',
                                    include_top=False,
                                    input_shape=(224, 224, 3))
     x = model.output
     x = Flatten()(x)
     x = Dense(1024, activation="relu")(x)
     predictions = Dense(2, activation='sigmoid')(x)
     model_final = Model(inputs=model.input,
                         outputs=predictions,
                         name='predictions')
     script_content = open("nyoka/tests/preprocess.py", 'r').read()
     pmml_obj = KerasToPmml(model_final,
                            dataSet='image',
                            predictedClasses=['cat', 'dog'],
                            script_args={
                                "content": script_content,
                                "def_name": "getBase64EncodedString",
                                "return_type": "string",
                                "encode": False
                            })
     pmml_obj.export(open("script_with_keras.pmml", 'w'), 0)
     self.assertEqual(os.path.isfile("script_with_keras.pmml"), True)
     reconPmmlObj = pml.parse("script_with_keras.pmml", True)
     content = reconPmmlObj.TransformationDictionary.DefineFunction[
         0].Apply.Extension[0].anytypeobjs_
     content[0] = content[0].replace("\t", "")
     content = "\n".join(content)
     self.assertEqual(script_content, content)
     self.assertEqual(len(model_final.layers),
                      len(reconPmmlObj.DeepNetwork[0].NetworkLayer))
Пример #5
0
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 get_transfer_model():
    # import inception with pre-trained weights. do not include fully #connected layers
    if MODEL == "resnet50":
        base_model = applications.ResNet50(weights='imagenet',
                                           include_top=False)
    elif MODEL == "mobilenet":
        base_model = applications.MobileNet(weights='imagenet',
                                            include_top=False,
                                            input_shape=input_shape)
    else:
        raise Exception("Invalid model: '{}'".format(MODEL))

    # add a global spatial average pooling layer
    x = base_model.output
    x = GlobalAveragePooling2D()(x)

    # add a fully-connected layer
    x = Dense(512, activation='relu')(x)

    # and a fully connected output/classification layer
    predictions = Dense(CLASS_COUNT, activation='softmax')(x)

    # create the full network so we can train on it
    transfer_learning_model = Model(inputs=base_model.input,
                                    outputs=predictions)

    # create the full network so we can train on it
    transfer_learning_model.compile(loss='categorical_crossentropy',
                                    optimizer=optimizer,
                                    metrics=['accuracy'])
    return transfer_learning_model
Пример #7
0
    def test_01_image_classifier_with_image_as_input(self):
        model = applications.MobileNet(weights='imagenet',
                                       include_top=False,
                                       input_shape=(224, 224, 3))
        activType = 'sigmoid'
        x = model.output
        x = Flatten()(x)
        x = Dense(1024, activation="relu")(x)
        predictions = Dense(2, activation=activType)(x)
        model_final = Model(inputs=model.input,
                            outputs=predictions,
                            name='predictions')

        cnn_pmml = KerasToPmml(model_final,model_name="MobileNetImage",description="Demo",\
            copyright="Internal User",dataSet='image',predictedClasses=['dogs','cats'])
        cnn_pmml.export(open('2classMBNet.pmml', "w"), 0)

        img = image.load_img('nyoka/tests/resizedCat.png')
        img = img_to_array(img)
        img = preprocess_input(img)
        imgtf = np.expand_dims(img, axis=0)
        model_pred = model_final.predict(imgtf)
        model_preds = {'dogs': model_pred[0][0], 'cats': model_pred[0][1]}

        model_name = self.adapa_utility.upload_to_zserver('2classMBNet.pmml')

        predictions, probabilities = self.adapa_utility.score_in_zserver(
            model_name, 'nyoka/tests/resizedCat.png', 'DN')

        self.assertEqual(
            abs(probabilities['cats'] - model_preds['cats']) < 0.00001, True)
        self.assertEqual(
            abs(probabilities['dogs'] - model_preds['dogs']) < 0.00001, True)
    def resnet_or_mobile():
        # If you want to use your our weight pre trained##
        #weights_res='/share_alpha/Submarina/pre_treined_features/new_dataset/resnet50_weights_tf_dim_ordering_tf_kernels.h5'
        #resnet_50_model = applications.ResNet50(weights='imagenet', include_top=True)#,input_tensor=input_tensor)

        mobile = applications.MobileNet(weights='imagenet', include_top=True)
        mobile.summary()
        return mobile
Пример #9
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
Пример #10
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
Пример #11
0
def get_mobilenet():
  baseModel = applications.MobileNet(weights='imagenet', include_top=False, input_shape=(constants.IMG_HEIGHT, constants.IMG_WIDTH, 3))
  mobileModel = Sequential()
  mobileModel.add(baseModel)
  mobileModel.add(GlobalAveragePooling2D())
  mobileModel.add(Dense(512, activation = 'relu'))
  mobileModel.add(Dense(256, activation = 'relu'))    
  mobileModel.add(Dropout(0.5))
  mobileModel.add(Dense(constants.NUM_CLASSES, activation='softmax'))
  mobileModel.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) 
  return mobileModel
Пример #12
0
 def setUpClass(self):
     print("******* Unit Test for Keras *******")
     model = applications.MobileNet(weights='imagenet',
                                    include_top=False,
                                    input_shape=(224, 224, 3))
     activType = 'sigmoid'
     x = model.output
     x = Flatten()(x)
     x = Dense(1024, activation="relu")(x)
     predictions = Dense(2, activation=activType)(x)
     self.model_final = Model(inputs=model.input,
                              outputs=predictions,
                              name='predictions')
Пример #13
0
def detector_mobilenet(win_h=32, win_w=32, win_ch=1, final_activation='sigmoid'):

    initial_model = applications.MobileNet(include_top=False, input_shape=(win_h, win_w, win_ch), weights=None)

    x = initial_model.output
    x = Flatten()(x)
    out = Dense(1, activation=final_activation)(x)

    detector = Model(inputs=initial_model.input, outputs=out)
    detector.compile(loss="binary_crossentropy", optimizer="adam", metrics=["accuracy"])
    detector.summary()

    return detector, initial_model
Пример #14
0
    def test_keras_01(self):

        model = applications.MobileNet(weights='imagenet', include_top=False,input_shape = (224, 224,3))
        activType='sigmoid'
        x = model.output
        x = Flatten()(x)
        x = Dense(1024, activation="relu")(x)
        predictions = Dense(2, activation=activType)(x)
        model_final = Model(inputs =model.input, outputs = predictions,name='predictions')
        cnn_pmml = KerasToPmml(model_final,model_name="MobileNet",description="Demo",\
            copyright="Internal User",dataSet='image',predictedClasses=['cats','dogs'])
        cnn_pmml.export(open('2classMBNet.pmml', "w"), 0)
        reconPmmlObj=ny.parse('2classMBNet.pmml',True)
        self.assertEqual(os.path.isfile("2classMBNet.pmml"),True)
        self.assertEqual(len(model_final.layers), len(reconPmmlObj.DeepNetwork[0].NetworkLayer))
Пример #15
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
Пример #16
0
 def test_construction_mobilenet(self):
     model = applications.MobileNet(weights = "imagenet", include_top=False,input_shape = (224, 224, 3))
     x = model.output
     x = layers.Flatten()(x)
     x = layers.Dense(1024, activation="relu")(x)
     x = layers.Dropout(0.5)(x)
     x = layers.Dense(1024, activation="relu")(x)
     predictions = layers.Dense(2, activation="softmax")(x)
     model_final = models.Model(input = model.input, output = predictions)
     model_final.compile(loss = "binary_crossentropy", optimizer = optimizers.SGD(lr=0.0001, momentum=0.9), metrics=["accuracy"])
     pmmlObj=KerasToPmml(model_final,model_name="MobileNet",dataSet='image')
     pmmlObj.export(open('mobilenet.pmml','w'),0)
     reconPmmlObj=ny.parse('mobilenet.pmml',True)
     self.assertEqual(os.path.isfile("mobilenet.pmml"),True)
     self.assertEqual(len(model_final.layers), len(reconPmmlObj.DeepNetwork[0].NetworkLayer))
Пример #17
0
    def test_keras_01(self):

        model = applications.MobileNet(weights='imagenet', include_top=False,input_shape = (224, 224,3)) #last layer not included

        activType='sigmoid'
        x = model.output
        x = Flatten()(x)
        x = Dense(1024, activation="relu")(x)
        predictions = Dense(2, activation=activType)(x)
        model_final = Model(inputs =model.input, outputs = predictions,name='predictions')

        cnn_pmml = KerasToPmml(model_final,predictedClasses=['cats','dogs'])

        cnn_pmml.export(open('2classMBNet.pmml', "w"), 0)

        self.assertEqual(os.path.isfile("2classMBNet.pmml"),True)
Пример #18
0
    def __init__(self, outputs=37, output_layer='sigmoid', **kwargs):
        super(MNet, self).__init__()

        sizes = [(128, 128, 3), (4, 4, 1024), 1024, outputs]

        # Load the pre-trained base model
        base = applications.MobileNet(weights='imagenet',
                                      include_top=False,
                                      input_shape=sizes[0])
        # Freeze the layers except the last ones
        # for layer in base.layers[:-4]:
        #     layer.trainable = False

        self.add(base)
        self.add(layers.Flatten())
        self.add(layers.Dense(sizes[-2], activation='relu'))
        self.add(layers.Dense(sizes[-1], activation=output_layer))
Пример #19
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
Пример #20
0
    def create_model(self, image_width, image_height, num_classes):
        model = applications.MobileNet(weights="imagenet",
                                       include_top=False,
                                       input_shape=(image_width, image_height,
                                                    3))

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

        x = Dropout(0.6)(x)

        x = Dense(num_classes)(x)
        x = BatchNormalization()(x)
        x = Activation('softmax')(x)

        predictions = x
        return Model(model.input, predictions)
Пример #21
0
    def test_02_image_classifier_with_base64string_as_input(self):
        model = applications.MobileNet(weights='imagenet',
                                       include_top=False,
                                       input_shape=(224, 224, 3))
        activType = 'sigmoid'
        x = model.output
        x = Flatten()(x)
        x = Dense(1024, activation="relu")(x)
        predictions = Dense(2, activation=activType)(x)
        model_final = Model(inputs=model.input,
                            outputs=predictions,
                            name='predictions')

        cnn_pmml = KerasToPmml(model_final,model_name="MobileNetBase64",description="Demo",\
            copyright="Internal User",dataSet='imageBase64',predictedClasses=['dogs','cats'])
        cnn_pmml.export(open('2classMBNetBase64.pmml', "w"), 0)

        img = image.load_img('nyoka/tests/resizedCat.png')
        img = img_to_array(img)
        img = preprocess_input(img)
        imgtf = np.expand_dims(img, axis=0)

        base64string = "data:float32;base64," + FloatBase64.from_floatArray(
            img.flatten(), 12)
        base64string = base64string.replace("\n", "")
        csvContent = "imageBase64\n\"" + base64string + "\""
        text_file = open("input.csv", "w")
        text_file.write(csvContent)
        text_file.close()

        model_pred = model_final.predict(imgtf)
        model_preds = {'dogs': model_pred[0][0], 'cats': model_pred[0][1]}

        model_name = self.adapa_utility.upload_to_zserver(
            '2classMBNetBase64.pmml')

        predictions, probabilities = self.adapa_utility.score_in_zserver(
            model_name, 'input.csv', 'DN')

        self.assertEqual(
            abs(probabilities['cats'] - model_preds['cats']) < 0.00001, True)
        self.assertEqual(
            abs(probabilities['dogs'] - model_preds['dogs']) < 0.00001, True)
Пример #22
0
def importPreTrainedNet(name):
    """ Imports the model specified by name, with weights trained on imagenet.
    """
    if name == 'VGG16':
        model = applications.VGG16(include_top=False, weights='imagenet')
    elif name == 'MobileNet':
        # MobileNet needs its input shape defined.
        model = applications.MobileNet(include_top=False,
                                       weights='imagenet',
                                       input_shape=(128, 128, 3),
                                       pooling='avg')
    elif name == 'InceptionV3':
        model = applications.InceptionV3(include_top=False,
                                         weights='imagenet',
                                         pooling='avg')
    else:
        print('failed to load model')
        sys.exit()
    print('Using ', name, " as base network.")
    return model
Пример #23
0
    def MobileNet_mod(width,
                      height,
                      numLatitudes,
                      numLongitudes,
                      finalAct="softmax"):
        base_model = applications.MobileNet(alpha=1.0,
                                            depth_multiplier=1,
                                            weights="imagenet",
                                            include_top=False,
                                            input_shape=(width, height, 3))
        x = base_model.output

        x = GlobalAveragePooling2D()(x)
        # x_lo = Flatten()(x)
        # x = GlobalAveragePooling2D()(x)

        # x_la = Dense(16, activation='relu', name='fc1_1')(x)
        # x_la = Dense(512, activation='relu', name='fc2_1')(x_la)
        x_la = Dense(numLatitudes)(x)
        latitudeBranch = Activation(finalAct, name="latitude_output")(x_la)

        # x_lo = Dense(64, activation='relu', name='fc1_2')(x)
        # x_lo = Dense(512, activation='relu', name='fc2_2')(x_lo)
        x_lo = Dense(numLongitudes)(x)
        longitudeBranch = Activation(finalAct, name="longitude_output")(x_lo)

        model = Model(inputs=base_model.input,
                      outputs=[latitudeBranch, longitudeBranch],
                      name='posenet')

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

        for layer in model.layers[:20]:
            layer.trainable = False

        return model
Пример #24
0
def getKeysByValue(dictOfElements, valueToFind):
    listOfKeys = list()
    listOfItems = dictOfElements.items()
    for item  in listOfItems:
        if item[1] == valueToFind:
            listOfKeys.append(item[0])
    return listOfKeys


img_width = 128
nb_train_samples = 4125
nb_validation_samples = 466
batch_size = 1
class_nb = 120

model = applications.MobileNet(weights="imagenet", include_top=False, input_shape=(img_width, img_width, 3))


for layer in model.layers:
    print(layer, layer.trainable)

x = model.output
x = Flatten()(x)
x = Dense(1024, activation="relu")(x)
x = Dropout(0.5)(x)
x = Dense(1024, activation="relu")(x)
predictions = Dense(class_nb, activation="softmax")(x)

# creating the final model
classifier = Model(input=model.input, output=predictions)
print(classifier.summary())
Пример #25
0
# changez le nom à chaque fois svp ↓
experiment_name = "INATURALIST_E25_Mobilenet8142_D8142Relu_D8142Sigmoids_Lr0.3"
tb_callback = TensorBoard("./logs/" + experiment_name, )

print("Model training will start soon")

if K.image_data_format() == 'channels_first':
    input_shape = (3, img_width, img_height)
else:
    input_shape = (img_width, img_height, 3)

# build the VGG16 network
model = applications.MobileNet(weights='imagenet',
                               input_shape=input_shape,
                               classes=8142,
                               include_top=False,
                               alpha=1.0,
                               depth_multiplier=1,
                               dropout=1e-3)
print('Model loaded.')

# build a classifier model to put on top of the convolutional model
top_model = Flatten(input_shape=model.output_shape[1:])(model.output)
top_model = Dense(8142, activation='relu')(top_model)
top_model = Dropout(0.5)(top_model)
top_model = Dense(8142, activation='sigmoid')(top_model)
super_model = Model(model.input, top_model)

# note that it is necessary to start with a fully-trained
# classifier, including the top classifier,
# in order to successfully do fine-tuning
Пример #26
0
def test_mobilenet_pooling():
    model = applications.MobileNet(weights=None,
                                   include_top=False,
                                   pooling='avg')
    assert model.output_shape == (None, 1024)
Пример #27
0
def test_mobilenet_no_top():
    model = applications.MobileNet(weights=None, include_top=False)
    assert model.output_shape == (None, None, None, 1024)
Пример #28
0
def test_mobilenet():
    model = applications.MobileNet(weights=None)
    assert model.output_shape == (None, 1000)
Пример #29
0
valid_datagen = ImageDataGenerator(rescale=1.0 / 255.0)
valid_batches = valid_datagen.flow_from_directory(DATASET_PATH + '/test',
                                                  target_size=IMAGE_SIZE,
                                                  interpolation='bicubic',
                                                  class_mode='categorical',
                                                  shuffle=False,
                                                  batch_size=BATCH_SIZE)
lrelu = lambda x: tensorflow.keras.activations.relu(x, alpha=0.1)

#Now make our first convolutional layer, 32 filters, 3x3, default stride and padding                                                   \

#model = applications.VGG16(weights = "imagenet", include_top=False, input_shape=[150,150,3])
model = applications.MobileNet(weights=None,
                               include_top=False,
                               input_shape=(128, 128, 3),
                               pooling='avg',
                               alpha=.5,
                               depth_multiplier=1,
                               dropout=.2)
# Freeze the layers which you don't want to train. Here I am freezing the first 5 layers.
#for layer in model.layers[:51]:
#layer.trainable = False
print(model.summary())
x = model.output
x = Dense(128, activation='sigmoid')(x)
x = Dropout(.2)(x)
x = Dense(32)(x)
predictions = Dense(2, activation="softmax")(x)

model.load_weights("MobileNet_Gleason_weights.h5", by_name=True)
#Use Adam optimizer (instead of plain SGD), set learning rate to explore.                                                              \
Пример #30
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