Skip to content

Commit 169689e

Browse files
committed
first draft semantic segmentation plugin
1 parent 9693282 commit 169689e

7 files changed

Lines changed: 243 additions & 0 deletions

File tree

1.8 MB
Loading
754 KB
Loading
82.1 KB
Loading
4.74 MB
Loading
16.6 MB
Loading
Lines changed: 242 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,242 @@
1+
.. _navigation2_with_semantic_segmentation:
2+
3+
Navigating with Semantic Segmentation
4+
**************************************
5+
6+
- `Overview`_
7+
- `Requirements`_
8+
- `Semantic Segmentation Overview`_
9+
- `Tutorial Steps`_
10+
- `Conclusion`_
11+
12+
.. image:: images/Navigation2_with_segmentation/video.gif
13+
:width: 90%
14+
:align: center
15+
16+
Overview
17+
========
18+
19+
This tutorial demonstrates how to use semantic segmentation in costmaps with nav2, using a custom `semantic_segmentation_layer plugin <https://github.com/ros-navigation/navigation2_tutorials/tree/master/nav2_semantic_segmentation_layer>`_ and a pre-trained segmentation model that works on gazebo's Baylands world. It was written by Pedro Gonzalez at `robot.com <https://robot.com/>`_.
20+
21+
Requirements
22+
============
23+
24+
It is assumed ROS2 and Nav2 dependent packages are installed or built locally. Additionally, you will need:
25+
26+
.. code-block:: bash
27+
28+
source /opt/ros/<ros2-distro>/setup.bash
29+
sudo apt install ros-$ROS_DISTRO-nav2-minimal-tb4*
30+
sudo apt install ros-$ROS_DISTRO-ros-gz-sim
31+
sudo apt install ros-$ROS_DISTRO-ros-gz-interfaces
32+
33+
You will also need to compile the semantic_segmentation_layer package. To do it, clone the repo to your ros2 workspace source, checkout to the appropriate branch and build the package:
34+
35+
.. code-block:: bash
36+
# on your workspace source replace rolling with your ros distro. branches are available for humble, jazzy and rolling.
37+
git clone -b rolling https://github.com/kiwicampus/semantic_segmentation_layer.git
38+
cd <your workspace path>
39+
colcon build --symlink-install # on your workspace path
40+
41+
The code for this tutorial is hosted in the `nav2_semantic_segmentation_demo <https://github.com/ros-navigation/navigation2_tutorials/tree/master/nav2_semantic_segmentation_demo>`_ package. It's highly recommended that you clone and build this package when setting up your development environment.
42+
43+
Finally, you will need:
44+
45+
- **ONNX Runtime**: For running the semantic segmentation model inference
46+
- **OpenCV**: For image processing
47+
48+
We will install these through the tutorial.
49+
50+
Semantic Segmentation Overview
51+
==============================
52+
53+
What is Semantic Segmentation?
54+
-------------------------------
55+
56+
Semantic segmentation is a computer vision task that assigns a class label to every pixel in an image. Unlike object detection, which identifies and localizes objects with bounding boxes, semantic segmentation provides pixel-level understanding of the scene.
57+
58+
Modern semantic segmentation is typically solved using deep learning, specifically convolutional neural networks (CNNs) or vision transformers. These models are trained on large datasets of images where each pixel has been labeled with its corresponding class.
59+
During training, the model learns to recognize patterns and features that distinguish different classes (e.g., the texture of grass vs. the smooth surface of a sidewalk). Common architectures include U-Net, DDRNet, and SegFormer.
60+
61+
As said above, a pretrained model is included in this tutorial, so you can skip the training part and go directly to the integration with Nav2.
62+
However, if you want to train your own model, you can use the `Simple Segmentation Toolkit <https://github.com/pepisg/simple_segmentation_toolkit>`_ to easily prototype one with SAM-based auto-labeling (no manual annotation required).
63+
64+
.. image:: images/Navigation2_with_segmentation/segmentation_example.png
65+
:width: 600px
66+
:align: center
67+
:alt: Example of semantic segmentation showing original image and segmented mask
68+
69+
Once trained, The output of a semantic segmentation model is typically an image with the same size as the input, where each pixel holds the probability of that pixel belonging to each class.
70+
For instance, in the model provided in this tutorial has 3 classes: sidewalk, grass, and background; hence its raw output is a 3-channel image, where each channel corresponds to the probability of the pixel belonging to each class.
71+
At the end, the class with the highest probability is selected for each pixel, and a confidence value is calculated as the probability of the class that was selected.
72+
73+
A perfectly working model should have a confidence value of 1 for the class that was selected, and 0 for the other classes, however this is rarely the case. Pixels with lower confidence usually correspond to classifications that may be wrong,
74+
for that reason both the class and the confidence are important inputs for deciding how to assign a cost to a pixel, and both are taken into account by the semantic segmentation layer. You can refer to its `README <https://github.com/kiwicampus/semantic_segmentation_layer>`_ for a detailed explanation on how this is done
75+
76+
77+
Tutorial Steps
78+
==============
79+
80+
0- Setup Simulation Environment
81+
------------------------------
82+
83+
To navigate using semantic segmentation, we first need to set up a simulation environment with a robot equipped with a camera sensor. For this tutorial, we will use the Baylands outdoor world in Gazebo with a TurtleBot 4 robot.
84+
Everything is already set up in the `nav2_semantic_segmentation_demo <https://github.com/ros-navigation/navigation2_tutorials/tree/master/nav2_semantic_segmentation_demo>`_ package, so clone the repo and build it if you haven't already:
85+
86+
87+
.. code-block:: bash
88+
# on your workspace source folder
89+
git clone https://github.com/navigation2-tutorials/navigation2_tutorials.git
90+
cd <your workspace path>
91+
colcon build --symlink-install --packages-up-to nav2_semantic_segmentation_demo
92+
source install/setup.bash
93+
94+
Test that the simulation launches correctly:
95+
96+
.. code-block:: bash
97+
98+
ros2 launch semantic_segmentation_sim simulation_launch.py headless:=0
99+
100+
You should see Gazebo launch with the TurtleBot 4 in the Baylands world.
101+
102+
.. image:: images/Navigation2_with_segmentation/gazebo_baylands.png
103+
:width: 700px
104+
:align: center
105+
:alt: Gazebo Baylands world
106+
107+
1- Setup Semantic Segmentation Inference Node
108+
-----------------------------------------------
109+
110+
The semantic segmentation node performs real-time inference on camera images using an ONNX model. It subscribes to camera images, runs inference, and publishes segmentation masks, confidence maps, and label information.
111+
To run the semantic segmentation node, you need to install the `requirements.txt <https://github.com/ros-navigation/navigation2_tutorials/blob/master/nav2_semantic_segmentation_demo/semantic_segmentation_node/requirements.txt>`_ file in the semantic_segmentation_node package:
112+
113+
.. code-block:: bash
114+
115+
pip install -r <your workspace path>/src/navigation2_tutorials/nav2_semantic_segmentation_demo/semantic_segmentation_node/requirements.txt --break-system-packages
116+
117+
118+
The segmentation node is configured through an ontology YAML file that defines:
119+
120+
- **Classes to detect**: Each class has a name, text prompt (for certain model types), and color for visualization. classes should be defined in the same order as the model output. 0 is always the background class.
121+
- **Model settings**: Device (CPU/CUDA), image preprocessing parameters. we use the CPU for inference for greater compatibility, however if you have a GPU you can install onnxruntime-gpu and set the device to cuda.
122+
123+
An example configuration file (`config/ontology.yaml`):
124+
125+
.. code-block:: yaml
126+
127+
ontology:
128+
classes:
129+
- name: sidewalk
130+
prompt: sidewalk
131+
color: [255, 0, 0] # BGR format
132+
- name: grass
133+
prompt: grass
134+
color: [0, 255, 0] # BGR format
135+
136+
model:
137+
device: cpu # cuda or cpu
138+
max_image_dim: 1024
139+
mask_opacity: 0.15
140+
border_width: 1
141+
142+
The node publishes several topics:
143+
144+
- ``/segmentation/mask``: Segmentation mask image (mono8, pixel values = class IDs)
145+
- ``/segmentation/confidence``: Confidence map (mono8, 0-255)
146+
- ``/segmentation/label_info``: Label information message with class metadata
147+
- ``/segmentation/overlay``: Visual overlay showing segmentation on original image (optional)
148+
149+
Launch the segmentation node (with simulation running):
150+
151+
.. code-block:: bash
152+
153+
ros2 run semantic_segmentation_node segmentation_node
154+
155+
Verify that segmentation topics are being published:
156+
157+
.. code-block:: bash
158+
159+
ros2 topic list | grep segmentation
160+
ros2 topic echo /segmentation/label_info --once
161+
162+
You should see the label information message with the classes defined in your ontology.
163+
164+
2- Configure Nav2 with Semantic Segmentation Layer
165+
--------------------------------------------------
166+
167+
Now we need to configure Nav2 to use the semantic segmentation layer in its costmaps. This involves adding the layer plugin to both the global and local costmaps and configuring the cost assignment for different segmentation classes. Key parameters include:
168+
169+
- **Observation Sources**: Defines which camera/segmentation topics to subscribe to
170+
- **Class Types**: Defines terrain categories (traversable, intermediate, danger)
171+
- **Cost Assignment**: Maps semantic classes to navigation costs
172+
- **Temporal Parameters**: Controls how long observations persist in the costmap
173+
174+
Here's an example configuration for the local costmap:
175+
176+
.. code-block:: yaml
177+
178+
local_costmap:
179+
local_costmap:
180+
ros__parameters:
181+
plugins: ["semantic_segmentation_layer", "inflation_layer"]
182+
semantic_segmentation_layer:
183+
plugin: "semantic_segmentation_layer::SemanticSegmentationLayer"
184+
enabled: True
185+
observation_sources: camera
186+
camera:
187+
segmentation_topic: "/segmentation/mask"
188+
confidence_topic: "/segmentation/confidence"
189+
labels_topic: "/segmentation/label_info"
190+
pointcloud_topic: "/rgbd_camera/depth/points"
191+
max_obstacle_distance: 5.0
192+
min_obstacle_distance: 0.3
193+
tile_map_decay_time: 2.0
194+
class_types: ["traversable", "intermediate", "danger"]
195+
traversable:
196+
classes: ["sidewalk"]
197+
base_cost: 0
198+
max_cost: 0
199+
intermediate:
200+
classes: ["background"]
201+
base_cost: 127
202+
max_cost: 127
203+
danger:
204+
classes: ["grass"]
205+
base_cost: 254
206+
max_cost: 254
207+
208+
The tutorial provides a pre-configured `nav2_params.yaml <https://github.com/ros-navigation/navigation2_tutorials/blob/master/nav2_semantic_segmentation_demo/semantic_segmentation_sim/config/nav2_params.yaml>`_ file in the semantic_segmentation_sim package. You can use it to configure the Nav2 costmaps for your own application.
209+
210+
3- Run everything together
211+
----------------------------
212+
213+
The tutorial provides a complete launch file that launches the simulation, the semantic segmentation node, and the Nav2 navigation stack. To run it, simply launch the `segmentation_simulation_launch.py <https://github.com/ros-navigation/navigation2_tutorials/blob/master/nav2_semantic_segmentation_demo/semantic_segmentation_sim/launch/segmentation_simulation_launch.py>`_ file:
214+
215+
.. code-block:: bash
216+
217+
ros2 launch semantic_segmentation_sim segmentation_simulation_launch.py
218+
219+
The baylands simulation and `rviz` should appear. You should be able to send navigation goals via `rviz` and the robot should navigate the Baylands world, preferring sidewalks and avoiding grass:
220+
221+
.. image:: images/Navigation2_with_segmentation/demo.gif
222+
:width: 90%
223+
:align: center
224+
225+
To better see what the plugin is doing, you can enable the segmentation tile map visualization in `rviz`. This will show a pointcloud of the segmentation observations for each tile, colored by their confidence.
226+
Again, you can refer to the picture on the Layer's `README <https://github.com/kiwicampus/semantic_segmentation_layer>`_ for a visual explanation of how observations are accumulated on the costmap tiles and how that translates to the cost assigned to each tile.
227+
228+
.. image:: images/Navigation2_with_segmentation/tile_map.gif
229+
:width: 90%
230+
:align: center
231+
232+
233+
234+
Conclusion
235+
==========
236+
237+
This tutorial demonstrated how to integrate semantic segmentation with Nav2 for terrain-aware navigation. using a pretrained model that works on gazebo's Baylands world and a custom semantic segmentation layer plugin.
238+
239+
To go further, you can train your own model using the `Simple Segmentation Toolkit <https://github.com/pepisg/simple_segmentation_toolkit>`_, and tune the costmap parameters to your own application.
240+
241+
Happy terrain-aware navigating!
242+

tutorials/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Nav2 Tutorials
1010

1111
docs/navigation2_on_real_turtlebot3.rst
1212
docs/navigation2_with_slam.rst
13+
docs/navigation2_with_semantic_segmentation.rst
1314
docs/navigation2_with_stvl.rst
1415
docs/navigation2_with_gps.rst
1516
docs/using_isaac_perceptor.rst

0 commit comments

Comments
 (0)