> [name=謝朋諺(Adam Hsieh)]
> [time=Fri, Aug 4, 2023 9:06 AM]
---
# MonoDETR: Depth-guided Transformer for Monocular 3D Object Detection
[論文連結](https://arxiv.org/pdf/2203.13310.pdf)
{%pdf https://arxiv.org/pdf/2203.13310.pdf %}
## Outline
> [TOC]
---
## Reference
> [MonoDETR:用于单目3D检测的深度感知transformer(CVPR2022)](https://blog.csdn.net/Yong_Qi2015/article/details/124811598)
> [小杨读文献 | MonoDETR,基于深度的Transformer 3D目标检测(附关键代码详解)](https://zhuanlan.zhihu.com/p/558183809)
> [github MonoDETR](https://github.com/ZrrSkywalker/MonoDETR/tree/main)
---
## 摘要重點
- 提出一個全新的架構 **M**onocular **DE**tection with a depth-guided **TR**ansformer,所以叫 **MonoDETR**,主要是使用 Transformer 來做 depth-aware。
- 將 3D 物件候選表示為一組 queries 並採用一組 attention-based depth encoder 來產出輸入影像的 non-local depth embeddings
- 這邊還提出一種 cross-attention 模塊的 depth-guided decoder 來針對 query 間和 query 與場景深度特徵的互動,以至於每個 query 都可以根據影像的深度自適應地來預測 3D 屬性,而不再侷限於僅使用相鄰的影像特徵
- MonoDETR 是一種 end-to-end 的網路,無須額外資料或 NMS 後處理。

:::info
:bulb: 上面那列是現在常用的 Center-guided 方法,下面則是本文提出的 Depth-guided 方法
- 現在的 Center-guided 方法利用中心周圍的特徵來預測物件的 3D 屬性,而本文方法通過預測前景深度圖來引導整個過程,並自適應地聚合整個場景的深度特徵。右下圖表示深度 Cross-Attention 層中的 attention map。
:::
## Contributions
1. 第一個 end-to-end DETR-based Monocular 3D 物件偵測網路,不需要 NMS 的後處理,並且採用 depth-guide 方法來聚合全域上下文中與深度相關的區域特徵。
2. MonoDETR 的 depth-guided Transformer 使對 object 查詢更能收集場景的資訊,也更好來對物件間關係來輔助深度的預測。
3. MonoDETR 引入了一些手動的設計跟幾何先驗,並在 KITTI 資料集上得到最好的結果
## Method

:::info
:bulb: MonoDETR Pipeline
:::
### Feature Extraction
- 使用 ResNet-50 backbone 來擷取特徵
- **Visual Features**
- 假設影像為 $I\in \mathbb{R}^{H\times W \times 3}$,$H$ 是影像高度、$W$ 是影像寬度
- 我們可以得到 multi-scale 的 feature map $f_{\dfrac{1}{8}}$、$f_{\dfrac{1}{16}}$、$f_{\dfrac{1}{32}}$
- 本文將最高 level $f_{\dfrac{1}{32}} \in \mathbb{R}^{\dfrac{H}{32}\times\dfrac{W}{32}\times C}$ 視為 visual feature $f_V$
- **Depth Features**

:::info
:bulb: 左邊是 Depth Predictor 右邊是 Depth Positional Encoding
- 使用 Depth Predictor 產出深度特徵還有情景深度圖。
- 在深度 cross-attention layer,本文使用可學習的 positional encoding 來對 depth embeddings 做內插。
:::
- 用輕量深度 predictor 來產出深度特徵如上圖左邊。
- 首先通過 Nearest-Neighbor 將三個等級特徵的 size 全部統一 downsample 或 upsample 到相同的 $\dfrac{1}{16}$ 比例,並通過 element-wise 加法將它們合併,通過這種方式,可以整合 multi-scale 的語義,並保留了影像的細粒度資訊。
- 再來使用兩層 $3\times 3$ 卷積層來得到影像的深度特徵 $f_D\in \mathbb{R}^{\dfrac{H}{16}\times\dfrac{W}{16}\times C}$
- 為了強制 $f_D$ 對深度進行編碼,本文在 $f_D$ 之上預測前景深度圖 $D_{fg}$,並使用物件深度標籤對其進行監督式學習,而不會對每個 pixel 都輸出它的深度(Dense Depth),而是對同個物件下 2D bounding box 中的所有 pixel 都分配該物件的相同深度標籤。
- 對於同時位於多個 bounding box 內的 pixel,本文會選擇距離相機最近的物件深度標籤。
- **Foreground Depth Map**
- 為了監督深度特徵,在 $f_D$ 之上應用 1×1 卷積來預測前景深度圖 $D_{fg} \in \mathbb{R}^{\dfrac{H}{16}\times\dfrac{W}{16}\times(k+1)}$。
- 離散深度到 $(k+1)$ bins,其中前面 $k$ 個序數都是代表前景深度,只有最後一個代表背景,這邊離散方法是使用 linear-increasing discretization (LID)。
- 將前景深度值限制在 $[d_{min},d_{max}]$ 內,並將第一區間長度和 LID 的公差設置為 $\delta$。 然後將物件的真實深度標籤 $d$ 分類到第 $k$ 個 bin 中:$$k=\lfloor -0.5+0.5\sqrt{1+\dfrac{8(d-d_{min})}{\delta}}\rfloor, where\ \ \delta=\dfrac{2(d_{max}-d_{min})}{k(k+1)}$$
背景就直接被設定在第 $(k+1)$ 個類別。
- 使用 Focal Loss 來對 $D_{fg}$ 每個 pixel 做深度預測分類,並定義為: $\mathcal{L}_{dmap}$
### Depth-guided Transformer
MonoDETR 的 Transformer 是由 visual encoder, depth encoder 還有 depth-guided
decoder 組成,這兩個 encoder 會產出 non-local visual 還有 depth embedding;depth-guided decoder 使物件查詢能夠自適應地聚合 global context 中的 scene-level 特徵。
- **Visual and Depth Encoders**
- 假設深度特徵為 $f_D$ 視覺特徵為 $f_V$,並使用兩個不同 encoder 分別獨立產出具有 global receptive field 的 scene-level embedding,各自表示為 $f^e_D$、$f^e_V$。
- 本文為 visual encoder 設置了 3 個 module,為 depth encoder 設置了 1 個 module,其中每個 encoder module 由 1 個 self-attention layer 和 1 個 feed-forward neural network(FFN)組成。
- depth-encoder 和 visual-encoder 的解耦使它們能夠更好地為自己學習特徵,分別對輸入影像的外觀和空間資訊進行 encoding。
- **Depth-guided Decoder**
- 基於 visual embedding $f^e_V$ 和 depth embedding $f^e_V$,本文利用一組可學習的物件 Queries $q \in \mathbb{R}^{N \times C}$ 通過 depth-guided decoder 檢測 3D 物件,其中 $N$ 表示預定義的最大數量輸入影像中的物件。
- 每個 decoder block 依次由 1 個 **depth cross-attention layer**, 1 個 **inter-query self-attention layer**, 1 個 **visual cross-attention layer** 還有一個 **FFN** 組成;前面三者分別是為了交互 **物件-深度特徵**、**物件間特徵**、**物件-2D 特徵**。通過將**物件-深度特徵**放在第一位,使得物件 Queries 可以優先的整合 Global 的深度特徵,並將該深度資訊作為引導,幫助後續的特徵提取。
- 具體來說,Queries 首先通過 depth cross-attention layer 聚合來自 $f^e_D$ 的深度特徵,會將 objetc queries 和 depth embedding 線性轉換為 queries、keys 和 values: $$Q_q=Linear(q),\ \ K_D,V_D=Linear(f^e_D)$$
其中 $Q_q \in \mathbb{R}^{N \times C}$ 另外 $K_D,V_D\in \mathbb{R}^{\dfrac{HW}{16^2}\times C}$,然後計算 query-depth attention map $A_D\in\mathbb{R}^{N\times\dfrac{HW}{16^2}}$ 並聚合由 $A_D$ 加權的相關深度特徵以產生深度感知查詢 $q'$,公式為: $$A_D=Softmax(Q_qK^T_D/\sqrt{C}),\ \ q'=Linear(A_DV_D)$$

:::info
:bulb: Attention maps $A_D$ 在 depth cross-attention layer 的視覺化
第 1 row 是原圖,最後 1 row 是檢測結果,中間三個 row 是目標 query 的 attention maps(圖中的白圓圈),顏色較紅的是較高的注意力權重。
:::
- **Depth Positional Encodings**
- 在 depth cross-attention layer 使用可學習的 depth positional encoding $f^e_D$ 而不是使用 sin 函數。

- 就像上圖右側可學的 embedding 可表示為: $p_D \in \mathbb{R}^{(d_{max}-d_{min}+1)\times C}$,其中每 row 編碼 1 公尺的深度位置資訊,範圍從 $d_{min}$ 到 $d_{max}$。
- 對於 $f^e_D$ 中的每個像素 $(x, y)$,首先從 $D_{fg}$ 中獲得 $(k + 1)-categorical$ 深度預測信心值,可以表示為: $D_{fg}(x, y) \in \mathbb{R}^{k+1}$,其中每個 channel 表示預測信心值為相應的深度 bin。
- 通過 depth-bin 信心值與其對應深度的加權求和來獲得像素 $(x,y)$ 的估計深度,將其表示為 $d_{map}(x,y)$。
- 然後根據深度 $d_{map}(x,y)$ 對 $p_D$ 進行線性插值,以獲得像素 $(x,y)$ 的深度位置編碼。
- 藉由 pixel-wisely 添加 $f^e_D$ 這種編碼,object queries 可以在 depth cross-attention layer 中捕獲更充分的深度線索,並更好地理解場景的 3D 結構。
### Detection Heads and Loss
- 在 depth-guided transformer 之後,depth-aware object queries 會被輸入到 MLP-based 的 head 中,以進行 3D 屬性預測。
- 在 inference 時期,會整合全部屬性預測來輸出 3D bounding box 結果而且不需要 NMS 的後處理。
- 在 training 階段,會分別計算每個 queries 的 loss,並利用匈牙利算法將無序 queries 與 ground-truth object label 進行匹配,不需要任何其他 rule-based 的 label 分配。
- **Attributes Prediction**
- Detection Head 會預測每個 Queries 的 6 個屬性:**Object Category**、**2D size $(l、r、t、b)$**、**投影 3D 中心 $(x_{3D}、y_{3D})$**、**深度 $d_{reg}$**、**3D size $(h_{3D}、w_{3D}、l_{3D})$** 和 **方向 $\alpha$**。
- 其中 $(l,r,t,b)$ 表示 2D bounding box 的四個邊到投影 3D 中心 $(x_{3D},y_{3D})$ 的距離。
- 另外 $(l,r,t,b)$ 和 $(x_{3D},y_{3D})$ 都標準化到 0~1 之間。
- 為了更準確地估計最終物件深度 $d_{pred}$,本文對三個預測值進行平均:由檢測頭回歸的 $d_{reg}$、由預測的 2D 和 3D 尺寸轉換的 $d_{geo}$ 以及從 $D_{fg}$ 內插的 $d_{map}(x_{3D}, y_{3D})$。公式表示為: $$d_{geo}=f\dfrac{h3D}{t+b},\ \ d_{pred}=(d_{reg}+d_{geo}+d_{map}(x_{3D}, y_{3D}))/3$$
其中 $h_{3D}$ 和 $t+b$ 被定義為 3D 的預測高度和 2D size,$f$ 是相機的 focal length。
- **Bipartite Matching**
- 為了將每個 query 與 ground truth 物件正確匹配,本文計算每個 Query-Label pair 的 loss,並利用匈牙利演算法來找到最佳匹配。
- 對於每一個 pair,我們將 6 個屬性的 Loss 整合為 2 組:
- 第一組包含 Object Category、2D size 和投影 3D 中心,因為這些屬性主要涉及影像的 2D 視覺外觀。
- 第二組由深度、3D size 和方向組成,它們是物件的 3D 空間屬性。
- 分別對兩組 Loss 求和,並將它們表示為 $\mathcal{L}_{2D}$ 和 $\mathcal{L}_{3D}$。
- 由於網路通常預測 3D 屬性不如 2D 屬性準確,特別是在訓練開始時,$\mathcal{L}_{3D}$ 的值不穩定,會干擾匹配過程。
- 本文僅使用 $\mathcal{L}_{2D}$ 作為匹配每個 query-label pair 的 matching cost。
- **Overall Loss**
- 在 query-label 匹配之後,我們從 $N$ 個查詢中獲得 $N_{gt}$ 個有效 pair,其中 $N_{gt}$ 表示 Ground Truth object 的數量。所以訓練影像的整體 Loss 可表示為: $$\mathcal{L}_{overall}=\dfrac{1}{N_{gt}}·\sum^{N_{gt}}_{n=1}(\mathcal{L}_{2D}+\mathcal{L}_{3D})+\mathcal{L}_{dmap}$$
$\mathcal{L}_{dmap}$ 代表預測的前景深度分類圖 $D_{fg}$ 的 Loss。
## Experiments
### Settings
* **KITTI 3D Dataset**
* 7,481 training
* 3,712 sub-training set, 3,769 val set
* 7,518 test images
* 提供 3 種等級難度結果: easy, moderate, hard
* Metrics: average precision (AP) 在 3D space 還有 bird-eye view,可定義成 $AP_{3D}$, $AP_{BEV}$
* IoU threshold 是設定為 0.7
* **Implementation Details**
* Backbone: ResNet-50
* 使用 deformable attention mechanisms 實作 visual encoder 還有 visual cross-attention layers 以節省 GPU 記憶體,並使用最原始的 attention 來更好地擷取 depth encoder 和 depth cross-attention layers 的全局空間結構。
* 本文對所有 attention modules 使用 8 個 heads,並將 queries 數量 $N$ 設置為 50。
* 將 channel $C$ 以及基於 FFN 和 MLP 的 detection head 的潛在特徵維度都設置為 256。
* 對於前景深度圖,我們設置 $[d_{min}, d_{max}]$ 為 $[0m, 60m]$,bin 數量 $k$ 為 $80$。
* 在單個 GeForce RTX 3090 GPU 上,訓練 MonoDETR 195 個 epochs,batch size 為 16,學習率為 $2 \times 10^{−4}$。
* 採用權重衰減為 $10^{-4}$ 的 AdamW Optimizer,並將 125 和 165 epoch 的學習率降低 0.1 倍。
### Performance Comparison

:::info
:bulb: 車子類別在 Kitti test 和 val sets 的結果
粗體字代表結果最好的,藍色則代表第二好的
:::
### Ablation Studies
- **Depth-guided Transformer**

:::info
:bulb: 實驗 depth-guided transformer 的有效性
- Depth-guided Trans.: depth-guided transformer
- Depth CA: depth cross-attention layer
- Depth Pos.: depth positional encodings
:::
- **Depth Encoder**

:::info
:bulb: 實驗 Depth Encoder 的設計
- Deform. $SA$: one-block deformable self-attention
- Global $SA_{\times2}$: 1 或 2 個 blocks 原型的 self-attention layer with a global receptive field.
- 3×3 $Conv._{\times2}$: 代表 3 個 3×3 卷積層
:::
- **Depth-guided Decoder**

:::info
:bulb: 實驗 Depth-guided Decoder 設計
- **D**: depth cross-attention
- **I**: inter-query self-attention
- **V**: visual cross-attention layers
- **Deform.Depth CA**: 使用 deformable attention 來實作 depth cross-attention layer
:::
- **Depth Positional Encodings**

:::info
:bulb: 實驗 Depth positional encodings
- Meter-wise $p_D$: 分配 meter 為可學習的 embedding
- k-bin $p_D$: 分配 depth bins 為可學習的 embedding
- sin/cos: 則是使用正弦函數來產出位置 encoding
:::
## Conclusion
* 提出了 MonoDETR,這是第一個 DETR-based 的 monocular 3D 物件偵測框架,它包含最少的幾何先驗,並且無需任何額外的輸入、anchors 或 NMS。
* 與現有的 center-guided 方法不同,本文使物件 Queries 能夠自適應地探索深度線索,並進行 inter-object 和 object-scene 深度互動。
## Appendix
### Details of the Loss Functions
* 在 depth-guided transformer 之後,這邊採用 detection heads 來估計每個物件 query 的屬性。所有 queries 共享同一屬性的 head weights。
* 物件類別使用 1 個 Linear Projection 層
* 對 2D size $(l、r、t、b)$ 和投影 3D 中心 $(x_{3D}、y_{3D})$ 使用 3 層 MLP
* 對深度 $d_{reg}$、3D size $(h_{3D}、w_{3D}、l_{3D})$ 和方向 $\alpha$ 使用 2 層 MLP 。
**Object Category and 2D Size (l, r, t, b)**
- 在 KITTI 中檢測 3 種類別的物件,car、pedestrian 和 cyclist,並採用 Focal loss 進行優化,記為 $\mathcal{L}_{class}$。
- 通過預測物件的四個邊 l、r、t、b 到投影 3D 中心 $(x_{3D},y_{3D})$ 的距離來獲得物件的 2D bounding box,並使用 L1 loss 來算距離,對 DETR 後恢復的 2D bounding box 應用 GIoU loss,分別表示為 $\mathcal{L}_{lrtb}$ 和 $\mathcal{L}_{GIoU}$。
**Projected 3D Center $(x_{3D}, y_{3D})$**
- 本文直接輸出每個 Queries 在影像上投影的 3D 中心的坐標 $(x_{3D}, y_{3D})$,而不是使用大家常用的偏移量。
- 通過這種方式,可以一步獲得每個物體的投影 3D 中心,而不會產生中間偏移的誤差,從而有助於提高定位精度。
- 採用 L1 Loss 進行中心預測,並將其表示為 $L_{xy3D}$。
**3D Size $(h_{3D}, w_{3D}, l_{3D})$ and Orientation $\alpha$**
- 本文沒有預測平均形狀值的 residuals,而是遵循 MonoDLE 對 3D size 使用 3D IoU oriented loss。
- 將航向角分為多個帶有 residuals 的 bin,並採用 MultiBin loss 來優化方向的預測。
- 這兩個 Loss 分別記為 $L_{size3D}$ 和 $L_{orien}$。
**Depth $d_{pred}$.**
- 平均 3 個深度值 $d_{reg}$, $d_{geo}$ 還有 $d_{map}(x_{3D}, y_{3D})$ 來產出 $d_{pred}$,然後,對整體 $d_{pred}$ 採用 Laplacian aleatoric uncertainty loss ,公式為: $$\mathcal{L}_{depth}=\dfrac{\sqrt{2}}{\sigma}||d_{gt}-d_{pred}||_1+log(\sigma)$$
其中 $\sigma$ 是 $d_{reg}$ 的標準差,$d_{gt}$ 是物件的 ground truth depth label
**Two Groups of Losses $\mathcal{L}_{2D}$ and $\mathcal{L}_{3D}$**
- 針對 Bipartite Matching,本文透過 $\mathcal{L}_{2D}$ 組來計算 matching cost,公式為: $$\mathcal{L}_{2D}=\lambda_1\mathcal{L}_{class}+\lambda_2\mathcal{L}_{xy3D}+\lambda_3\mathcal{L}_{lrtb}+\lambda_4\mathcal{L}_{GIoU}$$
其中 $\lambda_{1\sim4}$ 是設為 $2,10,5,2$
最後的 loss 就可以寫成: $$\mathcal{L}_{3D}=\mathcal{L}_{size3D}+\mathcal{L}_{orien}+\mathcal{L}_{depth}$$
### Additional Results
**Car Category on KITTI val Set**

:::info
:bulb: Car 類別在 KITTI val 資料集中以不同 IoU 閾值結果
- 粗體是最好的結果
- 藍色是第二好的結果
:::
**Pedestrian and Cyclist Categories**

:::info
:bulb: Pedestrian, Cyclist 類別在 KITTI test 資料集結果
- 粗體是最好的結果
- 藍色是第二好的結果
- IoU 這邊是設定為 0.5
- 這邊的資料明顯少於 Car 類別很多,所以有更大的挑戰
:::
### Additional Ablation Study
**Depth Discretization**

:::info
:bulb: 前景深度圖不同離散化的實驗
- **UD**: uniform 離散法
- **SID** spacing-increasing 離散法
- **LID** linear-increasing 離散法
- **Continuous Rep**: 深度的連續值
:::
**Bipartite Matching**

:::info
:bulb: 實驗 bipartite matching
- 使用不同的 losses 作為每個 query-label 的 matching cost
- '-' 代表 training collapse
:::
**Transformer Blocks and FFN Channels**

:::info
:bulb: Transformer blocks 還有 FFN channels 實驗
- 實驗 visual encoder 和 depth-guided decoder 的不同 block 數量,以及 FFN 的潛在 channel 不同設置的實驗。
:::
### Additional Implementation Details
- Testing Set 上的 Data Augmentation: 採用 Random Flip 和 photometric distortion
- Validation Set 多使用了 Random Crop 來進一步提高性能。
- 為了 Training 穩定性,丟棄深度標籤大於 65 公尺或小於 2 公尺的訓練樣本。
- 在 Inference 過程中,本文直接過濾掉類別信心值低於 0.2 的物件 Queries。
### Additional Visualization


:::info
:bulb: Visualization of detection results
- 左邊的綠色框代表沒有使用 depth-guided transformer 的預測
- 右邊的黃色框代表使用了 depth-guided transformer 的預測
- 紅色圓圈代表本文強調的部分
:::
### Depth Error Analysis

:::info
:bulb: MonoDETR 不同 varient 的深度誤差。
- x 軸和 y 軸分別表示在 KITTI val 集上 moderate 等級的 $AP_{3D}$ 平均深度誤差。
:::

:::info
:bulb: 深度誤差的定量結果
- 每次只移除一種方法來做實驗,a, b, c, d 請參照上圖圖示解說
- (a) 移除 depth-guided transformer
- (b) 移除 depth encoder
- (c) 移除 separate depth cross-attention layer
- (d) 移除 depth positional encodings
- 評估是用 $AP_{3D}$ 在 moderate level 的結果,並顯示平均深度誤差跟標準差。
:::