Home

Awesome

đŸ“Ļ Segmentation Gym :muscle:

Last Commit Maintenance Wiki GitHub Wiki

Python TensorFlow Keras

<!-- ![](https://user-images.githubusercontent.com/3596509/153691733-1fe98e37-5379-4122-8d02-adbcb0ab0db3.png) -->

gym

:scroll: Paper

Earth ArXiv Preprint
DOI

Buscombe, D., & Goldstein, E. B. (2022). A reproducible and reusable pipeline for segmentation of geoscientific imagery. Earth and Space Science, 9, e2022EA002332. https://doi.org/10.1029/2022EA002332

New in May 2023

make_datasets (as well as doodleverse_utils\make_mndwi_dataset and doodleverse_utils\make_ndwi_dataset) now works in a new way. Before, all files were read in, shuffled, split into train and val sets, then non-augmented and augmented npz files were created for each set. This causes a potential data leak between train and validation subsets, and validation was carried out on augmented imagery. We introduced a clunky 'mode' config parameter to try to control the degree of use of augmentation.

From May 29, 2023, make_datasets creates train_data and val_data subfolders, then copies splits of train and validation labels and images over (multiple bands of images if necessary). It makes non-augmented npzs for each, then makes augmented npzs for the training set only. This removes the potential data leak, and validation is carried out on non-augmented imagery, which is a better reflection of deployment. Like before, make_datasets does not make a test dataset. The test dataset is a domain/task specific problem: please make an independent test set for your problem.

New in February 2023

We now offer a segformer model option in the config file. The Segformer is part of the Huggingface transformers library, and we adapted the keras example to work within the Segmentation Gym framework. This is a transfer-learning-only option, using the mit-b0 set of weights that are fine-tuned on a new dataset.

🌟 Highlights

ℹī¸ Overview

Gym is a toolbox to segment imagery with a variety of a family of UNet models, which are supervised deep-learning models for image segmentation. Gym supports segmentation of image with any number of bands, and any number of classes (memory limited). We have built an end-to-end workflow that facilitates a fully reproducible label-to-model workflow when used in conjunction with companion program Doodler, however pairs of images and corresponding labels however-acquired may be used with Gym.

We have tested on a variety of Earth and environmental imagery of coastal, river, and other natural environments. However, we expect the toolbox to be useful for all types of imagery when properly applied.

✍ī¸ Authors

Package maintainers:

Contributions:

🚀 Usage

This toolbox is designed for 1,3, or 4-band imagery, and supports both binary (one class of interest and a null class) and multiclass (several classes of interest).

We recommend a 6 part workflow:

  1. Download & Install Gym
  2. Decide on which data to use and move them into the appropriate part of the Gym directory structure. (We recommend that you first use the included data as a test of Gym on your machine. After you have confirmed that this works, you can import your own data, or make new data using Doodler)
  3. Write a config file for your data. You will need to make some decisions about the model and hyperparameters.
  4. Run make_dataset.py to augment and package your images into npz files for training the model.
  5. Run train_model.py to train a segmentation model.
  6. Run seg_images_in_folder.py to segment images with your newly trained model, or ensemble_seg_images_in_folder.py to point more than one trained model at the same imagery and ensemble the model outputs

âŦ‡ī¸ Installation

(Feb. 2024 - if these instructions no longer work for you, please submit an Issue)

We advise creating a new conda environment to run the program. We recommend miniconda

  1. Create a conda environment called gym

[OPTIONAL] First you may want to do some conda and pip housekeeping (recommended):

conda update -n base conda
conda clean --all -y
python -m pip install --upgrade pip 

[OPTIONAL] Set mamba to the default installer (recommended - it is faster and more stable):

conda install -n base conda-libmamba-solver
conda config --set solver libmamba

Windows:

  1. you wish to use GPU for model training and the latest Tensorflow version, you now must use WSL2 and refer to the official Tensorflow instructions. These instructions are therefore catered to WSL2 users.

Install miniconda:

sudo apt-get update
sudo apt-get install wget
wget https://repo.anaconda.com/miniconda/Miniconda3-py39_4.12.0-Linux-x86_64.sh
bash Miniconda3-py39_4.12.0-Linux-x86_64.sh
bash

Create the conda environment:

conda create -n gym_gpu python=3.10 -y
conda activate gym_gpu
conda install -c conda-forge cudatoolkit=11.8.0 -y
conda install -c nvidia cuda-nvcc -y

python3 -m pip install nvidia-cudnn-cu11 tensorflow[and-cuda]

Verify the tensorflow GPU install:

python -c "import tensorflow as tf; print(tf.config.list_physical_devices('GPU'))"

For example, if you have 2 nvidia GPUs, you should see something like this:

[PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU'), PhysicalDevice(name='/physical_device:GPU:1', device_type='GPU')]

If you see your GPU listed, then success! Now install the rest of the dependencies:

conda install -c conda-forge scikit-image ipython tqdm pandas natsort matplotlib -y
python -m pip install doodleverse_utils chardet

python -m pip install transformers

Finally, test your transformers library installation:

python  -c "from transformers import TFSegformerForSemanticSegmentation"

If the above returns no error, congratulations! You are in business.

(and if you don't have git installed, conda install git)

Linux/Ubuntu:

conda create -n gym python -y
conda activate gym
pip install tensorflow[and-cuda]

Verify install:

python -c "import tensorflow as tf; print(tf.config.list_physical_devices('GPU'))"

For example, if you have 2 nvidia GPUs, you should see something like this:

[PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU'), PhysicalDevice(name='/physical_device:GPU:1', device_type='GPU')]
conda install -c conda-forge scikit-image ipython tqdm pandas natsort matplotlib -y
python -m pip install doodleverse_utils chardet

python -m pip install transformers

Troubleshooting

If you get conda errors you may need to configure the system paths:

mkdir -p $CONDA_PREFIX/etc/conda/activate.d
echo 'CUDNN_PATH=$(dirname $(python -c "import nvidia.cudnn;print(nvidia.cudnn.__file__)"))' >> $CONDA_PREFIX/etc/conda/activate.d/env_vars.sh
echo 'export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$CONDA_PREFIX/lib/:$CUDNN_PATH/lib' >> $CONDA_PREFIX/etc/conda/activate.d/env_vars.sh
source $CONDA_PREFIX/etc/conda/activate.d/env_vars.sh

From here, you may encounter the following error:

Can't find libdevice directory ${CUDA_DIR}/nvvm/libdevice.
...
Couldn't invoke ptxas --version
...
InternalError: libdevice not found at ./libdevice.10.bc [Op:__some_op]

To fix this error, you will need to run the following commands:

# Install NVCC
conda install -c nvidia cuda-nvcc=11.3.58 -y
# Configure the XLA cuda directory
mkdir -p $CONDA_PREFIX/etc/conda/activate.d
printf 'export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$CONDA_PREFIX/lib/\nexport XLA_FLAGS=--xla_gpu_cuda_data_dir=$CONDA_PREFIX/lib/\n' > $CONDA_PREFIX/etc/conda/activate.d/env_vars.sh
source $CONDA_PREFIX/etc/conda/activate.d/env_vars.sh
# Copy libdevice file to the required path
mkdir -p $CONDA_PREFIX/lib/nvvm/libdevice
cp $CONDA_PREFIX/lib/libdevice.10.bc $CONDA_PREFIX/lib/nvvm/libdevice/

In my case, I also had to link the path to the lib folder in miniconda to LD_LIBRARY_PATH:

ln -sf /usr/lib/x86_64-linux-gnu/libstdc++.so.6 ~/miniconda3/envs/gym/bin/../lib/libstdc++.so.6

If you get errors associated with loading the model weights you may need to:

pip install "h5py==2.10.0" --force-reinstall

and just ignore any errors.

  1. Clone the repo:
git clone --depth 1 https://github.com/Doodleverse/segmentation_gym.git

(--depth 1 means "give me only the present code, not the whole history of git commits" - this saves disk space, and time)

How to use

Check out the wiki for a guide of how to use Gym

  1. Organize your files according to this guide
  2. Create a configuration file according to this guide
  3. Create a model-ready dataset from your pairs of images and labels. We hope you find this guide helpful
  4. Train and evaluate an image segmentation model according to this guide
  5. Deploying / evaluate model on unseen sample imagery tends to be task specific. We offer basic implementation examples here as well as in Segmentation Zoo here and here

Test Dataset

A test data set, including a set of images/labels, model config files, and a dataset and models created with Gym, are available here and described on the zenodo page

You can train a model on the test set using the following commands:

wget https://zenodo.org/records/8170543/files/my_segmentation_gym_datasets_v5.zip
sudo apt-get install unzip
unzip my_segmentation_gym_datasets_v5.zip

then

git clone --depth 1 https://github.com/Doodleverse/segmentation_gym.git
cd segmentation_gym
python train_model.py

First folder to navigate to is this one

image (2)

then this one

image (3)

then the 'segformer' config file

image (4)

If you get an "OOM error", open the config file and reduce the BATCH_SIZE and try again.

💭 Feedback and Contributing

Please read our code of conduct

Please contribute to the Discussions tab - we welcome your ideas and feedback.

We also invite all to open issues for bugs/feature requests using the Issues tab