Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Onnx example #13

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 35 additions & 0 deletions python/OnnxUdf/example.py
@@ -0,0 +1,35 @@
import openeo

# This will be required to import onnxruntime in the UDF.
JeroenVerstraelen marked this conversation as resolved.
Show resolved Hide resolved
dependencies_url = "https://artifactory.vgt.vito.be:443/auxdata-public/openeo/onnx_dependencies.zip"
# You can upload your own model and paste the url here.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This a bit confusing, I don't think you mean that users can upload to artifactory themself.

Suggested change
# You can upload your own model and paste the url here.
# Optionally, put the URL to your own model here

model_url = "https://artifactory.vgt.vito.be:443/auxdata-public/openeo/test_onnx_model.zip"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is apparently a zip file with just one .onnx file. Isn't it possible to just provide this .onnx file?


spatial_extent = dict(zip(["west", "south", "east", "north"], [8.908, 53.791, 8.96, 54.016]))
JeroenVerstraelen marked this conversation as resolved.
Show resolved Hide resolved
temporal_extent = ["2022-10-01", "2022-12-01"]

connection = openeo.connect("openeo.vito.be").authenticate_oidc()

s2_cube = connection.load_collection(
"TERRASCOPE_S2_TOC_V2",
temporal_extent=temporal_extent,
spatial_extent=spatial_extent,
bands=["B04"],
max_cloud_cover=20,
)

udf = openeo.UDF.from_file("onnx_udf.py")
s2_cube = s2_cube.apply_neighborhood(
udf,
size=[{"dimension": "x", "value": 256, "unit": "px"}, {"dimension": "y", "value": 256, "unit": "px"}],
overlap=[{"dimension": "x", "value": 0, "unit": "px"}, {"dimension": "y", "value": 0, "unit": "px"}],
)

# We pass the model and dependencies as zip files to the UDF through the job options.
job_options = {
"udf-dependency-archives": [
f"{dependencies_url}#tmp/extra_venv",
JeroenVerstraelen marked this conversation as resolved.
Show resolved Hide resolved
f"{model_url}#tmp/extra_files",
JeroenVerstraelen marked this conversation as resolved.
Show resolved Hide resolved
],
}
s2_cube.execute_batch("output.nc", job_options=job_options)
22 changes: 22 additions & 0 deletions python/OnnxUdf/onnx_udf.py
@@ -0,0 +1,22 @@
import sys
from openeo.udf import XarrayDataCube
from typing import Dict
import xarray as xr
from openeo.udf.debug import inspect


def apply_datacube(cube: XarrayDataCube, context: Dict) -> XarrayDataCube:
sys.path.insert(0, "tmp/extra_venv")
import onnxruntime as ort
JeroenVerstraelen marked this conversation as resolved.
Show resolved Hide resolved

input_data = cube.get_array().isel(t=0).values # Only perform inference for the first date.
input_data = input_data[None, ...] # Neural network expects shape (1, 1, 256, 256)
inspect(input_data, "input data")
ort_session = ort.InferenceSession("tmp/extra_files/test_model.onnx")
ort_inputs = {ort_session.get_inputs()[0].name: input_data}
ort_outputs = ort_session.run(None, ort_inputs)
output_data = xr.DataArray(ort_outputs[0])
output_data = output_data.rename({"dim_0": "t", "dim_1": "bands", "dim_2": "y", "dim_3": "x"})
inspect(output_data, "output data")

return XarrayDataCube(output_data)