{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "provenance": [], "collapsed_sections": [ "j1mFIyblvH0-" ], "gpuType": "T4" }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" }, "accelerator": "GPU", "widgets": { "application/vnd.jupyter.widget-state+json": { "7d71deee94c64080badb10c59e3ac49b": { "model_module": "@jupyter-widgets/controls", "model_name": "HBoxModel", "model_module_version": "1.5.0", "state": { "_dom_classes": [], "_model_module": "@jupyter-widgets/controls", "_model_module_version": "1.5.0", "_model_name": "HBoxModel", "_view_count": null, "_view_module": "@jupyter-widgets/controls", "_view_module_version": "1.5.0", "_view_name": "HBoxView", "box_style": "", "children": [ "IPY_MODEL_013d6d22ebe04ebda019d9bf2e7e135d", "IPY_MODEL_dd04535976844f3b82bd58020d30acb7", "IPY_MODEL_a02104842c0a4b24adfc413f59d84fc0" ], "layout": "IPY_MODEL_94cb76a011a94602b4192f8e6e7142a4" } }, "013d6d22ebe04ebda019d9bf2e7e135d": { "model_module": "@jupyter-widgets/controls", "model_name": "HTMLModel", "model_module_version": "1.5.0", "state": { "_dom_classes": [], "_model_module": "@jupyter-widgets/controls", "_model_module_version": "1.5.0", "_model_name": "HTMLModel", "_view_count": null, "_view_module": "@jupyter-widgets/controls", "_view_module_version": "1.5.0", "_view_name": "HTMLView", "description": "", "description_tooltip": null, "layout": "IPY_MODEL_80d6e5f3592f440db67765ee24e83ccc", "placeholder": "​", "style": "IPY_MODEL_9b8c527888bd4721a6a4387797fc7ba0", "value": "ensemble_model.pkl: 100%" } }, "dd04535976844f3b82bd58020d30acb7": { "model_module": "@jupyter-widgets/controls", "model_name": "FloatProgressModel", "model_module_version": "1.5.0", "state": { "_dom_classes": [], "_model_module": "@jupyter-widgets/controls", "_model_module_version": "1.5.0", "_model_name": "FloatProgressModel", "_view_count": null, "_view_module": "@jupyter-widgets/controls", "_view_module_version": "1.5.0", "_view_name": "ProgressView", "bar_style": "success", "description": "", "description_tooltip": null, "layout": "IPY_MODEL_ed88c4a913d849a1a8222668aca3b1a0", "max": 278892144, "min": 0, "orientation": "horizontal", "style": "IPY_MODEL_52921704988a4cd49196eaab6a858268", "value": 278892144 } }, "a02104842c0a4b24adfc413f59d84fc0": { "model_module": "@jupyter-widgets/controls", "model_name": "HTMLModel", "model_module_version": "1.5.0", "state": { "_dom_classes": [], "_model_module": "@jupyter-widgets/controls", "_model_module_version": "1.5.0", "_model_name": "HTMLModel", "_view_count": null, "_view_module": "@jupyter-widgets/controls", "_view_module_version": "1.5.0", "_view_name": "HTMLView", "description": "", "description_tooltip": null, "layout": "IPY_MODEL_256010fb44964c1fabc7d96d5ea4c644", "placeholder": "​", "style": "IPY_MODEL_91b2880b6eea4ba7b6a96a97c3fb8060", "value": " 279M/279M [00:12<00:00, 23.6MB/s]" } }, "94cb76a011a94602b4192f8e6e7142a4": { "model_module": "@jupyter-widgets/base", "model_name": "LayoutModel", "model_module_version": "1.2.0", "state": { "_model_module": "@jupyter-widgets/base", "_model_module_version": "1.2.0", "_model_name": "LayoutModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "1.2.0", "_view_name": "LayoutView", "align_content": null, "align_items": null, "align_self": null, "border": null, "bottom": null, "display": null, "flex": null, "flex_flow": null, "grid_area": null, "grid_auto_columns": null, "grid_auto_flow": null, "grid_auto_rows": null, "grid_column": null, "grid_gap": null, "grid_row": null, "grid_template_areas": null, "grid_template_columns": null, "grid_template_rows": null, "height": null, "justify_content": null, "justify_items": null, "left": null, "margin": null, "max_height": null, "max_width": null, "min_height": null, "min_width": null, "object_fit": null, "object_position": null, "order": null, "overflow": null, "overflow_x": null, "overflow_y": null, "padding": null, "right": null, "top": null, "visibility": null, "width": null } }, "80d6e5f3592f440db67765ee24e83ccc": { "model_module": "@jupyter-widgets/base", "model_name": "LayoutModel", "model_module_version": "1.2.0", "state": { "_model_module": "@jupyter-widgets/base", "_model_module_version": "1.2.0", "_model_name": "LayoutModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "1.2.0", "_view_name": "LayoutView", "align_content": null, "align_items": null, "align_self": null, "border": null, "bottom": null, "display": null, "flex": null, "flex_flow": null, "grid_area": null, "grid_auto_columns": null, "grid_auto_flow": null, "grid_auto_rows": null, "grid_column": null, "grid_gap": null, "grid_row": null, "grid_template_areas": null, "grid_template_columns": null, "grid_template_rows": null, "height": null, "justify_content": null, "justify_items": null, "left": null, "margin": null, "max_height": null, "max_width": null, "min_height": null, "min_width": null, "object_fit": null, "object_position": null, "order": null, "overflow": null, "overflow_x": null, "overflow_y": null, "padding": null, "right": null, "top": null, "visibility": null, "width": null } }, "9b8c527888bd4721a6a4387797fc7ba0": { "model_module": "@jupyter-widgets/controls", "model_name": "DescriptionStyleModel", "model_module_version": "1.5.0", "state": { "_model_module": "@jupyter-widgets/controls", "_model_module_version": "1.5.0", "_model_name": "DescriptionStyleModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "1.2.0", "_view_name": "StyleView", "description_width": "" } }, "ed88c4a913d849a1a8222668aca3b1a0": { "model_module": "@jupyter-widgets/base", "model_name": "LayoutModel", "model_module_version": "1.2.0", "state": { "_model_module": "@jupyter-widgets/base", "_model_module_version": "1.2.0", "_model_name": "LayoutModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "1.2.0", "_view_name": "LayoutView", "align_content": null, "align_items": null, "align_self": null, "border": null, "bottom": null, "display": null, "flex": null, "flex_flow": null, "grid_area": null, "grid_auto_columns": null, "grid_auto_flow": null, "grid_auto_rows": null, "grid_column": null, "grid_gap": null, "grid_row": null, "grid_template_areas": null, "grid_template_columns": null, "grid_template_rows": null, "height": null, "justify_content": null, "justify_items": null, "left": null, "margin": null, "max_height": null, "max_width": null, "min_height": null, "min_width": null, "object_fit": null, "object_position": null, "order": null, "overflow": null, "overflow_x": null, "overflow_y": null, "padding": null, "right": null, "top": null, "visibility": null, "width": null } }, "52921704988a4cd49196eaab6a858268": { "model_module": "@jupyter-widgets/controls", "model_name": "ProgressStyleModel", "model_module_version": "1.5.0", "state": { "_model_module": "@jupyter-widgets/controls", "_model_module_version": "1.5.0", "_model_name": "ProgressStyleModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "1.2.0", "_view_name": "StyleView", "bar_color": null, "description_width": "" } }, "256010fb44964c1fabc7d96d5ea4c644": { "model_module": "@jupyter-widgets/base", "model_name": "LayoutModel", "model_module_version": "1.2.0", "state": { "_model_module": "@jupyter-widgets/base", "_model_module_version": "1.2.0", "_model_name": "LayoutModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "1.2.0", "_view_name": "LayoutView", "align_content": null, "align_items": null, "align_self": null, "border": null, "bottom": null, "display": null, "flex": null, "flex_flow": null, "grid_area": null, "grid_auto_columns": null, "grid_auto_flow": null, "grid_auto_rows": null, "grid_column": null, "grid_gap": null, "grid_row": null, "grid_template_areas": null, "grid_template_columns": null, "grid_template_rows": null, "height": null, "justify_content": null, "justify_items": null, "left": null, "margin": null, "max_height": null, "max_width": null, "min_height": null, "min_width": null, "object_fit": null, "object_position": null, "order": null, "overflow": null, "overflow_x": null, "overflow_y": null, "padding": null, "right": null, "top": null, "visibility": null, "width": null } }, "91b2880b6eea4ba7b6a96a97c3fb8060": { "model_module": "@jupyter-widgets/controls", "model_name": "DescriptionStyleModel", "model_module_version": "1.5.0", "state": { "_model_module": "@jupyter-widgets/controls", "_model_module_version": "1.5.0", "_model_name": "DescriptionStyleModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "1.2.0", "_view_name": "StyleView", "description_width": "" } } } } }, "cells": [ { "cell_type": "markdown", "source": [ "# Imports and Classes" ], "metadata": { "id": "j1mFIyblvH0-" } }, { "cell_type": "code", "source": [ "!pip install huggingface-hub\n", "!pip install datasets > delete.txt" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "N0MB1E-_udRx", "outputId": "3b240262-b2e5-43f5-d037-c8459ab5ad1f" }, "execution_count": 14, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Requirement already satisfied: huggingface-hub in /usr/local/lib/python3.10/dist-packages (0.26.3)\n", "Requirement already satisfied: filelock in /usr/local/lib/python3.10/dist-packages (from huggingface-hub) (3.16.1)\n", "Requirement already satisfied: fsspec>=2023.5.0 in /usr/local/lib/python3.10/dist-packages (from huggingface-hub) (2024.10.0)\n", "Requirement already satisfied: packaging>=20.9 in /usr/local/lib/python3.10/dist-packages (from huggingface-hub) (24.2)\n", "Requirement already satisfied: pyyaml>=5.1 in /usr/local/lib/python3.10/dist-packages (from huggingface-hub) (6.0.2)\n", "Requirement already satisfied: requests in /usr/local/lib/python3.10/dist-packages (from huggingface-hub) (2.32.3)\n", "Requirement already satisfied: tqdm>=4.42.1 in /usr/local/lib/python3.10/dist-packages (from huggingface-hub) (4.66.6)\n", "Requirement already satisfied: typing-extensions>=3.7.4.3 in /usr/local/lib/python3.10/dist-packages (from huggingface-hub) (4.12.2)\n", "Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests->huggingface-hub) (3.4.0)\n", "Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests->huggingface-hub) (3.10)\n", "Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests->huggingface-hub) (2.2.3)\n", "Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests->huggingface-hub) (2024.8.30)\n", "\u001b[31mERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.\n", "gcsfs 2024.10.0 requires fsspec==2024.10.0, but you have fsspec 2024.9.0 which is incompatible.\u001b[0m\u001b[31m\n", "\u001b[0m" ] } ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "id": "0MwL3yauuB8m" }, "outputs": [], "source": [ "import torch\n", "import pickle\n", "from huggingface_hub import hf_hub_download\n", "from datasets import load_dataset, Image\n", "import torch\n", "from torch import nn, optim\n", "from torch.utils.data import DataLoader, Dataset\n", "import numpy as np" ] }, { "cell_type": "code", "source": [ "# change runtype to GPU\n", "device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n", "print(device)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "6saYtLslw95c", "outputId": "e7d16fae-e5dd-452e-c80b-ed823258a322" }, "execution_count": 3, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "cuda\n" ] } ] }, { "cell_type": "code", "source": [ "class CNNModel1(nn.Module):\n", " def __init__(self, num_outputs=2):\n", " super(CNNModel1, self).__init__()\n", " self.features = nn.Sequential(\n", " nn.Conv2d(3, 64, kernel_size=11, stride=4, padding=2),\n", " nn.ReLU(inplace=True),\n", " nn.MaxPool2d(kernel_size=3, stride=2),\n", " nn.BatchNorm2d(64),\n", " nn.Conv2d(64, 192, kernel_size=5, padding=2),\n", " nn.ReLU(inplace=True),\n", " nn.MaxPool2d(kernel_size=3, stride=2),\n", " nn.BatchNorm2d(192),\n", " nn.Conv2d(192, 384, kernel_size=3, padding=1),\n", " nn.ReLU(inplace=True),\n", " nn.Conv2d(384, 256, kernel_size=3, padding=1),\n", " nn.ReLU(inplace=True),\n", " nn.Conv2d(256, 256, kernel_size=3, padding=1),\n", " nn.ReLU(inplace=True),\n", " nn.MaxPool2d(kernel_size=3, stride=2)\n", " )\n", " self.classifier = nn.Sequential(\n", " nn.Dropout(),\n", " nn.Linear(256 * 6 * 6, 4096),\n", " nn.ReLU(inplace=True),\n", " nn.Dropout(),\n", " nn.Linear(4096, 4096),\n", " nn.ReLU(inplace=True),\n", " nn.Linear(4096, num_outputs)\n", " )\n", "\n", " def forward(self, x):\n", " x = self.features(x)\n", " x = x.view(x.size(0), -1)\n", " x = self.classifier(x)\n", " return x" ], "metadata": { "id": "CPaE895Quyrt" }, "execution_count": 4, "outputs": [] }, { "cell_type": "code", "source": [ "class ResidualBlock(nn.Module):\n", " def __init__(self, in_channels, out_channels, stride=1, downsample=None):\n", " super(ResidualBlock, self).__init__()\n", " self.conv1 = nn.Conv2d(in_channels, out_channels, kernel_size=3, stride=stride, padding=1)\n", " self.bn1 = nn.BatchNorm2d(out_channels)\n", " self.relu = nn.ReLU(inplace=True)\n", " self.conv2 = nn.Conv2d(out_channels, out_channels, kernel_size=3, padding=1)\n", " self.bn2 = nn.BatchNorm2d(out_channels)\n", " self.downsample = downsample\n", "\n", " def forward(self, x):\n", " identity = x\n", " if self.downsample:\n", " identity = self.downsample(x)\n", " out = self.conv1(x)\n", " out = self.bn1(out)\n", " out = self.relu(out)\n", " out = self.conv2(out)\n", " out = self.bn2(out)\n", " out += identity\n", " out = self.relu(out)\n", " return out\n", "\n", "class CNNModel2(nn.Module):\n", " def __init__(self, num_outputs=2):\n", " super(CNNModel2, self).__init__()\n", " self.in_channels = 64\n", " self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3)\n", " self.bn1 = nn.BatchNorm2d(64)\n", " self.relu = nn.ReLU(inplace=True)\n", " self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)\n", "\n", " self.layer1 = self._make_layer(64, 2, stride=1)\n", " self.layer2 = self._make_layer(128, 2, stride=2)\n", " self.layer3 = self._make_layer(256, 2, stride=2)\n", " self.layer4 = self._make_layer(512, 2, stride=2)\n", "\n", " self.avgpool = nn.AdaptiveAvgPool2d((1, 1))\n", " self.fc = nn.Linear(512, num_outputs)\n", "\n", " def _make_layer(self, out_channels, blocks, stride):\n", " downsample = None\n", " if stride != 1 or self.in_channels != out_channels:\n", " downsample = nn.Sequential(\n", " nn.Conv2d(self.in_channels, out_channels, kernel_size=1, stride=stride),\n", " nn.BatchNorm2d(out_channels)\n", " )\n", " layers = []\n", " layers.append(ResidualBlock(self.in_channels, out_channels, stride, downsample))\n", " self.in_channels = out_channels\n", " for _ in range(1, blocks):\n", " layers.append(ResidualBlock(out_channels, out_channels))\n", " return nn.Sequential(*layers)\n", "\n", " def forward(self, x):\n", " x = self.conv1(x)\n", " x = self.bn1(x)\n", " x = self.relu(x)\n", " x = self.maxpool(x)\n", " x = self.layer1(x)\n", " x = self.layer2(x)\n", " x = self.layer3(x)\n", " x = self.layer4(x)\n", " x = self.avgpool(x)\n", " x = x.view(x.size(0), -1)\n", " x = self.fc(x)\n", " return x" ], "metadata": { "id": "BqqtP1WtuTF0" }, "execution_count": 5, "outputs": [] }, { "cell_type": "code", "source": [ "class InceptionModule(nn.Module):\n", " def __init__(self, in_channels, ch1x1, ch3x3_reduce, ch3x3, ch5x5_reduce, ch5x5, pool_proj):\n", " super(InceptionModule, self).__init__()\n", " self.branch1 = nn.Sequential(\n", " nn.Conv2d(in_channels, ch1x1, kernel_size=1),\n", " nn.ReLU(inplace=True)\n", " )\n", " self.branch2 = nn.Sequential(\n", " nn.Conv2d(in_channels, ch3x3_reduce, kernel_size=1),\n", " nn.ReLU(inplace=True),\n", " nn.Conv2d(ch3x3_reduce, ch3x3, kernel_size=3, padding=1),\n", " nn.ReLU(inplace=True)\n", " )\n", " self.branch3 = nn.Sequential(\n", " nn.Conv2d(in_channels, ch5x5_reduce, kernel_size=1),\n", " nn.ReLU(inplace=True),\n", " nn.Conv2d(ch5x5_reduce, ch5x5, kernel_size=5, padding=2),\n", " nn.ReLU(inplace=True)\n", " )\n", " self.branch4 = nn.Sequential(\n", " nn.MaxPool2d(kernel_size=3, stride=1, padding=1),\n", " nn.Conv2d(in_channels, pool_proj, kernel_size=1),\n", " nn.ReLU(inplace=True)\n", " )\n", "\n", " def forward(self, x):\n", " branch1 = self.branch1(x)\n", " branch2 = self.branch2(x)\n", " branch3 = self.branch3(x)\n", " branch4 = self.branch4(x)\n", " outputs = torch.cat([branch1, branch2, branch3, branch4], 1)\n", " return outputs\n", "\n", "class CNNModel3(nn.Module):\n", " def __init__(self, num_outputs=2):\n", " super(CNNModel3, self).__init__()\n", " self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3)\n", " self.maxpool1 = nn.MaxPool2d(3, stride=2)\n", " self.conv2 = nn.Conv2d(64, 192, kernel_size=3, padding=1)\n", " self.maxpool2 = nn.MaxPool2d(3, stride=2)\n", "\n", " self.inception3a = InceptionModule(192, 64, 96, 128, 16, 32, 32)\n", " self.inception3b = InceptionModule(256, 128, 128, 192, 32, 96, 64)\n", " self.maxpool3 = nn.MaxPool2d(3, stride=2)\n", "\n", " self.inception4a = InceptionModule(480, 192, 96, 208, 16, 48, 64)\n", " self.inception4b = InceptionModule(512, 160, 112, 224, 24, 64, 64)\n", " self.maxpool4 = nn.MaxPool2d(3, stride=2)\n", "\n", " self.avgpool = nn.AdaptiveAvgPool2d((1, 1))\n", " self.dropout = nn.Dropout(0.4)\n", " self.fc = nn.Linear(512, num_outputs)\n", "\n", " def forward(self, x):\n", " x = self.conv1(x)\n", " x = self.maxpool1(x)\n", " x = self.conv2(x)\n", " x = self.maxpool2(x)\n", " x = self.inception3a(x)\n", " x = self.inception3b(x)\n", " x = self.maxpool3(x)\n", " x = self.inception4a(x)\n", " x = self.inception4b(x)\n", " x = self.maxpool4(x)\n", " x = self.avgpool(x)\n", " x = x.view(x.size(0), -1)\n", " x = self.dropout(x)\n", " x = self.fc(x)\n", " return x" ], "metadata": { "id": "LSc0Yyzau5y1" }, "execution_count": 6, "outputs": [] }, { "cell_type": "code", "source": [ "from torch.utils.data import Dataset\n", "class GPSImageDataset(Dataset):\n", " def __init__(self, hf_dataset, transform, lat_mean=None, lat_std=None, lon_mean=None, lon_std=None):\n", " self.hf_dataset = hf_dataset\n", " self.transform = transform\n", "\n", " # Normalize the latitude and longitude\n", " self.latitudes = np.array(hf_dataset['Latitude'])\n", " self.longitudes = np.array(hf_dataset['Longitude'])\n", " self.latitude_mean = lat_mean if lat_mean is not None else self.latitudes.mean()\n", " self.latitude_std = lat_std if lat_std is not None else self.latitudes.std()\n", " self.longitude_mean = lon_mean if lon_mean is not None else self.longitudes.mean()\n", " self.longitude_std = lon_std if lon_std is not None else self.longitudes.std()\n", "\n", " self.normalized_latitudes = (self.latitudes - self.latitude_mean) / self.latitude_std\n", " self.normalized_longitudes = (self.longitudes - self.longitude_mean) / self.longitude_std\n", "\n", " def __len__(self):\n", " return len(self.hf_dataset)\n", "\n", " def __getitem__(self, idx):\n", " image = self.hf_dataset[idx]['image']\n", " latitude = self.normalized_latitudes[idx]\n", " longitude = self.normalized_longitudes[idx]\n", "\n", " if self.transform:\n", " image = self.transform(image)\n", "\n", " return image, torch.tensor([latitude, longitude], dtype=torch.float)" ], "metadata": { "id": "QXwlWCWazwGB" }, "execution_count": 20, "outputs": [] }, { "cell_type": "code", "source": [ "from torchvision import transforms, models\n", "transform = transforms.Compose([\n", " transforms.RandomResizedCrop(224),\n", " transforms.RandomHorizontalFlip(),\n", " transforms.ColorJitter(brightness=0.2, contrast=0.2, saturation=0.2, hue=0.1),\n", " transforms.ToTensor(),\n", " transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])\n", "])\n", "\n", "inference_transform = transforms.Compose([\n", " transforms.Resize((224, 224)),\n", " transforms.ToTensor(),\n", " transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])\n", "])" ], "metadata": { "id": "Jt8ZJtCM0MEl" }, "execution_count": 22, "outputs": [] }, { "cell_type": "markdown", "source": [ "# Loading the Pickle and Running on Publlic Dataset" ], "metadata": { "id": "DMHQWN_qvOwU" } }, { "cell_type": "code", "source": [ "!huggingface-cli login\n", "# use appropiate token" ], "metadata": { "id": "hMz1QFksv-Dt" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "pickle_file_path = hf_hub_download(repo_id= \"CIS-5190-CIA/Ensamble\", filename=\"ensemble_model.pkl\")" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 153, "referenced_widgets": [ "7d71deee94c64080badb10c59e3ac49b", "013d6d22ebe04ebda019d9bf2e7e135d", "dd04535976844f3b82bd58020d30acb7", "a02104842c0a4b24adfc413f59d84fc0", "94cb76a011a94602b4192f8e6e7142a4", "80d6e5f3592f440db67765ee24e83ccc", "9b8c527888bd4721a6a4387797fc7ba0", "ed88c4a913d849a1a8222668aca3b1a0", "52921704988a4cd49196eaab6a858268", "256010fb44964c1fabc7d96d5ea4c644", "91b2880b6eea4ba7b6a96a97c3fb8060" ] }, "id": "U--CT_wUwKzr", "outputId": "8b0c7205-6de6-4794-8da7-0e917874532e" }, "execution_count": 9, "outputs": [ { "output_type": "stream", "name": "stderr", "text": [ "/usr/local/lib/python3.10/dist-packages/huggingface_hub/utils/_auth.py:94: UserWarning: \n", "The secret `HF_TOKEN` does not exist in your Colab secrets.\n", "To authenticate with the Hugging Face Hub, create a token in your settings tab (https://huggingface.co/settings/tokens), set it as secret in your Google Colab and restart your session.\n", "You will be able to reuse this secret in all of your notebooks.\n", "Please note that authentication is recommended but still optional to access public models or datasets.\n", " warnings.warn(\n" ] }, { "output_type": "display_data", "data": { "text/plain": [ "ensemble_model.pkl: 0%| | 0.00/279M [00:00 THIS IS THE ONLY VALUE YOU\n", "## NEED TO UPDATE\n", "\n", "dataset_test = load_dataset(\"gydou/released_img\")" ], "metadata": { "id": "x2gDbdjMx5Pl" }, "execution_count": 32, "outputs": [] }, { "cell_type": "code", "source": [ "latitudes = np.array([item['Latitude'] for item in dataset_test['train']])\n", "longitudes = np.array([item['Longitude'] for item in dataset_test['train']])\n", "\n", "lat_mean = latitudes.mean()\n", "lat_std = latitudes.std()\n", "lon_mean = longitudes.mean()\n", "lon_std = longitudes.std()\n", "\n", "val_dataset = GPSImageDataset(\n", " hf_dataset=dataset_test['train'],\n", " transform=inference_transform,\n", " lat_mean=lat_mean,\n", " lat_std=lat_std,\n", " lon_mean=lon_mean,\n", " lon_std=lon_std\n", ")\n", "\n", "val_dataloader = DataLoader(\n", " val_dataset,\n", " batch_size=32,\n", " shuffle=False,\n", " num_workers=4\n", ")\n", "\n", "predictions = ensemble_predict(models, dataloader = val_dataloader)" ], "metadata": { "id": "Q8rQ-jCHzceg" }, "execution_count": 33, "outputs": [] }, { "cell_type": "code", "source": [ "from geopy.distance import geodesic\n", "def compute_rmse_in_meters(predictions, dataloader, lat_mean, lon_mean, lat_std, lon_std):\n", " total_loss = 0.0\n", " total_samples = 0\n", "\n", " predictions_denorm = predictions.cpu().numpy() * np.array([lat_std, lon_std]) + np.array([lat_mean, lon_mean])\n", " for idx, (_, gps_coords) in enumerate(dataloader):\n", " gps_coords = gps_coords.cpu().numpy()\n", "\n", "\n", " actuals_denorm = gps_coords * np.array([lat_std, lon_std]) + np.array([lat_mean, lon_mean])\n", " batch_preds = predictions_denorm[idx * len(gps_coords):(idx + 1) * len(gps_coords)]\n", " for pred, actual in zip(batch_preds, actuals_denorm):\n", " distance = geodesic((actual[0], actual[1]), (pred[0], pred[1])).meters\n", " total_loss += distance ** 2\n", "\n", " total_samples += len(gps_coords)\n", "\n", " rmse = np.sqrt(total_loss / total_samples)\n", " return rmse\n", "\n", "rmse = compute_rmse_in_meters(predictions, val_dataloader, lat_mean, lon_mean, lat_std, lon_std)\n", "\n", "print(f\"Root Mean Squared Error (meters): {rmse:.2f}\")" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "_jyPRsLi2mZ3", "outputId": "f71f0c8d-5de6-4bc5-82b6-53cc97ce6bee" }, "execution_count": 36, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Root Mean Squared Error (meters): 102.03\n" ] } ] } ] }