-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_clay_embeddings.py
More file actions
367 lines (289 loc) · 11.5 KB
/
Copy pathrun_clay_embeddings.py
File metadata and controls
367 lines (289 loc) · 11.5 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
"""
Clay Foundation Model - GeoAI Embeddings with Cosine Similarity Validation
Fetches Sentinel-2 imagery for a small region, generates embeddings using
Clay v1.5, and validates via cosine similarity between patches.
"""
import os
import sys
from pathlib import Path
import numpy as np
import torch
import yaml
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
from sklearn.metrics.pairwise import cosine_similarity
try:
import pystac_client
import planetary_computer
import stackstac
except ImportError:
print("Install dependencies: pip install pystac-client planetary-computer stackstac")
sys.exit(1)
try:
from claymodel.module import ClayMAEModule
except ImportError:
print("Install Clay model: pip install git+https://github.com/Clay-foundation/model.git")
sys.exit(1)
CHECKPOINT_PATH = os.environ.get("CLAY_CHECKPOINT", "v1.5/clay-v1.5.ckpt")
METADATA_PATH = "configs/metadata.yaml"
# Small region: Bangalore, India (urban + vegetation mix)
BBOX = [77.55, 12.93, 77.65, 13.03] # [west, south, east, north]
LATLON_CENTER = [12.98, 77.60]
TIME_RANGE = "2024-01-01/2024-03-31"
MAX_CLOUD_COVER = 10
PATCH_SIZE = 256
S2_BANDS = [
"B02", "B03", "B04", "B05", "B06", "B07", "B08", "B8A", "B11", "B12"
]
PLATFORM = "sentinel-2-l2a"
def load_metadata():
with open(METADATA_PATH) as f:
return yaml.safe_load(f)
def fetch_sentinel2_data():
"""Fetch Sentinel-2 data for the configured region from Planetary Computer."""
print(f"Fetching Sentinel-2 data for bbox={BBOX}, time={TIME_RANGE}...")
catalog = pystac_client.Client.open(
"https://planetarycomputer.microsoft.com/api/stac/v1",
modifier=planetary_computer.sign_inplace,
)
search = catalog.search(
collections=["sentinel-2-l2a"],
bbox=BBOX,
datetime=TIME_RANGE,
query={"eo:cloud_cover": {"lt": MAX_CLOUD_COVER}},
)
items = list(search.items())
if not items:
print("No items found. Try expanding the time range or cloud cover threshold.")
sys.exit(1)
print(f"Found {len(items)} scenes. Using the first one.")
item = items[0]
print(f" Scene: {item.id}")
print(f" Date: {item.datetime}")
stack = stackstac.stack(
[item],
assets=S2_BANDS,
resolution=10,
bounds_latlon=BBOX,
dtype="float64",
rescale=False,
epsg=32643,
)
data = stack.compute()
print(f" Data shape: {data.shape}")
image = data.values[0].astype(np.float32)
return image, item.datetime
def create_patches(image, patch_size=PATCH_SIZE):
"""Split image into non-overlapping patches of patch_size x patch_size."""
bands, h, w = image.shape
patches = []
coords = []
n_rows = h // patch_size
n_cols = w // patch_size
for i in range(n_rows):
for j in range(n_cols):
patch = image[
:,
i * patch_size : (i + 1) * patch_size,
j * patch_size : (j + 1) * patch_size,
]
if not np.isnan(patch).any() and patch.std() > 0:
patches.append(patch)
coords.append((i, j))
print(f"Created {len(patches)} valid patches of size {patch_size}x{patch_size}")
return patches, coords
def normalize_patches(patches, metadata):
"""Normalize patches using Clay's Sentinel-2 statistics from metadata."""
sensor = metadata[PLATFORM]
means = [sensor["bands"]["mean"][b] for b in sensor["band_order"]]
stds = [sensor["bands"]["std"][b] for b in sensor["band_order"]]
means = np.array(means, dtype=np.float32).reshape(1, -1, 1, 1)
stds = np.array(stds, dtype=np.float32).reshape(1, -1, 1, 1)
patches_array = np.stack(patches, axis=0).astype(np.float32)
normalized = (patches_array - means) / stds
return normalized
def generate_embeddings(patches_normalized, metadata, scene_datetime, device="cpu"):
"""Generate Clay embeddings for normalized patches."""
if not Path(CHECKPOINT_PATH).exists():
print(f"\nCheckpoint not found at: {CHECKPOINT_PATH}")
print("Download it with:")
print(" python -c \"from huggingface_hub import hf_hub_download; "
"hf_hub_download('made-with-clay/Clay', 'v1.5/clay-v1.5.ckpt', local_dir='.')\"")
sys.exit(1)
print(f"\nLoading Clay v1.5 model from {CHECKPOINT_PATH}...")
model = ClayMAEModule.load_from_checkpoint(
CHECKPOINT_PATH,
metadata_path=METADATA_PATH,
map_location=device,
)
model.eval()
model.to(device)
# Disable masking for inference
model.model.encoder.mask_ratio = 0.0
model.model.encoder.shuffle = False
sensor = metadata[PLATFORM]
wavelengths = torch.tensor(
[sensor["bands"]["wavelength"][b] for b in sensor["band_order"]],
dtype=torch.float32,
)
gsd = torch.tensor(sensor["gsd"], dtype=torch.float32)
batch_size = patches_normalized.shape[0]
# Encode time as [week_sin, week_cos, hour_sin, hour_cos]
if scene_datetime:
week = scene_datetime.isocalendar()[1]
hour = scene_datetime.hour
else:
week, hour = 0, 0
week_norm = 2 * np.pi * week / 52
hour_norm = 2 * np.pi * hour / 24
time_vec = [np.sin(week_norm), np.cos(week_norm), np.sin(hour_norm), np.cos(hour_norm)]
time_tensor = torch.tensor([time_vec] * batch_size, dtype=torch.float32, device=device)
# Lat/lon encoding [lat_sin, lat_cos, lon_sin, lon_cos]
lat, lon = LATLON_CENTER
lat_rad = np.radians(lat)
lon_rad = np.radians(lon)
latlon_vec = [np.sin(lat_rad), np.cos(lat_rad), np.sin(lon_rad), np.cos(lon_rad)]
latlon_tensor = torch.tensor(
[latlon_vec] * batch_size, dtype=torch.float32, device=device
)
print(f"Generating embeddings for {batch_size} patches (one at a time)...")
all_embeddings = []
for idx in range(batch_size):
chip = torch.tensor(
patches_normalized[idx : idx + 1], dtype=torch.float32, device=device
)
datacube = {
"pixels": chip,
"time": time_tensor[idx : idx + 1],
"latlon": latlon_tensor[idx : idx + 1],
"gsd": gsd.to(device),
"waves": wavelengths.to(device),
}
with torch.no_grad():
encoded_patches, _, _, _ = model.model.encoder(datacube)
cls_emb = encoded_patches[:, 0, :].cpu()
all_embeddings.append(cls_emb)
print(f" Patch {idx + 1}/{batch_size} done")
embeddings_np = torch.cat(all_embeddings, dim=0).numpy()
print(f"Embeddings shape: {embeddings_np.shape}")
return embeddings_np
def compute_and_validate_similarity(embeddings, coords, patches):
"""Compute cosine similarity and validate spatial coherence."""
print("\n--- Cosine Similarity Analysis ---")
sim_matrix = cosine_similarity(embeddings)
n = len(embeddings)
upper_tri_vals = sim_matrix[np.triu_indices(n, k=1)]
print(f"Similarity matrix shape: {sim_matrix.shape}")
print(f"Min similarity: {upper_tri_vals.min():.4f}")
print(f"Max similarity: {upper_tri_vals.max():.4f}")
print(f"Mean similarity: {upper_tri_vals.mean():.4f}")
print(f"Std similarity: {upper_tri_vals.std():.4f}")
adjacent_sims = []
distant_sims = []
for i in range(n):
for j in range(i + 1, n):
r1, c1 = coords[i]
r2, c2 = coords[j]
dist = abs(r1 - r2) + abs(c1 - c2)
if dist == 1:
adjacent_sims.append(sim_matrix[i, j])
elif dist >= 4:
distant_sims.append(sim_matrix[i, j])
print(f"\n--- Spatial Coherence Validation ---")
if adjacent_sims and distant_sims:
mean_adjacent = np.mean(adjacent_sims)
mean_distant = np.mean(distant_sims)
print(f"Adjacent patches (dist=1): mean={mean_adjacent:.4f}, n={len(adjacent_sims)}")
print(f"Distant patches (dist>=4): mean={mean_distant:.4f}, n={len(distant_sims)}")
print(f"Difference: {mean_adjacent - mean_distant:+.4f}")
if mean_adjacent > mean_distant:
print("PASS: Adjacent patches are more similar than distant patches.")
else:
print("NOTE: No clear spatial gradient — scene may be homogeneous.")
else:
print("Not enough patch pairs for adjacency validation.")
diag = np.diag(sim_matrix)
print(f"\nSelf-similarity (should be 1.0): min={diag.min():.6f}, max={diag.max():.6f}")
upper_tri = np.triu_indices(n, k=1)
sims_flat = sim_matrix[upper_tri]
most_idx = np.argmax(sims_flat)
least_idx = np.argmin(sims_flat)
i_m, j_m = upper_tri[0][most_idx], upper_tri[1][most_idx]
i_l, j_l = upper_tri[0][least_idx], upper_tri[1][least_idx]
print(f"\nMost similar: patches {i_m} & {j_m} "
f"(coords {coords[i_m]}, {coords[j_m]}) = {sims_flat[most_idx]:.4f}")
print(f"Least similar: patches {i_l} & {j_l} "
f"(coords {coords[i_l]}, {coords[j_l]}) = {sims_flat[least_idx]:.4f}")
return sim_matrix
def plot_results(sim_matrix, coords, patches, embeddings):
"""Visualize similarity matrix, patches, and embedding space."""
fig, axes = plt.subplots(1, 3, figsize=(18, 5))
im = axes[0].imshow(sim_matrix, cmap="RdYlGn", vmin=-1, vmax=1)
axes[0].set_title("Pairwise Cosine Similarity")
axes[0].set_xlabel("Patch Index")
axes[0].set_ylabel("Patch Index")
plt.colorbar(im, ax=axes[0])
n_show = min(8, len(patches))
rgb_strip = []
for i in range(n_show):
rgb = patches[i][[2, 1, 0]]
rgb = np.clip(rgb / 3000, 0, 1)
rgb_strip.append(rgb.transpose(1, 2, 0))
if rgb_strip:
combined = np.concatenate(rgb_strip, axis=1)
axes[1].imshow(combined)
axes[1].set_title(f"First {n_show} patches (RGB)")
axes[1].axis("off")
from sklearn.decomposition import PCA
if len(embeddings) >= 3:
pca = PCA(n_components=2)
emb_2d = pca.fit_transform(embeddings)
scatter = axes[2].scatter(
emb_2d[:, 0],
emb_2d[:, 1],
c=range(len(emb_2d)),
cmap="viridis",
s=60,
edgecolors="k",
linewidths=0.5,
)
axes[2].set_title("Embeddings (PCA 2D)")
axes[2].set_xlabel("PC1")
axes[2].set_ylabel("PC2")
plt.colorbar(scatter, ax=axes[2], label="Patch index")
plt.tight_layout()
output_path = "clay_similarity_results.png"
plt.savefig(output_path, dpi=150, bbox_inches="tight")
print(f"\nVisualization saved to: {output_path}")
plt.close()
def main():
print("=" * 60)
print("Clay Foundation Model - GeoAI Cosine Similarity Validation")
print("=" * 60)
device = "cpu"
if torch.cuda.is_available():
device = "cuda"
elif torch.backends.mps.is_available():
device = "mps"
print(f"Device: {device}")
metadata = load_metadata()
image, scene_datetime = fetch_sentinel2_data()
patches, coords = create_patches(image)
if len(patches) < 2:
print("Need at least 2 valid patches. Try a larger region.")
sys.exit(1)
max_patches = 16
if len(patches) > max_patches:
print(f"Using first {max_patches} patches for efficiency.")
patches = patches[:max_patches]
coords = coords[:max_patches]
patches_normalized = normalize_patches(patches, metadata)
embeddings = generate_embeddings(
patches_normalized, metadata, scene_datetime, device=device
)
sim_matrix = compute_and_validate_similarity(embeddings, coords, patches)
plot_results(sim_matrix, coords, patches, embeddings)
print("\nDone!")
if __name__ == "__main__":
main()