# Intelligent Detection of Parcels Based on Improved Faster R-CNN
## 2 Method
### Self-Attention ROI Align  
The main role of the ROI Pooling module is to convert the RPN-generated candidate region feature matrix into a fixed-size feature map for subsequent object classification and candidate box regression operations. As shown in Figure 7, suppose the target candidate box size is 500 \(\times\) 480. After the feature extraction network, the size of the target candidate box is reduced by 32 times compared with the input, which is 15.625 \(\times\) 15. At this time, ROI Pooling performs the first downward rounding operation to round the feature map size to 15 \(\times\) 15. Next, the feature map is divided into 7 \(\times\) 7 cells, each of which has a size of 2.1428 \(\times\) 2.1428, and ROI Pooling performs a second downward rounding operation to  
Figure 6: Edge detection branch. round the feature map size. In summary, there are two rounding operations in ROI Pooling: (1) rounding the target candidate frame boundary; and (2) dividing the rounded candidate frame into k \(\times\) k cells equally and rounding each cell. After the above two roundings, the position of the object candidate box can no longer be matched exactly with the candidate box generated by RPN, and there is some misalignment. For the parcel detection that requires precise location, such position misalignment will inevitably cause serious false and missed detections. To address the above problem, our method proposed the self-attention ROI Align module to eliminate the problem of feature location bias caused by rounding operations. The flow of self-attention ROI Align is shown in Figure 8. For a candidate box region in a given image, first, the valuable features are provided with a more significant weight and the useless features a smaller weight through the self-attention module [31], thus acting as a feature filter and enhancement; then, the feature map is fixed into a \(7\times 7\) sized feature map through the ROI Align layer [32], and finally, the \(7\times 7\) sized feature map is input to the subsequent classifier for object classification and position regression. The specific implementation of Self-attention is shown in Figure 9: (1) The input feature map first passes through three convolutional kernels of size \(1\times 1\), with the first two convolutional kernels having a channel count of C/8 and the last convolutional kernel having a channel count of C. Three feature maps are obtained, for query, key and value, where the size of the feature maps for query and key is \(C/8\times H\times W\), and the size of the value feature map is \(C\times H\times W\). (2) The query and each key are subjected to a similarity calculation to obtain the weights. Commonly used similarity functions are dot product, splicing and perceptron, etc. The dot product is chosen for the proposed method. (3) The weights in (2) are then normalized using the Softmax function and converted to an attention map. (4) The attention map and the feature map value are summed point by point to obtain the final Self-attention feature map. Figure 8: Self-attention ROI Align. Figure 7: ROI Pooling. The specific implementation process of ROI Align is as follows: (1) First, each candidate box is not rounded, and its floating-point boundary is maintained. (2) Then the candidate box is divided into k \(\times\) k cells, and the boundary of each cell is also not rounded. (3) Finally, the position of the four sampling points is calculated in each cell using bilinear interpolation, and the maximum pooling operation is performed on the sampling points of each cell to obtain the output of the cell. Step (3) is calculated by first sampling four random points in the cell (the blue points in Figure 10), then using the four actual pixel points around the sampled points to obtain the values of the sampled points through bilinear interpolation, assuming that the coordinates of the four sampled points (the red arrow in Figure 10) are \((x_{i},y_{i})(i=1,2,3,4)\), and the corresponding pixel values are \(f(x_{i},y_{i})\).