Skip to content

Commit bc5aa46

Browse files
committed
Merge remote-tracking branch 'origin/use_fixed_window'
2 parents 88fe2a2 + 5c8491f commit bc5aa46

6 files changed

Lines changed: 40 additions & 8 deletions

File tree

README.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,14 @@ Every script inherits settings from the aforementioned files.
8282
1. Run `export_medians_multi.py` to precompute the medians needed for training, validation and testing.
8383
2. If you don't want to use the given COCO files, then export your own using the `coco_data_split.py` script.
8484
3. Uncomment the precomputed class weights in the corresponding section of the configuration file depending on the scenario you are using (or compute your own).
85-
4. If you want to use a fixed window containing the months April through September, check out the branch `use_fixed_window`.
86-
5. Especially for OAD, run `object-based-csv.py` to export the statistics needed for the experiments.
85+
4. Especially for OAD, run `object-based-csv.py` to export the statistics needed for the experiments.
8786

8887
**For PAD:**
8988
1. Run `pad_experiments.py` with the appropriate arguments. Example:
9089
```
91-
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
90+
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 --fixed_window
9291
```
93-
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.
92+
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**. Finally, a **fixed window** is used containing months 4 (April) through 9 (September). Please use the `--help` argument to find information on all available parameters.
9493
2. Optionally, after training run `visualize_predictions.py` to visualize the image, ground truth and prediction for a specific model and image.
9594

9695
**For OAD:**

compute_class_weights.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
help='The prefix to use for the class weights file. Default none.')
2323
parser.add_argument('--ignore_zero', default=False, action='store_true', required=False,
2424
help='Ignore the zero class.')
25+
parser.add_argument('--fixed_window', action='store_true', default=False, required=False,
26+
help='Use a fixed window including months 4 (April) to 9 (September).')
2527
args = parser.parse_args()
2628

2729
# Define paths
@@ -53,6 +55,7 @@
5355
linear_encoder=LINEAR_ENCODER,
5456
saved_medians=True,
5557
window_len=12,
58+
fixed_window=args.fixed_window,
5659
requires_norm=True,
5760
return_masks=False,
5861
clouds=False,

pad_experiments.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,8 @@ def main():
149149
help='Starting learning rate. Default 1e-1')
150150
parser.add_argument('--window_len', type=int, default=6, required=False,
151151
help='The length of the rolling window to be used. Default 6')
152+
parser.add_argument('--fixed_window', action='store_true', default=False, required=False,
153+
help='Use a fixed window including months 4 (April) to 9 (September).')
152154

153155
parser.add_argument('--bands', nargs='+', default=sorted(list(BANDS.keys())),
154156
help='The image bands to use. Must be space separated')
@@ -388,13 +390,13 @@ def main():
388390
root_path_coco=root_path_coco,
389391
path_train=path_train,
390392
path_val=path_val,
391-
# transforms=transforms,
392393
group_freq=args.group_freq,
393394
prefix=prefix,
394395
bands=args.bands,
395396
linear_encoder=LINEAR_ENCODER,
396397
saved_medians=args.saved_medians,
397398
window_len=args.window_len,
399+
fixed_window=args.fixed_window,
398400
requires_norm=args.requires_norm,
399401
return_masks=args.return_masks,
400402
clouds=args.clouds,
@@ -459,6 +461,7 @@ def main():
459461
linear_encoder=LINEAR_ENCODER,
460462
saved_medians=args.saved_medians,
461463
window_len=args.window_len,
464+
fixed_window=args.fixed_window,
462465
requires_norm=args.requires_norm,
463466
return_masks=args.return_masks,
464467
clouds=args.clouds,

utils/PAD_datamodule.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ def __init__(
3434
linear_encoder: dict = None,
3535
prefix: str = None,
3636
window_len: int = 12,
37+
fixed_window: bool = False,
3738
requires_norm: bool = True,
3839
return_masks: bool = False,
3940
clouds: bool = True,
@@ -81,6 +82,9 @@ def __init__(
8182
over the data. E.g. if `window_len` = 6 and `group_freq` = '1M', then
8283
a 6-month rolling window will be applied and each batch will contain
8384
6 months of training data and the corresponding label.
85+
fixed_window: boolean, default False
86+
If True, then a fixed window including months 4 (April) to 9 (September) is used
87+
instead of a rolling one.
8488
requires_norm: boolean, default True
8589
If True, then it normalizes the dataset to [0, 1] range.
8690
return_masks: boolean, default False
@@ -126,6 +130,7 @@ def __init__(
126130
self.linear_encoder = linear_encoder
127131
self.saved_medians = saved_medians
128132
self.window_len = window_len
133+
self.fixed_window = fixed_window
129134
self.requires_norm = requires_norm
130135
self.return_masks = return_masks
131136
self.clouds = clouds
@@ -190,6 +195,7 @@ def setup(self, stage=None):
190195
linear_encoder=self.linear_encoder,
191196
saved_medians=self.saved_medians,
192197
window_len=self.window_len,
198+
fixed_window=self.fixed_window,
193199
requires_norm=self.requires_norm,
194200
return_masks=self.return_masks,
195201
clouds=self.clouds,
@@ -211,6 +217,7 @@ def setup(self, stage=None):
211217
linear_encoder=self.linear_encoder,
212218
saved_medians=self.saved_medians,
213219
window_len=self.window_len,
220+
fixed_window=self.fixed_window,
214221
requires_norm=self.requires_norm,
215222
return_masks=self.return_masks,
216223
clouds=self.clouds,
@@ -236,6 +243,7 @@ def setup(self, stage=None):
236243
linear_encoder=self.linear_encoder,
237244
saved_medians=self.saved_medians,
238245
window_len=self.window_len,
246+
fixed_window=self.fixed_window,
239247
requires_norm=self.requires_norm,
240248
return_masks=self.return_masks,
241249
clouds=self.clouds,

utils/PAD_dataset.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ def __init__(
5454
linear_encoder: dict = None,
5555
prefix: str = None,
5656
window_len: int = 12,
57+
fixed_window: bool = False,
5758
requires_norm: bool = True,
5859
return_masks: bool = False,
5960
clouds: bool = True,
@@ -94,6 +95,9 @@ def __init__(
9495
over the data. E.g. if `window_len` = 6 and `group_freq` = '1M', then
9596
a 6-month rolling window will be applied and each batch will contain
9697
6 months of training data and the corresponding label.
98+
fixed_window: boolean, default False
99+
If True, then a fixed window including months 4 (April) to 9 (September)
100+
is used instead of a rolling one.
97101
requires_norm: boolean, default True
98102
If True, then it normalizes the dataset to [0, 1] range.
99103
return_masks: boolean, default False
@@ -199,6 +203,7 @@ def __init__(
199203

200204
self.group_freq = group_freq
201205
self.window_len = window_len
206+
self.fixed_window = fixed_window
202207
self.transforms = transforms
203208
self.linear_encoder = linear_encoder
204209

@@ -335,14 +340,25 @@ def load_medians(self, path: Path, subpatch_id: int, start_bin: int) -> Tuple[np
335340
Just load and return
336341
"""
337342
# `medians` is a 4d numpy array (window length, bands, img_size, img_size)
338-
medians = np.empty((self.window_len, self.num_bands, self.output_size[0], self.output_size[1]),
339-
dtype=self.medians_dtype)
343+
if self.fixed_window:
344+
medians = np.empty((6, self.num_bands, self.output_size[0], self.output_size[1]),
345+
dtype=self.medians_dtype)
346+
else:
347+
medians = np.empty((self.window_len, self.num_bands, self.output_size[0], self.output_size[1]),
348+
dtype=self.medians_dtype)
340349

341350
padded_id = f'{str(subpatch_id).rjust(len(str(self.num_subpatches)), "0")}'
342351

343352
median_files = sorted(path.glob(f'sub{padded_id}_bin*'))
344353

345-
for i, bin_idx in enumerate(range(start_bin, start_bin + self.window_len)):
354+
if self.fixed_window:
355+
start_month = 3
356+
end_month = 9
357+
else:
358+
start_month = start_bin
359+
end_month = start_bin + self.window_len
360+
361+
for i, bin_idx in enumerate(range(start_month, end_month)):
346362
median = np.load(median_files[bin_idx]).astype(self.medians_dtype)
347363
medians[i] = median.copy()
348364

visualize_predictions.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ def get_window(idx, window_len, image_size, coco_file):
7171
'Check: https://pandas.pydata.org/pandas-docs/stable/user_guide/timeseries.html#timeseries-offset-aliases ')
7272
parser.add_argument('--window_len', type=int, default=6, required=False,
7373
help='The length of the rolling window to be used. Default 6')
74+
parser.add_argument('--fixed_window', action='store_true', default=False, required=False,
75+
help='Use a fixed window including months 4 (April) to 9 (September).')
7476

7577
parser.add_argument('--bands', nargs='+', default=sorted(list(BANDS.keys())),
7678
help='The image bands to use. Must be space separated')
@@ -181,6 +183,7 @@ def get_window(idx, window_len, image_size, coco_file):
181183
linear_encoder=LINEAR_ENCODER,
182184
saved_medians=args.saved_medians,
183185
window_len=args.window_len,
186+
fixed_window=args.fixed_window,
184187
requires_norm=args.requires_norm,
185188
return_masks=False,
186189
clouds=False,

0 commit comments

Comments
 (0)