Skip to content

Run inference on a Sentinel-2 image from the Google Earth Engine

  • Last Modified: 30-08-2024
  • Author: Gonzalo Mateo-García

Logo CloudSEN12

This notebook shows how to download a Sentinel-2 image and run the CloudSEN12 model over an image downloaded from the Google Earth Engine. Hence to run this notebook you a Google Earth Engine account.

We will run the models UnetMobV2_V1 and UnetMobV2_V2 with all bands proposed in:

> Aybar, C., Ysuhuaylas, L., Loja, J., Gonzales, K., Herrera, F., Bautista, L., Yali, R., Flores, A., Diaz, L., Cuenca, N., Espinoza, W., Prudencio, F., Llactayo, V., Montero, D., Sudmanns, M., Tiede, D., Mateo-García, G., & Gómez-Chova, L. (2022). CloudSEN12, a global dataset for semantic understanding of cloud and cloud shadow in Sentinel-2. Scientific Data, 9(1), Article 1. https://doi.org/10.1038/s41597-022-01878-2 > > Aybar, C., Bautista, L., Montero, D., Contreras, J., Ayala, D., Prudencio, F., Loja, J., Ysuhuaylas, L., Herrera, F., Gonzales, K., Valladares, J., Flores, L. A., Mamani, E., Quiñonez, M., Fajardo, R., Espinoza, W., Limas, A., Yali, R., Alcántara, A., Leyva, M., Loayza-Muro, M., Willems, M., Mateo-García, G. & Gómez-Chova, L. (2024). CloudSEN12+: The largest dataset of expert-labeled pixels for cloud and cloud shadow detection in Sentinel-2. Data in Brief, 110852. https://doi.org/10.1016/j.dib.2024.110852 > >

!pip install georeader-spaceml cloudsen12_models earthengine-api
from cloudsen12_models import cloudsen12
import ee
import matplotlib.pyplot as plt
from georeader import plot
from shapely.geometry import box
from georeader.readers import ee_image
/home/gonzalo/mambaforge/envs/georeader3/lib/python3.10/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html
  from .autonotebook import tqdm as notebook_tqdm

# ee.Authenticate()
ee.Initialize()
collection_name = "COPERNICUS/S2_HARMONIZED"
tile = "S2A_MSIL1C_20240417T064631_N0510_R020_T40RCN_20240417T091941"
img_col = ee.ImageCollection(collection_name)
image = img_col.filter(ee.Filter.eq("PRODUCT_ID", tile)).first()
info_img = image.getInfo()
info_img
{'type': 'Image',
 'bands': [{'id': 'B1',
   'data_type': {'type': 'PixelType',
    'precision': 'int',
    'min': 0,
    'max': 65535},
   'dimensions': [1830, 1830],
   'crs': 'EPSG:32640',
   'crs_transform': [60, 0, 300000, 0, -60, 2800020]},
  {'id': 'B2',
   'data_type': {'type': 'PixelType',
    'precision': 'int',
    'min': 0,
    'max': 65535},
   'dimensions': [10980, 10980],
   'crs': 'EPSG:32640',
   'crs_transform': [10, 0, 300000, 0, -10, 2800020]},
  {'id': 'B3',
   'data_type': {'type': 'PixelType',
    'precision': 'int',
    'min': 0,
    'max': 65535},
   'dimensions': [10980, 10980],
   'crs': 'EPSG:32640',
   'crs_transform': [10, 0, 300000, 0, -10, 2800020]},
  {'id': 'B4',
   'data_type': {'type': 'PixelType',
    'precision': 'int',
    'min': 0,
    'max': 65535},
   'dimensions': [10980, 10980],
   'crs': 'EPSG:32640',
   'crs_transform': [10, 0, 300000, 0, -10, 2800020]},
  {'id': 'B5',
   'data_type': {'type': 'PixelType',
    'precision': 'int',
    'min': 0,
    'max': 65535},
   'dimensions': [5490, 5490],
   'crs': 'EPSG:32640',
   'crs_transform': [20, 0, 300000, 0, -20, 2800020]},
  {'id': 'B6',
   'data_type': {'type': 'PixelType',
    'precision': 'int',
    'min': 0,
    'max': 65535},
   'dimensions': [5490, 5490],
   'crs': 'EPSG:32640',
   'crs_transform': [20, 0, 300000, 0, -20, 2800020]},
  {'id': 'B7',
   'data_type': {'type': 'PixelType',
    'precision': 'int',
    'min': 0,
    'max': 65535},
   'dimensions': [5490, 5490],
   'crs': 'EPSG:32640',
   'crs_transform': [20, 0, 300000, 0, -20, 2800020]},
  {'id': 'B8',
   'data_type': {'type': 'PixelType',
    'precision': 'int',
    'min': 0,
    'max': 65535},
   'dimensions': [10980, 10980],
   'crs': 'EPSG:32640',
   'crs_transform': [10, 0, 300000, 0, -10, 2800020]},
  {'id': 'B8A',
   'data_type': {'type': 'PixelType',
    'precision': 'int',
    'min': 0,
    'max': 65535},
   'dimensions': [5490, 5490],
   'crs': 'EPSG:32640',
   'crs_transform': [20, 0, 300000, 0, -20, 2800020]},
  {'id': 'B9',
   'data_type': {'type': 'PixelType',
    'precision': 'int',
    'min': 0,
    'max': 65535},
   'dimensions': [1830, 1830],
   'crs': 'EPSG:32640',
   'crs_transform': [60, 0, 300000, 0, -60, 2800020]},
  {'id': 'B10',
   'data_type': {'type': 'PixelType',
    'precision': 'int',
    'min': 0,
    'max': 65535},
   'dimensions': [1830, 1830],
   'crs': 'EPSG:32640',
   'crs_transform': [60, 0, 300000, 0, -60, 2800020]},
  {'id': 'B11',
   'data_type': {'type': 'PixelType',
    'precision': 'int',
    'min': 0,
    'max': 65535},
   'dimensions': [5490, 5490],
   'crs': 'EPSG:32640',
   'crs_transform': [20, 0, 300000, 0, -20, 2800020]},
  {'id': 'B12',
   'data_type': {'type': 'PixelType',
    'precision': 'int',
    'min': 0,
    'max': 65535},
   'dimensions': [5490, 5490],
   'crs': 'EPSG:32640',
   'crs_transform': [20, 0, 300000, 0, -20, 2800020]},
  {'id': 'QA10',
   'data_type': {'type': 'PixelType',
    'precision': 'int',
    'min': 0,
    'max': 65535},
   'crs': 'EPSG:4326',
   'crs_transform': [1, 0, 0, 0, 1, 0]},
  {'id': 'QA20',
   'data_type': {'type': 'PixelType',
    'precision': 'int',
    'min': 0,
    'max': 4294967295},
   'crs': 'EPSG:4326',
   'crs_transform': [1, 0, 0, 0, 1, 0]},
  {'id': 'QA60',
   'data_type': {'type': 'PixelType',
    'precision': 'int',
    'min': 0,
    'max': 65535},
   'dimensions': [1830, 1830],
   'crs': 'EPSG:32640',
   'crs_transform': [60, 0, 300000, 0, -60, 2800020]},
  {'id': 'MSK_CLASSI_OPAQUE',
   'data_type': {'type': 'PixelType',
    'precision': 'int',
    'min': 0,
    'max': 255},
   'dimensions': [1830, 1830],
   'crs': 'EPSG:32640',
   'crs_transform': [60, 0, 300000, 0, -60, 2800020]},
  {'id': 'MSK_CLASSI_CIRRUS',
   'data_type': {'type': 'PixelType',
    'precision': 'int',
    'min': 0,
    'max': 255},
   'dimensions': [1830, 1830],
   'crs': 'EPSG:32640',
   'crs_transform': [60, 0, 300000, 0, -60, 2800020]},
  {'id': 'MSK_CLASSI_SNOW_ICE',
   'data_type': {'type': 'PixelType',
    'precision': 'int',
    'min': 0,
    'max': 255},
   'dimensions': [1830, 1830],
   'crs': 'EPSG:32640',
   'crs_transform': [60, 0, 300000, 0, -60, 2800020]}],
 'version': 1725002640294788,
 'id': 'COPERNICUS/S2_HARMONIZED/20240417T064631_20240417T070110_T40RCN',
 'properties': {'DATATAKE_IDENTIFIER': 'GS2A_20240417T064631_046063_N05.10',
  'SPACECRAFT_NAME': 'Sentinel-2A',
  'RADIO_ADD_OFFSET_B8A': -1000,
  'MEAN_INCIDENCE_AZIMUTH_ANGLE_B8A': 134.743585255399,
  'RADIO_ADD_OFFSET_B10': -1000,
  'MEAN_SOLAR_AZIMUTH_ANGLE': 125.349533937834,
  'system:footprint': {'type': 'LinearRing',
   'coordinates': [[56.10367704603996, 25.314098248016826],
    [56.10366453265447, 25.314098890784862],
    [55.01337712640577, 25.303437154556363],
    [55.01333189229982, 25.303400042009695],
    [55.013281865761044, 25.303368247094216],
    [55.01327892302438, 25.303353436659044],
    [55.02125293865599, 24.807931098196637],
    [55.029016967685884, 24.312481156119308],
    [55.02905775321592, 24.312440193542837],
    [55.02909256809631, 24.31239490323549],
    [55.029108882320564, 24.31239232210239],
    [56.1107707065852, 24.322582474698056],
    [56.11081591149119, 24.322619338079825],
    [56.11086588379534, 24.3226507181641],
    [56.110868863405464, 24.3226655559011],
    [56.107364142267, 24.818357912166473],
    [56.103764810036296, 25.31400950922035],
    [56.10372403509453, 25.314050713644857],
    [56.10368930405189, 25.314096188484182],
    [56.10367704603996, 25.314098248016826]]},
  'SOLAR_IRRADIANCE_B12': 85.25,
  'SOLAR_IRRADIANCE_B10': 367.15,
  'SENSOR_QUALITY': 'PASSED',
  'SOLAR_IRRADIANCE_B11': 245.59,
  'GENERATION_TIME': 1713345581000,
  'RADIO_ADD_OFFSET_B12': -1000,
  'RADIO_ADD_OFFSET_B11': -1000,
  'SOLAR_IRRADIANCE_B8A': 955.32,
  'FORMAT_CORRECTNESS': 'PASSED',
  'CLOUD_COVERAGE_ASSESSMENT': 26.764549553585,
  'system:time_end': 1713337364342,
  'SNOW_PIXEL_PERCENTAGE': 0.00268744961031981,
  'system:time_start': 1713337364342,
  'RADIO_ADD_OFFSET_B1': -1000,
  'RADIO_ADD_OFFSET_B2': -1000,
  'DATASTRIP_ID': 'S2A_OPER_MSI_L1C_DS_2APS_20240417T091941_S20240417T070110_N05.10',
  'RADIO_ADD_OFFSET_B3': -1000,
  'RADIO_ADD_OFFSET_B4': -1000,
  'RADIO_ADD_OFFSET_B5': -1000,
  'PROCESSING_BASELINE': '05.10',
  'SENSING_ORBIT_NUMBER': 20,
  'RADIO_ADD_OFFSET_B6': -1000,
  'RADIO_ADD_OFFSET_B7': -1000,
  'SENSING_ORBIT_DIRECTION': 'DESCENDING',
  'GENERAL_QUALITY': 'PASSED',
  'GRANULE_ID': 'L1C_T40RCN_A046063_20240417T070110',
  'REFLECTANCE_CONVERSION_CORRECTION': 0.994777427364766,
  'MEAN_INCIDENCE_AZIMUTH_ANGLE_B8': 139.681287791114,
  'DATATAKE_TYPE': 'INS-NOBS',
  'MEAN_INCIDENCE_AZIMUTH_ANGLE_B9': 133.740738443782,
  'MEAN_INCIDENCE_AZIMUTH_ANGLE_B6': 135.599841609209,
  'MEAN_INCIDENCE_AZIMUTH_ANGLE_B7': 134.803767428332,
  'MEAN_INCIDENCE_AZIMUTH_ANGLE_B4': 136.819391209735,
  'MEAN_INCIDENCE_ZENITH_ANGLE_B1': 3.4699741268305,
  'RADIO_ADD_OFFSET_B8': -1000,
  'MEAN_INCIDENCE_AZIMUTH_ANGLE_B5': 136.157391224414,
  'RADIOMETRIC_QUALITY': 'PASSED',
  'RADIO_ADD_OFFSET_B9': -1000,
  'MEAN_INCIDENCE_AZIMUTH_ANGLE_B2': 141.092821944531,
  'MEAN_INCIDENCE_AZIMUTH_ANGLE_B3': 138.449840786385,
  'MEAN_INCIDENCE_ZENITH_ANGLE_B5': 3.15091982605653,
  'MEAN_INCIDENCE_AZIMUTH_ANGLE_B1': 134.379856175339,
  'MEAN_INCIDENCE_ZENITH_ANGLE_B4': 3.08006281838802,
  'MEAN_INCIDENCE_ZENITH_ANGLE_B3': 2.96278001824222,
  'MEAN_INCIDENCE_ZENITH_ANGLE_B2': 2.8579748625795,
  'MEAN_INCIDENCE_ZENITH_ANGLE_B9': 3.56288630670834,
  'MEAN_INCIDENCE_ZENITH_ANGLE_B8': 2.90182732293627,
  'MEAN_INCIDENCE_ZENITH_ANGLE_B7': 3.30685872829666,
  'MEAN_INCIDENCE_ZENITH_ANGLE_B6': 3.22591554030377,
  'MEAN_SOLAR_ZENITH_ANGLE': 22.6291255460394,
  'MEAN_INCIDENCE_ZENITH_ANGLE_B8A': 3.38761375959026,
  'MGRS_TILE': '40RCN',
  'CLOUDY_PIXEL_PERCENTAGE': 26.7645495535848,
  'PRODUCT_ID': 'S2A_MSIL1C_20240417T064631_N0510_R020_T40RCN_20240417T091941',
  'MEAN_INCIDENCE_ZENITH_ANGLE_B10': 3.04036914084227,
  'SOLAR_IRRADIANCE_B9': 812.92,
  'DEGRADED_MSI_DATA_PERCENTAGE': 0.0226,
  'MEAN_INCIDENCE_ZENITH_ANGLE_B11': 3.21424033876458,
  'MEAN_INCIDENCE_ZENITH_ANGLE_B12': 3.40704653978779,
  'SOLAR_IRRADIANCE_B6': 1287.61,
  'MEAN_INCIDENCE_AZIMUTH_ANGLE_B10': 136.718941327782,
  'SOLAR_IRRADIANCE_B5': 1424.64,
  'MEAN_INCIDENCE_AZIMUTH_ANGLE_B11': 135.050999538939,
  'SOLAR_IRRADIANCE_B8': 1041.63,
  'MEAN_INCIDENCE_AZIMUTH_ANGLE_B12': 134.030121466282,
  'SOLAR_IRRADIANCE_B7': 1162.08,
  'SOLAR_IRRADIANCE_B2': 1959.66,
  'SOLAR_IRRADIANCE_B1': 1884.69,
  'SOLAR_IRRADIANCE_B4': 1512.06,
  'GEOMETRIC_QUALITY': 'PASSED',
  'SOLAR_IRRADIANCE_B3': 1823.24,
  'system:asset_size': 1443626992,
  'system:index': '20240417T064631_20240417T070110_T40RCN'}}
%%time

# projgee = image.select("B2").projection().getInfo()

aoi = box(55.325, 25.225, 55.415, 25.28)

bands = ['B1', 'B2', 'B3', 'B4', 'B5', 'B6', 'B7', 'B8', 'B8A', 'B9', 'B10', 'B11', 'B12']
crs = info_img["bands"][1]["crs"]
transform = info_img["bands"][1]["crs_transform"]
projgee = {"crs": crs, "transform": transform}
# img_local = ee_image.export_image_fast(image=image, geometry=aoi)
img_local = ee_image.export_image_getpixels(asset_id=info_img['id'],
                                            proj=projgee,
                                            bands_gee=bands,
                                            geometry=aoi)
img_local
Warning 1: TIFFReadDirectory:Sum of Photometric type-related color channels and ExtraSamples doesn't match SamplesPerPixel. Defining non-color channels as ExtraSamples.

CPU times: user 404 ms, sys: 85.3 ms, total: 489 ms
Wall time: 4.99 s

 
         Transform: | 10.00, 0.00, 331260.00|
| 0.00,-10.00, 2797010.00|
| 0.00, 0.00, 1.00|
         Shape: (13, 622, 916)
         Resolution: (10.0, 10.0)
         Bounds: (331260.0, 2790790.0, 340420.0, 2797010.0)
         CRS: EPSG:32640
         fill_value_default: 0.0
        
swirnirred = (img_local.isel({"band": [bands.index(b) for b in ["B11","B8","B4"]]}) / 4_500.).clip(0,1)

plot.show(swirnirred)
<Axes: >
No description has been provided for this image

UNetMobV2_V1

Model trained on the first version of CloudSEN12 Aybar et al. 2022.

model = cloudsen12.load_model_by_name(name="UNetMobV2_V1", weights_folder="cloudsen12_models")
cloudsen12.MODELS_CLOUDSEN12.keys()
dict_keys(['cloudsen12', 'UNetMobV2_V1', 'UNetMobV2_V2', 'cloudsen12l2a', 'dtacs4bands', 'landsat30'])
cloudmask = model.predict(img_local/10_000)
fig, ax = plt.subplots(1,2,figsize=(14,5),sharey=True, tight_layout=True)

plot.show(swirnirred,ax=ax[0])
cloudsen12.plot_cloudSEN12mask(cloudmask,ax=ax[1])
fig.savefig("example_flood_dubai_2024.png")
No description has been provided for this image

UnetMobV2_V2

Model trained on the second version of CloudSEN12 (called CloudSEN12+) Aybar et al. 2024.

modelv2 = cloudsen12.load_model_by_name(name="UNetMobV2_V2", weights_folder="cloudsen12_models")
cloudmaskv2 = modelv2.predict(img_local/10_000)
fig, ax = plt.subplots(1,2,figsize=(14,5),sharey=True, tight_layout=True)

plot.show(swirnirred,ax=ax[0])
cloudsen12.plot_cloudSEN12mask(cloudmaskv2,ax=ax[1])
<Axes: >
No description has been provided for this image

Licence

The cloudsen12_models package is published under a GNU Lesser GPL v3 licence

The CloudSEN12 database and all pre-trained models are released under a Creative Commons non-commercial licence. For using the models in comercial pipelines written consent by the authors must be provided.

This notebook is released under a Creative Commons non-commercial licence.

If you find this work useful please cite:

@article{aybar_cloudsen12_2024,
    title = {{CloudSEN12}+: {The} largest dataset of expert-labeled pixels for cloud and cloud shadow detection in {Sentinel}-2},
    issn = {2352-3409},
    url = {https://www.sciencedirect.com/science/article/pii/S2352340924008163},
    doi = {10.1016/j.dib.2024.110852},
    journal = {Data in Brief},
    author = {Aybar, Cesar and Bautista, Lesly and Montero, David and Contreras, Julio and Ayala, Daryl and Prudencio, Fernando and Loja, Jhomira and Ysuhuaylas, Luis and Herrera, Fernando and Gonzales, Karen and Valladares, Jeanett and Flores, Lucy A. and Mamani, Evelin and Quiñonez, Maria and Fajardo, Rai and Espinoza, Wendy and Limas, Antonio and Yali, Roy and Alcántara, Alejandro and Leyva, Martin and Loayza-Muro, Rau´l and Willems, Bram and Mateo-García, Gonzalo and Gómez-Chova, Luis},
    month = aug,
    year = {2024},
    pages = {110852},
}

@article{aybar_cloudsen12_2022,
    title = {{CloudSEN12}, a global dataset for semantic understanding of cloud and cloud shadow in {Sentinel}-2},
    volume = {9},
    issn = {2052-4463},
    url = {https://www.nature.com/articles/s41597-022-01878-2},
    doi = {10.1038/s41597-022-01878-2},
    number = {1},
    urldate = {2023-01-02},
    journal = {Scientific Data},
    author = {Aybar, Cesar and Ysuhuaylas, Luis and Loja, Jhomira and Gonzales, Karen and Herrera, Fernando and Bautista, Lesly and Yali, Roy and Flores, Angie and Diaz, Lissette and Cuenca, Nicole and Espinoza, Wendy and Prudencio, Fernando and Llactayo, Valeria and Montero, David and Sudmanns, Martin and Tiede, Dirk and Mateo-García, Gonzalo and Gómez-Chova, Luis},
    month = dec,
    year = {2022},
    pages = {782},
}