Skip to main content

SHIFT Multitask Segformer

Exploring the use of the lightweight Segformer B0 in the CARLA driving simulator.
Last updated: September 27, 2023
The SHIFT dataset (Sun et al., 2022) comprises 2.5 million frames of synthetic multi-sensor driving data captured from the CARLA simulator under diverse environmental conditions, making it a powerful resource for kick-starting driving stacks intended for use within the simulator. Building an ultra-light Reinforcement Learning stack in CARLA with effective and understandable perception, planning, and control stacks built in Python is an exciting research project, but requires careful selection of high-efficiency architectures to keep complexity within a reasonable scope. The self-attention of transformers makes them an attractive network architecture for computer vision tasks in the autonomous driving domain, where there are many global dependencies in the images conveying semantic and geometric meaning, but historically this has also meant a high computational cost. Fortunately, recent advances in transformer design have ameliorated this weakness and made them a viable alternative to CNNs, which by their nature have issues capturing global context.
Segformer (Xie et al., 2021) is a transformer-based semantic segmentation architecture that demonstrates high potency and flexibility from a small number of parameters. Utilizing a lightweight hierarchical transformer encoder feeding multi-level features into an all-MLP decoding head, the architecture preserves global and local attention as well as positional information without use of complex convolution layers. The authors provided six model sizes (B0-B5) to choose from: the smallest, B0, is highly effective with only 3.7M parameters, the second largest, B4, established new state-of-the-art performance at the time of publication with 5x fewer parameters (64.1M) than the previous best method, and the largest, B5, beating the record even further with (a still comparably small) 82M parameters.
The following year, GLPN (Kim et al., 2022) showed that the hierarchical transformer encoder from Segformer could also be paired with a novel lightweight decoding head for monocular depth estimation to achieve state-of-the-art results. For those paying attention, this means that there is a ready-to-build, powerful, lightweight, transformer-based computer vision model for joint estimation of semantic segmentation and monocular depth that comes in six convenient sizes, with only some assembly required. Recently, I built such a model in PyTorch to experiment with using it as part of a lightweight perception stack for a CARLA agent, choosing the lightest B0 backbone, and the results are quite impressive. To see the model in action, please see the blog post. This report focuses on the training methods and results.

Semantic Segmentation Pre-Training

The multi-task model was built on top of huggingface's implementation of SegformerForSemanticSegmentation, so while the code for implementing multi-task training was developed, the pre-trained weights (nvidia/segformer-b0-finetuned-cityscapes-640-1280) were first fine-tuned on semantic segmentation-only training for 5 epochs, with a linear learning rate schedule from 6e-6 to zero. The results are shown below. Note that the last 5k training steps were resumed after a power outage, so the learning rate was off schedule and we see a sudden bump in performance, but this conveniently indicated that another round of training would improve results further. Nevertheless, the mIoU score after these 5 epochs is comparable to the benchmark given in the SHIFT paper, with a much lighter network. Keep in mind that this experiment was limited to data from the "front" camera, and training and evaluation were both done using data from all environmental conditions.
The network was trained on the semantic segmentation task using the PyTorch weighted Cross-Entropy loss ℓCE\ell_{CE} which is calculated as follows:
ln=−wynlog⁡exp⁡(xn,yn)∑c=1Cexp⁡(xn,c)⋅1{yn≠ignore_index}l_n = - w_{y_n} \log \frac{\exp(x_{n,y_n})}{\sum_{c=1}^C \exp(x_{n,c})} \cdot \mathbb{1}\{y_n \not= \text{ignore\_index}\}
where {N=batch dimC=num classesw=class loss weights\begin{cases} N = \text{batch dim} & \\ C = \text{num classes} & \\ w = \text{class loss weights} \end{cases}
ℓCE=∑n=1N1∑n=1Nwyn⋅1{yn≠ignore_index}ln\ell_{CE} = \sum_{n=1}^N \frac{1}{\sum_{n=1}^N w_{y_n} \cdot \mathbb{1}\{y_n \not= \text{ignore\_index}\}} l_n
Class loss weights were determined using the relative class-wise pixel densities across the training dataset, placing more weight on less represented classes to prevent larger classes from dominating the loss. The first 5 epochs of training are below:

1 selected runs


Adding the Monocular Depth Task Head

Once the code was ready for multi-task training, the weights were trained for another 5 epochs on a learning rate schedule from 5e-5 to zero, using the following formulation for multi-task loss:
ℓmulti=αCEℓCE+αSiLogℓSiLog\ell_{multi} = \alpha_{CE}\ell_{CE} + \alpha_{SiLog}\ell_{SiLog}
where αCE\alpha_{CE} and αSiLog\alpha_{SiLog} are task loss weights (which were both set to 1.0), and ℓSiLog\ell_{SiLog} is the scale-invariant log loss (Eigen et al., 2014), formulated as
ℓSiLog=1n∑idi2−λn2(∑idi)2\ell_{SiLog} = \frac{1}{n} \sum_{i} d_{i}^{2} - \frac{\lambda}{n^2}(\sum_{i}d_{i})^2 where {λ=0.25di=log⁡yi−log⁡yi∗\begin{cases} \lambda = 0.25 & \\ d_{i}=\log y_{i}-\log y_{i}^{*} \end{cases}.
The task scores showed mutual improvement over this training period, with the final scores looking quite good. It was later found that the MAE metric calculation for this run was unfortunately not masking out invalid ground truth pixels (sky pixels, in this case), so it is likely to be artificially high by a fraction of a meter. The next training run fixed this issue, so we get a better idea of the metric performance of the depth head.

1 selected runs


Fine-Tuning Depth Performance

Since metric scale is important in the context of autonomous driving, it seemed appropriate to fine-tune the model performance with the inclusion of metric-space MAE in the loss function. The loss formula is then as follows:
ℓmulti=αsemsegℓCE+αdepth(ℓSiLog+0.1∗ℓMAE)\ell_{multi} = \alpha_{semseg}\ell_{CE} + \alpha_{depth}(\ell_{SiLog} + 0.1*\ell_{MAE}) 
We can see from the following training run that the SiLog loss and MAE improved using this modification. The semantic segmentation did not show substantial improvement, likely partially due to the Cross-Entropy loss getting a bit dominated by the larger values in the depth component, and indicating that a larger αsemseg\alpha_{semseg} would have been appropriate.

1 selected runs