2023-11-27 17:55:08 -07:00

169 lines
5.0 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [],
"source": [
"from utils import *\n",
"warnings.filterwarnings('ignore')\n",
"%matplotlib inline\n",
"from sklearn.decomposition import PCA"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [],
"source": [
"fd_collection = getCollection(\"team_5_mwdb_phase_2\", \"fd_collection\")"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [],
"source": [
"selected_feature_model = \"fc_fd\"\n",
"\n",
"t = int(input(\"Enter value of t: \"))\n",
"if t < 1:\n",
" raise ValueError(\"t should be a positive integer\")\n",
"\n",
"num_layers = int(input(\"Enter the number of layers: \"))\n",
"if num_layers < 1:\n",
" raise ValueError(\"num_layers should be a positive integer\")\n",
"\n",
"num_hashes_per_layer = int(input(\"Enter the number of hashes per layer: \"))\n",
"if num_hashes_per_layer < 1:\n",
" raise ValueError(\"num_hashes_per_layer should be a positive integer\")"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [],
"source": [
"all_images = list(fd_collection.find())\n",
"all_images = sorted(all_images, key = lambda x: x[\"image_id\"])\n",
"\n",
"odd_image_ids = [img[\"image_id\"] for img in all_images if img[\"image_id\"] % 2 == 0]\n",
"\n",
"even_image_labels = [img[\"true_label\"] for img in all_images if img[\"image_id\"] % 2 == 0]\n",
"odd_image_labels = [img[\"true_label\"] for img in all_images if img[\"image_id\"] % 2 != 0]\n",
"\n",
"feature_vectors = [np.array(img[selected_feature_model]).flatten() for img in all_images]\n",
"\n",
"total_len = len(feature_vectors)\n",
"even_feature_vectors = []\n",
"odd_feature_vectors = []\n",
"\n",
"for i in range(total_len):\n",
" if i % 2 == 0:\n",
" even_feature_vectors.append(feature_vectors[i])\n",
" else:\n",
" odd_feature_vectors.append(feature_vectors[i])\n",
"\n",
"\n",
"even_feature_vectors = np.array(even_feature_vectors)\n",
"odd_feature_vectors = np.array(odd_feature_vectors)\n",
"\n",
"odd_len = odd_feature_vectors.shape[0]\n",
"even_len = even_feature_vectors.shape[0]"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [],
"source": [
"num_vectors = even_feature_vectors.shape[0]\n",
"vector_dimension = even_feature_vectors.shape[1]\n",
"\n",
"# lsh = LSHIndex(num_layers, num_hashes_per_layer, vector_dimension)\n",
"\n",
"# for index, vector in enumerate(even_feature_vectors):\n",
"# lsh.add_vector(vector.tolist(), index * 2)\n",
"\n",
"lsh = LSH(even_feature_vectors, num_layers, num_hashes_per_layer)"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Number of unique images considered: 1842\n",
"Overall number of images considered: 1842 \n",
"\n",
"Input image ID: 8675\n",
"Input image label: 100\n",
"10 images\n",
"Label: 61, ID: 6328\n",
"Label: 80, ID: 7314\n",
"Label: 100, ID: 8630\n",
"Label: 80, ID: 7306\n",
"Label: 99, ID: 8600\n",
"Label: 63, ID: 6482\n",
"Label: 100, ID: 8624\n",
"Label: 99, ID: 8594\n",
"Label: 100, ID: 8626\n",
"Label: 6, ID: 2724\n"
]
}
],
"source": [
"query_image_id = int(input(\"Enter value of query_image_id: \"))\n",
"if query_image_id < 1:\n",
" raise ValueError(\"query_image_id should be a positive integer\")\n",
"\n",
"query_vector = np.array(odd_feature_vectors[(query_image_id // 2)])\n",
"\n",
"similar_images = lsh.find_similar(query_vector, t)\n",
"unique_images_count = lsh.get_unique_images_considered_count()\n",
"overall_images_count = lsh.get_overall_images_considered_count()\n",
"\n",
"\n",
"print(\"Number of unique images considered:\", unique_images_count)\n",
"print(\"Overall number of images considered:\", overall_images_count, \"\\n\")\n",
"\n",
"print(f\"Input image ID: {query_image_id}\")\n",
"print(f\"Input image label: {odd_image_labels[query_image_id // 2]}\")\n",
"print(f\"{len(similar_images)} images\")\n",
"for sim in similar_images:\n",
" print(f\"Label: {even_image_labels[sim]}, ID: {sim * 2}\")"
]
}
],
"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
}