A pornographic image recognition method based on a CNN-Transformer cascade structure and a CBAM mechanism
Patent Information
- Application Number
- CN202411014897.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-07-26
- Publication Date
- 2026-10-27
AI Technical Summary
但此方法特征提取能力和泛化能力较为不足
[0046]本发明首次将Vision Transformer引入色情图像识别领域,传统的色情图像识别方法往往依赖于手工设计的特征和复杂的规则,这些方法不仅工作量大,而且识别效果有限。而Vision Transformer以其强大的特征提取能力和全局信息建模能力,为色情图像识别提供了新的解决方案。
Smart Images

Figure CN122990411A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the technical fields of artificial intelligence and image classification, and in particular to a method for pornographic image recognition based on a CNN-Transformer cascade structure and CBAM mechanism. Background Technology
[0002] In today's digital age, a massive amount of visual content floods the internet at an unpredictable rate, including a wide variety of images, some of which may contain inappropriate or pornographic content. Pornographic images typically contain obscene, sexually suggestive, or other content unsuitable for public display. This content can not only harm user experience but also lead to social and moral issues and create security risks. Therefore, in order to maintain the health and safety of online communities, timely and accurate identification and filtering of pornographic images has become a crucial task for cybersecurity and user protection.
[0003] Traditional methods for pornographic image recognition primarily rely on hand-designed features and classifiers. These methods utilize the proportion of exposed skin and the specific shapes and textures of genital areas as recognition criteria, which presents limitations when dealing with complex and varied pornographic images. In recent years, the rapid development of deep learning technology has brought new hope to the field of pornographic image recognition. However, existing deep learning methods have relatively insufficient feature extraction and generalization capabilities, with the highest classification accuracy reaching only 94.5%. The Transformer model, as a powerful deep learning architecture, has achieved significant results in natural language processing, and its influence is gradually expanding into image classification; however, there are currently no methods applying it to pornographic image classification.
[0004] The invention patent application CN202010059493.1 discloses a method for pornographic image recognition based on convolutional neural networks. This method involves inputting the image to be recognized into a trained convolutional neural network to extract image features, generating a feature image. The feature image is then recognized using a classification model of the convolutional neural network to generate a first recognition result. A target detection model of the convolutional neural network is then used to recognize the feature image, generating a second recognition result. Finally, the first and second recognition results are fused using a fusion decision model of the convolutional neural network to obtain the final recognition result, thus improving the accuracy, efficiency, and performance of image recognition. However, this method has relatively insufficient feature extraction and generalization capabilities. Summary of the Invention
[0005] To address the shortcomings of existing pornographic image recognition methods in terms of feature extraction and generalization capabilities, this invention proposes a pornographic image recognition method based on a CNN-Transformer cascade structure and a CBAM mechanism. By introducing the Vision Transformer (VIT) into the field of pornographic image recognition and incorporating a Convolutional Block Attention Module (CBAM) to improve the Vision Transformer, it can more accurately focus on local features of the image, thereby more effectively capturing detailed information in the image, improving the accuracy and richness of feature representation, enhancing the generalization ability of the pornographic image recognition method, and improving the accuracy of pornographic image identification.
[0006] To achieve the above objectives, the technical solution of the present invention is implemented as follows:
[0007] A method for pornographic image recognition based on CNN-Transformer cascade structure and CBAM mechanism, comprising the following steps:
[0008] S0: Construct the CNN-CBAM-ViT model, which includes a CNN convolutional neural network module and a VisionTransformer module connected in sequence;
[0009] S1: Obtain the experimental image dataset, including normal images and pornographic images, divide the experimental image dataset into training set, validation set and test set, use the training set and validation set as training samples, perform data preprocessing on the training samples to obtain input samples;
[0010] S2: Use the CNN convolutional neural network module to extract features from the input samples, obtain an initial feature map, and fuse the initial feature map with the input samples to obtain a fused feature map u;
[0011] S3: Perform image block processing, linear transformation and position encoding on the fused feature map u in sequence to obtain the embedded block sequence;
[0012] S4: Encode the embedded block sequence using the VisionTransformer module to obtain the final result sequence. Use the final result sequence and the VisionTransformer module to predict the image category label and obtain the prediction result. Validate the result using the validation set.
[0013] S5: Repeat steps S2 to S4 until the CNN-CBAM-ViT model achieves optimal performance on the validation set, thus obtaining the trained CNN-CBAM-ViT model;
[0014] S6: Test the trained CNN-CBAM-ViT model using the test set to obtain the classification of the images in the test set.
[0015] The Vision Transformer module described in step S0 includes a linear embedding layer, a Transformer encoder, and a multilayer perceptron classifier connected in sequence. The Transformer encoder includes a convolutional block attention module and a feedforward neural network module connected in sequence.
[0016] The specific method for obtaining the input sample in step S1 is as follows: Obtain an experimental image dataset containing a few normal images and a majority of pornographic images. Divide the experimental image dataset into a training set, a validation set, and a test set in a 6:2:2 ratio. Use the preprocessing module in the PyTorch framework to perform horizontal flipping, random cropping, and standardization to obtain the input sample x∈R. C×H×W C is the number of image channels, H is the image height, and W is the image width.
[0017] Step S2, obtaining the fused feature map u, specifically involves: inputting the input sample x into the CNN convolutional neural network module. The input sample x passes through a convolutional layer, a ReLU activation function layer, a max pooling layer, and a transposed convolutional layer to obtain the initial feature map x1. The process of obtaining the initial feature map x1 is as follows:
[0018] x1=ConvTranspose(MaxPool(ReLU(Conv(x))));
[0019] Where Conv is the convolution function, ReLU is the ReLU activation function, MaxPool is the max pooling operation, and ConvTranspose is the function for transposing the convolutional layer;
[0020] The initial feature map x1 is then fused with the input sample x to obtain the fused feature map u = x1⊕x.
[0021] The image block processing method described in step S3 is as follows: First, the fused feature map u is divided into N blocks using a linear embedding layer. Then, each block is flattened into a one-dimensional vector, ultimately obtaining a high-dimensional vector space feature x. p ;
[0022] The linear transformation method described in step S3 is as follows: For each flattened high-dimensional vector space feature x, a linear embedding layer is used. p Perform a transformation to obtain the transformed feature y. The formula for the linear transformation is: E is the weight matrix, and b is the bias term. The i-th block, y i It is the transformed feature, y i∈y, i={1,2,..,N}; a special classification label v is added to the transformed feature y. class The embedded feature k is obtained.
[0023] The location encoding method described in step S3 is as follows: Define a location embedding matrix E that matches the number of image blocks. pos Its dimension is (N+1, embed_dim), where embed_dim is the embedded spatial feature x. p The dimension is then determined, and the position embedding matrix is initialized using a truncated normal distribution. The position embedding matrix E is then... pos Add it to the corresponding embedded feature k.
[0024] The formula for obtaining the embedded block sequence in step S3 is as follows:
[0025] 1.
[0026] Where Z0 represents the embedded block sequence, v class E is a special classification marker. pos P represents the position embedding matrix. 2 The block size is P×P, and D represents the dimension of the embedding vector.
[0027] Step S4 describes the method of encoding the embedded block sequence using the VisionTransformer module as follows: The embedded block sequence Z0 is input to the Transformer encoder. The embedded block sequence Z0 passes through L encoding layers. The input of the l-th encoding layer is the output of the (l-1)-th encoding layer. In the l-th encoding layer, the result sequence Z of the previous encoding layer... l-1 After normalization layer I, and then through channel attention and spatial attention calculations in the convolutional block attention module, the intermediate variable Z of the transformer coding layer after passing through the convolutional block attention module is obtained. l Then, after normalization layer II, a feedforward neural network is performed to obtain the result sequence Z of the transformer coding layer. l .
[0028] The method for calculating channel attention is as follows: the result sequence Z of the previous coding layer l-1 After normalization layer I, the input feature map F∈R is obtained. C×H×W The input feature map F is first transformed into two feature maps F1 and F2 of size C×H×W through two parallel operations: max pooling and average pooling. Feature maps F1 and F2 are then fed into a shared multilayer perceptron module, where they undergo compression, ReLU activation function, and dilation operations to obtain two feature maps F11 and F2 of size C×1×1.22 , feature map F 11 and feature map F 22 Element-wise summation, followed by passing through a Sigmoid activation function to obtain the channel attention feature map:
[0029] M C (F)=σ(MLP(AvgPool(F))+MLP(MaxPool(F)));
[0030] The input feature map F is fused with the channel attention feature map Mc(F) to obtain the channel attention fused feature map:
[0031]
[0032] In this context, AvgPool() represents the average pooling operation, MaxPool represents the max pooling operation, MLP() represents the operation function for the shared multilayer perceptron module, and σ() represents the sigmoid activation function. This indicates element-wise multiplication.
[0033] The method for spatial attention calculation is as follows: First, the output of the channel attention module, the channel attention fusion feature map F', is processed through two parallel operations, a max pooling layer and an average pooling layer, to obtain two feature maps F'1 and F'2 of size 1×H×W. Next, a concatenation operation is used to concatenate feature maps F'1 and F'2 along the channel dimension to form a feature map F' of size 2×H×W. 12 Then, the feature map F' is processed through a 7×7 convolutional layer. 12 The feature map is converted into a single-channel feature map with the same spatial dimension as the attention fusion feature map F'. Finally, a sigmoid activation function is applied to obtain the spatial attention map: M. S (F′)=σ(f 7*7 ([AvgPool(F′);MaxPool(F′)]));
[0034] Among them, f 7*7 This represents a 7×7 convolution; ";" indicates a concatenation operation.
[0035] The channel attention fusion feature map F' is fused with the spatial attention map Mc(F) to obtain the output feature map of the convolutional block attention module. The output feature map F″ of the convolutional block attention module is then passed through a residual connection to obtain the intermediate variable after the transformer coding layer passes through the convolutional block attention module:
[0036] Z l =CBAM(LN(Z) l-1))+Z l-1 ,l=1…L;
[0037] LN() represents the normalization operation, and CBAM() represents the operation function of the convolutional block attention module.
[0038] The method for performing feedforward neural network calculations is as follows: The intermediate variable Z... l First, the signal passes through normalization layer II, then is fed into the feedforward neural network module. The output then passes through a residual connection to obtain the result sequence after a complete transformer encoding layer.
[0039] Z l =ffn(LN(Z) l ′))+Z l ′,l=1...L;
[0040] ffn() represents the computation function of the feedforward neural network, which yields the final sequence Z in the last transformer encoding layer. L ;
[0041] Step S4 describes the method for predicting image category labels using the final result sequence and the VisionTransformer module: Extract the final result sequence Z. L The first element in This data is then passed to a multilayer perceptron classifier, which predicts the image's class label based on the features extracted by the Transformer encoder. The formula for predicting the image's class label is as follows:
[0042]
[0043] mlp_head() represents the multilayer perceptron classification function, where q is the class label;
[0044] The method for validation using the validation set in step S4 is as follows: run the model on the validation set, use the cross-entropy loss function to calculate the error between the predicted result and the actual label, update the model weights using the SGD optimizer according to the loss gradient, adjust the learning rate using the cosine annealing algorithm according to the training progress, calculate the evaluation index, and if the accuracy is higher than the accuracy of previous rounds, save the model weights and optimizer state.
[0045] The beneficial effects of this invention are:
[0046] This invention introduces Vision Transformer into the field of pornographic image recognition for the first time. Traditional pornographic image recognition methods often rely on manually designed features and complex rules, which are not only labor-intensive but also have limited recognition results. Vision Transformer, with its powerful feature extraction and global information modeling capabilities, provides a new solution for pornographic image recognition.
[0047] This invention performs depth extraction on the input feature image. During this process, the resulting feature map is then fused with the input feature image to generate a richer and more detailed feature map, which serves as a key input for subsequent processing steps. Compared to methods that rely solely on the Vision Transformer without involving convolutional neural network operations, the CNN-Transformer cascade structure can more accurately focus on local features of the image, thereby more effectively capturing detailed information in the image and improving the accuracy and richness of feature representation.
[0048] This invention introduces a convolutional block attention mechanism to improve the Vision Transformer. Traditional Vision Transformer uses a multi-head self-attention mechanism for feature learning, but this method is computationally intensive and susceptible to noise. The convolutional block attention mechanism (CBAM), by combining channel attention and spatial attention, can more accurately capture key information in the image, thereby improving the model's performance. Attached Figure Description
[0049] To more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0050] Figure 1 In the diagram, (a) shows the overall structure of the CNN-CBAM-ViT model of the present invention, and (b) shows the Transformer encoder module with the Convolutional Block Attention (CBAM) module.
[0051] Figure 2 This is a flowchart of the computation process of the Convolutional Block Attention (CBAM) module of the present invention.
[0052] Figure 3 In the figure, (a) is the curve of accuracy change during the validation process, and (b) is the curve of loss value change during the validation process.
[0053] Figure 4In the figure, (a) and (b) represent the precision, recall, and F1 score of the model in identifying pornographic and normal images. Detailed Implementation
[0054] The technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.
[0055] like Figure 1 As shown, a pornographic image recognition method based on CNN-Transformer cascade structure and CBAM mechanism is presented. The invention includes the following steps:
[0056] S0: Construct the CNN-CBAM-ViT model, which includes a convolutional neural network module and a VisionTransformer module connected in sequence.
[0057] like Figure 1 As shown in (a), the Vision Transformer module includes a linear embedding layer, a Transformer encoder, and a (MLP Head) multilayer perceptron classifier connected in sequence. In the Vision Transformer, the (MLP Head) multilayer perceptron classifier consists of a fully connected layer (Linear Layer) and a softmax layer. The Transformer encoder includes a (CBAM) convolutional block attention module and a feedforward neural network module connected in sequence.
[0058] S1: Obtain the experimental image dataset, including normal images and pornographic images. Divide the experimental image dataset into training set, validation set and test set. Use the training set and validation set as training samples. Perform data preprocessing on the training samples to obtain input samples.
[0059] The specific method for obtaining the input sample in step S1 is as follows:
[0060] Obtaining the Experimental Image Dataset: The experimental image dataset used in this embodiment of the invention comes from the public dataset project nsfw_data_scrapper published on GitHub by data scientist Alexander Kim. The experimental image dataset consists of two parts, totaling 67,776 images. One part consists of normal images (neutral), totaling 21,917 images, accounting for 32%, and the other part consists of pornographic images (porn), totaling 45,859 images, accounting for 68%. The experimental image dataset is divided into a training set, a validation set, and a test set in a 6:2:2 ratio. The data in the divided training and validation sets are shuffled and then used as training samples in batches to be fed into the CNN-CBAM-ViT model for training.
[0061] And perform initialization operations: preprocess the training samples, including using the torchvision.transforms module of the PyTorch framework to perform horizontal flipping, random cropping, and standardization to obtain the input sample x∈R. 3×224×224 .
[0062] The training process for a convolutional neural network module is as follows:
[0063] S2: Use the convolutional neural network module to extract features from the input sample, obtain the initial feature map, and fuse the initial feature map with the input sample to obtain the fused feature map u.
[0064] The specific method for obtaining the fused feature map u in step S2 is as follows: Figure 1 (a) As shown in the upper part, the input sample x is fed into the CNN convolutional neural network module. The input sample x passes through a 3×3 convolutional layer (Conv), a ReLU activation function layer, a max pooling layer (MaxPool), and a 2×2 transposed convolutional layer (ConvTranspose) to obtain the initial feature map x1∈R. 224×224×3 The process of obtaining the initial feature map x1 is as follows:
[0065] x1=ConvTranspose(MaxPool(ReLU(Conv(x))));
[0066] The initial feature map x1 is then fused with the input sample x to obtain a richer fused feature map u∈R. 224 ×224×3 As the input to subsequent modules, the process of obtaining the fused feature map u is: u = x1 ⊕ x.
[0067] The training process for the Vision Transformer module is as follows:
[0068] S3: Perform image block processing, linear transformation and position encoding on the fused feature map u in sequence to obtain the embedded block sequence.
[0069] like Figure 1 (a) As shown in the lower half, the fused feature map u extracted by the (CNN) convolutional neural network module is fed into the linear embedding layer. The labeled embedding block sequence Z0 is obtained through image block processing, linear transformation and position encoding, which serves as the input sequence of the Transformer encoder.
[0070] The image block processing method described in step S3 is as follows: It is implemented using a convolution with a kernel size of 16×16, a stride of 16, and 768 kernels. Specifically, the input feature map u∈R is processed... 224×224×3 The vector is divided into (224 / 16) × (224 / 16) = 196 blocks of size 16 × 16 × 3. Each block is then flattened into a one-dimensional vector. For each 16 × 16 × 3 block, the length of the flattened vector is 16 × 16 × 3 = 768. Finally, the high-dimensional vector space feature x is obtained. p ∈R 196×768 196 is the number of one-dimensional vectors after flattening, and 768 is the dimension of each one-dimensional vector.
[0071] The linear transformation method described in step S3 is as follows: For each flattened high-dimensional vector space feature x, a linear embedding layer is used. p Perform a transformation to obtain the transformed feature y. The formula for the linear transformation is: E is the weight matrix, and b is the bias term. It is the i-th block, y i It is the transformed feature, y i ∈y, i={1,2,..,N}; a special classification label v is added to the transformed feature y. class The embedded feature k is obtained.
[0072] The location encoding method described in step S3 is as follows: Define a location embedding matrix E that matches the number of image blocks. pos Its dimension is (N+1, embed_dim), where embed_dim is the embedded spatial feature x. p The dimension is then determined, and the position embedding matrix is initialized using a truncated normal distribution. The position embedding matrix E is then... pos Add it to the corresponding embedded feature k.
[0073] Position embedding matrix E pos It is a feature x in a high-dimensional vector space. pA vector table with the same embedding dimension (i.e., 196) is used to provide the model with information about the location of blocks in the input samples. The purpose is that by adding the location encoding to the block embedding, the model can learn the spatial relationships between different locations in the sample image, so that the model can distinguish the spatial locations of different blocks.
[0074] At the beginning of the labeled embedding block sequence Z0, ViT (Vision Transformer) adds a special classification tag v. class This label, after passing through the Transformer encoder, will contain the classification information of the entire image. Finally, the labeled embedding block sequence Z0 is obtained. The formula for obtaining the embedding block sequence in step S3 is:
[0075]
[0076] Among them, v class E is a special classification marker. pos P represents the position embedding matrix. 2 The block size is P×P, which is 16×16 here. C represents the number of image channels (rgb3 channels), C=3. D represents the dimension of the embedding vector, which is the length of each image block, classification label, and location embedding matrix (768). N represents the number of blocks (196).
[0077] S4: Encode the embedded block sequence using the VisionTransformer module to obtain the final result sequence. Use the final result sequence and the VisionTransformer module to predict the image category label and obtain the prediction result. Validate the result using the validation set.
[0078] The method for encoding the embedded block sequence using the VisionTransformer module in step S4 is as follows: The labeled embedded block sequence Z0 is fed into the Transformer encoder. The labeled embedded block sequence Z0 passes through L encoding layers. Each encoding layer consists of two key components: a convolutional block attention module (CBAM) and a fully connected feedforward neural network module. The input of the l-th encoding layer is the output of the (l-1)-th encoding layer. In the l-th encoding layer, the result sequence Z0 of the previous encoding layer... l-1 After normalization layer I, and then through channel attention and spatial attention calculations in the Convolutional Block Attention Module (CBAM), the intermediate variable Z of the transformer coding layer after passing through the CBAM is obtained. l Then, after normalization layer II, the feedforward neural network is used to calculate the result Z of the transformer coding layer. lThe Transformer encoder uses multiple stacked Transformer coding layers to obtain the final result sequence Z. L , used for subsequent classification.
[0079] The CBAM convolutional block attention module consists of four parts: input features, channel attention module, spatial attention module, and output features. Figure 2 As shown. Assume the input feature map after normalization layer I is F∈R. C×H×W Where C represents the number of channels, and H and W represent the height and width of the feature map, respectively. Based on the processing flow of the Convolutional Block Attention Module (CBAM), two important attention maps can be derived sequentially: the one-dimensional channel attention map M... C And two-dimensional spatial attention diagram M S .
[0080] The processing flow of the Convolutional Block Attention (CBAM) module is as follows:
[0081] After processing by the channel attention module, the channel dimension of the input feature map F remains unchanged, while the spatial dimension is compressed.
[0082] The method for calculating channel attention is as follows: the result sequence Z of the previous coding layer l-1 After normalization layer I, the input feature map F∈R is obtained. C×H×WThe input feature map F is first transformed into two feature maps F1 and F2 of size C×H×W through two parallel operations: max pooling and average pooling. Feature maps F1 and F2 are then fed into the shared multilayer perceptron module of the (Shared MLP). In the shared multilayer perceptron module of the (Shared MLP), the number of channels of feature maps F1 and F2 is first compressed to 1 / R times the original number. The compression operation is achieved through 1×1 convolutional layers. This convolutional layer does not change the spatial size of the feature map, that is, H and W remain unchanged, but it can effectively reduce the number of channels. Specifically, two 1x1 convolutional layers are applied to feature maps F1 and F2, respectively. Their output channel count is set to 1 / R of the original channel count C, where R is the reduction rate. The reduction rate R is a hyperparameter used to reduce computational complexity and prevent overfitting; it is selected based on the specific task and network structure. In this embodiment, it is set to 16. Then, each feature map passes through a ReLU activation function to increase the model's non-linearity. Next, the channel count is expanded back to its original size C to match the channel dimension of the original feature maps. This expansion step is also implemented through 1x1 convolutional layers, but this time the output channel count is set to the original C. After processing by a shared multilayer perceptron (Shared MLP), two feature maps F1 and F2 of size C×1×1 are obtained. 11 and feature map F 22 The feature map F 11 and feature map F 22 Element-wise addition is performed, and the sum is constrained to between 0 and 1 by a Sigmoid activation function to obtain the channel attention feature map Mc(F). The formula for calculating the channel attention feature map is as follows:
[0083] MC(F)=σ(MLP(AvgPool(F))+MLP(MaxPool(F)));
[0084] Finally, the input feature map F is fused with the channel attention feature map Mc(F) to obtain the channel attention fused feature map F′. The formula for calculating the channel attention fused feature map is:
[0085]
[0086] Each element of this channel attention map Mc(F) has a value between 0 and 1, representing the importance weight of the corresponding channel.
[0087] In this context, AvgPool() represents the average pooling operation, MaxPool represents the max pooling operation, MLP() represents the shared multilayer perceptron operation function, and σ() represents the sigmoid activation function. This indicates element-wise multiplication.
[0088] After processing by the spatial attention module, the spatial dimension of the channel attention fusion feature map F' remains unchanged, while the channel dimension is compressed.
[0089] The method for calculating spatial attention is as follows: First, the output of the channel attention module, the channel attention fusion feature map F', is processed through two parallel operations: max pooling and average pooling, to obtain two feature maps F'1 and F'2 of size 1×H×W. Feature map F'1 captures the maximum response at a spatial location, and feature map F'2 captures the average response at a spatial location, which helps the model understand the distribution of spatial information. Next, the concatenation operation is used to concatenate feature maps F'1 and F'2 along the channel dimension to form a feature map F' of size 2×H×W. 12 This concatenation operation allows the model to simultaneously consider the maximum and average information at spatial locations, thus providing a more comprehensive understanding of spatial attention. Then, a 7×7 convolutional layer is used to concatenate the feature map F'. 12 The convolutional layer is converted into a single-channel feature map with the same spatial dimension as the attention fusion feature map F'. This convolutional layer can effectively fuse spatial information. Finally, a spatial attention map Ms(F') is obtained through a sigmoid activation function.
[0090] M S (F′)=σ(f 7*7 ([AvgPool(F′);MaxPool(F′)]))
[0091] Among them, f 7*7 The symbol "7×7" indicates a convolution, and ";" indicates a concatenation operation. Finally, the channel attention fusion feature map F' and the spatial attention map Mc(F) are fused to obtain the output feature map of the convolutional block attention module (CBAM). The output feature map F″ of the Convolutional Block Attention Module (CBAM) is then passed through a residual connection to obtain the intermediate variable Z of the transformer coding layer after passing through the CBAM. l ′:
[0092] Z l =CBAM(LN(Z) l-1 ))+Z l-1 ,l=1…L,LN() represents the normalization operation.
[0093] Each element of the spatial attention map Mc(F′) has a value between 0 and 1, representing the importance weight of the corresponding spatial location.
[0094] The method for performing feedforward neural network calculations is as follows:
[0095] intermediate variable Z l First, the sequence passes through a normalization layer II, then is fed into the feedforward neural network module. The output then passes through a residual connection to obtain the result sequence Z after passing through a complete transformer encoding layer. l .
[0096] Z l =ffn(LN(Z) l ′))+Z l ′,l=1...L
[0097] ffn() represents the computation function of the feedforward neural network;
[0098] The method for predicting image category labels using the final result sequence and the VisionTransformer module in step S4 is as follows:
[0099] The final result sequence Z is obtained in the last transformer coding layer. L Extract the final result sequence Z L The first element in and elements The data is passed to a multilayer perceptron classifier (MLP Head) to predict the image's class label based on the features extracted by the Transformer encoder. The formula for predicting the image's class label is:
[0100]
[0101] Where mlp_head() represents the (MLP Head) multilayer perceptron classification function, and q is the class label.
[0102] The method for validation using a validation set is as follows: The error between the predicted result and the actual label is calculated using a loss function (cross-entropy loss). The model weights are updated based on the loss gradient, and the SGD optimizer is used to adjust the weights to minimize the loss. The learning rate is adjusted using cosine annealing based on the training progress. The model is run on the validation set without updating the weights; only the model performance is evaluated. Evaluation metrics (such as accuracy, recall, F1 score, etc.) are calculated. If the accuracy is higher than that of previous epochs, the model weights and optimizer state are saved so that training can be resumed after a training interruption or for subsequent evaluation.
[0103] The model training epochs were set to 100, with a learning rate of 0.001. The learning rate was dynamically adjusted using cosine annealing during training. The SGD optimizer was used to update the model parameters with a batch size of 32, momentum of 0.9, and weight decay of 5E-5.
[0104] S5: Repeat steps S2 to S4 until the model achieves optimal performance on the validation set, resulting in a trained CNN-CBAM-ViT model.
[0105] S6: Test the trained CNN-CBAM-ViT model using the test set to obtain the classification of the images in the test set.
[0106] Run the trained best model on the test set to evaluate its performance in real-world applications. Calculate evaluation metrics on the test set to understand the model's generalization ability.
[0107] Model performance evaluation:
[0108] To evaluate the model's performance, this embodiment of the invention uses a confusion matrix to analyze and compare the differences between the model's predictions and the actual labels. Four key metrics can be obtained from the confusion matrix: True Positive (TP), False Negative (FN), False Positive (FP), and True Negative (TN). TP represents the number of pornographic images correctly classified as pornographic, FN represents the number of pornographic images incorrectly classified as normal images, FP represents the number of normal images incorrectly classified as pornographic, and TN represents the number of normal images correctly classified as normal. Combining these four metrics, the following commonly used evaluation measures can be obtained: Accuracy, Precision, Recall, F1-Score, and Pornographic Image False Negative Rate (SIMR).
[0109]
[0110]
[0111]
[0112]
[0113]
[0114] The SIMR (Simultaneous Primary Detection Rate) reflects the proportion of pornographic images a model fails to identify; that is, the percentage of actual pornographic images that the model fails to correctly identify. Due to the potential harm of pornography to society, especially its impact on minors, the SIMR is a crucial evaluation metric. A model with a low SIMR means it can more accurately capture the majority of pornographic images, thus reducing the risk of false negatives.
[0115] During training, the loss and accuracy curves of the CNN-CBAM-ViT model are as follows: Figure 3 As shown, in the initial stage, the model had not yet adapted to the data features, resulting in a high loss and low accuracy. As training progressed from epoch 10 to 50, the loss decreased rapidly and fluctuated, while the accuracy significantly improved, indicating that the model was learning and adapting to the data. However, after epoch 50, the fluctuations in loss and accuracy gradually decreased, showing that the model had learned sufficient features and its performance stabilized. Ultimately, the loss stabilized at 0.085, and the accuracy reached 0.9694, exceeding 0.97 in some epochs, indicating that the model training had achieved good results.
[0116] To comprehensively verify the effectiveness of the model proposed in the embodiments of the invention in the task of pornographic image classification, a series of comparative experiments were also designed in the embodiments of the invention. In the embodiments of the invention, ResNet101 and VGGNet16 were selected as the main comparison objects. Table 1 shows the comparison with commonly used methods.
[0117] Table 1
[0118]
[0119] To further verify the effectiveness of each module in improving the performance of the CNN-CBAM-ViT model, this embodiment includes a module comparison ablation experiment. To verify the effectiveness of each module separately, the proposed CNN-CBAM-ViT model was processed by removing the Convolutional Neural Network (CNN) module and the Convolutional Block Attention (CBAM) module, respectively. Comparative experiments were conducted on the same dataset, and the experimental results are shown in Table 2. Figure 4 As shown.
[0120] Table 2
[0121]
[0122] The Convolutional Neural Network (CNN) module and the Convolutional Block Attention (CBAM) module in the CNN-CBAM-ViT model proposed in this invention can effectively improve the model's classification ability, and the model performance reaches its optimal state when both modules are present. Specifically, the CNN module is responsible for extracting local features of the image and fusing them with the original image to form richer shallow features. The Convolutional Block Attention mechanism, by combining channel attention and spatial attention, can more accurately capture key information in the image, thereby improving the model's performance.
[0123] The CNN-CBAM-ViT model proposed in this invention is the first to introduce the Vision Transformer into the field of pornographic image recognition, filling the gap in the application of Vision Transformer in pornographic image recognition tasks. Experimental results show that the proposed CNN-CBAM-ViT model performs excellently in pornographic image recognition tasks, achieving a classification accuracy of 97.05% and a false negative rate of only 1.84% for pornographic images. It effectively identifies pornographic images, significantly outperforming existing methods. This ability to effectively identify pornographic images provides strong technical support for maintaining the health and security of cyberspace.
[0124] This invention first utilizes a convolutional neural network (CNN) to extract local features from the original image. These extracted features are then fused with features from the original image to form a richer feature representation. Next, these feature representations are fed into a modified Vision Transformer (CBAM). By combining channel and spatial attention, CBAM efficiently focuses on key features in the image, thereby enhancing the model's representational power. Combined with the stacked multi-layer structure and parameter learning capabilities of the Vision Transformer, this model can further improve the performance in recognizing pornographic images.
[0125] The above description is only a preferred embodiment of the present invention and is not intended to limit the present invention. Any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of the present invention should be included within the protection scope of the present invention.
Claims
1. A method for pornographic image recognition based on CNN-Transformer cascade structure and CBAM mechanism, characterized in that, Including the following steps: S0: Construct the CNN-CBAM-ViT model, which includes a CNN convolutional neural network module and a VisionTransformer module connected in sequence; S1: Obtain the experimental image dataset, including normal images and pornographic images, divide the experimental image dataset into training set, validation set and test set, use the training set and validation set as training samples, perform data preprocessing on the training samples to obtain input samples; S2: Use the CNN convolutional neural network module to extract features from the input samples, obtain an initial feature map, and fuse the initial feature map with the input samples to obtain a fused feature map u; S3: Perform image block processing, linear transformation and position encoding on the fused feature map u in sequence to obtain the embedded block sequence; S4: Encode the embedded block sequence using the VisionTransformer module to obtain the final result sequence. Use the final result sequence and the VisionTransformer module to predict the image category label and obtain the prediction result. Validate the result using the validation set. S5: Repeat steps S2 to S4 until the CNN-CBAM-ViT model achieves optimal performance on the validation set, thus obtaining the trained CNN-CBAM-ViT model; S6: Test the trained CNN-CBAM-ViT model using the test set to obtain the classification of the images in the test set.
2. The pornographic image recognition method based on CNN-Transformer cascade structure and CBAM mechanism according to claim 1, characterized in that, The Vision Transformer module described in step S0 includes a linear embedding layer, a Transformer encoder, and a multilayer perceptron classifier connected in sequence. The Transformer encoder includes a convolutional block attention module and a feedforward neural network module connected in sequence.
3. The pornographic image recognition method based on CNN-Transformer cascade structure and CBAM mechanism according to claim 1 or 2, characterized in that, The specific method for obtaining the input sample in step S1 is as follows: Obtain an experimental image dataset containing a few normal images and a majority of pornographic images. Divide the experimental image dataset into a training set, a validation set, and a test set in a 6:2:2 ratio. Use the preprocessing module in the PyTorch framework to perform horizontal flipping, random cropping, and standardization to obtain the input sample x∈R. C×H×W C is the number of image channels, H is the image height, and W is the image width.
4. The pornographic image recognition method based on CNN-Transformer cascade structure and CBAM mechanism according to claim 3, characterized in that, Step S2, obtaining the fused feature map u, specifically involves: inputting the input sample x into the CNN convolutional neural network module. The input sample x passes through a convolutional layer, a ReLU activation function layer, a max pooling layer, and a transposed convolutional layer to obtain the initial feature map x1. The process of obtaining the initial feature map x1 is as follows: x1=ConvTranspose(MaxPool(ReLU(Conv(x)))); Where Conv is the convolution function, ReLU is the ReLU activation function, and MaxPool is the max pooling operation. ConvTranspose is a function for transposed convolutional layers; The initial feature map x1 is then fused with the input sample x to obtain the fused feature map u = x1⊕x.
5. The pornographic image recognition method based on CNN-Transformer cascade structure and CBAM mechanism according to claim 4, characterized in that, The image block processing method described in step S3 is as follows: First, the fused feature map u is divided into N blocks using a linear embedding layer. Then, each block is flattened into a one-dimensional vector, ultimately obtaining a high-dimensional vector space feature x. p ; The linear transformation method described in step S3 is as follows: For each flattened high-dimensional vector space feature x, a linear embedding layer is used. p Perform a transformation to obtain the transformed feature y. The formula for the linear transformation is: E is the weight matrix, and b is the bias term. The i-th block, y i It is the transformed feature, y i ∈y, i={1,2,..,N}; a special classification label v is added to the transformed feature y. class Obtain the embedded feature k, The location encoding method described in step S3 is as follows: Define a location embedding matrix E that matches the number of image blocks. pos Its dimension is (N+1, embed_dim), where embed_dim is the embedded spatial feature x. p The dimension is then determined, and the position embedding matrix is initialized using a truncated normal distribution. The position embedding matrix E is then... pos Add it to the corresponding embedded feature k.
6. The pornographic image recognition method based on CNN-Transformer cascade structure and CBAM mechanism according to claim 5, characterized in that, The formula for obtaining the embedded block sequence in step S3 is as follows: Where Z0 represents the embedded block sequence, v class E is a special classification marker. pos P represents the position embedding matrix. 2 The block size is P×P, and D represents the dimension of the embedding vector.
7. The pornographic image recognition method based on CNN-Transformer cascade structure and CBAM mechanism according to claim 6, characterized in that, Step S4 describes the method of encoding the embedded block sequence using the VisionTransformer module as follows: The embedded block sequence Z0 is input to the Transformer encoder. The embedded block sequence Z0 passes through L encoding layers. The input of the l-th encoding layer is the output of the (l-1)-th encoding layer. In the l-th encoding layer, the result sequence Z of the previous encoding layer... l-1 After normalization layer I, and then through channel attention and spatial attention calculations in the convolutional block attention module, the intermediate variable Z of the transformer coding layer after passing through the convolutional block attention module is obtained. l Then, after normalization layer II, a feedforward neural network is performed to obtain the result sequence Z of the transformer coding layer. l .
8. The pornographic image recognition method based on CNN-Transformer cascade structure and CBAM mechanism according to claim 7, characterized in that, The method for calculating channel attention is as follows: the result sequence Z of the previous coding layer l-1 After normalization layer I, the input feature map F∈R is obtained. C×H×W The input feature map F is first transformed into two feature maps F1 and F2 of size C×H×W through two parallel operations: max pooling and average pooling. Feature maps F1 and F2 are then fed into a shared multilayer perceptron module, where they undergo compression, ReLU activation function, and dilation operations to obtain two feature maps F11 and F2 of size C×1×1. 22 , feature map F 11 and feature map F 22 Element-wise summation, followed by passing through a Sigmoid activation function to obtain the channel attention feature map: M C (F)<σ(MLP(AvgPool(F))+MLP(MaxPool(F))); The input feature map F is fused with the channel attention feature map Mc(F) to obtain the channel attention fused feature map: In this context, AvgPool() represents the average pooling operation, MaxPool represents the max pooling operation, MLP() represents the operation function for the shared multilayer perceptron module, and σ() represents the sigmoid activation function. This indicates element-wise multiplication.
9. The pornographic image recognition method based on CNN-Transformer cascade structure and CBAM mechanism according to claim 8, characterized in that, The method for spatial attention calculation is as follows: First, the output of the channel attention module, the channel attention fusion feature map F', is processed through two parallel operations, a max pooling layer and an average pooling layer, to obtain two feature maps F'1 and F'2 of size 1×H×W. Next, a concatenation operation is used to concatenate feature maps F'1 and F'2 along the channel dimension to form a feature map F' of size 2×H×W. 12 Then, the feature map F' is processed through a 7×7 convolutional layer. 12 The feature map is converted into a single-channel feature map with the same spatial dimension as the attention fusion feature map F'. Finally, a sigmoid activation function is applied to obtain the spatial attention map. M S (F′)=σ(f 7*7 ([AvgPool(F′);MaxPool(F′)])); Among them, f 7*7 This represents a 7×7 convolution, and ";" indicates a concatenation operation. The channel attention fusion feature map F' is fused with the spatial attention map Mc(F) to obtain the output feature map of the convolutional block attention module. The output feature map F″ of the convolutional block attention module is then passed through a residual connection to obtain the intermediate variable after the transformer coding layer passes through the convolutional block attention module: WITH l ′=CBAM(LN(Z l-1 ))+Z l-1 ,l=1…L; LN() represents the normalization operation, and CBAM() represents the operation function of the convolutional block attention module.
10. The pornographic image recognition method based on CNN-Transformer cascade structure and CBAM mechanism according to claim 9, characterized in that, The method for performing feedforward neural network calculations is as follows: The intermediate variable Z... l First, the signal passes through normalization layer II, then is fed into the feedforward neural network module. The output then passes through a residual connection to obtain the result sequence after a complete transformer encoding layer. FROM l =ffn(LN(Z′ l ))+Z′ l ,l=1...L; ffn() represents the computation function of the feedforward neural network, which yields the final sequence Z in the last transformer encoding layer. L Step S4 describes the method for predicting image category labels using the final result sequence and the VisionTransformer module: Extract the final result sequence Z... L The first element This data is then passed to a multilayer perceptron classifier, which predicts the image's class label based on the features extracted by the Transformer encoder. The formula for predicting the image's class label is as follows: mlp_head() represents the multilayer perceptron classification function, where q is the class label; The method for validation using the validation set in step S4 is as follows: run the model on the validation set, use the cross-entropy loss function to calculate the error between the predicted result and the actual label, update the model weights using the SGD optimizer according to the loss gradient, adjust the learning rate using the cosine annealing algorithm according to the training progress, calculate the evaluation index, and if the accuracy is higher than the accuracy of previous rounds, save the model weights and optimizer state.
Citation Information
Patent Citations
Pornographic image recognition method based on convolutional neural network
CN111259823A
