|
|
--- |
|
|
tags: |
|
|
- model_hub_mixin |
|
|
- pytorch_model_hub_mixin |
|
|
--- |
|
|
|
|
|
# GPS Prediction Using ResNet Model |
|
|
|
|
|
This repository contains a trained model for GPS coordinate prediction using a ResNet-based architecture. The model predicts latitude and longitude values from input images and has been deployed on the Hugging Face Hub using the [PyTorchModelHubMixin](https://huggingface.co/docs/huggingface_hub/package_reference/mixins#huggingface_hub.PyTorchModelHubMixin). |
|
|
|
|
|
## Inference Script |
|
|
|
|
|
Below is the Python implementation of the inference process, where we predict GPS coordinates and evaluate the model's performance using the Root Mean Squared Error (RMSE) based on geodesic distances. |
|
|
|
|
|
```python |
|
|
from geopy.distance import geodesic |
|
|
import numpy as np |
|
|
import torch |
|
|
from torch.utils.data import DataLoader |
|
|
from huggingface_hub import PyTorchModelHubMixin |
|
|
|
|
|
# Load the pre-trained model from the Hub |
|
|
model = CustomResNetModel.from_pretrained("5190final/model1") |
|
|
|
|
|
# Normalization constants for latitude and longitude |
|
|
lat_mean = 39.951611366653395 |
|
|
lat_std = 0.0006686190927448403 |
|
|
lon_mean = -75.19145880459313 |
|
|
lon_std = 0.0006484111794126842 |
|
|
|
|
|
# Set up device (use MPS if available, otherwise fallback to CPU) |
|
|
device = torch.device("mps" if torch.backends.mps.is_available() else "cpu") |
|
|
model.to(device) |
|
|
model.eval() |
|
|
|
|
|
# Prepare the dataset and dataloader |
|
|
test_dataset = GPSImageDataset( |
|
|
hf_dataset=dataset_test, |
|
|
transform=inference_transform, |
|
|
lat_mean=lat_mean, |
|
|
lat_std=lat_std, |
|
|
lon_mean=lon_mean, |
|
|
lon_std=lon_std |
|
|
) |
|
|
test_dataloader = DataLoader(test_dataset, batch_size=32, shuffle=False) |
|
|
|
|
|
# Initialize lists to store predictions and actual values |
|
|
all_preds = [] |
|
|
all_actuals = [] |
|
|
|
|
|
# Run inference |
|
|
with torch.no_grad(): |
|
|
for images, gps_coords in test_dataloader: |
|
|
images = images.to(device) |
|
|
gps_coords = gps_coords.to(device) |
|
|
|
|
|
outputs = model(images) |
|
|
logits = outputs.logits # Extract predictions |
|
|
|
|
|
all_preds.extend(logits.cpu().numpy()) # Store predictions |
|
|
all_actuals.extend(gps_coords.cpu().numpy()) # Store actual values |
|
|
|
|
|
# Denormalize predictions and actual values |
|
|
all_preds = np.array(all_preds) |
|
|
all_actuals = np.array(all_actuals) |
|
|
|
|
|
all_preds_denorm = all_preds * np.array([lat_std, lon_std]) + np.array([lat_mean, lon_mean]) |
|
|
all_actuals_denorm = all_actuals * np.array([lat_std, lon_std]) + np.array([lat_mean, lon_mean]) |
|
|
|
|
|
# Calculate RMSE using geodesic distances |
|
|
squared_errors = [] |
|
|
for pred, actual in zip(all_preds_denorm, all_actuals_denorm): |
|
|
distance = geodesic((actual[0], actual[1]), (pred[0], pred[1])).meters |
|
|
squared_errors.append(distance**2) # Square the distance for RMSE |
|
|
|
|
|
rmse = np.sqrt(np.mean(squared_errors)) |
|
|
print(f"RMSE: {rmse:.2f} meters") |