Update README.md
Browse files
README.md
CHANGED
|
@@ -3,11 +3,65 @@ tags:
|
|
| 3 |
- model_hub_mixin
|
| 4 |
- pytorch_model_hub_mixin
|
| 5 |
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
lat_mean = [39.951611366653395],
|
| 8 |
-
lat_std = [0.0006686190927448403],
|
| 9 |
-
lon_mean = [-75.19145880459313],
|
| 10 |
-
lon_std = [0.0006484111794126842]
|
| 11 |
|
| 12 |
This model has been pushed to the Hub using the [PytorchModelHubMixin](https://huggingface.co/docs/huggingface_hub/package_reference/mixins#huggingface_hub.PyTorchModelHubMixin) integration:
|
| 13 |
- Library: [More Information Needed]
|
|
|
|
| 3 |
- model_hub_mixin
|
| 4 |
- pytorch_model_hub_mixin
|
| 5 |
---
|
| 6 |
+
from geopy.distance import geodesic
|
| 7 |
+
import numpy as np
|
| 8 |
+
import torch
|
| 9 |
+
import torch.nn as nn
|
| 10 |
+
import torchvision.models as models
|
| 11 |
+
import torchvision.transforms as transforms
|
| 12 |
+
from torch.utils.data import DataLoader, Dataset
|
| 13 |
+
from transformers import AutoImageProcessor, AutoModelForImageClassification
|
| 14 |
+
from huggingface_hub import PyTorchModelHubMixin
|
| 15 |
+
|
| 16 |
+
model = CustomResNetModel.from_pretrained("5190final/model1")
|
| 17 |
+
|
| 18 |
+
lat_mean = 39.951611366653395
|
| 19 |
+
lat_std = 0.0006686190927448403
|
| 20 |
+
lon_mean = -75.19145880459313
|
| 21 |
+
lon_std = 0.0006484111794126842
|
| 22 |
+
|
| 23 |
+
device = torch.device("mps" if torch.backends.mps.is_available() else "cpu")
|
| 24 |
+
model.to(device)
|
| 25 |
+
model.eval()
|
| 26 |
+
|
| 27 |
+
test_dataset = GPSImageDataset(
|
| 28 |
+
hf_dataset=dataset_test,
|
| 29 |
+
transform=inference_transform,
|
| 30 |
+
lat_mean=lat_mean,
|
| 31 |
+
lat_std=lat_std,
|
| 32 |
+
lon_mean=lon_mean,
|
| 33 |
+
lon_std=lon_std
|
| 34 |
+
)
|
| 35 |
+
test_dataloader = DataLoader(test_dataset, batch_size=32, shuffle=False)
|
| 36 |
+
|
| 37 |
+
all_preds = []
|
| 38 |
+
all_actuals = []
|
| 39 |
+
|
| 40 |
+
with torch.no_grad(): # Disable gradient calculations during inference
|
| 41 |
+
for images, gps_coords in test_dataloader:
|
| 42 |
+
images = images.to(device)
|
| 43 |
+
gps_coords = gps_coords.to(device)
|
| 44 |
+
|
| 45 |
+
outputs = model(images)
|
| 46 |
+
logits = outputs.logits # Extract the predictions
|
| 47 |
+
|
| 48 |
+
all_preds.extend(logits.cpu().numpy()) # Append predictions to the list
|
| 49 |
+
all_actuals.extend(gps_coords.cpu().numpy()) # Append actual values to the list
|
| 50 |
+
|
| 51 |
+
all_preds = np.array(all_preds)
|
| 52 |
+
all_actuals = np.array(all_actuals)
|
| 53 |
+
|
| 54 |
+
all_preds_denorm = all_preds * np.array([lat_std, lon_std]) + np.array([lat_mean, lon_mean]) # Assuming lat_std, etc., are lists or NumPy arrays
|
| 55 |
+
all_actuals_denorm = all_actuals * np.array([lat_std, lon_std]) + np.array([lat_mean, lon_mean])
|
| 56 |
+
|
| 57 |
+
squared_errors = []
|
| 58 |
+
for pred, actual in zip(all_preds_denorm, all_actuals_denorm):
|
| 59 |
+
# Calculate geodesic distance between predicted and actual coordinates
|
| 60 |
+
distance = geodesic((actual[0], actual[1]), (pred[0], pred[1])).meters
|
| 61 |
+
squared_errors.append(distance**2) # Square the distance for RMSE
|
| 62 |
+
|
| 63 |
+
rmse = np.sqrt(np.mean(squared_errors))
|
| 64 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
This model has been pushed to the Hub using the [PytorchModelHubMixin](https://huggingface.co/docs/huggingface_hub/package_reference/mixins#huggingface_hub.PyTorchModelHubMixin) integration:
|
| 67 |
- Library: [More Information Needed]
|