-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #77 from WorldCereal/57-udf-memory-profiling
57 udf memory profiling
- Loading branch information
Showing
3 changed files
with
89 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -179,4 +179,5 @@ catboost_info/catboost_training.json | |
*.tar | ||
*.zip | ||
|
||
.notebook-tests/ | ||
.notebook-tests/ | ||
.local-presto-test/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
"""Perform cropland mapping inference using a local execution of presto. | ||
Make sure you test this script on the Python version 3.9+, and have worldcereal | ||
dependencies installed with the presto wheel file installed with it's dependencies. | ||
""" | ||
|
||
from pathlib import Path | ||
|
||
import requests | ||
import xarray as xr | ||
from openeo_gfmap.features.feature_extractor import ( | ||
EPSG_HARMONIZED_NAME, | ||
apply_feature_extractor_local, | ||
) | ||
from openeo_gfmap.inference.model_inference import apply_model_inference_local | ||
|
||
from worldcereal.openeo.feature_extractor import PrestoFeatureExtractor | ||
from worldcereal.openeo.inference import CroplandClassifier | ||
|
||
TEST_FILE_URL = "https://artifactory.vgt.vito.be/artifactory/auxdata-public/worldcereal/presto/localtestdata/local_presto_inputs.nc" | ||
TEST_FILE_PATH = Path.cwd() / "presto_test_inputs.nc" | ||
PRESTO_URL = "https://artifactory.vgt.vito.be/artifactory/auxdata-public/worldcereal-minimal-inference/presto.pt" | ||
CATBOOST_URL = "https://artifactory.vgt.vito.be/artifactory/auxdata-public/worldcereal-minimal-inference/wc_catboost.onnx" | ||
|
||
if __name__ == "__main__": | ||
if not TEST_FILE_PATH.exists(): | ||
print("Downloading test input data...") | ||
# Download the test input data | ||
with requests.get(TEST_FILE_URL, stream=True, timeout=180) as response: | ||
response.raise_for_status() | ||
with open(TEST_FILE_PATH, "wb") as f: | ||
for chunk in response.iter_content(chunk_size=8192): | ||
f.write(chunk) | ||
|
||
print("Loading array in-memory...") | ||
arr = ( | ||
xr.open_dataset(TEST_FILE_PATH) | ||
.to_array(dim="bands") | ||
.drop_sel(bands="crs") | ||
.astype("uint16") | ||
) | ||
|
||
print("Running presto UDF locally") | ||
features = apply_feature_extractor_local( | ||
PrestoFeatureExtractor, | ||
arr, | ||
parameters={ | ||
EPSG_HARMONIZED_NAME: 32631, | ||
"ignore_dependencies": True, | ||
"presto_model_url": PRESTO_URL, | ||
}, | ||
) | ||
|
||
features.to_netcdf(Path.cwd() / "presto_test_features_cropland.nc") | ||
|
||
print("Running classification inference UDF locally") | ||
|
||
classification = apply_model_inference_local( | ||
CroplandClassifier, | ||
features, | ||
parameters={ | ||
EPSG_HARMONIZED_NAME: 32631, | ||
"ignore_dependencies": True, | ||
"classifier_url": CATBOOST_URL, | ||
}, | ||
) | ||
|
||
classification.to_netcdf(Path.cwd() / "test_classification_cropland.nc") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters