# Vision-Based Distance Measurement in Advanced Driving Assistance Systems
## 3 Depth Map Estimation
### Semi-Supervised Learning Network for Depth Map Estimation
#### 3.2.1 Loss Functions  
In the training process, the left view \(\mathbf{I}^{l}\) and right view \(\mathbf{I}^{r}\) captured by the left and right cameras, respectively, are simultaneously input into the network. When the left view \(\mathbf{I}^{l}\) is input into this depth estimation network, the disparity map \(\mathbf{d}^{r}\) that corresponds to the conversion from the left view to the right view can be predicted pixel by pixel. Similarly, the disparity map \(\mathbf{d}^{l}\) is obtained when the input image is the right view \(\mathbf{I}^{r}\). Further, we can reconstruct the right view \(\overset{\rightarrow}{\mathbf{I}}^{l}\) based on the original left view \(\mathbf{I}^{l}\) and the right disparity map \(\mathbf{d}^{r}\), and the left view \(\overset{\rightarrow}{\mathbf{I}}^{l}\) based on the right view \(\mathbf{I}^{r}\) and the left disparity map \(\mathbf{d}^{l}\). Finally, the reconstructed left view \(\overset{\rightarrow}{\mathbf{I}}^{l}\) and right view \(\overset{\rightarrow}{\mathbf{I}}^{r}\) are respectively combined with the original left view \(\mathbf{I}^{l}\) and right view \(\mathbf{I}^{r}\) to form a loss function, known as the appearance matching loss for network training. This appearance matching loss is expressed as follows [29]:  
\[ \begin{array}{l}C^{l}_{ap}=\frac{\alpha}{mn}\sum\limits_{i=1}^{m}\sum \limits_{j=1}^{n}\frac{1-X_{\text{SSIM}}(\mathbf{i}^{l}(i,j),\overset{ \rightarrow}{\mathbf{I}}^{l}(i,j))}{2}+(1-\alpha)\|\mathbf{I}^{l}(i,j)- \overset{\rightarrow}{\mathbf{I}}^{l}(i,j)\|_{1}\\ C^{r}_{ap}=\frac{\alpha}{mn}\sum\limits_{i=1}^{m}\sum\limits_{j=1}^{n}\frac{1-X _{\text{SSIM}}(\mathbf{i}^{r}(i,j),\overset{\rightarrow}{\mathbf{I}}^{r}(i,j) )}{2}+(1-\alpha)\|\mathbf{I}^{r}(i,j)-\overset{\rightarrow}{\mathbf{I}}^{r}( i,j)\|_{1}\end{array}  \tag{4}\]  
where \(\mathbf{I}^{l},\overset{\rightarrow}{\mathbf{I}}^{l}\) and \(\overset{\rightarrow}{\mathbf{I}}^{r}\in\mathbb{R}^{m\times n}\), \(\alpha=0.85\), \(X_{\text{SSIM}}(\mathbf{x},\mathbf{y})\) can be calculated using Ref. [30]. Disparity smoothness loss consists of two parts: (1) the gradient values of the disparity map in two directions \(\left|\mathbf{d}^{l}(i+1,j)-\mathbf{d}^{l}(i-1,j)\right|\) and \(\left|\mathbf{d}^{l}(i,j+1)-\mathbf{d}^{l}(i,j-1)\right|\) are used to create local smoothness; (2) considering that depth discontinuities often occur at image edges, we weight the gradient values of the disparity map with an edge-aware term using the image gradients in two directions. \[\begin{array}{l}C^{l}_{ds}=\frac{1}{mn}\sum\limits_{i=1}^{m}\sum\limits_{j =1}^{n}\left|\mathbf{d}^{l}(i+1,j)-\mathbf{d}^{l}(i-1,j)\right|\exp\!\left(- \left|\mathbf{I}^{l}(i+1,j)-\mathbf{I}^{l}(i-1,j)\right|\right)+\\ \left|\mathbf{d}^{l}(i,j+1)-\mathbf{d}^{l}(i,j-1)\right|\exp\!\left(-\left| \mathbf{I}^{l}(i,j+1)-\mathbf{I}^{l}(i,j-1)\right|\right)\\ C^{r}_{ds}=\frac{1}{mn}\sumThe main difference between unsupervised and semi-supervised depth estimation is that the semi-supervised method adds a supervised loss to the above three losses. The prerequisite for using supervised loss is to know the true depth values and the predicted depth values. In the training process, the true depth values corresponding to parts of pixels are first obtained and matched. The predicted depth values of the pixels with true depth values can be converted from the predicted disparity map using Equation (3). The supervised loss can be defined as the deviation of the predicted depth values \(\vec{\mathbf{Z}}\) from the available ground truth \(\mathbf{Z}\), and expressed as follows:  
\[ \begin{array}{l}C_{sv}^{l}=\frac{1}{N}\sum\limits_{(i,j)\in\Omega}\|\vec{ \mathbf{Z}}^{l}(i,j)-\mathbf{Z}^{l}(i,j)\|_{\delta}\\ C_{sv}^{r}=\frac{1}{N}\sum\limits_{(i,j)\in\Omega}\|\vec{\mathbf{Z}}^{r}(i,j)- \mathbf{Z}^{r}(i,j)\|_{\delta}\end{array}  \tag{7}\]  
where \(\Omega\) is the set of all pixels with true depth values, and \(N\) is the number of the pixels with true depth values. \(\|\bullet\|_{\delta}\) is the berHu norm and defined as follows:  
\[\|d\|_{\delta}=\left\{