Fence Detection with Deep Learning
July 30, 2025 ยท View on GitHub
This repository contains the implementation of various deep learning models configuration to detect fences using multimodal imagery (RGB and DSM). The project, related to the paper Where are the fences? Large-scale fence detection using deep learning and multimodal aerial imagery, explores the impact of different fusion strategies, loss functions, and sampling techniques on model performance. It also includes tools for dataset visualization and evaluation. This work is the first one to address fence detection using aerial imagery.
๐ Features
- Multiple architectures: UNet, UNetLateFusion, UNetConcatenate, and D-LinkNet.
- Multimodal input support (RGB + DSM).
- Flexible loss functions: Dice Loss, Binary Cross-Entropy (BCE), and Combined Loss (Dice + BCE).
- Sampling strategies: random and geographic.
- Tools for dataset visualization and validation.
๐ง Model Architecture
The segmentation models are based on the UNet architecture and its multimodal extensions (e.g., Late Fusion). Below is the structure of the proposed UNetLateFusion model:
๐ Project Structure
โโโ Departments/ # GPKG dataset for geographic stratification (train, val, test)
โโโ LICENSE # Code license
โโโ README.md
โโโ data_loader.py # Data loading pipeline for RGB, DSM, and masks, adapted to each network
โโโ graphical_validation.py # Dataset visualization tool for inspecting masks and features (launch it separately)
โโโ loss.py # Implementation of Dice Loss, BCE, and Combined Loss
โโโ main.py # Script for training models (modify each path/variable in the config function)
โโโ main_test.py # Script for testing and evaluating models (modify each path/variable in the config function)
โโโ model.py # Implementation of UNet, UNetLateFusion, UNetConcatenate, and D-LinkNet
โโโ requirements.txt # Python dependencies
โโโ test.py # Evaluation metrics and patch analysis
โโโ train.py # Training logic and scheduler implementation
๐ฆ Dataset Preparation
1. Input Data Structure
The project expects the following directory structure for the dataset:
/dataset/
โโโ ortho/ # RGB orthophotos
โโโ lidar/ # DSM (Digital Surface Model)
โโโ fences_3m/ # Binary masks (3-meter buffer for fences)
โโโ fences_2m/ # Binary masks (2-meter buffer for fences)
You can download it here: MultiFranceFences
2. Geographic Stratification
The Departments/ folder contains a GeoPackage (GPKG) file, which ensures balanced geographic stratification for training, validation, and testing splits. This is critical for evaluating generalization across regions.
๐ Dataset Visualization
The graphical_validation.py script is designed to help you visually inspect the dataset quality, including masks, RGB, and DSM alignment. Run the script as follows:
python graphical_validation.py
It saves the name of the patch and the tag (valid or not) in a CSV file.
โ๏ธ Training
1. Configurable Parameters
The training script (main.py) is built around a flexible Config class. Key parameters include:
- Models:
UNet,UNetLateFusion,UNetConcatenate,D-LinkNet. - Input modalities: RGB, DSM, or both.
- Sampling strategies: random or geographic.
- Loss functions: Dice Loss, BCE, or Combined Loss.
- Learning rate and scheduler: Supports dynamic learning rate reduction.
The code is commented. You can adapt the class depending on your needs. It is developed in order to use multi-GPU with nn.DataParallel.
2. Running the Training
To start training, modify the Config class in main.py and execute:
python main.py
Model checkpoints are saved in the models/ directory with automatically generated filenames based on the configuration.
๐งช Testing and Evaluation
1. Testing
To evaluate a trained model, use the main_test.py script and modify the path to the .pth file:
python main_test.py
This will:
- Compute predictions and save them as GeoTIFF files.
- Generate classification metrics (precision, recall, IoU, Dice coefficient).
- Identify the best and worst-performing patches.
2. Example Output
The script outputs:
- Evaluation metrics in a text file.
- Visualizations of predictions and ground truths for selected patches.
๐งฐ Requirements
Install dependencies using:
pip install -r requirements.txt
Key libraries:
- PyTorch
- segmentation-models-pytorch
- GDAL
- NumPy
- Matplotlib
๐ Citation
If you use this work, please cite:
@article{wenger2025fences,
title = {Where are the fences? Large-scale fence detection using deep learning and multimodal aerial imagery},
journal = {Remote Sensing Applications: Society and Environment},
volume = {39},
pages = {101658},
year = {2025},
issn = {2352-9385},
doi = {https://doi.org/10.1016/j.rsase.2025.101658},
url = {https://www.sciencedirect.com/science/article/pii/S2352938525002113},
author = {Romain Wenger and Eric Maire and Caryl Buton and Sylvain Moulherat and Cybill Staentzel}
}
๐ License
This project is licensed under the MIT License. See the LICENSE file for details.