Counterfactual explanation generation method and device
By constructing opposite text instructions and using fine-tuned potential diffusion models, the problem of inefficient and inefficient counterfactual interpretation generation in the prior art is solved, and more efficient and robust interpretation generation is achieved.
Patent Information
- Application Number
- CN202510090708.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-01-21
- Publication Date
- 2025-04-29
- Estimated Expiration
- 2045-01-21
AI Technical Summary
The effectiveness and efficiency of generating counterfactual explanations in the prior art, especially the lack of effectiveness in facial exchange, and multiple optimizations lead to reduced efficiency.
By constructing text instructions that are opposite to the true and false category of the image to be interpreted, the fine-tuned potential diffusion model is guided to generate counterfactual explanations. This model is fine-tuned by the loss of the integrated classifier, enhancing the robustness of the generated explanation.
A more efficient and efficient counterfactual interpretation generation is achieved, which improves the robustness and generation efficiency of interpretation, and can effectively flip the category judgment of the detection model.
Smart Images

Figure CN119540659B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of counterfactual explanations, and in particular to a method and device for generating counterfactual explanations. Background Art
[0002] Deepfake is a hybrid of "deep learning" and "fake", which refers to the use of deep learning algorithms to create realistic simulations and fictions in video and audio. For highly realistic fakes that are indistinguishable to the human eye, it is not enough to rely on detection models to understand their inner workings. Therefore, it is crucial to explain these models.
[0003] There are two main automated approaches to applying counterfactual explanations to Deepfake datasets: Approach 1: Generate more realistic and deceptive counterfactual explanations by enhancing suspicious images; Approach 2: Fine-tune the encoder to achieve accurate image reconstruction and generate counterfactual explanations through adversarial attacks in the latent space.
[0004] However, method one is limited to face swapping and lacks effectiveness, and method two involves multiple optimizations, which reduces efficiency. Therefore, it is necessary to provide a more effective and efficient method to generate counterfactual explanations. Summary of the invention
[0005] The present invention provides a method and device for generating counterfactual explanations, so as to solve the defects of low effectiveness and low efficiency of counterfactual explanations generated in the prior art, and to achieve more effective and efficient generation of counterfactual explanations.
[0006] The present invention provides a method for generating counterfactual explanations, comprising:
[0007] According to the true or false category of the image to be interpreted, construct a first text instruction; the first text instruction is a text instruction opposite to the true or false category of the image to be interpreted;
[0008] Inputting the image to be interpreted into a fine-tuned latent diffusion model, wherein the fine-tuned latent diffusion model generates a counterfactual explanation corresponding to the image to be interpreted based on the guidance of the first text instruction;
[0009] The fine-tuned latent diffusion model is obtained by fine-tuning the latent diffusion model based on the loss of the ensemble classifier.
[0010] In some embodiments, the latent diffusion model includes: a latent diffusion encoder, a denoising model and a latent diffusion decoder; the method further includes:
[0011] Inputting the training image into the latent diffusion encoder for encoding to obtain a first latent representation;
[0012] perturbing the first latent representation by perturbing noise to generate a latent noise representation;
[0013] Inputting the latent noise representation into the denoising model for denoising to obtain a second latent representation and a noise loss;
[0014] Inputting the second latent representation into the latent diffusion decoder for decoding to generate a counterfactual explanation corresponding to the training image;
[0015] Inputting the counterfactual explanation corresponding to the training image into an ensemble classifier, and calculating the loss of the ensemble classifier;
[0016] Based on the noise loss and the loss of the ensemble classifier, the parameters of the denoising model are updated.
[0017] In some embodiments, the step of inputting the potential noise representation into the denoising model for denoising to obtain a second potential representation and a noise loss comprises:
[0018] constructing a second text instruction according to the true or false category of the training image; the second text instruction is a text instruction opposite to the true or false category of the training image;
[0019] Based on the second text instruction, instruct the denoising model to generate predicted noise;
[0020] removing the prediction noise from the potential noise representation to obtain the second potential representation;
[0021] The noise penalty is generated based on the second text instruction, the disturbance noise, and the predicted noise.
[0022] In some embodiments, instructing the denoising model to generate predicted noise based on the second text instruction includes:
[0023] Based on a text encoder, encoding the second text instruction into a text embedding vector;
[0024] Based on a cross-attention mechanism, the text embedding vector is combined with the potential noise representation to guide the denoising model to generate the predicted noise.
[0025] In some embodiments, the text embedding vector is combined with the potential noise representation based on a cross-attention mechanism to guide the denoising model to generate the predicted noise, including:
[0026] Calculating an attention output based on the text embedding vector and the potential noise representation;
[0027] Based on the attention output, the denoising model is guided to generate the predicted noise.
[0028] In some embodiments, inputting the counterfactual explanation corresponding to the training image into an integrated classifier and calculating the loss of the integrated classifier includes:
[0029] Performing cross entropy loss calculation on the probability of true or false judgment of the counterfactual explanation corresponding to the training image by each classifier in the ensemble classifier and the true or false category label of the training image to obtain the loss of each classifier;
[0030] Based on the loss of each classifier and the weight corresponding to each classifier, the loss of the ensemble classifier is obtained.
[0031] The present invention also provides a counterfactual explanation generation device, comprising:
[0032] A construction module, configured to construct a first text instruction according to the true or false category of the image to be interpreted; the first text instruction is a text instruction opposite to the true or false category of the image to be interpreted;
[0033] A generating module, configured to input the image to be interpreted into a fine-tuned latent diffusion model, wherein the fine-tuned latent diffusion model generates a counterfactual explanation corresponding to the image to be interpreted based on the guidance of the first text instruction;
[0034] The fine-tuned latent diffusion model is obtained by fine-tuning the latent diffusion model based on the loss of the ensemble classifier.
[0035] The present invention also provides an electronic device, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein when the processor executes the computer program, the counterfactual explanation generating method as described above is implemented.
[0036] The present invention also provides a non-transitory computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements any of the counterfactual explanation generation methods described above.
[0037] The present invention also provides a computer program product, comprising a computer program, wherein when the computer program is executed by a processor, the counterfactual explanation generation method as described above is implemented.
[0038] The counterfactual explanation generation method and device provided by the present invention use the loss of an integrated classifier to fine-tune the latent diffusion model, enhance the robustness of the generated counterfactual explanation, and reconstruct text instructions that are opposite to the true or false categories of the image to be interpreted. The text instructions provide directional guidance for the fine-tuned latent diffusion model to generate counterfactual explanations, thereby achieving more effective and efficient generation of counterfactual explanations. BRIEF DESCRIPTION OF THE DRAWINGS
[0039] In order to more clearly illustrate the technical solutions in the present invention or the prior art, the following briefly introduces the drawings required for use in the embodiments or the description of the prior art. Obviously, the drawings described below are some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0040] Figure 1 It is a flowchart of the counterfactual explanation generation method provided by the present invention.
[0041] Figure 2 It is a flowchart of the training process of the potential diffusion model provided by the present invention.
[0042] Figure 3 It is a schematic diagram of the structure of the counterfactual explanation generation device provided by the present invention.
[0043] Figure 4 It is a structural schematic diagram of the electronic device provided by the present invention. DETAILED DESCRIPTION
[0044] In order to make the purpose, technical solution and advantages of the present invention clearer, the technical solution of the present invention will be clearly and completely described below in conjunction with the drawings of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0045] In order to facilitate a clearer understanding of the counterfactual explanation generation method provided by the present invention, some relevant technical knowledge is first introduced as follows.
[0046] (1) Counterfactual Explanation
[0047] Essentially, counterfactual explanations answer the following question: “What is the difference between the input Minimal modification or disturbance What makes the resulting disturbance input is classified as the desired target class with sufficiently high confidence (similar to the input ) and remain realistic? "Formally speaking, given the input and detection model , the goal is to find counterfactual explanations , to realize the loss function Desired target categories defined , and as close as possible to the distance metric Defined Input , as shown below:
[0048]
[0049] in, represents the distance metric function, represents a counterfactual explanation, Indicates input, represents the loss function, represents the probability of the category judgment of the detection model for the counterfactual explanation, Representation and Input The desired target category is opposite to the category of .
[0050] The generated counterfactual explanation must satisfy the following goals: 1) Validity: Perturbing the input The original predictions of the classifier should be flipped; 2) Sparsity: Perturbation It should be sparse so that changes are interpretable to humans; 3) Reality: Perturbed input Should be close to the data manifold.
[0051] (2) Diffusion model
[0052] Diffusion models have become a powerful tool for computer vision generation tasks, especially in generating high-quality images. Diffusion models are a class of generative models that simulate the process from a data distribution to a noise distribution and then recover the data through the reverse process. Specifically, there are two main processes in the diffusion model:
[0053] 1. Forward Process: gradually add noise to the real data and finally convert it into pure noise.
[0054] 2. Reverse Process: Starting from the noise, restore the original data by gradually removing the noise.
[0055] Traditional diffusion models operate in pixel space, which makes them computationally expensive for high-resolution image generation.
[0056] (3) Potential Diffusion Model
[0057] The Latent Diffusion Model (LDM) is a generative model that is mainly used for efficient image generation tasks. It significantly improves computational efficiency by transferring the image generation process from pixel space to latent space. LDM is based on the principle of diffusion model and solves the problem of high computational overhead of traditional diffusion model in image generation by adding and removing noise in latent space. In addition, LDM allows guided denoising by combining control signals such as text, images or semantic maps.
[0058] The potential diffusion model has the following advantages:
[0059] 1. Lower computational overhead: LDM encodes the image into a latent space and then performs a diffusion process in the latent space. In this way, the diffusion process does not operate directly in the high-dimensional pixel space, but in the lower-dimensional latent space, which significantly reduces the computational overhead.
[0060] 2. More efficient computing speed: By adding and denoising noise in the latent space, LDM can effectively reduce the computing resources required in the generation process. Generating high-quality images in pixel space usually requires a lot of calculations, while in the latent space, the dimension of the data is lower and the calculation speed is faster.
[0061] 3. Fine control and high-quality generation: LDM preserves the details of the image during the generation process, while making it easier to control the generation process through the latent representation. For example, LDM can be used for tasks such as image super-resolution, style transfer, image restoration, and can generate high-quality images.
[0062] (4) Cross-Attention
[0063] Cross-Attention is an extension of the attention mechanism, which is usually used to combine two different input information sources. It is widely used in multimodal learning, sequence-to-sequence tasks (such as machine translation, image generation, etc.), and other tasks that require combining different information sources.
[0064] In the standard self-attention mechanism, each element of the input interacts with its own elements to learn the relationship between elements in the sequence. In cross-attention, two different inputs (usually called "query" and "key-value") interact. The query comes from one input source, and the key and value come from another input source. The query usually comes from the target sequence or target modality, and the key and value usually come from the source sequence or source modality.
[0065] Cross-attention has the following advantages:
[0066] 1. Fusion of multimodal information: The cross-attention mechanism can effectively combine information from different modalities (such as text and images, speech and text) to enhance the model’s understanding of complex data.
[0067] 2. Improve generation quality: By allowing interactions between queries and key-value pairs, cross-attention can improve the quality of generation tasks (such as image generation or machine translation), ensuring that the output is well matched to the different modalities of the input.
[0068] 3. Flexibility and scalability: The cross-attention mechanism is not limited to the interaction between two modalities, but can also be extended to more modalities (such as multimodal tasks such as image-text-audio).
[0069] Figure 1 : is a flow chart of a counterfactual explanation generation method provided by the present invention. As shown in the figure, the present invention provides a counterfactual explanation generation method, comprising:
[0070] Step 110, constructing a first text instruction according to the true or false category of the image to be interpreted; the first text instruction is a text instruction opposite to the true or false category of the image to be interpreted.
[0071] Specifically, the image to be interpreted comes from a Deepfake image dataset, and according to the true or false category of the image to be interpreted, a first text instruction opposite to the true or false category of the image to be interpreted is constructed.
[0072] For example, if the image to be interpreted is a real face image, the first text instruction is constructed as "a fake face"; for example, if the image to be interpreted is a forged face image, the first text instruction is constructed as "a real face".
[0073] Step 120, input the image to be interpreted into the fine-tuned latent diffusion model, and the fine-tuned latent diffusion model generates a counterfactual explanation corresponding to the image to be interpreted based on the guidance of the first text instruction; wherein the fine-tuned latent diffusion model is obtained after the latent diffusion model is fine-tuned based on the loss of the ensemble classifier.
[0074] Specifically, the latent diffusion model is fine-tuned using the loss of the ensemble classifier to obtain a fine-tuned latent diffusion model, which effectively enhances the robustness of the generated counterfactual explanations. The generated counterfactual explanations can flip the category judgment of the detection model.
[0075] The image to be explained is input into the fine-tuned latent diffusion model. The first text instruction provides directional guidance for the generation process of the counterfactual explanation. The fine-tuned latent diffusion model provides directional guidance through the first text instruction to generate the counterfactual explanation corresponding to the image to be explained.
[0076] The counterfactual explanation generation method provided by the embodiment of the present invention uses the loss of the integrated classifier to fine-tune the latent diffusion model, enhances the robustness of the generated counterfactual explanation, and reconstructs text instructions that are opposite to the true and false categories of the image to be interpreted. The text instructions provide directional guidance for the fine-tuned latent diffusion model to generate counterfactual explanations, thereby achieving more effective and efficient generation of counterfactual explanations.
[0077] In some embodiments, the counterfactual explanation generation method provided by the present invention further includes:
[0078] Input the training image into the latent diffusion encoder for encoding to obtain a first latent representation;
[0079] perturbing the first latent representation by perturbing noise to generate a latent noise representation;
[0080] Input the latent noise representation into the denoising model for denoising to obtain a second latent representation and a noise loss;
[0081] Input the second latent representation into the latent diffusion decoder for decoding to generate the counterfactual explanation corresponding to the training image;
[0082] Input the counterfactual explanation corresponding to the training image into the ensemble classifier and calculate the loss of the ensemble classifier;
[0083] Based on the noise loss and the loss of the ensemble classifier, the parameters of the denoising model are updated.
[0084] Specifically, the training image is input into the latent diffusion model to obtain the counterfactual explanation corresponding to the training image, and then the counterfactual explanation corresponding to the training image is input into the ensemble classifier. Based on the loss of the ensemble classifier, the latent diffusion model is fine-tuned to obtain the fine-tuned latent diffusion model.
[0085] The latent diffusion model includes: a latent diffusion encoder, a denoising model and a latent diffusion decoder. Figure 2 FIG2 is a flow chart of the training process of the potential diffusion model provided by the present invention. As shown in FIG2 , the specific training process of the potential diffusion model is as follows:
[0086] In the forward process, the training image is input into the latent diffusion encoder for encoding to obtain the first latent representation , and then by perturbing the noise For the first potential Perturb to generate a potential noise representation .
[0087] In the reverse process, the potential noise is expressed as Input denoising model to denoise and obtain the second potential representation and noise loss .
[0088] The second potential Input the latent diffusion decoder for decoding to generate the counterfactual explanation corresponding to the training image. Input the counterfactual explanation corresponding to the training image into the ensemble classifier and calculate the loss of the ensemble classifier .
[0089] Finally, based on the noise loss and the loss of the ensemble classifier Back propagation is performed to update the parameters of the denoising model, thereby fine-tuning the potential diffusion model.
[0090] Therefore, the overall loss of the latent diffusion model during fine-tuning is as follows:
[0091]
[0092] In the formula, represents the overall loss, represents the weight corresponding to the noise loss, represents the noise loss, represents the weight corresponding to the loss of the integrated classifier, represents the loss of the ensemble classifier.
[0093] In related technologies, an autoencoder is first trained on an image reconstruction task, and then counterfactual explanations are generated through adversarial attacks during the inference phase. This method requires multiple optimizations, which not only incurs significant time overhead, but may also generate adversarial samples. In addition, these methods still rely on the guidance of a classifier to generate counterfactual explanations after training, so they are not convenient to use.
[0094] The present invention introduces the loss of integrated classifier in the training stage, transforming the image reconstruction task into a counterfactual explanation generation task, so that the model can directly generate counterfactual explanations in the reasoning stage without relying on classifier guidance.
[0095] The counterfactual explanation generation method provided by the embodiment of the present invention obtains the counterfactual explanation corresponding to the training image by inputting the training image into the latent diffusion model, and then inputs the counterfactual explanation corresponding to the training image into the ensemble classifier, and fine-tunes the latent diffusion model based on the noise loss of the denoising process and the loss of the ensemble classifier, thereby effectively improving the robustness of the counterfactual explanation.
[0096] In some embodiments, inputting the potential noise representation into a denoising model for denoising to obtain a second potential representation and a noise loss includes:
[0097] According to the true or false category of the training image, construct a second text instruction; the second text instruction is a text instruction opposite to the true or false category of the training image;
[0098] Instructing the denoising model to generate prediction noise based on the second text instruction;
[0099] Remove the prediction noise from the latent noise representation to obtain a second latent representation;
[0100] A noise loss is generated based on the second text instruction, the disturbance noise, and the prediction noise.
[0101] Specifically, the goal of adversarial explanation is to generate a counterfactual explanation , making it possible to transform the output of a given classifier from the input The category of is changed to the desired target category In order to express the generation direction, text conditional control is used in the latent diffusion model. In this way, the latent diffusion model is able to learn adversarial transformations rather than simple image reconstruction, thus reflecting the essence of adversarial interpretation. The specific process is as follows:
[0102] According to the true and false categories of the training images, a second text instruction opposite to the true and false categories of the training images is constructed. The second text instruction provides guidance for the generation of prediction noise, and the denoising model generates prediction noise based on the guidance provided by the second text instruction. The potential noise represents By removing the prediction noise , restored to the second potential representation .
[0103] During fine-tuning, the expression of the noise loss is as follows:
[0104]
[0105] In the formula, represents the noise loss, Express expectations, represents the potential representation encoded by the encoder, represents the input of the denoising model, represents the disturbance noise, represents the tth time step, represents the prediction noise, represents the latent space representation (i.e., the latent noise representation) at time step t, represents the control signal generated by the second text instruction, Represents the standard normal distribution.
[0106] The counterfactual explanation generation method provided by the embodiment of the present invention constructs a second text instruction that is opposite to the true or false category of the training image. The denoising model generates prediction noise based on the second text instruction, removes the prediction noise from the potential noise representation, and obtains the second potential representation, which is beneficial for the subsequent potential diffusion decoder to generate an effective counterfactual explanation based on the second potential representation.
[0107] In some embodiments, based on the second text instruction, instructing the denoising model to generate predicted noise includes:
[0108] Based on the text encoder, encode the second text instruction into a text embedding vector;
[0109] Based on the cross-attention mechanism, the text embedding vector is combined with the potential noise representation to guide the denoising model to generate prediction noise.
[0110] Specifically, the second text instruction is input into a text encoder (eg, a CLIP (Contrastive Language-Image Pre-training) model), and the text encoder encodes the second text instruction into a text embedding vector.
[0111] The denoising process of the denoising model depends not only on the latent noise representation, but also on the second text instruction. The key to cross-attention is to use the text embedding vector as a conditional input to participate in the denoising process. In each step of the denoising process, the text embedding vector works together with the latent noise representation to provide guidance for the generation of prediction noise, ensuring that the true and false category labels of the counterfactual explanations generated at the end are opposite to the true and false category labels of the training images.
[0112] The counterfactual explanation generation method provided by the embodiment of the present invention encodes the second text instruction into a text embedding vector through a text encoder, and then combines the text embedding vector with the potential noise representation through a cross-attention mechanism to guide the denoising process, so that the generation of predicted noise meets expectations, and further makes the true and false category labels of the generated counterfactual explanations meet the expected categories, thereby improving the effectiveness of the counterfactual explanations.
[0113] In some embodiments, based on a cross-attention mechanism, the text embedding vector is combined with the potential noise representation to guide the denoising model to generate predicted noise, including:
[0114] Calculate the attention output based on the text embedding vector and the potential noise representation;
[0115] Based on the attention output, the denoising model is guided to generate prediction noise.
[0116] Specifically, the query comes from the latent noise representation, and the key-value pair comes from the text embedding vector. In each step of denoising, the latent noise representation and the text embedding vector are input into the cross-attention module. First, the attention score between the query and the key is calculated based on the text embedding vector and the latent noise representation; then, the value is weighted summed based on the attention score to obtain the attention output; finally, based on the attention output, the denoising model is guided to generate prediction noise. The specific process is as follows:
[0117] The cross-attention module adjusts the noise removal strategy by calculating the similarity between the text embedding vector and the potential noise representation. The similarity between the text embedding vector and the potential noise representation can be obtained by calculating the dot product between the query from the potential noise representation and the key from the text embedding vector. The dot product is then normalized to get the attention score. The attention score is used to weight the value from the text embedding vector to get the attention output. The output will focus on the key-value pairs that are most relevant to the query.
[0118] The expression of attention output is as follows:
[0119]
[0120] In the formula, represents the attention output, represents a query from a latent noisy representation, represents the key from the text embedding vector, represents the value from the text embedding vector, Indicates the dimension of the key, used for scaling, superscript Represents a transpose operation.
[0121] Finally, the obtained attention output influences the denoising process of the denoising model, which contains value information weighted according to the relationship between the query from the latent noise representation and the key from the text embedding vector.
[0122] In some embodiments, inputting the counterfactual explanation corresponding to the training image into the ensemble classifier and calculating the loss of the ensemble classifier includes:
[0123] The cross entropy loss is calculated by calculating the true or false judgment probability of the counterfactual explanation corresponding to the training image by each classifier in the ensemble classifier and the true or false category label of the training image to obtain the loss of each classifier;
[0124] Based on the loss of each classifier and the weight corresponding to each classifier, the loss of the integrated classifier is obtained.
[0125] Specifically, the ensemble classifier contains multiple classifiers. The counterfactual explanations corresponding to the training images are input into the ensemble classifier. Each classifier judges the counterfactual explanations corresponding to the training images and outputs a true or false judgment probability. The true or false judgment probability of the counterfactual explanations corresponding to the training images of each classifier is calculated by cross entropy loss with the true or false category labels of the training images to obtain the loss of each classifier. The loss expression of a classifier is as follows:
[0126]
[0127] In the formula, Indicates The loss of the classifier, represents the true and false category labels of the training images, , where the real image label is 0 and the deep fake image label is 1, Indicates classifiers, represents the counterfactual explanation corresponding to the training image, Indicates The probability that a classifier judges the counterfactual explanation corresponding to the training image to be true or false.
[0128] The loss of all classifiers and their corresponding weights are multiplied and added together to get the loss of the ensemble classifier. The expression of the ensemble classifier loss is as follows:
[0129]
[0130] In the formula, represents the ensemble classifier loss , represents the number of classifiers in the ensemble classifier, Indicates The weight of the classifier, Indicates The loss of a classifier.
[0131] The counterfactual explanation generation method provided by the embodiment of the present invention obtains the loss of each classifier by performing cross entropy loss calculation on the true or false judgment probability of the counterfactual explanation corresponding to the training image of each classifier and the true or false category label of the training image; then, based on the loss of each classifier and the weight corresponding to each classifier, the loss of the integrated classifier is obtained, thereby obtaining the loss of the integrated classifier, which is beneficial to the subsequent fine-tuning of the potential diffusion model.
[0132] The counterfactual explanation generation device provided by the present invention is described below. The counterfactual explanation generation device described below and the counterfactual explanation generation method described above can be referenced to each other.
[0133] Figure 3 is a schematic diagram of the structure of the counterfactual explanation generation device provided by the present invention, such as Figure 3 As shown, the present invention provides a counterfactual explanation generation device, comprising:
[0134] A construction module 310 is used to construct a first text instruction according to the true or false category of the image to be interpreted; the first text instruction is a text instruction opposite to the true or false category of the image to be interpreted;
[0135] A generating module 320, configured to input the image to be interpreted into a fine-tuned latent diffusion model, wherein the fine-tuned latent diffusion model generates a counterfactual explanation corresponding to the image to be interpreted based on the guidance of the first text instruction;
[0136] The fine-tuned latent diffusion model is obtained by fine-tuning the latent diffusion model based on the loss of the ensemble classifier.
[0137] In some embodiments, the latent diffusion model comprises: a latent diffusion encoder, a denoising model and a latent diffusion decoder; the apparatus further comprises:
[0138] An encoding module, used for inputting a training image into the latent diffusion encoder for encoding to obtain a first latent representation;
[0139] a perturbation module, configured to perturb the first latent representation by using perturbation noise to generate a latent noise representation;
[0140] A denoising module, configured to input the potential noise representation into the denoising model for denoising to obtain a second potential representation and a noise loss;
[0141] A decoding module, configured to input the second latent representation into the latent diffusion decoder for decoding, and generate a counterfactual explanation corresponding to the training image;
[0142] A calculation module, used for inputting the counterfactual explanation corresponding to the training image into an integrated classifier, and calculating the loss of the integrated classifier;
[0143] An updating module is used to update the parameters of the denoising model based on the noise loss and the loss of the ensemble classifier.
[0144] In some embodiments, the denoising module comprises:
[0145] A construction submodule, configured to construct a second text instruction according to the true or false category of the training image; the second text instruction is a text instruction opposite to the true or false category of the training image;
[0146] a guidance submodule, configured to guide the denoising model to generate predicted noise based on the second text instruction;
[0147] a removal submodule, configured to remove the predicted noise from the potential noise representation to obtain the second potential representation;
[0148] A generating submodule is used to generate the noise loss based on the second text instruction, the disturbance noise and the predicted noise.
[0149] In some embodiments, the guidance submodule comprises:
[0150] An encoding unit, configured to encode the second text instruction into a text embedding vector based on a text encoder;
[0151] A guidance unit is used to combine the text embedding vector with the potential noise representation based on a cross-attention mechanism to guide the denoising model to generate the predicted noise.
[0152] In some embodiments, the instruction unit is specifically configured to:
[0153] Calculating an attention output based on the text embedding vector and the potential noise representation;
[0154] Based on the attention output, the denoising model is guided to generate the predicted noise.
[0155] In some embodiments, the computing module includes:
[0156] A calculation submodule, used to perform cross entropy loss calculation on the true or false judgment probability of the counterfactual explanation corresponding to the training image by each classifier in the integrated classifier and the true or false category label of the training image to obtain the loss of each classifier;
[0157] The acquisition submodule is used to obtain the loss of the integrated classifier based on the loss of each classifier and the weight corresponding to each classifier.
[0158] It should be noted here that the above-mentioned counterfactual explanation generation device provided by the present invention can implement all the method steps implemented by the above-mentioned method embodiment, and can achieve the same technical effect. The parts and beneficial effects that are the same as the method embodiment in this embodiment will not be specifically described here.
[0159] Figure 4 is a schematic diagram of the structure of the electronic device provided by the present invention, such as Figure 4As shown, the electronic device may include: a processor 410, a communication interface 420, a memory 430 and a communication bus 440, wherein the processor 410, the communication interface 420 and the memory 430 communicate with each other through the communication bus 440. The processor 410 may call the logic instructions in the memory 430 to execute the counterfactual explanation generation method, which includes: constructing a first text instruction according to the true or false category of the image to be explained; the first text instruction is a text instruction opposite to the true or false category of the image to be explained; the image to be explained is input into the fine-tuned latent diffusion model, and the fine-tuned latent diffusion model generates the counterfactual explanation corresponding to the image to be explained based on the guidance of the first text instruction; wherein the fine-tuned latent diffusion model is obtained after the latent diffusion model is fine-tuned based on the loss of the ensemble classifier.
[0160] In addition, the logic instructions in the above-mentioned memory 430 can be implemented in the form of a software functional unit and can be stored in a computer-readable storage medium when it is sold or used as an independent product. Based on this understanding, the technical solution of the present invention, in essence, or the part that contributes to the prior art or the part of the technical solution, can be embodied in the form of a software product, and the computer software product is stored in a storage medium, including a number of instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the method described in each embodiment of the present invention. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (ROM, Read-Only Memory), random access memory (RAM, Random Access Memory), disk or optical disk, etc. Various media that can store program codes.
[0161] On the other hand, the present invention also provides a computer program product, which includes a computer program, and the computer program can be stored on a non-transitory computer-readable storage medium. When the computer program is executed by a processor, the computer can execute the counterfactual explanation generation method provided by the above-mentioned methods, and the method includes: constructing a first text instruction according to the true or false category of the image to be interpreted; the first text instruction is a text instruction opposite to the true or false category of the image to be interpreted; the image to be interpreted is input into a fine-tuned latent diffusion model, and the fine-tuned latent diffusion model generates a counterfactual explanation corresponding to the image to be interpreted based on the guidance of the first text instruction; wherein the fine-tuned latent diffusion model is obtained after fine-tuning the latent diffusion model based on the loss of the ensemble classifier.
[0162] On the other hand, the present invention also provides a non-transitory computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, is implemented to execute the counterfactual explanation generation method provided by the above-mentioned methods, the method comprising: constructing a first text instruction according to the true or false category of the image to be interpreted; the first text instruction is a text instruction opposite to the true or false category of the image to be interpreted; the image to be interpreted is input into a fine-tuned latent diffusion model, and the fine-tuned latent diffusion model generates a counterfactual explanation corresponding to the image to be interpreted based on the guidance of the first text instruction; wherein the fine-tuned latent diffusion model is obtained after fine-tuning the latent diffusion model based on the loss of the ensemble classifier.
[0163] The device embodiments described above are merely illustrative, wherein the units described as separate components may or may not be physically separated, and the components displayed as units may or may not be physical units, that is, they may be located in one place, or they may be distributed on multiple network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the scheme of this embodiment. Ordinary technicians in this field can understand and implement it without paying creative labor.
[0164] Through the description of the above implementation methods, those skilled in the art can clearly understand that each implementation method can be implemented by means of software plus a necessary general hardware platform, and of course, can also be implemented by hardware. Based on this understanding, the above technical solution is essentially or the part that contributes to the prior art can be embodied in the form of a software product, and the computer software product can be stored in a computer-readable storage medium, such as ROM / RAM, a disk, an optical disk, etc., including a number of instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to execute the methods described in each embodiment or some parts of the embodiments.
[0165] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit it. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present invention.
Claims
1. A method for generating counterfactual explanations, characterized in that: include: According to the true or false category of the image to be interpreted, construct a first text instruction; the first text instruction is a text instruction opposite to the true or false category of the image to be interpreted; Inputting the image to be interpreted into a fine-tuned latent diffusion model, wherein the fine-tuned latent diffusion model generates a counterfactual explanation corresponding to the image to be interpreted based on the guidance of the first text instruction; The fine-tuned latent diffusion model is obtained by fine-tuning the latent diffusion model based on the loss of the ensemble classifier; The potential diffusion model includes: a potential diffusion encoder, a denoising model and a potential diffusion decoder; the method also includes: Inputting the training image into the latent diffusion encoder for encoding to obtain a first latent representation; perturbing the first latent representation by perturbing noise to generate a latent noise representation; Inputting the latent noise representation into the denoising model for denoising to obtain a second latent representation and a noise loss; Inputting the second latent representation into the latent diffusion decoder for decoding to generate a counterfactual explanation corresponding to the training image; Inputting the counterfactual explanation corresponding to the training image into an ensemble classifier, and calculating the loss of the ensemble classifier; Based on the noise loss and the loss of the ensemble classifier, updating the parameters of the denoising model; The step of inputting the potential noise representation into the denoising model for denoising to obtain a second potential representation and a noise loss comprises: constructing a second text instruction according to the true or false category of the training image; the second text instruction is a text instruction opposite to the true or false category of the training image; Based on the second text instruction, instruct the denoising model to generate predicted noise; removing the prediction noise from the potential noise representation to obtain the second potential representation; generating the noise loss based on the second text instruction, the disturbance noise, and the predicted noise; The step of instructing the denoising model to generate predicted noise based on the second text instruction includes: Based on a text encoder, encoding the second text instruction into a text embedding vector; Based on a cross-attention mechanism, the text embedding vector is combined with the potential noise representation to guide the denoising model to generate the predicted noise.
2. The method for generating counterfactual explanations according to claim 1, characterized in that: The method of combining the text embedding vector with the potential noise representation based on the cross attention mechanism to guide the denoising model to generate the predicted noise includes: Calculating an attention output based on the text embedding vector and the potential noise representation; Based on the attention output, the denoising model is guided to generate the predicted noise.
3. The method for generating counterfactual explanations according to claim 1, characterized in that: The step of inputting the counterfactual explanation corresponding to the training image into an integrated classifier and calculating the loss of the integrated classifier comprises: Performing cross entropy loss calculation on the probability of true or false judgment of the counterfactual explanation corresponding to the training image by each classifier in the ensemble classifier and the true or false category label of the training image to obtain the loss of each classifier; Based on the loss of each classifier and the weight corresponding to each classifier, the loss of the ensemble classifier is obtained.
4. A counterfactual explanation generating device, characterized in that: include: A construction module, configured to construct a first text instruction according to the true or false category of the image to be interpreted; the first text instruction is a text instruction opposite to the true or false category of the image to be interpreted; A generating module, configured to input the image to be interpreted into a fine-tuned latent diffusion model, wherein the fine-tuned latent diffusion model generates a counterfactual explanation corresponding to the image to be interpreted based on the guidance of the first text instruction; The fine-tuned latent diffusion model is obtained by fine-tuning the latent diffusion model based on the loss of the ensemble classifier; The potential diffusion model includes: a potential diffusion encoder, a denoising model and a potential diffusion decoder; the device also includes: An encoding module, used for inputting a training image into the latent diffusion encoder for encoding to obtain a first latent representation; a perturbation module, configured to perturb the first latent representation by using perturbation noise to generate a latent noise representation; A denoising module, configured to input the potential noise representation into the denoising model for denoising to obtain a second potential representation and a noise loss; A decoding module, configured to input the second latent representation into the latent diffusion decoder for decoding, and generate a counterfactual explanation corresponding to the training image; A calculation module, used for inputting the counterfactual explanation corresponding to the training image into an integrated classifier, and calculating the loss of the integrated classifier; An updating module, configured to update the parameters of the denoising model based on the noise loss and the loss of the ensemble classifier; The denoising module comprises: A construction submodule, configured to construct a second text instruction according to the true or false category of the training image; the second text instruction is a text instruction opposite to the true or false category of the training image; a guidance submodule, configured to guide the denoising model to generate predicted noise based on the second text instruction; a removal submodule, configured to remove the predicted noise from the potential noise representation to obtain the second potential representation; A generating submodule, configured to generate the noise loss based on the second text instruction, the disturbance noise and the predicted noise; The guidance submodule comprises: An encoding unit, configured to encode the second text instruction into a text embedding vector based on a text encoder; A guidance unit is used to combine the text embedding vector with the potential noise representation based on a cross-attention mechanism to guide the denoising model to generate the predicted noise.
5. An electronic device comprising a memory, a processor, and a computer program stored in the memory and running on the processor, characterized in that: When the processor executes the computer program, the counterfactual explanation generation method according to any one of claims 1 to 3 is implemented.
6. A non-transitory computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the counterfactual explanation generation method according to any one of claims 1 to 3 is implemented.
7. A computer program product, comprising a computer program, characterized in that When the computer program is executed by a processor, the counterfactual explanation generation method according to any one of claims 1 to 3 is implemented.
Citation Information
Patent Citations
Image processing method and related device
CN115147635A
Information processing method, information processing device and storage medium
CN116090556A