File size: 2,386 Bytes
51d04d9 c65afc8 51d04d9 bd7ea13 51d04d9 |
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 |
---
tags:
- model_hub_mixin
- pytorch_model_hub_mixin
---
from geopy.distance import geodesic
import numpy as np
import torch
import torch.nn as nn
import torchvision.models as models
import torchvision.transforms as transforms
from torch.utils.data import DataLoader, Dataset
from transformers import AutoImageProcessor, AutoModelForImageClassification
from huggingface_hub import PyTorchModelHubMixin
model = CustomResNetModel.from_pretrained("5190final/model1")
lat_mean = 39.951611366653395
lat_std = 0.0006686190927448403
lon_mean = -75.19145880459313
lon_std = 0.0006484111794126842
device = torch.device("mps" if torch.backends.mps.is_available() else "cpu")
model.to(device)
model.eval()
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)
all_preds = []
all_actuals = []
with torch.no_grad(): # Disable gradient calculations during inference
for images, gps_coords in test_dataloader:
images = images.to(device)
gps_coords = gps_coords.to(device)
outputs = model(images)
logits = outputs.logits # Extract the predictions
all_preds.extend(logits.cpu().numpy()) # Append predictions to the list
all_actuals.extend(gps_coords.cpu().numpy()) # Append actual values to the list
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]) # Assuming lat_std, etc., are lists or NumPy arrays
all_actuals_denorm = all_actuals * np.array([lat_std, lon_std]) + np.array([lat_mean, lon_mean])
squared_errors = []
for pred, actual in zip(all_preds_denorm, all_actuals_denorm):
# Calculate geodesic distance between predicted and actual coordinates
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))
This model has been pushed to the Hub using the [PytorchModelHubMixin](https://huggingface.co/docs/huggingface_hub/package_reference/mixins#huggingface_hub.PyTorchModelHubMixin) integration:
- Library: [More Information Needed]
- Docs: [More Information Needed] |