Update README.md
Browse files
README.md
CHANGED
|
@@ -6,4 +6,197 @@ tags:
|
|
| 6 |
|
| 7 |
This model has been pushed to the Hub using the [PytorchModelHubMixin](https://huggingface.co/docs/huggingface_hub/package_reference/mixins#huggingface_hub.PyTorchModelHubMixin) integration:
|
| 8 |
- Library: [More Information Needed]
|
| 9 |
-
- Docs: [More Information Needed]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
This model has been pushed to the Hub using the [PytorchModelHubMixin](https://huggingface.co/docs/huggingface_hub/package_reference/mixins#huggingface_hub.PyTorchModelHubMixin) integration:
|
| 8 |
- Library: [More Information Needed]
|
| 9 |
+
- Docs: [More Information Needed]
|
| 10 |
+
|
| 11 |
+
---
|
| 12 |
+
dataset_info:
|
| 13 |
+
features:
|
| 14 |
+
- name: image
|
| 15 |
+
dtype: image
|
| 16 |
+
- name: label
|
| 17 |
+
dtype:
|
| 18 |
+
class_label:
|
| 19 |
+
names:
|
| 20 |
+
'0': test
|
| 21 |
+
'1': train
|
| 22 |
+
'2': validation
|
| 23 |
+
splits:
|
| 24 |
+
- name: train
|
| 25 |
+
num_bytes: 7260686.0
|
| 26 |
+
num_examples: 560
|
| 27 |
+
- name: validation
|
| 28 |
+
num_bytes: 182280987.0
|
| 29 |
+
num_examples: 78
|
| 30 |
+
- name: test
|
| 31 |
+
num_bytes: 2290972.0
|
| 32 |
+
num_examples: 147
|
| 33 |
+
download_size: 172987254
|
| 34 |
+
dataset_size: 191832645.0
|
| 35 |
+
configs:
|
| 36 |
+
- config_name: default
|
| 37 |
+
data_files:
|
| 38 |
+
- split: train
|
| 39 |
+
path: data/train-*
|
| 40 |
+
- split: validation
|
| 41 |
+
path: data/validation-*
|
| 42 |
+
- split: test
|
| 43 |
+
path: data/test-*
|
| 44 |
+
---
|
| 45 |
+
Model inference:
|
| 46 |
+
# model
|
| 47 |
+
|
| 48 |
+
import os
|
| 49 |
+
import torch
|
| 50 |
+
import torch.nn as nn
|
| 51 |
+
from torch.utils.data import Dataset, DataLoader
|
| 52 |
+
from PIL import Image
|
| 53 |
+
from torchvision import transforms
|
| 54 |
+
import pandas as pd
|
| 55 |
+
from huggingface_hub import PyTorchModelHubMixin
|
| 56 |
+
|
| 57 |
+
# Define the custom dataset
|
| 58 |
+
class GPSImageDataset(Dataset):
|
| 59 |
+
def __init__(self, hf_dataset, transform=None, lat_mean=None, lat_std=None, lon_mean=None, lon_std=None):
|
| 60 |
+
self.hf_dataset = hf_dataset
|
| 61 |
+
self.transform = transform
|
| 62 |
+
|
| 63 |
+
# Compute mean and std from the dataframe if not provided
|
| 64 |
+
self.latitude_mean = lat_mean if lat_mean is not None else np.mean(np.array(self.hf_dataset['Latitude']))
|
| 65 |
+
self.latitude_std = lat_std if lat_std is not None else np.std(np.array(self.hf_dataset['Latitude']))
|
| 66 |
+
self.longitude_mean = lon_mean if lon_mean is not None else np.mean(np.array(self.hf_dataset['Longitude']))
|
| 67 |
+
self.longitude_std = lon_std if lon_std is not None else np.std(np.array(self.hf_dataset['Longitude']))
|
| 68 |
+
|
| 69 |
+
def __len__(self):
|
| 70 |
+
return len(self.hf_dataset)
|
| 71 |
+
|
| 72 |
+
def __getitem__(self, idx):
|
| 73 |
+
# Extract data
|
| 74 |
+
example = self.hf_dataset[idx]
|
| 75 |
+
|
| 76 |
+
# Load and process the image
|
| 77 |
+
image = example['image']
|
| 78 |
+
latitude = example['Latitude']
|
| 79 |
+
longitude = example['Longitude']
|
| 80 |
+
# image = image.rotate(-90, expand=True)
|
| 81 |
+
if self.transform:
|
| 82 |
+
image = self.transform(image)
|
| 83 |
+
|
| 84 |
+
# Normalize GPS coordinates
|
| 85 |
+
latitude = (latitude - self.latitude_mean) / self.latitude_std
|
| 86 |
+
longitude = (longitude - self.longitude_mean) / self.longitude_std
|
| 87 |
+
gps_coords = torch.tensor([latitude, longitude], dtype=torch.float32)
|
| 88 |
+
|
| 89 |
+
return image, gps_coords
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
# Define the CNN model
|
| 93 |
+
class CustomCNNModel(nn.Module, PyTorchModelHubMixin):
|
| 94 |
+
def __init__(self, num_classes=2):
|
| 95 |
+
super(CustomCNNModel, self).__init__()
|
| 96 |
+
|
| 97 |
+
# Define the CNN architecture (4 convolutional layers followed by fully connected layers)
|
| 98 |
+
self.conv1 = nn.Conv2d(3, 32, kernel_size=3, stride=1, padding=1)
|
| 99 |
+
self.conv2 = nn.Conv2d(32, 64, kernel_size=3, stride=1, padding=1)
|
| 100 |
+
self.conv3 = nn.Conv2d(64, 128, kernel_size=3, stride=1, padding=1)
|
| 101 |
+
self.conv4 = nn.Conv2d(128, 256, kernel_size=3, stride=1, padding=1)
|
| 102 |
+
|
| 103 |
+
self.pool = nn.MaxPool2d(2, 2)
|
| 104 |
+
|
| 105 |
+
# Define the fully connected layers after flattening
|
| 106 |
+
self.fc1 = nn.Linear(256 * 14 * 14, 512) # Output size after pooling (assuming input image is 224x224)
|
| 107 |
+
self.fc2 = nn.Linear(512, 256)
|
| 108 |
+
self.fc3 = nn.Linear(256, num_classes) # Output layer (2 values: latitude and longitude)
|
| 109 |
+
|
| 110 |
+
# Activation functions
|
| 111 |
+
self.relu = nn.ReLU()
|
| 112 |
+
|
| 113 |
+
def forward(self, x):
|
| 114 |
+
# Pass through convolutional layers
|
| 115 |
+
x = self.relu(self.conv1(x))
|
| 116 |
+
x = self.pool(x)
|
| 117 |
+
x = self.relu(self.conv2(x))
|
| 118 |
+
x = self.pool(x)
|
| 119 |
+
x = self.relu(self.conv3(x))
|
| 120 |
+
x = self.pool(x)
|
| 121 |
+
x = self.relu(self.conv4(x))
|
| 122 |
+
x = self.pool(x)
|
| 123 |
+
|
| 124 |
+
# Flatten the tensor before passing it to the fully connected layers
|
| 125 |
+
x = x.view(-1, 256 * 14 * 14)
|
| 126 |
+
|
| 127 |
+
# Pass through fully connected layers
|
| 128 |
+
x = self.relu(self.fc1(x))
|
| 129 |
+
x = self.relu(self.fc2(x))
|
| 130 |
+
x = self.fc3(x)
|
| 131 |
+
|
| 132 |
+
return x
|
| 133 |
+
|
| 134 |
+
|
| 135 |
+
# Define transformations for images
|
| 136 |
+
transform = transforms.Compose([
|
| 137 |
+
transforms.Resize((224, 224)), # Resize to match the input size of the model
|
| 138 |
+
transforms.ToTensor(), # Convert image to tensor
|
| 139 |
+
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) # Optional normalization
|
| 140 |
+
])
|
| 141 |
+
|
| 142 |
+
from datasets import load_dataset
|
| 143 |
+
|
| 144 |
+
# loading in data
|
| 145 |
+
ds = load_dataset("gydou/released_img", split = "train")
|
| 146 |
+
|
| 147 |
+
# pulling in weights from hugging face
|
| 148 |
+
model=CustomCNNModel.from_pretrained("CIS-5190-Project-1/model")
|
| 149 |
+
|
| 150 |
+
lat_mean = 35
|
| 151 |
+
lat_std = 8
|
| 152 |
+
lon_mean = 70
|
| 153 |
+
lon_std = 6
|
| 154 |
+
# Optionally, you can create a separate transform for inference without augmentations
|
| 155 |
+
inference_transform = transforms.Compose([
|
| 156 |
+
transforms.Resize((224, 224)),
|
| 157 |
+
transforms.ToTensor(),
|
| 158 |
+
transforms.Normalize(mean=[0.485, 0.456, 0.406],
|
| 159 |
+
std=[0.229, 0.224, 0.225])
|
| 160 |
+
])
|
| 161 |
+
|
| 162 |
+
val_dataset = GPSImageDataset(
|
| 163 |
+
hf_dataset=ds,
|
| 164 |
+
transform=inference_transform,
|
| 165 |
+
lat_mean=lat_mean,
|
| 166 |
+
lat_std=lat_std,
|
| 167 |
+
lon_mean=lon_mean,
|
| 168 |
+
lon_std=lon_std
|
| 169 |
+
)
|
| 170 |
+
val_dataloader = DataLoader(val_dataset, batch_size=32, shuffle=False)
|
| 171 |
+
|
| 172 |
+
from sklearn.metrics import mean_absolute_error, mean_squared_error
|
| 173 |
+
|
| 174 |
+
# Initialize lists to store predictions and actual values
|
| 175 |
+
all_preds = []
|
| 176 |
+
all_actuals = []
|
| 177 |
+
|
| 178 |
+
model.eval()
|
| 179 |
+
with torch.no_grad():
|
| 180 |
+
for images, gps_coords in val_dataloader:
|
| 181 |
+
images, gps_coords = images.to("cpu"), gps_coords.to("cpu")
|
| 182 |
+
|
| 183 |
+
outputs = model(images)
|
| 184 |
+
|
| 185 |
+
# Denormalize predictions and actual values
|
| 186 |
+
preds = outputs.cpu() * torch.tensor([lat_std, lon_std]) + torch.tensor([lat_mean, lon_mean])
|
| 187 |
+
actuals = gps_coords.cpu() * torch.tensor([lat_std, lon_std]) + torch.tensor([lat_mean, lon_mean])
|
| 188 |
+
|
| 189 |
+
all_preds.append(preds)
|
| 190 |
+
all_actuals.append(actuals)
|
| 191 |
+
break
|
| 192 |
+
|
| 193 |
+
# Concatenate all batches
|
| 194 |
+
all_preds = torch.cat(all_preds).numpy()
|
| 195 |
+
all_actuals = torch.cat(all_actuals).numpy()
|
| 196 |
+
|
| 197 |
+
# Compute error metrics
|
| 198 |
+
mae = mean_absolute_error(all_actuals, all_preds)
|
| 199 |
+
rmse = mean_squared_error(all_actuals, all_preds, squared=False)
|
| 200 |
+
|
| 201 |
+
print(f'Mean Absolute Error: {mae}')
|
| 202 |
+
print(f'Root Mean Squared Error: {rmse}')
|