Andywxy commited on
Commit
8232884
·
verified ·
1 Parent(s): 6875d22

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +21 -28
README.md CHANGED
@@ -3,27 +3,37 @@ tags:
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,
@@ -34,35 +44,18 @@ test_dataset = GPSImageDataset(
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])
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]
68
- - Docs: [More Information Needed]
 
3
  - model_hub_mixin
4
  - pytorch_model_hub_mixin
5
  ---
6
+
7
+ # GPS Prediction Using ResNet Model
8
+
9
+ 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).
10
+
11
+ ## Inference Script
12
+
13
+ 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.
14
+
15
+ ```python
16
  from geopy.distance import geodesic
17
  import numpy as np
18
  import torch
19
+ from torch.utils.data import DataLoader
 
 
 
 
20
  from huggingface_hub import PyTorchModelHubMixin
21
 
22
+ # Load the pre-trained model from the Hub
23
  model = CustomResNetModel.from_pretrained("5190final/model1")
24
 
25
+ # Normalization constants for latitude and longitude
26
  lat_mean = 39.951611366653395
27
  lat_std = 0.0006686190927448403
28
  lon_mean = -75.19145880459313
29
  lon_std = 0.0006484111794126842
30
 
31
+ # Set up device (use MPS if available, otherwise fallback to CPU)
32
  device = torch.device("mps" if torch.backends.mps.is_available() else "cpu")
33
  model.to(device)
34
  model.eval()
35
 
36
+ # Prepare the dataset and dataloader
37
  test_dataset = GPSImageDataset(
38
  hf_dataset=dataset_test,
39
  transform=inference_transform,
 
44
  )
45
  test_dataloader = DataLoader(test_dataset, batch_size=32, shuffle=False)
46
 
47
+ # Initialize lists to store predictions and actual values
48
  all_preds = []
49
  all_actuals = []
50
 
51
+ # Run inference
52
+ with torch.no_grad():
53
  for images, gps_coords in test_dataloader:
54
  images = images.to(device)
55
  gps_coords = gps_coords.to(device)
56
 
57
  outputs = model(images)
58
+ logits = outputs.logits # Extract predictions
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
 
60
+ all_preds.extend(logits.cpu().numpy()) # Store predictions
61
+ all_actuals.extend(gps_coords.cpu