Views
No views yet
# Evaluate on Test Set
model.eval()
all_preds, all_actuals = [], []
with torch.no_grad():
for images, gps_coords in val_loader:
images, gps_coords = images.to(device), gps_coords.to(device)
outputs = model(images)
all_preds.append(outputs.cpu())
all_actuals.append(gps_coords.cpu())
all_preds = torch.cat(all_preds).numpy()
all_actuals = torch.cat(all_actuals).numpy()
# Denormalize Predictions
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])
# Compute Error Metrics
mae = mean_absolute_error(all_actuals_denorm, all_preds_denorm)
rmse = mean_squared_error(all_actuals_denorm, all_preds_denorm, squared=False)
print(f"Test Set Mean Absolute Error: {mae:.4f}")
print(f"Test Set Root Mean Squared Error: {rmse:.4f}")39.951737299221730.0006877829213952256-75.191388048517960.0006182574854250925# Model Definition
class CustomGPSModel(nn.Module):
def __init__(self):
super(CustomGPSModel, self).__init__()
# Load EfficientNet-B0 with pretrained weights
self.efficientnet = efficientnet_b0(pretrained=True)
# Modify the final layer for regression (predicting latitude and longitude)
num_features = self.efficientnet.classifier[1].in_features
self.efficientnet.classifier[1] = nn.Linear(num_features, 2) # Output layer has 2 outputs for latitude & longitude
# Freeze earlier layers except the last few
for param in self.efficientnet.features.parameters():
param.requires_grad = True
def forward(self, x):
return self.efficientnet(x) # Forward pass through EfficientNet!pip install datasets
!pip install huggingface_hub
!pip install requests
import torch
import torch.nn as nn
import torch.optim as optim
from torchvision.models import efficientnet_b0
from torch.optim.lr_scheduler import CosineAnnealingLR
from torchvision import transforms
from torch.utils.data import DataLoader, Dataset
from torchvision.transforms import functional as F
from PIL import Image
import numpy as np
from sklearn.metrics import mean_absolute_error, mean_squared_error
from huggingface_hub import PyTorchModelHubMixin
import os
# Model Definition
class CustomGPSModel(nn.Module):
def __init__(self):
super(CustomGPSModel, self).__init__()
# Load EfficientNet-B0 with pretrained weights
self.efficientnet = efficientnet_b0(pretrained=True)
# Modify the final layer for regression (predicting latitude and longitude)
num_features = self.efficientnet.classifier[1].in_features
self.efficientnet.classifier[1] = nn.Linear(num_features, 2) # Output layer has 2 outputs for latitude & longitude
# Freeze earlier layers except the last few
for param in self.efficientnet.features.parameters():
param.requires_grad = True
def forward(self, x):
return self.efficientnet(x) # Forward pass through EfficientNet
from huggingface_hub import hf_hub_download
import torch
path_name = "efficientnet_gps_regressor_complete.pth"
repo_name = "CustomGPSModel_EfficientNetB0_Run2"
organization_name = "LAJ-519-Image-Project"
# Specify the repository and the filename of the model you want to load
repo_id = f"{organization_name}/{repo_name}"
filename = f"{path_name}"
model_path = hf_hub_download(repo_id=repo_id, filename=filename)
# Load the model using torch
model_test = torch.load(model_path)
model_test.eval()