{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":6799,"databundleVersionId":4225553,"sourceType":"competition"}],"dockerImageVersionId":30776,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport matplotlib.pyplot as plt\n\n\nimport os\n\n","metadata":{"_uuid":"c80ddf2e-ff20-47e4-903d-1cf5405425dc","_cell_guid":"b565c43e-e003-40cb-b9c6-9d61143b0684","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-09-28T23:35:53.508296Z","iopub.execute_input":"2024-09-28T23:35:53.50927Z","iopub.status.idle":"2024-09-28T23:35:53.514032Z","shell.execute_reply.started":"2024-09-28T23:35:53.509222Z","shell.execute_reply":"2024-09-28T23:35:53.513156Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras.datasets import cifar10\nfrom tensorflow.keras.applications.resnet50 import ResNet50, preprocess_input","metadata":{"execution":{"iopub.status.busy":"2024-09-28T23:35:53.51601Z","iopub.execute_input":"2024-09-28T23:35:53.516518Z","iopub.status.idle":"2024-09-28T23:35:53.526301Z","shell.execute_reply.started":"2024-09-28T23:35:53.516474Z","shell.execute_reply":"2024-09-28T23:35:53.525073Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport os\nimport gc\nimport torch.nn.functional as F\n\nfrom tqdm import tqdm\nfrom torchvision.io import read_image, ImageReadMode\nfrom torchvision.models import *\nfrom torchvision.datasets import ImageFolder\nfrom torchvision import transforms\nfrom torch.utils.data import DataLoader, Dataset","metadata":{"execution":{"iopub.status.busy":"2024-09-28T23:35:53.52748Z","iopub.execute_input":"2024-09-28T23:35:53.528172Z","iopub.status.idle":"2024-09-28T23:35:53.538284Z","shell.execute_reply.started":"2024-09-28T23:35:53.528137Z","shell.execute_reply":"2024-09-28T23:35:53.537435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = 'cuda' if torch.cuda.is_available() else 'cpu'\n# image_dir = \"/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/train/n01440764/\"\n","metadata":{"execution":{"iopub.status.busy":"2024-09-28T23:35:53.540032Z","iopub.execute_input":"2024-09-28T23:35:53.540389Z","iopub.status.idle":"2024-09-28T23:35:53.548262Z","shell.execute_reply.started":"2024-09-28T23:35:53.540349Z","shell.execute_reply":"2024-09-28T23:35:53.547398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device","metadata":{"execution":{"iopub.status.busy":"2024-09-28T23:35:53.549365Z","iopub.execute_input":"2024-09-28T23:35:53.549655Z","iopub.status.idle":"2024-09-28T23:35:53.559574Z","shell.execute_reply.started":"2024-09-28T23:35:53.549599Z","shell.execute_reply":"2024-09-28T23:35:53.558717Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = 'cuda' if torch.cuda.is_available() else 'cpu'\n","metadata":{"execution":{"iopub.status.busy":"2024-09-28T23:35:53.560564Z","iopub.execute_input":"2024-09-28T23:35:53.560874Z","iopub.status.idle":"2024-09-28T23:35:53.567657Z","shell.execute_reply.started":"2024-09-28T23:35:53.560842Z","shell.execute_reply":"2024-09-28T23:35:53.566761Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = tf.keras.applications.ResNet50(weights=\"imagenet\", include_top=False, pooling=\"avg\")\ndef extract_features(image_path):\n    img = tf.keras.preprocessing.image.load_img(image_path, target_size=(224, 224))\n    img_array = tf.keras.preprocessing.image.img_to_array(img)\n    img_array = np.expand_dims(img_array, axis=0)\n    img_array = tf.keras.applications.resnet50.preprocess_input(img_array)\n    features = model.predict(img_array)\n    return features\n\n    ","metadata":{"execution":{"iopub.status.busy":"2024-09-28T23:35:53.568892Z","iopub.execute_input":"2024-09-28T23:35:53.569718Z","iopub.status.idle":"2024-09-28T23:35:54.700547Z","shell.execute_reply.started":"2024-09-28T23:35:53.569656Z","shell.execute_reply":"2024-09-28T23:35:54.699498Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_dirs = ['/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/train/n01440764',\n '/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/train/n01443537',\n '/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/train/n01484850',\n '/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/train/n01491361',\n '/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/train/n01494475',\n '/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/train/n01496331',\n '/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/train/n01498041',\n '/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/train/n01514668',\n '/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/train/n01514859',\n '/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/train/n01518878',\n '/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/train/n01530575',\n '/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/train/n01531178',\n '/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/train/n01532829',\n '/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/train/n01534433',\n '/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/train/n01537544',\n '/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/train/n01558993',\n '/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/train/n01560419',\n '/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/train/n01580077',\n '/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/train/n01582220',\n '/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/train/n01592084',\n '/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/train/n01601694',\n '/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/train/n01608432',\n '/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/train/n01614925',\n '/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/train/n01616318',\n '/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/train/n01622779',\n '/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/train/n01629819',\n '/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/train/n01630670',\n '/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/train/n01631663',\n '/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/train/n01632458',\n '/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/train/n01632777',\n '/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/train/n01641577',\n '/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/train/n01644373',\n '/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/train/n01644900',\n '/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/train/n01664065',\n '/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/train/n01665541',\n '/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/train/n01667114',\n '/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/train/n01667778',\n '/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/train/n01669191',\n '/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/train/n01675722',\n '/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/train/n01677366',\n '/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/train/n01682714',\n '/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/train/n01685808',\n '/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/train/n01687978',\n '/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/train/n01688243',\n '/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/train/n01689811',\n '/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/train/n01692333',\n '/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/train/n01693334',\n '/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/train/n01694178',\n '/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/train/n01695060',\n '/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/train/n01697457',\n '/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/train/n01698640',\n '/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/train/n01704323',\n '/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/train/n01728572',\n '/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/train/n01728920',\n '/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/train/n01729322',\n '/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/train/n01729977',\n '/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/train/n01734418',\n '/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/train/n01735189',\n '/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/train/n01737021',\n '/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/train/n01739381']","metadata":{"execution":{"iopub.status.busy":"2024-09-28T23:35:54.703851Z","iopub.execute_input":"2024-09-28T23:35:54.704429Z","iopub.status.idle":"2024-09-28T23:35:54.714122Z","shell.execute_reply.started":"2024-09-28T23:35:54.704382Z","shell.execute_reply":"2024-09-28T23:35:54.713194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from glob import glob\ndataset_image_paths = []\n\n# Loop through each directory and collect image paths\nfor image_dir in image_dirs:\n    # Use glob to find all JPEG images in the current directory\n    dataset_image_paths.extend(glob(os.path.join(image_dir, \"*.JPEG\")))\n# dataset_image_paths = glob(os.path.join(image_dir, \"*.JPEG\"))\n\n","metadata":{"execution":{"iopub.status.busy":"2024-09-28T23:35:54.715436Z","iopub.execute_input":"2024-09-28T23:35:54.716263Z","iopub.status.idle":"2024-09-28T23:35:55.0293Z","shell.execute_reply.started":"2024-09-28T23:35:54.716215Z","shell.execute_reply":"2024-09-28T23:35:55.028534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# Extract features for dataset images\ndataset_features = [extract_features(img_path) for img_path in dataset_image_paths]\n# Assuming dataset_features is already computed and stored\ndataset_features = np.vstack(dataset_features).astype('float32')","metadata":{"execution":{"iopub.status.busy":"2024-09-28T23:35:55.030414Z","iopub.execute_input":"2024-09-28T23:35:55.030724Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.decomposition import PCA\npca = PCA(n_components=128)\ndataset_features = pca.fit_transform(dataset_features)\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import joblib\n\n# Assuming `dataset_features` is your feature matrix\npca = PCA(n_components=128)  # For example, reducing to 128 dimensions\npca.fit(dataset_features)\n\n# Save the PCA model to a file\njoblib.dump(pca, \"pca_model.pkl\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca_fit_dataset_features = dataset_features","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install faiss-cpu\nimport faiss","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"index = faiss.IndexIVFFlat(faiss.IndexFlatL2(dataset_features.shape[1]), 128, 200)  # 128: dimension, 100: number of clusters\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nindex.train(dataset_features)  # Train the index\nindex.add(dataset_features)  # Add dataset features","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Extract features for the new random image\nrandom_image_features = extract_features('/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/train/n01440764/n01440764_10026.JPEG').astype('float32')\n\n# Apply the PCA transformation to reduce the dimensionality to 128 (same as when training the index)\nrandom_image_features_pca = pca.transform(random_image_features)\n\n# Ensure the random image features have the correct shape (1, 128)\nif random_image_features_pca.ndim == 1:\n    random_image_features_pca = np.expand_dims(random_image_features_pca, axis=0)\n\n# Search for top k similar images in the FAISS index\nD, I = index.search(random_image_features_pca, 5)  # D: distances, I: indices\n\n# Get the paths of the top-k similar images\ntop_k_images = [dataset_image_paths[i] for i in I[0]]\n\nprint(top_k_images)\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.image as mpimg\n\n# Function to display images with titles\ndef display_images(image_paths, title_prefix=\"Random Image\"):\n    num_images = len(image_paths)\n    plt.figure(figsize=(15, 5))  # Adjust the figure size based on the number of images\n    \n    for i, image_path in enumerate(image_paths):\n        img = mpimg.imread(image_path)\n        plt.subplot(1, num_images, i + 1)  # Create subplots for each image\n        plt.imshow(img)\n        plt.title(f\"{title_prefix} {i + 1}\")\n        plt.axis('off')  # Hide axes for better presentation\n    \n    plt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display_images(top_k_images, title_prefix=\"Random Image\")\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display_images(['/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/train/n01440764/n01440764_10026.JPEG'],title_prefix='manik')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"faiss.write_index(index, \"faiss_index_2.ivf\")\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}