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

"to_scl_dilation_mask" does not work as intended with filter_spatial #715

Closed
GriffinBabe opened this issue Mar 8, 2024 · 10 comments
Closed
Assignees

Comments

@GriffinBabe
Copy link

GriffinBabe commented Mar 8, 2024

When executing the "to_scl_dilation_mask" process and using the filter_spatial(geometry), undesired results arrive. The array contains NaN, 1.0 and very few 0.0 values.

Here is how the extracted scl dilated mask look like, other dates from the datacube yield the same result
Image

The process graph is the following:
Image

I managed to reproduce the issue with this minimal example:

import openeo

connection = openeo.connect('http://openeo-staging.dataspace.copernicus.eu')
connection.authenticate_oidc()

cube = connection.load_collection(
    'SENTINEL2_L2A',
    bands=['B04', 'B03', 'B02', 'SCL'],
    temporal_extent=["2020-01-01", "2020-06-01"],
)

geometry = connection.load_url(
    'https://artifactory.vgt.vito.be/artifactory/auxdata-public/gfmap-temp/openeogfmap_dataframe_0.parquet',
    format="Parquet"
)

cube = cube.filter_spatial(geometry)

scl_mask = cube.process(
        "to_scl_dilation_mask",
        data=cube.filter_bands(["SCL"]),
        scl_band_name="SCL",
        kernel1_size=17,  # 17px dilation on a 20m layer
        kernel2_size=77,   # 77px dilation on a 20m layer
        mask1_values=[2, 4, 5, 6, 7],
        mask2_values=[3, 8, 9, 10, 11],
        erosion_kernel_size=3
).rename_labels("bands", ["SCL_DILATED_MASK"])

cube = cube.merge_cubes(scl_mask)
# cube = cube.linear_scale_range(0, 65534, 0, 65534)


job = cube.create_job(
    out_format='NetCDF',
    title="To_scl_dilation",
    job_options = {
        "executor-memory": "5G",
        "executor-memoryOverhead": "2G",
    }
)

job.start_and_wait()
@GriffinBabe GriffinBabe changed the title Throw an error when providing more than one band (SCL) to the "to_scl_dilation_mask" process "to_scl_dilation_mask" does not work as intended with filter_spatial Mar 8, 2024
@soxofaan soxofaan self-assigned this Mar 8, 2024
@GriffinBabe
Copy link
Author

GriffinBabe commented Mar 11, 2024

  • Upload new geometries on static link with 1 bbox and multiple bbox
  • Make the test before merging the cubes
  • And then with the merging

soxofaan added a commit to Open-EO/openeo-python-driver that referenced this issue Mar 11, 2024
soxofaan added a commit to Open-EO/openeo-python-driver that referenced this issue Mar 11, 2024
soxofaan added a commit to Open-EO/openeo-python-driver that referenced this issue Mar 11, 2024
soxofaan added a commit to Open-EO/openeo-python-driver that referenced this issue Mar 11, 2024
soxofaan added a commit to Open-EO/openeo-python-driver that referenced this issue Mar 11, 2024
soxofaan added a commit to Open-EO/openeo-python-driver that referenced this issue Mar 11, 2024
@GriffinBabe
Copy link
Author

GriffinBabe commented Mar 11, 2024

@soxofaan
I uploaded the geoparquet files with one and three locations in the artifactory
https://artifactory.vgt.vito.be/artifactory/auxdata-public/gfmap-temp/openeogfmap_dataframe_singleloc.parquet
https://artifactory.vgt.vito.be/artifactory/auxdata-public/gfmap-temp/openeogfmap_dataframe_threelocs.parquet

One location, non-merged (cdse-staging j-240311d26137423897c1c73ab3301587)
So far I tested the scl dilated mask without merging it with the optical bands and it seems to work fine, although there are artifacts on the borders due to the convolution so not sure how can this be useful for WorldCereal project:
Image

One location, merged (cdse-staging j-240311ad92974f4b828733e8a3717a13)
Here is the result after we merge the mask with the optical bands. Results are shifted:
Image

Three locations, non-merged
Image

Three locations, merged
Image

@jdries
Copy link
Contributor

jdries commented Mar 12, 2024

FYI, the logs do show that a pixel buffer is applied:

 'pixel_buffer': [38.5, 38.5]}
Loading Sentinel2 with params DataCubeParameters(256, {}, FloatingLayoutScheme, ByDay, 6, None, NearestNeighbor, 38.5, 38.5)

The fact that there's still an edge effect means it's either not applied properly in this case, or not large enough.

@soxofaan
Copy link
Member

FYI, as discussed in our meeting earlier this week:

@jdries jdries assigned jdries and unassigned EmileSonneveld Mar 18, 2024
@jdries
Copy link
Contributor

jdries commented Mar 18, 2024

Reassigned in Emile's absence, will look for unit test.

@jdries
Copy link
Contributor

jdries commented Mar 18, 2024

@GriffinBabe I've been trying to reproduce this. I'm focusing on the case where there are multiple patches, and using sample_by_feature.
In that case, there does not seem to be a border effect, both with and without merging optical bands.
I've used your geoparquet with 3 samples, focusing on date range: ("2020-03-29","2020-04-06")

Can you also try again, to help figure out what part leads to these artifacts?

@GriffinBabe
Copy link
Author

GriffinBabe commented Mar 18, 2024

@jdries
Indeed, when applying sample_by_feature it works (I didn't applied it previously as I was told by Stefaan to not take this into account for the moment)

There is still the issue with the border though. You said previously that I'm applying a pixel buffer of a certain size:

FYI, the logs do show that a pixel buffer is applied:

 'pixel_buffer': [38.5, 38.5]}
Loading Sentinel2 with params DataCubeParameters(256, {}, FloatingLayoutScheme, ByDay, 6, None, NearestNeighbor, 38.5, 38.5)

The fact that there's still an edge effect means it's either not applied properly in this case, or not large enough.

Is this related to the size of the polygons? I suppose this is, because that's the only moment in the pipeline where I am choosing a buffer, but the polygon size is 64x64 pixels, so it should be 32.0, not 38.5

What I am getting so far:

Image

@jdries
Copy link
Contributor

jdries commented Mar 18, 2024

@GriffinBabe can you share the code again that produces this? Because I don't get the border.

The buffer is calculated based on the largest kernel size, so that the convolution is applied correctly. This is what normally should prevent border effects. The value of 38.5 is correct for a kernel size of 77.

@GriffinBabe
Copy link
Author

This is the code:

import openeo

connection = openeo.connect('http://openeo-staging.dataspace.copernicus.eu')
connection.authenticate_oidc()

cube = connection.load_collection(
    'SENTINEL2_L2A',
    bands=['B04', 'B03', 'B02', 'SCL'],
    temporal_extent=["2020-01-01", "2020-06-01"],
)

geometry = connection.load_url(
    'https://artifactory.vgt.vito.be/artifactory/auxdata-public/gfmap-temp/openeogfmap_dataframe_threelocs.parquet',
    format="Parquet"
)

cube = cube.filter_spatial(geometry)

scl_mask = cube.process(
        "to_scl_dilation_mask",
        data=cube.filter_bands(["SCL"]),
        scl_band_name="SCL",
        kernel1_size=17,  # 17px dilation on a 20m layer
        kernel2_size=77,   # 77px dilation on a 20m layer
        mask1_values=[2, 4, 5, 6, 7],
        mask2_values=[3, 8, 9, 10, 11],
        erosion_kernel_size=3
).rename_labels("bands", ["SCL_DILATED_MASK"])

cube = cube.merge_cubes(scl_mask)
# cube = scl_mask
# cube = cube.linear_scale_range(0, 65534, 0, 65534)


job = cube.create_job(
    out_format='NetCDF',
    title="To_scl_dilation",
    job_options = {
        "executor-memory": "5G",
        "executor-memoryOverhead": "2G",
    },
    sample_by_feature=True,
)

job.start_and_wait()

@jdries
Copy link
Contributor

jdries commented Mar 18, 2024

Thanks, now I spotted it: you do the filter_spatial before to_mask_scl_dilation. Problem is that this process actually needs neighbouring pixels, but filter_spatial will remove them, so output is no longer correct.
Solution is to simply move filter_spatial after mask dilation process, that seems to work for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants