Monday, September 14, 2026

 

Sample Application of vision model and global tiling:

import torch 

from transformers import AutoProcessor, AutoModel 

import requests 

from PIL import Image 

import io 

import numpy as np 

from sklearn.neighbors import NearestNeighbors 

# ------------------------------------------------------------ 

# 1. Load Prithvi EO 2.0 model + processor 

# ------------------------------------------------------------ 

model_name = "ibm-nasa-geospatial/Prithvi-EO-2.0-300M" 

 

processor = AutoProcessor.from_pretrained(model_name) 

model = AutoModel.from_pretrained(model_name) 

model.eval() 

 

# ------------------------------------------------------------ 

# 2. Load drone image, say from SAS URL 

# ------------------------------------------------------------ 

url = "https://sadronevideo.blob.core.windows.net/input/interesting/what-location.jpg?sp=r&st=2026-09-13T01:22:02Z&se=2026-09-13T09:37:02Z&spr=https&sv=2026-02-06&sr=b&sig=9Ab0REdBAyuLT5lsOizuRLd8ijPtqle8XtOvw%2FjjDKQ%3D" 

 

response = requests.get(url) 

image = Image.open(io.BytesIO(response.content)).convert("RGB") 

 

# ------------------------------------------------------------ 

# 3. Preprocess + embed using Prithvi EO 2.0 

# ------------------------------------------------------------ 

inputs = processor(images=image, return_tensors="pt") 

 

with torch.no_grad(): 

    outputs = model(**inputs) 

    # Prithvi returns last_hidden_state; we pool it to get a single vector 

    embedding = outputs.last_hidden_state.mean(dim=1).squeeze().cpu().numpy() 

 

print("Embedding shape:", embedding.shape) 

 

# ------------------------------------------------------------ 

# 4. Build a tiny reference corpus  

# Each entry: (embedding_vector, (lat, lon)) 

# ------------------------------------------------------------ 

 

# Example reference embeddings  

reference_embeddings = np.random.rand(5, embedding.shape[0]) reference_locations = [ 

    (37.769939, -122.387722), # San Francisco 

    (47.608494, -122.339175), # Seattle 

    (40.706347, -74.010397), # New York 

    (25.758758, -80.191192), # Miami 

    (42.371839, -71.117986), # Cambridge  

 


REFERENCE_DIR = "./reference_tiles" 

 

tile_files = [ 

    ("Cambridge.jpg", (42.371839, -71.117986)), 

    ("Miami.jpg", (25.758758, -80.191192)), 

    ("NewYork.jpg", (40.706347, -74.010397)), 

    ("SanFrancisco.jpg", (37.769939, -122.387722)), 

    ("Seattle.jpg", (47.608494, -122.339175)), 

 

image_paths = [os.path.join(REFERENCE_DIR, f[0]) for f in tile_files] 

gps_coords = [f[1] for f in tile_files] 

embeddings = [] 

images = [] 

 

for path in image_paths: 

    img = Image.open(path).convert("RGB") 

    images.append(img) 

 

    inputs = processor(images=img, return_tensors="pt") 

 

    with torch.no_grad(): 

        outputs = model(**inputs) 

        emb = outputs.last_hidden_state.mean(dim=1).squeeze().cpu().numpy() 

 

    embeddings.append(emb) 

 

embeddings = np.array(embeddings, dtype=np.float32) 

images = np.array(images, dtype=object) 

gps_coords = np.array(gps_coords, dtype=np.float32) 

 

np.save("reference_embeddings.npy", embeddings) 

np.save("reference_images.npy", images) 

np.save("reference_gps.npy", gps_coords) 

 

reference_embeddings = np.load("earth_tile_embeddings.npy") 


reference_locations = np.load("earth_tile_locations.npy") 


 

 

# ------------------------------------------------------------ 

# 5. Fit nearest-neighbor search 

# ------------------------------------------------------------ 

nn = NearestNeighbors(n_neighbors=1, metric="cosine") 

nn.fit(reference_embeddings) 

 

dist, idx = nn.kneighbors([embedding]) 

best_index = idx[0][0] 

best_distance = dist[0][0] 

 

estimated_location = reference_locations[best_index] 

 

print("\nEstimated GPS coordinates:", estimated_location) 

print("Cosine distance:", best_distance)


## Result:

# Nearest match GPS: [ 42.371839 -71.117986 ]

# Cosine distance: 0.82509133


No comments:

Post a Comment