# X-Ray Small Target Security Inspection Based on TB-YOLOv5

Authors: Muchen Wang; Yueming Zhu; Yongkang Liu; Huiping Deng
Journal: Security and Communication Networks
 (2022)

---

## Abstract

Security inspection is extremely important for the safety of public places. In this research, we are trying to propose a novel algorithm and investigated theoretically in the X-ray dataset, which can optimize the relative low detection accuracy and the latent omission detection of smaller objects when using You Only Look Once version 5 (YOLOv5). For one side, the transform detection network is selected to be added at the bottom layer of backbone structure to avoid the loss of useful information during sequential calculation. On another side, we attempt to adjust the existing PANet structural elements of the model, including their connections and other related parameters to improve the detection performance. We integrate an efficient BiFPN with the CA mechanism, which can enhance feature extraction, and named it attention-BiFPN. Experimental consequences demonstrate that the detection accuracy of the proposed model, which we name “TB-YOLOv5,” has obvious advantages in check performance compared with the mainstream one-stage object detection models. Meanwhile, compared with YOLOv5, the data results display an improvement of up to 14.9%, and the average precision at 0.5 IOU even reached 23.4% higher in the region of small object detection. Our purpose was to explore the potential of changing a popular detection algorithm such as YOLO to address specific tasks and provide insights on how specialized adjustments can influence the detection of small objects. Our work can supply an effective method of enhancing the performance of X-ray security inspection and show promising potential for deep learning in related fields.


---

## Body

## 1. Introduction

Security inspection plays an indispensable role in safeguarding public occasions from security threats of terrorism. With the development of population density in metropolitan transportation hubs, it is increasingly critical to quickly, automatically, and accurately identify prohibited products in trunks or packages. The disadvantages of traditional manual checking methods caused by subjectivity and visual fatigue after prolonged detection result in poor accuracy judging contraband goods. Consequently, the conventional detection way is no more adapted to the modern high-speed lifestyle and is not satisfying humans’ higher demand for safety, so advanced techniques are emerging. Electromagnetic ultrasonic [1] and other detection measures have been used earlier. However, the high requirements of advanced equipment make those technologies impractical to be put into widespread use. Recently, under the circumstances that artificial intelligence technology has a significant breakthrough, especially in convolutional neural networks, the security inspection method based on deep learning to find and recognize target objects in X-ray images [2–4] comes into being.To effectively limit the prohibited dangerous articles and increase the accuracy of image detection results of different products in luggage considering different conditions, it becomes indispensable for people to optimize the existing related technology. Different from natural images and X-ray detection in other scenes [5], the articles in the suitcase are stacked randomly and overlap with each other seriously, giving rise to the finite resolution ratio and context information available to the model in the process of algorithm design. Meanwhile, detecting small target objects is always a more challenging task.Although great efforts have been made to improve the detection ability of smaller objects [6], most of them only focus on guiding the processing of specific areas of the picture [7,8], or on improving the resolution ratio of the image, which come at the expense of detecting speed. However, considering the development of computer vision algorithms, many existing object detection networks show outstanding detection effects in different areas. Among them, the detecting networks based on deep learning can be broadly divided into two types—two-stage algorithm and one-stage algorithm. Two-stage algorithms include regional convolutional neural network (R-CNN) [9], faster R-CNN [10], and spatial pyramid pooling net (SPP-NET) [11]. Two-stage object detection algorithms are dependent on reorganization box and classifier. The bounding box is first searched to generate a series of candidate regions. The features are extracted from the original image by convolutional neural network to locate and classify them. Despite the detection accuracy of the two-stage algorithm often showing obvious advantages, the problem of slow detecting speed still exists due to the complex network structure, which means two-stage is not suitable for real-time applications such as security inspection and automatic driving. Therefore, experts further developed a series of one-stage target detection algorithms [12], including single shot multibox detector (SSD) [13], efficient object detection (EfficientDet) [14], and You Only Look Once (YOLO) [15]. Those kinds of algorithms define the process of target detection as a regression problem, and the target objection can be directly located and classified through the regression model. Compared with others, one-stage algorithm has a relatively faster-checking efficiency, which can be better used in a system with high demand for speed. Among all kinds of one-stage algorithms, version 5 of YOLO (YOLOv5) is a popular target detector [16], which earns a reputation for its high performance and running speed. The excellent function of YOLOv5 can be attributed to its flexible structure and can be decomposed, adjusted, or built on many widely accessible platforms. However, many systems attempt to apply the YOLOv5 architecture to optimize, mainly relying on adjusting specific parameters or enlarging training datasets to improve performance [17], rather than changing network structure to modify the model itself when encountering the detection of objection with insufficient feature information. Therefore, those measures cannot effectively ameliorate the performance of smaller object detection. Miao et al. proposed a class-balanced hierarchical thinning (CHR) to solve the problem of information loss caused by overlapping image data in X-ray security check and designed a class-balanced loss function to minimize noise introduced by negative samples [2]. However, this method still fails to solve the problem of insufficient feature information extracted from small objects and needs a huge dataset as support. Benjumea et al. have enhanced the algorithm ability to extract small target objects by modifying the neck structure, which is called YOLO-Z [18], and the results exhibit better detection performance in automatic driving, though there is still much room for the improvement to grantee that this method can be used in X-ray security inspection.Our study aimed to continue using the networks based on YOLOv5 for X-ray security detection and do some improvements to solve the problem that the existing algorithms’ disadvantage is to extract the information of small target features. The proposed algorithm is named “TB-YOLOv5,” partly referencing the existing YOLO-Z algorithm. It is improved by adding a transformer module to the bottom layer of backbone and substituting the PANet structure in the neck part with attention-BiFPN, which integrates BiFPN structure with coordinate attention (CA) module to enhance the collection of characteristic information. The transformer model has been widely used in object detection these years [19, 20] and shows advantages in simplicity and efficiency compared with traditional CNN. CA also demonstrates the speed superiority in image processing region [21, 22]. The investigation proves that the TB-YOLOv5 can develop target detection performance in X-ray security inspection. Compared with the traditional YOLOv5 algorithm, the results of TB-YOLOv5 are significantly improved, especially for the small objects, which demonstrate great significance to the development of X-ray security inspection in public places. Furthermore, we believe that this approach could provide new avenues for realizing various intelligent industrial applications. The following part of this article will be divided into three sections: (1) in the second section, we will firstly introduce the conventional YOLOv5 structure and some relevant components in our methods. (2) Then, we will investigate the detection consequences trained by YOLOv5 and TB-YOLOv5 to demonstrate the advantages of its performance. Meanwhile, the third section will give the analysis and comparison of the experimental results and checking inference. (3) In the last section, we will conclude our research and propose the prospect of the algorithm.


## 2. Materials and Methods

### 2.1. Materials and Samples

X-ray detection machine is the most common security inspection technology applied by the relevant departments in China at present, widely seen in urban rail transit, railway, airport, key venues, logistics delivery, and other scenes. Using artificial intelligence technology to assist front-line security inspectors in making judgments can effectively reduce the possibility of missing reports caused by fatigue or inattention, but in the actual scene, due to the diversity of objects, imaging angles, occlusion, and other problems, there are certain challenges in the development of the algorithm. The dataset of this study comes from iFLYTEK AI Open Challenge of USTC. The format of the dataset is PASCAL VOC2007 [23], and the labeled files contain 12 categories, including knife, scissors, sharp tools, expandable baton, small glass bottle, electric baton, plastic beverage bottle, plastic bottle with a nozzle, electronic equipment, battery, seal, umbrella, and a total of 4,000 pictures. Figure 1 shows the number of items for each category in the dataset. MSCOCO dataset is generally used for evaluating the size of the targets. For an object with an area less than a specified pixel value, MSCOCO considers it as a small object. Based on this criterion, we defined scissors, knife, small glass bottle, battery, and seal as the small objects in our later research because they have an obviously smaller pixel than others in most images.Figure 1
Pie chart of each target objection’s number in the presented dataset with twelve categories.


### 2.2. Detection Principle

#### 2.2.1. One Stage and YOLO Detection Principle

Two-stage algorithms, such as R–CNN, use the region suggestion method to first generate potential bounding boxes in the image and then run classifiers on these suggested boxes. After classification, the bounding box is refined by post-processing, duplicate detection is eliminated, and the box is rescaled based on other objects in the scene [13]. These complex pipes limit the running speed and are difficult to optimize because each individual component must be trained separately.YOLOv5 is the highest version of existing YOLO until today. YOLO is the first algorithm to extend the CNN recognition idea to target detection, transforming target detection into a regression problem. Boundary regression in model classification is also called one-stage detection [24]. Each convolution network of YOLO simultaneously predicts multiple bounding boxes and their class probabilities. YOLO trains the whole image and directly optimizes the detection performance. Compared with traditional target detection methods, this unified model is fast in target detection. The reason is that this method treats detection as a regression problem, so it does not need complex pipes and only needs to run the neural network on the new image during the test to predict the detection results. The original YOLOv2 uses BN as regularization to accelerate convergence and avoid overfitting, and the BN layer and ReLU are connected to each convolution layer. To improve the detection performance of YOLO, Darknet-53, Anchor, FPN, and other structures were added to YOLOv3 proposed in 2018. Among them, using the residual component of ResNet for reference, the network can be built deeper, the model capacity is larger, and the feature learning ability is stronger. Darknet-53 makes the network easier to train and faster to merge through residual connections. Conv is used to implement downsampling to reduce the negative gradient effect caused by the pool. Darknet-53 uses a convolution operation with a step size of 2. YOLOv3 uses logistic regression to score the anchor boxes objectively, leaving some anchors with low scores before prediction. This dramatically reduces the amount of calculation.In a nut, YOLO’s average accuracy is higher than other real-time systems. Therefore, we have reason to believe that the YOLO algorithm has a bright application prospect in the field of real-time target detection.


#### 2.2.2. YOLOv5 Detection Principle

In this study, we intend to improve the existing YOLOv5 model to increase the detection capability of small target objects for further application in X-ray security screening recognition. The detection of small target objects has always been a complex field in deep learning due to insufficient relevant information. At the same time, with the increase in people’s travel frequency, security identification will increase the requirement of detection speed, which will dramatically increase the complexity of target detection. To optimize the YOLOv5 algorithm, it is indispensable for us to understand its foundation and the current state of development.The structure of YOLOv5 algorithm is similar to YOLOv4 and further improved on the basis of it. The network structure of traditional YOLOv5 can be divided into four parts: input, backbone, neck network, and prediction (output), and the complete framework of YOLOv5-s, for example, is shown in Figures1 and 2.Figure 2
Architectural structure diagram of YOLOv5-s, which consists of input, backbone, neck, and prediction four parts.The main functional structures on the input side are mosaic data enhancement, adaptive anchor frame calculation, and adaptive image scaling. The input image resolutions of the YOLO algorithm are generally 416 × pixels × 416pixels, 512 × pixels × 512 pixels, and 608 × pixels × 608pixels. Experiments show that if the input resolution is higher, the model’s performance will improve. The data enhancement adopts the mosaic approach, which takes scaling, cropping, and random arrangement to the dataset, thus increasing the complexity of the dataset, making the dataset gets greatly enriched, and adding many small targets, which makes the robustness of trained model much better. Meanwhile, the number of images read from the dataset for training is smaller in each batch, which reduces the memory usage of GPU. In the YOLO series of detection algorithms, the default anchor frame length and width are initially invented for different targets. A prediction frame is an output based on the initially set anchor frame when the dataset is training. The difference between the labeled real frame and the prediction frame is calculated, and then, the parameters in the network structure are iteratively updated in the reverse direction. In the YOLOv5 algorithm, this feature is embedded in the structure, and the best anchor frame values are computed adaptively for different training sets at each learning. The adaptive image scaling in the target detection algorithm is decided by the length and width of images in the respective dataset. The original image is first scaled into a uniform standard size and then sent to the detection network after processing. When YOLOv5 zooms the original image, it can adaptively add the least black edge according to the image size. After the black edge of the image is processed, the amount of calculation will be reduced during reasoning, so that the target detection speed of the network will be improved.The main functional structures of the backbone network include the focus and the CSP modules. The key for the focus structure is the slicing operation, and the convolution operation after the slicing is completed. Different YOLOv5 network structures have exclusive numbers of convolution kernels, and the focus slicing operation is shown in Figure3(a). There are two types of CSP structures in YOLOv5. The CSP1_X structure in the backbone network consists of CBL module, RES unit module, and convolution layer, and the other CSP2_X structure is in the neck, which includes the convolution layer and X RES unit modules. Using the CSP module can enhance the network’s learning ability and make the trained model, which can keep lightweight and have high accuracy at the same time. The addition of the SPP module (spatial pyramid pooling) to the CSP increases the receptive field and extracts the most essential contextual features without causing a reduction in the operation speed. The structure of the two CSP modules is shown in Figure 3(b), and the structure of the SPP module is shown in Figure 3(c).Figure 3
Schematic of the details inside the YOLOv5-s networks. (a) Focus slice operation, (b) CSP module, and (c) and SPP structure.
(a)(b)(c)The neck network has an FPN + PANet structure. FPN is a top-down structure, and the predicted feature maps are computed by fusing the feature information from the higher layers with the lower layer features through an up-sampling operation [25]. The YOLOv5 network structure has a bottom-up feature pyramid added behind the FPN layer, in which there are two PANet structures. This has the advantage of conveying strong semantic features through the top-down FPN layer and strong localization features through the bottom-up feature pyramid. From different backbone layers to different detection layers, the parameter is aggregated. The path aggregation network structure is shown in Figure 4.Figure 4
Illustration of PANet framework in YOLOv5. (a) The backbone with FPN structure. (b) Bottom-up augmentation path. (c) Adaptive feature information pooling. (d) The branch of box and fully connection fusion. The channel dimension of feature map does not appear in the schematic.The GIOU loss function at the output is an improvement of the traditional IOU loss. Suppose the intersection of the prediction frame and the real frame isA and the concatenation set is B. IOU is defined as the intersection set A divided by the concatenation set B. The loss of IOU can be expressed as follows:(1)IOU_LOSS=1−IOU=1−AB.The loss of IOU is relatively simple, but there are also some problems. Firstly, there may be a situation in the prediction frame and the real frame does not intersect when the IOU is 0, which cannot reflect the distance between the prediction frame and the real frame. In addition, when the prediction frame and the real frame have the same size, the IOU may also be the same, and the IOU loss function cannot distinguish between these two situations. Therefore, GIOU loss is proposed for improvement. Defining the minimum outer rectangle of the prediction frame and the real frame be the set C, and the difference set S stands for the difference between the set C and the concatenated set B. Then, the GIOU loss can be expressed as follows:(2)GIOU_LOSS=1−GIOU=1−IOU−SC.The GIOU loss function improves how to measure the intersection scale and reduces the deficiency when it is simply IOU loss.


#### 2.2.3. Comparison of YOLOv5 Network Structures

The four network structures of YOLOv5, YOLOv5-s, YOLOv5-m, YOLOv5-l, and YOLOv5-x, have the same framework, differing only in-depth and width, which are controlled by two parameters, depth multiple and width multiple. There are two CSP structures in the YOLOv5 network structure, CSP1 and CSP2, which have been mentioned above, and the depth of each CSP structure is different in the four networks, respectively.The number of convolutional kernels used in each network structure depends on the specific characteristics of locations, which directly affects the third dimension of the convolutional feature map, the width of the network. The more the number of convolutional kernels, the wider the width of the feature map and the better the learning ability of the network to extract features. The four network structures YOLOv5-s, YOLOv5-m, YOLOv5-l, and YOLOv5-x are getting deeper and wider in order, and the detection accuracy is also increasing successively, but the requirement for hardware configuration is also raised. Considering the individuality of different datasets, to ensure the overall detection performance, we need to use the YOLOv5 network structure with the most suitable quantization degree, depth, and feature map width in the series to train the model as much as possible. Table1 demonstrates the different complexity of different models.Table 1
Four network structures of YOLOv5. The parameters depth multiple and width multiple control the number of BottleneckCSP and the number of convolution kernels of the architectural models, respectively.
YOLOv5-sYOLOv5-mYOLOv5-lYOLOv5-xDepth multiple0.330.671.01.33Width multiple0.500.751.01.25BottleneckCSP BCSPn (true)1, 3, 32, 6, 63, 9, 94, 12, 12BottleneckCSP BCSPn (false)1234Number of convolution kernels32, 64, 128, 256, 51248, 96, 192, 384, 76864, 128, 256, 512, 102480, 160, 320, 640, 1280


### 2.3. Our Method’s Detection Principle

Although two-stage methods such as Faster-RCNN may yield better detection results, they are not suitable for time-insensitive systems due to their different detection principles. Therefore, our approach needs to improve the one-stage algorithm YOLOv5, which is much faster in detection speed, and improves its checking accuracy for smaller objects. Different types of feature pyramid networks (FPNs) [26–28] are obtained by investigating how to handle feature mappings, rather than just modifying the backbone, which can often be aggregated in different ways to enhance the backbone. Also, adding a transformer to the backbone network is a feasible approach [29], considering that small targets can be extracted with less feature information. In this part, we will introduce each module’s principle in detail.


#### 2.3.1. Transformer Principle

Considering that the computation of traditional RNN is restricted to be sequential, that is, the relevant algorithm can only compute sequentially from left to right or from right to left. This mechanism brings two problems: one is the computation of time slicet depends on the computation result at t−1 moment, which limits the parallelism capability of the model. Another is that the information may be lost in sequential computation. The proposed Transformer model has successfully solved the two problems mentioned above. It uses the attention mechanism to reduce the distance between any two positions in the sequence to a constant and has better parallelism and conforms to the existing GPU framework. Transformer is essentially an encoder-decoder structure, and the structure of encoder-decoder is shown in Figure 5(a); the specific module of encoder is shown in Figure 5(b). The difference between the encoder and the decoder is that the latter has one more encoder-decoder attention. Two attention is used to calculate the input and output weights, respectively. Self-attention is the relationship between the current translation and the previous text translated. Encoder-decoder attention is the relationship between the current translation and the encoded feature vector.Figure 5
Illustration of internal details in transformer structure. (a) The modules of encoder-decoder. (b) The self-attention and the feed-forward neural network constitute one unit in transformer. (c) The completed transformer structure is the composite of self-attention, encoder-decoder attention, and feed-forward neural network.
(a)(b)(c)Then, we analyze the detailed structure in the transformer’s encoder. The data first go through a module called “self-attention,” which is the transformer’s core. In self-attention, each data possess three different vectors, which are query vectorQ, key vector K, and value vector V. They are obtained by multiplying the three different weight matrices by the embedding vector X from three different weight matrices WQ, WK, and WV, where the dimensions of the three matrices are the same, all being 512×64. A weighted eigenvector is obtained by self-attention called AttentionQ,K,V, which can also be denoted as vector Z, and the value is defined as follows:(3)Z=AttentionQ,K,V=softmaxQKTdkV.After getting the value ofAttentionQ,K,V, it will be sent to the next module of encoder—feed-forward neural network. This full connection has two layers, the activation function of the first layer is ReLU, and the second layer is a linear activation function that can be expressed as follows:(4)FNNZ=max0,ZW1+b1W2+b2.A whole trainable network structure is the stack of encoder and decoder, and we can get the complete transformer structure in Figure6. Transformer may not only be applied in the field of NLP machine translation. It has a very promising potential for scientific research.Figure 6
Transformer—model architecture.


#### 2.3.2. BiFPN Principle

Since the introduction of FPN [2], it has been widely used for multi-scale feature fusion, and according to the previous introduction of neck part, FPN introduces a top-down channel to fuse features. Recently, PANet, NAS-FPN, and other studies have developed more cross-scale feature fusion network structures such as PANet [30], which adds a bottom-up channel to FPN, and NAS-FPN [31], which uses an irregular topology to search out. While fusing different input features, most previous works simply summarize them indiscriminately. However, since these different input features have different resolutions, we observe that they usually contribute unequally to the fused output features, which means that the feature information is not consistent across scales. At the same time, improved PANet and NAS-FPN bring a great computational effort. Thus, state-of-the-art object detectors are becoming more and more expensive (and some advanced target detectors show excellent performance even at the cost of RAM). For example, NAS-FPN-based detectors require 167M parameters and 3045B FLOPs (30 times more than RetinaNet) to achieve state-of-the-art accuracy. To address these problems, we applied the weighted bi-directional feature pyramid network (BiFPN), proposed by Tan et al., which can perform multi-scale feature fusion easily and quickly. The FPN, PANet, NAS-FPN, and BiFPN structures are, respectively, shown in Figure 7.Figure 7
Reticular formation of four structures. (a) FPN adopts a top-down path to mix different features fromP3 to P7. (b) PANet introduces an added down-top path based on FPN. (c) NAS-FPN uses neural networks to exploit unusual information and recycle the same module. (d) BiFPN adds extra branches to attain superior detection accuracy and realize a more efficient trade-off.
(a)(b)(c)(d)BiFPN, as shown in Figure7(d), is based on a simplified version of PANet by adding residual links, removing nodes with only one input edge, and performing weight fusion if the input and output nodes are at the same level. Adding residual links enhances the representation of features by simple residual operations. Removing nodes with a single input edge is because the nodes with a single input edge are not fused, that is why they have less information and do not contribute much to the final fusion. At the same time, removing a single input edge can reduce the computation and speed up the detection. BiFPN fuses more features without increasing the cost and removes the intermediate nodes of P3 and P7 in PANet, resulting in a simplified two-way network.


#### 2.3.3. Coordinate Attention (CA)

The CA mechanism [32] has the following advantages. First, it can capture not only cross-channel information but also direction-aware and position-aware information, which can help the model to locate and identify the target of interest more precisely. Secondly, CA is flexible and lightweight, easily inserted into classical modules, such as the inverted residual block proposed by MobileNetV2 [33] and the sandglass block presented by MobileNeXt [34]. Both enhance features utilizing enhanced information representation. Finally, as a pretrained model, the CA mechanism can significantly benefit downstream tasks on top of lightweight networks, especially those where intensive prediction exists, such as semantic segmentation.The CA module encodes channel relationships and long-range dependencies with precise location information in two steps: coordinate information embedding and coordinate attention generation, structured as Figure8.Figure 8
Schematic diagram of the proposed coordinate attention block, while theX Avg Pool and Y Avg Pool stand for 1D horizontal and vertical global pooling, respectively.Firstly, we come to part of coordinate information embedding. Global pooling is commonly used in channel attention to globally encode spatial information as channel descriptors, and thus, it is difficult to preserve location information. The authors decompose global pooling into a pair of one-dimensional feature encoding operations to facilitate the attention module to capture spatial long-range dependencies with precise location information. In particular, for the inputX, first using the dimensions H,1 and 1,W of the pooling kernel encodes each channel along with the horizontal and vertical coordinate directions, so that the height h and the output of the first c output of the first channel are expressed as follows:(5)zchh=1W∑0≤i≤Wxch,j.Similarly, the width ofw of the first c output of the first channel is expressed as follows:(6)zcwh=1H∑0≤i≤Hxcj,w.These two transformations perform feature aggregation along with two spatial directions, returning a pair of direction-aware attentional maps. This is quite different from the SE(SENet) [35] module that generates a feature vector. Both transformations allow the attention module to capture long-range dependencies along one spatial direction and preserve precise location information along the other spatial direction, which helps the network locate the target of interest more accurately. This coordinate information embedding operation corresponds to the X Avg Pool and Y Avg Pool of Figure 8.Looking specifically at the operation of the CA mechanism, the two feature maps generated by the previous module are first cascaded and then transformed using a shared1×1 convolution F1 that is expressed in the following equation (7). The downsampling ration f∈RC/r×H+W is an intermediate feature map of the spatial information in the horizontal and vertical directions, and is used to control the size of the module as in the SE module.(7)f=δF1zh,zw.Then, along the spatial dimension,f is decomposed into two separate tensors f∈RC/r×H+W and fw∈RC/r×W, and then, two 1×1 convolution Fh and Fw is operated on feature maps fh and fw, respectively, and transform them into the same channels as the same as the input X. The result of the following equations is obtained:(8)gh=σFhfh,(9)gw=σFwfw.Thegh and gw are defined as attention weights. Then, the final output of the CA module can be expressed in the following equation:(10)yci,j=xci,j×gchi×gcwj.This part of the coordinate attention generation corresponds to the remaining part of Figure8, so that the CA module has completed both horizontal attention and vertical attention, and it is also a kind of channel attention.


### 2.4. TB-YOLOv5 Principle

For small object target detection, the transformer module can enhance the extracted information to make up for the small volume object contextual information, and the BiFPN structure with three inputs can better integrate the input features. These methods are introduced in detail in Sections2.3.1 and 2.3.2. Both are better means to enhance small target detection. The addition of CA mechanism makes up for the problem that the features extracted by the convolution operation in the general algorithm are more limited. The information obtained is more abbreviated, which makes it challenging to integrate the corresponding features, and allows the neck part to better focus on the detection object we want when performing feature extraction, thus improving the detection accuracy of small target objects.To increase the feature information extraction capability, we added transformer capability to the bottom layer of backbone to increase, extract information, and optimize detection. The PANet of the traditional neck part is replaced with a BiFPN structure, and a CA mechanism is added to form the new neck structure named attention-BiFPN. A CA module can be regarded as a computational unit to enhance the feature. A CA module can be considered as a computational unit that enhances the expressiveness of features in a mobile network. It can take any intermediate feature tensor as input and output the same size as the tensor with enhanced representations by transformation [32]. The original YOLOv5 algorithm is optimized with the added transformer structure and the designed attention-BiFPN mechanism, and the improved algorithm is named TB-YOLOv5.In addition, after the above analysis, we know that a significant advantage of the one-stage series algorithm for X-ray security detection is that it has a faster detection speed than the traditional two-stage algorithm, which can be better applied to real-time systems. This is an important reason why the CA mechanism is used, which has the advantage of fast detection speed, compared with some attention mechanisms such as CBAM (convolutional block attention module), an attention mechanism compared with [36], which needs to detect target features in both time and space dimensions of serial detection of target features, so CBAM attention mechanism detection increases the detection time, which contradicts our intention of trying to apply the algorithm to X-ray cases. On the contrary, CA is used to extract information features by coordinates, modeling the location information faster, and the detection time is controlled and suitable for detection needs. In addition, compared with SE block, another attention mechanism has been widely used in recent years, and it only considers the importance of each channel by modeling the channel relationships and ignores the location information. However, the location information is important for generating spatially selective attention maps, which cannot improve the detection results for small targets. The network structure of TB-YOLOv5 is shown in Figure 9.Figure 9
Network structure model and parameters of TB-YOLOv5, which consisted of input, backbone, attention-BiFPN, and output four parts while the input section is omitted.


### 2.5. Evaluating Indicators

As the number of iterations increases during the training process, various relevant parameters change. In our study, we adopt some usual indicators to measure the performance of results and the exact definitions of them are as follows [37–39]:Loss Indexes. It is defined by the GIOU loss function; the closer the value is to 0, the more accurate the target frame, detection, and classification are.Precision. It is defined by the number of correct targets marked divided by the total number of targets marked; the closer to 1, the higher the accuracy rate. TP and FP mean true positive and false positive, respectively.(11)Precision=TPTP+FP×100%.Recall. It is defined by the number of correct targets marked divided by the total number of targets to be marked; the closer to 1, the higher the accuracy rate, and FN means false negative.(12)Recall=TPTP+FN.mAP.5. AP is the area enclosed after plotting with precision and recall as the two axes; when IOU is set to 0.5, the closer to 1, the higher the accuracy. The mAP.5-small especially expresses the small object detection results in this article.(13)mAP=∑i=1cAPic, where C stands for the total number of categories and APi indicates the ith category value of AP.


## 2.1. Materials and Samples

X-ray detection machine is the most common security inspection technology applied by the relevant departments in China at present, widely seen in urban rail transit, railway, airport, key venues, logistics delivery, and other scenes. Using artificial intelligence technology to assist front-line security inspectors in making judgments can effectively reduce the possibility of missing reports caused by fatigue or inattention, but in the actual scene, due to the diversity of objects, imaging angles, occlusion, and other problems, there are certain challenges in the development of the algorithm. The dataset of this study comes from iFLYTEK AI Open Challenge of USTC. The format of the dataset is PASCAL VOC2007 [23], and the labeled files contain 12 categories, including knife, scissors, sharp tools, expandable baton, small glass bottle, electric baton, plastic beverage bottle, plastic bottle with a nozzle, electronic equipment, battery, seal, umbrella, and a total of 4,000 pictures. Figure 1 shows the number of items for each category in the dataset. MSCOCO dataset is generally used for evaluating the size of the targets. For an object with an area less than a specified pixel value, MSCOCO considers it as a small object. Based on this criterion, we defined scissors, knife, small glass bottle, battery, and seal as the small objects in our later research because they have an obviously smaller pixel than others in most images.Figure 1
Pie chart of each target objection’s number in the presented dataset with twelve categories.


## 2.2. Detection Principle

### 2.2.1. One Stage and YOLO Detection Principle

Two-stage algorithms, such as R–CNN, use the region suggestion method to first generate potential bounding boxes in the image and then run classifiers on these suggested boxes. After classification, the bounding box is refined by post-processing, duplicate detection is eliminated, and the box is rescaled based on other objects in the scene [13]. These complex pipes limit the running speed and are difficult to optimize because each individual component must be trained separately.YOLOv5 is the highest version of existing YOLO until today. YOLO is the first algorithm to extend the CNN recognition idea to target detection, transforming target detection into a regression problem. Boundary regression in model classification is also called one-stage detection [24]. Each convolution network of YOLO simultaneously predicts multiple bounding boxes and their class probabilities. YOLO trains the whole image and directly optimizes the detection performance. Compared with traditional target detection methods, this unified model is fast in target detection. The reason is that this method treats detection as a regression problem, so it does not need complex pipes and only needs to run the neural network on the new image during the test to predict the detection results. The original YOLOv2 uses BN as regularization to accelerate convergence and avoid overfitting, and the BN layer and ReLU are connected to each convolution layer. To improve the detection performance of YOLO, Darknet-53, Anchor, FPN, and other structures were added to YOLOv3 proposed in 2018. Among them, using the residual component of ResNet for reference, the network can be built deeper, the model capacity is larger, and the feature learning ability is stronger. Darknet-53 makes the network easier to train and faster to merge through residual connections. Conv is used to implement downsampling to reduce the negative gradient effect caused by the pool. Darknet-53 uses a convolution operation with a step size of 2. YOLOv3 uses logistic regression to score the anchor boxes objectively, leaving some anchors with low scores before prediction. This dramatically reduces the amount of calculation.In a nut, YOLO’s average accuracy is higher than other real-time systems. Therefore, we have reason to believe that the YOLO algorithm has a bright application prospect in the field of real-time target detection.


### 2.2.2. YOLOv5 Detection Principle

In this study, we intend to improve the existing YOLOv5 model to increase the detection capability of small target objects for further application in X-ray security screening recognition. The detection of small target objects has always been a complex field in deep learning due to insufficient relevant information. At the same time, with the increase in people’s travel frequency, security identification will increase the requirement of detection speed, which will dramatically increase the complexity of target detection. To optimize the YOLOv5 algorithm, it is indispensable for us to understand its foundation and the current state of development.The structure of YOLOv5 algorithm is similar to YOLOv4 and further improved on the basis of it. The network structure of traditional YOLOv5 can be divided into four parts: input, backbone, neck network, and prediction (output), and the complete framework of YOLOv5-s, for example, is shown in Figures1 and 2.Figure 2
Architectural structure diagram of YOLOv5-s, which consists of input, backbone, neck, and prediction four parts.The main functional structures on the input side are mosaic data enhancement, adaptive anchor frame calculation, and adaptive image scaling. The input image resolutions of the YOLO algorithm are generally 416 × pixels × 416pixels, 512 × pixels × 512 pixels, and 608 × pixels × 608pixels. Experiments show that if the input resolution is higher, the model’s performance will improve. The data enhancement adopts the mosaic approach, which takes scaling, cropping, and random arrangement to the dataset, thus increasing the complexity of the dataset, making the dataset gets greatly enriched, and adding many small targets, which makes the robustness of trained model much better. Meanwhile, the number of images read from the dataset for training is smaller in each batch, which reduces the memory usage of GPU. In the YOLO series of detection algorithms, the default anchor frame length and width are initially invented for different targets. A prediction frame is an output based on the initially set anchor frame when the dataset is training. The difference between the labeled real frame and the prediction frame is calculated, and then, the parameters in the network structure are iteratively updated in the reverse direction. In the YOLOv5 algorithm, this feature is embedded in the structure, and the best anchor frame values are computed adaptively for different training sets at each learning. The adaptive image scaling in the target detection algorithm is decided by the length and width of images in the respective dataset. The original image is first scaled into a uniform standard size and then sent to the detection network after processing. When YOLOv5 zooms the original image, it can adaptively add the least black edge according to the image size. After the black edge of the image is processed, the amount of calculation will be reduced during reasoning, so that the target detection speed of the network will be improved.The main functional structures of the backbone network include the focus and the CSP modules. The key for the focus structure is the slicing operation, and the convolution operation after the slicing is completed. Different YOLOv5 network structures have exclusive numbers of convolution kernels, and the focus slicing operation is shown in Figure3(a). There are two types of CSP structures in YOLOv5. The CSP1_X structure in the backbone network consists of CBL module, RES unit module, and convolution layer, and the other CSP2_X structure is in the neck, which includes the convolution layer and X RES unit modules. Using the CSP module can enhance the network’s learning ability and make the trained model, which can keep lightweight and have high accuracy at the same time. The addition of the SPP module (spatial pyramid pooling) to the CSP increases the receptive field and extracts the most essential contextual features without causing a reduction in the operation speed. The structure of the two CSP modules is shown in Figure 3(b), and the structure of the SPP module is shown in Figure 3(c).Figure 3
Schematic of the details inside the YOLOv5-s networks. (a) Focus slice operation, (b) CSP module, and (c) and SPP structure.
(a)(b)(c)The neck network has an FPN + PANet structure. FPN is a top-down structure, and the predicted feature maps are computed by fusing the feature information from the higher layers with the lower layer features through an up-sampling operation [25]. The YOLOv5 network structure has a bottom-up feature pyramid added behind the FPN layer, in which there are two PANet structures. This has the advantage of conveying strong semantic features through the top-down FPN layer and strong localization features through the bottom-up feature pyramid. From different backbone layers to different detection layers, the parameter is aggregated. The path aggregation network structure is shown in Figure 4.Figure 4
Illustration of PANet framework in YOLOv5. (a) The backbone with FPN structure. (b) Bottom-up augmentation path. (c) Adaptive feature information pooling. (d) The branch of box and fully connection fusion. The channel dimension of feature map does not appear in the schematic.The GIOU loss function at the output is an improvement of the traditional IOU loss. Suppose the intersection of the prediction frame and the real frame isA and the concatenation set is B. IOU is defined as the intersection set A divided by the concatenation set B. The loss of IOU can be expressed as follows:(1)IOU_LOSS=1−IOU=1−AB.The loss of IOU is relatively simple, but there are also some problems. Firstly, there may be a situation in the prediction frame and the real frame does not intersect when the IOU is 0, which cannot reflect the distance between the prediction frame and the real frame. In addition, when the prediction frame and the real frame have the same size, the IOU may also be the same, and the IOU loss function cannot distinguish between these two situations. Therefore, GIOU loss is proposed for improvement. Defining the minimum outer rectangle of the prediction frame and the real frame be the set C, and the difference set S stands for the difference between the set C and the concatenated set B. Then, the GIOU loss can be expressed as follows:(2)GIOU_LOSS=1−GIOU=1−IOU−SC.The GIOU loss function improves how to measure the intersection scale and reduces the deficiency when it is simply IOU loss.


### 2.2.3. Comparison of YOLOv5 Network Structures

The four network structures of YOLOv5, YOLOv5-s, YOLOv5-m, YOLOv5-l, and YOLOv5-x, have the same framework, differing only in-depth and width, which are controlled by two parameters, depth multiple and width multiple. There are two CSP structures in the YOLOv5 network structure, CSP1 and CSP2, which have been mentioned above, and the depth of each CSP structure is different in the four networks, respectively.The number of convolutional kernels used in each network structure depends on the specific characteristics of locations, which directly affects the third dimension of the convolutional feature map, the width of the network. The more the number of convolutional kernels, the wider the width of the feature map and the better the learning ability of the network to extract features. The four network structures YOLOv5-s, YOLOv5-m, YOLOv5-l, and YOLOv5-x are getting deeper and wider in order, and the detection accuracy is also increasing successively, but the requirement for hardware configuration is also raised. Considering the individuality of different datasets, to ensure the overall detection performance, we need to use the YOLOv5 network structure with the most suitable quantization degree, depth, and feature map width in the series to train the model as much as possible. Table1 demonstrates the different complexity of different models.Table 1
Four network structures of YOLOv5. The parameters depth multiple and width multiple control the number of BottleneckCSP and the number of convolution kernels of the architectural models, respectively.
YOLOv5-sYOLOv5-mYOLOv5-lYOLOv5-xDepth multiple0.330.671.01.33Width multiple0.500.751.01.25BottleneckCSP BCSPn (true)1, 3, 32, 6, 63, 9, 94, 12, 12BottleneckCSP BCSPn (false)1234Number of convolution kernels32, 64, 128, 256, 51248, 96, 192, 384, 76864, 128, 256, 512, 102480, 160, 320, 640, 1280


## 2.2.1. One Stage and YOLO Detection Principle

Two-stage algorithms, such as R–CNN, use the region suggestion method to first generate potential bounding boxes in the image and then run classifiers on these suggested boxes. After classification, the bounding box is refined by post-processing, duplicate detection is eliminated, and the box is rescaled based on other objects in the scene [13]. These complex pipes limit the running speed and are difficult to optimize because each individual component must be trained separately.YOLOv5 is the highest version of existing YOLO until today. YOLO is the first algorithm to extend the CNN recognition idea to target detection, transforming target detection into a regression problem. Boundary regression in model classification is also called one-stage detection [24]. Each convolution network of YOLO simultaneously predicts multiple bounding boxes and their class probabilities. YOLO trains the whole image and directly optimizes the detection performance. Compared with traditional target detection methods, this unified model is fast in target detection. The reason is that this method treats detection as a regression problem, so it does not need complex pipes and only needs to run the neural network on the new image during the test to predict the detection results. The original YOLOv2 uses BN as regularization to accelerate convergence and avoid overfitting, and the BN layer and ReLU are connected to each convolution layer. To improve the detection performance of YOLO, Darknet-53, Anchor, FPN, and other structures were added to YOLOv3 proposed in 2018. Among them, using the residual component of ResNet for reference, the network can be built deeper, the model capacity is larger, and the feature learning ability is stronger. Darknet-53 makes the network easier to train and faster to merge through residual connections. Conv is used to implement downsampling to reduce the negative gradient effect caused by the pool. Darknet-53 uses a convolution operation with a step size of 2. YOLOv3 uses logistic regression to score the anchor boxes objectively, leaving some anchors with low scores before prediction. This dramatically reduces the amount of calculation.In a nut, YOLO’s average accuracy is higher than other real-time systems. Therefore, we have reason to believe that the YOLO algorithm has a bright application prospect in the field of real-time target detection.


## 2.2.2. YOLOv5 Detection Principle

In this study, we intend to improve the existing YOLOv5 model to increase the detection capability of small target objects for further application in X-ray security screening recognition. The detection of small target objects has always been a complex field in deep learning due to insufficient relevant information. At the same time, with the increase in people’s travel frequency, security identification will increase the requirement of detection speed, which will dramatically increase the complexity of target detection. To optimize the YOLOv5 algorithm, it is indispensable for us to understand its foundation and the current state of development.The structure of YOLOv5 algorithm is similar to YOLOv4 and further improved on the basis of it. The network structure of traditional YOLOv5 can be divided into four parts: input, backbone, neck network, and prediction (output), and the complete framework of YOLOv5-s, for example, is shown in Figures1 and 2.Figure 2
Architectural structure diagram of YOLOv5-s, which consists of input, backbone, neck, and prediction four parts.The main functional structures on the input side are mosaic data enhancement, adaptive anchor frame calculation, and adaptive image scaling. The input image resolutions of the YOLO algorithm are generally 416 × pixels × 416pixels, 512 × pixels × 512 pixels, and 608 × pixels × 608pixels. Experiments show that if the input resolution is higher, the model’s performance will improve. The data enhancement adopts the mosaic approach, which takes scaling, cropping, and random arrangement to the dataset, thus increasing the complexity of the dataset, making the dataset gets greatly enriched, and adding many small targets, which makes the robustness of trained model much better. Meanwhile, the number of images read from the dataset for training is smaller in each batch, which reduces the memory usage of GPU. In the YOLO series of detection algorithms, the default anchor frame length and width are initially invented for different targets. A prediction frame is an output based on the initially set anchor frame when the dataset is training. The difference between the labeled real frame and the prediction frame is calculated, and then, the parameters in the network structure are iteratively updated in the reverse direction. In the YOLOv5 algorithm, this feature is embedded in the structure, and the best anchor frame values are computed adaptively for different training sets at each learning. The adaptive image scaling in the target detection algorithm is decided by the length and width of images in the respective dataset. The original image is first scaled into a uniform standard size and then sent to the detection network after processing. When YOLOv5 zooms the original image, it can adaptively add the least black edge according to the image size. After the black edge of the image is processed, the amount of calculation will be reduced during reasoning, so that the target detection speed of the network will be improved.The main functional structures of the backbone network include the focus and the CSP modules. The key for the focus structure is the slicing operation, and the convolution operation after the slicing is completed. Different YOLOv5 network structures have exclusive numbers of convolution kernels, and the focus slicing operation is shown in Figure3(a). There are two types of CSP structures in YOLOv5. The CSP1_X structure in the backbone network consists of CBL module, RES unit module, and convolution layer, and the other CSP2_X structure is in the neck, which includes the convolution layer and X RES unit modules. Using the CSP module can enhance the network’s learning ability and make the trained model, which can keep lightweight and have high accuracy at the same time. The addition of the SPP module (spatial pyramid pooling) to the CSP increases the receptive field and extracts the most essential contextual features without causing a reduction in the operation speed. The structure of the two CSP modules is shown in Figure 3(b), and the structure of the SPP module is shown in Figure 3(c).Figure 3
Schematic of the details inside the YOLOv5-s networks. (a) Focus slice operation, (b) CSP module, and (c) and SPP structure.
(a)(b)(c)The neck network has an FPN + PANet structure. FPN is a top-down structure, and the predicted feature maps are computed by fusing the feature information from the higher layers with the lower layer features through an up-sampling operation [25]. The YOLOv5 network structure has a bottom-up feature pyramid added behind the FPN layer, in which there are two PANet structures. This has the advantage of conveying strong semantic features through the top-down FPN layer and strong localization features through the bottom-up feature pyramid. From different backbone layers to different detection layers, the parameter is aggregated. The path aggregation network structure is shown in Figure 4.Figure 4
Illustration of PANet framework in YOLOv5. (a) The backbone with FPN structure. (b) Bottom-up augmentation path. (c) Adaptive feature information pooling. (d) The branch of box and fully connection fusion. The channel dimension of feature map does not appear in the schematic.The GIOU loss function at the output is an improvement of the traditional IOU loss. Suppose the intersection of the prediction frame and the real frame isA and the concatenation set is B. IOU is defined as the intersection set A divided by the concatenation set B. The loss of IOU can be expressed as follows:(1)IOU_LOSS=1−IOU=1−AB.The loss of IOU is relatively simple, but there are also some problems. Firstly, there may be a situation in the prediction frame and the real frame does not intersect when the IOU is 0, which cannot reflect the distance between the prediction frame and the real frame. In addition, when the prediction frame and the real frame have the same size, the IOU may also be the same, and the IOU loss function cannot distinguish between these two situations. Therefore, GIOU loss is proposed for improvement. Defining the minimum outer rectangle of the prediction frame and the real frame be the set C, and the difference set S stands for the difference between the set C and the concatenated set B. Then, the GIOU loss can be expressed as follows:(2)GIOU_LOSS=1−GIOU=1−IOU−SC.The GIOU loss function improves how to measure the intersection scale and reduces the deficiency when it is simply IOU loss.


## 2.2.3. Comparison of YOLOv5 Network Structures

The four network structures of YOLOv5, YOLOv5-s, YOLOv5-m, YOLOv5-l, and YOLOv5-x, have the same framework, differing only in-depth and width, which are controlled by two parameters, depth multiple and width multiple. There are two CSP structures in the YOLOv5 network structure, CSP1 and CSP2, which have been mentioned above, and the depth of each CSP structure is different in the four networks, respectively.The number of convolutional kernels used in each network structure depends on the specific characteristics of locations, which directly affects the third dimension of the convolutional feature map, the width of the network. The more the number of convolutional kernels, the wider the width of the feature map and the better the learning ability of the network to extract features. The four network structures YOLOv5-s, YOLOv5-m, YOLOv5-l, and YOLOv5-x are getting deeper and wider in order, and the detection accuracy is also increasing successively, but the requirement for hardware configuration is also raised. Considering the individuality of different datasets, to ensure the overall detection performance, we need to use the YOLOv5 network structure with the most suitable quantization degree, depth, and feature map width in the series to train the model as much as possible. Table1 demonstrates the different complexity of different models.Table 1
Four network structures of YOLOv5. The parameters depth multiple and width multiple control the number of BottleneckCSP and the number of convolution kernels of the architectural models, respectively.
YOLOv5-sYOLOv5-mYOLOv5-lYOLOv5-xDepth multiple0.330.671.01.33Width multiple0.500.751.01.25BottleneckCSP BCSPn (true)1, 3, 32, 6, 63, 9, 94, 12, 12BottleneckCSP BCSPn (false)1234Number of convolution kernels32, 64, 128, 256, 51248, 96, 192, 384, 76864, 128, 256, 512, 102480, 160, 320, 640, 1280


## 2.3. Our Method’s Detection Principle

Although two-stage methods such as Faster-RCNN may yield better detection results, they are not suitable for time-insensitive systems due to their different detection principles. Therefore, our approach needs to improve the one-stage algorithm YOLOv5, which is much faster in detection speed, and improves its checking accuracy for smaller objects. Different types of feature pyramid networks (FPNs) [26–28] are obtained by investigating how to handle feature mappings, rather than just modifying the backbone, which can often be aggregated in different ways to enhance the backbone. Also, adding a transformer to the backbone network is a feasible approach [29], considering that small targets can be extracted with less feature information. In this part, we will introduce each module’s principle in detail.


### 2.3.1. Transformer Principle

Considering that the computation of traditional RNN is restricted to be sequential, that is, the relevant algorithm can only compute sequentially from left to right or from right to left. This mechanism brings two problems: one is the computation of time slicet depends on the computation result at t−1 moment, which limits the parallelism capability of the model. Another is that the information may be lost in sequential computation. The proposed Transformer model has successfully solved the two problems mentioned above. It uses the attention mechanism to reduce the distance between any two positions in the sequence to a constant and has better parallelism and conforms to the existing GPU framework. Transformer is essentially an encoder-decoder structure, and the structure of encoder-decoder is shown in Figure 5(a); the specific module of encoder is shown in Figure 5(b). The difference between the encoder and the decoder is that the latter has one more encoder-decoder attention. Two attention is used to calculate the input and output weights, respectively. Self-attention is the relationship between the current translation and the previous text translated. Encoder-decoder attention is the relationship between the current translation and the encoded feature vector.Figure 5
Illustration of internal details in transformer structure. (a) The modules of encoder-decoder. (b) The self-attention and the feed-forward neural network constitute one unit in transformer. (c) The completed transformer structure is the composite of self-attention, encoder-decoder attention, and feed-forward neural network.
(a)(b)(c)Then, we analyze the detailed structure in the transformer’s encoder. The data first go through a module called “self-attention,” which is the transformer’s core. In self-attention, each data possess three different vectors, which are query vectorQ, key vector K, and value vector V. They are obtained by multiplying the three different weight matrices by the embedding vector X from three different weight matrices WQ, WK, and WV, where the dimensions of the three matrices are the same, all being 512×64. A weighted eigenvector is obtained by self-attention called AttentionQ,K,V, which can also be denoted as vector Z, and the value is defined as follows:(3)Z=AttentionQ,K,V=softmaxQKTdkV.After getting the value ofAttentionQ,K,V, it will be sent to the next module of encoder—feed-forward neural network. This full connection has two layers, the activation function of the first layer is ReLU, and the second layer is a linear activation function that can be expressed as follows:(4)FNNZ=max0,ZW1+b1W2+b2.A whole trainable network structure is the stack of encoder and decoder, and we can get the complete transformer structure in Figure6. Transformer may not only be applied in the field of NLP machine translation. It has a very promising potential for scientific research.Figure 6
Transformer—model architecture.


### 2.3.2. BiFPN Principle

Since the introduction of FPN [2], it has been widely used for multi-scale feature fusion, and according to the previous introduction of neck part, FPN introduces a top-down channel to fuse features. Recently, PANet, NAS-FPN, and other studies have developed more cross-scale feature fusion network structures such as PANet [30], which adds a bottom-up channel to FPN, and NAS-FPN [31], which uses an irregular topology to search out. While fusing different input features, most previous works simply summarize them indiscriminately. However, since these different input features have different resolutions, we observe that they usually contribute unequally to the fused output features, which means that the feature information is not consistent across scales. At the same time, improved PANet and NAS-FPN bring a great computational effort. Thus, state-of-the-art object detectors are becoming more and more expensive (and some advanced target detectors show excellent performance even at the cost of RAM). For example, NAS-FPN-based detectors require 167M parameters and 3045B FLOPs (30 times more than RetinaNet) to achieve state-of-the-art accuracy. To address these problems, we applied the weighted bi-directional feature pyramid network (BiFPN), proposed by Tan et al., which can perform multi-scale feature fusion easily and quickly. The FPN, PANet, NAS-FPN, and BiFPN structures are, respectively, shown in Figure 7.Figure 7
Reticular formation of four structures. (a) FPN adopts a top-down path to mix different features fromP3 to P7. (b) PANet introduces an added down-top path based on FPN. (c) NAS-FPN uses neural networks to exploit unusual information and recycle the same module. (d) BiFPN adds extra branches to attain superior detection accuracy and realize a more efficient trade-off.
(a)(b)(c)(d)BiFPN, as shown in Figure7(d), is based on a simplified version of PANet by adding residual links, removing nodes with only one input edge, and performing weight fusion if the input and output nodes are at the same level. Adding residual links enhances the representation of features by simple residual operations. Removing nodes with a single input edge is because the nodes with a single input edge are not fused, that is why they have less information and do not contribute much to the final fusion. At the same time, removing a single input edge can reduce the computation and speed up the detection. BiFPN fuses more features without increasing the cost and removes the intermediate nodes of P3 and P7 in PANet, resulting in a simplified two-way network.


### 2.3.3. Coordinate Attention (CA)

The CA mechanism [32] has the following advantages. First, it can capture not only cross-channel information but also direction-aware and position-aware information, which can help the model to locate and identify the target of interest more precisely. Secondly, CA is flexible and lightweight, easily inserted into classical modules, such as the inverted residual block proposed by MobileNetV2 [33] and the sandglass block presented by MobileNeXt [34]. Both enhance features utilizing enhanced information representation. Finally, as a pretrained model, the CA mechanism can significantly benefit downstream tasks on top of lightweight networks, especially those where intensive prediction exists, such as semantic segmentation.The CA module encodes channel relationships and long-range dependencies with precise location information in two steps: coordinate information embedding and coordinate attention generation, structured as Figure8.Figure 8
Schematic diagram of the proposed coordinate attention block, while theX Avg Pool and Y Avg Pool stand for 1D horizontal and vertical global pooling, respectively.Firstly, we come to part of coordinate information embedding. Global pooling is commonly used in channel attention to globally encode spatial information as channel descriptors, and thus, it is difficult to preserve location information. The authors decompose global pooling into a pair of one-dimensional feature encoding operations to facilitate the attention module to capture spatial long-range dependencies with precise location information. In particular, for the inputX, first using the dimensions H,1 and 1,W of the pooling kernel encodes each channel along with the horizontal and vertical coordinate directions, so that the height h and the output of the first c output of the first channel are expressed as follows:(5)zchh=1W∑0≤i≤Wxch,j.Similarly, the width ofw of the first c output of the first channel is expressed as follows:(6)zcwh=1H∑0≤i≤Hxcj,w.These two transformations perform feature aggregation along with two spatial directions, returning a pair of direction-aware attentional maps. This is quite different from the SE(SENet) [35] module that generates a feature vector. Both transformations allow the attention module to capture long-range dependencies along one spatial direction and preserve precise location information along the other spatial direction, which helps the network locate the target of interest more accurately. This coordinate information embedding operation corresponds to the X Avg Pool and Y Avg Pool of Figure 8.Looking specifically at the operation of the CA mechanism, the two feature maps generated by the previous module are first cascaded and then transformed using a shared1×1 convolution F1 that is expressed in the following equation (7). The downsampling ration f∈RC/r×H+W is an intermediate feature map of the spatial information in the horizontal and vertical directions, and is used to control the size of the module as in the SE module.(7)f=δF1zh,zw.Then, along the spatial dimension,f is decomposed into two separate tensors f∈RC/r×H+W and fw∈RC/r×W, and then, two 1×1 convolution Fh and Fw is operated on feature maps fh and fw, respectively, and transform them into the same channels as the same as the input X. The result of the following equations is obtained:(8)gh=σFhfh,(9)gw=σFwfw.Thegh and gw are defined as attention weights. Then, the final output of the CA module can be expressed in the following equation:(10)yci,j=xci,j×gchi×gcwj.This part of the coordinate attention generation corresponds to the remaining part of Figure8, so that the CA module has completed both horizontal attention and vertical attention, and it is also a kind of channel attention.


## 2.3.1. Transformer Principle

Considering that the computation of traditional RNN is restricted to be sequential, that is, the relevant algorithm can only compute sequentially from left to right or from right to left. This mechanism brings two problems: one is the computation of time slicet depends on the computation result at t−1 moment, which limits the parallelism capability of the model. Another is that the information may be lost in sequential computation. The proposed Transformer model has successfully solved the two problems mentioned above. It uses the attention mechanism to reduce the distance between any two positions in the sequence to a constant and has better parallelism and conforms to the existing GPU framework. Transformer is essentially an encoder-decoder structure, and the structure of encoder-decoder is shown in Figure 5(a); the specific module of encoder is shown in Figure 5(b). The difference between the encoder and the decoder is that the latter has one more encoder-decoder attention. Two attention is used to calculate the input and output weights, respectively. Self-attention is the relationship between the current translation and the previous text translated. Encoder-decoder attention is the relationship between the current translation and the encoded feature vector.Figure 5
Illustration of internal details in transformer structure. (a) The modules of encoder-decoder. (b) The self-attention and the feed-forward neural network constitute one unit in transformer. (c) The completed transformer structure is the composite of self-attention, encoder-decoder attention, and feed-forward neural network.
(a)(b)(c)Then, we analyze the detailed structure in the transformer’s encoder. The data first go through a module called “self-attention,” which is the transformer’s core. In self-attention, each data possess three different vectors, which are query vectorQ, key vector K, and value vector V. They are obtained by multiplying the three different weight matrices by the embedding vector X from three different weight matrices WQ, WK, and WV, where the dimensions of the three matrices are the same, all being 512×64. A weighted eigenvector is obtained by self-attention called AttentionQ,K,V, which can also be denoted as vector Z, and the value is defined as follows:(3)Z=AttentionQ,K,V=softmaxQKTdkV.After getting the value ofAttentionQ,K,V, it will be sent to the next module of encoder—feed-forward neural network. This full connection has two layers, the activation function of the first layer is ReLU, and the second layer is a linear activation function that can be expressed as follows:(4)FNNZ=max0,ZW1+b1W2+b2.A whole trainable network structure is the stack of encoder and decoder, and we can get the complete transformer structure in Figure6. Transformer may not only be applied in the field of NLP machine translation. It has a very promising potential for scientific research.Figure 6
Transformer—model architecture.


## 2.3.2. BiFPN Principle

Since the introduction of FPN [2], it has been widely used for multi-scale feature fusion, and according to the previous introduction of neck part, FPN introduces a top-down channel to fuse features. Recently, PANet, NAS-FPN, and other studies have developed more cross-scale feature fusion network structures such as PANet [30], which adds a bottom-up channel to FPN, and NAS-FPN [31], which uses an irregular topology to search out. While fusing different input features, most previous works simply summarize them indiscriminately. However, since these different input features have different resolutions, we observe that they usually contribute unequally to the fused output features, which means that the feature information is not consistent across scales. At the same time, improved PANet and NAS-FPN bring a great computational effort. Thus, state-of-the-art object detectors are becoming more and more expensive (and some advanced target detectors show excellent performance even at the cost of RAM). For example, NAS-FPN-based detectors require 167M parameters and 3045B FLOPs (30 times more than RetinaNet) to achieve state-of-the-art accuracy. To address these problems, we applied the weighted bi-directional feature pyramid network (BiFPN), proposed by Tan et al., which can perform multi-scale feature fusion easily and quickly. The FPN, PANet, NAS-FPN, and BiFPN structures are, respectively, shown in Figure 7.Figure 7
Reticular formation of four structures. (a) FPN adopts a top-down path to mix different features fromP3 to P7. (b) PANet introduces an added down-top path based on FPN. (c) NAS-FPN uses neural networks to exploit unusual information and recycle the same module. (d) BiFPN adds extra branches to attain superior detection accuracy and realize a more efficient trade-off.
(a)(b)(c)(d)BiFPN, as shown in Figure7(d), is based on a simplified version of PANet by adding residual links, removing nodes with only one input edge, and performing weight fusion if the input and output nodes are at the same level. Adding residual links enhances the representation of features by simple residual operations. Removing nodes with a single input edge is because the nodes with a single input edge are not fused, that is why they have less information and do not contribute much to the final fusion. At the same time, removing a single input edge can reduce the computation and speed up the detection. BiFPN fuses more features without increasing the cost and removes the intermediate nodes of P3 and P7 in PANet, resulting in a simplified two-way network.


## 2.3.3. Coordinate Attention (CA)

The CA mechanism [32] has the following advantages. First, it can capture not only cross-channel information but also direction-aware and position-aware information, which can help the model to locate and identify the target of interest more precisely. Secondly, CA is flexible and lightweight, easily inserted into classical modules, such as the inverted residual block proposed by MobileNetV2 [33] and the sandglass block presented by MobileNeXt [34]. Both enhance features utilizing enhanced information representation. Finally, as a pretrained model, the CA mechanism can significantly benefit downstream tasks on top of lightweight networks, especially those where intensive prediction exists, such as semantic segmentation.The CA module encodes channel relationships and long-range dependencies with precise location information in two steps: coordinate information embedding and coordinate attention generation, structured as Figure8.Figure 8
Schematic diagram of the proposed coordinate attention block, while theX Avg Pool and Y Avg Pool stand for 1D horizontal and vertical global pooling, respectively.Firstly, we come to part of coordinate information embedding. Global pooling is commonly used in channel attention to globally encode spatial information as channel descriptors, and thus, it is difficult to preserve location information. The authors decompose global pooling into a pair of one-dimensional feature encoding operations to facilitate the attention module to capture spatial long-range dependencies with precise location information. In particular, for the inputX, first using the dimensions H,1 and 1,W of the pooling kernel encodes each channel along with the horizontal and vertical coordinate directions, so that the height h and the output of the first c output of the first channel are expressed as follows:(5)zchh=1W∑0≤i≤Wxch,j.Similarly, the width ofw of the first c output of the first channel is expressed as follows:(6)zcwh=1H∑0≤i≤Hxcj,w.These two transformations perform feature aggregation along with two spatial directions, returning a pair of direction-aware attentional maps. This is quite different from the SE(SENet) [35] module that generates a feature vector. Both transformations allow the attention module to capture long-range dependencies along one spatial direction and preserve precise location information along the other spatial direction, which helps the network locate the target of interest more accurately. This coordinate information embedding operation corresponds to the X Avg Pool and Y Avg Pool of Figure 8.Looking specifically at the operation of the CA mechanism, the two feature maps generated by the previous module are first cascaded and then transformed using a shared1×1 convolution F1 that is expressed in the following equation (7). The downsampling ration f∈RC/r×H+W is an intermediate feature map of the spatial information in the horizontal and vertical directions, and is used to control the size of the module as in the SE module.(7)f=δF1zh,zw.Then, along the spatial dimension,f is decomposed into two separate tensors f∈RC/r×H+W and fw∈RC/r×W, and then, two 1×1 convolution Fh and Fw is operated on feature maps fh and fw, respectively, and transform them into the same channels as the same as the input X. The result of the following equations is obtained:(8)gh=σFhfh,(9)gw=σFwfw.Thegh and gw are defined as attention weights. Then, the final output of the CA module can be expressed in the following equation:(10)yci,j=xci,j×gchi×gcwj.This part of the coordinate attention generation corresponds to the remaining part of Figure8, so that the CA module has completed both horizontal attention and vertical attention, and it is also a kind of channel attention.


## 2.4. TB-YOLOv5 Principle

For small object target detection, the transformer module can enhance the extracted information to make up for the small volume object contextual information, and the BiFPN structure with three inputs can better integrate the input features. These methods are introduced in detail in Sections2.3.1 and 2.3.2. Both are better means to enhance small target detection. The addition of CA mechanism makes up for the problem that the features extracted by the convolution operation in the general algorithm are more limited. The information obtained is more abbreviated, which makes it challenging to integrate the corresponding features, and allows the neck part to better focus on the detection object we want when performing feature extraction, thus improving the detection accuracy of small target objects.To increase the feature information extraction capability, we added transformer capability to the bottom layer of backbone to increase, extract information, and optimize detection. The PANet of the traditional neck part is replaced with a BiFPN structure, and a CA mechanism is added to form the new neck structure named attention-BiFPN. A CA module can be regarded as a computational unit to enhance the feature. A CA module can be considered as a computational unit that enhances the expressiveness of features in a mobile network. It can take any intermediate feature tensor as input and output the same size as the tensor with enhanced representations by transformation [32]. The original YOLOv5 algorithm is optimized with the added transformer structure and the designed attention-BiFPN mechanism, and the improved algorithm is named TB-YOLOv5.In addition, after the above analysis, we know that a significant advantage of the one-stage series algorithm for X-ray security detection is that it has a faster detection speed than the traditional two-stage algorithm, which can be better applied to real-time systems. This is an important reason why the CA mechanism is used, which has the advantage of fast detection speed, compared with some attention mechanisms such as CBAM (convolutional block attention module), an attention mechanism compared with [36], which needs to detect target features in both time and space dimensions of serial detection of target features, so CBAM attention mechanism detection increases the detection time, which contradicts our intention of trying to apply the algorithm to X-ray cases. On the contrary, CA is used to extract information features by coordinates, modeling the location information faster, and the detection time is controlled and suitable for detection needs. In addition, compared with SE block, another attention mechanism has been widely used in recent years, and it only considers the importance of each channel by modeling the channel relationships and ignores the location information. However, the location information is important for generating spatially selective attention maps, which cannot improve the detection results for small targets. The network structure of TB-YOLOv5 is shown in Figure 9.Figure 9
Network structure model and parameters of TB-YOLOv5, which consisted of input, backbone, attention-BiFPN, and output four parts while the input section is omitted.


## 2.5. Evaluating Indicators

As the number of iterations increases during the training process, various relevant parameters change. In our study, we adopt some usual indicators to measure the performance of results and the exact definitions of them are as follows [37–39]:Loss Indexes. It is defined by the GIOU loss function; the closer the value is to 0, the more accurate the target frame, detection, and classification are.Precision. It is defined by the number of correct targets marked divided by the total number of targets marked; the closer to 1, the higher the accuracy rate. TP and FP mean true positive and false positive, respectively.(11)Precision=TPTP+FP×100%.Recall. It is defined by the number of correct targets marked divided by the total number of targets to be marked; the closer to 1, the higher the accuracy rate, and FN means false negative.(12)Recall=TPTP+FN.mAP.5. AP is the area enclosed after plotting with precision and recall as the two axes; when IOU is set to 0.5, the closer to 1, the higher the accuracy. The mAP.5-small especially expresses the small object detection results in this article.(13)mAP=∑i=1cAPic, where C stands for the total number of categories and APi indicates the ith category value of AP.


## 3. Results and Discussion

### 3.1. Parameter Setting and Experimental Environment

When a complete dataset passes through the neural network once and returns once, the process is called an epoch. Passing the complete dataset once in the neural network is not enough, and we need to pass the complete dataset multiple times in the same neural network. We use a finite number of datasets, and we optimize the learning process using an iterative process called gradient descent. Therefore, when an epoch is too large for the computer, it needs to be divided into smaller pieces. However, as the number of epochs increases, the number of updates of the weights in the neural network increases, and the curve may go from underfitting to overfitting. In this experiment, we choose all epochs = 500.The batch size will determine the number of samples we train at a time, and it will also affect the degree of optimization and speed of the model. Batch size is chosen correctly to find the best balance between memory efficiency and memory capacity. A proper increase in batch size can improve memory utilization by parallelization, and the number of iterations in a single epoch is reduced, increasing the running speed in this experiment, and batch size = 8.The model training process uses the original 4000 generated datasets with X-ray security images, divided into a training set and a validation set in the ratio of 8 : 2.The configuration of the training device used in our research is Intel Xeon E5-2678 processor, NVIDIA 2080 Ti graphics card, 256 GB RAM, software running environment is Windows 10 operating system, deep learning framework version number Torch 1.7, and Python 3.7 used libraries including CV2, Matplotlib, and NumPy.


### 3.2. Detection Based on YOLOv5

The initial YOLOv5 target detection algorithm network structure (YOLOv5-s, YOLOv5-m, YOLOv5-l, and YOLOv5-x) was compared for detection accuracy on the X-ray security dataset. The detection accuracies for different categories of objects and the overall average detection accuracy are shown in Figure10. We specifically define the set of small target objects to further observe the improvement effect of small targets specifically. Section 2.1 has stated that the small target objects in our dataset include seal, knife, scissors, battery, and small bottle glass. We have additionally marked the small objects in red in the figure.Figure 10
Detection results of YOLOv5 algorithm for four network structures. Small target objects are marked in red.By comparing the four network structures, we can find that for the same dataset of X-ray security screening, the versionss, m, l, and x of YOLOv5 algorithms have a large difference in detection accuracy for 12 different objects. The average detection accuracy between the four networks is mainly in this accuracy interval of 0.54–0.58. Among them, the detection results of electric equipment are as follows: umbrella reaches 0.8 or even 0.9, most of the object detection accuracy is around 60, for small target objects detection accuracy is lower, only 0.3–0.4, and for scissors detection accuracy is even lower to only about 0.2.Although there is a difference in the number of pictures of different objects in the dataset, the expandable baton, which has a smaller number of pictures in the dataset, has a higher detection accuracy than some objects with a much larger number of pictures in the dataset compared with itself.  On the contrary, some of the small objects, such as scissor, shows poor detection results even when it has a certain number of image data to ensure the training effect. Therefore, although it is impossible to control the same number of materials for each image in the dataset, we can see that the algorithm of the YOLOv5 series will have a decrease in detection accuracy for small targets due to less information feature extraction. Therefore, there is a great need to improve this problem to ensure that the YOLOv5 algorithm can be better used in real target detection conditions.


### 3.3. Detection Based on TB-YOLOv5

#### 3.3.1. Improvement Method

In the backbone structure of TB-YOLOv5, the focus structure is replaced by the Conv module. In addition, a new detection branch has been added to better detect the lower-level feature information. As we can see in Figure9, the first C3 structure in the backbone structure is also input to the attention-BiFPN to detect the feature map, and the feature information is extracted during the optimization process of the CA attention module, and then, the optimized information is input to the detection layer of TB-YOLOv5 to obtain the results and output. The C3 structure is suitable for YOLOv5 5.0 and higher, as shown in Figure 11(a). Because of its low number of convolutional layers, the newly added detection branch contains lower-level feature information. The detection module is used to fuse the lower-level visual feature information in the backbone structure with the higher-level visual feature information that our YOLOv5-BT algorithm can obtain a more robust output. The attention-BiFPN-based aggregation path is shown in Figure 11(b).Figure 11
(a) Schematic diagram of the C3 module structure removes a Conv and a BN layer compared with CSP mentioned in Section2.2.2. (b) Aggregation path based on the attention-BiFPN structure.
(a)(b)Using the same no pretrained versionss, m, l, and x network structure model as the regular YOLOv5 for the improved TB-YOLOv5 algorithm, the detection results obtained for each of the 12 objects are shown in Figure 12. Compared with the data in Figure 10, the mAP.5 values under the detection of TB-YOLOv5 algorithm are significantly improved. Some objects such as small glass bottles and plastic bottles have improved from less than 0.6 to about 0.65–0.7. The detection accuracy of the electric baton is somewhat reduced, which may be related to its special morphological structure and the division of the dataset. The indicators are shown in Figure 13. Overall, TB-YOLOv5 improves the detection of this dataset very well.Figure 12
Detection results of TB-YOLOv5 algorithm for four network structures. Small target objects are marked in red.Figure 13
Changes in relevant parameters of training model. Most of the evaluating indicator changes tend to be stable after 500 training iterations.Figure14 depicts the difference between the modified mAP.5 value of TB-YOLOv5 and the YOLOv5 training results. This bar chart intuitively makes us feel the improvement of TB-YOLOv5’s object detection efficiency. Most of the products have an increased detection accuracy under different networks, and version x of YOLOv5 shows the greatest performance in general. The electric baton only gets inspection development under the YOLOv5-m structure.Figure 14
Changes in the detection accuracy of 12 objects between TB-YOLOv5 and YOLOv5. The data above the abscissa mean the improvement achieved by TB-YOLOv5, while the data below the abscissa stand for the negative effect trained by TP-YOLOv5.


#### 3.3.2. Comparison of Model Detection Effect and Small Object Detection

The detection accuracy of the mainstream one-stage target detection algorithms (YOLOv3, YOLOv4, and YOLOv5) is compared with the proposed TB-YOLOv5 in this study on the X-ray security dataset. Also, the average detection accuracy of mAP.5-small for five small target objects (knife, scissors, small glass bottle, battery, and seal) and the overall average detection accuracy of mAP.5 are shown in Table2. From the data in the table, it can be seen that the YOLOv5 algorithm has significantly improved the performance of detection results compared with YOLOv3 and YOLOv4 algorithms, and our modified TB-YOLOv5 algorithm has additional development results compared with YOLOv5. Furthermore, the four network versions s, m, l, and x of YOLOv5 become better in detection results as the depth and width of the network structure increase.Table 2
Performance comparison of different object detection algorithms. With the progression of YOLO detector, the performance becomes much better for newer version, and the different network structures show different detecting results for YOLOv5.
MethodsmAP.5mAP.5-smallYOLOv30.4790.392YOLOv40.4920.4022YOLOv5-s0.5320.428YOLOv5-m0.5520.4534YOLOv5-l0.5710.4804YOLOv5-x0.5780.4846TB-YOLOv5-s0.5680.483TB-YOLOv5-m0.6160.5312TB-YOLOv5-l0.6370.5586TB-YOLOv5-x0.6640.5982The improvement of detection results by the TB-YOLOv5 algorithm is different in different network structures. The comparison of the detection results of TB-YOLOv5 and YOLOv5 algorithms with different structures, including the detection results of all objects mAP.5 and small target objects mAP.5-small, is shown to us in Table3. In comparing the detection results of all objects, the improvement of the s network structure is poor, and with only 6.8% improvement, the improvement of the m and l network structures is similar, both reaching 11.6%. In addition, the network model of TB-YOLOv5-x has the best detection accuracy of 66.4% for the X-ray security inspection dataset, which is a 14.9% improvement compared with YOLOv5. For small target detection objects, the m-network structure is slightly better than the l-network structure. x-Model has the best small target detection, and s-model has the worst results. Compared with the total mAP.5 value, we can discover that the mAP.5-small detection value is improved more significantly. This shows that our algorithm improvement for smaller objects in the dataset achieves the expected results numerically.Table 3
Performance comparison of YOLOv5 and TB-YOLOv5. With the increase in depth and width, the detection results of both YOLOv5 and TB-YOLOv5 become much better.
ScalesmAP.5mAP.5-smallYOLOv5TB-YOLOv5Difference (%)YOLOv5TB-YOLOv5Difference (%)S0.5320.5686.80.4280.48312.9M0.5520.61611.60.45340.531217.2L0.5710.63711.60.48040.558616.3X0.5780.66414.90.48460.598223.4Then, we come to the inference of the model. As a real-time system, the X-ray security inspection should instantly judge the baggage passing the machine. We utilize the training outcome to conduct detection operations on the validation set and come to the detecting time of 800 images. Though the total inference of TB-YOLOv5 indeed has an extension compared with YOLOv5, which can be attributed to extra models that are added in it, the costing time on each image only has a few tens of microseconds. The microsecond difference can be overlooked because it takes several seconds for the luggage compartment to pass through the detector. Meanwhile, a better performance in inspecting accuracy plays a more important role in our research, so the tiny increase in detection time can be tolerated. Table4 demonstrates different inferences under different network scales for the whole validation set and per image, respectively.Table 4
Inference comparison of YOLOv5 and TB-YOLOv5. With the increase in depth and width, the detection speed of both YOLOv5 and TB-YOLOv5 becomes much slower.
ScalesInference (ms) (800 images)Inference (μs) (per image)YOLOv5TB-YOLOv5Difference (%)YOLOv5TB-YOLOv5Difference (%)s40.354.635.550.3868.2535.5m44.870.557.456.0088.1357.4l46.271.354.357.7589.1354.3x51.483.462.364.25104.2562.3To get a more intuitive feeling of the improvement of TB-YOLOv5 in small target object detection, we select some images in the validation set for comparison. As shown in Figure15, from the yellow dashed box in the figure, we can notice that these small target objects are easily missed in the YOLOv5 algorithm because of the missing information of small volume features. However, in our improved TB-YOLOv5, these small objects are successfully detected. The improvement of the algorithm can be seen more intuitively on the detection effect of the X-ray security dataset.Figure 15
Visual demonstration of the improved detection comparisons of TB-YOLOv5 compared with YOLOv5 over some X-ray dataset images covering small-scale objects under x network. Yellow dotted lines circle the missed inspection of small targets investigated by YOLOv5, while they are all detected by TB-YOLOv5 algorithms.Other images in the validation set are shown in Figure16. Though the missing inspection is still existed, from the yellow dashed box in the figure, we can notice that some relevant “big scale” target objects do not have obvious performance improvement in the TB-YOLOv5 algorithm because the characteristic features are enough. Consequently, the “umbrella” and “electronic equipment” are both detected, and the “electronic equipment” show better detection performance under TB-YOLOv5 structure.Figure 16
Visual demonstration of the improved detection comparisons of TB-YOLOv5 compared with YOLOv5 over some X-ray dataset images of other objects under x network. Yellow dotted lines circle the missed inspection of small targets investigated by YOLOv5, while they are detected by TB-YOLOv5 algorithms. Other things are both detected by the two network structure.


### 3.4. Discussion

In our study of popular object detectors such as YOLOv5 to better detect smaller objects, we are able to identify architectural modifications that offer significant performance improvements compared with the original model. The context in which we apply the proposed technique, X-ray security detection, is an environment that can benefit significantly from such improvements. As we have seen, this change does have a quantifiable impact on detection. In this work, we have not only significantly improved detection performance but also identified specific techniques that can be applied to any other application involving the detection of small- or long-range targets.The result is that the TB-YOLOv5 family of models outperforms the YOLOv5 class of models in X-ray security screening, especially for smaller objects, which has been the focus of this study. At the same time, detection performance is improved and enhanced for medium-sized objects. Although our focus here is on modifying the popular YOLOv5 model, the methods and techniques we explore can potentially evolve into a wholly original model structure.Finally, while this study suggests significant empirical gains from the proposed architectural changes, the consistency and generalizability of the results can and should be further investigated. For example, further testing using different datasets and possible challenges such as security detection would greatly aid the analysis. While we have demonstrated the usefulness of the technique our study presented, these techniques can only be refined and better understood when applied to different environments and settings. Doing so would be an important step toward a more robust solution for small target detection. In addition, there are more directions and techniques that would fit well into this topic and were not considered in this study, but these will remain the subject of future research.


## 3.1. Parameter Setting and Experimental Environment

When a complete dataset passes through the neural network once and returns once, the process is called an epoch. Passing the complete dataset once in the neural network is not enough, and we need to pass the complete dataset multiple times in the same neural network. We use a finite number of datasets, and we optimize the learning process using an iterative process called gradient descent. Therefore, when an epoch is too large for the computer, it needs to be divided into smaller pieces. However, as the number of epochs increases, the number of updates of the weights in the neural network increases, and the curve may go from underfitting to overfitting. In this experiment, we choose all epochs = 500.The batch size will determine the number of samples we train at a time, and it will also affect the degree of optimization and speed of the model. Batch size is chosen correctly to find the best balance between memory efficiency and memory capacity. A proper increase in batch size can improve memory utilization by parallelization, and the number of iterations in a single epoch is reduced, increasing the running speed in this experiment, and batch size = 8.The model training process uses the original 4000 generated datasets with X-ray security images, divided into a training set and a validation set in the ratio of 8 : 2.The configuration of the training device used in our research is Intel Xeon E5-2678 processor, NVIDIA 2080 Ti graphics card, 256 GB RAM, software running environment is Windows 10 operating system, deep learning framework version number Torch 1.7, and Python 3.7 used libraries including CV2, Matplotlib, and NumPy.


## 3.2. Detection Based on YOLOv5

The initial YOLOv5 target detection algorithm network structure (YOLOv5-s, YOLOv5-m, YOLOv5-l, and YOLOv5-x) was compared for detection accuracy on the X-ray security dataset. The detection accuracies for different categories of objects and the overall average detection accuracy are shown in Figure10. We specifically define the set of small target objects to further observe the improvement effect of small targets specifically. Section 2.1 has stated that the small target objects in our dataset include seal, knife, scissors, battery, and small bottle glass. We have additionally marked the small objects in red in the figure.Figure 10
Detection results of YOLOv5 algorithm for four network structures. Small target objects are marked in red.By comparing the four network structures, we can find that for the same dataset of X-ray security screening, the versionss, m, l, and x of YOLOv5 algorithms have a large difference in detection accuracy for 12 different objects. The average detection accuracy between the four networks is mainly in this accuracy interval of 0.54–0.58. Among them, the detection results of electric equipment are as follows: umbrella reaches 0.8 or even 0.9, most of the object detection accuracy is around 60, for small target objects detection accuracy is lower, only 0.3–0.4, and for scissors detection accuracy is even lower to only about 0.2.Although there is a difference in the number of pictures of different objects in the dataset, the expandable baton, which has a smaller number of pictures in the dataset, has a higher detection accuracy than some objects with a much larger number of pictures in the dataset compared with itself.  On the contrary, some of the small objects, such as scissor, shows poor detection results even when it has a certain number of image data to ensure the training effect. Therefore, although it is impossible to control the same number of materials for each image in the dataset, we can see that the algorithm of the YOLOv5 series will have a decrease in detection accuracy for small targets due to less information feature extraction. Therefore, there is a great need to improve this problem to ensure that the YOLOv5 algorithm can be better used in real target detection conditions.


## 3.3. Detection Based on TB-YOLOv5

### 3.3.1. Improvement Method

In the backbone structure of TB-YOLOv5, the focus structure is replaced by the Conv module. In addition, a new detection branch has been added to better detect the lower-level feature information. As we can see in Figure9, the first C3 structure in the backbone structure is also input to the attention-BiFPN to detect the feature map, and the feature information is extracted during the optimization process of the CA attention module, and then, the optimized information is input to the detection layer of TB-YOLOv5 to obtain the results and output. The C3 structure is suitable for YOLOv5 5.0 and higher, as shown in Figure 11(a). Because of its low number of convolutional layers, the newly added detection branch contains lower-level feature information. The detection module is used to fuse the lower-level visual feature information in the backbone structure with the higher-level visual feature information that our YOLOv5-BT algorithm can obtain a more robust output. The attention-BiFPN-based aggregation path is shown in Figure 11(b).Figure 11
(a) Schematic diagram of the C3 module structure removes a Conv and a BN layer compared with CSP mentioned in Section2.2.2. (b) Aggregation path based on the attention-BiFPN structure.
(a)(b)Using the same no pretrained versionss, m, l, and x network structure model as the regular YOLOv5 for the improved TB-YOLOv5 algorithm, the detection results obtained for each of the 12 objects are shown in Figure 12. Compared with the data in Figure 10, the mAP.5 values under the detection of TB-YOLOv5 algorithm are significantly improved. Some objects such as small glass bottles and plastic bottles have improved from less than 0.6 to about 0.65–0.7. The detection accuracy of the electric baton is somewhat reduced, which may be related to its special morphological structure and the division of the dataset. The indicators are shown in Figure 13. Overall, TB-YOLOv5 improves the detection of this dataset very well.Figure 12
Detection results of TB-YOLOv5 algorithm for four network structures. Small target objects are marked in red.Figure 13
Changes in relevant parameters of training model. Most of the evaluating indicator changes tend to be stable after 500 training iterations.Figure14 depicts the difference between the modified mAP.5 value of TB-YOLOv5 and the YOLOv5 training results. This bar chart intuitively makes us feel the improvement of TB-YOLOv5’s object detection efficiency. Most of the products have an increased detection accuracy under different networks, and version x of YOLOv5 shows the greatest performance in general. The electric baton only gets inspection development under the YOLOv5-m structure.Figure 14
Changes in the detection accuracy of 12 objects between TB-YOLOv5 and YOLOv5. The data above the abscissa mean the improvement achieved by TB-YOLOv5, while the data below the abscissa stand for the negative effect trained by TP-YOLOv5.


### 3.3.2. Comparison of Model Detection Effect and Small Object Detection

The detection accuracy of the mainstream one-stage target detection algorithms (YOLOv3, YOLOv4, and YOLOv5) is compared with the proposed TB-YOLOv5 in this study on the X-ray security dataset. Also, the average detection accuracy of mAP.5-small for five small target objects (knife, scissors, small glass bottle, battery, and seal) and the overall average detection accuracy of mAP.5 are shown in Table2. From the data in the table, it can be seen that the YOLOv5 algorithm has significantly improved the performance of detection results compared with YOLOv3 and YOLOv4 algorithms, and our modified TB-YOLOv5 algorithm has additional development results compared with YOLOv5. Furthermore, the four network versions s, m, l, and x of YOLOv5 become better in detection results as the depth and width of the network structure increase.Table 2
Performance comparison of different object detection algorithms. With the progression of YOLO detector, the performance becomes much better for newer version, and the different network structures show different detecting results for YOLOv5.
MethodsmAP.5mAP.5-smallYOLOv30.4790.392YOLOv40.4920.4022YOLOv5-s0.5320.428YOLOv5-m0.5520.4534YOLOv5-l0.5710.4804YOLOv5-x0.5780.4846TB-YOLOv5-s0.5680.483TB-YOLOv5-m0.6160.5312TB-YOLOv5-l0.6370.5586TB-YOLOv5-x0.6640.5982The improvement of detection results by the TB-YOLOv5 algorithm is different in different network structures. The comparison of the detection results of TB-YOLOv5 and YOLOv5 algorithms with different structures, including the detection results of all objects mAP.5 and small target objects mAP.5-small, is shown to us in Table3. In comparing the detection results of all objects, the improvement of the s network structure is poor, and with only 6.8% improvement, the improvement of the m and l network structures is similar, both reaching 11.6%. In addition, the network model of TB-YOLOv5-x has the best detection accuracy of 66.4% for the X-ray security inspection dataset, which is a 14.9% improvement compared with YOLOv5. For small target detection objects, the m-network structure is slightly better than the l-network structure. x-Model has the best small target detection, and s-model has the worst results. Compared with the total mAP.5 value, we can discover that the mAP.5-small detection value is improved more significantly. This shows that our algorithm improvement for smaller objects in the dataset achieves the expected results numerically.Table 3
Performance comparison of YOLOv5 and TB-YOLOv5. With the increase in depth and width, the detection results of both YOLOv5 and TB-YOLOv5 become much better.
ScalesmAP.5mAP.5-smallYOLOv5TB-YOLOv5Difference (%)YOLOv5TB-YOLOv5Difference (%)S0.5320.5686.80.4280.48312.9M0.5520.61611.60.45340.531217.2L0.5710.63711.60.48040.558616.3X0.5780.66414.90.48460.598223.4Then, we come to the inference of the model. As a real-time system, the X-ray security inspection should instantly judge the baggage passing the machine. We utilize the training outcome to conduct detection operations on the validation set and come to the detecting time of 800 images. Though the total inference of TB-YOLOv5 indeed has an extension compared with YOLOv5, which can be attributed to extra models that are added in it, the costing time on each image only has a few tens of microseconds. The microsecond difference can be overlooked because it takes several seconds for the luggage compartment to pass through the detector. Meanwhile, a better performance in inspecting accuracy plays a more important role in our research, so the tiny increase in detection time can be tolerated. Table4 demonstrates different inferences under different network scales for the whole validation set and per image, respectively.Table 4
Inference comparison of YOLOv5 and TB-YOLOv5. With the increase in depth and width, the detection speed of both YOLOv5 and TB-YOLOv5 becomes much slower.
ScalesInference (ms) (800 images)Inference (μs) (per image)YOLOv5TB-YOLOv5Difference (%)YOLOv5TB-YOLOv5Difference (%)s40.354.635.550.3868.2535.5m44.870.557.456.0088.1357.4l46.271.354.357.7589.1354.3x51.483.462.364.25104.2562.3To get a more intuitive feeling of the improvement of TB-YOLOv5 in small target object detection, we select some images in the validation set for comparison. As shown in Figure15, from the yellow dashed box in the figure, we can notice that these small target objects are easily missed in the YOLOv5 algorithm because of the missing information of small volume features. However, in our improved TB-YOLOv5, these small objects are successfully detected. The improvement of the algorithm can be seen more intuitively on the detection effect of the X-ray security dataset.Figure 15
Visual demonstration of the improved detection comparisons of TB-YOLOv5 compared with YOLOv5 over some X-ray dataset images covering small-scale objects under x network. Yellow dotted lines circle the missed inspection of small targets investigated by YOLOv5, while they are all detected by TB-YOLOv5 algorithms.Other images in the validation set are shown in Figure16. Though the missing inspection is still existed, from the yellow dashed box in the figure, we can notice that some relevant “big scale” target objects do not have obvious performance improvement in the TB-YOLOv5 algorithm because the characteristic features are enough. Consequently, the “umbrella” and “electronic equipment” are both detected, and the “electronic equipment” show better detection performance under TB-YOLOv5 structure.Figure 16
Visual demonstration of the improved detection comparisons of TB-YOLOv5 compared with YOLOv5 over some X-ray dataset images of other objects under x network. Yellow dotted lines circle the missed inspection of small targets investigated by YOLOv5, while they are detected by TB-YOLOv5 algorithms. Other things are both detected by the two network structure.


## 3.3.1. Improvement Method

In the backbone structure of TB-YOLOv5, the focus structure is replaced by the Conv module. In addition, a new detection branch has been added to better detect the lower-level feature information. As we can see in Figure9, the first C3 structure in the backbone structure is also input to the attention-BiFPN to detect the feature map, and the feature information is extracted during the optimization process of the CA attention module, and then, the optimized information is input to the detection layer of TB-YOLOv5 to obtain the results and output. The C3 structure is suitable for YOLOv5 5.0 and higher, as shown in Figure 11(a). Because of its low number of convolutional layers, the newly added detection branch contains lower-level feature information. The detection module is used to fuse the lower-level visual feature information in the backbone structure with the higher-level visual feature information that our YOLOv5-BT algorithm can obtain a more robust output. The attention-BiFPN-based aggregation path is shown in Figure 11(b).Figure 11
(a) Schematic diagram of the C3 module structure removes a Conv and a BN layer compared with CSP mentioned in Section2.2.2. (b) Aggregation path based on the attention-BiFPN structure.
(a)(b)Using the same no pretrained versionss, m, l, and x network structure model as the regular YOLOv5 for the improved TB-YOLOv5 algorithm, the detection results obtained for each of the 12 objects are shown in Figure 12. Compared with the data in Figure 10, the mAP.5 values under the detection of TB-YOLOv5 algorithm are significantly improved. Some objects such as small glass bottles and plastic bottles have improved from less than 0.6 to about 0.65–0.7. The detection accuracy of the electric baton is somewhat reduced, which may be related to its special morphological structure and the division of the dataset. The indicators are shown in Figure 13. Overall, TB-YOLOv5 improves the detection of this dataset very well.Figure 12
Detection results of TB-YOLOv5 algorithm for four network structures. Small target objects are marked in red.Figure 13
Changes in relevant parameters of training model. Most of the evaluating indicator changes tend to be stable after 500 training iterations.Figure14 depicts the difference between the modified mAP.5 value of TB-YOLOv5 and the YOLOv5 training results. This bar chart intuitively makes us feel the improvement of TB-YOLOv5’s object detection efficiency. Most of the products have an increased detection accuracy under different networks, and version x of YOLOv5 shows the greatest performance in general. The electric baton only gets inspection development under the YOLOv5-m structure.Figure 14
Changes in the detection accuracy of 12 objects between TB-YOLOv5 and YOLOv5. The data above the abscissa mean the improvement achieved by TB-YOLOv5, while the data below the abscissa stand for the negative effect trained by TP-YOLOv5.


## 3.3.2. Comparison of Model Detection Effect and Small Object Detection

The detection accuracy of the mainstream one-stage target detection algorithms (YOLOv3, YOLOv4, and YOLOv5) is compared with the proposed TB-YOLOv5 in this study on the X-ray security dataset. Also, the average detection accuracy of mAP.5-small for five small target objects (knife, scissors, small glass bottle, battery, and seal) and the overall average detection accuracy of mAP.5 are shown in Table2. From the data in the table, it can be seen that the YOLOv5 algorithm has significantly improved the performance of detection results compared with YOLOv3 and YOLOv4 algorithms, and our modified TB-YOLOv5 algorithm has additional development results compared with YOLOv5. Furthermore, the four network versions s, m, l, and x of YOLOv5 become better in detection results as the depth and width of the network structure increase.Table 2
Performance comparison of different object detection algorithms. With the progression of YOLO detector, the performance becomes much better for newer version, and the different network structures show different detecting results for YOLOv5.
MethodsmAP.5mAP.5-smallYOLOv30.4790.392YOLOv40.4920.4022YOLOv5-s0.5320.428YOLOv5-m0.5520.4534YOLOv5-l0.5710.4804YOLOv5-x0.5780.4846TB-YOLOv5-s0.5680.483TB-YOLOv5-m0.6160.5312TB-YOLOv5-l0.6370.5586TB-YOLOv5-x0.6640.5982The improvement of detection results by the TB-YOLOv5 algorithm is different in different network structures. The comparison of the detection results of TB-YOLOv5 and YOLOv5 algorithms with different structures, including the detection results of all objects mAP.5 and small target objects mAP.5-small, is shown to us in Table3. In comparing the detection results of all objects, the improvement of the s network structure is poor, and with only 6.8% improvement, the improvement of the m and l network structures is similar, both reaching 11.6%. In addition, the network model of TB-YOLOv5-x has the best detection accuracy of 66.4% for the X-ray security inspection dataset, which is a 14.9% improvement compared with YOLOv5. For small target detection objects, the m-network structure is slightly better than the l-network structure. x-Model has the best small target detection, and s-model has the worst results. Compared with the total mAP.5 value, we can discover that the mAP.5-small detection value is improved more significantly. This shows that our algorithm improvement for smaller objects in the dataset achieves the expected results numerically.Table 3
Performance comparison of YOLOv5 and TB-YOLOv5. With the increase in depth and width, the detection results of both YOLOv5 and TB-YOLOv5 become much better.
ScalesmAP.5mAP.5-smallYOLOv5TB-YOLOv5Difference (%)YOLOv5TB-YOLOv5Difference (%)S0.5320.5686.80.4280.48312.9M0.5520.61611.60.45340.531217.2L0.5710.63711.60.48040.558616.3X0.5780.66414.90.48460.598223.4Then, we come to the inference of the model. As a real-time system, the X-ray security inspection should instantly judge the baggage passing the machine. We utilize the training outcome to conduct detection operations on the validation set and come to the detecting time of 800 images. Though the total inference of TB-YOLOv5 indeed has an extension compared with YOLOv5, which can be attributed to extra models that are added in it, the costing time on each image only has a few tens of microseconds. The microsecond difference can be overlooked because it takes several seconds for the luggage compartment to pass through the detector. Meanwhile, a better performance in inspecting accuracy plays a more important role in our research, so the tiny increase in detection time can be tolerated. Table4 demonstrates different inferences under different network scales for the whole validation set and per image, respectively.Table 4
Inference comparison of YOLOv5 and TB-YOLOv5. With the increase in depth and width, the detection speed of both YOLOv5 and TB-YOLOv5 becomes much slower.
ScalesInference (ms) (800 images)Inference (μs) (per image)YOLOv5TB-YOLOv5Difference (%)YOLOv5TB-YOLOv5Difference (%)s40.354.635.550.3868.2535.5m44.870.557.456.0088.1357.4l46.271.354.357.7589.1354.3x51.483.462.364.25104.2562.3To get a more intuitive feeling of the improvement of TB-YOLOv5 in small target object detection, we select some images in the validation set for comparison. As shown in Figure15, from the yellow dashed box in the figure, we can notice that these small target objects are easily missed in the YOLOv5 algorithm because of the missing information of small volume features. However, in our improved TB-YOLOv5, these small objects are successfully detected. The improvement of the algorithm can be seen more intuitively on the detection effect of the X-ray security dataset.Figure 15
Visual demonstration of the improved detection comparisons of TB-YOLOv5 compared with YOLOv5 over some X-ray dataset images covering small-scale objects under x network. Yellow dotted lines circle the missed inspection of small targets investigated by YOLOv5, while they are all detected by TB-YOLOv5 algorithms.Other images in the validation set are shown in Figure16. Though the missing inspection is still existed, from the yellow dashed box in the figure, we can notice that some relevant “big scale” target objects do not have obvious performance improvement in the TB-YOLOv5 algorithm because the characteristic features are enough. Consequently, the “umbrella” and “electronic equipment” are both detected, and the “electronic equipment” show better detection performance under TB-YOLOv5 structure.Figure 16
Visual demonstration of the improved detection comparisons of TB-YOLOv5 compared with YOLOv5 over some X-ray dataset images of other objects under x network. Yellow dotted lines circle the missed inspection of small targets investigated by YOLOv5, while they are detected by TB-YOLOv5 algorithms. Other things are both detected by the two network structure.


## 3.4. Discussion

In our study of popular object detectors such as YOLOv5 to better detect smaller objects, we are able to identify architectural modifications that offer significant performance improvements compared with the original model. The context in which we apply the proposed technique, X-ray security detection, is an environment that can benefit significantly from such improvements. As we have seen, this change does have a quantifiable impact on detection. In this work, we have not only significantly improved detection performance but also identified specific techniques that can be applied to any other application involving the detection of small- or long-range targets.The result is that the TB-YOLOv5 family of models outperforms the YOLOv5 class of models in X-ray security screening, especially for smaller objects, which has been the focus of this study. At the same time, detection performance is improved and enhanced for medium-sized objects. Although our focus here is on modifying the popular YOLOv5 model, the methods and techniques we explore can potentially evolve into a wholly original model structure.Finally, while this study suggests significant empirical gains from the proposed architectural changes, the consistency and generalizability of the results can and should be further investigated. For example, further testing using different datasets and possible challenges such as security detection would greatly aid the analysis. While we have demonstrated the usefulness of the technique our study presented, these techniques can only be refined and better understood when applied to different environments and settings. Doing so would be an important step toward a more robust solution for small target detection. In addition, there are more directions and techniques that would fit well into this topic and were not considered in this study, but these will remain the subject of future research.


## 4. Conclusion

The security inspection based on X-ray is a very useful way in each country to guarantee safety in public places such as transportation hubs. The traditional detection methods have many defects to be improved. In this research, we investigate the performance of different architectural models applied to the YOLOv5 objection detector and propose the novel concept of TB-YOLOv5, proving that the algorithm improves the disadvantages of insufficient feature information of target detection in the trunks scanned by X-ray images. Based on the previous studies, the transformer module is added in the backbone, and the attention-BiFPN constituted CA mechanism, and BiFPN principle is applied. The average value of twelve objections’ detection accuracy trained by TB-YOLOv5 shows the highest 14.9% increase compared with YOLOv5. Furthermore, the small-object detection ability of TB-YOLOv5 acquires a more significant improvement in the article, in which the maximum is reached 23.4%. We validate the proposed technique in X-ray security inspection, underlining the specific requirements and limitations, and expect further research. The proposed TB-YOLOv5 structure system can be updated to detect better smaller targets in a situation where existing methods are unable to achieve. Through the experimentation results still have disadvantages that some objection detection precisions are relatively low and the model is unable to be applied in actual project now, and we have reasons to believe that the TB-YOLOv5 algorithm based on attention-BiFPNs has a large potential prospect in other relevant fields such as realizing factory intelligence and automatic driving.



---

*Source: 2050793-2022-08-02.xml*