def parallel_testing(test_image, test_label, lin_svm, std_scaler): gray = io.load_grayscale_image(test_image) kpt, des = feature_extraction.sift(gray) predictions = classification.predict_svm(des, lin_svm, std_scaler=std_scaler) values, counts = np.unique(predictions, return_counts=True) predicted_class = values[np.argmax(counts)] return predicted_class == test_label
def parallel_testing(test_image, test_label, codebook, svm, scaler, pca): gray = io.load_grayscale_image(test_image) kpt, des = feature_extraction.dense(gray) labels = np.array([test_label] * des.shape[0]) ind = np.array([0] * des.shape[0]) vis_word, _ = bovw.visual_words(des, labels, ind, codebook) prediction_prob = classification.predict_svm(vis_word, svm, std_scaler=scaler, pca=pca) predicted_class = lin_svm.classes_[np.argmax(prediction_prob)] return predicted_class == test_label, predicted_class, np.ravel(prediction_prob)
def parallel_testing(test_image, test_label, codebook, svm, scaler, pca): gray = io.load_grayscale_image(test_image) kpt, des = feature_extraction.dense(gray) kpt_pos = np.array([kpt[i].pt for i in range(0, len(kpt))], dtype=np.float64) labels = np.array([test_label] * des.shape[0]) ind = np.array([0] * des.shape[0]) vis_word, _ = bovw.visual_words(des, labels, ind, codebook, spatial_pyramid=True) prediction_prob = classification.predict_svm(vis_word, svm, std_scaler=scaler, pca=pca) predicted_class = svm.classes_[np.argmax(prediction_prob)] return predicted_class == test_label, predicted_class, np.ravel(prediction_prob)
def parallel_testing(test_image, test_label, svm, scaler, gmm): gray = io.load_grayscale_image(test_image) kpt, des = feature_extraction.sift(gray) labels = np.array([test_label] * des.shape[0]) ind = np.array([0] * des.shape[0]) fisher, _ = bovw.fisher_vectors(des, labels, ind, gmm) prediction_prob = classification.predict_svm(fisher, svm, std_scaler=scaler) predicted_class = svm.classes_[np.argmax(prediction_prob)] return predicted_class == test_label, predicted_class, np.ravel(prediction_prob)
def parallel_testing(test_image, test_label, svm, std_scaler, pca): gray = io.load_grayscale_image(test_image) kpt, des = feature_extraction.sift(gray) predictions = classification.predict_svm(des, svm, std_scaler=std_scaler, pca=pca, probability=True) probabilities = np.sum(predictions, axis=0) predicted_class = svm.classes_[np.argmax(probabilities)] return predicted_class == test_label, predicted_class, test_label
def parallel_testing(test_image, test_label, lin_svm, std_scaler, pca): probs = [0, 0, 0, 0, 0, 0, 0, 0] gray = io.load_grayscale_image(test_image) kpt, des = feature_extraction.sift(gray) predictions = classification.predict_svm(des, lin_svm, std_scaler=std_scaler, pca=pca, probability=True) for j in range(0, len(predictions)): probs = probs + predictions[j] predicted_class = lin_svm.classes_[np.argmax(probs)] return predicted_class == test_label, predicted_class, test_label
def parallel_testing(test_image, test_label, svm, scaler, gmm, model, pca): D, _, _ = feature_extraction.compute_CNN_features(None, test_image, None, model) D_pca = pca.transform(D) D_pca = np.float32(D_pca) labels = np.array([test_label] * D.shape[0]) ind = np.array([0] * D.shape[0]) fisher, _ = bovw.fisher_vectors(D_pca, labels, ind, gmm, normalization='l2') prediction_prob = classification.predict_svm(fisher, svm, std_scaler=scaler) # print prediction_prob # print prediction_prob.shape predicted_class = svm.classes_[np.argmax(prediction_prob)] return predicted_class == test_label, predicted_class, np.ravel( prediction_prob)
D, L, degree=p1, coef0=p2) elif kernel == 'rbf': svm, std_scaler, pca = classification.train_rbf_svm( D, L, gamma=p1) elif kernel == 'sigmoid': svm, std_scaler, pca = classification.train_sigmoid_svm( D, L, gamma=p1, coef0=p2) else: svm, std_scaler, pca = classification.train_poly_svm( D, L, degree=p1, coef0=p2) print('Time spend: {:.2f} s'.format(time.time() - temp)) temp = time.time() # Predict labels for all sift descriptors print('Predicting labels for all descriptors...') predicted_labels = classification.predict_svm( D_t, svm, std_scaler=std_scaler, pca=pca) print('Time spend: {:.2f} s'.format(time.time() - temp)) temp = time.time() # Aggregate predictions print( 'Aggregating predictions of descriptors to obtain a single label...' ) num_correct = 0 for i in range(len(test_images_filenames)): predictions_image = predicted_labels[I_t == i] values, counts = np.unique(predictions_image, return_counts=True) predicted_class = values[np.argmax(counts)] if predicted_class == test_labels[i]: num_correct += 1
print('Training the SVM classifier...') lin_svm, std_scaler, pca = classification.train_linear_svm( D, L, model_name='linsvm_sift_30s') print('Time spend: {:.2f} s'.format(time.time() - temp)) temp = time.time() # Read the test set test_images_filenames, test_labels = io.load_test_set() print('Loaded {} test images.'.format(len(test_images_filenames))) # Feature extraction with sift print('Predicting test data...') D, L, I = feature_extraction.seq_sift(test_images_filenames, test_labels) # Predict labels for all sift descriptors predicted_labels = classification.predict_svm(D, lin_svm, std_scaler) # Aggregate predictions num_correct = 0 for i in range(len(test_images_filenames)): predictions_image = predicted_labels[I == i] values, counts = np.unique(predictions_image, return_counts=True) predicted_class = values[np.argmax(counts)] if predicted_class == test_labels[i]: num_correct += 1 print('Time spend: {:.2f} s'.format(time.time() - temp)) temp = time.time() # Compute accuracy accuracy = num_correct * 100.0 / len(test_images_filenames)