Skip to content

Commit 0e7cb0c

Browse files
Merge pull request #22 from sentinel-hub/develop
Migration to TF2 and addition of time-series architectures
2 parents fd42a73 + f510f51 commit 0e7cb0c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+2547
-5924
lines changed

Diff for: LICENSE

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
MIT License
3+
4+
Copyright (c) 2017-2020 Matej Aleksandrov, Matej Batič, Matic Lubej, Grega Milčinski (Sinergise)
5+
Copyright (c) 2017-2020 Devis Peressutti, Jernej Puc, Anže Zupanc, Lojze Žust, Jovan Višnjić (Sinergise)
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in all
15+
copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
SOFTWARE.

Diff for: MODELS.md

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Models
2+
3+
## Fully-Convolutional-Network (FCN)
4+
5+
The vanilla architecture as in the following figure is implemented. Convolutions are run along spatial dimensions of input tensor, which is supposed to have `[M, H, W, D]` shape, where M is the mini-batch size, and H, W and D are the height, width and number of bands (i.e. depth) of the input image tensor. The 2d convolutions perform a `VALID` convolution, therefore the output tensor size is smaller than the input size.
6+
7+
![FCN](./figures/fcn-architecture.png "FCN")
8+
9+
An example training script is provided. To run it execute the `configs/fcn_example.json` configuration:
10+
```
11+
python -m eoflow.execute configs/fcn_example.json
12+
```
13+
14+
The example configuration can be used as a base to run your own experiments.
15+
16+
## Temporal Fully-Convolutional-Network (TFCN)
17+
18+
Similarly to the RFCN, the TFCN works with time-series of input shape `[M, T, H, W, D]`. This network performs 3d convolutions along the tempo-spatial dimensions, i.e. the convolutional kernels are 3d `k x k x k`. As default, the temporal dimension is not pooled. For temporal pooling, enough time-frames need to be available in the input tensors. At the bottom of the TFCN and along the skip connections, a 1d convolution along the temporal dimension is performed to linearly combine the temporal features. The resulting tensors are 4d of shape `[M, H, W, D]`. The decoding path is as in FCN.
19+
20+
![TFCN](./figures/tfcn-architecture.png "TFCN")
21+
22+
An example training script is provided. To run it execute the `configs/tfcn_example.json` configuration:
23+
```
24+
python -m eoflow.execute configs/tfcn_example.json
25+
```
26+
27+
The example configuration can be used as a base to run your own experiments.
28+
29+
## Recurrent Fully-Convolutional-Network (RFCN)
30+
31+
A recurrent version of the **FCN** is implemented as in below figure. The input tensor in this case is 5d with shape `[M, T, H, W, D]`, where `T` is the number of temporal acquisitions. As for the FCN, the 2d convolutions operate along the `H` and `W` dimensions. The recurrent layers are applied along the skip connections and the bottom layers to model the temporal relationship between the features extracted by the 2d convolutions. The output of the recurrent layers is a 4d tensor of shape `[M, H, W, D]` (the height, width and depth of the tensors will vary along the network). The decoding path is as in **FCN**. The 2d convolutions perform a `VALID` convolution, therefore the output tensor size is smaller than the input size.
32+
33+
![RFCN](./figures/rfcn-architecture.png "RFCN")
34+
35+
An example training script is provided. To run it execute the `configs/rfcn_example.json` configuration:
36+
```
37+
python -m eoflow.execute configs/rfcn_example.json
38+
```
39+
40+
The example configuration can be used as a base to run your own experiments.

Diff for: README.md

+37-72
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
# EOFlow
22

3-
This repository provides a templated structure to generate TensorFlow projects. Using the same base structure for all TF projects should benefit model creation, debugging and experiments reproducibility.
3+
This repository provides code and examples for creation of Earth Observation (EO) projects using TensorFlow. The code uses TensorFlow 2.0 with Keras as the main model building API.
44

5-
The project contains the package `eoflow` which contains the base abstract classes and implements the common models, tasks and input methods. Custom models tasks and input methods can also be implemented building on top of the provided abstract classes.
5+
Common model architectures, layers, and input methods for EO tasks are provided in the package `eoflow`. Custom models and input methods can also be implemented building on top of the provided abstract classes. This package aims at seamlessly integrate with [`eo-learn`](https://github.com/sentinel-hub/eo-learn), and favours both creation of models for prototypying as well as production of EO applications.
6+
7+
Architectures and examples for land cover and crop classification using time-series derived from satellite images are provided.
8+
9+
## Installation
610

711
The package can be installed by running the following command.
812
```
@@ -14,92 +18,53 @@ You can also install the package from source. Clone the repository and run the f
1418
$ pip install .
1519
```
1620

17-
The project also contains example configurations to run common tasks on implemented models. Examples of implementing and running custom models and input methods are also provided.
18-
19-
## Package structure
20-
21-
The structure is inspired by the tensorflow [Estimator API](https://www.tensorflow.org/guide/custom_estimators).
22-
23-
The subpackages of `eoflow` are as follows:
24-
* `base`: this directory contains the abstract classes to build models, inputs and tasks. Any useful abstract class should go in this folder.
25-
* `models`: classes implementing the TF models (e.g. Fully-Convolutional-Network, GANs, seq2seq, ...). These classes inherit and implement the `BaseModel` abstract class.
26-
* `tasks`: classes handling the actions that can be applied to each TF model. These actions may include training, inference, exporting the model, validation, etc. The tasks inherit the `BaseTask` abstract class. Currently only the training task is implemented in `TrainTask` class.
27-
* `input`: these classes handle the loading of the input data into a tf Dataset. These classes may be specific to the problem and data at hand, but can also contain common classes for reading certain type of data (EOPatch, np arrays, etc.). Currently only random input data generation is implemented.
28-
* `utils`: collection of utility functions
29-
30-
## Examples and scripts
31-
32-
Project also contains other folders:
33-
* `configs`: folder containing example configurations for different models. Config parameters are stored in .json files. Results of an experiment should be reproducible by re-running the same config file. Config files specify the whole workflow (model, task, data input if required).
34-
* `examples`: folder containing example implementations of custom models and input functions. Also contains a jupyter notebook example.
35-
36-
## Currently implemented projects
37-
38-
The projects currently implemented are:
39-
* Fully-Convolutional-Network (FCN, a.k.a. U-net), vanilla implementation of method described in this [paper](https://arxiv.org/abs/1505.04597). This network expects 2D MSI images as inputs and predicts 2D label maps as output.
40-
* Recurrent FCN, where a time series is used as input and the temporal dependency between images is modelled by recurrent convolutions. The output of the network is a 2D label map as in previous case.
41-
* Temporal FCN, where the whole time-series is considered as a 3D MSI volume and convolutions are performed along the temporal dimension as well spatial dimension. The output of the network is a 2D label map as in previous cases.
42-
43-
### Fully-Convolutional-Network (FCN)
44-
45-
The vanilla architecture as in the following figure is implemented. Convolutions are run along spatial dimensions of input tensor, which is supposed to have `[M, H, W, D]` shape, where M is the mini-batch size, and H, W and D are the height, width and number of bands (i.e. depth) of the input image tensor. The 2d convolutions perform a `VALID` convolution, therefore the output tensor size is smaller than the input size.
46-
47-
![FCN](./figures/fcn.png "FCN")
48-
49-
An example training script is provided. To run it execute the `configs/fcn_example.json` configuration:
50-
```
51-
python -m eoflow.execute configs/fcn_example.json
52-
```
53-
54-
The example configuration can be used as a base to run your own experiments.
55-
56-
### Recurrent Fully-Convolutional-Network (RFCN)
21+
## Getting started
5722

58-
A recurrent version of the **FCN** is implemented as in below figure. The input tensor in this case is 5d with shape `[M, T, H, W, D]`, where `T` is the number of temporal acquisitions. As for the FCN, the 2d convolutions operate along the `H` and `W` dimensions. The recurrent layers are applied along the skip connections and the bottom layers to model the temporal relationship between the features extracted by the 2d convolutions. The output of the recurrent layers is a 4d tensor of shape `[M, H, W, D]` (the height, width and depth of the tensors will vary along the network). The decoding path is as in **FCN**. The 2d convolutions perform a `VALID` convolution, therefore the output tensor size is smaller than the input size.
23+
The `eoflow` package can be used in two ways. For best control over the workflow and faster prototyping, the package can be used programmatically (in code). The [example notebook](examples/notebook.ipynb) should help you get started with that. It demonstrates how to prepare a dataset pipeline, train the model, evaluate the model and make predictions using the trained model.
5924

60-
![RFCN](./figures/rfcn.png "RFCN")
25+
An alternate way of using `eoflow` is by writing configuration `json` files and running them using `eoflow`'s execute script. Configuration files specify and configure the task (training, evaluation, etc.) and contain the configurations of the model and input methods. Example configurations are provided in the `configs` directory. Once a configuration file is created it can be run using the execute command.
6126

62-
An example training script is provided. To run it execute the `configs/rfcn_example.json` configuration:
27+
A simple example can be run using the following command. More advanced configurations are also provided.
6328
```
64-
python -m eoflow.execute configs/rfcn_example.json
29+
$ python -m eoflow.execute configs/example.json
6530
```
6631

67-
The example configuration can be used as a base to run your own experiments.
68-
69-
### Temporal Fully-Convolutional-Network (TFCN)
70-
71-
Similarly to the RFCN, the TFCN works with time-series of input shape `[M, T, H, W, D]`. This network performs 3d convolutions along the tempo-spatial dimensions, i.e. the convolutional kernels are 3d `k x k x k`. As default, the temporal dimension is not pooled. For temporal pooling, enough time-frames need to be available in the input tensors. At the bottom of the TFCN and along the skip connections, a 1d convolution along the temporal dimension is performed to linearly combine the temporal features. The resulting tensors are 4d of shape `[M, H, W, D]`. The decoding path is as in FCN.
72-
73-
![TFCN](./figures/tfcn.png "TFCN")
32+
This will create an output folder `temp/experiment` containing the tensorboard logs and model checkpoints.
7433

75-
An example training script is provided. To run it execute the `configs/tfcn_example.json` configuration:
34+
To visualize the logs in TensorBoard, run
7635
```
77-
python -m eoflow.execute configs/tfcn_example.json
36+
$ tensorboard --logdir=temp/experiment
7837
```
7938

80-
The example configuration can be used as a base to run your own experiments.
39+
## Writing custom code
8140

82-
## Custom models and input functions
41+
To get started with writing custom models and input methods for `eoflow` take a look at the example implementations ([`examples` folder](examples/)). Custom classes use schemas to define the configuration parameters in order to work with the execute script and configuration files. Since eoflow builds on top of TF2 and Keras, model building is very similar.
8342

84-
In order to create your own model, create a new class that inherits from `BaseModel` or any of its subclasses. The model must specify a schema for its configuration and a function `build_model` that builds the model with the provided input and label tensors. Look at the example implementation for more details.
43+
## Package structure
8544

86-
The package `eoflow` contains a few of the most common input methods. It also provides common dataset building blocks: EOPatch loading, subpatch extraction, data augmentation. You can use these functions to create a custom input method that fits you data and model. To implement a custom input method create a new class that inherits from `BaseInput` or any of its subclasses. The input method must specify a schema for its configuration and a function `get_dataset` that builds a tensorflow Dataset that reads the input data. For further details look at the example implementation.
45+
The subpackages of `eoflow` are as follows:
46+
* `base`: this directory contains the abstract classes to build models, inputs and tasks. Any useful abstract class should go in this folder.
47+
* `models`: classes implementing the TF models (e.g. Fully-Convolutional-Network, GANs, seq2seq, ...). These classes inherit and implement the `BaseModel` abstract class. The module also contains custom losses, metrics and layers.
48+
* `tasks`: classes handling the configurable actions that can be applied to each TF model, when using the execute script. These actions may include training, inference, exporting the model, validation, etc. The tasks inherit the `BaseTask` abstract class.
49+
* `input`: building blocks and helper methods for loading the input data (EOPatch, numpy arrays, etc.) into a tensoflow Dataset and applying different transformations (data augmentation, patch extraction)
50+
* `utils`: collection of utility functions
8751

88-
Implementations in files `examples/models.py` and `examples/input.py` are available as guidelines on how the configurable classes for **models** and **input** should be implemented.
52+
### Examples and scripts
8953

90-
A toy example using the example model and input is configured in the`configs/example.json` configuration file.
54+
Project also contains other folders:
55+
* `configs`: folder containing example configurations for different models. Config parameters are stored in .json files. Results of an experiment should be reproducible by re-running the same config file. Config files specify the whole workflow (model, task, data input if required).
56+
* `examples`: folder containing example implementations of custom models and input functions. Also contains a jupyter notebook example.
9157

92-
To run the example, run
93-
```
94-
$ python -m eoflow.execute configs/example.json
95-
```
96-
This will create an output folder `temp/experiment` containing the tensorboard logs and model checkpoints.
58+
## Implemented architectures
9759

98-
To visualise the logs in TensorBoard, run
99-
```
100-
$ tensorboard --logdir=temp/experiment
101-
```
60+
Segmentation models for land cover semantic segmentation:
61+
* **Fully-Convolutional-Network (FCN, a.k.a. U-net)**, vanilla implementation of method described in this [paper](https://arxiv.org/abs/1505.04597). This network expects 2D MSI images as inputs and predicts 2D label maps as output.
62+
* **Temporal FCN**, where the whole time-series is considered as a 3D MSI volume and convolutions are performed along the temporal dimension as well spatial dimension. The output of the network is a 2D label map as in previous cases. More details can be found in this [paper](https://www.researchgate.net/publication/333262625_Spatio-Temporal_Deep_Learning_An_Application_to_Land_Cover_Classification).
10263

103-
## Programatic use
64+
Classification models for crop classification using time-series:
65+
* **TCN**: Implementation of the TCN network taken from the [keras-TCN implementation by Philippe Remy](https://github.com/philipperemy/keras-tcn).
66+
* **TempCNN**: Implementation of the TempCNN network taken from the [temporalCNN implementation of Charlotte Pelletier](https://github.com/charlotte-pel/temporalCNN).
67+
* **Recurrent NN**: Implementation of (bidirectional) Recurrent Neural Networks for the classification of time-series. Implementation allows to use either `SimpleRNN`, `GRU` or `LSTM` layers as building blocks of the architecture.
68+
* **TransformerEncoder**: Implementation of a time-series classification architecture based on [self-attention](https://arxiv.org/abs/1706.03762) layers. This implementation follows [this PyTorch implementation of Marc Russwurm](https://github.com/MarcCoru/crop-type-mapping).
10469

105-
Notebook `examples/notebook.ipnyb` shows how `eoflow` can be also used in code. The notebook shows model building, defining the input datasets, training, evaluation and prediction.
70+
Descriptions and examples of semantic segmentation architectures are available [here](MODELS.md).

Diff for: configs/example.json

+4-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"config": {
55
"output_size": 10,
66
"hidden_units": 256,
7-
"learning_rate": 0.01
7+
"learning_rate": 0.001
88
}
99
},
1010
"task": {
@@ -18,13 +18,12 @@
1818
"input_shape": [512],
1919
"num_classes": 10,
2020
"batch_size": 20,
21-
"batches_per_epoch": 1000
21+
"batches_per_epoch": 200
2222
}
2323
},
24+
"iterations_per_epoch": 200,
2425
"save_steps": 400,
25-
"summary_steps": 50,
26-
"progress_steps": 100
26+
"summary_steps": 50
2727
}
2828
}
2929
}
30-

Diff for: configs/example_val.json

+3-5
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"input_shape": [512],
2020
"num_classes": 10,
2121
"batch_size": 20,
22-
"batches_per_epoch": 1000
22+
"batches_per_epoch": 200
2323
}
2424
},
2525
"val_input_config":{
@@ -28,13 +28,11 @@
2828
"input_shape": [512],
2929
"num_classes": 10,
3030
"batch_size": 1,
31-
"batches_per_epoch": 1000
31+
"batches_per_epoch": 200
3232
}
3333
},
3434
"save_steps": 400,
35-
"summary_steps": 50,
36-
"progress_steps": 100
35+
"summary_steps": 50
3736
}
3837
}
3938
}
40-

Diff for: configs/fcn_example.json

+5-5
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
"bias_init": 0.0,
1616
"padding": "VALID",
1717
"pool_size": 2,
18-
"pool_stride": 2
18+
"pool_stride": 2,
19+
"loss": "focal_loss",
20+
"metrics": ["accuracy"]
1921
}
2022
},
2123
"task": {
@@ -30,13 +32,11 @@
3032
"output_shape": [128, 128],
3133
"num_classes": 3,
3234
"batch_size": 2,
33-
"batches_per_epoch": 1000
35+
"batches_per_epoch": 200
3436
}
3537
},
3638
"save_steps": 100,
37-
"summary_steps": 50,
38-
"progress_steps": 1
39+
"summary_steps": 50
3940
}
4041
}
4142
}
42-

0 commit comments

Comments
 (0)