mirror of
https://github.com/20kaushik02/CSE515_MWDB_Project.git
synced 2025-12-06 09:24:07 +00:00
182 lines
5.8 KiB
Plaintext
182 lines
5.8 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"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": 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)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 1,
|
|
"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": 2,
|
|
"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": 3,
|
|
"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": 4,
|
|
"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
|
|
}
|