SHARE
TWEET

Untitled

a guest Apr 19th, 2019 100 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import numpy as np
  2.  
  3. """
  4. returns the euclidean distance between vector_one and vector_two
  5. """
  6. def compute_euclidean_distance(vector_one, vector_two):
  7.   return np.linalg.norm(vector_one - vector_two)
  8.  
  9. """
  10. image-image similarity computation
  11. """
  12. def calculate_similarity(features_image1, features_image2):
  13.   return compute_euclidean_distance(np.array(features_image1), np.array(features_image2))
  14.  
  15. """
  16. retains top k similar image links
  17. """
  18. def top_k(graph_list, k):
  19.   reduced_graph_file = open("reduced_graph_file_" + str(k) + ".txt", "a+")
  20.   top_k = sorted(graph_list, key=lambda x:(-x[2], x[1], x[0]))[0:k]
  21.   for iter in top_k:
  22.     reduced_graph_file.write(str(iter[0]) + " " + str(iter[1]) + " " + str(iter[2]) + "\n")
  23.  
  24. """
  25. generate_imgximg_edgelist returns image to image similarity in form of an edge list
  26. """
  27. def generate_imgximg_edgelist(image_list1, image_list2, image_feature_map, k):
  28.   imgximg_edgelist_file = open("entire_graph_file.txt", "w")
  29.   image_id_mapping_file = open("image_id_mapping.pickle", "wb")
  30.   image_id_mapping = {}
  31.  
  32.   for index1 in range(0, len(image_list1)):
  33.     local_img_img_sim_list = []
  34.     for index2 in range(0, len(image_list2)):
  35.       image1 = image_list1[index1]
  36.       image2 = image_list2[index2]
  37.       features_image1 = image_feature_map[image1]
  38.       features_image2 = image_feature_map[image2]
  39.       score = 1 / (1 + calculate_similarity(features_image1, features_image2))
  40.       imgximg_edgelist_file.write(str(image1) + " " + str(image2) + " " + str(score) + "\n")
  41.       local_img_img_sim_list.append((image1, image2, score))
  42.  
  43.     top_k(local_img_img_sim_list, k)
  44.     image_id_mapping[image1] = index1
  45.  
  46.   pickle.dump(["Image_id mapping:", image_id_mapping], image_id_mapping_file)
  47.   image_id_mapping_file.close()
  48.  
  49. """
  50. if similarity is false, adj_matrix represents connectivity between two nodes in the graph
  51. if similarity is true, adj_matrix contains similarity between any two nodes in the graph
  52. """
  53. def create_adj_mat_from_reduced_file(k, similarity=False):
  54.   image_id_mapping_file = open("image_id_mapping.pickle", "rb")
  55.   image_id_mapping = pickle.load(image_id_mapping_file)[1]
  56.  
  57.   graph_file = open("reduced_graph_file_" + str(k) + ".txt", "r")
  58.   edges = graph_file.readlines()
  59.  
  60.   graph_file_len = len(edges)
  61.   size_of_graph = graph_file_len // k
  62.   adj_matrix = []
  63.   image1 = ""
  64.   for line in edges:
  65.     temp = line.split(" ")
  66.     if image1 != image_id_mapping[temp[0]]:
  67.       adj_matrix.append([0]*size_of_graph)
  68.       image1 = image_id_mapping[temp[0]]
  69.     else:
  70.       img2 = image_id_mapping[temp[1]]
  71.       if(similarity):
  72.         adj_matrix[-1][img2] = float(temp[2])
  73.       else:
  74.         adj_matrix[-1][img2] = 1
  75.  
  76.   return adj_matrix
  77.  
  78.  
  79. k = 7
  80. image_feature_map = prepare_dataset(mapping)
  81. image_list = list(image_feature_map.keys())
  82. generate_imgximg_edgelist(image_list, image_list, image_feature_map, k)
  83. graph = create_adj_mat_from_reduced_file(k, True)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top