Upload README.md
Browse files
README.md
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# LAJ CNN Image-to-GPS Model Iteration 1
|
| 2 |
+
|
| 3 |
+
This project features a convolutional neural network (CNN) for predicting GPS coordinates (latitude and longitude) from image inputs. Below, you'll find details on loading the model, performing inference, and the architecture of the network.
|
| 4 |
+
|
| 5 |
+
---
|
| 6 |
+
|
| 7 |
+
## 1. Loading the Model
|
| 8 |
+
|
| 9 |
+
To load the model, look at the sampleRun_v2.ipynb and run the same commands.
|
| 10 |
+
|
| 11 |
+
## 2. Running the Model
|
| 12 |
+
|
| 13 |
+
To perform inference on our model, just normalize the latitudes and longitudes to our means and standard deviations below.
|
| 14 |
+
Then run code similar to the code provided to test code provided below:
|
| 15 |
+
|
| 16 |
+
```
|
| 17 |
+
# Evaluate on Test Set
|
| 18 |
+
model.eval()
|
| 19 |
+
all_preds, all_actuals = [], []
|
| 20 |
+
with torch.no_grad():
|
| 21 |
+
for images, gps_coords in val_loader:
|
| 22 |
+
images, gps_coords = images.to(device), gps_coords.to(device)
|
| 23 |
+
outputs = model(images)
|
| 24 |
+
all_preds.append(outputs.cpu())
|
| 25 |
+
all_actuals.append(gps_coords.cpu())
|
| 26 |
+
|
| 27 |
+
all_preds = torch.cat(all_preds).numpy()
|
| 28 |
+
all_actuals = torch.cat(all_actuals).numpy()
|
| 29 |
+
|
| 30 |
+
# Denormalize Predictions
|
| 31 |
+
all_preds_denorm = all_preds * np.array([lat_std, lon_std]) + np.array([lat_mean, lon_mean])
|
| 32 |
+
all_actuals_denorm = all_actuals * np.array([lat_std, lon_std]) + np.array([lat_mean, lon_mean])
|
| 33 |
+
|
| 34 |
+
# Compute Error Metrics
|
| 35 |
+
mae = mean_absolute_error(all_actuals_denorm, all_preds_denorm)
|
| 36 |
+
rmse = mean_squared_error(all_actuals_denorm, all_preds_denorm, squared=False)
|
| 37 |
+
print(f"Test Set Mean Absolute Error: {mae:.4f}")
|
| 38 |
+
print(f"Test Set Root Mean Squared Error: {rmse:.4f}")
|
| 39 |
+
```
|
| 40 |
+
|
| 41 |
+
## 3. Latitude and Longitude Means and Standard Deviations
|
| 42 |
+
|
| 43 |
+
The following values represent the **means** and **standard deviations** of the latitude and longitude used in this model:
|
| 44 |
+
|
| 45 |
+
- **Latitude Mean**: `39.95173729922173`
|
| 46 |
+
- **Latitude Standard Deviation**: `0.0006877829213952256`
|
| 47 |
+
- **Longitude Mean**: `-75.19138804851796`
|
| 48 |
+
- **Longitude Standard Deviation**: `0.0006182574854250925`
|
| 49 |
+
|
| 50 |
+
These values are used to normalize and denormalize the latitude and longitude predictions during inference.
|
| 51 |
+
|
| 52 |
+
## 4. CNN Architecture
|
| 53 |
+
|
| 54 |
+
Finally here is the architecture of the CNN we used:
|
| 55 |
+
|
| 56 |
+
```
|
| 57 |
+
# Model Definition
|
| 58 |
+
class CustomGPSModel(nn.Module):
|
| 59 |
+
def __init__(self):
|
| 60 |
+
super(CustomGPSModel, self).__init__()
|
| 61 |
+
|
| 62 |
+
# Load EfficientNet-B0 with pretrained weights
|
| 63 |
+
self.efficientnet = efficientnet_b0(pretrained=True)
|
| 64 |
+
|
| 65 |
+
# Modify the final layer for regression (predicting latitude and longitude)
|
| 66 |
+
num_features = self.efficientnet.classifier[1].in_features
|
| 67 |
+
self.efficientnet.classifier[1] = nn.Linear(num_features, 2) # Output layer has 2 outputs for latitude & longitude
|
| 68 |
+
|
| 69 |
+
# Freeze earlier layers except the last few
|
| 70 |
+
for param in self.efficientnet.features.parameters():
|
| 71 |
+
param.requires_grad = True
|
| 72 |
+
|
| 73 |
+
def forward(self, x):
|
| 74 |
+
return self.efficientnet(x) # Forward pass through EfficientNet
|
| 75 |
+
```
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
## 5. Sample Run Code (how to install and run everything)
|
| 79 |
+
|
| 80 |
+
```
|
| 81 |
+
!pip install datasets
|
| 82 |
+
!pip install huggingface_hub
|
| 83 |
+
!pip install requests
|
| 84 |
+
|
| 85 |
+
import torch
|
| 86 |
+
import torch.nn as nn
|
| 87 |
+
import torch.optim as optim
|
| 88 |
+
from torchvision.models import efficientnet_b0
|
| 89 |
+
from torch.optim.lr_scheduler import CosineAnnealingLR
|
| 90 |
+
from torchvision import transforms
|
| 91 |
+
from torch.utils.data import DataLoader, Dataset
|
| 92 |
+
from torchvision.transforms import functional as F
|
| 93 |
+
from PIL import Image
|
| 94 |
+
import numpy as np
|
| 95 |
+
from sklearn.metrics import mean_absolute_error, mean_squared_error
|
| 96 |
+
from huggingface_hub import PyTorchModelHubMixin
|
| 97 |
+
import os
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
# Model Definition
|
| 101 |
+
class CustomGPSModel(nn.Module):
|
| 102 |
+
def __init__(self):
|
| 103 |
+
super(CustomGPSModel, self).__init__()
|
| 104 |
+
|
| 105 |
+
# Load EfficientNet-B0 with pretrained weights
|
| 106 |
+
self.efficientnet = efficientnet_b0(pretrained=True)
|
| 107 |
+
|
| 108 |
+
# Modify the final layer for regression (predicting latitude and longitude)
|
| 109 |
+
num_features = self.efficientnet.classifier[1].in_features
|
| 110 |
+
self.efficientnet.classifier[1] = nn.Linear(num_features, 2) # Output layer has 2 outputs for latitude & longitude
|
| 111 |
+
|
| 112 |
+
# Freeze earlier layers except the last few
|
| 113 |
+
for param in self.efficientnet.features.parameters():
|
| 114 |
+
param.requires_grad = True
|
| 115 |
+
|
| 116 |
+
def forward(self, x):
|
| 117 |
+
return self.efficientnet(x) # Forward pass through EfficientNet
|
| 118 |
+
|
| 119 |
+
from huggingface_hub import hf_hub_download
|
| 120 |
+
import torch
|
| 121 |
+
|
| 122 |
+
path_name = "efficientnet_gps_regressor_complete.pth"
|
| 123 |
+
repo_name = "CustomGPSModel_EfficientNetB0_Run2"
|
| 124 |
+
organization_name = "LAJ-519-Image-Project"
|
| 125 |
+
|
| 126 |
+
# Specify the repository and the filename of the model you want to load
|
| 127 |
+
repo_id = f"{organization_name}/{repo_name}"
|
| 128 |
+
filename = f"{path_name}"
|
| 129 |
+
|
| 130 |
+
model_path = hf_hub_download(repo_id=repo_id, filename=filename)
|
| 131 |
+
|
| 132 |
+
# Load the model using torch
|
| 133 |
+
model_test = torch.load(model_path)
|
| 134 |
+
model_test.eval()
|
| 135 |
+
```
|