File size: 2,749 Bytes
51d04d9 8232884 c65afc8 8232884 c65afc8 8232884 c65afc8 8232884 c65afc8 8232884 c65afc8 8232884 c65afc8 8232884 c65afc8 8232884 c65afc8 8232884 bd7ea13 8232884 3708b71 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
---
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") |