accuracy = num_correct * 100.0 / len(test_images_filenames) # Show results and timing print('\nACCURACY: {:.2f}'.format(accuracy)) print('\nTOTAL TIME: {:.2f} s'.format(time.time() - start)) classes = lin_svm.classes_ # Create confusion matrix conf = confusion_matrix(test_labels, pred_class, labels=classes) # Create ROC curve and AUC score test_labels_bin = label_binarize(test_labels, classes=classes) fpr = [] tpr = [] roc_auc = [] for i in range(len(classes)): c_fpr, c_tpr, _ = roc_curve(test_labels_bin[:, i], np.array(pred_prob)[:, i]) c_roc_auc = auc(c_fpr, c_tpr) fpr.append(c_fpr) tpr.append(c_tpr) roc_auc.append(c_roc_auc) # Plot plotting.plot_confusion_matrix(conf, classes=classes, normalize=True) plotting.plot_roc_curve(fpr, tpr, roc_auc, classes=classes, title='ROC curve for linear SVM with codebook of {} words'.format(settings.codebook_size) ) print('Done.')
svm = io.load_object(SESSION1['model']) std_scaler = io.load_object(SESSION1['scaler']) pca = io.load_object(SESSION1['pca']) # Feature extraction with sift, prediction with SVM and aggregation to obtain final class print('Predicting test data...') result = joblib.Parallel(n_jobs=SESSION1['n_jobs'], backend='threading')( joblib.delayed(parallel_testing)(test_image, test_label, svm, std_scaler, pca) for test_image, test_label in zip(test_images_filenames, test_labels)) correct_class = [i[0] for i in result] predicted = [i[1] for i in result] expected = [i[2] for i in result] num_correct = np.count_nonzero(correct_class) print('Time spend: {:.2f} s'.format(time.time() - start)) temp = time.time() # Compute accuracy accuracy = num_correct * 100.0 / len(test_images_filenames) # Plot and save normalized confusion matrix conf = metrics.confusion_matrix(expected, predicted, labels=svm.classes_) plot_confusion_matrix(conf, classes=svm.classes_, normalize=True) io.save_object(conf, SESSION1['conf_matrix']) # Show results and timing print('\nACCURACY: {:.2f}'.format(accuracy)) print('\nTOTAL TIME: {:.2f} s'.format(time.time() - start))