# An Efficient Neural Network for Shape from Focus with Weight Passing Method
## 4 Initial Weight Setting
### RS Initial Weight Setting Method  
In [10], the initial weights for each pixel are determined randomly. If the initial depth map significantly deviate from the desired depth map generated from the initial weight, it may lead to local minimization problem and network may fail in obtaining a precise depth map. Furthermore, this method may lead to slow convergence of the network as it needs larger numbers of iterations for 3D shape learning. In other words, it fails to estimate a depth precisely with a high probability if the initial weights for a pixel are determined randomly. Therefore, in [27], initial weights are taken randomly around a reference depth map \(z_{r}(x,y)\) to complement the problem. In this paper, we call it RS method. The RS method is summarized in Algorithm 1. At first, a test set of initial weights (**u**, **v**, and \(w_{b}\)) are randomly generated in the range \([0,1]\). Next, \(z^{(1)}_{(x,y)}\) is calculated using Equation (7). Then, the distance between initial depth \(z^{(1)}_{(x,y)}\) and the reference depth \(z_{r}(x,y)\) is compared with the threshold \(T_{1}\). If the distance is less than or equal to \(T_{1}\), the test set of initial weights are regarded as the proper initial weights for \((x,y)\) and are used for further computation of (\(\mathbf{u}^{(1)}\), \(\mathbf{v}^{(1)}\), and \(w_{b}^{(1)}\)). On the other side, if the distance is greater than to \(T_{1}\) then again initial weights are generated randomly and the distance is compared with \(T_{1}\). This process is repeated for a certain times; say \(R=1000\). If the proper weights are not found within \(R\) iterations then an inaccurate depth for \((x,y)\) may be computed. Once weights are obtained, the depth for \((x,y)\) is estimated through NN.Planar (or NN.FIS) model using Equations (7)-(21) (or Equations (1)-(5)) with \(\mathbf{u}^{(1)}\), \(\mathbf{v}^{(1)}\), and \(w_{b}^{(1)}\). In this way, the entire depth map is obtained by computing depth for all pixel positions.  
```
1:procedureRandom Setting
2:for\(x=1\) to \(I_{W}\)do
3:for\(y=1\) to \(I_{H}\)do
4:for\(i=1\) to \(R\)do
5: Generate initial weights \(\mathbf{u},\mathbf{v}_{i}w_{b}\) randomly.
6:\(z_{(x,y)}^{(1)}\leftarrow\phi\left(\left(\mathbf{v}\right)^{T}\cdot\phi\left( \mathbf{u}\right)+w_{b}\right)\)
7:if\(\left|z_{(x,y)}^{(1)}-z_{r}(x,y)\right|\leq T_{1}\)then
8:\(\mathbf{u}^{(1)}\leftarrow\mathbf{u}\)
9:\(\mathbf{v}^{(1)}_{i}\leftarrow\mathbf{v}\)
10:\(w_{b}^{(1)}\gets w_{b}\)
11:break
12:endif
13:endfor
14:\(d(x,y)\leftarrow\) estimated depth at \((x,y)\) by using Equations (7)-(21) (or Equations (1)-(5))
15:endfor
16:endfor
17:endprocedure
```  
**Algorithm 1** Random Setting method.  
In summary, this method has two main problems: (1) the time complexity is significantly increased by determining the initial weights for every pixel position; and (2) If the initial depth map is close to the reference depth map, RS method may avoid the local minimization problem and can provide reasonable initial weights however, when the reference depth map is near to the minimum or maximum value of depth range, it is hard to get proper initial weights and accurate depth map. This phenomena is highlighted in Figure 4. It shows the histogram of \(z_{(x,y)}^{(1)}\) and represents the distribution of numerical data of \(z_{(x,y)}^{(1)}\) from 10,000 trials of Equation (7) by using random weights. From Figure 4, we can find that the frequency is very low near to the minimum or maximum depth values. Consequently, it is hard to meet the requirements of \(\left|z_{(x,y)}^{(1)}-z_{r}(x,y)\right|\leq T_{1}\) within \(R\) trials, and thus it is hard to set the proper initial weights in RS method. For example, if \(z_{r}(x,y)=0.05\) and \(T_{1}=0.02\), and then \(z_{(x,y)}^{(1)}\) should be in \([0.03,0.07]\). However, there is low probability that \(z_{(x,y)}^{(1)}\) be within \([0.03,0.07]\) after \(R\) trials, thus there is high probability of not setting proper initial weights.