def main(): """Main function for the Task 8""" k_value = get_input_k() while k_value > 8: print("Please enter a value of k within 8") k_value = get_input_k() folder = get_input_folder() print(global_constants.LINE_SEPARATOR) print("User Inputs summary") print(global_constants.LINE_SEPARATOR) print("k-value: {}\nFolder: {}".format(k_value, folder)) print(global_constants.LINE_SEPARATOR) dim_red = DimensionReduction(None, "NMF", k_value, image_metadata=True, folder_metadata=folder) w, h, model = dim_red.execute() # printing the term weight print_tw(w, h, image_metadata=True) # save to csv filename = "task8" + "_" + str(k_value) CSVReader().save_to_csv(w, h, filename, image_metadata=True) print("Please check the CSV file: output/{}.csv".format(filename))
def main(): """Main function for the task 4""" feature_extraction_model = get_input_feature_extractor_model() dimension_reduction_model = get_input_dimensionality_reduction_model() k_value = get_input_k() label = get_input_image_label() folder = get_input_folder() image_name = get_input_image(folder) m_value = get_input_m() if dimension_reduction_model != "NMF": dist_func = "euclidean" elif feature_extraction_model in ["CM", "LBP"]: dist_func = "nvsc1" else: dist_func = "euclidean" # dist_func = "cosine" # dist_func = "chebyshev" # dist_func = "manhattan" # dist_func = "chi_square" # dist_func = "euclidean" print(global_constants.LINE_SEPARATOR) print("User Inputs summary") print(global_constants.LINE_SEPARATOR) print("Feature Extraction Model: {}\nDimensionality Reduction Model: {}\nk-value: {}\nLabel: {}\nFolder: {}\n" "Image: {}\nm-value: {}".format(feature_extraction_model, dimension_reduction_model, k_value, label, folder, image_name, m_value)) print(global_constants.LINE_SEPARATOR) # Saves the returned model filename = "{0}_{1}_{2}_{3}".format(feature_extraction_model, dimension_reduction_model, label.replace(" ", ''), str(k_value)) model = model_interact.load_model(filename=filename) if not model: print("Please run Task 3 for {}, {}, {} and {}".format(feature_extraction_model, dimension_reduction_model, label, k_value)) sys.exit(1) # Compute the reduced dimensions for the new query image and find m similar images dim_reduction = DimensionReduction(feature_extraction_model, dimension_reduction_model, k_value, label) result = dim_reduction.find_m_similar_images(model, m_value, folder, image_name, dist_func) print(global_constants.LINE_SEPARATOR) print("Similar Images") print(global_constants.LINE_SEPARATOR) for rec in result: print(rec) print(global_constants.LINE_SEPARATOR) title = { "Feature Extraction": feature_extraction_model, "Dimension Reduction": dimension_reduction_model, "k": k_value, "Label": label, "Distance": dist_func } show_images(os.path.abspath(os.path.join(folder, image_name)), result, title)
def main(): query_image = get_input_image("Hands") no_images = get_input_k("t") similar_images, img_vectors, query_image_vector = get_LSH_results( query_image, no_images) # while True: # rerank_results(None) relevancefeedback.relevance_fdbk("DT", query_image, similar_images, img_vectors, query_image_vector) pass
def main(): """Main function for the script""" number_of_tasks = 6 print("Welcome to Phase 3!") choice = get_task_number() if choice == "0": module_name = "phase3.load_csv" elif choice == "1": module_name = "phase3.task1" elif choice == "2": module_name = "p3task2" elif choice == "3": module_name = "phase3.task3" elif choice == "5a": l = get_input_k("L") k = get_input_k("K") comb = get_bool("Combine Models") task5a(l, k, comb) sys.exit(0) elif choice == "5b": query = get_input_image("Hands") top = get_input_k("K") comb = get_bool("Combine Models") task5b(query, top, visualize=True, combine_models=comb) sys.exit(0) elif choice == "6a": module_name = "task6_svm" elif choice == "6b": module_name = "task6_dt" elif choice == "6c": module_name = "task6_ppr" elif choice == "4c": module_name = "phase3.task4_ppr" elif choice == "4b": module_name = "task4_dt" elif choice == "4a": module_name = "task4_svm" else: module_name = "task{}".format(choice) module = importlib.import_module('{0}'.format(module_name)) module.main()
def main(): feature_extraction_model = get_input_feature_extractor_model() dimension_reduction_model = get_input_dimensionality_reduction_model() label = get_input_image_label() k_value = get_input_k() query_folder = get_input_folder() image_name = get_input_image(query_folder) m_value = 1 print(global_constants.LINE_SEPARATOR) print("User Inputs summary") print(global_constants.LINE_SEPARATOR) print( "Feature Extraction Model: {}\nDimensionality Reduction Model: {}\nLabel: {}\nk-value: {}\nQuery Folder: {}" "\nImage: {}".format(feature_extraction_model, dimension_reduction_model, label, k_value, query_folder, image_name, m_value)) print(global_constants.LINE_SEPARATOR) if dimension_reduction_model != "NMF": dist_func = "euclidean" elif feature_extraction_model in ["CM", "LBP"]: dist_func = "nvsc1" else: dist_func = "euclidean" # dist_func = "cosine" # dist_func = "chebyshev" # dist_func = "manhattan" # dist_func = "chi_square" # dist_func = "euclidean" class1_label, class2_label = get_class_labels(label) run_task3(feature_extraction_model, dimension_reduction_model, class1_label, k_value) result1 = run_task4(feature_extraction_model, dimension_reduction_model, query_folder, image_name, dist_func, class1_label, k_value, m_value) class1_score = result1[0]['score'] # print(class1_score) run_task3(feature_extraction_model, dimension_reduction_model, class2_label, k_value) result2 = run_task4(feature_extraction_model, dimension_reduction_model, query_folder, image_name, dist_func, class2_label, k_value, m_value) class2_score = result2[0]['score'] # print(class2_score) final_label = class1_label if class1_score > class2_score else class2_label print(global_constants.LINE_SEPARATOR) print("{} !!!".format(final_label.upper())) print(global_constants.LINE_SEPARATOR)
def main(): feature_extraction_model = task6.feature_extraction_model dimension_reduction_model = task6.dimension_reduction_model k_value_for_ss_similarity = 10 given_k_value = get_input_k() print(global_constants.LINE_SEPARATOR) print("User Inputs summary") print(global_constants.LINE_SEPARATOR) print("k-value: {}".format(given_k_value)) print(global_constants.LINE_SEPARATOR) dim_reduction = DimensionReduction(feature_extraction_model, dimension_reduction_model, k_value_for_ss_similarity) # original feature vectors obj_feat_matrix = dim_reduction.get_object_feature_matrix() # get the img IDs from the database for images in the fit model img_set = pd.DataFrame({"imageId": obj_feat_matrix['imageId']}) # get the metadata for each image with given subject id subject_data = dim_reduction.get_metadata("imageName", list(set(img_set["imageId"].tolist()))) # unique subject IDs in dataset dataset_subject_ids = set((subject_data)["id"]) subject_subject_matrix = [] m_value = len(img_set) starttime = time.time() model = task6.load_model(dim_reduction, feature_extraction_model, dimension_reduction_model, k_value_for_ss_similarity) folder = path.basename(path.dirname(obj_feat_matrix['path'][0])) for i, subjectid in enumerate(dataset_subject_ids): given_subject_images = dim_reduction.get_metadata("id", list([subjectid]))["imageName"].tolist() image_list_for_subject = list(set(given_subject_images).intersection(set(img_set["imageId"].tolist()))) similar_subjects = task6.find_similar_subjects(subjectid, image_list_for_subject, model, img_set, dim_reduction, m_value, folder) subject_subject_matrix.append(np.asarray(list(similar_subjects.values()))) print("\nTime taken to create subject subject matrix: {}\n".format(time.time() - starttime)) # perform nmf on subject_subject_matrix # given_k_value = 1 matrix = pd.DataFrame(data={'imageId': list(dataset_subject_ids), 'featureVector': subject_subject_matrix}) dim_red = DimensionReduction(None, "NMF", given_k_value, subject_subject=True, matrix=matrix) w, h, model = dim_red.execute() # display latent semantics # printing the term weight print_tw(w, h, subject_subject=True) # save to csv filename = "task7" + '_' + str(given_k_value) CSVReader().save_to_csv(w, None, filename, subject_subject=True) print("Please check the CSV file: output/{}.csv".format(filename))
def main(): """Main function for the script""" feature_extraction_model = "HOG" dimension_reduction_model = "PCA" k_value = get_input_k("k") K_value = get_input_k("K") folder = get_input_folder("Folder") dim_k_value = 40 query_images = get_input_image_list(folder) start = time.time() dim_red = DimensionReduction(feature_extraction_model, dimension_reduction_model, dim_k_value, folder_metadata=folder, metadata_collection="labelled") obj_feat = dim_red.get_object_feature_matrix() features_list = np.array(obj_feat['featureVector'].tolist()) images_list = np.array(obj_feat['imageId']) cos_sim = cosine_similarity(features_list) sim_graph = sim_graph_from_sim_max(cos_sim, images_list, k_value) results = ppr(sim_graph, images_list, query_images) results = results[:K_value] print("Top {} images from Personalized page Rank are:".format(K_value)) for r in results: r["path"] = os.path.abspath(os.path.join(folder, r['imageId'])) print(r) query_images_list = [ os.path.abspath(os.path.join(folder, img)) for img in query_images ] title = {"Model": "Personalized Page Rank", "k": k_value, "K": K_value} show_images_ppr(query_images_list, title, results) print("Execution time: {} seconds".format(time.time() - start))
def main(): """Main function for the Task3""" feature_extraction_model = get_input_feature_extractor_model() dimension_reduction_model = get_input_dimensionality_reduction_model() k_value = get_input_k() label = get_input_image_label() print(global_constants.LINE_SEPARATOR) print("User Inputs summary") print(global_constants.LINE_SEPARATOR) print( "Feature Extraction Model: {}\nDimensionality Reduction Model: {}\nk-value: {}" .format(feature_extraction_model, dimension_reduction_model, k_value)) print(global_constants.LINE_SEPARATOR) print(global_constants.LINE_SEPARATOR) print("Saving the metadata to MongoDB") print(global_constants.LINE_SEPARATOR) csv_reader.save_hand_csv_mongo("HandInfo.csv") print(global_constants.LINE_SEPARATOR) # Performs the dimensionality reduction dim_reduction = DimensionReduction(feature_extraction_model, dimension_reduction_model, k_value, label) obj_lat, feat_lat, model = dim_reduction.execute() # Saves the returned model filename = "{0}_{1}_{2}_{3}".format(feature_extraction_model, dimension_reduction_model, label.replace(" ", ''), str(k_value)) model_interact.save_model(model=model, filename=filename) # Printing the term weight pairs data_tw = print_tw(obj_lat, feat_lat) # save term weight pairs to csv filename = "task3_{}_{}_{}_{}".format(feature_extraction_model, dimension_reduction_model, label, k_value) csv_reader.save_to_csv(obj_lat, feat_lat, filename) print("Please check the CSV file: output/{}.csv".format(filename))
def main(): """Main function for the task 1""" feature_extraction_model = get_input_feature_extractor_model() dimension_reduction_model = get_input_dimensionality_reduction_model() k_value = get_input_k() print(global_constants.LINE_SEPARATOR) print("User Inputs summary") print(global_constants.LINE_SEPARATOR) print( "Feature Extraction Model: {}\nDimensionality Reduction Model: {}\nk-value: {}" .format(feature_extraction_model, dimension_reduction_model, k_value)) print(global_constants.LINE_SEPARATOR) # Performs the dimensionality reduction dim_reduction = DimensionReduction(feature_extraction_model, dimension_reduction_model, k_value) obj_lat, feat_lat = save_model(dim_reduction, feature_extraction_model, dimension_reduction_model, k_value) # Print term weight pairs to terminal data_tw = print_tw(obj_lat, feat_lat) # save term weight pairs to csv filename = "task1" + '_' + feature_extraction_model + '_' + dimension_reduction_model + '_' + str( k_value) CSVReader().save_to_csv(obj_lat, feat_lat, filename) print("Please check the CSV file: output/{}.csv".format(filename)) data = dim_reduction.get_object_feature_matrix() title = { "Feature Extraction": feature_extraction_model, "Dimensionality Reduction": dimension_reduction_model, "k": k_value, } if k_value <= 20: print("Generating Visualization ...") show_data_ls(data, data_tw, title) print("Generating Visualization ...") show_feature_ls(data, feat_lat, title)
def main(): query = get_input_image("Hands") no_images = get_input_k("t") get_probability_revelance_feedback(query, no_images)
def main(): query_image = get_input_image("Hands") no_images = get_input_k("t") # query_image = get_input_image() similar_images, img_vectors, query_image_vector = get_LSH_results(query_image, no_images) relevancefeedback.relevance_fdbk("PPR", query_image,similar_images,img_vectors, query_image_vector)
def main(): fea_ext_mod = "HOG" dim_red_mod = "SVD" dist_func = "euclidean" k_value = get_input_k("k-value") training_set = os.path.abspath(get_input_folder("Labelled")) test_set = os.path.abspath(get_input_folder("Classify")) label = "dorsal" obj_lat, feat_lat, model = compute_latent_semantic_for_label( fea_ext_mod, dim_red_mod, label, k_value, training_set) filename = "p3task1_{0}_{1}_{2}_{3}".format(fea_ext_mod, dim_red_mod, label, str(k_value)) csv_reader.save_to_csv(obj_lat, feat_lat, filename) x_train = obj_lat['reducedDimensions'].tolist() red_dim_unlabelled_images = reduced_dimensions_for_unlabelled_folder( fea_ext_mod, dim_red_mod, k_value, label, training_set, test_set) x_test = red_dim_unlabelled_images['reducedDimensions'].tolist() dim_red = DimensionReduction(fea_ext_mod, dim_red_mod, k_value) labelled_aspect = dim_red.get_metadata_collection( "imageName", obj_lat['imageId'].tolist(), "labelled")['aspectOfHand'].tolist() y_train = [i.split(' ')[0] for i in labelled_aspect] label_p = 'palmar' obj_lat_p, feat_lat_p, model_p = compute_latent_semantic_for_label( fea_ext_mod, dim_red_mod, label_p, k_value, training_set) filename = "p3task1_{0}_{1}_{2}_{3}".format(fea_ext_mod, dim_red_mod, label_p, str(k_value)) csv_reader.save_to_csv(obj_lat_p, feat_lat_p, filename) x_train += (obj_lat_p['reducedDimensions'].tolist()) labelled_aspect = dim_red.get_metadata_collection( "imageName", obj_lat_p['imageId'].tolist(), "labelled")['aspectOfHand'].tolist() y_train += ([i.split(' ')[0] for i in labelled_aspect]) zip_train = list(zip(x_train, y_train)) random.shuffle(zip_train) x_train, y_train = zip(*zip_train) unlabelled_aspect = dim_red.get_metadata_collection( "imageName", red_dim_unlabelled_images['imageId'].tolist(), "unlabelled")['aspectOfHand'].tolist() y_test = [i.split(' ')[0] for i in unlabelled_aspect] lr = LogisticRegression(penalty='l2', random_state=np.random.RandomState(42), solver='lbfgs', max_iter=300, multi_class='ovr', class_weight='balanced', n_jobs=-1, l1_ratio=0) lr.fit(x_train, y_train) # y_pred = lr.predict(x_test) predictions = lr.predict(x_test) unlabelled_images = red_dim_unlabelled_images['imageId'].tolist() predicted_labels = list(predictions) actual_labels = list(y_test) print("---------------------------") print(" Results:") print("---------------------------") print(" Accuracy:", lr.score(x_test, y_test)) print("---------------------------") print(" Image ID | Prediction | Actual") for image_id, p, a in zip(unlabelled_images, predicted_labels, actual_labels): print(" " + image_id + " | " + p + " | " + a)
def main(): k = get_input_k("C") training_set = get_input_folder("Labelled") test_set = get_input_folder("Classify") k_value = 30 dim_reduction = DimensionReduction(feature_extraction_model, dimension_reduction_model, k_value) # obj_lat, feat_lat, model = dim_reduction.execute() label = 'dorsal' obj_lat, feat_lat, model = p3task1.compute_latent_semantic_for_label( feature_extraction_model, dimension_reduction_model, label, k_value, training_set) label_p = 'palmar' obj_lat_p, feat_lat_p, model_p = p3task1.compute_latent_semantic_for_label( feature_extraction_model, dimension_reduction_model, label_p, k_value, training_set) red_dim = p3task1.reduced_dimensions_for_unlabelled_folder( feature_extraction_model, dimension_reduction_model, k_value, label, training_set, test_set) #input for project df = obj_lat[['reducedDimensions', 'imageId']] df_p = obj_lat_p[['reducedDimensions', 'imageId']] #inputt for scikit tf = obj_lat['reducedDimensions'] tf_p = obj_lat_p['reducedDimensions'] a = [] a_p = [] for x in tf: a.append(x) for x in tf_p: a_p.append(x) X = df.values Y = df_p.values # k clusters # k=5 # km = KMeans(n_clusters=k, random_state=0, n_init=30, init='k-means++', precompute_distances=True, n_jobs=-1).fit(a) km_p = KMeans(n_clusters=k, random_state=0, n_init=30, init='k-means++', precompute_distances=True, n_jobs=-1).fit(a_p) # print(km.labels_) counter = np.zeros(k) counter_p = np.zeros(k) for k_m in km.labels_: counter[k_m] += 1 # print(counter) for k_m_p in km_p.labels_: counter_p[k_m_p] += 1 # print(counter_p) # d_cluster = km.predict(red_dim['reducedDimensions'].tolist()) p_cluster = km_p.predict(red_dim['reducedDimensions'].tolist()) unlabelled_aspect = dim_reduction.get_metadata_collection( "imageName", red_dim['imageId'].tolist(), "unlabelled")['aspectOfHand'].tolist() y_test = [i.split(' ')[0] for i in unlabelled_aspect] #min max test good = 0 bad = 0 # for ind in range(len(red_dim['reducedDimensions'])): # cc_dorsal = km.cluster_centers_[d_cluster[ind]] # cc_palmar = km_p.cluster_centers_[p_cluster[ind]] # dist_dorsal = np.linalg.norm(red_dim['reducedDimensions'][ind]-cc_dorsal) # dist_palmar = np.linalg.norm(red_dim['reducedDimensions'][ind]-cc_palmar) # if dist_dorsal<dist_palmar: # #print(red_dim['imageId'][ind], label, y_test[ind]) # if y_test[ind] == label: # good +=1 # else: # bad+=1 # else: # #print(red_dim['imageId'][ind], 'palmar', y_test[ind]) # if y_test[ind] == label_p: # good +=1 # else: # bad+=1 # print ("good",good) # print("bad",bad) # km.score() def kmeans_implementation(X): random = np.random.choice(len(X), size=k, replace=False) centroid = {} classes = {} classes2 = {} # for cen in range(k): # for im in range(0,len(X)): # distance=[np.linalg.norm(np.asarray(X[im][0]) - np.asarray(centroid[0])))] for i in range(k): centroid[i] = X[random[i]][0] for iter in range(500): for i in range(k): classes[i] = [] classes2[i] = [] distance = [] for x in X: # print(x[1]) distance = [ np.linalg.norm( np.asarray(x[0]) - np.asarray(centroid[ind])) for ind in range(len(centroid)) ] classification = distance.index(min(distance)) classes[classification].append(x) classes2[classification].append(x[0]) previous = dict(centroid) for classification in classes2: centroid[classification] = np.average(classes2[classification], axis=0) opti = 0 for c in centroid: og_c = previous[c] current = centroid[c] if (np.array_equal(current, og_c)): opti += 1 if (opti == (k)): # print(iter) break return classes, centroid classes, centroid = kmeans_implementation(X) classes_p, centroid_p = kmeans_implementation(Y) #predict loop red_dimension is the query folder def predict_class(red_dim, centroid): query_classes = {} for i in range(k): query_classes[i] = [] for ind in range(len(red_dim['reducedDimensions'])): cluster_distance = [] cluster_distance = [ np.linalg.norm(red_dim['reducedDimensions'][ind] - np.asarray(centroid[q])) for q in range(len(centroid)) ] query_classification = cluster_distance.index( min(cluster_distance)) query_classes[query_classification].append(red_dim['imageId'][ind]) return query_classes query_classes_dorsal = predict_class(red_dim, centroid) query_classes_palmar = predict_class(red_dim, centroid) correct = 0 wrong = 0 def centroid_mean(centroid): res_list = [0] * k_value mean_centroid = [] for i in range(k): res_list = [a + b for a, b in zip(res_list, centroid[i])] for x in res_list: mean_centroid.append(x / k) return mean_centroid mean_centroid_dorsal = centroid_mean(centroid) mean_centroid_palmar = centroid_mean(centroid_p) dorsal_images = [] palmar_images = [] for ind in range(len(red_dim['reducedDimensions'])): image_center_dorsal = 0 image_center_palmar = 0 image_name = red_dim['imageId'][ind] for i in range(k): if (image_name in query_classes_dorsal[i]): image_center_dorsal = i if (image_name in query_classes_palmar[i]): image_center_palmar = i dorsal_distance = np.linalg.norm(red_dim['reducedDimensions'][ind] - centroid[image_center_dorsal]) palmar_distance = np.linalg.norm(red_dim['reducedDimensions'][ind] - centroid_p[image_center_palmar]) if dorsal_distance < palmar_distance: #print(red_dim['imageId'][ind], label, y_test[ind])ยด dorsal_images.append(red_dim['imageId'][ind]) if y_test[ind] == label: correct += 1 else: wrong += 1 else: #print(red_dim['imageId'][ind], 'palmar', y_test[ind]) palmar_images.append(red_dim['imageId'][ind]) if y_test[ind] == label_p: correct += 1 else: wrong += 1 print("correct" + str(correct)) print("wrong" + str(wrong)) print("\nClick here: http://localhost:{0}/result\n".format(port_g)) print("\nClick here: http://localhost:{0}/dorsal\n".format(port_g)) print("\nClick here: http://localhost:{0}/palmar\n".format(port_g)) # APP_ROOT = os.path.dirname(os.path.abspath(__file__)) @app.route('/Dataset2/<filename>') def send_image(filename): return send_from_directory((training_set), filename) @app.route('/test_set/<filename>') def send_image_result(filename): return send_from_directory((test_set), filename) @app.route('/dorsal') def get_gallery(): image_names = [classes, k] return render_template("demo.html", image_names=image_names) @app.route('/palmar') def get_gallery_p(): image_names_p = [classes_p, k] return render_template("demo_p.html", image_names_p=image_names_p) @app.route('/result') def get_gallery_result(): results = [dorsal_images, palmar_images] return render_template("task2.html", results=results) app.run(port=port_g)