A shadow detection method based on hybrid attention
By adopting a hybrid attention method in shadow detection, combining channel and spatial attention, and using a repeated cross attention module and an improved CBAM module, the problem of poor shadow detection results in the existing technology is solved and more efficient is achieved. and more accurate shadow detection.
Patent Information
- Application Number
- CN202110922894.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-08-12
- Publication Date
- 2024-10-29
- Estimated Expiration
- 2041-08-12
AI Technical Summary
Existing shadow detection algorithms fail to make full use of the attention mechanism and cross-channel dimensional information, resulting in poor shadow detection results, and it is difficult for convolutional neural networks to distinguish shadows from target pixels in gray space, reducing the accuracy and recklessness of the algorithm. Great sex.
A shadow detection method based on hybrid attention is used, combining channel attention and spatial attention. By repeating the cross attention module and the improved CBAM module, a convolutional neural network that fuses attention is built, using dense connections and feature fusion reuse. Ignore features and improve shadow feature extraction capabilities.
It improves the accuracy and generalization of shadow detection, reduces the semantic relevance, improves the efficiency and reliability of the detection system, and makes the shadow detection results more accurate.
Smart Images

Figure CN113642634B_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the technical field of shadow detection, and in particular relates to a shadow detection method based on hybrid attention. Background Art
[0002] In computer vision classification tasks such as image recognition or semantic segmentation, the algorithm needs to first extract features from the image or video. In deep learning, convolutional neural networks are usually used to perform convolution operations, process feature semantic information pixel by pixel, and find the likelihood with the label. Therefore, the semantic composition of the image and the correlation between each pixel determine the efficiency and complexity of the convolution operation.
[0003] File: Hoeim D. Single-image shadow detection and removal using paired regions [C] / / Computer Vision and Pattern Recognition. Colorado Springs: IEEE, 2011: 2033-2040 points out that shadows, as irrelevant background semantics, contain less available key information and do not play a relevant role in classification tasks. However, due to their close connection with the target foreground pixels and similar shapes, they usually affect the smooth progress of computer vision tasks. At the same time, in grayscale space, shadows have a higher degree of confusion for the target pixel semantics, making it difficult for convolutional neural networks to discover significant differences between the two, thereby reducing the accuracy and robustness of algorithm training and prediction. Therefore, in common computer vision tasks such as intelligent driving, face detection, target tracking and recognition, and posture estimation, image shadow detection is particularly important. An efficient, comprehensive, and generalizable shadow detection model will improve the work efficiency and capability ceiling of the entire task.
[0004] At present, some scholars have proposed shadow detection algorithms, but most of them do not take into account the applicability of the attention mechanism in such tasks and the correlation between cross-channel dimensions and global pixels, so the final detection effect often does not meet the expected results.
[0005] Vicente et al. proposed a support vector machine detection algorithm in Vicente TFY, Yu CP, Samaras D. Single image shadow detection using multiple cues in a supermodular MRF [C] / / British Machine Vision Conference. Bristol: British Machine Vision Association, 2013: 1-12, and constructed an SVM classifier to effectively classify shadows; Maryam et al. published the document Maryam G, Fatimah K, Abdullah L N. Shadow detection using color and edge information [J]. Journal of Computer Science, 2013, 9 (11): 1575-1588, and proposed an edge information detection algorithm, which uses an operator to refine the low-order edge information in the image and obtain the edge of the foreground and shadow, thereby achieving the classification effect; Hosseinzadeh et al. proposed in the article Hosseinzadeh S, Shakeri M, Zhang H. Fast shadow detection from a single image using a patched convolutional neural network [C] / / 2018 IEEE / RSJ International Conference on Intelligent Robots and Systems (IROS). IEEE, 2018: 3124-3129 proposed a neural network detection algorithm, which used a convolutional neural network to perform convolution processing on the image to obtain high-order features, greatly improving the accuracy; Nguyen et al. proposed Nguyen V, Vicente Y, Tomas F, et al. Shadow detection with conditional generative adversarial networks [C] / / Proceedings of the IEEE International Conference on Computer Vision. 2017: 4510-4518, using a generative adversarial network detection algorithm, using a generator and a discriminator to process the image, which improved the accuracy but greatly increased the complexity of the model.
[0006] File: Vaswani A, Shazeer N, Parmar N, et al. Attention is all you need [C] / / Advances in neural information processing systems. 2017: 5998-6008 Using the attention mechanism, by assigning weights to pixel-level semantics, the most significant features of the target foreground are extracted, and the algorithm is guided to focus on processing these primary target features, reducing the correlation between the foreground and the background, so as to improve the iteration speed and accuracy of the model. Therefore, the attention mechanism is suitable for shadow detection tasks, and its unique characteristics have a high recognition sensitivity for the discovery of shadows in images. Summary of the invention
[0007] The technical problem to be solved by the present invention is to overcome the shortcomings of the prior art and provide a shadow detection method based on hybrid attention. By studying the shadow model and the hybrid attention model, combined with the design ideas of the hybrid attention mechanism, a network integrating channel attention and spatial attention is built, and dense connections and feature fusion are used to reuse ignored features. The ability to extract shadow features is increased, the generalization and efficiency of the detection system are improved, and the shadow detection results are more accurate.
[0008] The present invention provides a shadow detection method based on hybrid attention, comprising the following steps:
[0009] Step S1. Obtain a public shadow dataset to be processed for training the system;
[0010] Step S2. Obtaining the image to be detected;
[0011] Step S3. Preprocess the data set to obtain the corresponding training set and test set;
[0012] Step S4. Preprocessing the image to be tested;
[0013] Step S5. Build and fuse the modules of the core neural network to construct a convolutional neural network based on a hybrid attention mechanism;
[0014] Step S6. Calculate the loss of the convolutional neural network prediction and the label, and adjust the parameters of the convolutional neural network according to the loss;
[0015] Step S7. Inputting the preprocessed image to be tested into the fully trained convolutional neural network;
[0016] Step S8: Output the shadow detection result and classify the shadow attributes of each pixel.
[0017] As a further technical solution of the present invention, the construction of each module of the core neural network in step S5 includes the construction of a pre-trained convolutional neural network ResNeSt50, the construction of a repeated cross attention module, the improvement and construction of an attention module CBAM, and the construction of an upsampling and feature fusion output module;
[0018] The pre-trained convolutional neural network ResNeSt50 includes 3 3×3 convolutional layers with 64 channels and 2 steps; 1 3×3 maximum pooling layer with 2 steps; 3 repeated first residual convolution blocks, each repeated first residual convolution block includes 1 1×1 convolutional layer with 64 channels, 1 3×3 convolutional layer with 128 channels, 1 1×1 convolutional layer with 32 channels, 1 1×1 convolutional layer with 128 channels, and 1 1×1 convolutional layer with 256 channels; 4 repeated second residual convolution blocks, each repeated second residual convolution block includes 1 1×1 convolutional layer with 128 channels, 1 3×3 convolutional layer with 256 channels, 1 1×1 convolutional layer with 64 channels, 1 1×1 convolutional layer with 256 channels, and 1 1×1 convolution layer with 512 channels; 6 repeated third residual convolution blocks, each repeated third residual convolution block includes 1 1×1 convolution layer with 256 channels, 1 3×3 convolution layer with 512 channels, 1 1×1 convolution layer with 128 channels, 1 1×1 convolution layer with 512 channels, and 1 1×1 convolution layer with 1024 channels; 3 repeated fourth residual convolution blocks, each repeated fourth residual convolution block includes 1 1×1 convolution layer with 512 channels, 1 3×3 convolution layer with 1024 channels, 1 1×1 convolution layer with 256 channels, 1 1×1 convolution layer with 1024 channels, 1 1×1 convolution layer with 2048 channels, and convolution layers for downsampling between residual convolution blocks;
[0019] The repeated cross attention module is composed of two CCA modules in series, and the input and output feature map sizes are consistent. The CCA module inputs the feature maps of shape (n, c, x, y) into three 1×1 convolutional layers respectively, where n is the batch size, c is the number of feature map channels, x and y are the feature map sizes, and obtains feature maps Q, K, and V with a channel number of c / 2; Q and K are associated, and then the weighted feature map is obtained through the softmax function, and then the weighted result is aggregated with V and added to the original feature map to obtain the final feature map;
[0020] The formula for the association operation is Among them, Q u ,Ω u are vectors with position u on feature maps Q and K respectively, Ω u The transpose of the ith element ofi,u Q u and Ω i,u degree of relevance;
[0021] The formula for the aggregation operation is Among them, φ i,u , A i,u are the scalar values of the feature vectors in the same row or column at the i-th channel and position u in the feature map V and A respectively; H u is the feature vector of the original feature map H at position u, H′ u is the feature vector of the last generated attention feature map at position u;
[0022] The formula for the softmax function is Among them, z i is the output value of the ith node, C is the number of output nodes, that is, the number of categories. The Softmax function is used to convert the output values of multiple categories into a probability distribution ranging from [0, 1] and 1.
[0023] The attention module CBAM includes the spatial domain attention generation module SAM, the spatial domain attention generation module CAM and the attention fusion module;
[0024] The generation formula of the spatial domain generation module is: Among them, Ms(F) is the spatial domain attention matrix, Generates the matrix for spatial domain average pooling, Generates the matrix for the maximum pooling in the spatial domain. AvgPool, MaxPool, and Sigmoid are the average pooling operation, the maximum pooling operation, and the Sigmoid transformation, respectively. 7×7 It is a convolution operation with a convolution kernel size of 7×7;
[0025] The generation formula of the channel domain generation module is: Among them, M c (F) is the channel domain attention matrix, Generates the matrix for channel-domain average pooling, Generates the matrix for maximum pooling in the channel domain. AvgPool, MaxPool and Sigmoid are average pooling operation, maximum pooling operation and Sigmoid transformation respectively. MLP is a multi-layer perceptron.
[0026] The fusion formula of the attention fusion module is F Attention =F×M s (F)×M c (F×M s (F)), where F Attention To integrate the new feature map of attention, M s(F) is the spatial domain attention matrix, M c (F) is the channel domain attention matrix;
[0027] The improvements of the attention module CBAM include replacing the channel domain generation module, improving the spatial domain generation module and improving the attention fusion module.
[0028] Replacing the channel domain generation module is to replace the original channel domain module with 4 different numbers of repeated residual convolution blocks in ResNeSt50;
[0029] The improved spatial domain generation module is to add a feature map with a channel number of 1 obtained by repeated cross attention modules to the CBAM spatial domain generation module, connect it with the original average pooling and maximum pooling results, and then input three 3×3 dimensionality reduction convolution layers to output the spatial domain attention matrix;
[0030] The formula of the improved spatial domain generation module is: Among them, M s (F) is the spatial domain attention matrix, Generates the matrix for spatial domain average pooling, Generates the matrix for spatial domain maximum pooling, Generates the matrix for spatial domain convolution. AvgPool, MaxPool and Sigmoid are average pooling operation, maximum pooling operation and Sigmoid transformation respectively. 3*3×3 is a convolution operation with a kernel size of 3 × 3, CCA (CCA) is a CCA module operation twice, and RCCA represents a repeated cross attention module;
[0031] The improved spatial domain generation module is to add a dimensionality reduction convolution module at the beginning of the original attention fusion module. The dimensionality reduction convolution module is composed of a 3×3 convolution layer and a batch normalization layer.
[0032] The formula for improving the attention fusion module is Among them, F Attention To integrate the new feature map of attention, M s (F) is the spatial domain attention matrix, M c (F) is the channel domain attention matrix, f 3*3×3 It is a convolution operation with 3 convolution kernels.
[0033] The feature fusion operation includes upsampling the multi-channel feature map generated by the improved spatial domain: from the bottom layer upwards, the image size is upsampled to 80×80, the number of channels is 256, 128, 64, and 64, respectively, and then spliced according to the channel dimension.
[0034] Furthermore, in step S6, the formula of the FocalLoss loss function is: Among them, α is 0.25, γ is 2, L fl is the loss value, y is the label, and y′ is the predicted value.
[0035] The parameters of the convolutional neural network were adjusted according to the loss. The network optimization method was stochastic gradient descent, the initial learning rate was 0.01, the learning rate decay cycle was 100 steps, and the number of training iterations was 2000 steps.
[0036] Furthermore, in step S1, a public shadow data set to be processed for training the system is obtained, and each data in the public shadow data set to be processed includes a color original image and a black-and-white annotated image.
[0037] Furthermore, the public shadow datasets to be processed include two public shadow datasets, SBU and UCF, wherein the SBU dataset contains 4089 training data and 638 test data, and the UCF dataset contains 245 test data.
[0038] Furthermore, the specific steps for preprocessing the public shadow dataset used for training the system in S3 are as follows:
[0039] Step S31. Randomly crop the training data and test data of the data set;
[0040] Step S32: perform image augmentation on the training data of the dataset. Image augmentation is horizontal or vertical mirror flipping of the image. Synchronous operation is performed on the original color image and the black and white annotated image of each data in the dataset.
[0041] Furthermore, in step S4, the specific method of preprocessing the image to be detected is to sample the image, the sampling threshold is 320×320, and the sampling method is bilinear interpolation;
[0042] If the image length and width are both greater than 320 pixels, downsample the image to 320×320;
[0043] If the image length and width are both less than 320 pixels, upsample the image to 320×320;
[0044] If the image size is between the image length and width, resample the image to 320×320;
[0045] If the image length and width are both 320 pixels, no sampling operation is performed on the image.
[0046] The advantage of the present invention is that the pre-trained ResNeSt50 network is used, and the CBAM design concept is combined to improve the spatial domain feature extraction to form a mixed domain attention mechanism. On the basis of making full use of the mixed attention mechanism, the ignored features are retained and reused with reference to the dense connection concept. The mixed attention mechanism improves the system's ability to recognize shadows, improves the system's extraction of cross-channel dimensional information, expands the system's receptive field from local to global, and achieves high efficiency and reliability of shadow detection. This method increases the ability to extract shadow features, reduces the correlation between semantics, improves the generalization and efficiency of the detection system, and makes the shadow detection results more accurate. BRIEF DESCRIPTION OF THE DRAWINGS
[0047] Figure 1 This is the basic module structure diagram of Resnet50 of the present invention;
[0048] Figure 2 It is the structural diagram of the CCA module of the present invention;
[0049] Figure 3 It is a schematic diagram of the method flow of the present invention;
[0050] Figure 4 It is a structural diagram of the shadow detection network of the present invention. DETAILED DESCRIPTION
[0051] See also Figure 1-Figure 4 ,This embodiment provides a shadow detection method based on hybrid attention, comprising the following steps,
[0052] Step S1, obtaining a public shadow dataset to be processed for training the system. The dataset includes two public shadow datasets, SBU and UCF, where the SBU dataset contains 4089 training data and 638 test data, and the UCF dataset contains 245 test data. Each data contains two images: a color original image and a black and white annotated image.
[0053] Step S2, obtaining the image to be detected from the camera or the local hard disk.
[0054] Step S3, preprocessing the shadow data set used to train the system and obtaining the corresponding training set and test set.
[0055] The specific process includes the following steps:
[0056] Step S31, randomly cropping the training data and test data of the data set obtained in step 1, with the cropping size being 320×320.
[0057] Step S32, performing image augmentation on the training data set obtained in step 32, wherein the augmentation mode is horizontal or vertical mirror flipping of the image, and the augmentation scale is 2 times.
[0058] In the above preprocessing stage, all cropping and augmentation operations on the data set are synchronous operations of the original color image and the black and white labeled image of each data in the data set.
[0059] Step S4, preprocessing the image to be detected.
[0060] The preprocessing process is specifically a sampling process for the image. The sampling threshold is 320×320, and the sampling method is bilinear interpolation.
[0061] If the image length and width are both greater than 320 pixels, downsample the image to 320×320.
[0062] If the image length and width are both less than 320 pixels, upsample the image to 320×320.
[0063] If the image size is between 320 and 320, resample the image to 320×320.
[0064] If the image length and width are both 320 pixels, no sampling operation is performed on the image.
[0065] Step S5: Construction and integration of each module of the core neural network to form a convolutional neural network based on the attention mechanism.
[0066] The construction of each core neural network module, including the construction of the pre-trained convolutional neural network ResNeSt50, the construction of the repeated cross attention module, the improvement and construction of the attention module CBAM, and the construction of the upsampling and feature fusion output module:
[0067] Optimization and adjustment of pre-trained convolutional neural network Resnet50:
[0068] For the original ResNeSt50 network, the last fully connected layer and global pooling layer were removed, and the existing parameters of the Resnet50 network trained on the Imagenet dataset were used. The optimized network includes: 3 3×3 convolutional layers with 64 channels and 2 steps; 1 3×3 maximum pooling layer with 2 steps; 3 repeated first residual convolution blocks, each of which includes 1 1×1 convolutional layer with 64 channels, 1 3×3 convolutional layer with 128 channels, 1 1×1 convolutional layer with 32 channels, 1 1×1 convolutional layer with 128 channels, and 1 1×1 convolutional layer with 256 channels; 4 repeated second residual convolution blocks, each of which includes 1 1×1 convolutional layer with 128 channels, 1 3×3 convolutional layer with 256 channels, and 1 1×1 convolutional layer with 256 channels. 1×1 convolution layer with 64 channels, 1×1 convolution layer with 256 channels, and 1×1 convolution layer with 512 channels; 6 repeated third residual convolution blocks, each of which includes 1×1 convolution layer with 256 channels, 1×3 convolution layer with 512 channels, 1×1 convolution layer with 128 channels, 1×1 convolution layer with 512 channels, and 1×1 convolution layer with 512 channels. 1×1 convolution layer with a channel number of 1024; 3 repeated fourth residual convolution blocks, each of which includes 1 1×1 convolution layer with a channel number of 512, 1 3×3 convolution layer with a channel number of 1024, 1 1×1 convolution layer with a channel number of 256, 1 1×1 convolution layer with a channel number of 1024, 1 1×1 convolution layer with a channel number of 2048, and convolution layers for downsampling between the residual convolution blocks;
[0069] Repeat the construction and definition of the cross attention module:
[0070] The repeated cross attention module is composed of two CCA modules in series, and the input and output feature maps have the same size. The CCA module inputs the feature maps of shape (n, c, x, y) into three 1×1 convolutional layers, where n is the batch size, c is the number of feature map channels, and x and y represent the feature map size, and obtains feature maps Q, K, and V with a channel number of c / 2. Q and K are associated, and then the softmax function is used to obtain the weighted feature map. The result is then aggregated with V and added to the original feature map to obtain the final feature map;
[0071] The calculation formula for the associated operation is:
[0072]
[0073] Among them, Q u ,Ω u Respectively represent the vector at position u on the feature graphs Q and K, Represents Ω uThe transpose of the ith element of d i,u It means Q u and Ω i,u degree of relevance;
[0074] Aggregation operation calculation formula:
[0075]
[0076] Among them, φ i,u , A i,u H represents the scalar value of the feature vector in the same row or column at the i-th channel and position u in the feature map V and A respectively. u Represents the feature vector of the original feature map H at position u, H′ u Represents the feature vector of the last generated attention feature map at position u;
[0077] Softmax function calculation formula:
[0078]
[0079] Among them, z i is the output value of the ith node, and C is the number of output nodes, that is, the number of categories. The Softmax function can be used to convert the output values of multiple categories into a probability distribution in the range of [0, 1] and 1;
[0080] The attention module CBAM includes a spatial domain attention generation module SAM, a spatial domain attention generation module CAM and an attention fusion module.
[0081] The calculation process of the spatial domain generation module is as follows:
[0082]
[0083]
[0084]
[0085] Among them, M s (F) is the spatial domain attention matrix, Generates the matrix for spatial domain flat pooling, Generates the matrix for the maximum pooling in the spatial domain. AvgPool, MaxPool, and Sigmoid represent the average pooling operation, the maximum pooling operation, and the Sigmoid transformation, respectively. 7×7 It is a convolution operation with a convolution kernel size of 7×7;
[0086] The calculation process of the channel domain generation module is as follows:
[0087]
[0088]
[0089]
[0090] Among them, M c (F) is the channel domain attention matrix, Generates the matrix for channel-domain average pooling, Generates the matrix for channel domain maximum pooling, AvgPool, MaxPool and Sigmoid represent average pooling operation, maximum pooling operation and Sigmoid transformation respectively, and MLP is a multi-layer perceptron;
[0091] The calculation process of the attention fusion module:
[0092] F Attention =F×M s (F)×M c (F×M s (F)
[0093] Among them, F Attention To integrate the new feature map of attention, M s (F) is the spatial domain attention matrix, M c (F) is the channel domain attention matrix;
[0094] The improvements of the attention module CBAM include replacing the channel domain generation module, improving the spatial domain generation module and improving the attention fusion module;
[0095] Replace the channel domain generation module, specifically, replace the original channel domain module with 4 different numbers of repeated residual convolution blocks in ResNeSt50;
[0096] Improve the spatial domain generation module. Specifically, add a feature map with a channel number of 1 obtained by repeated cross attention modules to the CBAM spatial domain generation module, connect it with the original average pooling and maximum pooling results, and then input three 3×3 dimensionality reduction convolution layers to output the spatial domain attention matrix:
[0097] The calculation process of the improved spatial domain generation module is as follows:
[0098]
[0099]
[0100]
[0101]
[0102] Among them, M s(F) is the spatial domain attention matrix, Generates the matrix for spatial domain flat pooling, Generates the matrix for spatial domain maximum pooling, Generates the matrix for spatial domain convolution. AvgPool, MaxPool, and Sigmoid represent average pooling operation, maximum pooling operation, and Sigmoid transformation, respectively. 3*3×3 is a convolution operation with a kernel size of 3 × 3, CCA (CCA) means two CCA module operations, and RCCA means repeated cross attention module;
[0103] Improve the attention fusion module by adding a dimensionality reduction convolution module at the beginning of the original attention fusion module. The dimensionality reduction convolution module is composed of a 3×3 convolution layer and a batch normalization layer.
[0104] The calculation process of the improved attention fusion module is as follows:
[0105] F′=f 3*3×3 (F)
[0106] F Attention =F′×M s (F′)×M c (F′×M s (F)
[0107] Among them, F Attention To integrate the new feature map of attention, M s (F) is the spatial domain attention matrix, M c (F) is the channel domain attention matrix, f 3*3×3 It is a convolution operation with 3 convolution kernels.
[0108] The feature fusion operation includes upsampling the multi-channel feature maps generated by the improved spatial domain: from the bottom layer upwards, the image size is upsampled to 80×80, the number of channels is 256, 128, 64, 64, and then spliced according to the channel dimension;
[0109] The upsampling and output module includes a 3×3 convolution layer with 64 channels, an upsampling bilinear interpolation transposed convolution layer, and a Sigmoid activation function. This module is built at the end of the model to output the model's prediction results.
[0110] Since the model adopts a parallel structure in the channel dimension, it extracts more cross-channel features and adds global context spatial information extraction to the original spatial domain module. Compared with the original ResNeSt50 network, in addition to improving the spatial domain module and multi-scale feature fusion operations, the model depth is not increased. The weighted attention feature maps in each part of the model are densely connected. Compared with the addition operation, the input feature width is expanded to fully reuse the features. While avoiding model degradation, the attention of each dimension is fully extracted, improving the model accuracy and work efficiency.
[0111] The system was built in the ubuntu16.04, Python3.6, and mxnet-cu100 environment, trained and tested on a Tesla P100 graphics card. It takes an average of 0.034 seconds to process an image, and the expected detection speed reaches 29.4 frames per second, which has a certain degree of efficient real-time detection capability.
[0112] Step S6, calculate the loss of network prediction and label, and adjust the network parameters according to the loss.
[0113] During training, the FocalLoss loss function is used to calculate the loss between the prediction and the label.
[0114] FocalLoss calculation formula:
[0115]
[0116] Among them, α is 0.25, γ is 2, L fl is the loss value, y is the label, and y′ is the predicted value.
[0117] The optimization method of the system network uses stochastic gradient descent, with an initial learning rate of 0.01, a learning rate decay cycle of 100 steps, and a training iteration number of 2000 steps.
[0118] Step S7, the deep convolutional neural network is fully trained and the pre-processed shadow image to be detected is inputted into the neural network.
[0119] Step S8: output the shadow detection result and classify the shadow attributes of each pixel.
[0120] This embodiment takes into account the interference of the shadow model on the image foreground in computer vision tasks, analyzes the ease of use of the attention mechanism and the weighted summation working mechanism, and combines the cBAM design concept to improve the spatial domain feature extraction to form a mixed domain attention mechanism. The model proposed in the present invention makes full use of the hybrid attention mechanism and refers to the dense connection idea to retain and reuse ignored features. In addition, in view of the imbalance between deep feature maps and shallow semantics and position information, multi-scale feature fusion is used to further improve the model detection effect. Therefore, it has efficient shadow recognition and shadow semantic extraction capabilities. After verification, the system has excellent performance in predicting shadows, has efficient shadow detection capabilities, and has proved the generalization ability of the model through cross-model verification.
[0121] The above shows and describes the basic principles, main features and advantages of the present invention. Those skilled in the art should understand that the present invention is not limited to the above specific embodiments. The above specific embodiments and the description in the specification are only for further illustrating the principles of the present invention. Without departing from the spirit and scope of the present invention, the present invention may have various changes and improvements, which fall within the scope of the present invention to be protected. The scope of the present invention to be protected is defined by the claims and their equivalents.
Claims
1. A shadow detection method based on hybrid attention, characterized in that: The following steps are included: Step S1. Obtain a public shadow dataset to be processed for training the system; Step S2. Obtaining the image to be detected; Step S3. Preprocess the data set to obtain the corresponding training set and test set; Step S4. Preprocessing the image to be tested; Step S5. Build and fuse the modules of the core neural network to construct a convolutional neural network based on a hybrid attention mechanism; Step S6. Calculate the loss of the convolutional neural network prediction and the label, and adjust the parameters of the convolutional neural network according to the loss; Step S7. Inputting the preprocessed image to be tested into the fully trained convolutional neural network; Step S8. Output the shadow detection result and classify the shadow attributes of each pixel; The construction of each module of the core neural network in step S5 includes the construction of the pre-trained convolutional neural network ResNeSt50, the construction of the repeated cross attention module, the improvement and construction of the attention module CBAM, and the construction of the upsampling and feature fusion output module; The pre-trained convolutional neural network ResNeSt50 includes three 3×3 convolutional layers with 64 channels and 2 steps; one 3×3 maximum pooling layer with 2 steps; three repeated first residual convolutional blocks, each of which includes one 1×1 convolutional layer with 64 channels, one 3×3 convolutional layer with 128 channels, one 1×1 convolutional layer with 32 channels, one 1×1 convolutional layer with 128 channels, and one 1×1 convolutional layer with 256 channels; four repeated second residual convolutional blocks, each of which includes one 1×1 convolutional layer with 128 channels, one 3×3 convolutional layer with 256 channels, one 1×1 convolutional layer with 64 channels, one 1×1 convolutional layer with 256 channels, 1 1×1 convolution layer with 512 channels; 6 repeated third residual convolution blocks, each repeated third residual convolution block includes 1 1×1 convolution layer with 256 channels, 1 3×3 convolution layer with 512 channels, 1 1×1 convolution layer with 128 channels, 1 1×1 convolution layer with 512 channels, and 1 1×1 convolution layer with 1024 channels; 3 repeated fourth residual convolution blocks, each repeated fourth residual convolution block includes 1 1×1 convolution layer with 512 channels, 1 3×3 convolution layer with 1024 channels, 1 1×1 convolution layer with 256 channels, 1 1×1 convolution layer with 1024 channels, 1 1×1 convolution layer with 2048 channels, and convolution layers for downsampling between residual convolution blocks; The repeated cross attention module is composed of two CCA modules in series, and the input and output feature map sizes are consistent. The CCA module inputs the feature map of shape (n, c, x, y) into three 1×1 convolutional layers respectively, where n is the batch size, c is the number of feature map channels, x and y are the feature map sizes, and obtains feature maps Q, K, and V with a channel number of c / 2; Q and K are associated, and then a weighted feature map is obtained through a softmax function, and then the weighted result is aggregated with V and added to the original feature map to obtain the final feature map; the formula for the association operation is Among them, Q u ,Ω u are vectors with position u on feature maps Q and K respectively, Ω u The transpose of the ith element of i,u Q u and Ω i,u degree of relevance; The formula for the aggregation operation is Among them, φ i,u , A i,u are the scalar values of the feature vectors in the same row or column at the i-th channel and position u in the feature map V and A respectively; H u is the feature vector of the original feature map H at position u, H u ' is the feature vector of the last generated attention feature map at position u; The formula of the softmax function is Among them, Z i is the output value of the ith node, C is the number of output nodes, that is, the number of categories. The Softmax function is used to convert the output values of multiple categories into a probability distribution ranging from [0, 1] and 1. The attention module CBAM includes a spatial domain attention generation module SAM, a spatial domain attention generation module CAM and an attention fusion module; The generation formula of the spatial domain attention generation module is Among them, M s (F) is the spatial domain attention matrix, Generates the matrix for spatial domain average pooling, Generates the matrix for the maximum pooling in the spatial domain. AvgPool, MaxPool, and Sigmoid are the average pooling operation, the maximum pooling operation, and the Sigmoid transformation, respectively. 7×7 It is a convolution operation with a convolution kernel size of 7×7; The generation formula of the channel domain generation module is: Among them, M c (F) is the channel domain attention matrix, Generates the matrix for channel-domain average pooling, Generates the matrix for maximum pooling in the channel domain. AvgPool, MaxPool and Sigmoid are average pooling operation, maximum pooling operation and Sigmoid transformation respectively. MLP is a multi-layer perceptron. The fusion formula of the attention fusion module is F Attention =F×M s (F)×M c (F×M s (F)), where F Attention To integrate the new feature map of attention, M s (F) is the spatial domain attention matrix, M c (F) is the channel domain attention matrix; The improvement of the attention module CBAM includes replacing the channel domain generation module, improving the spatial domain attention generation module and improving the attention fusion module. The replacement channel domain generation module is to replace the original channel domain module with 4 different numbers of repeated residual convolution blocks in ResNeSt50; The improved spatial domain attention generation module adds a feature map with a channel number of 1 obtained by repeated cross attention modules to the CBAM spatial domain attention generation module, connects it with the original average pooling and maximum pooling results, and then inputs three 3×3 dimensionality reduction convolution layers to output the spatial domain attention matrix; The formula of the improved spatial domain attention generation module is: Among them, M s (F) is the spatial domain attention matrix, Generates the matrix for spatial domain average pooling, Generates the matrix for spatial domain maximum pooling, Generates the matrix for spatial domain convolution. AvgPool, MaxPool and Sigmoid are average pooling operation, maximum pooling operation and Sigmoid transformation respectively. 3*3×3 is a convolution operation with a kernel size of 3 × 3, CCA (CCA) is a CCA module operation twice, and RCCA represents a repeated cross attention module; The improved spatial domain attention generation module is to add a dimensionality reduction convolution module at the beginning of the original attention fusion module, and the dimensionality reduction convolution module is composed of a 3×3 convolution layer and a batch normalization layer; F′=f 3*3×3 (F) The formula of the improved attention fusion module is F Attention =F′×M s (F′)×M c (F′×M s (F′)), where F Attention To integrate the new feature map of attention, M s (F) is the spatial domain attention matrix, M c (F) is the channel domain attention matrix, f 3*3×3 It is a convolution operation with 3 convolution kernels. The feature fusion operation of the feature fusion output module includes upsampling the multi-channel feature map generated by the improved spatial domain: from the bottom layer upward, the image size is all upsampled to 80×80, the number of channels is 256, 128, 64, and 64 respectively, and then spliced according to the channel dimension in turn.
2. The shadow detection method based on hybrid attention according to claim 1, characterized in that: In step S6, the formula of the FocalLoss loss function is: Among them, α is 0.25, γ is 2, L fl is the loss value, y is the label, y′ is the predicted value. The parameters of the convolutional neural network are adjusted according to the loss. The network optimization method is stochastic gradient descent, the initial learning rate is 0.01, the learning rate decay cycle is 100 steps, and the number of training iterations is 2000 steps.
3. The shadow detection method based on hybrid attention according to claim 1, characterized in that: In the step S1, a public shadow data set to be processed for training the system is obtained, and each data in the public shadow data set to be processed includes a color original image and a black and white annotated image.
4. A shadow detection method based on hybrid attention according to claim 1 or 3, characterized in that: The public shadow dataset to be processed includes two public shadow datasets, SBU and UCF, wherein the SBU dataset includes 4089 training data and 638 test data, and the UCF dataset includes 245 test data.
5. The shadow detection method based on hybrid attention according to claim 1, characterized in that: The specific steps of preprocessing the public shadow dataset for training the system in S3 are as follows: Step S31. Randomly crop the training data and test data of the data set; Step S32: perform image augmentation on the training data of the dataset. Image augmentation is horizontal or vertical mirror flipping of the image. Synchronous operation is performed on the original color image and the black and white annotated image of each data in the dataset.
6. The shadow detection method based on hybrid attention according to claim 1, characterized in that: The specific method of preprocessing the image to be detected in step S4 is to sample the image, the sampling threshold is 320×320, and the sampling method is bilinear interpolation; If the image length and width are both greater than 320 pixels, downsample the image to 320×320; If the image length and width are both less than 320 pixels, upsample the image to 320×320; If 320 is between the image length and width, sample the image to 320×320; If the image length and width are both 320 pixels, no sampling operation is performed on the image.
Citation Information
Patent Citations
Driver behavior identification method based on multi-scale attention convolutional neural network
CN110059582A
Shadow detection method based on attention mechanism
CN111639692A
