def run(grid_search=True):

    if grid_search:

        t0 = time.time()

        model = KerasRegressor(build_fn=ANN_model, verbose=1)
        grid = Grid_Search_Training(model)
        print 'Start Training the model......'

        grid_result = grid.fit(X_train, y_train)
        print("Best R2 Score: %f using %s" %
              (grid_result.best_score_, grid_result.best_params_))

        t1 = time.time()
        t = t1 - t0

        print 'The GirdSearch on ANN took %.2f mins.' % (round(t / 60., 2))
        means = grid_result.cv_results_['mean_test_score']
        stds = grid_result.cv_results_['std_test_score']
        params = grid_result.cv_results_['params']
        for mean, stdev, param in zip(means, stds, params):
            print("%f (%f) with: %r" % (mean, stdev, param))

    else:
        # create a model
        model = ANN_model()

        # serialize model to JSON
        model_json = model.to_json()
        with open("ANN_model.json", "w") as json_file:
            json_file.write(model_json)
        print model.summary()

        checkpointer = ModelCheckpoint(ANN_weights_path,
                                       verbose=1,
                                       save_best_only=True)
        history = model.fit(X_train,
                            y_train,
                            validation_split=0.2,
                            epochs=150,
                            batch_size=10,
                            callbacks=[checkpointer])
        #Summarize History for Loss
        plt.plot(history.history['loss'])
        plt.plot(history.history['val_loss'])
        plt.title('Model loss')
        plt.ylabel('loss')
        plt.xlabel('epoch')
        plt.legend(['train', 'val'], loc='upper left')
        plt.show()
예제 #2
0
model.add(GlobalAveragePooling2D())

model.add(Dense(128, activation='relu'))
model.add(Dropout(0.3))
model.add(BatchNormalization())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.3))
model.add(BatchNormalization())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.3))
model.add(BatchNormalization())

model.add(Dense(30))

# Summarize the model
model.summary()

# ---
# <a id='step6'></a>
#
# ## Compile and Train the Model
#
# After specifying your architecture, you'll need to compile and train the model to detect facial keypoints'

# ### (IMPLEMENTATION) Compile and Train the Model
#
# Use the `compile` [method](https://keras.io/models/sequential/#sequential-model-methods) to configure the learning process.  Experiment with your choice of [optimizer](https://keras.io/optimizers/); you may have some ideas about which will work best (`SGD` vs. `RMSprop`, etc), but take the time to empirically verify your theories.
#
# Use the `fit` [method](https://keras.io/models/sequential/#sequential-model-methods) to train the model.  Break off a validation set by setting `validation_split=0.2`.  Save the returned `History` object in the `history` variable.
#
# Your model is required to attain a validation loss (measured as mean squared error) of at least **XYZ**.  When you have finished training, [save your model](https://keras.io/getting-started/faq/#how-can-i-save-a-keras-model) as an HDF5 file with file path `my_model.h5`.
예제 #3
0
def LPI(dataset='RPI'):
    data_dim = 620
    timesteps = 1
    batch_size = 64
    epochs = 10
    X, labels = get_data(dataset)
    y, encoder = preprocess_labels(labels)

    num_cross_val = 5
    all_performance_lpa = []
    all_performance_rf = []
    all_performance_xgb = []
    all_performance_rse = []
    all_performance_blend1 = []
    all_performance = []

    all_labels = []
    all_prob = {}
    num_classifier = 3
    all_prob[0] = []
    all_prob[1] = []
    for fold in range(num_cross_val):
        train = []
        test = []
        train = np.array(
            [x for i, x in enumerate(X) if i % num_cross_val != fold])
        test = np.array(
            [x for i, x in enumerate(X) if i % num_cross_val == fold])
        train_label = np.array(
            [x for i, x in enumerate(y) if i % num_cross_val != fold])
        test_label = np.array(
            [x for i, x in enumerate(y) if i % num_cross_val == fold])
        train1 = np.reshape(train, (train.shape[0], 1, train.shape[1]))
        test1 = np.reshape(test, (test.shape[0], 1, test.shape[1]))

        real_labels = []
        for val in test_label:
            if val[0] == 1:
                real_labels.append(0)
            else:
                real_labels.append(1)

        train_label_new = []
        for val in train_label:
            if val[0] == 1:
                train_label_new.append(0)
            else:
                train_label_new.append(1)

        blend_train = np.zeros((
            train1.shape[0],
            num_classifier))  # Number of training data x Number of classifiers
        blend_test = np.zeros(
            (test1.shape[0],
             num_classifier))  # Number of testing data x Number of classifiers

        real_labels = []
        for val in test_label:
            if val[0] == 1:
                real_labels.append(0)
            else:
                real_labels.append(1)

        all_labels = all_labels + real_labels
        '''
        prefilter_train1 = xgb.DMatrix( prefilter_train, label=train_label_new)
        evallist  = [(prefilter_train1, 'train')]
        num_round = 10
        clf = xgb.train( plst, prefilter_train1, num_round, evallist )
        prefilter_test1 = xgb.DMatrix( prefilter_test)
        ae_y_pred_prob = clf.predict(prefilter_test1)
        '''
        tmp_aver = [0] * len(real_labels)

        print("Train...")

        svc = OneVsRestClassifier(SVC(kernel="linear",
                                      random_state=123,
                                      probability=True),
                                  n_jobs=-1)  #, C=1
        #svc=SVC(kernel='poly',degree=2,gamma=1,coef0=0)
        rfe = RFE(estimator=svc, n_features_to_select=200, step=1)
        rfe.fit(train, train_label_new)
        train2 = rfe.transform(train)
        test2 = rfe.transform(test)
        train11 = np.reshape(train2, (train2.shape[0], 1, train2.shape[1]))
        test11 = np.reshape(test2, (test2.shape[0], 1, test2.shape[1]))

        class_index = 0
        model = KerasRegressor(build_fn=LSTM_model, epochs=15, verbose=0)
        model.fit(train11, train_label, epochs=15, verbose=2)
        pred_prob = model.predict(test11)[:, 1]
        all_prob[class_index] = all_prob[class_index] + [
            val for val in pred_prob
        ]
        proba = transfer_label_from_prob(pred_prob)
        acc, precision, sensitivity, specificity, MCC = calculate_performace(
            len(real_labels), proba, real_labels)
        fpr_1, tpr_1, auc_thresholds = roc_curve(real_labels, pred_prob)
        auc_score_1 = auc(fpr_1, tpr_1)
        precision1, recall, threshods = precision_recall_curve(
            real_labels, pred_prob)
        aupr_score = auc(recall, precision1)
        print "LPA_DL :", acc, precision, sensitivity, specificity, MCC, auc_score_1, aupr_score
        all_performance_lpa.append([
            acc, precision, sensitivity, specificity, MCC, auc_score_1,
            aupr_score
        ])
        print '---' * 50

        model = Sequential()
        model.add(
            LSTM(64,
                 return_sequences=False,
                 input_shape=(timesteps, data_dim),
                 name='lstm1')
        )  #kernel_regularizer=regularizers.l2(0.0001),# returns a sequence of vectors of dimension 32
        model.add(Dropout(0.25, name='dropout'))
        #model.add(Dense(2, name='full_connect'))
        model.add(
            DropConnect(Dense(2,
                              activation='relu',
                              kernel_regularizer=regularizers.l2(0.0001),
                              bias_regularizer=regularizers.l2(0.0001)),
                        prob=0.25,
                        name='full_connect'))
        model.add(Activation('sigmoid'))
        model.summary()

        print('Compiling the Model...')
        model.compile(loss=huber, optimizer='adam', metrics=['accuracy'])

        es = EarlyStopping(monitor='val_loss',
                           mode='min',
                           verbose=1,
                           patience=5)
        model.fit(train1,
                  train_label,
                  batch_size=batch_size,
                  epochs=epochs,
                  callbacks=[es],
                  shuffle=True,
                  verbose=2)  #validation_split=0.1,
        class_index = class_index + 1
        proba = model.predict_proba(test1)[:, 1]
        tmp_aver = [val1 + val2 / 3 for val1, val2 in zip(proba, tmp_aver)]
        all_prob[class_index] = all_prob[class_index] + [val for val in proba]
        y_pred_xgb = transfer_label_from_prob(proba)

        real_labels = []
        for val in test_label:
            if val[0] == 1:
                real_labels.append(0)
            else:
                real_labels.append(1)

        #pdb.set_trace()
        acc, precision, sensitivity, specificity, MCC = calculate_performace(
            len(real_labels), y_pred_xgb, real_labels)
        fpr_1, tpr_1, auc_thresholds = roc_curve(real_labels, proba)
        auc_score_1 = auc(fpr_1, tpr_1)
        precision1, recall, pr_threshods = precision_recall_curve(
            real_labels, proba)
        aupr_score = auc(recall, precision1)
        print acc, precision, sensitivity, specificity, MCC, auc_score_1, aupr_score
        all_performance.append([
            acc, precision, sensitivity, specificity, MCC, auc_score_1,
            aupr_score
        ])
        print '---' * 50

    print 'mean performance of LPA_DL-FS'
    print np.mean(np.array(all_performance_lpa), axis=0)
    print '---' * 50
    print 'mean performance of LPA_DL'
    print np.mean(np.array(all_performance), axis=0)
    print '---' * 50

    Figure = plt.figure()
    plot_roc_curve(all_labels, all_prob[0], 'LPI_WFS')
    plot_roc_curve(all_labels, all_prob[1], 'LPI_NFS')
    plt.plot([0, 1], [0, 1], 'k--')
    plt.xlim([-0.05, 1.05])
    plt.ylim([0, 1.05])
    plt.xlabel('False Positive Rate')
    plt.ylabel('True Positive Rate')
    plt.title('ROC')
    plt.legend(loc="lower right")
    plt.show()