isaaccorley commited on
Commit
7c972a7
·
verified ·
1 Parent(s): cc86187

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +123 -3
README.md CHANGED
@@ -1,3 +1,123 @@
1
- ---
2
- license: cc-by-4.0
3
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: cc-by-4.0
3
+ tags:
4
+ - climate
5
+ pretty_name: aef-eurosat
6
+ size_categories:
7
+ - 10K<n<100K
8
+ ---
9
+
10
+ This is an export of Google's AlphaEarth embeddings which align with the EuroSAT Sentinel-2 patches from roughly sometime during 2018.
11
+
12
+ The code to reproduce the dataset download is below (requires a GEE project and login):
13
+
14
+ ```python
15
+ import os
16
+ import argparse
17
+ import concurrent.futures
18
+ from concurrent.futures import ThreadPoolExecutor
19
+ from pathlib import Path
20
+ from functools import partial
21
+ import shutil
22
+
23
+ import ee
24
+ import xarray as xr
25
+ import rioxarray as rio
26
+ from torchgeo.datasets import EuroSAT
27
+ from tqdm import tqdm
28
+
29
+
30
+ def quantize_aef(image):
31
+ """quantize float64 -> uint8"""
32
+ power = 2.0
33
+ scale = 127.5
34
+ min_value = -127
35
+ max_value = 127
36
+
37
+ sat = image.abs().pow(ee.Number(1.0).divide(power)).multiply(image.signum())
38
+ snapped = sat.multiply(scale).round()
39
+ return snapped.clamp(min_value, max_value).add(ee.Number(127)).uint8()
40
+
41
+
42
+ def download_aef(filepath, output, year, collection):
43
+ try:
44
+ raster = rio.open_rasterio(filepath)
45
+ raster = raster.rio.reproject("EPSG:4326")
46
+ bounds = raster.rio.bounds() # must by in EPSG:4326
47
+ startDate = ee.Date.fromYMD(year, 1, 1)
48
+ endDate = startDate.advance(1, "year")
49
+ geometry = ee.Geometry.BBox(*bounds)
50
+ image = (
51
+ collection.filter(ee.Filter.date(startDate, endDate))
52
+ .filter(ee.Filter.bounds(geometry))
53
+ .first()
54
+ )
55
+ ds = xr.open_dataset(
56
+ quantize_aef(image),
57
+ engine="ee",
58
+ geometry=bounds,
59
+ projection=image.select(0).projection(),
60
+ )
61
+ ds = ds.isel(time=0)
62
+ ds = ds.rename({"X": "x", "Y": "y"})
63
+ ds = ds.to_array(dim="band").transpose("band", "y", "x")
64
+ output_path = os.path.join(output, filepath.stem + ".tif")
65
+ ds.rio.to_raster(output_path, driver="COG", compress="deflate", dtype="uint8")
66
+ except Exception as e:
67
+ return filepath, str(e)
68
+ return filepath, None
69
+
70
+
71
+ def main(args):
72
+ ee.Authenticate()
73
+ ee.Initialize(
74
+ project=args.project,
75
+ opt_url="https://earthengine-highvolume.googleapis.com",
76
+ )
77
+ ee.data.setWorkloadTag(args.workload_tag)
78
+ collection = ee.ImageCollection("GOOGLE/SATELLITE_EMBEDDING/V1/ANNUAL")
79
+
80
+ filepaths = []
81
+ for split in ["train", "val", "test"]:
82
+ ds = EuroSAT(root=args.root, split=split, download=True, checksum=True)
83
+ filepaths.extend([Path(img) for img, _ in ds.imgs])
84
+
85
+ with open("errors.csv", "w") as f:
86
+ f.write("filepath,error\n")
87
+
88
+ os.makedirs(args.output, exist_ok=True)
89
+
90
+ with ThreadPoolExecutor(max_workers=args.num_workers) as executor:
91
+ func = partial(
92
+ download_aef, output=args.output, year=args.year, collection=collection
93
+ )
94
+ futures = [executor.submit(func, filepath) for filepath in filepaths]
95
+
96
+ for future in tqdm(
97
+ concurrent.futures.as_completed(futures), total=len(filepaths)
98
+ ):
99
+ filepath, error = future.result()
100
+ if error:
101
+ with open("errors.csv", "a") as f:
102
+ f.write(f"{filepath},{error}\n")
103
+
104
+ # Reorganize images into folders (optional)
105
+ images = list(Path(args.output).glob("*.tif"))
106
+ print(len(images))
107
+ for image in tqdm(images):
108
+ folder = image.parent / image.stem.split("_")[0]
109
+ folder.mkdir(parents=True, exist_ok=True)
110
+ shutil.move(image, folder / image.name)
111
+
112
+
113
+ if __name__ == "__main__":
114
+ parser = argparse.ArgumentParser()
115
+ parser.add_argument("--year", type=int, default=2018)
116
+ parser.add_argument("--output", type=str, default="eurosat-aef")
117
+ parser.add_argument("--num_workers", type=int, default=32)
118
+ parser.add_argument("--root", type=str, default="data")
119
+ parser.add_argument("--project", type=str)
120
+ parser.add_argument("--workload_tag", type=str, default="aef-eurosat")
121
+ args = parser.parse_args()
122
+ main(args)
123
+ ```