mirror of
https://github.com/20kaushik02/CSE515_MWDB_Project.git
synced 2025-12-06 12:44:06 +00:00
resnet softmax, cleaned files
This commit is contained in:
parent
8ebf13d597
commit
e95cbf335d
@ -1,51 +0,0 @@
|
|||||||
import numpy as np
|
|
||||||
|
|
||||||
def svd(matrix, k):
|
|
||||||
# Step 1: Compute the covariance matrix
|
|
||||||
cov_matrix = np.dot(matrix.T, matrix)
|
|
||||||
|
|
||||||
# Step 2: Compute the eigenvalues and eigenvectors of the covariance matrix
|
|
||||||
eigenvalues, eigenvectors = np.linalg.eig(cov_matrix)
|
|
||||||
|
|
||||||
# Step 3: Sort the eigenvalues and corresponding eigenvectors
|
|
||||||
sort_indices = eigenvalues.argsort()[::-1]
|
|
||||||
eigenvalues = eigenvalues[sort_indices]
|
|
||||||
eigenvectors = eigenvectors[:, sort_indices]
|
|
||||||
|
|
||||||
# Step 4: Compute the singular values and the left and right singular vectors
|
|
||||||
singular_values = np.sqrt(eigenvalues)
|
|
||||||
left_singular_vectors = np.dot(matrix, eigenvectors)
|
|
||||||
right_singular_vectors = eigenvectors
|
|
||||||
|
|
||||||
# Step 5: Normalize the singular vectors
|
|
||||||
for i in range(left_singular_vectors.shape[1]):
|
|
||||||
left_singular_vectors[:, i] /= singular_values[i]
|
|
||||||
|
|
||||||
for i in range(right_singular_vectors.shape[1]):
|
|
||||||
right_singular_vectors[:, i] /= singular_values[i]
|
|
||||||
|
|
||||||
# Keep only the top k singular values and their corresponding vectors
|
|
||||||
singular_values = singular_values[:k]
|
|
||||||
left_singular_vectors = left_singular_vectors[:, :k]
|
|
||||||
right_singular_vectors = right_singular_vectors[:, :k]
|
|
||||||
|
|
||||||
return left_singular_vectors, np.diag(singular_values), right_singular_vectors.T
|
|
||||||
|
|
||||||
def nmf(matrix, k, num_iterations = 100):
|
|
||||||
d1, d2 = matrix.shape
|
|
||||||
# Initialize W and H matrices with random non-negative values
|
|
||||||
W = np.random.rand(d1, k)
|
|
||||||
H = np.random.rand(k, d2)
|
|
||||||
|
|
||||||
for iteration in range(num_iterations):
|
|
||||||
# Update H matrix
|
|
||||||
numerator_h = np.dot(W.T, matrix)
|
|
||||||
denominator_h = np.dot(np.dot(W.T, W), H)
|
|
||||||
H *= numerator_h / denominator_h
|
|
||||||
|
|
||||||
# Update W matrix
|
|
||||||
numerator_w = np.dot(matrix, H.T)
|
|
||||||
denominator_w = np.dot(W, np.dot(H, H.T))
|
|
||||||
W *= numerator_w / denominator_w
|
|
||||||
|
|
||||||
return W, H
|
|
||||||
@ -1,198 +0,0 @@
|
|||||||
{
|
|
||||||
"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 <a href='vscode-notebook-cell:/e%3A/Fall%2023/CSE%20515%20-%20Multimedia%20and%20web%20databases/CSE515_MWDB_Project/Phase%202/task5.ipynb#W1sZmlsZQ%3D%3D?line=88'>89</a>\u001b[0m extractKLatentSemantics(k, label_sim_matrix, feature_model, dim_reduction)\n\u001b[0;32m <a href='vscode-notebook-cell:/e%3A/Fall%2023/CSE%20515%20-%20Multimedia%20and%20web%20databases/CSE515_MWDB_Project/Phase%202/task5.ipynb#W1sZmlsZQ%3D%3D?line=92'>93</a>\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---> <a href='vscode-notebook-cell:/e%3A/Fall%2023/CSE%20515%20-%20Multimedia%20and%20web%20databases/CSE515_MWDB_Project/Phase%202/task5.ipynb#W1sZmlsZQ%3D%3D?line=93'>94</a>\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 <a href='vscode-notebook-cell:/e%3A/Fall%2023/CSE%20515%20-%20Multimedia%20and%20web%20databases/CSE515_MWDB_Project/Phase%202/task5.ipynb#W1sZmlsZQ%3D%3D?line=66'>67</a>\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39mmain\u001b[39m():\n\u001b[1;32m---> <a href='vscode-notebook-cell:/e%3A/Fall%2023/CSE%20515%20-%20Multimedia%20and%20web%20databases/CSE515_MWDB_Project/Phase%202/task5.ipynb#W1sZmlsZQ%3D%3D?line=68'>69</a>\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 <a href='vscode-notebook-cell:/e%3A/Fall%2023/CSE%20515%20-%20Multimedia%20and%20web%20databases/CSE515_MWDB_Project/Phase%202/task5.ipynb#W1sZmlsZQ%3D%3D?line=70'>71</a>\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 <a href='vscode-notebook-cell:/e%3A/Fall%2023/CSE%20515%20-%20Multimedia%20and%20web%20databases/CSE515_MWDB_Project/Phase%202/task5.ipynb#W1sZmlsZQ%3D%3D?line=72'>73</a>\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
|
|
||||||
}
|
|
||||||
@ -1,181 +0,0 @@
|
|||||||
{
|
|
||||||
"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
|
|
||||||
}
|
|
||||||
@ -1,64 +0,0 @@
|
|||||||
import json
|
|
||||||
import tensorly as tl
|
|
||||||
import numpy as np
|
|
||||||
from pymongo import MongoClient
|
|
||||||
from phase1_mongodb import *
|
|
||||||
|
|
||||||
client = MongoClient()
|
|
||||||
client = MongoClient(host="localhost", port=27017)
|
|
||||||
|
|
||||||
# Select the database
|
|
||||||
db = client.Multimedia_Web_DBs
|
|
||||||
|
|
||||||
caltechDataset = loadDataset()
|
|
||||||
num_labels = 101
|
|
||||||
# Fetch all documents from the collection and then sort them by "_id"
|
|
||||||
feature_descriptors = list(db.Feature_Descriptors.find({}))
|
|
||||||
feature_descriptors = sorted(list(db.Feature_Descriptors.find({})), key=lambda x: x["_id"], reverse=False)
|
|
||||||
label_ids = [x["label"] for x in feature_descriptors]
|
|
||||||
|
|
||||||
def compute_cp_decomposition(feature_model, rank):
|
|
||||||
|
|
||||||
label_vectors = [(x["label"], x[feature_model]) for x in feature_descriptors if x["_id"] % 2 == 0]
|
|
||||||
|
|
||||||
num_labels = 101
|
|
||||||
tensor_shape = (len(label_vectors), len(feature_descriptors[0][feature_model]), num_labels)
|
|
||||||
tensor = np.zeros(tensor_shape)
|
|
||||||
for id in range(len(label_vectors)):
|
|
||||||
label = label_vectors[id][0]
|
|
||||||
tensor[id, :, label] = label_vectors[id][1]
|
|
||||||
|
|
||||||
weights, factors = tl.decomposition.parafac(tensor, rank=rank, normalize_factors=True)
|
|
||||||
return weights, factors
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# Step 4: Perform CP-decomposition (parafac) to extract latent semantics
|
|
||||||
|
|
||||||
features = ['color_moments', 'hog', 'layer3', 'avgpool', 'fc']
|
|
||||||
|
|
||||||
# User input for feature model to extract
|
|
||||||
print("1: Color moments")
|
|
||||||
print("2: HOG")
|
|
||||||
print("3: Resnet50 Avgpool layer")
|
|
||||||
print("4: Resnet50 Layer 3")
|
|
||||||
print("5: Resnet50 FC layer")
|
|
||||||
feature_model = features[int(input("Select the feature model: ")) - 1]
|
|
||||||
k = int(input("Enter k: "))
|
|
||||||
weights, factors = compute_cp_decomposition(feature_model, k)
|
|
||||||
k_latent_semantics = list(zip(label_ids, factors[0].tolist()))
|
|
||||||
k_latent_semantics_display = sorted(list(zip(label_ids, factors[0].tolist())), key = lambda x: x[1][0], reverse = True)
|
|
||||||
k_latent_semantics_display = [{"_id": item[0], "semantics": item[1]} for item in k_latent_semantics_display]
|
|
||||||
filename = f'{feature_model}-CP-semantics-{k}.json'
|
|
||||||
k_latent_semantics = [{"_id": item[0], "semantics": item[1]} for item in k_latent_semantics]
|
|
||||||
with open(filename, 'w', encoding='utf-8') as f:
|
|
||||||
json.dump(k_latent_semantics, f, ensure_ascii = False)
|
|
||||||
|
|
||||||
print(k_latent_semantics_display)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
main()
|
|
||||||
@ -276,7 +276,7 @@ def resnet_extractor(image):
|
|||||||
|
|
||||||
|
|
||||||
def resnet_output(image):
|
def resnet_output(image):
|
||||||
"""Get image features from ResNet50 (full execution)"""
|
"""Get image features from ResNet50 (full execution) and apply a softmax layer"""
|
||||||
resized_image = (
|
resized_image = (
|
||||||
torch.Tensor(np.array(transforms.Resize((224, 224))(image)).flatten())
|
torch.Tensor(np.array(transforms.Resize((224, 224))(image)).flatten())
|
||||||
.view(1, 3, 224, 224)
|
.view(1, 3, 224, 224)
|
||||||
@ -285,6 +285,7 @@ def resnet_output(image):
|
|||||||
|
|
||||||
with torch.no_grad():
|
with torch.no_grad():
|
||||||
features = model(resized_image)
|
features = model(resized_image)
|
||||||
|
features = torch.nn.Softmax()(features)
|
||||||
|
|
||||||
return features.detach().cpu().tolist()
|
return features.detach().cpu().tolist()
|
||||||
|
|
||||||
|
|||||||
1391
phase2_madhura.ipynb
1391
phase2_madhura.ipynb
File diff suppressed because one or more lines are too long
Loading…
x
Reference in New Issue
Block a user