Skip to content

Commit

Permalink
Merge pull request #9 from earthnet2021/quickfix
Browse files Browse the repository at this point in the history
fix sen2nbar errors, cloud mask rescaling
  • Loading branch information
vitusbenson authored Mar 27, 2023
2 parents 4607785 + 4d541ba commit 2e29d21
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 15 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import earthnet_minicuber as emc
specs = {
"lon_lat": (43.598946, 3.087414), # center pixel
"xy_shape": (256, 256), # width, height of cutout around center pixel
"resolution": 20, # in meters.. will use this on a local UTM grid..
"resolution": 10, # in meters.. will use this on a local UTM grid..
"time_interval": "2021-07-01/2021-07-31",
"providers": [
{
Expand Down Expand Up @@ -94,6 +94,7 @@ Kwargs:
- `five_daily_filter`: If `True` returns a regular 5-daily cycle starting with the first date in `full_time_interval`. It has no effect, if `best_orbit_filter` is used.
- `brdf_correction`: If `True`, does BRDF correction based on the Sentinel 2 Metadata (illumination angles).
- `cloud_mask`: If `True`, creates a cloud and cloud shadow mask based on deep learning. It automatically finds the best available cloud mask for the requested `bands`.
- `cloud_mask_rescale_factor`: If using cloud mask and a lower resolution than 10m, set this rescaling factor to the multiple of 10m that you are requesting. E.g. if `resolution = 20`, set `cloud_mask_rescale_factor = 2`.
- `correct_processing_baseline`: If `True` (default): corrects the shift of +1000 that exists in Sentinel 2 data with processing baseline >= 4.0


Expand Down
2 changes: 1 addition & 1 deletion earthnet_minicuber/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""EarthNet Minicuber"""

__version__ = "0.1.2"
__version__ = "0.1.3"
__author__ = "Vitus Benson"


Expand Down
16 changes: 13 additions & 3 deletions earthnet_minicuber/provider/s2/cloudmask.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ def get_checkpoint(bands_avail):


class CloudMask:
def __init__(self, bands = ["B02", "B03", "B04", "B8A"]):
def __init__(self, bands = ["B02", "B03", "B04", "B8A"], cloud_mask_rescale_factor = None):

self.cloud_mask_rescale_factor = cloud_mask_rescale_factor
self.bands = bands
ckpt, self.ckpt_bands = get_checkpoint(bands)

Expand Down Expand Up @@ -63,12 +64,21 @@ def __call__(self, stack):

x = torch.nn.functional.pad(x, (w_pad_left, w_pad_right, h_pad_left, h_pad_right), mode = "reflect")

if self.cloud_mask_rescale_factor:
#orig_size = (x.shape[-2], x.shape[-1])
x = torch.nn.functional.interpolate(x, scale_factor = self.cloud_mask_rescale_factor, mode = 'bilinear')

with torch.no_grad():
y_hat = self.model(x)

y_hat = y_hat[:, :, h_pad_left:-h_pad_right, w_pad_left:-w_pad_right]
y_hat = torch.argmax(y_hat, dim = 1).float()

if self.cloud_mask_rescale_factor:
y_hat = torch.nn.functional.max_pool2d(y_hat[:,None,...], kernel_size = self.cloud_mask_rescale_factor)[:,0,...]#torch.nn.functional.interpolate(y_hat, size = orig_size, mode = "bilinear")

y_hat = y_hat[:, h_pad_left:-h_pad_right, w_pad_left:-w_pad_right]

ds["mask"] = (("time", "y", "x"),torch.argmax(y_hat, dim = 1).float().cpu().numpy())
ds["mask"] = (("time", "y", "x"),y_hat.cpu().numpy())

return ds.to_array("band")

Expand Down
17 changes: 10 additions & 7 deletions earthnet_minicuber/provider/s2/nbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,16 @@ def call_sen2nbar(stack, items, epsg):
# Compute the c-factor per item and extract the processing baseline
c_array = []
for item in ordered_items:#tqdm(ordered_items, disable=quiet):
c = c_factor_from_item(item, f"epsg:{epsg}")
c = c.interp(
y=stack.y.values,
x=stack.x.values,
method="linear",
kwargs={"fill_value": "extrapolate"},
)
try:
c = c_factor_from_item(item, f"epsg:{epsg}")
c = c.interp(
y=stack.y.values,
x=stack.x.values,
method="linear",
kwargs={"fill_value": "extrapolate"},
)
except ValueError:
c = xr.DataArray(np.full((9,len(stack.y), len(stack.x)), np.NaN), coords = {"band": ['B02','B03','B04','B05','B06','B07','B08','B11','B12'], "y": stack.y, "x": stack.x}, dims = ("band", "y", "x"))
c_array.append(c)

orig_bands = stack.band.values.tolist()
Expand Down
7 changes: 5 additions & 2 deletions earthnet_minicuber/provider/s2/sentinel2.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@

class Sentinel2(provider_base.Provider):

def __init__(self, bands = ["AOT", "B01", "B02", "B03", "B04", "B05", "B06", "B07", "B08", "B8A", "B09", "B11", "B12", "WVP"], best_orbit_filter = True, five_daily_filter = False, brdf_correction = True, cloud_mask = True, aws_bucket = "planetary_computer", s2_avail_var = True, correct_processing_baseline = True):
def __init__(self, bands = ["AOT", "B01", "B02", "B03", "B04", "B05", "B06", "B07", "B08", "B8A", "B09", "B11", "B12", "WVP"], best_orbit_filter = True, five_daily_filter = False, brdf_correction = True, cloud_mask = True, cloud_mask_rescale_factor = None, aws_bucket = "planetary_computer", s2_avail_var = True, correct_processing_baseline = True):

self.is_temporal = True

self.cloud_mask = CloudMask(bands=bands) if cloud_mask else None
self.cloud_mask = CloudMask(bands=bands, cloud_mask_rescale_factor = cloud_mask_rescale_factor) if cloud_mask else None

if self.cloud_mask and "SCL" not in bands:
bands += ["SCL"]
Expand Down Expand Up @@ -201,6 +201,9 @@ def load_data(self, bbox, time_interval, **kwargs):

stack = stack.sel(time = stack.time.dt.date.isin(dates))

if len(stack.time) == 0:
return None

if self.correct_processing_baseline:
stack = correct_processing_baseline(stack, items_s2)

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@


setup(name='earthnet-minicuber',
version='0.1.2',
version='0.1.3',
description="EarthNet Minicuber",
author="Vitus Benson, Christian Requena-Mesa",
author_email="[email protected]",
Expand Down

0 comments on commit 2e29d21

Please sign in to comment.