Delete README.md
Browse files
README.md
DELETED
|
@@ -1,136 +0,0 @@
|
|
| 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_v3.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 |
-
all_preds = torch.cat(all_preds).numpy()
|
| 27 |
-
all_actuals = torch.cat(all_actuals).numpy()
|
| 28 |
-
|
| 29 |
-
# Denormalize Predictions
|
| 30 |
-
all_preds_denorm = all_preds * np.array([lat_std, lon_std]) + np.array([lat_mean, lon_mean])
|
| 31 |
-
all_actuals_denorm = all_actuals * np.array([lat_std, lon_std]) + np.array([lat_mean, lon_mean])
|
| 32 |
-
|
| 33 |
-
# Compute Error Metrics
|
| 34 |
-
mae = mean_absolute_error(all_actuals_denorm, all_preds_denorm)
|
| 35 |
-
rmse = mean_squared_error(all_actuals_denorm, all_preds_denorm, squared=False)
|
| 36 |
-
print(f"Test Set Mean Absolute Error: {mae:}")
|
| 37 |
-
print(f"Test Set Root Mean Squared Error: {rmse:}")
|
| 38 |
-
```
|
| 39 |
-
|
| 40 |
-
## 3. Latitude and Longitude Means and Standard Deviations
|
| 41 |
-
|
| 42 |
-
The following values represent the **means** and **standard deviations** of the latitude and longitude used in this model:
|
| 43 |
-
|
| 44 |
-
- **Latitude Mean**: `39.95173729922173`
|
| 45 |
-
- **Latitude Standard Deviation**: `0.0006877829213952256`
|
| 46 |
-
- **Longitude Mean**: `-75.19138804851796`
|
| 47 |
-
- **Longitude Standard Deviation**: `0.0006182574854250925`
|
| 48 |
-
These values are used to normalize and denormalize the latitude and longitude predictions during inference.
|
| 49 |
-
|
| 50 |
-
## 4. CNN Architecture
|
| 51 |
-
|
| 52 |
-
Finally here is the architecture of the CNN we used:
|
| 53 |
-
|
| 54 |
-
```
|
| 55 |
-
import torch
|
| 56 |
-
import torch.nn as nn
|
| 57 |
-
import torch.optim as optim
|
| 58 |
-
from torchvision.models import efficientnet_v2_s
|
| 59 |
-
from torch.optim.lr_scheduler import CosineAnnealingLR
|
| 60 |
-
from torchvision import transforms
|
| 61 |
-
from torch.utils.data import DataLoader, Dataset
|
| 62 |
-
from torchvision.transforms import functional as F
|
| 63 |
-
from PIL import Image
|
| 64 |
-
import numpy as np
|
| 65 |
-
from sklearn.metrics import mean_absolute_error, mean_squared_error
|
| 66 |
-
from huggingface_hub import PyTorchModelHubMixin
|
| 67 |
-
import os
|
| 68 |
-
|
| 69 |
-
# Model Definition
|
| 70 |
-
class CustomGPSModel(nn.Module):
|
| 71 |
-
def __init__(self):
|
| 72 |
-
super(CustomGPSModel, self).__init__()
|
| 73 |
-
|
| 74 |
-
# Load EfficientNetV2-S with pretrained weights
|
| 75 |
-
self.efficientnet = efficientnet_v2_s(pretrained=True)
|
| 76 |
-
|
| 77 |
-
# Modify the final layer for regression (predicting latitude and longitude)
|
| 78 |
-
num_features = self.efficientnet.classifier[1].in_features
|
| 79 |
-
self.efficientnet.classifier[1] = nn.Linear(num_features, 2) # Output layer has 2 outputs for latitude & longitude
|
| 80 |
-
|
| 81 |
-
# Don't freeze earlier layers
|
| 82 |
-
for param in self.efficientnet.features.parameters():
|
| 83 |
-
param.requires_grad = True
|
| 84 |
-
|
| 85 |
-
def forward(self, x):
|
| 86 |
-
return self.efficientnet(x) # Forward pass through EfficientNet
|
| 87 |
-
|
| 88 |
-
```
|
| 89 |
-
|
| 90 |
-
## 5. Sample Run Code (how to install and run everything)
|
| 91 |
-
|
| 92 |
-
```
|
| 93 |
-
!pip install datasets
|
| 94 |
-
!pip install huggingface_hub
|
| 95 |
-
!pip install requests
|
| 96 |
-
import torch
|
| 97 |
-
import torch.nn as nn
|
| 98 |
-
import torch.optim as optim
|
| 99 |
-
from torchvision.models import efficientnet_b0
|
| 100 |
-
from torch.optim.lr_scheduler import CosineAnnealingLR
|
| 101 |
-
from torchvision import transforms
|
| 102 |
-
from torch.utils.data import DataLoader, Dataset
|
| 103 |
-
from torchvision.transforms import functional as F
|
| 104 |
-
from PIL import Image
|
| 105 |
-
import numpy as np
|
| 106 |
-
from sklearn.metrics import mean_absolute_error, mean_squared_error
|
| 107 |
-
from huggingface_hub import PyTorchModelHubMixin
|
| 108 |
-
import os
|
| 109 |
-
# Model Definition
|
| 110 |
-
class CustomGPSModel(nn.Module):
|
| 111 |
-
def __init__(self):
|
| 112 |
-
super(CustomGPSModel, self).__init__()
|
| 113 |
-
# Load EfficientNet-B0 with pretrained weights
|
| 114 |
-
self.efficientnet = efficientnet_b0(pretrained=True)
|
| 115 |
-
# Modify the final layer for regression (predicting latitude and longitude)
|
| 116 |
-
num_features = self.efficientnet.classifier[1].in_features
|
| 117 |
-
self.efficientnet.classifier[1] = nn.Linear(num_features, 2) # Output layer has 2 outputs for latitude & longitude
|
| 118 |
-
# Freeze earlier layers except the last few
|
| 119 |
-
for param in self.efficientnet.features.parameters():
|
| 120 |
-
param.requires_grad = True
|
| 121 |
-
def forward(self, x):
|
| 122 |
-
return self.efficientnet(x) # Forward pass through EfficientNet
|
| 123 |
-
|
| 124 |
-
from huggingface_hub import hf_hub_download
|
| 125 |
-
import torch
|
| 126 |
-
path_name = "efficientnet_gps_regressor_complete.pth"
|
| 127 |
-
repo_name = "CustomGPSModel_EfficientNetB0_Run2"
|
| 128 |
-
organization_name = "LAJ-519-Image-Project"
|
| 129 |
-
# Specify the repository and the filename of the model you want to load
|
| 130 |
-
repo_id = f"{organization_name}/{repo_name}"
|
| 131 |
-
filename = f"{path_name}"
|
| 132 |
-
model_path = hf_hub_download(repo_id=repo_id, filename=filename)
|
| 133 |
-
# Load the model using torch
|
| 134 |
-
model_test = torch.load(model_path)
|
| 135 |
-
model_test.eval()
|
| 136 |
-
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|