{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.11","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":91498,"databundleVersionId":11655853,"sourceType":"competition"}],"dockerImageVersionId":31041,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\n\ntrain_path = '/kaggle/input/image-matching-challenge-2025/train'\ntest_path = '/kaggle/input/image-matching-challenge-2025/test'\n\ntrain_datasets = [d for d in os.listdir(train_path) if os.path.isdir(os.path.join(train_path, d))]\ntest_datasets = [d for d in os.listdir(test_path) if os.path.isdir(os.path.join(test_path, d))]\n\nprint(\"Train datasets:\", train_datasets)\nprint(\"Test datasets:\", test_datasets)\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-06-02T21:58:53.54106Z","iopub.execute_input":"2025-06-02T21:58:53.541604Z","iopub.status.idle":"2025-06-02T21:58:53.55342Z","shell.execute_reply.started":"2025-06-02T21:58:53.541578Z","shell.execute_reply":"2025-06-02T21:58:53.552671Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\n\ntrain_labels_path = '/kaggle/input/image-matching-challenge-2025/train_labels.csv'\ntrain_thresholds_path = '/kaggle/input/image-matching-challenge-2025/train_thresholds.csv'\n\ntrain_labels_df = pd.read_csv(train_labels_path)\nthresholds_df = pd.read_csv(train_thresholds_path)\n\nprint(train_labels_df.head())\nprint(thresholds_df.head())\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-02T21:58:56.415543Z","iopub.execute_input":"2025-06-02T21:58:56.416101Z","iopub.status.idle":"2025-06-02T21:58:56.716624Z","shell.execute_reply.started":"2025-06-02T21:58:56.416075Z","shell.execute_reply":"2025-06-02T21:58:56.715922Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_labels_df['image_path'] = train_labels_df.apply(\n    lambda row: os.path.join(train_path, row['dataset'], row['image']),\n    axis=1\n)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-02T21:59:14.471002Z","iopub.execute_input":"2025-06-02T21:59:14.471301Z","iopub.status.idle":"2025-06-02T21:59:14.492358Z","shell.execute_reply.started":"2025-06-02T21:59:14.471275Z","shell.execute_reply":"2025-06-02T21:59:14.491643Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!pip install timm torch torchvision --quiet\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-02T21:59:22.801Z","iopub.execute_input":"2025-06-02T21:59:22.801253Z","iopub.status.idle":"2025-06-02T22:00:35.634116Z","shell.execute_reply.started":"2025-06-02T21:59:22.801231Z","shell.execute_reply":"2025-06-02T22:00:35.633398Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import torch\nimport timm\nfrom torchvision import transforms\nfrom PIL import Image\nfrom tqdm.notebook import tqdm\n\ndevice = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\nprint(f'Using device: {device}')\n\nmodel_name = 'vit_large_patch14_dinov2.lvd142m'\nmodel = timm.create_model(model_name, pretrained=True)\nmodel.eval().to(device)\n\npreprocess = transforms.Compose([\n    transforms.Resize((518, 518)),\n    transforms.CenterCrop(518),\n    transforms.ToTensor(),\n    transforms.Normalize(mean=[0.5]*3, std=[0.5]*3)\n])\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-02T22:00:35.63574Z","iopub.execute_input":"2025-06-02T22:00:35.635954Z","iopub.status.idle":"2025-06-02T22:00:57.985897Z","shell.execute_reply.started":"2025-06-02T22:00:35.635933Z","shell.execute_reply":"2025-06-02T22:00:57.983406Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def extract_embeddings(image_paths, batch_size=32):\n    embeddings = []\n    for i in tqdm(range(0, len(image_paths), batch_size)):\n        batch = image_paths[i:i+batch_size]\n        images = [preprocess(Image.open(p).convert('RGB')) for p in batch]\n        images = torch.stack(images).to(device)\n        with torch.no_grad():\n            feats = model.forward_features(images)\n            emb = feats[:, 0, :].cpu().numpy()  # CLS token\n            embeddings.append(emb)\n    return np.concatenate(embeddings)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-02T22:00:57.986771Z","iopub.execute_input":"2025-06-02T22:00:57.987211Z","iopub.status.idle":"2025-06-02T22:00:57.992536Z","shell.execute_reply.started":"2025-06-02T22:00:57.987186Z","shell.execute_reply":"2025-06-02T22:00:57.991822Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_image_paths = train_labels_df['image_path'].tolist()\ntrain_embeddings = extract_embeddings(train_image_paths)\nnp.save('/kaggle/working/train_embeddings.npy', train_embeddings)\nprint(f'Train embeddings shape: {train_embeddings.shape}')\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-02T22:00:57.994176Z","iopub.execute_input":"2025-06-02T22:00:57.994437Z","iopub.status.idle":"2025-06-02T22:14:05.9161Z","shell.execute_reply.started":"2025-06-02T22:00:57.994412Z","shell.execute_reply":"2025-06-02T22:14:05.915147Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_image_paths = []\nfor dataset in test_datasets:\n    dataset_path = os.path.join(test_path, dataset)\n    for file in os.listdir(dataset_path):\n        if file.endswith('.png'):\n            test_image_paths.append(os.path.join(dataset_path, file))\n\nprint(f'Total test images: {len(test_image_paths)}')\n\ntest_embeddings = extract_embeddings(test_image_paths)\nnp.save('/kaggle/working/test_embeddings.npy', test_embeddings)\nprint(f'Test embeddings shape: {test_embeddings.shape}')\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-02T22:14:05.91644Z","iopub.status.idle":"2025-06-02T22:14:05.916658Z","shell.execute_reply.started":"2025-06-02T22:14:05.916545Z","shell.execute_reply":"2025-06-02T22:14:05.916554Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}