Skip to content

Commit bd3e7f3

Browse files
committed
Merge remote-tracking branch 'origin/fix_coco_scipts'
2 parents 5af8ed1 + 72a6985 commit bd3e7f3

4 files changed

Lines changed: 60 additions & 158 deletions

File tree

README.md

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33

44
**Contributors:** [Sykas D.](https://github.com/dimsyk), [Zografakis D.](https://github.com/dimzog), [Sdraka M.](https://github.com/paren8esis)
55

6-
**This repository contains the models and training scripts for reproducing the experiments presented in [add publication].**
6+
**This repository contains the models and training scripts for reproducing the experiments presented in:**\
7+
[A Sentinel-2 multi-year, multi-country benchmark dataset for crop classification and segmentation with deep learning](https://ieeexplore.ieee.org/document/9749916).
78

89
### Requirements
910

@@ -36,7 +37,6 @@ For OAD:
3637
S4A-models\
3738
L dataset\
3839
L netcdf\
39-
L annotations\
4040
L oad\
4141
L coco_files\
4242
L logs\
@@ -47,18 +47,14 @@ S4A-models\
4747
L mappings\
4848
```
4949

50-
#### Annotations
50+
#### COCO files
5151

52-
In the `dataset/annotations/` folder are the annotations required for exporting the COCO files.
52+
In the `coco_files/` folder are the COCO files required for training, validating and testing the models. Some proof-of-concept COCO files are also given (with the refix `poc_`) just for testing and playing around.
5353

5454
#### NetCDF4 files
5555

5656
In the `dataset/netcdf/` folder you should place the netCDF4 files.
5757

58-
#### COCO files
59-
60-
In the `coco_files/` folder are the COCO files required for training, validating and testing the models. Some proof-of-concept COCO files are also given (with the refix `poc_`) just for testing and playing around.
61-
6258
#### OAD files
6359

6460
In the `dataset/oad/` folder you should place the exported files containing the OAD statistics.
@@ -72,13 +68,13 @@ Every script inherits settings from the aforementioned files.
7268

7369
#### Essential scripts
7470

75-
- `coco_data_split.py`: Uses the nectCDF4 data and the annotations to produce three COCO files for training, validation and testing.
76-
- `export_medians_multi.py`: Uses the netCDF4 data and the COCO files to compute the median image per month.
71+
- `coco_data_split.py`: Uses the nectCDF4 data to produce three COCO files for training, validation and testing.
72+
- `export_medians_multi.py`: Uses the netCDF4 data and the COCO files to compute the median image per month and export them to the disk.
7773
- `compute_class_weights.py`: Computes the class weights based on the exported medians, to account for class imbalance.
7874
- `object-based-csv.py`: Uses the netCDF4 data to compute the statistics required for OAD.
7975
- `pad_experiments.py`: The main script for training/testing the PAD models.
8076
- `oad_experiments.py`: The main script for training/testing the OAD models.
81-
- `visualize_predictions.py`: Produces a visualization of the ground truth and the prediction of a given model for a given image.
77+
- `visualize_predictions.py`: Produces a visualization of the ground truth and the prediction of a given model for a given image. Only relevant for PAD models.
8278

8379
#### Using the repo
8480

@@ -95,7 +91,7 @@ Every script inherits settings from the aforementioned files.
9591
python pad_experiments.py --train --model convlstm --parcel_loss --weighted_loss --root_path_coco <coco_folder_path> --prefix_coco <coco_file_prefix> --prefix <run_prefix> --num_epochs 10 --batch_size 32 --bands B02 B03 B04 B08 --saved_medians --img_size 61 61 --requires_norm --num_workers 16 --num_gpus 1 --window_len 12
9692
```
9793
The above command is for training the **ConvLSTM** model using the **weighted parcel loss** described in the associated publication. Training will continue for **10 epochs** with **batch size 32**, using the Sentinel-2 **bands Blue (B02), Green (B03), Red (B04) and NIR (B08)**. The **input image size is 61x61**, the **precomputed medians are used** to speed up training and all input data are **normalized**. The **window length is 12**, including all months. Please use the `--help` argument to find information on all available parameters.
98-
2. Optionally, run `visualize_predictions.py` to visualize the image, ground truth and prediction for a specific model and image.
94+
2. Optionally, after training run `visualize_predictions.py` to visualize the image, ground truth and prediction for a specific model and image.
9995

10096
**For OAD:**
10197
1. Run `oad_experiments.py` with the appropriate arguments. Example:
@@ -135,3 +131,18 @@ Scenario | Model | Acc. W. (%) | F1 W. (%) | Precision W. (%)
135131
3 | LSTM | **60.60** | **63.96** | **70.55**
136132
3 | Transformer | 51.21 | 56.71 | 67.76
137133
3 | TempCNN | 52.32 | 57.38 | 68.35
134+
135+
### Citation
136+
137+
If you use our work, please cite:
138+
139+
```
140+
@ARTICLE{
141+
9749916,
142+
author={Sykas, Dimitrios and Sdraka, Maria and Zografakis, Dimitrios and Papoutsis, Ioannis},
143+
journal={IEEE Journal of Selected Topics in Applied Earth Observations and Remote Sensing},
144+
title={A Sentinel-2 multi-year, multi-country benchmark dataset for crop classification and segmentation with deep learning},
145+
year={2022},
146+
doi={10.1109/JSTARS.2022.3164771}
147+
}
148+
```

coco_data_split.py

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
from utils.settings.mappings.mappings_cat import SAMPLE_TILES as CAT_TILES
2929
from utils.settings.mappings.mappings_fr import SAMPLE_TILES as FR_TILES
3030
from utils.settings.mappings.encodings_en import CROP_ENCODING
31+
from utils.settings.config import LINEAR_ENCODER
3132

3233
from sklearn.preprocessing import MultiLabelBinarizer
3334
from skmultilearn.model_selection.iterative_stratification import IterativeStratification
@@ -73,6 +74,11 @@ def create_dataframe(data_path, tiles, years, common_labels=None):
7374

7475
unique_labels = set(np.unique(labels.labels.data))
7576

77+
# Keep only labels contained in LINEAR_ENCODER
78+
unique_labels = unique_labels & set(LINEAR_ENCODER.keys())
79+
80+
if (unique_labels) == 0: continue
81+
7682
if common_labels is not None:
7783
# We want the train/val and test tiles to have common labels
7884
if unique_labels.isdisjoint(common_labels): continue
@@ -90,12 +96,8 @@ def create_dataframe(data_path, tiles, years, common_labels=None):
9096

9197
parser.add_argument('--how', type=str, required=True, help='Perform a stratified split.',
9298
choices=['stratified', 'random'])
93-
parser.add_argument('--ann_limit', type=int, required=False, default=None,
94-
help='Maximum number of annotations per patch. Patches exceeding this limit, are dismissed')
9599
parser.add_argument('--data_path', type=str, default='dataset/netcdf/', required=False,
96100
help='The path containing the data in netCDF format. Default "dataset/netcdf/".')
97-
parser.add_argument('--data_ann', type=str, default='dataset/annotations/', required=False,
98-
help='The path containing subfolders with the annotations files. Default "dataset/annotations/".')
99101
parser.add_argument('--coco_path', type=str, default='coco_files/', required=False,
100102
help='The path to export the COCO files into. Default "coco_files/"')
101103
parser.add_argument('--ratio', nargs='+', default=['60', '20', '20'], required=False,
@@ -130,7 +132,6 @@ def create_dataframe(data_path, tiles, years, common_labels=None):
130132
# Define paths
131133
data_path = Path(args.data_path)
132134
coco_path = Path(args.coco_path)
133-
ann_path = Path(args.data_ann)
134135

135136
# Ignore tile/year filtering in case an explicit experiment scheme is selected
136137
if args.experiment is not None:
@@ -253,41 +254,32 @@ def create_dataframe(data_path, tiles, years, common_labels=None):
253254
# Export COCO files
254255
create_coco_dataframe(df=X_train,
255256
path_coco=coco_path / f'{prefix}_coco_train.json',
256-
ann_path=ann_path,
257-
ann_limit=args.ann_limit,
258257
keep_tiles=train_tiles,
259258
keep_years=train_years,
260259
common_labels=common_lbls
261260
)
262261

263262
create_coco_dataframe(df=X_val,
264263
path_coco=coco_path / f'{prefix}_coco_val.json',
265-
ann_path=ann_path,
266-
ann_limit=args.ann_limit,
267264
keep_tiles=train_tiles,
268265
keep_years=train_years,
269266
common_labels=common_lbls
270267
)
271268

272269
create_coco_dataframe(df=X_test,
273270
path_coco=coco_path / f'{prefix}_coco_test.json',
274-
ann_path=ann_path,
275-
ann_limit=args.ann_limit,
276271
keep_tiles=test_tiles,
277272
keep_years=test_years,
278273
common_labels=common_lbls
279274
)
280275

281276
elif args.how == 'random':
282277
create_coco_netcdf(netcdf_path=data_path,
283-
ann_path=ann_path,
284278
path_train=coco_path / f'{prefix}_coco_train.json',
285279
path_test=coco_path / f'{prefix}_coco_test.json',
286280
path_val=coco_path / f'{prefix}_coco_val.json',
287-
having_annotations=False,
288281
train_r=train_r,
289282
val_r=val_r,
290-
ann_limit=args.ann_limit,
291283
keep_tiles=args.tiles,
292284
keep_years=args.years,
293285
experiment=args.experiment,

utils/PAD_dataset.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -328,11 +328,6 @@ def get_labels(self, netcdf: netCDF4.Dataset, start_bin: int) -> np.ndarray:
328328
return labels
329329

330330

331-
def get_annotations(self, coco: COCO) -> dict:
332-
# https://medium.com/fullstackai/how-to-train-an-object-detector-with-your-own-coco-dataset-in-pytorch-319e7090da5
333-
raise NotImplementedError('')
334-
335-
336331
def load_medians(self, path: Path, subpatch_id: int, start_bin: int) -> Tuple[np.ndarray, np.ndarray]:
337332
"""
338333
Loads precomputed medians for requested path.

0 commit comments

Comments
 (0)