{ "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": [], "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": 3, "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 = 'ls4.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": 4, "metadata": {}, "outputs": [], "source": [ "def findImageImageSimMatrix(feature_model):\n", " \n", " feature_vectors = [x[feature_model] for x in feature_descriptors if x[\"_id\"] % 2 == 0]\n", "\n", " n = len(feature_vectors)\n", "\n", " image_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", " image_sim_matrix[i][j] = image_sim_matrix[j][i] = math.dist(feature_vectors[i], feature_vectors[j])\n", " \n", " case \"hog\":\n", " image_sim_matrix[i][j] = image_sim_matrix[j][i] = (np.dot(feature_vectors[i], feature_vectors[j]) / (np.linalg.norm(feature_vectors[i]) * np.linalg.norm(feature_vectors[j])))\n", "\n", " case \"avgpool\" | \"layer3\" | \"fc\":\n", " image_sim_matrix[i][j] = image_sim_matrix[j][i] = scipy.stats.pearsonr(feature_vectors[i], feature_vectors[j]).statistic\n", " \n", " return image_sim_matrix" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "\n", "\n", "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", " image_sim_matrix = findImageImageSimMatrix(feature_model)\n", " print(image_sim_matrix)\n", "\n", " extractKLatentSemantics(k, image_sim_matrix, feature_model, dim_reduction)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "1: Color moments\n", "2: HOG\n", "3: Resnet50 Avgpool layer\n", "4: Resnet50 Layer 3\n", "5: Resnet50 FC layer\n" ] }, { "ename": "ValueError", "evalue": "invalid literal for int() with base 10: 'cm'", "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\\task6.ipynb Cell 6\u001b[0m line \u001b[0;36m2\n\u001b[0;32m 1\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----> 2\u001b[0m main()\n", "\u001b[1;32me:\\Fall 23\\CSE 515 - Multimedia and web databases\\CSE515_MWDB_Project\\Phase 2\\task6.ipynb Cell 6\u001b[0m line \u001b[0;36m1\n\u001b[0;32m 11\u001b[0m \u001b[39mprint\u001b[39m(\u001b[39m\"\u001b[39m\u001b[39m4: Resnet50 Layer 3\u001b[39m\u001b[39m\"\u001b[39m)\n\u001b[0;32m 12\u001b[0m \u001b[39mprint\u001b[39m(\u001b[39m\"\u001b[39m\u001b[39m5: Resnet50 FC layer\u001b[39m\u001b[39m\"\u001b[39m)\n\u001b[1;32m---> 13\u001b[0m feature_model \u001b[39m=\u001b[39m features[\u001b[39mint\u001b[39;49m(\u001b[39minput\u001b[39;49m(\u001b[39m\"\u001b[39;49m\u001b[39mSelect the feature model: \u001b[39;49m\u001b[39m\"\u001b[39;49m)) \u001b[39m-\u001b[39m \u001b[39m1\u001b[39m]\n\u001b[0;32m 15\u001b[0m \u001b[39mprint\u001b[39m(\u001b[39m\"\u001b[39m\u001b[39m\\n\u001b[39;00m\u001b[39m1. SVD\u001b[39m\u001b[39m\"\u001b[39m)\n\u001b[0;32m 16\u001b[0m \u001b[39mprint\u001b[39m(\u001b[39m\"\u001b[39m\u001b[39m2. NNMF\u001b[39m\u001b[39m\"\u001b[39m)\n", "\u001b[1;31mValueError\u001b[0m: invalid literal for int() with base 10: 'cm'" ] } ], "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 }