Living body detection device and method based on local convolution and semantic information
By combining the deep neural network structure of local convolution and transformer in living body detection, the problems of large number of parameters and poor generalization ability in the existing technology are solved, and efficient living body detection in resource-constrained environments is achieved, which can effectively resist multiple kind of attack.
Patent Information
- Application Number
- CN202410923964.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-07-11
- Publication Date
- 2024-09-06
- Estimated Expiration
- 2044-07-11
AI Technical Summary
Existing living body detection technology has problems such as large number of parameters, high consumption of computing resources, and inability to effectively utilize global context information and two-dimensional neighbor relationships, resulting in poor model generalization capabilities.
It uses a deep neural network structure based on local convolution and transformer, combined with a multi-scale attention module and a semantic information alignment module to reduce the number of model parameters and the number of memory accesses. It uses the transformer branch to assist in training the CNN branch and enhances the model's long-distance dependence on images. Relationship modeling capabilities.
It significantly reduces the amount of model parameters and the number of memory accesses, improves the generalization performance and anti-interference ability of the model, can run in resource-constrained devices, and effectively resists attacks such as high-precision 3D head models and resin masks.
Smart Images

Figure CN118470808B
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of computer vision, and in particular to a living body detection device and method based on local convolution and transformer semantic information. Background Art
[0002] Liveness detection is a method used to verify identity, which aims to determine the true physiological characteristics of the subject. Liveness detection plays an important role in face recognition and can effectively resist various common attack methods, such as fraud using photos, videos, face-changing technology, masks, occlusion, 3D animation, and screen reshoots. By performing liveness detection on users, their authenticity can be ensured, thereby protecting the interests and safety of users.
[0003] With the development of deep learning, the performance of liveness detection methods based on deep neural networks has been greatly improved. This type of method uses deep learning networks to learn highly discriminative features from large-scale face datasets. The deep learning networks used include: residual learning networks, center difference convolutional networks, LSTM, adversarial generative networks, and transformers. Although liveness detection technology has made significant progress, there are still some defects, such as:
[0004] Conventional convolutional neural networks (CNNs) have a large number of parameters and require a lot of computing resources. Although depthwise separable convolution or grouped convolution can reduce the number of parameters, it increases memory access overhead, resulting in slower model training speed. Although CNN-based liveness detection methods can effectively extract two-dimensional neighbor information in images, they cannot use global context information, resulting in poor ability of the model to extract semantic information, thereby reducing the generalization ability of the model. Although transformer-based liveness detection methods can effectively use global context information, they are not good at learning two-dimensional neighbor relationships in images and have high computational overhead.
[0005] In view of this, the present invention is developed by deeply considering the many deficiencies and inconveniences caused by the imperfection of the existing liveness detection technology, and actively researching and improving the trial production to develop the present invention. Summary of the invention
[0006] The purpose of the present invention is to overcome the shortcomings of the prior art and provide a living body detection device and method based on local convolution and transformer semantic information, which can significantly reduce the number of model parameters and the number of memory accesses while maintaining the discriminability and robustness of the extracted features and improving the generalization performance of the model.
[0007] In order to achieve the above object, the solution of the present invention is:
[0008] A liveness detection device based on local convolution and semantic information, comprising an image acquisition module, for acquiring a face image of a user to be detected, and inputting the face image into a trained liveness detection model after normalization operation, characterized in that: the liveness detection device uses a liveness detection model based on a deep neural network structure and performs model reasoning, and the liveness detection model includes a CNN branch, a transformer branch, a semantic information alignment module and a model loss calculation module;
[0009] The CNN branch includes a local convolution module and a multi-scale attention module. The local convolution module includes local convolution and point-by-point convolution. Local convolution is used to perform convolution processing on some channels of the image, while the remaining channels remain as they are. Point-by-point convolution is used to make full use of the information of different channels to further extract features. The multi-scale attention module is used to extract feature representations with long-range contextual information at different stages of the CNN branch. By using convolution operations of different scales to obtain attention weights, different weights are assigned to different positions in the feature map.
[0010] The transformer branch includes multiple transformer modules, which are used to assist in training the CNN branch during the training phase. The transformer module has a multi-head self-attention module with multi-scale pooling to compress the length of feature sequences and enhance feature representation capabilities.
[0011] The semantic information alignment module is used to align the feature representations extracted by the CNN branch and the transformer branch;
[0012] The model loss calculation module includes channel-by-channel distillation loss, cross entropy loss and overall loss; channel-by-channel distillation loss is used to transfer the knowledge of the transformer branch to the CNN branch. The calculation formula of channel-by-channel distillation loss is as follows
[0013]
[0014] Where c represents the feature map channel index; i represents the map feature pixel index; Hyperparameters for controlling the smoothness of probability distributions;
[0015]
[0016] in, Hyperparameters for controlling the smoothness of probability distributions; is the number of image samples; C is the number of channels of the feature map; H is the height of the feature map; W is the width of the feature map; Represents the output feature of the i-th image in the transformer branch, the c-th channel and the pixel position index is j; Represents the output feature of the i-th image in the CNN branch, the c-th channel and the pixel position index is j; represents the image index in the feature map; c represents the channel index of the feature map; Represents the pixel index of the image feature; :Indicates is a function of the independent variable; Represents the output features of the i-th image and the c-th channel; represents an exponential function with the natural exponent e as the base, which ensures non-negative output and enhances numerical stability; even if the differences between the inputs are small, these differences will be amplified after the exponential operation, which helps to better distinguish categories during training;
[0017] The CNN branch and the transformer branch calculate the cross entropy loss and cross entropy loss respectively. The calculation formula is as follows,
[0018]
[0019] in, is the number of image samples; is the image label; is the model prediction value;
[0020] Overall loss function The calculation formula is as follows,
[0021]
[0022] in, is the weight of the channel-by-channel distillation loss; is the cross entropy loss of the CNN branch; is the cross entropy loss of the transformer branch; is the weight of the cross entropy loss of the CNN branch; is the weight of the cross entropy loss of the transformer branch; is the channel-by-channel distillation loss;
[0023] When the liveness detection model is inferred, the image is normalized and then input into the liveness detection model. The output of the liveness detection model is a liveness score with a shape of [N, 2]. The category corresponding to the maximum score is taken as the model prediction category.
[0024] A liveness detection method based on local convolution and semantic information comprises the following steps: obtaining a face image of a user to be detected, normalizing the face image and inputting it into a trained liveness detection model, the liveness detection model calculating a liveness score corresponding to the face image, and determining whether the user to be detected is alive according to the liveness score;
[0025] The training process of the liveness detection model includes:
[0026] Input the image of the training sample into the living body detection model to be trained;
[0027] Step 1: Extract local features of the image in the CNN branch: Use local convolution to convolve some channels of the image, keep the other channels as they are, and use point-by-point convolution to make full use of the information of different channels to further extract features;
[0028] Step 2: Use multiple transformer modules to form a transformer branch to assist in training the CNN branch during the training phase of the liveness detection model, provide global context information for the CNN branch, and enhance the model's ability to model long-distance dependencies in the image. The processing steps of the transformer module include:
[0029] Step B1: The input features of shape [C, H, W] are serialized into a tensor of shape [(H / P)×(W / P), C], where P is the size of the patch, which can also be understood as the multiple of downsampling.
[0030] The tensors of step B2 and step B1 pass through a multi-head self-attention module with multi-scale pooling. The multi-head self-attention module uses pooling layers of different scales to obtain context information of different scales and reduce Size;
[0031] Step B3: add the output of step B1 and the output of step B2 and perform layer normalization processing;
[0032] Step B4: sending the output of step B3 to a feedforward neural network, which is composed of convolution and activation functions, performs nonlinear transformation and mapping, obtains two-dimensional neighbor information, and enhances the representation ability of the model;
[0033] Step B5: add the output of step B3 and the output of step B4 and perform layer normalization processing;
[0034] Step B6: perform feature deserialization operation on the output of step B5, so that the output feature becomes a tensor of shape [C, H / P, W / P], where P is the size of the patch, which can also be understood as the multiple of downsampling;
[0035] Step 3: Use the multi-scale attention module to extract feature representations with different distance context information at different stages of the CNN branch. By using convolution operations of different scales to obtain attention weights, different attention weights are assigned to different positions in the feature map to reduce the feature differences between the CNN branch and the transformer branch, so that the CNN branch can extract high-quality semantic information from the transformer branch.
[0036] Step 4: Use the semantic alignment module to align the feature representations extracted by the CNN branch and the transformer branch to avoid direct alignment of the CNN branch and the transformer branch.
[0037] Step 5: Calculate the loss of the liveness detection model, including channel-by-channel distillation loss, cross entropy loss, and overall loss. The channel-by-channel distillation loss is used to transfer the knowledge of the transformer branch to the CNN branch. The calculation formula of the channel-by-channel distillation loss is as follows:
[0038]
[0039] Where c represents the feature map channel index; i represents the map feature pixel index; Hyperparameters for controlling the smoothness of probability distributions;
[0040]
[0041] in, Hyperparameters for controlling the smoothness of probability distributions; is the number of image samples; C is the number of channels of the feature map; H is the height of the feature map; W is the height of the feature map; Represents the output feature of the i-th image in the transformer branch, the c-th channel and the pixel position index is j; Represents the output feature of the i-th image in the CNN branch, the c-th channel and the pixel position index is j; represents the image index in the feature map; c represents the channel index of the feature map; Represents the pixel index of the image feature; :Indicates is a function of the independent variable; Represents the output features of the i-th image and the c-th channel; represents an exponential function with the natural exponent e as the base, which ensures non-negative output and enhances numerical stability; even if the differences between the inputs are small, these differences will be amplified after the exponential operation, which helps to better distinguish categories during training;
[0042] The CNN branch and the transformer branch calculate the cross entropy loss and cross entropy loss respectively. The calculation formula is as follows,
[0043]
[0044] in, is the number of image samples; is the image label; is the model prediction value;
[0045] Overall loss function The calculation formula is as follows,
[0046] in, is the weight of the channel-by-channel distillation loss; is the cross entropy loss of the CNN branch; is the cross entropy loss of the transformer branch; is the weight of the cross entropy loss of the CNN branch; is the weight of the cross entropy loss of the transformer branch; is the channel-by-channel distillation loss;
[0047] Step 6: Infer the liveness detection model: Normalize the image and input it into the liveness detection model. The output of the liveness detection model is a liveness score in the shape of [N, 2], where N is the number of images. The category corresponding to the maximum score is taken as the model prediction category.
[0048] Furthermore, in step 1, the local convolution specifically includes the following steps:
[0049] Step A1: The shape of the input tensor is [C, H, W], where C is the channel, H is the height, and W is the width. The local convolution splits the input tensor along the channel dimension into two tensor features. , , the shapes are [K,H,W] and [CK,H,W] respectively, K is the feature The number of channels, CK is the characteristic The number of channels, and The sum of the number of channels is C, that is, K+ (CK) = C;
[0050]
[0051] in, represents the input features, Indicates that the features are segmented along the dimension dim;
[0052] Step A2: For the first tensor feature Perform convolution operation to obtain features :
[0053]
[0054] Step A3: Concatenate two tensor features along the channel dimension , ,
[0055]
[0056] in, Indicates that the features are concatenated along the dimension dim. represents the output features;
[0057] The calculation formula of the point-by-point convolution PWConv is as follows:
[0058]
[0059] in, represents the feature output of point-wise convolution, represents the output features of local convolution, ReLU represents the input feature, ReLU represents the ReLU activation function, and BN represents the batch normalization layer.
[0060] Furthermore, in step B1, the feature serialization processing steps include:
[0061] Step C1, input features pass through the convolution module;
[0062] Step C2, flatten the output of the convolution module;
[0063] Step C3, converting dimensions;
[0064] Step C4: Use layer normalization to process the features to obtain output features.
[0065] Further, in step B2, let the input feature The shape of is [B,H×W,C], B is the batch size, and the processing steps of the multi-head self-attention module with multi-scale pooling include:
[0066] Step D1: Reshape input features The shape of the input feature is changed to [B, C, H, W]; the average pooling layers of different scales P1, P2, P3, and P4 are used to process the input feature. , and obtain the following features , , , , the shapes are: [B,C,H / P1,W / P1], [B,C,H / P2,W / P2], [B,C,H / P3,W / P3], [B,C,H / P4,W / P4],
[0067]
[0068]
[0069]
[0070]
[0071] Step D2: Reshape the 4 features output by step D1 , , , , so that their shapes become [B,C,(H / P1)×(W / P1)], [B,C,(H / P2)×(W / P2)], [B,C,(H / P3)×(W / P3)], [B,C,(H / P4)×(W / P4)]. After splicing the above features, the shape becomes [B,C,S],
[0072]
[0073]
[0074]
[0075]
[0076]
[0077]
[0078] in, Represents feature reshaping, represents the concatenation function, represents the concatenated output feature; S represents the feature The length of the last dimension;
[0079] Step D3: reshape the concatenated output features output by step D2 and perform layer normalization to obtain features , the shape becomes [B,S,C],
[0080]
[0081] in, Representation layer normalization processing, Functions represent permutation operations;
[0082] Step D4: Output the features of step D3 Send it to the fully connected layer FC to get the features , the shape becomes [B,S,2C]; then, reshape the features to obtain the features , the shape becomes [2, B, N, S, C / N], where N is the number of heads in the multi-head self-attention module, which is used to control the number of attention heads in the attention mechanism.
[0083]
[0084]
[0085] in, Functions are used to change the shape of dimensions;
[0086] Step D5: Segment the features output from step D4 The features after segmentation are and , the shape is [B,N,S,C / N]:
[0087]
[0088] Step D6: Input features Feed into the fully connected layer , the shape is [B,H×W,C], reshape the features, and get the features , the shape is [B,N,H×W,C / N],
[0089]
[0090] Step D7 Reshape Features The shape of the feature is [B, N, C / N, S] ;right and Perform matrix multiplication, and the feature shape after multiplication becomes [B, N, H W,S]; along the last dimension, the features are Processing, getting features :
[0091]
[0092]
[0093] in, represents matrix multiplication, The output layer of the neural network converts the output of the network into the probability of occurrence of each category;
[0094] Step D8: The features output from step D7 With features Perform matrix multiplication to obtain features , the shape is [B,N,H W, C / N]; reshape features , get the features , the shape becomes [B,H W,C]:
[0095]
[0096]
[0097] in, represents transpose;
[0098] Step D9: Output the features of step D8 Feed into the fully connected layer , and obtain the final output features , the shape is [B,H W,C]:
[0099] .
[0100] Furthermore, in step B4, the fully connected layer in the feedforward neural network is replaced with a local convolution layer, and the local convolution layer is connected after the local convolution layer. Activation function, The activation function provides a nonlinear transformation when the input value is between -3 and 3, alleviating the gradient vanishing problem while enhancing the expressiveness of the model;
[0101] Input Features A tensor of shape [H W,C], the feedforward neural network processing steps include:
[0102] Step E1: Input features Reshape the features to get the features , the shape becomes [C,H,W]:
[0103]
[0104] Step E2: After two convolutional layers and activation layers, the feature :
[0105]
[0106] in, The calculation formula of the activation function is as follows,
[0107]
[0108]
[0109] Step E3: Get features after convolution module ,feature After reshaping and transposition, the output features are obtained , the shape is [H W, C],
[0110]
[0111] .
[0112] Furthermore, in step B6, feature deserialization is equivalent to feature reshaping, the purpose of which is to transform the shape of the input feature from [H W,C] is reshaped into [C,H,W],
[0113] .
[0114] Furthermore, the processing steps of the multi-scale attention module include:
[0115] Step F1: Use 1×1 convolution to input feature map Perform linear transformation and reduce dimension, using The activation function introduces nonlinearity, enhances the expressiveness of features, and obtains features ,
[0116]
[0117] in, The formula is as follows,
[0118]
[0119] Step F2: Use convolutions of different scales to extract context information of different receptive fields and obtain features , , , and , , Add element by element to get the feature ,
[0120]
[0121]
[0122]
[0123]
[0124] Step F3: Use 1×1 convolution to mix information from different channels and increase the dimension to obtain attention weights ,
[0125]
[0126] Step F4: Input features With attention weight Perform matrix multiplication to obtain output features , improve the ability of the liveness detection model to understand and process image features,
[0127] .
[0128] Furthermore, the semantic information alignment module further extracts features before aligning the feature representations extracted by the CNN branch and the transformer branch. The CNN branch first performs a 1×1 convolution to change the number of channels of the feature map to match the number of channels of the transformer branch features, and then performs a 3×3 convolution to further extract information. The transformer branch performs a 3×3 convolution to avoid direct alignment of the features of the transformer branch with the features of the CNN branch.
[0129] Furthermore, before the image of the training sample is input into the liveness detection model to be trained, it is necessary to process the input image using any one of the following methods or any combination: random rotation, random flipping, adding Gaussian noise, and random contrast enhancement. Subsequently, the input image is normalized to limit the pixel values of the image to between 0 and 1.
[0130] Furthermore, the model is trained for 100 epochs, the batch size is 256, the learning rate is 0.01, the optimizer is SGD, the momentum is 0.9, and the optimizer adjustment strategy is StepLR.
[0131] After adopting the above structure, the living body detection device and method based on local convolution and transformer semantic information of the present invention utilizes local convolution to significantly reduce the number of model parameters and memory access times, and enables the model to run in resource-constrained devices or scenarios while ensuring the original discriminability and anti-interference ability of the model. At the same time, the present invention utilizes the transformer branch composed of the transformer module (only used during training) to assist in training the CNN branch, providing global context information for the CNN branch, and does not increase the model calculation amount and reasoning time in the reasoning stage. Considering the huge amount of calculation of the multi-head self-attention module in the transformer, the present invention introduces multi-scale pooling into the multi-head self-attention module, so that it can obtain stronger feature representation while compressing the length of the feature sequence. In addition, considering that there is a large difference between the features extracted by CNN and transformer, if the features extracted by the transformer module are directly used to supervise the features extracted by the CNN branch, it will make the model difficult to converge. Therefore, the present invention uses a multi-scale attention module and a semantic information alignment module to further extract features, which not only avoids directly aligning the features of the CNN branch and the transformer branch, but also makes it easy for the CNN branch to extract high-quality semantic information from the transformer branch, thereby improving the generalization performance of the model.
[0132] Compared with the prior art, the present invention has the following advantages:
[0133] The present invention uses local convolution to replace standard convolution or depth-wise separable convolution in the network structure, which significantly reduces the number of parameters and memory access times of the living body detection model while maintaining the discriminability and robustness of the extracted features.
[0134] The present invention uses transformer branches to assist in training CNN branches, so that the liveness detection model can simultaneously learn two-dimensional neighbor information and global context information, thereby enhancing the discrimination and generalization performance of the liveness detection model.
[0135] The present invention introduces multi-scale pooling into the multi-head self-attention module, so that it can obtain stronger feature representation while compressing the length of feature sequence.
[0136] The present invention adds a multi-scale attention module after the output of different stages of the CNN branch, so that the output obtains long-distance context information similar to the transformer, which makes it easy to extract high-quality semantic information from the transformer branch.
[0137] The present invention takes into account the differences in features learned by the two branches and uses a semantic information alignment module before aligning the outputs of the CNN branch and the transformer branch, thereby avoiding directly aligning the features of the CNN branch and the transformer branch.
[0138] The liveness detection device and method based on local convolution and transformer semantic information of the present invention can not only ensure a pass rate of more than 99% of real people, but also defend against more than 99% of various types of paper, high-precision 3D head models, resin masks, silicone masks and other attack props. BRIEF DESCRIPTION OF THE DRAWINGS
[0139] Figure 1 It is a schematic diagram of the overall framework of the present invention.
[0140] Figure 2 This is a flow chart of the transformer module with multi-scale pooling of the present invention.
[0141] Figure 3 This is a structural diagram of the characteristic serialization module of the present invention.
[0142] Figure 4 This is a flow chart of the multi-head self-attention module with multi-scale pooling of the present invention.
[0143] Figure 5 Flowchart of the feedforward neural network of the present invention.
[0144] Figure 6 This is the flow chart of the multi-scale attention module of the present invention.
[0145] Figure 7 This is a flow chart of the semantic information alignment module of the present invention. DETAILED DESCRIPTION
[0146] In order to further explain the technical solution of the present invention, the present invention is described in detail below through specific embodiments.
[0147] like Figure 1 As shown, the present invention discloses a living body detection device and method based on local convolution and transformer semantic information, including a local convolution module, a transformer module, a multi-scale attention module and a semantic information alignment module.
[0148] The local convolution module is used to extract local features of the image and significantly reduce the number of model parameters and memory access times. By using local convolution instead of traditional standard convolution or depthwise separable convolution, the computational and storage overhead of the network can be reduced while maintaining feature discriminability and anti-interference capabilities, enabling the model to run in resource-constrained devices or scenarios.
[0149] The local convolution module consists of local convolution and point-by-point convolution. Local convolution only performs convolution on some channels, while the rest of the channels remain the same, which significantly reduces the number of model parameters and memory access times. Point-by-point convolution makes full use of information from different channels to further extract features, making the extracted features highly discriminative and robust.
[0150] The local convolution specifically includes the following steps:
[0151] Step A1: The shape of the input tensor is [C, H, W] (ignoring the batch size dimension), where C is the channel, H is the height, and W is the width. The local convolution splits the input tensor along the channel dimension into two tensor features: , , the shapes are [K,H,W] and [CK,H,W] respectively, K is the feature The number of channels, CK is the characteristic The number of channels, and The sum of the number of channels is C, that is, K+ (CK) = C;
[0152]
[0153] in, represents the input features, Indicates that the features are segmented along the dimension dim.
[0154] Step A2: For the first tensor feature Perform convolution operation,
[0155]
[0156] Step A3: Concatenate two tensor features along the channel dimension , ,
[0157]
[0158] in, Indicates that the features are concatenated along the dimension dim. Represents the output features.
[0159] At this point, the local convolution operation is completed.
[0160] The calculation formula of the point-by-point convolution PWConv is as follows:
[0161]
[0162] in, represents the feature output of point-wise convolution, represents the input features, Represents the output features of local convolution, ReLU represents the ReLU activation function, and BN represents the batch normalization layer.
[0163] Point-by-point convolution can flexibly adjust the number of feature channels to adapt to different network structures and task requirements. This dimensional adjustment method can help the network learn richer and more expressive feature representations, thereby improving model performance.
[0164] The transformer module has a multi-head self-attention module with multi-scale pooling. Multiple transformer modules constitute the transformer branch, which is used to assist in training the CNN branch during the training phase. It can provide global context information for the CNN branch and enhance the model's ability to model long-distance dependencies in the image. In addition, by introducing multi-scale pooling into the multi-head self-attention module of the transformer module, the length of the feature sequence can be effectively compressed, reducing the computing resources required for training the model, while obtaining highly discriminative feature representations.
[0165] Although the standard transformer module can perform long-distance dependency modeling and global information interaction, this also leads to high computational complexity. For longer input sequences, the computational and storage overhead will increase further. Therefore, the present invention introduces multi-scale pooling into the transformer module to form a transformer module with multi-scale pooling, which can effectively compress the length of the feature sequence, reduce the computing resources required for the training model, and obtain highly discriminative feature representations.
[0166] like Figure 2 As shown in Figure 1, the processing steps of the transformer module with multi-scale pooling include:
[0167] Step B1, the input features of shape [C, H, W] are serialized into a tensor of shape [(H / P)*(W / P), C], where P is the size of the patch, which can also be understood as the multiple of downsampling;
[0168] Step B2: The tensor passes through a multi-head self-attention module with multi-scale pooling. The multi-head self-attention module uses pooling layers of different scales to obtain context information of different scales and reduce (key) and The size of (value);
[0169] Step B3: add the output of step B1 and the output of step B2 and perform layer normalization processing;
[0170] Step B4: sending the output of step B3 to a feedforward neural network, which is composed of convolution and activation functions, performs nonlinear transformation and mapping, obtains two-dimensional neighbor information, and enhances the representation ability of the model;
[0171] Step B5: add the output of step B3 and the output of step B4 and perform layer normalization processing;
[0172] Step B6: Perform feature deserialization on the output of step B5 to convert the output features into a tensor with a shape of [C, H / P, W / P].
[0173] like Figure 3 As shown, in step B1, the processing steps of feature serialization include:
[0174] Step C1: Input features are passed through a convolution module, where the step length is P (the image is divided into N small blocks, and the block size is P);
[0175] Step C2, flatten the output of the convolution module;
[0176] Step C3, converting dimensions;
[0177] Step C4: Use layer normalization to process the features to obtain output features.
[0178] like Figure 4 As shown, in step B2, the input feature The shape of is [B,H×W,C], B is the batchsize, and the processing steps of the multi-head self-attention module with multi-scale pooling include:
[0179] Step D1: Reshape the input feature to [B, C, H, W]. Use average pooling layers of different scales (P1, P2, P3, P4) to process the feature and obtain the following features: , , , , the shapes are: [B,C,H / P1,W / P1], [B,C,H / P2,W / P2], [B,C,H / P3,W / P3], [B,C,H / P4,W / P4],
[0180]
[0181]
[0182]
[0183]
[0184] Step D2: Reshape the 4 features output by step D1 , , , , so that their shapes become [B,C,(H / P1)×(W / P1)], [B,C,(H / P2)×(W / P2)], [B,C,(H / P3)×(W / P3)], [B,C,(H / P4)×(W / P4)]. After splicing the above features, the shape becomes [B,C,S],
[0185]
[0186]
[0187]
[0188]
[0189]
[0190]
[0191] in, Represents feature reshaping, represents the concatenation function, Represents the concatenated output features.
[0192] Step D3: reshape the concatenated output features output by step D2 and perform layer normalization to obtain features , the shape becomes [B,S,C],
[0193]
[0194] in, Representation layer normalization processing, Function represents a permutation operation.
[0195] Step D4: Output the features of step D3 Send it to the fully connected layer FC to get the features , the shape becomes [B,S,2C]. Then, reshape the features to get the features , the shape becomes [2, B, N, S, C / N], where N is the number of heads in the multi-head self-attention module, which is used to control the number of attention heads in the attention mechanism.
[0196]
[0197]
[0198] in, Functions are used to change the shape of dimensions;
[0199] Step D5: Segment the features output from step D4 The features after segmentation are (key) and (value), the shape is [B,N,S,C / N]:
[0200]
[0201] Step D6: Input features Feed into the fully connected layer , the shape is [B,H×W,C]. Reshape the features and get the features (query), the shape is [B, N, H×W, C / N],
[0202]
[0203] Step D7 Reshape Features The shape of the feature is [B, N, C / N, S] .right and Perform matrix multiplication, and the feature shape after multiplication becomes [B, N, H*W, S]. Processing, getting features :
[0204]
[0205]
[0206] in, represents matrix multiplication, The output layer of the neural network converts the output of the network into the probability of occurrence of each category.
[0207] Step D8: The features output from step D7 With features Perform matrix multiplication to obtain features , the shape is [B,N,H W,C / N]. Reshape features , get the features , the shape becomes [B,H W,C]:
[0208]
[0209]
[0210] in, Indicates transpose.
[0211] Step D9: Output the features of step D8 Feed into the fully connected layer , we can get the final output features , the shape is [B,H W,C]:
[0212] .
[0213] like Figure 5 As shown, in step B4, the present invention replaces the fully connected layer in the feedforward neural network with a local convolution, aiming to help the model obtain two-dimensional neighbor information while reducing the amount of model calculation. Activation function, The activation function provides a nonlinear transformation when the input value is between -3 and 3, alleviating the gradient vanishing problem while enhancing the expressiveness of the model. In addition, the calculation of this activation function is relatively simple, involving only multiplication and addition.
[0214] Ignore the batch size dimension and input features The shape is [H W,C], the feedforward neural network processing steps include:
[0215] Step E1: Input features Reshape the features to get the features , the shape becomes [C,H,W]:
[0216]
[0217] Step E2: After two convolutional layers and activation layers, the feature :
[0218]
[0219] in, The calculation formula of the activation function is as follows,
[0220]
[0221]
[0222] Step E3: Get features after convolution module ,feature After reshaping and transposition, the output features are obtained , the shape is [H W, C],
[0223]
[0224] .
[0225] In step B6, feature deserialization is equivalent to feature reshaping, the purpose of which is to transform the shape of the input feature from [H W,C] is reshaped into [C,H,W],
[0226] .
[0227] The multi-scale attention module is used to extract feature representations with different distance context information at different stages of the CNN branch. It uses convolution operations of different scales to obtain attention weights, which are similar to the context information in the transformer, and assigns different attention weights to different positions in the feature map. In this way, the feature differences between the two branches can be reduced, so that the CNN branch can effectively extract high-quality semantic information from the transformer branch and improve the generalization performance of the model.
[0228] like Figure 6 As shown, the processing steps of the multi-scale attention module include:
[0229] Step F1: Use 1×1 convolution to input feature map Perform linear transformation and reduce dimension, using The activation function introduces nonlinearity, enhances the expressiveness of features, and obtains features ,
[0230]
[0231] in, The formula is as follows,
[0232]
[0233] Step F2: Use convolutions of different scales to extract context information of different receptive fields and obtain features , , , and , , Add element by element to get the feature ,
[0234]
[0235]
[0236]
[0237]
[0238] Step F3: Use 1×1 convolution to mix information from different channels and increase the dimension to obtain attention weights ,
[0239]
[0240] Step F4: Input features With attention weight Perform matrix multiplication to obtain output features , improve the model's ability to understand and process image features,
[0241] .
[0242] The semantic information alignment module is used to align the feature representations extracted by the CNN branch and the transformer branch. Due to the differences in the features learned by the two branches, directly aligning their features may make it difficult for the model to converge. Therefore, by using the semantic information alignment module, features are further extracted before alignment, avoiding the problem of direct alignment and making the transformer branch more efficient in assisting the training of the CNN branch.
[0243] The CNN branch first performs a 1×1 convolution to change the number of channels in the feature map to match the number of channels in the transformer branch features, and then performs a 3×3 convolution to further extract information. The transformer branch performs a 3×3 convolution to prevent the features of the transformer branch from being directly aligned with the features of the CNN branch.
[0244] The input of the model is a face image cropped and scaled to a specific size by the detection box output by the face detector.
[0245] During model training, before the training sample images are input into the liveness detection model to be trained, they need to be processed using methods such as random rotation, random flipping, adding Gaussian noise, and random contrast enhancement to improve the richness of the training samples. Subsequently, the input images are normalized to limit the pixel values of the images to between 0 and 1.
[0246] The training cycle of the model is 100 epochs (when the model is trained to 100 epochs, the model loss has converged), the batch size is 256, the learning rate is 0.01, the optimizer is SGD, the momentum is 0.9, and the optimizer adjustment strategy is StepLR.
[0247] The loss function of the model is divided into two parts:
[0248] The first part is the channel-wise distillation loss, which is used to transfer the knowledge of the transformer branch (complex teacher model) to the lightweight CNN branch (student model). The calculation formula is as follows:
[0249]
[0250] Where c represents the feature map channel index; i represents the map feature pixel index; Hyperparameters for controlling the smoothness of probability distributions;
[0251] in, Hyperparameters for controlling the smoothness of probability distributions; is the number of image samples; C is the number of channels of the feature map; H is the height of the feature map; W is the width of the feature map; Represents the output feature of the i-th image in the transformer branch, the c-th channel and the pixel position index is j; Represents the output feature of the i-th image in the CNN branch, the c-th channel and the pixel position index is j; represents the image index in the feature map; c represents the channel index of the feature map; Represents the pixel index of the image feature; :Indicates is a function of the independent variable; Represents the output features of the i-th image and the c-th channel; represents an exponential function with the natural exponent e as the base, which ensures non-negative output and enhances numerical stability; even if the differences between the inputs are small, these differences will be amplified after the exponential operation, which helps to better distinguish categories during training;
[0252] The second part, cross-entropy loss, is calculated as follows:
[0253]
[0254] in, : Number of image samples; : image label; : Model predicted value.
[0255] Overall loss function The calculation formula is as follows,
[0256]
[0257] in, : The weight of the channel-by-channel distillation loss; : CNN branch; : Cross entropy loss of transformer branch; : CNN branch; : The weight of the cross entropy loss of the transformer branch (set to 1 and 0.5 by default); : represents the channel-by-channel distillation loss.
[0258] During model inference, the image is normalized before being input into the model. The output of the model is a liveness score with a shape of [N,2], and the category corresponding to the maximum score is taken as the model prediction category. The transformer branch is not required at this stage, so the model can run in resource-constrained devices or scenarios.
[0259] The liveness detection device based on local convolution and transformer semantic information of the present invention utilizes a deep neural network structure, and realizes accurate distinction between real and fake faces in images through the cooperation of local convolution module, transformer module, multi-scale attention module and semantic information alignment module. It has a high real person pass rate and the ability to resist various attacks. The multi-head self-attention module with multi-scale pooling in the transformer module further improves the performance of the model by compressing the feature sequence length and enhancing the feature representation capability.
[0260] The above embodiments and drawings do not limit the product form and style of the present invention. Any appropriate changes or modifications made by ordinary technicians in the relevant technical field should be deemed to be within the patent scope of the present invention.
Claims
1. A liveness detection device based on local convolution and semantic information, comprising an image acquisition module for acquiring a face image of a user to be detected, performing a normalization operation on the face image and then inputting it into a trained liveness detection model, characterized in that: The liveness detection device uses a liveness detection model based on a deep neural network structure and performs model reasoning. The liveness detection model includes a CNN branch, a transformer branch, a semantic information alignment module, and a model loss calculation module. The CNN branch includes a local convolution module and a multi-scale attention module. The local convolution module includes local convolution and point-by-point convolution. Local convolution is used to perform convolution processing on some channels of the image, while the remaining channels remain as they are. Point-by-point convolution is used to make full use of the information of different channels to further extract features. The multi-scale attention module is used to extract feature representations with long-range contextual information at different stages of the CNN branch. It uses convolution operations of different scales to obtain attention weights and assigns different weights to different positions in the feature map. The transformer branch includes multiple transformer modules, which are used to assist in training the CNN branch during the training phase. The transformer module has a multi-head self-attention module with multi-scale pooling to compress the length of feature sequences and enhance feature representation capabilities. The processing steps of the transformer module include: Step B1: The input features of shape [C, H, W] are serialized into a tensor of shape [(H / P)×(W / P), C], where P is the size of the patch. The tensors of step B2 and step B1 pass through a multi-head self-attention module with multi-scale pooling. The multi-head self-attention module uses pooling layers of different scales to obtain context information of different scales and reduce Size; Step B3: add the output of step B1 and the output of step B2 and perform layer normalization processing; Step B4: sending the output of step B3 to a feedforward neural network, which is composed of local convolution and activation functions, performs nonlinear transformation and mapping, obtains two-dimensional neighbor information, and enhances the representation ability of the model; Step B5: add the output of step B3 and the output of step B4 and perform layer normalization processing; Step B6: perform feature deserialization operation on the output of step B5, so that the output feature becomes a tensor with a shape of [C, H / P, W / P]; The semantic information alignment module is used to align the feature representations extracted by the CNN branch and the transformer branch; The model loss calculation module includes channel-by-channel distillation loss, cross entropy loss and overall loss; channel-by-channel distillation loss is used to transfer the knowledge of the transformer branch to the CNN branch. The calculation formula of channel-by-channel distillation loss is as follows in, Hyperparameters for controlling the smoothness of probability distributions; is the number of image samples; C is the number of channels of the feature map; H is the height of the feature map; W is the width of the feature map; Represents the output feature of the i-th image in the transformer branch, the c-th channel and the pixel position index is j; Represents the output feature of the i-th image in the CNN branch, the c-th channel and the pixel position index is j; represents the image index in the feature map; c represents the channel index of the feature map; Represents the pixel index of the image feature; :Indicates is a function of the independent variable; Represents the output features of the i-th image and the c-th channel; Represents an exponential function with natural exponent e as base, which guarantees non-negativity of output and enhances numerical stability; The CNN branch and the transformer branch calculate the cross entropy loss and cross entropy loss respectively. The calculation formula is as follows, in, is the image label; is the model prediction value; Overall loss function The calculation formula is as follows, in, is the weight of the channel-by-channel distillation loss; is the cross entropy loss of the CNN branch; is the cross entropy loss of the transformer branch; is the weight of the cross entropy loss of the CNN branch; is the weight of the cross entropy loss of the transformer branch; is the channel-by-channel distillation loss; When the liveness detection model is inferred, the image is normalized and then input into the liveness detection model. The output of the liveness detection model is a liveness score with a shape of [N, 2]. The category corresponding to the maximum score is taken as the model prediction category.
2. A method for liveness detection based on local convolution and semantic information, characterized in that: The method comprises the following steps: obtaining a face image of a user to be detected, normalizing the face image and inputting the image into a trained liveness detection model, wherein the liveness detection model calculates a liveness score corresponding to the face image, and determines whether the user to be detected is live according to the liveness score; The training process of the liveness detection model includes: Input the image of the training sample into the living body detection model to be trained; Step 1: Extract local features of the image in the CNN branch: Use local convolution to convolve some channels of the image, keep the other channels as they are, and use point-by-point convolution to make full use of the information of different channels to further extract features; Step 2: Use multiple transformer modules to form a transformer branch to assist in training the CNN branch during the training phase of the liveness detection model, provide global context information for the CNN branch, and enhance the model's ability to model long-distance dependencies in the image. The processing steps of the transformer module include: Step B1: The input features of shape [C, H, W] are serialized into a tensor of shape [(H / P)×(W / P), C], where P is the size of the patch. The tensors of step B2 and step B1 pass through a multi-head self-attention module with multi-scale pooling. The multi-head self-attention module uses pooling layers of different scales to obtain context information of different scales and reduce Size; Step B3: add the output of step B1 and the output of step B2 and perform layer normalization processing; Step B4: sending the output of step B3 to a feedforward neural network, which is composed of local convolution and activation functions, performs nonlinear transformation and mapping, obtains two-dimensional neighbor information, and enhances the representation ability of the model; Step B5: add the output of step B3 and the output of step B4 and perform layer normalization processing; Step B6: perform feature deserialization operation on the output of step B5, so that the output feature becomes a tensor with a shape of [C, H / P, W / P]; Step 3: Use the multi-scale attention module to extract feature representations with different distance context information at different stages of the CNN branch. By using convolution operations of different scales to obtain attention weights, different attention weights are assigned to different positions in the feature map to reduce the feature differences between the CNN branch and the transformer branch, so that the CNN branch can extract high-quality semantic information from the transformer branch. Step 4: Use the semantic alignment module to align the feature representations extracted by the CNN branch and the transformer branch to avoid direct alignment of the CNN branch and the transformer branch. Step 5: Calculate the loss of the liveness detection model, including channel-by-channel distillation loss, cross entropy loss, and overall loss. The channel-by-channel distillation loss is used to transfer the knowledge of the transformer branch to the CNN branch. The calculation formula of the channel-by-channel distillation loss is as follows: in, Hyperparameters for controlling the smoothness of probability distributions; is the number of image samples; C is the number of channels of the feature map; H is the height of the feature map; W is the width of the feature map; Represents the output feature of the i-th image in the transformer branch, the c-th channel and the pixel position index is j; Represents the output feature of the i-th image in the CNN branch, the c-th channel and the pixel position index is j; represents the image index in the feature map; c represents the channel index of the feature map; Represents the pixel index of the image feature; :Indicates is a function of the independent variable; Represents the output features of the i-th image and the c-th channel; Represents an exponential function with natural exponent e as base, which guarantees non-negativity of output and enhances numerical stability; The CNN branch and the transformer branch calculate the cross entropy loss and cross entropy loss respectively. The calculation formula is as follows, in, is the image label; is the model prediction value; Overall loss function The calculation formula is as follows, in, is the weight of the channel-by-channel distillation loss; is the cross entropy loss of the CNN branch; is the cross entropy loss of the transformer branch; is the weight of the cross entropy loss of the CNN branch; is the weight of the cross entropy loss of the transformer branch; is the channel-by-channel distillation loss; Step 6: Infer the liveness detection model: Normalize the image and input it into the liveness detection model. The output of the liveness detection model is a liveness score with a shape of [N, 2]. The category corresponding to the maximum score is taken as the model prediction category.
3. The method for liveness detection based on local convolution and semantic information as claimed in claim 2, characterized in that: In step 1, local convolution specifically includes the following steps: Step A1: The shape of the input tensor is [C, H, W], where C is the channel, H is the height, and W is the width. The local convolution splits the input tensor along the channel dimension into two tensor features. , , the shapes are [K,H,W] and [CK,H,W] respectively, K is the feature The number of channels, CK is the characteristic The number of channels, and The sum of the number of channels is C, that is, K+ (CK) = C; in, represents the input features, Indicates that the features are segmented along the dimension dim; Step A2: For the first tensor feature Perform convolution operation to obtain features : Step A3: Concatenate two tensor features along the channel dimension , , in, Indicates that the features are concatenated along the dimension dim. represents the output features; The calculation formula of the point-by-point convolution PWConv is as follows: in, Represents the feature output of point-by-point convolution, ReLU represents the ReLU activation function, and BN represents the batch normalization layer.
4. The method for liveness detection based on local convolution and semantic information as claimed in claim 2, characterized in that: In step B1, the processing steps of feature serialization include: Step C1, input features pass through the convolution module; Step C2, flatten the output of the convolution module; Step C3, converting dimensions; Step C4: Use layer normalization to process the features to obtain output features.
5. The method for liveness detection based on local convolution and semantic information as claimed in claim 2, characterized in that: In step B2, let the input feature The shape of is [B,H×W,C], B is the batch size, and the processing steps of the multi-head self-attention module with multi-scale pooling include: Step D1: Reshape input features The shape of the input feature is changed to [B, C, H, W]; the average pooling layers of different scales P1, P2, P3, and P4 are used to process the input feature. , and obtain the following features , , , , the shapes are: [B,C,H / P1,W / P1], [B,C,H / P2,W / P2], [B,C,H / P3,W / P3], [B,C,H / P4,W / P4], Step D2: Reshape the 4 features output by step D1 , , , , so that their shapes become [B,C,(H / P1)×(W / P1)], [B,C,(H / P2)×(W / P2)], [B,C,(H / P3)×(W / P3)], [B,C,(H / P4)×(W / P4)] respectively. By combining the above features, the shape becomes [B,C,S]. in, Represents feature reshaping, represents the concatenation function, represents the concatenated output feature; S represents the feature The length of the last dimension; Step D3: reshape the concatenated output features output by step D2 and perform layer normalization to obtain features , the shape becomes [B,S,C], in, Representation layer normalization processing, Functions represent permutation operations; Step D4: Output the features of step D3 Send it to the fully connected layer FC to get the features , the shape becomes [B,S,2C]; then, reshape the features to obtain the features , the shape becomes [2, B, M, S, C / M], where M is the number of heads in the multi-head self-attention module, which is used to control the number of attention heads in the attention mechanism. in, Functions are used to change the shape of dimensions; Step D5: Segment the features output from step D4 The features after segmentation are and , the shape is [B,M,S,C / M]: Step D6: Input features Feed into the fully connected layer , the shape is [B,H×W,C], reshape the features, and get the features , the shape is [B,M,H×W,C / M], Step D7 Reshape Features The shape of the feature is [B,M,C / M,S] ;right and Perform matrix multiplication, and the feature shape after multiplication becomes [B, M, H W,S]; along the last dimension, the features are Processing, getting features : in, represents matrix multiplication, The output layer of the neural network converts the output of the network into the probability of occurrence of each category; Step D8: The features output from step D7 With features Perform matrix multiplication to obtain features , the shape is [B,M,H W, C / M]; reshape features , get the features , the shape becomes [B,H W,C]: in, represents transpose; Step D9: Output the features of step D8 Feed into the fully connected layer , and obtain the final output features , the shape is [B,H W,C]: 。 6. The method for liveness detection based on local convolution and semantic information as claimed in claim 2, characterized in that: In step B4, replace the fully connected layer in the feedforward neural network with a local convolution layer and connect Activation function, The activation function provides a nonlinear transformation when the input value is between -3 and 3, alleviating the gradient vanishing problem while enhancing the expressiveness of the model; Input Features A tensor of shape [H W,C], the feedforward neural network processing steps include: Step E1: Input features Reshape the features to get the features , the shape becomes [C,H,W]: Step E2: After two convolutional layers and activation layers, the feature : in, The calculation formula of the activation function is as follows, Step E3: Get features after convolution module ,feature After reshaping and transposition, the output features are obtained , the shape is [H W, C], 。 7. The method for liveness detection based on local convolution and semantic information as claimed in claim 2, characterized in that: In step B6, feature deserialization is equivalent to feature reshaping, the purpose of which is to transform the shape of the input feature from [H W,C] is reshaped into [C,H,W], 。 8. The method for liveness detection based on local convolution and semantic information as claimed in claim 2, characterized in that: The processing steps of the multi-scale attention module include: Step F1: Use 1×1 convolution to input feature map Perform linear transformation and reduce dimension, using The activation function introduces nonlinearity, enhances the expressiveness of features, and obtains features , in, The formula is as follows, Step F2: Use convolutions of different scales to extract context information of different receptive fields and obtain features , , , and , , Add element by element to get the feature , Step F3: Use 1×1 convolution to mix information from different channels and increase the dimension to obtain attention weights , Step F4: Input features With attention weight Perform matrix multiplication , and get the output features , improve the ability of the liveness detection model to understand and process image features, 。 9. The method for liveness detection based on local convolution and semantic information as claimed in claim 2, characterized in that: The semantic information alignment module further extracts features before aligning the feature representations extracted by the CNN branch and the transformer branch. The CNN branch first performs a 1×1 convolution to change the number of channels of the feature map to match the number of channels of the transformer branch features, and then performs a 3×3 convolution to further extract information. The transformer branch performs a 3×3 convolution to avoid direct alignment of the transformer branch features with the CNN branch features.
10. The method for liveness detection based on local convolution and semantic information as claimed in claim 2, characterized in that: Before the image of the training sample is input into the liveness detection model to be trained, it is necessary to process the input image using any one of the following methods or any combination: random rotation, random flipping, adding Gaussian noise, and random contrast enhancement. Subsequently, the input image is normalized to limit the pixel value of the image to between 0 and 1.
