mirror of
https://github.com/20kaushik02/CSE515_MWDB_Project.git
synced 2025-12-06 10:44:06 +00:00
306 lines
11 KiB
Plaintext
306 lines
11 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 49,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"The autoreload extension is already loaded. To reload it, use:\n",
|
|
" %reload_ext autoreload\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"%load_ext autoreload\n",
|
|
"%autoreload 2"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 50,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"import json\n",
|
|
"import os\n",
|
|
"import numpy as np\n",
|
|
"from utils import *\n",
|
|
"import math\n",
|
|
"import heapq\n",
|
|
"import random"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 51,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"fd_collection = getCollection(\"team_5_mwdb_phase_2\", \"fd_collection\")\n",
|
|
"all_images = fd_collection.find()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 52,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"image_sim-cm_fd-kmeans-10-semantics.json loaded\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"selected_latent_space = valid_latent_spaces[\n",
|
|
" str(input(\"Enter latent space - one of \" + str(list(valid_latent_spaces.keys()))))\n",
|
|
"]\n",
|
|
"\n",
|
|
"selected_feature_model = valid_feature_models[\n",
|
|
" str(input(\"Enter feature model - one of \" + str(list(valid_feature_models.keys()))))\n",
|
|
"]\n",
|
|
"\n",
|
|
"k = int(input(\"Enter value of k: \"))\n",
|
|
"if k < 1:\n",
|
|
" raise ValueError(\"k should be a positive integer\")\n",
|
|
"\n",
|
|
"selected_dim_reduction_method = str(\n",
|
|
" input(\n",
|
|
" \"Enter dimensionality reduction method - one of \"\n",
|
|
" + str(list(valid_dim_reduction_methods.keys()))\n",
|
|
" )\n",
|
|
")\n",
|
|
"\n",
|
|
"image_id = int(input(\"Enter image ID: \"))\n",
|
|
"if image_id < 0 and image_id > 8676 and image_id % 2 != 0:\n",
|
|
" raise ValueError(\"image id should be even number between 0 and 8676\")\n",
|
|
"\n",
|
|
"knum = int(input(\"Enter value of knum: \"))\n",
|
|
"if knum < 1:\n",
|
|
" raise ValueError(\"knum should be a positive integer\")\n",
|
|
"\n",
|
|
"match selected_latent_space:\n",
|
|
" case \"\":\n",
|
|
" if os.path.exists(f\"{selected_feature_model}-{selected_dim_reduction_method}-{k}-semantics.json\"):\n",
|
|
" data = json.load(open(f\"{selected_feature_model}-{selected_dim_reduction_method}-{k}-semantics.json\"))\n",
|
|
" print(f\"{selected_feature_model}-{selected_dim_reduction_method}-{k}-semantics.json loaded\")\n",
|
|
" else:\n",
|
|
" print(f\"{selected_feature_model}-{selected_dim_reduction_method}-{k}-semantics.json does not exist\")\n",
|
|
" case \"cp\":\n",
|
|
" if os.path.exists(f\"{selected_feature_model}-cp-{k}-semantics.json\"):\n",
|
|
" data = json.load(open(f\"{selected_feature_model}-cp-{k}-semantics.json\"))\n",
|
|
" print(f\"{selected_feature_model}-cp-{k}-semantics.json loaded\")\n",
|
|
" else: \n",
|
|
" print(f\"{selected_feature_model}-cp-{k}-semantics.json does not exist\")\n",
|
|
" case _:\n",
|
|
" if os.path.exists(f\"{selected_latent_space}-{selected_feature_model}-{selected_dim_reduction_method}-{k}-semantics.json\"):\n",
|
|
" data = json.load(open(f\"{selected_latent_space}-{selected_feature_model}-{selected_dim_reduction_method}-{k}-semantics.json\"))\n",
|
|
" print(f\"{selected_latent_space}-{selected_feature_model}-{selected_dim_reduction_method}-{k}-semantics.json loaded\")\n",
|
|
" else:\n",
|
|
" print(f\"{selected_latent_space}-{selected_feature_model}-{selected_dim_reduction_method}-{k}-semantics.json does not exist\")\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 53,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"def extract_similarities_ls1_ls4(latent_space, dim_reduction, selected_feature_model, data, image_id):\n",
|
|
"\n",
|
|
" image_fd = np.array(all_images[int(image_id / 2)][selected_feature_model]).flatten()\n",
|
|
"\n",
|
|
" match dim_reduction:\n",
|
|
"\n",
|
|
" case 'svd':\n",
|
|
" U = np.array(data[\"image-semantic\"])\n",
|
|
" S = np.array(data[\"semantics-core\"])\n",
|
|
" if len(S.shape) == 1:\n",
|
|
" S = np.diag(S)\n",
|
|
" V = np.transpose(np.array(data[\"semantic-feature\"]))\n",
|
|
" \n",
|
|
" comparison_feature_space = np.matmul(U, S)\n",
|
|
"\n",
|
|
" if latent_space == \"image_sim\":\n",
|
|
" comparison_vector = comparison_feature_space[int(image_id / 2)]\n",
|
|
" else:\n",
|
|
" comparison_vector = np.matmul(np.matmul(image_fd, V), S)\n",
|
|
" \n",
|
|
" case \"nmf\":\n",
|
|
" H = np.array(data['semantic-feature'])\n",
|
|
" comparison_feature_space = np.array(data['image-semantic'])\n",
|
|
"\n",
|
|
" if latent_space == \"image_sim\":\n",
|
|
" comparison_vector = comparison_feature_space[int(image_id / 2)]\n",
|
|
" else:\n",
|
|
" comparison_vector = np.matmul(image_fd, np.transpose(H))\n",
|
|
"\n",
|
|
" case \"kmeans\":\n",
|
|
" comparison_vector = []\n",
|
|
" comparison_feature_space = np.array(data[\"image-semantic\"])\n",
|
|
" S = np.array(data[\"semantic-feature\"])\n",
|
|
"\n",
|
|
" for centroid in S:\n",
|
|
" if latent_space == \"image_sim\":\n",
|
|
" sim_matrix = np.array(data[\"sim-matrix\"])\n",
|
|
" comparison_vector.append(math.dist(sim_matrix[int(image_id / 2)], centroid))\n",
|
|
" else:\n",
|
|
" comparison_vector.append(math.dist(image_fd, centroid))\n",
|
|
"\n",
|
|
" n = len(comparison_feature_space)\n",
|
|
"\n",
|
|
" distances = []\n",
|
|
" for i in range(n):\n",
|
|
" if (i * 2) != image_id:\n",
|
|
" distances.append({\"image_id\": i, \"label\": all_images[i][\"true_label\"], \"distance\": math.dist(comparison_vector, comparison_feature_space[i])})\n",
|
|
"\n",
|
|
" distances = sorted(distances, key=lambda x: x[\"distance\"], reverse=False)[:knum]\n",
|
|
"\n",
|
|
" for x in distances:\n",
|
|
" print(x)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 54,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"def extract_similarities_ls2(data, image_id):\n",
|
|
"\n",
|
|
" IS = np.array(data[\"image-semantic\"])\n",
|
|
" S = np.array(data[\"semantics-core\"])\n",
|
|
"\n",
|
|
" if len(S.shape) == 1:\n",
|
|
" S = np.diag(S)\n",
|
|
"\n",
|
|
" comparison_feature_space = np.matmul(IS, S)\n",
|
|
" comparison_vector = comparison_feature_space[int(image_id / 2)]\n",
|
|
"\n",
|
|
" distances = []\n",
|
|
"\n",
|
|
" n = len(comparison_feature_space)\n",
|
|
" for i in range(n):\n",
|
|
" if i != (image_id / 2):\n",
|
|
" distances.append({\"image_id\": i * 2, \"distance\": math.dist(comparison_vector, comparison_feature_space[i])})\n",
|
|
" \n",
|
|
" distances = sorted(distances, key=lambda x: x[\"distance\"], reverse=False)[:knum]\n",
|
|
"\n",
|
|
" for x in distances:\n",
|
|
" print(x)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 55,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"def extract_similarities_ls3(dim_reduction, data, image_id):\n",
|
|
"\n",
|
|
" img_label = all_images[int(image_id / 2)][\"true_label\"]\n",
|
|
"\n",
|
|
" match dim_reduction:\n",
|
|
"\n",
|
|
" case 'svd':\n",
|
|
" U = np.array(data[\"image-semantic\"])\n",
|
|
" S = np.array(data[\"semantics-core\"])\n",
|
|
" V = np.transpose(np.array(data[\"semantic-feature\"]))\n",
|
|
"\n",
|
|
" comparison_feature_space = np.matmul(U, S)\n",
|
|
" comparison_vector = comparison_feature_space[img_label]\n",
|
|
" \n",
|
|
" case \"nmf\":\n",
|
|
" comparison_feature_space = np.array(data['image-semantic'])\n",
|
|
" comparison_vector = comparison_feature_space[img_label]\n",
|
|
"\n",
|
|
" case \"kmeans\":\n",
|
|
" comparison_feature_space = np.array(data[\"image-semantic\"])\n",
|
|
" comparison_vector = comparison_feature_space[img_label]\n",
|
|
"\n",
|
|
" n = len(comparison_feature_space)\n",
|
|
" distance = float('inf')\n",
|
|
" most_similar_label = img_label\n",
|
|
" for i in range(n):\n",
|
|
" if i != img_label:\n",
|
|
" temp_distance = math.dist(comparison_vector, comparison_feature_space[i])\n",
|
|
" if distance > temp_distance:\n",
|
|
" distance = temp_distance\n",
|
|
" most_similar_label = i\n",
|
|
"\n",
|
|
" label_images = [x[\"image_id\"] for x in all_images if x[\"true_label\"] == most_similar_label]\n",
|
|
" similar_images = random.sample(label_images, knum)\n",
|
|
"\n",
|
|
" print(f\"Most similar label to {img_label} is {most_similar_label}\")\n",
|
|
" for img in similar_images:\n",
|
|
" print(img)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 56,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"{'image_id': 2457, 'label': 39, 'distance': 5.400083378408386}\n",
|
|
"{'image_id': 2629, 'label': 46, 'distance': 6.360136822031199}\n",
|
|
"{'image_id': 1916, 'label': 23, 'distance': 8.279651870400942}\n",
|
|
"{'image_id': 1975, 'label': 24, 'distance': 9.305370097143731}\n",
|
|
"{'image_id': 3287, 'label': 65, 'distance': 9.696792665660324}\n",
|
|
"{'image_id': 292, 'label': 1, 'distance': 10.198675122162054}\n",
|
|
"{'image_id': 3965, 'label': 90, 'distance': 11.544874878013612}\n",
|
|
"{'image_id': 4018, 'label': 92, 'distance': 12.064116415014514}\n",
|
|
"{'image_id': 4037, 'label': 92, 'distance': 13.383239007317492}\n",
|
|
"{'image_id': 4307, 'label': 99, 'distance': 14.448284626506538}\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"match selected_latent_space:\n",
|
|
"\n",
|
|
" case \"\" | \"image_sim\":\n",
|
|
" \n",
|
|
" extract_similarities_ls1_ls4(selected_latent_space, selected_dim_reduction_method, selected_feature_model, data, image_id)\n",
|
|
"\n",
|
|
" case \"label_sim\":\n",
|
|
"\n",
|
|
" extract_similarities_ls3(selected_dim_reduction_method, data, image_id)\n",
|
|
"\n",
|
|
" case \"cp\":\n",
|
|
"\n",
|
|
" extract_similarities_ls2(data, image_id)\n"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "Python 3",
|
|
"language": "python",
|
|
"name": "python3"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": {
|
|
"name": "ipython",
|
|
"version": 3
|
|
},
|
|
"file_extension": ".py",
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython3",
|
|
"version": "3.11.4"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 2
|
|
}
|