# A Deep Similarity Metric Method Based on Incomplete Data for Traffic Anomaly Detection in IoT
## 3 Methodology
### Channel Attention Model for Siamese Architecture  
In recent years, ResNet [12] has been used in many fields such as detection, segmentation, recognition and so on. It was first introduced in 2015 and won the first place in the ImageNet classification competition. This kind of structure is for the sake of the problem that the accuracy of the network decreases on the validation set with the deepening of the network. As with the block module shown in Figure 3, two feature mapping functions are proposed in ResNet, one is the identity mapping, the other is residual mapping. The block modules are cascaded to form the overall structure of ResNet. Because only very few samples of anomaly objects on the road have been provided for training the model, there will be a serious over-fitting problem if a single ResNet structure is chosen to repeatedly train the net on this incomplete data-set. Under this circumstance, the model trained in this way has insufficient generalization ability to accurately predict the new test samples on the road. As shown in Figure 3, we choose ResNet18 as the main body of the two branches, where four blocks are interconnected with each other. The weights of the two branches are shared in both training and testing stages. The features output from the last block are down-sampled by an average pooling layer. Then the pooled features are concatenated together for high dimensional feature fusion. Afterwards, the fused feature is inputted into the full connected layer to get a vector for discriminating whether the two image patches belong to the same category. The two inputs indicate the images to be detected and the image already exists in the incomplete database, respectively. The candidate image is extracted from a specific area in the successive video frames when an anomaly object is detected in the subsequent frames. An attention model is designed after each block, including a channel attention layer and a soft attention layer. The visual attention model is the brain signal processing mechanism peculiar to human vision. Human vision scans the global image quickly to get the focus area, which is generally called the focus of attention. Then, more attention resources are invested in this area to obtain more details of the target and suppress other useless information. We hope to input a word vector to the model through the attention mechanism, which can be used to select the categories of the anomaly object, and indirectly assist in identifying the similarity between the two images. The attention map generated from the attention model can mask the irrelevant areas on the feature map and highlight the salient region to calculate the similarity between two branches. The word vectors of different levels have different lengths while the attentional maps of different hierarchies have different resolutions. The implementation details of the hierarchical attention mechanism are shown in Figure 4. \(R1\) and \(R2\) are the raw features extracted by the blocks and \(R^{l}_{i,j,k}\) indicates the feature value at the position \((i,j)\) of the \(k^{\prime}\)th channel map extracted by \(l^{\prime}\)th block. \(Att^{l}_{i,j}\) denotes the attention value at the position \((i,j)\) on  
Figure 3: Overall framework of the proposed method. The simplest version of ResNet is selected as the main body of the two branches. The block indicates a deep residual structure in the dotted line. A soft attention model is appended after each block for feature location (att1, att2, att3, att4). The word vectors for each block have different lengths (word64, word128, word256, word512) according to hierarchical feature channels. the soft attentional map generated after \(l^{\prime}\)th block. \(Ratt1\) and \(Ratt2\) represent the attentional features masked by the attention map of both branches while \(Ratt^{l}_{i,j,k}\) is the value at the position \((i,j)\) of the \(k^{\prime}\)th feature map after \(l^{\prime}\)th block. \(R\) and \(Ratt\) are both three-dimensional and the \(Att\) is two-dimensional.