Adversarial sample generation method and device based on filter and attention constraint
Through a method based on filters and attention constraints, the filter function is adjusted to generate adversarial samples, which solves the problem of low imperceptibility of traditional adversarial attack samples and improves the effectiveness of adversarial attacks.
Patent Information
- Application Number
- CN202410750310.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-06-12
- Publication Date
- 2024-09-10
AI Technical Summary
Traditional adversarial attack samples have low imperceptibility, cannot implement effective adversarial attacks, and cannot effectively deceive the classifier of the deep learning model.
Using a method based on filters and attention constraints, by obtaining the initial spectrum of the original image, performing Fourier transform and inverse discrete Fourier transform, combined with the preset inverse attention matrix and Adam optimization algorithm, the filter function is adjusted to generate Adversarial samples concentrate disturbances in key areas and improve the imperceptibility of adversarial samples.
It significantly improves the imperceptibility of adversarial samples, supports effective adversarial attacks, and improves the success rate and perceived quality of adversarial attacks.
Smart Images

Figure CN118628812A
Abstract
Description
Technical Field
[0001] The present application relates to the field of computer technology, and in particular to a method, apparatus, computer device, storage medium and computer program product for generating adversarial samples based on filters and attention constraints. Background Art
[0002] With the success of deep learning in computer vision tasks, more and more researchers have begun to study the defects of mainstream deep learning models such as CNN (Convolutional Neural Network) and ViT (Vision Transformer, a Transformer-based neural network architecture) to improve the security of such models in real-world applications.
[0003] Researchers have found that by adding subtle perturbations to the original image, adversarial samples can be constructed. From the perspective of the human visual system, these adversarial samples are very similar to the original images, but can deceive the classifier. This method of constructing adversarial samples to deceive the classifier is called adversarial attack.
[0004] Adversarial attacks can reveal the security weaknesses of models, promote the development of defense strategies, and enhance network security awareness. The generation of adversarial attack samples is a very important part of adversarial attacks. However, traditional adversarial attack samples are less perceptible and cannot support effective adversarial attacks. Summary of the invention
[0005] Based on this, it is necessary to provide a method, device, computer equipment, storage medium and computer program product for generating adversarial samples based on filters and attention constraints that can support effective adversarial attacks in response to the above technical problems.
[0006] In a first aspect, the present application provides a method for generating adversarial samples based on filters and attention constraints. The method comprises:
[0007] Obtain an initial spectrum of the original image, multiply the initial spectrum by the filter function to obtain a modified spectrum, and perform an IDFT (Inverse Discrete Fourier Transform) on the modified spectrum to obtain a corresponding initial adversarial sample;
[0008] Sending the initial adversarial sample into the classifier to obtain adversarial loss;
[0009] Applying a preset anti-attention matrix to the original image and the initial adversarial sample to obtain non-critical areas of the original image and the initial adversarial sample respectively, and calculating the difference between the two non-critical areas to obtain attention loss;
[0010] Performing weighted calculation on the adversarial loss and the attention loss to obtain a final loss;
[0011] Based on the final loss and the initial adversarial sample, an Adam optimization algorithm is used to minimize an objective function to adjust a filter function, wherein the objective function is used to characterize the distance between the adversarial sample and the target label;
[0012] An adversarial sample is generated according to the adjusted filter function and the initial spectrum.
[0013] In one embodiment, obtaining the initial spectrum of the original image includes:
[0014] The original image is converted from the spatial domain to the frequency domain using DFT (Discrete Fourier Transform) to obtain the initial spectrum.
[0015] In one embodiment, the applying a preset anti-attention matrix to the original image and the initial adversarial sample to obtain non-critical areas of the original image and the initial adversarial sample, respectively, and calculating the difference between the two non-critical areas to obtain the attention loss, further includes:
[0016] Input the original image into the ViT model to obtain the original attention weight matrix;
[0017] According to the original attention weight matrix, a target attention weight matrix is obtained by using a bilinear difference algorithm;
[0018] The target attention weight matrix is truncated based on a preset truncation frequency to obtain a preset anti-attention matrix.
[0019] In one embodiment, obtaining a target attention weight matrix based on the original attention weight matrix and using a bilinear difference algorithm includes:
[0020] Averaging all heads of each layer in the original attention weight matrix to obtain a first matrix;
[0021] Add the first matrix to the identity matrix and perform residual connection to obtain a second matrix;
[0022] recursively multiplying the second matrices to obtain a recursive result;
[0023] Filtering the recursive result and performing a normalization operation to obtain a normalized matrix;
[0024] The size of the normalized matrix is adjusted based on the size of the Patch in the ViT model, and the target attention weight matrix is obtained using a bilinear interpolation algorithm.
[0025] In one embodiment, the formula of the truncation method is:
[0026]
[0027] Wherein, cutoff is the preset cutoff frequency, cutoff∈[0,1].
[0028] In one embodiment, minimizing the objective function based on the final loss and the initial adversarial sample using the Adam optimization algorithm to adjust the filter function includes:
[0029] Defining an objective function based on the final loss, the initial adversarial sample, and the filter function;
[0030] The filter function is adjusted, and the modified spectrum obtained by multiplying the initial spectrum by the filter function is returned, and the modified spectrum is subjected to IDFT to obtain the corresponding initial adversarial sample. The step is iterated a preset number of times, and the Adam optimization algorithm is used to minimize the objective function during the iteration process.
[0031] In a second aspect, the present application also provides an adversarial sample generation device based on filters and attention constraints. The device comprises:
[0032] An initial sample generation module is used to obtain an initial spectrum of the original image, and multiply the initial spectrum by the filter function to obtain a modified spectrum, and obtain a corresponding initial adversarial sample by performing IDFT on the modified spectrum;
[0033] An adversarial loss module, used for sending the initial adversarial sample into a classifier to obtain an adversarial loss;
[0034] An attention loss module, used for applying a preset anti-attention matrix to the original image and the initial adversarial sample, obtaining non-critical areas of the original image and the initial adversarial sample respectively, and calculating the difference between the two non-critical areas to obtain attention loss;
[0035] A loss weighted calculation module, used to perform weighted calculation on the adversarial loss and the attention loss to obtain a final loss;
[0036] A filter adjustment module, configured to adjust a filter function by minimizing an objective function based on the final loss and the initial adversarial sample using an Adam optimization algorithm, wherein the objective function is used to characterize the distance between the adversarial sample and the target label;
[0037] The adversarial sample generation module is used to generate an adversarial sample according to the adjusted filter function and the initial spectrum.
[0038] In a third aspect, the present application further provides a computer device. The computer device includes a memory and a processor, the memory stores a computer program, and the processor implements the following steps when executing the computer program:
[0039] Obtain an initial spectrum of the original image, multiply the initial spectrum by the filter function to obtain a modified spectrum, and perform IDFT on the modified spectrum to obtain a corresponding initial adversarial sample;
[0040] Sending the initial adversarial sample into the classifier to obtain adversarial loss;
[0041] Applying a preset anti-attention matrix to the original image and the initial adversarial sample to obtain non-critical areas of the original image and the initial adversarial sample respectively, and calculating the difference between the two non-critical areas to obtain attention loss;
[0042] Performing weighted calculation on the adversarial loss and the attention loss to obtain a final loss;
[0043] Based on the final loss and the initial adversarial sample, an Adam optimization algorithm is used to minimize an objective function to adjust a filter function, wherein the objective function is used to characterize the distance between the adversarial sample and the target label;
[0044] An adversarial sample is generated according to the adjusted filter function and the initial spectrum.
[0045] In a fourth aspect, the present application further provides a computer-readable storage medium. The computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the following steps are implemented:
[0046] Obtain an initial spectrum of the original image, multiply the initial spectrum by the filter function to obtain a modified spectrum, and perform IDFT on the modified spectrum to obtain a corresponding initial adversarial sample;
[0047] Sending the initial adversarial sample into the classifier to obtain adversarial loss;
[0048] Applying a preset anti-attention matrix to the original image and the initial adversarial sample to obtain non-critical areas of the original image and the initial adversarial sample respectively, and calculating the difference between the two non-critical areas to obtain attention loss;
[0049] Performing weighted calculation on the adversarial loss and the attention loss to obtain a final loss;
[0050] Based on the final loss and the initial adversarial sample, an Adam optimization algorithm is used to minimize an objective function to adjust a filter function, wherein the objective function is used to characterize the distance between the adversarial sample and the target label;
[0051] An adversarial sample is generated according to the adjusted filter function and the initial spectrum.
[0052] In a fifth aspect, the present application further provides a computer program product. The computer program product includes a computer program, and when the computer program is executed by a processor, the following steps are implemented:
[0053] Obtain an initial spectrum of the original image, multiply the initial spectrum by the filter function to obtain a modified spectrum, and perform IDFT on the modified spectrum to obtain a corresponding initial adversarial sample;
[0054] Sending the initial adversarial sample into the classifier to obtain adversarial loss;
[0055] Applying a preset anti-attention matrix to the original image and the initial adversarial sample to obtain non-critical areas of the original image and the initial adversarial sample respectively, and calculating the difference between the two non-critical areas to obtain attention loss;
[0056] Performing weighted calculation on the adversarial loss and the attention loss to obtain a final loss;
[0057] Based on the final loss and the initial adversarial sample, an Adam optimization algorithm is used to minimize an objective function to adjust a filter function, wherein the objective function is used to characterize the distance between the adversarial sample and the target label;
[0058] An adversarial sample is generated according to the adjusted filter function and the initial spectrum.
[0059] The above-mentioned adversarial sample generation method, device, computer equipment, storage medium and computer program product based on filter and attention constraints obtain the initial spectrum of the original image, and multiply the initial spectrum with the filter function to obtain the modified spectrum, and obtain the corresponding initial adversarial sample through IDFT; send the initial adversarial sample to the classifier to obtain the adversarial loss; apply the preset anti-attention matrix to the original image and the initial adversarial sample to obtain the non-critical areas of the original image and the initial adversarial sample respectively, and calculate the difference between the two non-critical areas to obtain the attention loss; perform weighted calculation on the adversarial loss and the attention loss to obtain the final loss; based on the final loss and the initial adversarial sample, the Adam optimization algorithm is used to minimize the objective function to adjust the filter function; generate adversarial samples according to the adjusted filter function and the initial spectrum. In the whole process, attention is paid to the addition of disturbances in non-critical areas, and the Adam optimization algorithm is used to minimize the objective function to adjust the filter function, so that the disturbances are concentrated in the critical area, which can significantly improve the imperceptibility of adversarial samples, thereby supporting effective adversarial attacks. BRIEF DESCRIPTION OF THE DRAWINGS
[0060] Figure 1 FIG. 1 is an application environment diagram of an adversarial sample generation method based on filters and attention constraints in one embodiment;
[0061] Figure 2 A flowchart of a method for generating adversarial samples based on filters and attention constraints in one embodiment;
[0062] Figure 3 A flowchart of a method for generating adversarial samples based on filters and attention constraints in another embodiment;
[0063] Figure 4 This is a schematic diagram of the original architecture of the ViT model;
[0064] Figure 5 is the attention heat map of the ViT model on the current image;
[0065] Figure 6 is a structural block diagram of an adversarial sample generation device based on filters and attention constraints in one embodiment;
[0066] Figure 7 FIG. 4 is a diagram showing the internal structure of a computer device in one embodiment. DETAILED DESCRIPTION
[0067] In order to make the purpose, technical solution and advantages of the present application more clearly understood, the present application is further described in detail below in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present application and are not used to limit the present application.
[0068] The adversarial sample generation method based on filters and attention constraints provided in the embodiments of the present application can be applied to Figure 1 In the application environment shown. Among them, the terminal 102 communicates with the server 104 through the network. The data storage system can store the data that the server 104 needs to process. The data storage system can be integrated on the server 104, or it can be placed on the cloud or other network servers. The terminal 102 sends an adversarial sample generation request to the server 104, and the server 104 responds to the request, obtains the initial spectrum of the original image, and multiplies the initial spectrum with the filter function to obtain the modified spectrum, and obtains the corresponding initial adversarial sample through IDFT after the modified spectrum; the initial adversarial sample is sent to the classifier to obtain the adversarial loss; the preset anti-attention matrix is applied to the original image and the initial adversarial sample, and the non-critical areas of the original image and the initial adversarial sample are obtained respectively, and the difference between the two non-critical areas is calculated to obtain the attention loss; the adversarial loss and the attention loss are weighted to obtain the final loss; based on the final loss, the initial adversarial sample, the Adam optimization algorithm is used to minimize the objective function to adjust the filter function; the adversarial sample is generated according to the adjusted filter function and the initial spectrum. Further, the server 104 can feed back the generated adversarial sample to the terminal 102. The terminal 102 may be, but is not limited to, various personal computers, laptops, smart phones, tablet computers, IoT devices, and portable wearable devices. The IoT devices may be smart speakers, smart TVs, smart air conditioners, smart car-mounted devices, etc. The portable wearable devices may be smart watches, smart bracelets, head-mounted devices, etc. The server 104 may be implemented as an independent server or a server cluster consisting of multiple servers.
[0069] In one embodiment, Figure 2 As shown in the figure, a method for generating adversarial samples based on filters and attention constraints is provided. Figure 1 Taking the server 104 in the example as an example, the following steps are included:
[0070] S100: Obtain an initial spectrum of the original image, multiply the initial spectrum by the filter function to obtain a modified spectrum, and perform IDFT on the modified spectrum to obtain a corresponding initial adversarial sample.
[0071] Specifically, the original image can be converted from the spatial domain to the frequency domain to obtain the initial spectrum S of the original image. The initial spectrum S is multiplied by the filter function f to obtain the modified spectrum S′, and then the modified spectrum S′ is subjected to IDFT to obtain the corresponding initial adversarial sample x′=IDFT(S×f).
[0072] S200: Send the initial adversarial sample into the classifier to obtain the adversarial loss.
[0073] The generated initial adversarial sample is sent to the classifier M to obtain the adversarial loss loss1 = M(x′).
[0074] S300: Apply the preset anti-attention matrix to the original image and the initial adversarial sample to obtain the non-critical areas of the original image and the initial adversarial sample respectively, and calculate the difference between the two non-critical areas to obtain the attention loss.
[0075] The preset inverse attention matrix IAM (InverseAttentionMap, inverse of AM) is a pre-set matrix, which specifically refers to the inverse of AM (AttentionMap, the attention matrix of the image), and AM refers to the attention matrix of the image calculated using ViT (VisionTransformer, the Transformer model in the field of computer vision), whose element values are between [0, 1]. The larger the value, the more important the corresponding area of the image. Apply the preset inverse attention matrix to the original image x and the adversarial sample x′ to obtain their non-critical areas respectively, and calculate the difference between the non-critical areas of the two to obtain the attention loss loss2 = IAM (x′) - IAM (x). Specifically, here we do not pay attention to the specific content of the non-critical area, and directly obtain the "gap" between the adversarial sample and the original sample in the non-critical area by making a difference, that is, a loss term loss2. By minimizing this gap as much as possible, the perturbation of the adversarial sample in the non-critical area is as small as possible (close to the original sample), so that the perturbation is concentrated in the critical area.
[0076] S400: Perform weighted calculation on the adversarial loss and the attention loss to obtain the final loss.
[0077] The weighted sum of the two losses is added to get the final loss L adv =α·loss1+(1-α)loss2, where α∈[0,1] is the weight coefficient used to balance the adversarial loss and attention loss.
[0078] S500: Based on the final loss and the initial adversarial sample, the Adam optimization algorithm is used to minimize the objective function to adjust the filter function. The objective function is used to characterize the distance between the adversarial sample and the target label.
[0079] Based on the final loss and the initial adversarial sample, an objective function L containing a filter function is generated. adv (x′,y t )where x′=IDFT(DFT(x)×f), Adam optimization algorithm is used to minimize the objective function min f L adv (x′,y t)where x′=IDFT(DFT(x)×f), the filter function is adjusted iteratively during the optimization process to obtain the minimum value of the objective function. The objective function is used to characterize the distance between the adversarial sample and the target label. t is the target label. In the field of image adversarial attacks, the target label refers to the misclassification that the attacker wants the classifier to make.
[0080] Specifically, the above S500 includes two parts. First, the objective function is defined based on the final loss, the initial adversarial sample and the filter function; then the filter function is adjusted, and the initial spectrum is multiplied by the filter function to obtain the modified spectrum, and the modified spectrum is subjected to IDFT to obtain the corresponding initial adversarial sample. The step is iterated for a preset number of times, and the Adam optimization algorithm is used to minimize the objective function during the iteration process. The preset number here is pre-set, and it can be adjusted according to the needs of the actual situation.
[0081] S600: Generate an adversarial sample according to the adjusted filter function and the initial spectrum.
[0082] According to the filter function finally adjusted by S500 and the initial spectrum S, the final adversarial sample is obtained by multiplying the two.
[0083] The above-mentioned adversarial sample generation method based on filters and attention constraints obtains the initial spectrum of the original image, and multiplies the initial spectrum with the filter function to obtain the modified spectrum, and obtains the corresponding initial adversarial sample through IDFT; the initial adversarial sample is sent to the classifier to obtain the adversarial loss; the preset anti-attention matrix is applied to the original image and the initial adversarial sample to obtain the non-critical areas of the original image and the initial adversarial sample respectively, and the difference between the two non-critical areas is calculated to obtain the attention loss; the adversarial loss and the attention loss are weighted to obtain the final loss; based on the final loss and the initial adversarial sample, the Adam optimization algorithm is used to minimize the objective function to adjust the filter function; the adversarial sample is generated according to the adjusted filter function and the initial spectrum. In the whole process, attention is paid to the addition of disturbances in non-critical areas, and the Adam optimization algorithm is used to minimize the objective function to adjust the filter function so that the disturbances are concentrated in the critical area, which can significantly improve the imperceptibility of the adversarial sample, thereby supporting effective adversarial attacks.
[0084] In one embodiment, obtaining the initial spectrum of the original image includes: converting the original image from the spatial domain to the frequency domain using DFT transformation to obtain the initial spectrum.
[0085] Specifically, DFT (Discrete Fourier Transform) may be used to transform the image from the spatial domain to the frequency domain to obtain a spectrum S=DFT(x).
[0086] like Figure 3 As shown, in one embodiment, before S300, the process further includes:
[0087] S220: Input the original image into the ViT model to obtain the original attention weight matrix.
[0088] The ViT model is a deep learning model proposed by the Google team. It is the first to introduce the Transformer model structure, which has achieved great success in the field of natural language processing (NLP), into the field of computer vision (CV). The ViT model can be used as follows: Figure 4 The original architecture shown in Figure 1 is as follows. Let B represent the number of images, L represent the number of layers of ViT, H represent the number of heads in each layer, and P represent the dimension of each token. The original image is input into ViT for inference to obtain the attention weight matrix AWM (Attention Weight Matrix), whose dimension is [L, B, H, P, P].
[0089] S240: According to the original attention weight matrix, a target attention weight matrix is obtained by using a bilinear difference algorithm.
[0090] Based on the original attention weight matrix, it is preprocessed by averaging, residual connection, recursion, normalization, size cropping, etc., and then the target attention weight matrix is obtained by using the bilinear difference algorithm.
[0091] S260: Truncate the target attention weight matrix based on a preset truncation frequency to obtain a preset anti-attention matrix.
[0092] The preset truncation frequency is pre-set and can be adjusted according to actual conditions. The target attention weight matrix is truncated based on the preset truncation frequency to obtain a preset anti-attention matrix. Specifically, the formula of the truncation method is:
[0093]
[0094] Where cutoff is the preset cutoff frequency, cutoff∈[0,1].
[0095] In one embodiment, obtaining a target attention weight matrix using a bilinear difference algorithm based on an original attention weight matrix includes:
[0096] The first matrix is obtained by averaging all heads of each layer in the original attention weight matrix; the first matrix is added to the unit matrix to perform residual connection to obtain the second matrix; the second matrix is recursively multiplied to obtain the recursive result; the recursive result is filtered and normalized to obtain the normalized matrix; the size of the normalized matrix is adjusted based on the size of the patch in the ViT model, and the target attention weight matrix is obtained using the bilinear interpolation algorithm.
[0097] Specifically, the original attention weight matrix is averaged over all heads in each layer, and the dimension becomes [L, B, P, P]; [L, B, P, P] is added to the identity matrix to process the residual connection, and the dimension becomes [L, B, P, P]; starting from the first layer, the weight matrix of each layer is recursively multiplied, that is,
[0098] T[0]=AVM[0], T[i]=T[i-1]×AWM[i], 1≤i≤L
[0099] Take the last layer T[L-1] as the final result, select the dimension related to the current image and remove the token dimension used for classification, and normalize it to [B, P-1]. Resize the matrix according to the size of the patch in ViT and apply the bilinear interpolation algorithm to obtain the final attention matrix AM, whose dimension is [B, N, N], and the element value is in [0, 1]. The larger the element value, the more important the corresponding area is, which means that the area plays a greater role in image classification.
[0100] In order to illustrate in detail the effect of the adversarial sample generation method based on filters and attention constraints in this application, the following will be introduced in combination with actual experimental data.
[0101] The experiment adopts the commonly used conditions of each algorithm, the dataset is ImageNet-1k, the target model is Resnet50, the perturbation budget is 8 / 255, the number of iterations is 200 steps, and the batch size is 20; the hyperparameters of the algorithm of the present invention are set to α=0.3, cutoff=0.85, and the other comparison algorithms adopt the best hyperparameter settings provided by each. Figure 5 This is the ViT model's attention heat map for the current image. The darker the color, the more important the area.
[0102] Table 1 shows the comparison data of the algorithm in this application and other algorithms in terms of perceived quality under experimental conditions.
[0103] algorithm ASR↑ L2↓ Linf↓ SSIM↑ LPIPS↓ PSNR↑ FID↓ ours 99.12% 0.65 0.0228 0.998 0.0056 54.59 0.17 PGD 100% 59.20 0.0314 0.877 0.1542 34.05 7.14 PerC-AL 100% 0.96 0.0516 0.997 0.0134 52.80 0.32 SSAH 97.36% 1.75 0.0118 0.997 0.0085 49.91 0.39 AdvDrop 99.65% 12.44 0.0596 0.982 0.0344 41.43 1.04
[0104] The experimental data in Table 1 above are as follows: ASR is the attack success rate (Attack Success Rate). The arrow pointing upwards indicates that the larger the index is, the better, and the arrow pointing downwards is the opposite. It can be seen from the table that the attack success rate of the algorithm proposed in this application is basically the same as that of other comparison algorithms, while each perceptual quality index is better than other comparison algorithms. Explanation of other perceptual quality indicators: L2: The L2 norm of the original sample and the adversarial sample, that is, the Euclidean distance, indicates the overall size of the perturbation, the smaller the better. Linf: The infinite norm of the original sample and the adversarial sample, indicating the maximum perturbation size of each pixel in the image. SSIM (Structural Similarity Index): Structural similarity index, ranging from [0,1]. The larger the value, the more similar the images are. LPIPS (Learned Perceptual Image Patch Similarity): Perceptual image similarity index, the smaller the better.
[0105] PSNR (Peak Signal-to-Noise Ratio): Peak signal-to-noise ratio, more than 40 is the benchmark, the larger the better. PSNR is close to 50dB, which means that the compressed image has only a very small error; PSNR is greater than 30dB, it is difficult for the human eye to detect the difference between the compressed and original images; PSNR is between 20dB and 30dB, the human eye can detect the difference between the images; PSNR is between 10dB and 20dB, the human eye can still see the original structure of the image with the naked eye, and intuitively judge that there is no big difference between the two images; PSNR is lower than 10dB, it is difficult for humans to judge whether the two images are the same or whether one image is the compression result of another image. FID (Frechet Inception Distance): Spectral embedding distance of generative adversarial network.
[0106] It should be understood that, although the steps in the flowcharts involved in the above embodiments are displayed in sequence according to the indication of the arrows, these steps are not necessarily executed in sequence according to the order indicated by the arrows. Unless there is a clear explanation in this article, the execution of these steps is not strictly limited in order, and these steps can be executed in other orders. Moreover, at least a part of the steps in the flowcharts involved in the above embodiments may include multiple steps or multiple stages, and these steps or stages are not necessarily executed at the same time, but can be executed at different times, and the execution order of these steps or stages is not necessarily carried out in sequence, but can be executed in turn or alternately with other steps or at least a part of the steps or stages in other steps.
[0107] Based on the same inventive concept, the embodiment of the present application also provides an adversarial sample generation device based on filters and attention constraints for implementing the adversarial sample generation method based on filters and attention constraints involved above. The implementation scheme for solving the problem provided by the device is similar to the implementation scheme recorded in the above method, so the specific limitations in one or more embodiments of the adversarial sample generation device based on filters and attention constraints provided below can be found in the above limitations on the adversarial sample generation method based on filters and attention constraints, and will not be repeated here.
[0108] In one embodiment, Figure 6 As shown, a device for generating adversarial samples based on filters and attention constraints is provided, comprising:
[0109] The initial sample generation module 100 is used to obtain the initial spectrum of the original image, and multiply the initial spectrum with the filter function to obtain a modified spectrum, and obtain the corresponding initial adversarial sample by IDFT of the modified spectrum;
[0110] An adversarial loss module 200 is used to send the initial adversarial sample into the classifier to obtain the adversarial loss;
[0111] An attention loss module 300 is used to apply a preset anti-attention matrix to the original image and the initial adversarial sample, obtain non-critical areas of the original image and the initial adversarial sample, and calculate the difference between the two non-critical areas to obtain attention loss;
[0112] A loss weighted calculation module 400 is used to perform weighted calculation on the adversarial loss and the attention loss to obtain a final loss;
[0113] A filter adjustment module 500, for adjusting the filter function by minimizing an objective function based on the final loss and the initial adversarial sample using an Adam optimization algorithm, wherein the objective function is used to characterize the distance between the adversarial sample and the target label;
[0114] The adversarial sample generation module 600 is used to generate adversarial samples according to the adjusted filter function and the initial spectrum.
[0115] In one embodiment, the initial sample generation module 100 is further configured to convert the original image from the spatial domain to the frequency domain using DFT transformation to obtain an initial spectrum.
[0116] In one embodiment, the above-mentioned adversarial sample generation device based on filters and attention constraints also includes: a matrix construction module, used to input the original image into the ViT model to obtain the original attention weight matrix; based on the original attention weight matrix, a target attention weight matrix is obtained by using a bilinear difference algorithm; the target attention weight matrix is truncated based on a preset truncation frequency to obtain a preset anti-attention matrix.
[0117] In one of the embodiments, the matrix construction module is also used to average all heads in each layer of the original attention weight matrix to obtain a first matrix; add the first matrix and the unit matrix to process the residual connection to obtain a second matrix; recursively multiply the second matrix to obtain a recursive result; filter the recursive result and perform a normalization operation to obtain a normalized matrix; adjust the size of the normalized matrix based on the size of the Patch in the ViT model, and use a bilinear interpolation algorithm to obtain the target attention weight matrix.
[0118] In one embodiment, the formula of the truncation method is:
[0119]
[0120] Where cutoff is the preset cutoff frequency, cutoff∈[0,1].
[0121] In one embodiment, the filter adjustment module 500 is also used to define an objective function based on the final loss, the initial adversarial sample and the filter function; adjust the filter function, return the step of multiplying the initial spectrum by the filter function to obtain a modified spectrum, and iterate the modified spectrum through IDFT to obtain the corresponding initial adversarial sample for a preset number of times, and use the Adam optimization algorithm to minimize the objective function during the iteration process.
[0122] Each module in the above-mentioned adversarial sample generation device based on filters and attention constraints can be implemented in whole or in part by software, hardware, and a combination thereof. Each of the above-mentioned modules can be embedded in or independent of a processor in a computer device in the form of hardware, or can be stored in a memory in a computer device in the form of software, so that the processor can call and execute the operations corresponding to each of the above modules.
[0123] In one embodiment, a computer device is provided. The computer device may be a server, and its internal structure diagram may be as follows: Figure 7As shown. The computer device includes a processor, a memory and a network interface connected through a system bus. Among them, the processor of the computer device is used to provide computing and control capabilities. The memory of the computer device includes a non-volatile storage medium and an internal memory. The non-volatile storage medium stores an operating system, a computer program and a database. The internal memory provides an environment for the operation of the operating system and the computer program in the non-volatile storage medium. The database of the computer device is used to store preset data. The network interface of the computer device is used to communicate with an external terminal through a network connection. When the computer program is executed by the processor, a method for generating adversarial samples based on filters and attention constraints is implemented.
[0124] Those skilled in the art will understand that Figure 7 The structure shown in the figure is only a block diagram of a part of the structure related to the solution of the present application, and does not constitute a limitation on the computer device to which the solution of the present application is applied. The specific computer device may include more or fewer components than those shown in the figure, or combine certain components, or have a different arrangement of components.
[0125] In one embodiment, a computer device is provided, including a memory and a processor, wherein a computer program is stored in the memory, and when the processor executes the computer program, the above-mentioned adversarial sample generation method based on filters and attention constraints is implemented.
[0126] In one embodiment, a computer-readable storage medium is provided, on which a computer program is stored. When the computer program is executed by a processor, the above-mentioned adversarial sample generation method based on filters and attention constraints is implemented.
[0127] In one embodiment, a computer program product is provided, including a computer program, which, when executed by a processor, implements the above-mentioned adversarial sample generation method based on filters and attention constraints.
[0128] Those of ordinary skill in the art can understand that all or part of the processes in the above-mentioned embodiment methods can be completed by instructing the relevant hardware through a computer program, and the above-mentioned computer program can be stored in a non-volatile computer-readable storage medium. When the computer program is executed, it can include the processes of the embodiments of the above-mentioned methods. Among them, any reference to the memory, database or other medium used in the embodiments provided in the present application can include at least one of non-volatile and volatile memory. Non-volatile memory can include read-only memory (ROM), magnetic tape, floppy disk, flash memory, optical memory, high-density embedded non-volatile memory, resistive random access memory (ReRAM), magnetoresistive random access memory (MRAM), ferroelectric random access memory (FRAM), phase change memory (PCM), graphene memory, etc. Volatile memory can include random access memory (RAM) or external cache memory, etc. As an illustration and not limitation, RAM can be in various forms, such as static random access memory (SRAM) or dynamic random access memory (DRAM). The database involved in each embodiment provided in this application may include at least one of a relational database and a non-relational database. Non-relational databases may include distributed databases based on blockchains, etc., but are not limited to this. The processor involved in each embodiment provided in this application may be a general-purpose processor, a central processing unit, a graphics processor, a digital signal processor, a programmable logic device, a data processing logic device based on quantum computing, etc., but are not limited to this.
[0129] The technical features of the above embodiments may be combined arbitrarily. To make the description concise, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this specification.
[0130] The above embodiments only express several implementation methods of the present application, and the descriptions thereof are relatively specific and detailed, but they cannot be understood as limiting the scope of the present application. It should be pointed out that, for a person of ordinary skill in the art, several variations and improvements can be made without departing from the concept of the present application, and these all belong to the protection scope of the present application. Therefore, the protection scope of the present application shall be subject to the attached claims.
Claims
1. A method for generating adversarial samples based on filters and attention constraints, characterized in that: The method comprises: Obtain an initial spectrum of the original image, multiply the initial spectrum by the filter function to obtain a modified spectrum, and perform IDFT on the modified spectrum to obtain a corresponding initial adversarial sample; Sending the initial adversarial sample into the classifier to obtain adversarial loss; Applying a preset anti-attention matrix to the original image and the initial adversarial sample to obtain non-critical areas of the original image and the initial adversarial sample respectively, and calculating the difference between the two non-critical areas to obtain attention loss; Performing weighted calculation on the adversarial loss and the attention loss to obtain a final loss; Based on the final loss and the initial adversarial sample, an Adam optimization algorithm is used to minimize an objective function to adjust a filter function, wherein the objective function is used to characterize the distance between the adversarial sample and the target label; An adversarial sample is generated according to the adjusted filter function and the initial spectrum.
2. The method according to claim 1, characterized in that The obtaining of the initial spectrum of the original image comprises: The DFT transform is used to convert the original image from the spatial domain to the frequency domain to obtain the initial spectrum.
3. The method according to claim 1, characterized in that: The method further includes applying the preset anti-attention matrix to the original image and the initial adversarial sample to obtain non-critical areas of the original image and the initial adversarial sample, respectively, and calculating the difference between the two non-critical areas to obtain the attention loss: Input the original image into the ViT model to obtain the original attention weight matrix; According to the original attention weight matrix, a target attention weight matrix is obtained by using a bilinear difference algorithm; The target attention weight matrix is truncated based on a preset truncation frequency to obtain a preset anti-attention matrix.
4. The method according to claim 3, characterized in that The step of obtaining a target attention weight matrix based on the original attention weight matrix and using a bilinear difference algorithm includes: Averaging all heads of each layer in the original attention weight matrix to obtain a first matrix; Add the first matrix to the identity matrix and perform residual connection to obtain a second matrix; recursively multiplying the second matrices to obtain a recursive result; Filtering the recursive result and performing a normalization operation to obtain a normalized matrix; The size of the normalized matrix is adjusted based on the size of the Patch in the ViT model, and the target attention weight matrix is obtained using a bilinear interpolation algorithm.
5. The method according to claim 3, characterized in that: The formula for the truncation method is: Wherein, cutoff is the preset cutoff frequency, cutoff∈[0,1].
6. The method according to claim 1, characterized in that The step of minimizing the objective function based on the final loss and the initial adversarial sample by using the Adam optimization algorithm to adjust the filter function includes: Defining an objective function based on the final loss, the initial adversarial sample, and the filter function; The filter function is adjusted, and the modified spectrum obtained by multiplying the initial spectrum by the filter function is returned, and the modified spectrum is subjected to IDFT to obtain the corresponding initial adversarial sample. The step is iterated a preset number of times, and the Adam optimization algorithm is used to minimize the objective function during the iteration process.
7. A device for generating adversarial samples based on filters and attention constraints, characterized in that: The device comprises: An initial sample generation module is used to obtain an initial spectrum of the original image, and multiply the initial spectrum by the filter function to obtain a modified spectrum, and obtain a corresponding initial adversarial sample by performing IDFT on the modified spectrum; An adversarial loss module, used for sending the initial adversarial sample into a classifier to obtain an adversarial loss; An attention loss module, used for applying a preset anti-attention matrix to the original image and the initial adversarial sample, obtaining non-critical areas of the original image and the initial adversarial sample respectively, and calculating the difference between the two non-critical areas to obtain attention loss; A loss weighted calculation module, used to perform weighted calculation on the adversarial loss and the attention loss to obtain a final loss; A filter adjustment module, configured to adjust a filter function by minimizing an objective function based on the final loss and the initial adversarial sample using an Adam optimization algorithm, wherein the objective function is used to characterize the distance between the adversarial sample and the target label; The adversarial sample generation module is used to generate an adversarial sample according to the adjusted filter function and the initial spectrum.
8. A computer device comprising a memory and a processor, wherein the memory stores a computer program, wherein: When the processor executes the computer program, the steps of the method according to any one of claims 1 to 6 are implemented.
9. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the steps of the method according to any one of claims 1 to 6 are implemented.
10. A computer program product, comprising a computer program, characterized in that When the computer program is executed by a processor, the steps of the method according to any one of claims 1 to 6 are implemented.
Citation Information
Patent Citations
Adversarial sample generation method and system, computer equipment and storage medium
CN113780123A
Generative adversarial model training method, adversarial sample generation method and related devices
CN117993476A
