{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import json\n", "from pymongo import MongoClient\n", "from task0a import *\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\n" ] }, { "cell_type": "code", "execution_count": null, "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)\n", "\n", "num_labels = 101" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def extractKLatentSemantics(k, feature_model, dim_reduction):\n", "\n", " feature_vectors = [x[feature_model] 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", " feature_ids = [x[\"_id\"] for x in feature_descriptors if x[\"_id\"] % 2 == 0]\n", "\n", " filename = ''\n", "\n", "\n", " match dim_reduction:\n", "\n", " case 1:\n", " filename = f'{feature_model}-svd-semantics.json'\n", " U, S, Vh = scipy.sparse.linalg.svds(np.array(feature_vectors), 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", " filename = f'{feature_model}-nnmf-semantics.json'\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(feature_vectors)\n", " feature_vectors_shifted = feature_vectors - 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", " filename = f'{feature_model}-lda-semantics.json'\n", " U = LinearDiscriminantAnalysis(n_components = k).fit_transform(feature_vectors, 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", " filename = f'{feature_model}-kmeans-semantics.json'\n", " kmeans = KMeans(n_clusters = k)\n", " kmeans.fit(feature_vectors)\n", " U = kmeans.transform(feature_vectors)\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)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def main():\n", "\n", " # Load dataset\n", "\n", " # User input for Image ID\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", " extractKLatentSemantics(k, 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": { "language_info": { "name": "python" } }, "nbformat": 4, "nbformat_minor": 2 }