mirror of
https://github.com/20kaushik02/CSE515_MWDB_Project.git
synced 2025-12-06 11:04:07 +00:00
task7 lda
This commit is contained in:
parent
6adc8bcf70
commit
579c68f9fa
@ -2,7 +2,7 @@
|
|||||||
"cells": [
|
"cells": [
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 49,
|
"execution_count": 102,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
@ -21,7 +21,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 50,
|
"execution_count": 103,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
@ -36,7 +36,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 51,
|
"execution_count": 104,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
@ -46,14 +46,14 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 52,
|
"execution_count": 105,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
"name": "stdout",
|
"name": "stdout",
|
||||||
"output_type": "stream",
|
"output_type": "stream",
|
||||||
"text": [
|
"text": [
|
||||||
"image_sim-cm_fd-kmeans-10-semantics.json loaded\n"
|
"label_sim-cm_fd-lda-10-model.joblib loaded\n"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
@ -87,6 +87,15 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"match selected_latent_space:\n",
|
"match selected_latent_space:\n",
|
||||||
" case \"\":\n",
|
" case \"\":\n",
|
||||||
|
" if selected_dim_reduction_method == \"lda\":\n",
|
||||||
|
" if os.path.exists(f\"{selected_feature_model}-{selected_dim_reduction_method}-{k}-model.joblib\") and os.path.exists(f\"{selected_feature_model}-{selected_dim_reduction_method}-{k}-semantics.json\"):\n",
|
||||||
|
" if os.path.exists(f\"{selected_feature_model}-{selected_dim_reduction_method}-{k}-model.joblib\"):\n",
|
||||||
|
" model = load(f\"{selected_feature_model}-{selected_dim_reduction_method}-{k}-model.joblib\")\n",
|
||||||
|
" data = json.load(open(f\"{selected_feature_model}-{selected_dim_reduction_method}-{k}-semantics.json\"))\n",
|
||||||
|
" print(f\"{selected_feature_model}-{selected_dim_reduction_method}-{k}-model.joblib and json loaded\")\n",
|
||||||
|
" else:\n",
|
||||||
|
" print(f\"{selected_feature_model}-{selected_dim_reduction_method}-{k}-model.joblib does not exist\")\n",
|
||||||
|
" else:\n",
|
||||||
" if os.path.exists(f\"{selected_feature_model}-{selected_dim_reduction_method}-{k}-semantics.json\"):\n",
|
" if os.path.exists(f\"{selected_feature_model}-{selected_dim_reduction_method}-{k}-semantics.json\"):\n",
|
||||||
" data = json.load(open(f\"{selected_feature_model}-{selected_dim_reduction_method}-{k}-semantics.json\"))\n",
|
" data = json.load(open(f\"{selected_feature_model}-{selected_dim_reduction_method}-{k}-semantics.json\"))\n",
|
||||||
" print(f\"{selected_feature_model}-{selected_dim_reduction_method}-{k}-semantics.json loaded\")\n",
|
" print(f\"{selected_feature_model}-{selected_dim_reduction_method}-{k}-semantics.json loaded\")\n",
|
||||||
@ -99,6 +108,14 @@
|
|||||||
" else:\n",
|
" else:\n",
|
||||||
" print(f\"{selected_feature_model}-cp-{k}-semantics.json does not exist\")\n",
|
" print(f\"{selected_feature_model}-cp-{k}-semantics.json does not exist\")\n",
|
||||||
" case _:\n",
|
" case _:\n",
|
||||||
|
" if selected_dim_reduction_method == \"lda\":\n",
|
||||||
|
" if os.path.exists(f\"{selected_latent_space}-{selected_feature_model}-{selected_dim_reduction_method}-{k}-model.joblib\") and os.path.exists(f\"{selected_latent_space}-{selected_feature_model}-{selected_dim_reduction_method}-{k}-semantics.json\"):\n",
|
||||||
|
" model = load(f\"{selected_latent_space}-{selected_feature_model}-{selected_dim_reduction_method}-{k}-model.joblib\")\n",
|
||||||
|
" data = json.load(open(f\"{selected_latent_space}-{selected_feature_model}-{selected_dim_reduction_method}-{k}-semantics.json\"))\n",
|
||||||
|
" print(f\"{selected_latent_space}-{selected_feature_model}-{selected_dim_reduction_method}-{k}-model.joblib loaded\")\n",
|
||||||
|
" else:\n",
|
||||||
|
" print(f\"{selected_latent_space}-{selected_feature_model}-{selected_dim_reduction_method}-{k}-model.joblib does not exist\")\n",
|
||||||
|
" else:\n",
|
||||||
" if os.path.exists(f\"{selected_latent_space}-{selected_feature_model}-{selected_dim_reduction_method}-{k}-semantics.json\"):\n",
|
" if os.path.exists(f\"{selected_latent_space}-{selected_feature_model}-{selected_dim_reduction_method}-{k}-semantics.json\"):\n",
|
||||||
" data = json.load(open(f\"{selected_latent_space}-{selected_feature_model}-{selected_dim_reduction_method}-{k}-semantics.json\"))\n",
|
" data = json.load(open(f\"{selected_latent_space}-{selected_feature_model}-{selected_dim_reduction_method}-{k}-semantics.json\"))\n",
|
||||||
" print(f\"{selected_latent_space}-{selected_feature_model}-{selected_dim_reduction_method}-{k}-semantics.json loaded\")\n",
|
" print(f\"{selected_latent_space}-{selected_feature_model}-{selected_dim_reduction_method}-{k}-semantics.json loaded\")\n",
|
||||||
@ -108,7 +125,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 53,
|
"execution_count": 106,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
@ -153,6 +170,19 @@
|
|||||||
" else:\n",
|
" else:\n",
|
||||||
" comparison_vector.append(math.dist(image_fd, centroid))\n",
|
" comparison_vector.append(math.dist(image_fd, centroid))\n",
|
||||||
" \n",
|
" \n",
|
||||||
|
" case \"lda\":\n",
|
||||||
|
" comparison_feature_space = np.array(data[\"image-semantic\"])\n",
|
||||||
|
" if latent_space == \"image_sim\":\n",
|
||||||
|
" comparison_vector = comparison_feature_space[int(image_id / 2)]\n",
|
||||||
|
" else:\n",
|
||||||
|
" fd = np.array(all_images[int(image_id / 2)][selected_feature_model])\n",
|
||||||
|
" min_value = np.min(fd)\n",
|
||||||
|
" feature_vectors_shifted = fd - min_value\n",
|
||||||
|
" comparison_vector = model.transform(feature_vectors_shifted.flatten().reshape(1, -1)).flatten()\n",
|
||||||
|
" print(comparison_feature_space.shape)\n",
|
||||||
|
" print(comparison_vector.shape)\n",
|
||||||
|
" # print(retValue)\n",
|
||||||
|
"\n",
|
||||||
" n = len(comparison_feature_space)\n",
|
" n = len(comparison_feature_space)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" distances = []\n",
|
" distances = []\n",
|
||||||
@ -168,7 +198,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 54,
|
"execution_count": 107,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
@ -198,7 +228,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 55,
|
"execution_count": 108,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
@ -224,6 +254,10 @@
|
|||||||
" comparison_feature_space = np.array(data[\"image-semantic\"])\n",
|
" comparison_feature_space = np.array(data[\"image-semantic\"])\n",
|
||||||
" comparison_vector = comparison_feature_space[img_label]\n",
|
" comparison_vector = comparison_feature_space[img_label]\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
" case \"lda\":\n",
|
||||||
|
" comparison_feature_space = np.array(data[\"image-semantic\"])\n",
|
||||||
|
" comparison_vector = comparison_feature_space[img_label]\n",
|
||||||
|
"\n",
|
||||||
" n = len(comparison_feature_space)\n",
|
" n = len(comparison_feature_space)\n",
|
||||||
" distance = float('inf')\n",
|
" distance = float('inf')\n",
|
||||||
" most_similar_label = img_label\n",
|
" most_similar_label = img_label\n",
|
||||||
@ -244,23 +278,24 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 56,
|
"execution_count": 109,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
"name": "stdout",
|
"name": "stdout",
|
||||||
"output_type": "stream",
|
"output_type": "stream",
|
||||||
"text": [
|
"text": [
|
||||||
"{'image_id': 2457, 'label': 39, 'distance': 5.400083378408386}\n",
|
"Most similar label to 0 is 19\n",
|
||||||
"{'image_id': 2629, 'label': 46, 'distance': 6.360136822031199}\n",
|
"3634\n",
|
||||||
"{'image_id': 1916, 'label': 23, 'distance': 8.279651870400942}\n",
|
"3600\n",
|
||||||
"{'image_id': 1975, 'label': 24, 'distance': 9.305370097143731}\n",
|
"3562\n",
|
||||||
"{'image_id': 3287, 'label': 65, 'distance': 9.696792665660324}\n",
|
"3618\n",
|
||||||
"{'image_id': 292, 'label': 1, 'distance': 10.198675122162054}\n",
|
"3574\n",
|
||||||
"{'image_id': 3965, 'label': 90, 'distance': 11.544874878013612}\n",
|
"3604\n",
|
||||||
"{'image_id': 4018, 'label': 92, 'distance': 12.064116415014514}\n",
|
"3550\n",
|
||||||
"{'image_id': 4037, 'label': 92, 'distance': 13.383239007317492}\n",
|
"3542\n",
|
||||||
"{'image_id': 4307, 'label': 99, 'distance': 14.448284626506538}\n"
|
"3564\n",
|
||||||
|
"3538\n"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
@ -279,6 +314,13 @@
|
|||||||
"\n",
|
"\n",
|
||||||
" extract_similarities_ls2(data, image_id)\n"
|
" extract_similarities_ls2(data, image_id)\n"
|
||||||
]
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"metadata": {
|
"metadata": {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user