diff --git a/Phase 2/task_11.ipynb b/Phase 2/task_11.ipynb index f00272a..42bd050 100644 --- a/Phase 2/task_11.ipynb +++ b/Phase 2/task_11.ipynb @@ -39,53 +39,51 @@ " def create_similarity_graph(\n", " self, n, feature_model, semantic_data=None, dim_reduction_method=None\n", " ):\n", + " import glob\n", + "\n", " if semantic_data is None:\n", - " # Similarity graph from feature models\n", - " image_sim_matrix = find_image_image_similarity(fd_collection, feature_model)\n", + " file_prefix = f\"image_sim-{feature_model}-*.json\"\n", + " if len(glob.glob(file_prefix)) > 0:\n", + " matrix_data = json.load(open(glob.glob(file_prefix)[0]))\n", + " image_sim_matrix = matrix_data[\"sim-matrix\"]\n", + " if self.verbose:\n", + " print(\"Using image-image similarity matrix from semantic data\")\n", + " else:\n", + " # Similarity graph from feature models\n", + " image_sim_matrix = find_image_image_similarity(\n", + " fd_collection, feature_model\n", + " )\n", " if self.verbose:\n", " print(\"Image-image similarity matrix constructed from\", feature_model)\n", " else:\n", " # Similarity graph from image-semantic latent space\n", - " # LS3, LS4\n", - " if \"sim-matrix\" in semantic_data:\n", - " # for now, don't work with LS3\n", - " # TODO: do similar to task 7 and 10\n", - " image_sim_matrix = np.array(semantic_data[\"sim-matrix\"])\n", - " if image_sim_matrix.shape[0] != NUM_IMAGES:\n", - " raise TypeError(\n", - " \"Functionality to construct similarity graph from LS3 not yet done\"\n", - " )\n", - " if self.verbose:\n", - " print(\"Using image-image similarity matrix from semantic data\")\n", - " # LS1, LS2\n", - " else:\n", - " image_semantic = semantic_data[\"image-semantic\"]\n", - " # SVD, CP\n", - " if \"semantics-core\" in semantic_data:\n", - " semantics_core = np.array(semantic_data[\"semantics-core\"])\n", - " if len(semantics_core.shape) == 1:\n", - " semantics_core = np.diag(semantics_core)\n", - " image_semantic = np.matmul(image_semantic, semantics_core)\n", + " image_semantic = semantic_data[\"image-semantic\"]\n", + " # SVD, CP\n", + " if \"semantics-core\" in semantic_data:\n", + " semantics_core = np.array(semantic_data[\"semantics-core\"])\n", + " if len(semantics_core.shape) == 1:\n", + " semantics_core = np.diag(semantics_core)\n", + " image_semantic = np.matmul(image_semantic, semantics_core)\n", "\n", - " image_sim_matrix = np.zeros((NUM_IMAGES, NUM_IMAGES))\n", - " # Calculate half and fill the other\n", - " for i in range(NUM_IMAGES):\n", - " for j in range(i + 1, NUM_IMAGES):\n", - " # Note: lower the value, lower the distance => higher the similarity\n", - " distance_measure = (\n", - " kl_divergence_measure\n", - " if dim_reduction_method == \"lda\"\n", - " else euclidean_distance_measure\n", - " )\n", - " image_sim_matrix[j][i] = distance_measure(\n", - " np.array(image_semantic[i]),\n", - " np.array(image_semantic[j]),\n", - " )\n", - " image_sim_matrix[i][j] = image_sim_matrix[j][i]\n", - " if self.verbose:\n", - " print(\n", - " \"Image-image similarity matrix constructed from given image-semantic\"\n", + " image_sim_matrix = np.zeros((NUM_IMAGES, NUM_IMAGES))\n", + " # Calculate half and fill the other\n", + " for i in range(NUM_IMAGES):\n", + " for j in range(i + 1, NUM_IMAGES):\n", + " # Note: lower the value, lower the distance => higher the similarity\n", + " distance_measure = (\n", + " kl_divergence_measure\n", + " if dim_reduction_method == \"lda\"\n", + " else euclidean_distance_measure\n", " )\n", + " image_sim_matrix[j][i] = distance_measure(\n", + " np.array(image_semantic[i]),\n", + " np.array(image_semantic[j]),\n", + " )\n", + " image_sim_matrix[i][j] = image_sim_matrix[j][i]\n", + " if self.verbose:\n", + " print(\n", + " \"Image-image similarity matrix constructed from given image-semantic\"\n", + " )\n", "\n", " # Create an unweighted directed similarity graph, with no self-loops\n", " self.similarity_graph = []\n", @@ -103,8 +101,6 @@ " def personalized_pagerank(\n", " self, label, m, damping_factor=0.85, max_iter=1000, tol=1e-6\n", " ):\n", - " import time\n", - "\n", " if self.similarity_graph is None:\n", " raise ValueError(\n", " \"Similarity graph not created. Call create_similarity_graph() first.\"\n",