Diffusion model image restoration method based on regional mask and dynamic exit
By adopting a diffusion model-based image restoration method based on region masking and dynamic exit, the problems of high computational overhead and redundant iteration steps in existing technologies are solved, achieving efficient and accurate image restoration results and improving computational efficiency and image quality.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- TAIYUAN UNIVERSITY OF TECHNOLOGY
- Filing Date
- 2025-12-19
- Publication Date
- 2026-04-14
AI Technical Summary
Existing diffusion models suffer from high computational overhead and redundant iteration steps in image restoration tasks, especially when some areas do not require restoration, resulting in wasted computing power and increased time costs.
An image restoration method based on a diffusion model with region masking and dynamic termination is adopted. Latent space mask is generated by edge detection, and UNet denoising and ControlNet conditional constraints are applied only to the effective region. The iteration is dynamically terminated to meet the requirements of quality and consistency.
The computational load of the effective region is reduced by 30%-60%, the inference speed is increased by 2-3 times, the memory usage is reduced, the structure restoration accuracy of the effective region is improved by 15%-25%, the consistency is improved by 20%, and the computational interference and iteration redundancy of the invalid region are avoided.
Smart Images

Figure CN121860872A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of image restoration technology, and in particular to an image restoration method based on a diffusion model using region masking and dynamic exit. Background Technology
[0002] The diffusion model, as a mainstream generative deep learning model, has been widely used in image restoration due to its powerful probabilistic modeling capabilities and high-quality image generation results. The core principle of the diffusion model is to gradually add Gaussian noise to the original image through "forward diffusion" until it reaches a completely random state, and then gradually remove noise from the random noise through "backward diffusion" to finally restore a clear image.
[0003] However, existing diffusion models have two major bottlenecks in image restoration tasks: First, the backdiffusion process requires dozens to hundreds of iterations, and each step requires performing UNet denoising calculations on the latent space vectors obtained by the VAE encoder for the entire image. However, in most image restoration scenarios, only some areas need fine denoising and restoration, while invalid areas such as the background have little noise and do not need restoration. Performing full UNet calculations on them would result in a large waste of computing power and a high computational cost. Second, existing diffusion models usually execute a fixed number of preset backdiffusion steps. However, some images or image regions have already achieved the required restoration effect in the middle of the iteration. Continuing to execute subsequent iterations will only increase the time cost and have a negligible improvement on the restoration quality, resulting in redundant iteration steps.
[0004] Therefore, there is an urgent need for a diffusion model image restoration method that can overcome the aforementioned defects. Summary of the Invention
[0005] To overcome the technical shortcomings of existing diffusion model image restoration methods, such as high computational overhead and redundant iteration steps, this invention provides a diffusion model image restoration method based on region masking and dynamic exit.
[0006] The image restoration method based on region masking and dynamic exit diffusion model provided by this invention includes the following steps:
[0007] S1. Preprocess the input image to obtain a preprocessed image;
[0008] S2. Process the preprocessed image in two paths:
[0009] One image is processed in pixel space by an edge detection algorithm to generate a single-channel edge map, and then the single-channel edge map is converted into a latent space vector by an encoder; another image is input to a spatial region mask generation submodule to generate a binary repair mask in pixel space, and then the binary repair mask is converted into a latent space mask aligned with the latent space vector.
[0010] S3. The latent space mask is transmitted in two paths:
[0011] One latent space mask is multiplied by the pure noise and then input into the UNet denoising network; the other latent space mask is concatenated with the latent space vector and then input into the ControlNet condition constraint module.
[0012] For the latent space vector of the effective region, the UNet denoising network and the ControlNet conditional constraint module are used to jointly restore it to obtain the constraint denoising vector. For the latent space vector of the invalid region, the latent space vector of the previous step is reused to obtain the reuse vector. Finally, the constraint denoising vector and the reuse vector are merged to obtain the latent vector of the current step.
[0013] S4. Evaluate the latent vector of the current step by dynamically exiting the module through backdiffusion:
[0014] If the restoration quality and consistency of the effective region meet the requirements, the iteration is terminated early.
[0015] If the restoration quality and / or consistency of the effective region does not meet the requirements, continue to the next iteration;
[0016] S5. Convert the latent vector after the iteration terminates into the final restored image using a decoder.
[0017] Furthermore, in step S1, the preprocessing includes image size adjustment, format adjustment, and pixel value normalization.
[0018] Furthermore, in step S2, the edge detection algorithm adopts the Canny edge detection algorithm.
[0019] Furthermore, in step S2, the binary mask is converted into the latent space mask by a downsampling operation and three 1×1 convolution operations.
[0020] Furthermore, in step S3, the UNet denoising network only restores the latent space vector of the effective region, while the latent space vector of the invalid region is reused from the previous step; the ControlNet condition constraint module only constrains the latent space vector of the effective region, while the latent space vector of the invalid region is directly set to zero.
[0021] Furthermore, in step S4, the reverse diffusion dynamic exit module adopts the SUEM module.
[0022] Further:
[0023] The SUEM module outputs a temporary restored image through a latent space upsampler;
[0024] Define a preset maximum number of steps as T. At the t-th step of the back diffusion, perform a judgment: if t=T, then terminate the iteration; if t<T, then calculate the restoration quality and consistency of the temporary restored image.
[0025] Further:
[0026] The consistency evaluator calculates the IoU value between the temporary restored image and the single-channel edge map. If the IoU value is greater than or equal to the preset value, the consistency is achieved.
[0027] The PSNR value of the temporary image and the single-channel edge map is calculated by the quality evaluator. If the PSNR value is greater than or equal to the preset value, the restoration quality meets the standard.
[0028] Furthermore, both the encoder and decoder adopt a VAE structure.
[0029] The technical solution provided by this invention has the following advantages compared with the prior art:
[0030] 1) The diffusion model image restoration method provided by this invention synchronously filters the processing regions of the UNet denoising network and the ControlNet conditional constraint module through latent space masking, and only performs collaborative computation in the effective regions. This can reduce the amount of back diffusion iteration computation by 30%-60% (depending on the proportion of invalid regions) while ensuring the restoration quality of the effective regions, and improve the inference speed by 2-3 times, while reducing memory usage. At the same time, the features of the previous step are directly reused in the invalid regions, and the conditional signals of the ControlNet conditional constraint module are set to zero in the invalid regions and do not participate in the constraint computation, thereby ensuring that the original effective features of the invalid regions are not interfered with by additional computation. The feature fidelity of the invalid regions is improved by 30%-40%, further reducing the amount of invalid computation, forming a double efficiency improvement with "collaborative optimization".
[0031] 2) The diffusion model image restoration method provided by this invention aligns the latent space mask with the latent space vector, which enables the structural constraints of the ControlNet condition constraint module and the denoising features of the UNet denoising network to be fully aligned in spatial position and channel dimension, improving the structural restoration accuracy of the effective region by 15%-25% and avoiding secondary distortion caused by constraint misalignment.
[0032] 3) The diffusion model image restoration method provided by this invention inputs the latent space mask into the UNet denoising network and the ControlNet conditional constraint module at the same time, which can ensure that the effective regions selected by the two are completely consistent. This makes the denoising process of the UNet denoising network and the constraint process of the ControlNet conditional constraint module strictly target the same effective region, and the constraint logic is self-consistent. This effectively avoids the structural chaos caused by the asynchronous denoising and constraint, thereby improving the consistency of the restored image by more than 20%.
[0033] 4) The diffusion model image restoration method provided by the present invention uses a reverse diffusion dynamic exit module to evaluate the latent vector of the current step. If the restoration quality and consistency meet the requirements, the iteration is terminated in advance, which can overcome the technical defect of redundant iteration steps. Attached Figure Description
[0034] The accompanying drawings, which are incorporated in and form part of this specification, illustrate embodiments consistent with the invention and, together with the description, serve to explain the principles of the invention.
[0035] To more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, for those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0036] Figure 1 This diagram illustrates the overall framework of the image restoration method in this embodiment of the invention.
[0037] Figure 2 A flowchart illustrating the spatial region mask generation submodule in this embodiment of the invention;
[0038] Figure 3 A schematic diagram illustrating the ControlNet condition constraint module in an embodiment of the present invention;
[0039] Figure 4 This is a schematic diagram illustrating the SUEM module in an embodiment of the present invention. Detailed Implementation
[0040] To better understand the above-mentioned objectives, features, and advantages of the present invention, the solutions of the present invention will be further described below. It should be noted that, unless otherwise specified, the embodiments of the present invention and the features thereof can be combined with each other.
[0041] Numerous specific details are set forth in the following description in order to provide a full understanding of the invention, but the invention may also be practiced in other ways different from those described herein; obviously, the embodiments in the specification are only some, and not all, of the embodiments of the invention.
[0042] The specific embodiments of the present invention will now be described in detail with reference to the accompanying drawings.
[0043] First, the hardware and software configuration on which this embodiment is based will be explained.
[0044] Core framework: PyTorch 2.0+, Diffusers library (Stable Diffusion v1.5 pre-trained model);
[0045] ControlNet configuration: Load pre-trained "edge-detection" weights for edge structure constraints;
[0046] Hardware requirements: GPU memory ≥ 8GB, supporting FP16 precision, such as RTX 3090; CPU ≥ 16 cores, for data preprocessing.
[0047] This embodiment provides a diffusion model image restoration method based on region masking and dynamic exit, including steps S1 to S5.
[0048] S1. Preprocess the input image to obtain a preprocessed image.
[0049] Specifically, preprocessing includes image resizing, format adjustment, and pixel value normalization. For example, in this embodiment, the input image is resized to 512×512 pixels to match the default input size of Stable Diffusion, saved in RGB format, and the pixel values are normalized to [0,1] to unify the image specifications.
[0050] S2. The preprocessed image is processed in two paths: one path is processed in pixel space by an edge detection algorithm to generate a single-channel edge map, and then the single-channel edge map is converted into a latent space vector by an encoder; the other path is input to the spatial region mask generation submodule to generate a binary repair mask in pixel space, and then the binary repair mask is converted into a latent space mask aligned with the latent space vector.
[0051] Specifically, the edge detection algorithm used in this embodiment is the Canny edge detection algorithm.
[0052] Specifically, the latent space vector in this embodiment has a size of 64×64×4, which matches the standard latent size of Stable Diffusion.
[0053] Specifically, the encoder in this embodiment adopts a VAE structure, which is used to compress the single-channel edge map from a pixel space size of 512×512×3 to a latent space vector size of 64×64×4. Through adaptation and adjustment, it is ensured that the dimension of the latent space vector output by the encoder is consistent with the input dimension of UNet and ControlNet, and a Stable Diffusion pre-trained VAE model is used.
[0054] It should be noted that the VAE encoder in Figure 1 The term ε is used to represent this structure, which is an existing mature structure and will not be elaborated upon here.
[0055] It should be noted that the spatial region mask generation submodule is in Figure 1 The submodule, denoted by M, represents an improved structure of this method. It addresses the redundant computation issue in the diffusion model decoding process by improving upon the original masking module. It adds collaborative logic with ControlNet to ensure that constraints only apply to valid regions, avoiding redundant constraints in invalid regions. The workflow of the improved spatial region masking generation submodule is as follows: Figure 2 As shown: The preprocessed image generates its binary repair mask in pixel space, where "1" represents the valid region and "0" represents the invalid region. Then, it is converted into a latent space mask, namely Mask_latent, after downsampling and three 1×1 convolution operations. Mask_latent is aligned with the latent space vector.
[0056] S3. The latent space mask is transmitted in two paths: one path multiplies the latent space mask with pure noise and inputs it into the UNet denoising network; the other path concatenates the latent space mask with the latent space vector and inputs it into the ControlNet conditional constraint module. For the latent space vector of the effective region, the UNet denoising network and the ControlNet conditional constraint module are used to collaboratively restore it to obtain the constrained denoising vector. For the latent space vector of the invalid region, the latent space vector of the previous step is reused to obtain the reused vector. Finally, the constrained denoising vector and the reused vector are merged to obtain the latent vector of the current step.
[0057] It should be noted that the UNet denoising network in Figure 1The term U represents a mature existing architecture, responsible for receiving the latent space mask and pure noise (Z). t The dot product result, diffusion step embedding, and output features of the ControlNet conditional constraint module are used to achieve constrained image restoration through multi-scale convolution and attention mechanism. Through adaptation and adjustment, a conditional feature fusion interface is added to each layer of the decoder of the UNet denoising network. The interface is used to receive the same-scale conditional features output by the ControlNet conditional constraint module in the form of channel splicing, without modifying the original convolution and attention layer structure of the UNet denoising network.
[0058] It should be noted that the ControlNet condition constraint module is in Figure 1 In this context, C represents a newly added structure in this method, which is the extracted latent space vector (conditional signal C). f The signal is concatenated with the latent space mask features and then fed into the ControlNet conditional constraint module. Internally, the ControlNet conditional constraint module first performs initial encoding of the signal using zero-convolution, initializing the convolution kernel weights to all zero values. Unlike traditional random weight initialization, this blank starting point ensures that the model does not introduce additional interference to the input data during the early stages of training, perfectly preserving the original system's operational characteristics. Figure 3 As shown, the ControlNet conditional constraint module contains a multi-scale encoder structure fully aligned with the UNet denoising network. It includes feature blocks of five scales, from SDEncoder Block A (64×64) to SD Middle Block (8×8), which constrain the UNet image restoration process by introducing structural conditional signals. A key design feature of the ControlNet conditional constraint module is that the conditional features must correspond one-to-one with each layer of the UNet denoising network's decoder, ensuring that each denoising layer is structurally constrained. This structural design guarantees collaborative restoration between ControlNet and UNet. ControlNet employs a low-rank adaptation (LoRA) and residual connection architecture, containing the same number of feature processing layers as UNet. The input to each decoder layer is the spatial conditional features of the corresponding scale and the original features of that UNet layer. The fusion process uses both residual connections and weighted fusion methods, with the fusion weight set to 0.7, which can be adjusted according to different image restoration tasks.
[0059] It is important to note that the ControlNet condition constraint module and the UNet denoising network share the output of the TimeEncoder, ensuring that the information of the "backward diffusion time step" is fully synchronized during the condition constraint and denoising processes, allowing the strength of the structural constraints to dynamically adapt with the diffusion steps.
[0060] like Figure 2As shown, the newly adapted Mask_latent is simultaneously output to both the UNet denoising network and the ControlNet conditional constraint module, ensuring that the effective regions selected by both are completely consistent. During the UNet denoising network's processing, only the latent space vectors of effective regions with Mask_latent=1 are fed into the UNet denoising network and ControlNet conditional constraint module for collaborative restoration, while the latent space vectors of invalid regions with Mask_latent=0 are reused from the previous step. During the ControlNet conditional constraint module's processing, only the latent space vectors of effective regions with Mask_latent=1 participate in constraint feature generation, while the latent space vectors of invalid regions with Mask_latent=0 are directly set to zero, avoiding unnecessary structural constraints on regions that do not need repair. Merging the constraint denoising vectors of the effective regions and the reused vectors of the invalid regions yields the complete latent vector for the current step.
[0061] S4. Evaluate the latent vector of the current step through the backdiffusion dynamic exit module: if the restoration quality and consistency of the effective region meet the requirements, terminate the iteration early; if the restoration quality and / or consistency of the effective region do not meet the requirements, continue to the next iteration.
[0062] Specifically, the reverse diffusion dynamic exit module in this embodiment adopts the SUEM module.
[0063] It should be noted that the SUEM module is in Figure 1 The SUEM module, denoted by S, represents an improved structure. Based on the core logic of real-time evaluation and dynamic exit of the original UEM module, it adds an evaluation dimension of "structural consistency" to ensure that not only does the restoration quality meet the standards, but the structural integrity also satisfies the requirements when exiting prematurely. The structure of the SUEM module is as follows: Figure 4 As shown, the SUEM module outputs a temporary restored image Img_temp through a latent space upsampler (two-layer transposed convolutional structure); a new consistency evaluator is added, using the "edge matching degree" index, to evaluate the consistency of the image structure by calculating the IoU value between the temporary restored image and the single-channel edge map. If the IoU value is ≥ a preset value (set to 0.8 in this embodiment), the consistency is met; the original quality evaluator calculates the PSNR value between the temporary image and the single-channel edge map. If the PSNR value is ≥ a preset value, the restoration quality is met. If the preset maximum number of steps is defined as T, at the t-th step of back diffusion, a judgment is performed: if t = T, the iteration is terminated; if t < T, the restoration quality and consistency of the temporary restored image are calculated; if both consistency and restoration quality meet the requirements, the iteration is terminated early; if the restoration quality and / or consistency of the effective region do not meet the requirements, the next iteration continues.
[0064] S5. Convert the latent vector after the iteration terminates into the final restored image using a decoder.
[0065] Specifically, the decoder in this embodiment adopts a VAE structure, which is used to decode latent space vectors into pixel space images.
[0066] It should be noted that the VAE decoder in Figure 1 The term D is used to represent this, and it belongs to the existing mature structure, so it will not be elaborated on here.
[0067] This method is based on an extended architecture of "VAE encoding / decoding, UNet denoising network, and ControlNet conditional constraint module". While retaining the core functions of the existing VAE encoder, VAE decoder, and UNet denoising network, it innovatively integrates the spatial region mask generation submodule and the back diffusion dynamic exit module with the ControlNet conditional constraint module to form a triple optimization architecture of "conditional constraint to preserve structure + spatial redundancy reduction + time step reduction". This ensures that image restoration satisfies both structural consistency and high-efficiency acceleration.
[0068] The above are merely specific embodiments of the present invention, enabling those skilled in the art to understand or implement the present invention. Although detailed descriptions have been provided with reference to the foregoing embodiments, those skilled in the art should understand that modifications can still be made to the technical solutions described in the foregoing embodiments, or equivalent substitutions can be made to some or all of the technical features therein; and these modifications or substitutions do not cause the essence of the corresponding technical solutions to deviate from the scope of the technical solutions of the embodiments, and they should all be covered within the protection scope of the claims.
Claims
1. An image restoration method based on a diffusion model using region masking and dynamic exit, characterized in that, Includes the following steps: S1. Preprocess the input image to obtain a preprocessed image; S2. Process the preprocessed image in two paths: One image is processed in pixel space by an edge detection algorithm to generate a single-channel edge map, and then the single-channel edge map is converted into a latent space vector by an encoder; another image is input to a spatial region mask generation submodule to generate a binary repair mask in pixel space, and then the binary repair mask is converted into a latent space mask aligned with the latent space vector. S3. The latent space mask is transmitted in two paths: One latent space mask is multiplied by the pure noise and then input into the UNet denoising network; the other latent space mask is concatenated with the latent space vector and then input into the ControlNet condition constraint module. For the latent space vector of the effective region, the UNet denoising network and the ControlNet conditional constraint module are used to jointly restore it to obtain the constraint denoising vector. For the latent space vector of the invalid region, the latent space vector of the previous step is reused to obtain the reuse vector. Finally, the constraint denoising vector and the reuse vector are merged to obtain the latent vector of the current step. S4. Evaluate the latent vector of the current step by dynamically exiting the module through backdiffusion: If the restoration quality and consistency of the effective region meet the requirements, the iteration is terminated early. If the restoration quality and / or consistency of the effective region does not meet the requirements, continue to the next iteration; S5. Convert the latent vector after the iteration terminates into the final restored image using a decoder.
2. The image restoration method based on region masking and dynamic exit diffusion model according to claim 1, characterized in that, In step S1, the preprocessing includes image size adjustment, format adjustment, and pixel value normalization.
3. The image restoration method based on region masking and dynamic exit diffusion model according to claim 1, characterized in that, In step S2, the edge detection algorithm used is the Canny edge detection algorithm.
4. The image restoration method based on region masking and dynamic exit diffusion model according to claim 1, characterized in that, In step S2, the binary mask is converted into the latent space mask by a downsampling operation and three 1×1 convolution operations.
5. The image restoration method based on region masking and dynamic exit diffusion model according to claim 1, characterized in that, In step S3, the UNet denoising network restores the latent space vector of the effective region only, while the latent space vector of the invalid region is reused from the previous step; the ControlNet condition constraint module only constrains the latent space vector of the effective region, while the latent space vector of the invalid region is directly set to zero.
6. The image restoration method based on region masking and dynamic exit diffusion model according to claim 1, characterized in that, In step S4, the reverse diffusion dynamic exit module adopts the SUEM module.
7. The image restoration method based on region masking and dynamic exit diffusion model according to claim 6, characterized in that: The SUEM module outputs a temporary restored image through a latent space upsampler; Define a preset maximum number of steps as T. At the t-th step of the back diffusion, perform a judgment: if t=T, then terminate the iteration; if t<T, then calculate the restoration quality and consistency of the temporary restored image.
8. The image restoration method based on region masking and dynamic exit diffusion model according to claim 7, characterized in that: The consistency evaluator calculates the IoU value between the temporary restored image and the single-channel edge map. If the IoU value is greater than or equal to the preset value, the consistency is achieved. The PSNR value of the temporary image and the single-channel edge map is calculated by the quality evaluator. If the PSNR value is greater than or equal to the preset value, the restoration quality meets the standard.
9. The image restoration method based on region masking and dynamic exit diffusion model according to claim 1, characterized in that, Both the encoder and decoder adopt a VAE structure.
Citation Information
Patent Citations
Hybrid degraded image restoration method based on joint conditional diffusion model
CN118864291A
Fingerprint image restoration method based on conditional diffusion probability model
CN119784628A
Universal image restoration method and system based on double-space joint diffusion
CN120495139A
Sketch guide image generation method and system based on stable diffusion model
CN120525985A
Image super-resolution reconstruction method and device for fusing image restoration and rapid diffusion
CN120876232A
Cited By
A method and related device for recovering a severe weather degraded image based on a texture enhanced diffusion model
CN122510128A