# An Efficient Neural Network for Shape from Focus with Weight Passing Method
## 3 Neural Network for SFF
### Proposed Model  
In NN.FIS a focus value is computed from a current frame \((z_{(x,y)}^{(n)}(0,0))\) and its neighboring frames \((z_{(x,y)}^{(n)}(j,k))\) that increases the size of the weight matrix **W** and over all complexity for shape recovery become high. We have observed through the experiments that the focus measure computed from the current frame not only provides smaller weight matrix but also reduces the time complexity remarkably without compromising on accuracy. Figure 3 shows the proposed neural network model over planar surface (NN.Planar) for shape from focus. In SFF, a planar surface is considered in 3D space, however, in this paper \"planar\" means only on the same frame i.e., 2D surface. From the figure, it can be observed that the number of neurons at the input layer are decreased from three to one. In the NN.Planar, the depth \(z_{(x,y)}^{(n)}\) at iteration \((n)\) for the central pixel \((x,y)\) of the window can be expressed as:  
\[z_{(x,y)}^{(n)}=\phi\left(\left(\textbf{v}^{(n)}\right)^{T}\cdot\phi\left( \textbf{u}^{(n)}\right)+w_{b}^{(n)}\right) \tag{7}\]  
where \(\textbf{u}^{(n)}\in\mathbb{R}^{H}\), \(\textbf{v}^{(n)}\in\mathbb{R}^{H}\), and \(w_{b}^{(n)}\in\mathbb{R}\), and \(\phi(\cdot)\) is sigmoidal function defined as:  
\[\phi(\eta)=\frac{1}{1+e^{-\eta}} \tag{8}\]  
where \(\eta\) is a dummy variable. Accordingly, in the NN.Planar, the focus measure \(F\) is calculated as  
\[F=\sum_{ \begin{subarray}{c}-m\leq j\leq m\\ -m\leq k\leq m\end{subarray} }\left[I\left(x+j,y+k,z_{(x,y)}^{(n)}\right)-\mu \right]^{2} \tag{9}\]  
where \(I\left(x+j,y+k,z_{(x,y)}^{(n)}\right)\) is the gray level at the pixel position of \((x+j,y+k)\) in the frame number \(z_{(x,y)}^{(n)}\), and \(\mu\) represents the mean of gray levels within the window and it is expressed as  
\[\mu=\frac{1}{M^{2}}\sum_{ \begin{subarray}{c}-m\leq j\leq m\\ -m\leq k\leq m\end{subarray} }I\left(x+j,y+k,z_{(x,y)}^{(n)}\right) \tag{10}\]  
In order to update weights, we use gradient descent rule. Note that, in NN.Planar, a scalar depth is computed instead of a matrix (as in the case of NN.FIS model) therefore the computation of gradient becomes simplified. Moreover, it becomes more efficient due to the reduced dimensions of \(\mathbf{u}^{(n)}\). By using chain rule, Equation (4) can be written as  
\[ \begin{array}{rcl}\Delta\mathbf{W}^{(n)}&=&\beta\left(\frac{\partial F}{ \partial\mathbf{W}^{(n)}}\right)\\ &=&\beta\left(\frac{\partial F}{\partial z_{(x,y)}^{(n)}}\right)\cdot\left( \frac{\partial z_{(x,y)}^{(n)}}{\partial\mathbf{W}^{(n)}}\right)\\ &=&2\beta\sum_{-m\leq j\leq m}\left[I\left(x+j,y+k,z_{(x,y)}^{(n)}\right)-\mu \right]\\ &&\cdot\left(\frac{\partial\left[I\left(x+j,y+k,z_{(x,y)}^{(n)}\right)-\mu \right]}{\partial z_{(x,y)}^{(n)}}\right)\\ &&\cdot\left(\frac{\partial z_{(x,y)}^{(n)}}{\partial\mathbf{W}^{(n)}}\right) \end{array}  \tag{11}\]  
Figure 3: Proposed neural network structure for shape recovery of neural network model over Planar and local depth map from the neural network model. **Left**: NN.Planar model; **Right**: local depth map from NN.Planar model. The local depth map consists of 9 neighboring pixels centered around \((x,y)\), when small window size is \(3\times 3\). In addition, all local depths are same to the depth at center, so it can reduce the dimension of input layer from 3 to 1. Here \(\mu\) is constant, thus Equation (11) can be written as  
\[ \begin{array}{rcl}\Delta\mathbf{W}^{(n)}&=&2\beta\sum_{-m\leq j\leq m}\left[I \left(x+j,y+k,z_{(x,y)}^{(n)}\right)-\mu\right]\\ &&\cdot\left(\frac{\partial I\left(x+j,y+k,z_{(x,y)}^{(n)}\right)}{\partial z_{ (x,y)}^{(n)}}\right)\\ &&\cdot\left(\frac{\partial z_{(x,y)}^{(n)}}{\partial\mathbf{W}^{(n)}}\right) \end{array}  \tag{12}\]  
where \(I\left(x+j,y+k,z_{(x,y)}^{(n)}\right)\) represents gray level values in the window, and the term \(\left(\partial I\left(x+j,y+k,z_{(x,y)}^{(n)}\right)/\partial z_{(x,y)}^{(n)}\right)\) is the derivative of the image intensity of the window surface with respect to the image frame number \(z_{(x,y)}^{(n)}\).