From 1bc5dd69ce9614e56b5722a692778bce948635ee Mon Sep 17 00:00:00 2001 From: pranavbrkr Date: Sun, 8 Oct 2023 10:53:54 -0700 Subject: [PATCH] task 5 init --- Phase 2/task5.ipynb | 198 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 198 insertions(+) create mode 100644 Phase 2/task5.ipynb diff --git a/Phase 2/task5.ipynb b/Phase 2/task5.ipynb new file mode 100644 index 0000000..63f1ba0 --- /dev/null +++ b/Phase 2/task5.ipynb @@ -0,0 +1,198 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "import json\n", + "import math\n", + "from pymongo import MongoClient\n", + "import scipy\n", + "import numpy as np\n", + "from sklearn.decomposition import NMF\n", + "from sklearn.discriminant_analysis import LinearDiscriminantAnalysis\n", + "from sklearn.cluster import KMeans" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "ename": "ValueError", + "evalue": "invalid literal for int() with base 10: ''", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32me:\\Fall 23\\CSE 515 - Multimedia and web databases\\CSE515_MWDB_Project\\Phase 2\\task5.ipynb Cell 2\u001b[0m line \u001b[0;36m9\n\u001b[0;32m 89\u001b[0m extractKLatentSemantics(k, label_sim_matrix, feature_model, dim_reduction)\n\u001b[0;32m 93\u001b[0m \u001b[39mif\u001b[39;00m \u001b[39m__name__\u001b[39m \u001b[39m==\u001b[39m \u001b[39m\"\u001b[39m\u001b[39m__main__\u001b[39m\u001b[39m\"\u001b[39m:\n\u001b[1;32m---> 94\u001b[0m main()\n", + "\u001b[1;32me:\\Fall 23\\CSE 515 - Multimedia and web databases\\CSE515_MWDB_Project\\Phase 2\\task5.ipynb Cell 2\u001b[0m line \u001b[0;36m6\n\u001b[0;32m 67\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39mmain\u001b[39m():\n\u001b[1;32m---> 69\u001b[0m k \u001b[39m=\u001b[39m \u001b[39mint\u001b[39;49m(\u001b[39minput\u001b[39;49m(\u001b[39m\"\u001b[39;49m\u001b[39mEnter k: \u001b[39;49m\u001b[39m\"\u001b[39;49m))\n\u001b[0;32m 71\u001b[0m features \u001b[39m=\u001b[39m [\u001b[39m'\u001b[39m\u001b[39mcolor_moments\u001b[39m\u001b[39m'\u001b[39m, \u001b[39m'\u001b[39m\u001b[39mhog\u001b[39m\u001b[39m'\u001b[39m, \u001b[39m'\u001b[39m\u001b[39mlayer3\u001b[39m\u001b[39m'\u001b[39m, \u001b[39m'\u001b[39m\u001b[39mavgpool\u001b[39m\u001b[39m'\u001b[39m, \u001b[39m'\u001b[39m\u001b[39mfc\u001b[39m\u001b[39m'\u001b[39m]\n\u001b[0;32m 73\u001b[0m \u001b[39m# User input for feature model to extract\u001b[39;00m\n", + "\u001b[1;31mValueError\u001b[0m: invalid literal for int() with base 10: ''" + ] + } + ], + "source": [ + "client = MongoClient()\n", + "client = MongoClient(host = \"localhost\", port = 27017)\n", + "\n", + "# Select the database\n", + "db = client.Multimedia_Web_DBs\n", + "\n", + "# Fetch all documents from the collection and then sort them by \"_id\"\n", + "feature_descriptors = list(db.Caltech101_Feature_Descriptors.find({}))\n", + "feature_descriptors = sorted(list(db.Caltech101_Feature_Descriptors.find({})), key=lambda x: x[\"_id\"], reverse=False)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def extractKLatentSemantics(k, image_sim_matrix, feature_model, dim_reduction):\n", + "\n", + " feature_ids = [x[\"_id\"] for x in feature_descriptors if x[\"_id\"] % 2 == 0]\n", + " feature_labels = [x[\"label\"] for x in feature_descriptors if x[\"_id\"] % 2 == 0]\n", + "\n", + " filename = 'ls3.json'\n", + "\n", + " match dim_reduction:\n", + "\n", + " case 1:\n", + " U, S, Vh = scipy.sparse.linalg.svds(np.array(image_sim_matrix), k=k)\n", + " k_latent_semantics = sorted(list(zip(feature_ids, U.tolist())), key = lambda x: x[1][0], reverse = True)\n", + "\n", + " case 2:\n", + " model = NMF(n_components = k, init = 'random', solver = 'cd', alpha_H = 0.01, alpha_W = 0.01, max_iter = 10000)\n", + " min_value = np.min(image_sim_matrix)\n", + " feature_vectors_shifted = image_sim_matrix - min_value\n", + " U = model.fit_transform(np.array(feature_vectors_shifted))\n", + " k_latent_semantics = sorted(list(zip(feature_ids, U.tolist())), key = lambda x: x[1][0], reverse = True)\n", + "\n", + " case 3:\n", + " U = LinearDiscriminantAnalysis(n_components = k).fit_transform(image_sim_matrix, feature_labels)\n", + " k_latent_semantics = sorted(list(zip(feature_ids, U.tolist())), key = lambda x: x[1][0], reverse = True)\n", + "\n", + " case 4:\n", + " kmeans = KMeans(n_clusters = k)\n", + " kmeans.fit(image_sim_matrix)\n", + " U = kmeans.transform(image_sim_matrix)\n", + " k_latent_semantics = sorted(list(zip(feature_ids, U.tolist())), key = lambda x: x[1][0], reverse = True)\n", + " \n", + " k_latent_semantics = [{\"_id\": item[0], \"semantics\": item[1]} for item in k_latent_semantics]\n", + " with open(filename, 'w', encoding='utf-8') as f:\n", + " json.dump(k_latent_semantics, f, ensure_ascii = False)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def findLabelLabelSimMatrix(feature_model):\n", + "\n", + " label_sim_matrix = []\n", + " label_mean_vectors = []\n", + "\n", + " for label in range(101):\n", + " label_vectors = [x[feature_model] for x in feature_descriptors if x[\"label\"] == label and x[\"_id\"] % 2 == 0]\n", + " mean_vector = [sum(col)/len(col) for col in zip(*label_vectors)]\n", + " label_mean_vectors.append(mean_vector)\n", + " \n", + "\n", + " n = len(label_mean_vectors)\n", + "\n", + " label_sim_matrix = np.zeros((n, n))\n", + "\n", + " for i in range(n):\n", + " for j in range(i + 1, n):\n", + "\n", + " match feature_model:\n", + "\n", + " case \"color_moments\":\n", + " label_sim_matrix[i][j] = label_sim_matrix[j][i] = math.dist(label_mean_vectors[i], label_mean_vectors[j])\n", + " \n", + " case \"hog\":\n", + " label_sim_matrix[i][j] = label_sim_matrix[j][i] = (np.dot(label_mean_vectors[i], label_mean_vectors[j]) / (np.linalg.norm(label_mean_vectors[i]) * np.linalg.norm(label_mean_vectors[j])))\n", + "\n", + " case \"avgpool\" | \"layer3\" | \"fc\":\n", + " label_sim_matrix[i][j] = label_sim_matrix[j][i] = scipy.stats.pearsonr(label_mean_vectors[i], label_mean_vectors[j]).statistic\n", + " \n", + " return label_sim_matrix" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def main():\n", + "\n", + " k = int(input(\"Enter k: \"))\n", + "\n", + " features = ['color_moments', 'hog', 'layer3', 'avgpool', 'fc']\n", + "\n", + " # User input for feature model to extract\n", + " print(\"\\n1: Color moments\")\n", + " print(\"2: HOG\")\n", + " print(\"3: Resnet50 Avgpool layer\")\n", + " print(\"4: Resnet50 Layer 3\")\n", + " print(\"5: Resnet50 FC layer\")\n", + " feature_model = features[int(input(\"Select the feature model: \")) - 1]\n", + "\n", + " print(\"\\n1. SVD\")\n", + " print(\"2. NNMF\")\n", + " print(\"3. LDA\")\n", + " print(\"4. k-means\")\n", + " dim_reduction = int(input(\"Select the dimensionality reduction technique: \"))\n", + "\n", + " label_sim_matrix = findLabelLabelSimMatrix(feature_model)\n", + "\n", + " extractKLatentSemantics(k, label_sim_matrix, feature_model, dim_reduction)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "if __name__ == \"__main__\":\n", + " main()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "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 +}