Land Cover Classification using Sentinel2 and Deep Learning

Intro

This post is about getting to know LULC classificaiton using Sentinel 2 based on deep learing.

Sentinel 2

Sentinel-2 is a mission of two twin satellites, promoted by the European Space Agency, travelling around the Globe, completing a full circle every ten days. The two satellites are flying in the same orbit but phased at 180°, which means that they co-operate to provide freshly captured imagery every five days. One is called S2A and its twin is called S2B1!

Data Download

1
wget --content-disposition --continue --user={mengyuchi} --password={5212109mengle} "https://scihub.copernicus.eu/dhus/odata/v1/Products('9586b4cc-c2bf-4efb-8ee7-87f2ff6f2089')/$value"

Download with aria2c

  1. download product.meta4

  2. cd to the folder that you want to place

  3. aria2c --http-user=mengyuchi --http-passwd=5212109mengle --check-certificate=false --max-concurrent-downloads=2 -M products.meta4

Download with python

  1. Setup python environment for python

  2. Open Anaconda powershell prompt, install sentinelsat package4

1
pip install sentinalsat

Sen2Cor

Sen2Cor in SNAP

  1. Unzip Sentinel2 file

  2. Install Sen2Cor in SNAP Tools

  3. Optical\Thematic Land Processing\Sen2Cor

  4. I\O Parameter: Select .xml file in D:\Data\Sentinel2\xuzhou\Original\S2A_MSIL1C_20161206T030112_N0204_R032_T50SMC_20161206T030749.SAFE\MTD_MSIL1C.xml

  5. Processing Parameter: Resolution → All

  6. Wait for processing

Sen2Cor in cmd

  1. Download Sen2Cor v2.10

  2. Unzip

  3. Open dir in command prompt

  4. Copy Sentinel2 L1C data file path L2A_Process.bat I:\Data\Sentinel2\xuzhou\L1C\S2A_MSIL1C_20200320T025541_N0209_R032_T50SMC_20200320T060846.SAFE

  5. Run command L2A_Process.bat I:\Data\Sentinel2\xuzhou\L1C\S2A_MSIL1C_20200320T025541_N0209_R032_T50SMD_20200320T060846.SAFE

  6. Wait around 10 mins.

  7. Check results in SNAP or QGIS.

Sen2Cor using bat script

  1. Create new txt file in folder E:\Code\Sentinel2_L1C_to_L2A

  2. Copy these code in txt file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
title Sentinel-2A L1C bat script


set "Sen2cor_bin=C:\Users\mengy\.snap\auxdata\Sen2Cor-02.10.01-win64"

rem set "input_dir=E:\DATA\LULC\Sentinel2\L1C"

set "input_dir=E:\DATA\L1C_temp"

set "output_dir=E:\DATA\LULC\Sentinel2\L2A"

for /d %%i in (%input_dir%\S2*.SAFE ) do (
%Sen2cor_bin%\L2A_Process.bat %%i --output_dir=%output_dir% --resolution=10
echo "--------------------------------------------------------------"
)
echo "All Done"
  1. Save as Sen2cor_weak.bat

  2. Open Windows powershell, cd to Dir E:\Code\Sentinel2_L1C_to_L2A and run ./Sen2cor_weak.bat

  3. Error Sen2cor 2.10.01 - Product metadata file cannot be read with 14.2 baseline L1C

The solution to this error: using Sen2Cor-2.05.05-win64 to process sentinel 2 data that produced before 20176.

  1. Output dir for data before 2017 is E:\DATA\LULC\Sentinel2\L1C\Before_2017, and output dir for data after 2017 is E:\DATA\LULC\Sentinel2\L2A

Sen2Res in SNAP

  1. Get Extent of AOI in ArcGIS
  • Extent for Xuzhou_center in deg
1
2
3
4
Top: 34.585279
Bottom: 34.022282
Left: 116.811255
Right: 117.713315
  • Extent for feng_pei in deg
1
2
3
4
Top: 34.975770
Bottom: 34.406956
Left: 116.355183
Right: 117.144540
  • Geo Coordinates setting:
1
2
3
4
S2A_*SMC_*: South and West -> 34.022282 & 116.811255 
S2A_*SMD_*: North and West -> 34.585279 & 116.811255
S2A_*SNC_*: South and East -> 34.022282 & 117.713315
S2A_*SND_*: North and East -> 34.585279 & 117.713315

Code for subset

1
2
3
4
S2A_*SMC_*: North 34.337, East 117.104, South 34.022 116.811 POLYGON ((34.337 117.104, 34.337 116.811, 34.022 116.811, 34.022 117.104, 34.337 117.104))
S2A_*SMD_*:
S2A_*SNC_*:
S2A_*SND_*:
  1. Rastersubset

  2. OpticalSentinel2 super-resolution

  3. Edit Geo Coordinates, I/O Parameters → Directory in panel

Sen2Res parameters

Sen2Res parameters

  1. Run, this would take a while

  2. Total time spend for processing: 01:48:46.160

Mosaic

  1. Raster → Geometric → Mosaicking

Subset in SNAP

Due to long processing time of Sen2Res, we have to clip our original L2A product into sub-areas2. We use subset tool in SNAP for this process.

  1. Open multi L2A products in SNAP.

overview of dataset

  1. RasterSubset

Using SNAP-GPT tool to process Sentinel2 data

Setup

GPT for Resampling and subset

LULC classification using lstm-sentinel2-landcover

Code for Deep Learning for Land Cover Change Detection5

Python environment setup

  1. conda create -n n_env_py37 python=3.7

  2. conda activate n_env_py37

  3. cd to folder C:\Users\mengy\Documents\VS workspace\LSTM_Sentinel2_land_cover\lstm-sentinel2-landcover

  4. conda config --env --add channels conda-forge pip install numpy

  5. conda install -c conda-forge fire

  6. conda create -n tf tensorflow conda activate tf

  7. conda install -c conda-forge segmentation-models-pytorch

  8. conda install -c conda-forge scikit-learn

  9. conda install -c conda-forge scikit-image

  10. conda install -c conda-forge tqdm

  11. conda install -c conda-forge gdal

  12. conda install -c conda-forge rasterio

Data preparation and placement

  • Sentinel images: data/raw/sentinel/

  • Ground Truth shapefile: data/raw/ground_truth/gt_shapefile/

  • shapefile of area of interest (AOI) = shape of GT as shapefile: data/raw/ground_truth/overallshape/

Pre-processing

Example models are in src/models/, the data generators used for training are in src/helpers/image_functions.py.

  • baseline_cnn.py is a simple FCN that trains on single images at a time (i.e. no sequences). It can be used as a pretrained base model for the LSTM models, as shown in e.g. lstm_fixed_seq.py.

  • lstm_fixed_seq.py is a FCN+LSTM that trains with sequences of images, however the images that build the sequence are fixed (``.

  • lstm_random_seq.py is also a FCN+LSTM that randomly builds a new sequence of images for each training batch.

Trained models are saved in models/. Tensorboard logs are saved in separate subdirectories of reports/logs/; this way they can be called via tensorboard --logdir=reports/logs (when in main directory).

Sentinel-2 10 m Land Use/Land Cover Time series Downloader

This application provides access to individual 10-meter resolution GeoTIFF scenes for all land masses on the planet, for each year from 2017-20213.

All scenes for each year are also available to download as a zip file: 2017, 2018, 2019, 2020, 2021.

Each annual zip download is approximately 60 GB of global extent.