コード例 #1
0
def Multimodel(cnn_weights_path=None,
               all_weights_path=None,
               class_num=119,
               cnn_no_vary=False):

    input_layer = Input(shape=(200, 200, 3))
    incptionResnet = InceptionResNetV2(include_top=False,
                                       weights=None,
                                       input_tensor=input_layer,
                                       input_shape=(224, 224, 3))
    xception = Xception(include_top=False,
                        weights=None,
                        input_tensor=input_layer,
                        input_shape=(224, 224, 3))

    if cnn_no_vary:
        for i, layer in enumerate(incptionResnet.layers):
            incptionResnet.layers[i].trainable = False
        for i, layer in enumerate(xception.layers):
            xception.layers[i].trainable = False

    if cnn_weights_path != None:
        incptionResnet.load_weights(cnn_weights_path[0])
        xception.load_weights(cnn_weights_path[1])

    print(incptionResnet.output.shape, xception.output.shape)
    model1 = GlobalMaxPool2D(data_format='channels_last')(
        incptionResnet.output)
    model2 = GlobalMaxPool2D(data_format='channels_last')(xception.output)

    print(model1.shape, model2.shape)
    # 把top1_model和top2_model连接起来
    x = keras.layers.Concatenate(axis=1)([model1, model2])
    # x = keras.layers.Add()([model1, model2])

    # 全连接层
    x = Dense(units=256 * 3, activation="relu")(x)
    x = Dense(units=256, activation="relu")(x)
    # x = Dropout(0.5)(x)
    x = Dense(units=class_num, activation="softmax")(x)

    model = Model(inputs=input_layer, outputs=x)

    # 加载全部的参数
    if all_weights_path:
        model.load_weights(all_weights_path)

    return model
コード例 #2
0
def build_model(input_shapes, n_classes=None):

    #### DEFINING INPUT AND BASE ARCHITECTURE
    # You need to modify the name and shape of the "image_input"
    # according to the preprocessing and name of your
    # initial feature.
    # This feature should to be preprocessed as an "Image", with a
    # custom preprocessing.
    image_shape = (197, 197, 3)
    image_input_name = "name_of_your_image_input_preprocessed"
    image_input = Input(shape=image_shape, name=image_input_name)

    base_model = Xception(include_top=False,
                          weights=None,
                          input_tensor=image_input)

    #### LOADING WEIGHTS OF PRE TRAINED MODEL
    # To leverage this architecture, it is better to use weights
    # computed on a previous training on a large dataset (Imagenet).
    # To do so, you need to download the file containing the weights
    # and load them into your model.
    # You can do it by using the macro "Download pre-trained model"
    # of the "Deep Learning image" plugin (CPU or GPU version depending
    # on your setup) available in the plugin store. For this architecture,
    # you need to select:
    #    "Xception trained on Imagenet"
    # This will download the weights and put them into a managed folder
    folder = dataiku.Folder("name_of_folder_containing_xception_weights")
    weights_path = "xception_imagenet_weights_notop.h5"

    base_model.load_weights(os.path.join(folder.get_path(), weights_path),
                            by_name=True,
                            skip_mismatch=True)

    #### ADDING FULLY CONNECTED CLASSIFICATION LAYER
    x = base_model.layers[-1].output
    x = Flatten()(x)
    predictions = Dense(n_classes, activation="softmax")(x)

    model = Model(input=base_model.input, output=predictions)
    return model
コード例 #3
0
            validation_data=[X_val, y_val_matrix],
            epochs=EPOCHS,
            steps_per_epoch=STEPS_PER_EPOCH,
            verbose=1,
            callbacks=[checkpoint, lr_scheduler, csv_logger])

###################################### end version basic and balance ###################################################

####################################### run on test set ################################################################
## only run for testing by adding parameter 'test' when running script
elif modus == 'test':
    y_test_matrix = to_categorical(y_test, len(labeltonumber))
    if worker == 'single':
        print(model.metrics_names)
        #model.load_weights(save_modeldirectory + '/Xception_genus_pad_version1.1/Xception.109.0.964.hdf5')
        model.load_weights(save_modeldirectory + '/{}'.format(weightfile))
        accuracy = model.evaluate(x=X_test, y=y_test_matrix)
        ## get predicted labels for test set
        y_prob = model.predict(X_test)
        y_pred = y_prob.argmax(axis=-1)

    elif worker == 'parallel':
        print(parallel_model.metrics_names)
        parallel_model.load_weights(save_modeldirectory +
                                    '/{}'.format(weightfile))
        accuracy = parallel_model.evaluate(x=X_test, y=y_test_matrix)
        ## get predicted labels for test set
        y_prob = parallel_model.predict(X_test)
        y_pred = y_prob.argmax(axis=-1)
    print('loss: {}, accuracy: {}'.format(accuracy[0], accuracy[1]))
    ## get precision, recall, f1-score and support for each class predicted on test set
コード例 #4
0
X_train, X_val, y_train, y_val = train_test_split(X_train,
                                                  y_train,
                                                  test_size=0.25,
                                                  random_state=42,
                                                  stratify=y_train)
# print(X_train)
# print(y_train)
# print(X_test)
# print(y_test)

## define the model (preset weights)
print('[INFO] defining model...')

## create the pre-trained base model
base_model = Xception(include_top=True, weights=None, classes=40)
base_model.load_weights(
    'frogsumimodels/Xception_species_distort_TF000/Xception.101.0.952.hdf5')
base_model.layers.pop()
base_model_layers = base_model.output
predictions = Dense(len(labeltonumber),
                    activation='softmax')(base_model_layers)

## create the model to train with the correct number of output
model = Model(inputs=base_model.input, outputs=predictions)

## print a summary of the model
print(model.summary())

## values from Olafenwa and Olafenva - 2018 and
## https://machinelearningmastery.com/evaluate-performance-deep-learning-models-keras/
EPOCHS = 200
print('epochs: {}'.format(EPOCHS))