# Anomalous Vehicle Recognition in Smart Urban Traffic Monitoring as an Edge Service +
## 5 Anomalous Vehicle Behavior Interpretation
### Cascaded Capsules Network
#### 5.2.1 CapNet 1  
There are three layers in the first Capsules Network of the cascaded approach: primary capsules layer, behavior capsules layer and reconstruction decoder layer.  
The vehicle behavior matrix is fed into a convolutional layer first to extract preliminary features. The kernel size is \(6\times 6\), and the stride is one. The input channel is equal to one and there are 256 output channels. The primary capsules layer consists of 32 channels of capsules, and each of them has eight convolutional units with kernels of size \(9\times 9\), and the stride is equal to two. Then, the output vectors are multiplied by weight matrix \(W_{ij}\) and gets summed over with different weights for each channel in a behavior capsules layer:  
\[s_{j}=\sum_{i}c_{ij}\hat{\mathbf{\mu}}_{j|i},\ \hat{\mathbf{\mu}}_{j|i}=\mathbf{W}_{ij}\mathbf{u}_{i} \tag{17}\]  
where \(c_{ij}\) is the coupling coefficients between the lower capsules layer and the upper capsules layer, which is determined by the dynamic routing agreement process, and \(s_{j}\) is the input vector of the upper level capsules network, which is a weighted sum over all output vectors \(\hat{\mathbf{u}}_{j|i}\) from the lower capsules layer.  
A squash activation function is applied on \(s_{j}\) to ensure the nonlinearity between different capsules layers:  
\[v_{j}=\frac{\left\|\mathbf{s}_{j}\right\|^{2}}{1+\left\|\mathbf{s}_{j}\right\|^{2}} \frac{\mathbf{s}_{j}}{\left\|\mathbf{s}_{j}\right\|} \tag{18}\]  
This activation function maintains the direction of input vectors while the length is normalized. With this squash function, short vectors are closer to zero while long vectors are close to one. The coupling coefficients \(c_{ij}\) is calculated by routing softmax:  
\[c_{ij}=\frac{exp\,b_{ij}}{\sum_{k}exp\,b_{ik}} \tag{19}\]  
where \(b_{ij}\) is the logits of the coupling coefficients, which is calculated by the dynamic routing process.  
The dynamic routing process is illustrated as Algorithm 1 below. With the dynamic routing algorithm, the scalar product of \(\hat{\mathbf{\mu}}_{j|i}v_{j}\) is leveraged to evaluate the agreement  
Figure 2: Architecture of the Cascaded Capsules Network.  
between lower level features in primary capsules and high level features in behavior capsules. Then, the agreement is added into \(b_{ij}\) to update the coupling coefficients between different capsules layers.  
```
ROUTING(\(\hat{\mathbf{\mu}}_{[i|},\tau,l\))
2:for all capsule \(i\) in layer \(l\) and capsule \(j\) in layer \((l+1)\): \(b_{ij}\gets 0\) for\(r\) iterations do
4: for all capsule \(i\) in layer \(l\): \(c_{i}\gets softmax(b_{i})\) for all capsule \(j\) in layer \((l+1)\): \(\mathbf{s}_{j}\leftarrow\sum_{i}c_{ij}\hat{\mathbf{\mu}}_{[i|}\)
6: for all capsule \(j\) in layer \((l+1)\): \(\mathbf{v}_{j}\gets squash(\mathbf{s}_{j})\) for all capsule \(i\) in layer \(l\) and capsule \(j\) in layer \((l+1)\): \(b_{ij}\gets b_{ij}+\hat{\mathbf{\mu}}_{[i|}\mathbf{v}_{j}\) return\(\mathbf{v}_{j}\)
```  
**Algorithm 1** Dynamic Routing Algorithm.  
The \(l2\) norm of the output vectors are used to represent the probability of the existence of a capsule entity. The margin loss for class \(k\) would be:  
\[L_{k}=T_{k}max(0,m^{+}-\left\|\mathbf{v}_{k}^{2}\right\|)+\lambda(1-T_{k})max( 0,\left\|\mathbf{v}_{k}\right\|-m^{-})^{2} \tag{20}\]  
where \(T_{k}=1\) if the class instantiation exists and \(m^{+}=0.9\) while \(m^{-}=0.1\). \(\lambda\) is set to 0.5 and the total loss becomes the summation of each capsules. Following the behavior Capsules layer, we will check whether the detected behavior falls into any class of normal behaviors. If not, the vehicle driving behavior data will be reconstructed by the reconstruction decoder layer, which consists of three fully connected layers. The size of output matrix is the same as the size of the input matrix.