results = {} # SVM ensemble print("\nSVM ENSEMBLE:") from sklearn.svm import SVC model = SVC(verbose=True, max_iter=1e5) params = {"kernel": ["linear", "rbf", "poly", "sigmoid"], "C": [0.1, 1, 10]} ensemble_model = fit_ensemble(members, ensembleX_train, y_train, train_size, model, params) # grid search results ensemble_results = pd.DataFrame.from_dict(ensemble_model.cv_results_) print(ensemble_results) ensemble_results.to_csv("../../results/ensembles_svm.csv") print("Best model score:", ensemble_model.best_score_) print(ensemble_model.best_params_) # predict output using ensemble model yhat_train = ensemble_predict(members, ensemble_model, ensembleX_train, train_size) train_acc = accuracy_score(y_train, yhat_train) print("Train accuracy:", train_acc) yhat_test = ensemble_predict(members, ensemble_model, ensembleX_test, test_size) test_acc = accuracy_score(y_test, yhat_test) print("Test accuracy:", test_acc) results["SVM(E)"] = {"Train": train_acc, "Test": test_acc} # results to data frame df = pd.DataFrame.from_dict(results, orient="index") df = df.reset_index() df = df.rename(columns={ "index": "Model", "Train": "Train Accuracy", "Test": "Test Accuracy"
model_names = ['Xception', 'DenseNet121', 'MobileNetV2'] models = [] for name in model_names: print("Model:", name) m = get_model(name, **config['model_params']) m.load_weights('./models/from-scratch/' + name + '.h5') # m.compile(loss='binary_crossentropy', optimizer=tf.keras.optimizers.Adam(), metrics=['accuracy', tf.keras.metrics.AUC()]) # m.evaluate(test_dataset) models.append(m) # get numpy arrays from tensorflow datasets y_test = tf_dataset_to_numpy(test_dataset) y_val = tf_dataset_to_numpy(val_dataset) # perform ensemble prediction y_probs = ensemble_predict(models, val_dataset) y_pred = (y_probs > 0.5).astype(np.uint8).flatten() # print the ensemble eval metrics get_eval_scores(y_val, y_probs, y_pred) class_names = np.array(['normal', 'tumor']) # Plot non-normalized confusion matrix plot_confusion_matrix(y_val, y_pred, classes=class_names, title='Confusion matrix') plt.show() ######################## print("----------Test----------")