Method for generating image based on cross attention mechanism layout condition
By introducing the cross-attention mechanism and layout loss function into the image generation model, the deficiencies of the image generation model in controlling image layout are solved, the quality and consistency of the generated images are improved, and precise control of the layout and semantic coherence are achieved. , and low cost, suitable for integration of existing technologies.
Patent Information
- Application Number
- CN202410871990.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-07-01
- Publication Date
- 2024-09-20
AI Technical Summary
Existing image generation models are difficult to effectively control image layout when generating images that conform to given text prompts, especially in complex scenes and tasks in professional fields, resulting in insufficient quality and layout consistency of generated images.
An image generation method based on layout conditions based on the cross-attention mechanism is adopted. By introducing internal enhancement loss, external weakening loss and centroid loss function, the attention map is adjusted during the sampling process to accurately control the object position and ensure the consistency of the generated image with the preset layout. .
It improves the layout control accuracy of the image generation model, maintains semantic coherence and logical consistency, and does not require additional fine-tuning and pairing data. The cost is lower and it has a plug-and-play effect.
Smart Images

Figure CN118674808A
Abstract
Description
Technical Field
[0001] The present invention belongs to the technical field of image generation, and specifically relates to a method for generating images based on a cross-attention mechanism layout condition, which is a method for calculating a loss function under a cross-attention mechanism during sampling to ensure the consistency of a generated image with a preset layout. Background Art
[0002] Controllable image generation has multiple meanings in the fields of computer vision and artificial intelligence. It provides users with the opportunity for personalized customization, so that the generated images can better meet the needs and interests of different users and achieve more flexible applications.
[0003] A layout image is an image in which the layout information is a crucial part of the image. Especially for image generation tasks in complex scenes and professional fields, it is difficult to describe the precise location of objects using text, while bounding boxes / keypoints can easily achieve this. By considering the layout, the adaptability of the generated model can be improved, enabling it to better understand and capture the relationship between elements in the image, and improve the quality of the generated image.
[0004] While there are conditional diffusion models and GANs (Generative Adversarial Nets) that accept other input modalities for inpainting, layout-to-image generation, etc., they rarely combine these inputs for controllable text-to-image generation. Although revolutionary, current state-of-the-art diffusion models can still fail in generating images that fully convey the semantics of a given text prompt. Therefore, in the task of image generation for layout generation, it is crucial to achieve effective manipulation of image layout without increasing training cost. Summary of the invention
[0005] The purpose of the present invention is to provide a method for generating images based on the layout conditions of the cross-attention mechanism. Three innovative layout loss functions are proposed, namely internal reinforcement loss, external weakening loss and centroid loss. By refocusing the attention map according to the given layout during the sampling process, the position placement of the object is effectively controlled.
[0006] To achieve the above object, the technical solution adopted by the present invention is:
[0007] A method for generating an image based on a cross-attention mechanism layout condition, the method for generating an image based on a cross-attention mechanism layout condition comprising:
[0008] Step S1, obtaining input conditional text, a layout box set and phrase objects corresponding to the layout boxes, extracting the positions of the phrase objects in the conditional text, and encoding the conditional text and the current time to obtain a text embedding vector and a time embedding vector respectively;
[0009] Step S2, obtaining the potential vector at the current moment, inputting the potential vector, the text embedding vector and the time embedding vector into the denoising network, obtaining the noise at the current moment output by the denoising network and the cross attention matrix in the denoising process;
[0010] Step S3, using the output cross attention matrix to respectively calculate the internal reinforcement loss, external weakening loss and centroid loss of the phrase object layout box to obtain the overall loss;
[0011] Step S4: if the overall loss is greater than a preset threshold and the current time is less than a set time threshold, the denoising network is updated according to the overall loss, and the potential vector at the current time is updated, and the noise at the current time is used to denoise the updated potential vector at the current time to obtain the potential vector at the next time; otherwise, the noise at the current time is used to denoise the potential vector at the current time to obtain the potential vector at the next time;
[0012] Step S5: If the next moment is not the final moment, the latent vector of the next moment is used as the latent vector of the current moment, and the time embedding vector is updated and then the process returns to step S2; otherwise, the latent vector of the next moment is converted back into an image to obtain an image that conforms to the given layout.
[0013] Several optional methods are also provided below, but they are not intended to be additional limitations on the above-mentioned overall solution, but are merely further supplements or preferences. Under the premise that there are no technical or logical contradictions, each optional method can be combined with the above-mentioned overall solution separately, and multiple optional methods can also be combined.
[0014] Preferably, the denoising network comprises a first cross-attention downsampling layer, a second cross-attention downsampling layer, a third cross-attention downsampling layer, a downsampling layer, a cross-attention intermediate layer, an upsampling layer, a first cross-attention upsampling layer, a second cross-attention upsampling layer and a third cross-attention downsampling layer;
[0015] The input of the first cross-attention downsampling layer includes the potential vector, text embedding vector and time embedding vector at the current moment, and the output is feature D1;
[0016] The input of the second cross-attention downsampling layer includes feature D1, text embedding vector and time embedding vector, and the output is feature D2;
[0017] The input of the third cross-attention downsampling layer includes feature D2, text embedding vector and time embedding vector, and the output is feature D3;
[0018] The input of the downsampling layer includes feature D3 and a time embedding vector, and the output is feature D4;
[0019] The input of the cross attention intermediate layer includes feature D4, text embedding vector and time embedding vector, and the output is feature M1;
[0020] The input of the upsampling layer includes feature M1 and a time embedding vector, and the output is feature U1;
[0021] The input of the first cross-attention upsampling layer includes feature U1, text embedding vector, time embedding vector and feature D3, and the output is feature U2;
[0022] The input of the second cross-attention upsampling layer includes feature U2, text embedding vector, time embedding vector and feature D2, and the output is feature U3;
[0023] The input of the third cross-attention upsampling layer includes feature U3, text embedding vector, time embedding vector and feature D1, and the output is the noise at the current moment.
[0024] Preferably, the structures of the first cross-attention downsampling layer, the second cross-attention downsampling layer, and the third cross-attention downsampling layer include a 2D residual module, a 2D Transformer module, a 2D residual module, a 2D Transformer module, and a 2D downsampling layer connected in sequence, the latent vector or feature is used as the input of the first 2D residual module, the text embedding vector is respectively input to the two 2D Transformer modules, and the time embedding vector is respectively input to the two 2D residual modules;
[0025] The structure of the downsampling layer includes two 2D residual modules connected in sequence, the feature D3 is used as the input of the first 2D residual module, and the time embedding vector is input into the two 2D residual modules respectively;
[0026] The structure of the cross-attention intermediate layer includes a 2D residual module, a 2D Transformer module and a 2D residual module connected in sequence, the feature D4 is used as the input of the first 2D residual module, the text embedding vector is input to the 2D Transformer module, and the time embedding vector is input to the two 2D residual modules respectively;
[0027] The upsampling layer includes a 2D residual module, a 2D residual module, a 2D residual module and a 2D upsampling layer connected in sequence, the feature M1 is used as the input of the first 2D residual module, and the time embedding vector is spliced with the feature D3 output by the third cross-attention downsampling layer and input into the first 2D residual module, the time embedding vector is spliced with the output of the first 2D residual module in the downsampling layer and input into the second 2D residual module, and the time embedding vector is spliced with the feature D4 output by the downsampling layer and input into the third 2D residual module;
[0028] The structures of the first cross-attention upsampling layer, the second cross-attention upsampling layer and the third cross-attention downsampling layer include a 2D residual module, a 2D Transformoer module, a 2D residual module, a 2D Transformoer module, a 2D residual module, a 2D Transformoer module and a 2D upsampling layer. The concatenation of feature U1 and feature D3 or the concatenation of feature U2 and feature D2 or the concatenation of feature U3 and feature D1 serves as the input of the first 2D residual module of the corresponding cross-attention upsampling layer, the text embedding vector is input into the three 2D Transformoer modules respectively, and the time embedding vector is input into the three 2D residual modules respectively.
[0029] Preferably, the cross-attention matrix in the denoising process is the normalized dot product attention matrix obtained by the first cross-attention downsampling layer, the second cross-attention downsampling layer, the third cross-attention downsampling layer, the cross-attention intermediate layer, the first cross-attention upsampling layer, the second cross-attention upsampling layer and the third cross-attention downsampling layer in the denoising process.
[0030] Preferably, the output cross attention matrix is used to respectively calculate the internal reinforcement loss, external weakening loss and centroid loss of the phrase object layout box to obtain the overall loss, including:
[0031] The internal strengthening loss L s The calculation formula is as follows:
[0032]
[0033] L s =L c +βL res
[0034] Among them, L c represents the layout loss corresponding to each phrase object, A (γ) represents the cross attention matrix of the γth layer of the denoising network output, γ represents the selected preset layer, B represents the layout box, i represents the position of the phrase object in the conditional text, u represents the pixel position, represents the attention value at pixel position u in the attention map corresponding to position i in the cross attention matrix of the γth layer, B global represents the union of all layout boxes, n represents the number of layout boxes, j represents the subscript of the layout box, B j represents the jth layout box, L res represents the residual attention loss, represents the attention value at pixel position u in the residual attention map in the cross-attention matrix of the γth layer, and β represents the residual attention loss L res The weight, L s represents internal reinforcement loss;
[0035] The external weakening loss L w The calculation formula is as follows:
[0036]
[0037] Where B′ represents the layout box B j The complement of ;
[0038] The centroid loss L cen The calculation formula is as follows:
[0039]
[0040] Where centroid(obj,i) represents the object centroid position calculated by the cross attention matrix corresponding to the position i of the phrase object, h represents the height of the cross attention matrix, w represents the width of the cross attention matrix, and A h,w,i represents the attention value of the cross attention matrix corresponding to the position i of the phrase object at position (h, w), centroid(B,i) represents the centroid of the layout box corresponding to the position i of the phrase object, Indicates the horizontal left endpoint of the layout box, that is, the upper left corner. Indicates the horizontal right endpoint of the layout box, that is, the upper right corner. Indicates the vertical right endpoint of the layout box, that is, the lower right corner. Indicates the vertical left endpoint of the layout box, that is, the lower left corner, c j represents the object centroid position calculated by the phrase object corresponding to the j-th layout box, b j represents the centroid of the layout box calculated by the phrase object corresponding to the j-th layout box;
[0041] The overall loss calculation formula is as follows:
[0042] L=L s +η1L w +η2L cen
[0043] Among them, L is the overall loss, η1 is the external weakening loss L w The weight of η2 is the centroid loss L cen The weight of .
[0044] Preferably, the updating of the potential vector at the current moment includes:
[0045]
[0046] Among them, z t ′ is the updated potential vector at the current moment, z t is the potential vector at the current moment, α represents the loss guidance weight, Represents the overall loss function L with respect to parameter z t The gradient of the overall loss function L at z t is the direction of maximum growth, and L is the overall loss.
[0047] Preferably, the latent vector at the initial moment is obtained by randomly sampling from a normal distribution N(0,1) to obtain the latent vector at the initial moment.
[0048] The method for generating images based on the layout condition of the cross attention mechanism provided by the present invention has the following beneficial effects compared with the prior art:
[0049] (1) The centroid loss is added to the overall loss, and the centroid control is introduced to control the position of objects in the image more accurately; the residual attention loss is added to the overall loss to maintain the semantic coherence and logical consistency.
[0050] (2) During the iteration of the denoising network, the latent vector is adjusted based on the vector update formula, without the need for additional fine-tuning and paired layout-image data, thus ensuring the original generation capability of the model at a lower cost.
[0051] (3) The method of the present invention has a plug-and-play effect, and the speed of generating images that conform to the layout will not be slowed down too much, and can be easily integrated into the current existing solutions. BRIEF DESCRIPTION OF THE DRAWINGS
[0052] Figure 1 An architecture diagram of a method for generating images based on a cross-attention mechanism layout condition in the present invention;
[0053] Figure 2 It is a denoising network structure diagram of the Unet architecture of the present invention;
[0054] Figure 3 Schematic diagram of the structure of the cross-attention downsampling layer of the present invention;
[0055] Figure 4 Schematic diagram of the structure of the downsampling layer of the present invention;
[0056] Figure 5 It is a schematic diagram of the structure of the cross-attention intermediate layer of the present invention;
[0057] Figure 6 Schematic diagram of the structure of the upsampling layer of the present invention;
[0058] Figure 7 Schematic diagram of the structure of the cross-attention upsampling layer of the present invention;
[0059] Figure 8 Schematic diagram of the structure of the 2D residual module of the present invention;
[0060] Fig. 9 It is a structural schematic diagram of the 2D Transformer module of the present invention;
[0061] Fig.10 It is a structural diagram of the Basic Transformer module of the present invention;
[0062] Fig.11 Schematic diagram of the structure of the feedforward network layer of the present invention;
[0063] Fig.12 Schematic diagram of the structure of the 2D upsampling layer of the present invention;
[0064] Fig.13 A schematic diagram of calculating cross attention for the present invention;
[0065] Fig.14 Schematic diagram of decoding a latent vector into an image module according to the present invention. DETAILED DESCRIPTION
[0066] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are only 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.
[0067] Unless otherwise defined, all technical and scientific terms used herein have the same meaning as those commonly understood by those skilled in the art of the present invention. The terms used in the specification of the present invention herein are only for the purpose of describing specific embodiments and are not intended to limit the present invention.
[0068] Specifically, Figure 1As shown, the method for generating images based on the layout conditions of the cross-attention mechanism in this embodiment is a layout generation method for generating images based on the semantic space attention mechanism, which can achieve fine positioning of the target object in the text-to-image synthesis process. It includes the following steps:
[0069] Step S1, obtain the conditional text and the corresponding layout, and encode the text. This step mainly requires matching the input layout with the object in the conditional text in order to subsequently extract the attention map corresponding to the object.
[0070] Step S11, obtaining an input conditional text prompt, where the conditional text prompt is composed of English word text. In this embodiment, the input conditional text prompt "A dog is playing with a ball" is taken as an example.
[0071] Step S12: Obtain a local frame set consisting of two input layout frames (this embodiment takes two layout frames as an example, but the input layout frames are not limited to only two, and the actual input shall prevail), recorded as Each layout box B j It is composed of four coordinates (left-upper-right-lower), recorded as Indicates the horizontal left endpoint of layout box j, that is, the upper left corner. Indicates the horizontal right endpoint of layout box j, that is, the upper right corner. Indicates the vertical right endpoint of layout box j, that is, the lower right corner. Indicates the vertical left endpoint of the layout box j, that is, the lower left corner. In this embodiment, B1 = {0.31, 0.29, 0.90, 0.70}, B2 = {0.47, 0.06, 0.62, 0.21}.
[0072] Step S13, obtaining the phrase object phase corresponding to the layout frame, which is "dog; ball" in this embodiment, wherein the layout frame corresponding to dog is B1, and the layout frame corresponding to "ball" is B2, which are character objects stored in the list.
[0073] For a given layout box and a given conditional text prompt, we need to extract the position of the phrase object phase in the conditional text prompt, denoted as p. In the example, the position of "dog" in the conditional text "A dog is playing with a ball" is 2, and the position of "ball" in the conditional text "Adog is playing with a ball" is 7, so the value of p should be [2,7]. p contains position information, which is used to determine the position of the word phrase of the object whose position needs to be controlled in the text, so as to facilitate the subsequent extraction of the cross-attention map related to the phrase object characters.
[0074] Step S14: Encode the input conditional text prompt. Use the pre-trained CLP ViT-L / 14 text encoder to convert the conditional text prompt into a text embedding vector c (also called text encoding text_embedding), which is used as the conditional embedding of the SpatialTransformer module in the Unet architecture. The size of the text embedding vector c is 1×77×768.
[0075] Step S15: Encode the current time t to obtain a time embedding vector t embedding (Also called time encoding time_embedding).
[0076] The time t dimension is 981, and the sinusoidal position encoding of the encoding module is as follows: Figure 2 As shown in the lower right, the dimension becomes 1×320. Two consecutive linear transformation layers are then used to process the encoded high-dimensional temporal features. Finally, the fully characterized temporal embedding vector t is obtained. embedding , the dimension is 1×1280. Time code t embedding It plays a role in indicating the noise intensity in the process of predicting noise in the diffusion model.
[0077] Step S2: Randomly sample from the normal distribution N(0,1) to obtain the potential vector z at the initial moment T , which is then fed into the denoising network and outputs the cross attention matrix of the Unet architecture during the denoising process. T is the initialization potential vector of the process, which is also a pure noise image. The structure of the denoising network is as follows Figure 2 shown.
[0078] Step S21: Randomly sample from the normal distribution N(0,1). Specifically, the torch.randn function in the PyTorch library is used to generate a random tensor that conforms to the standard normal distribution (Gaussian distribution). This random tensor is pure noise z T , where z T The size of is 1×4×64×64, and the subscript T represents the sampling time, which can also be called the potential vector at time T. T , the time embedding vector t embesding and text embedding vector c as input, and are sent to the trained Unet architecture to prepare Z T Denoising is performed to obtain the potential vector z at time T-1 (i.e. the next time). T-1 .
[0079] Step S22: the potential vector z at the current time t∈(T,0) (i.e., in this embodiment, time T is the initial time and time 0 is the final time) T The size of is [1,4,64,64]. After a convolutional layer, the size becomes [1,320,64,64]. Figure 2 The downsampling module in (including Figure 2 The cross-attention downsampling layer-1 (also called the first cross-attention downsampling layer), cross-attention downsampling layer-2 (also called the second cross-attention downsampling layer), cross-attention downsampling layer-3 (also called the third cross-attention downsampling layer) and downsampling layer-4 (referred to as downsampling layer) in the cross-attention middle layer and upsampling module (upsampling layer-1 (referred to as upsampling layer), cross-attention upsampling layer-2 (also called the first cross-attention upsampling layer), cross-attention upsampling layer-3 (also called the second cross-attention upsampling layer), cross-attention upsampling layer-4 (also called the third cross-attention upsampling layer)) are used to obtain the noise at the current time t.
[0080] The input of the cross-attention downsampling layer-1 includes the current potential vector, text embedding vector and time embedding vector, and the output is feature D1. The structure of the cross-attention downsampling layer-1 is as follows Figure 3 As shown, it includes a 2D residual module, a 2D Transformer module, a 2D residual module, a 2D Transformer module and a 2D downsampling layer connected in sequence. The potential vector or feature is used as the input of the first 2D residual module, the text embedding vector is input to the two 2D Transformer modules respectively, and the time embedding vector is input to the two 2D residual modules respectively. The last layer of the cross attention downsampling layer-1 is a 2D downsampling layer, which uses a convolution layer with a step size of 2 and a convolution kernel size of 3×3 for downsampling. The output feature size D1 is adjusted to half of the original size, which is 1×320×32×32, where the normalized dot product attention matrix is calculated
[0081] The input of the cross-attention downsampling layer-2 includes feature D1, text embedding vector and time embedding vector, and the output is feature D2. The structure of the cross-attention downsampling layer-2 is the same as that of the cross-attention downsampling layer-1, which will not be repeated here. The feature D1 output by the first cross-attention downsampling layer is input to the second downsampling module, which is the cross-attention downsampling layer-2. This layer also contains the BasicTransformerBlock module to realize the interaction between image features and text information, in which the normalized dot product attention matrix is calculated. At the end of the cross-attention downsampling layer-2, a convolutional layer with a stride of 2 is also used to perform downsampling technology, and the output feature D2 has a size of 1×640×16×16.
[0082] The input of the cross-attention downsampling layer-3 includes feature D2, text embedding vector and time embedding vector, and the output is feature D3. The structure of the cross-attention downsampling layer-3 is the same as that of the cross-attention downsampling layer-1, and will not be repeated here. The feature D2 output by the second cross-attention downsampling layer is input to the third downsampling module, which is the cross-attention downsampling layer-3. This layer also implements an operation process similar to the first two downsampling layers. The output feature D3 has a size of 1×1280×8×8. In this process, the output of the cross-attention layer of this layer is also obtained, which is recorded as
[0083] The input of downsampling layer-4 includes feature D3 and time embedding vector, and the output is feature D4. Figure 4 As shown, the structure of downsampling layer-4 includes two 2D residual modules connected in sequence, feature D3 is used as the input of the first 2D residual module, and the time embedding vector is input into the two 2D residual modules respectively. In this embodiment, feature D3 output by the third cross-attention downsampling layer is input into the fourth downsampling module. This module is downsampling layer-4. This layer does not integrate the text and image feature fusion mechanism, but only consists of two consecutive 2D residual modules. It accepts two input signals: one is derived from the output of the previous layer (here is feature D3, and the other is derived from the time embedding vector t generated by the time step encoding module. embedding The output is feature D4, and its size is consistent with the input, which is 1×1280×8×8.
[0084] The input of the cross-attention intermediate layer includes feature D4, text embedding vector and time embedding vector, and the output is feature M1. Figure 5 As shown, the structure of the cross-attention intermediate layer includes a 2D residual module, a 2D Transformer module and a 2D residual module connected in sequence. The feature D4 is used as the input of the first 2D residual module, the text embedding vector is input into the 2D Transformer module, and the time embedding vector is input into the two 2D residual modules respectively. In this embodiment, the feature D4 is input into the cross-attention intermediate layer for processing. Specifically, D4 first interacts with the time feature through the ResnetBlock2D module (i.e., the 2D residual module). Subsequently, the processed features enter the 2D Transformer module to interact with the text encoding. The core operation of this module is to calculate the cross-attention between the image features and the text features, and output the cross-attention matrix of this layer. Finally, the output of the 2D Transformer module is input into the 2D residual module to output feature M1, and the size is maintained at 1×1280×8×8.
[0085] The input of upsampling layer-1 includes feature M1 and time embedding vector, and the output is feature U1. Figure 6 As shown, the upsampling layer-1 includes a 2D residual module, a 2D residual module, a 2D residual module and a 2D upsampling layer connected in sequence, and the feature M1 is used as the input of the first 2D residual module. At the same time, the temporal embedding vector is spliced with the feature D3 output by the third cross-attention downsampling layer and input into the first 2D residual module, the temporal embedding vector is spliced with the output of the first 2D residual module in the downsampling layer and input into the second 2D residual module, and the temporal embedding vector is spliced with the feature D4 output by the downsampling layer and input into the third 2D residual module. In this embodiment, the feature M1 is first input into the upsampling layer-1 to obtain the output feature U1, which is resized to 1×1280×16×16.
[0086] The input of the cross-attention upsampling layer-2 includes feature U1, text embedding vector, time embedding vector and feature D3, and the output is feature U2. Figure 7 As shown, the structure of the cross-attention upsampling layer-2 includes a 2D residual module, a 2D transformer module, a 2D residual module, a 2D transformer module, a 2D residual module, a 2D transformer module and a 2D upsampling layer. The features U1 and D3 are concatenated as the input of the first 2D residual module. The text embedding vectors are respectively input into the three 2D transformer modules, and the time embedding vectors are respectively input into the three 2D residual modules. In this embodiment, the feature U1 is input into the cross-attention upsampling layer-2. The cross-attention upsampling layer-2 receives two inputs, U1 and the output feature D3 of the cross-attention downsampling layer-3, and outputs the feature U2. The size becomes 1×1280×32×32, and the cross-attention matrix is obtained at the same time.
[0087] The input of the cross-attention upsampling layer-3 includes feature U2, text embedding vector, time embedding vector and feature D2, and the output is feature U3. The structure of the cross-attention upsampling layer-3 is the same as that of the cross-attention upsampling layer-2, which will not be repeated here. The input of the first 2D residual module of the cross-attention upsampling layer-3 is the concatenation of feature U2 and feature D2. In this embodiment, feature U2 is input into the cross-attention upsampling layer-3. The cross-attention upsampling layer-2 receives two inputs, feature U2 and the output feature D2 of the cross-attention downsampling layer-2, and outputs feature U3. The size becomes 1×640×64×64, and the cross-attention matrix is obtained at the same time.
[0088] The input of the cross-attention upsampling layer-4 includes feature U3, text embedding vector, time embedding vector and feature D1, and the output is the noise at the current moment. The structure of the cross-attention upsampling layer-4 is the same as that of the cross-attention upsampling layer-2, which will not be repeated here. The input of the first 2D residual module of the cross-attention upsampling layer-3 is the concatenated feature of feature U3 and feature D1. In this embodiment, feature U3 is input into the cross-attention upsampling layer-4. The cross-attention upsampling layer-4 receives two inputs, feature U3 and the output feature D1 of the cross-attention downsampling layer-1, and outputs the noise predicted by the model at time t, with a dimension of 1×320×64×64, and the cross-attention matrix is obtained at the same time.
[0089] In the process of performing the denoising operation, this embodiment will simultaneously calculate and save the value of the cross attention matrix, and ensure that the function returns the result including the output of the cross attention matrix after the softmax transformation. For example, the structure of the 2D residual module is as follows Figure 8 As shown, it includes a group normalization layer, a Sigmoid gated linear unit and a 3*3 convolution layer that receive input, as well as a linear layer, a Sigmoid gated linear unit and a dimension expansion layer that receive a time embedding vector. The outputs of the 3*3 convolution layer and the dimension expansion layer are concatenated and input into a new group normalization layer, a Sigmoid gated linear unit and a 3*3 convolution layer, and the second 3*3 convolution layer is concatenated with the input as the output of the 2D residual module.
[0090] In addition, the structure of the 2DTransformoer module is as follows Fig. 9 As shown, it includes a group normalization layer, a 1*1 convolution layer, a permute and reshape operation layer, a BasicTransformer module, a permute and reshape operation layer and a 1*1 convolution layer connected in sequence, wherein the text embedding vector is input into the BasicTransformer module, and the output of the second 1*1 convolution layer is concatenated with the input as the output of the 2DTransformoer module.
[0091] The BasicTransformer module contains a cross-attention module. The specific structure is as follows Fig.10As shown. It includes a layer normalization module, a self-attention module, a normalization module, a cross-attention module, a normalization module and a feedforward network layer connected in sequence from input to output, and the input of the BasicTransformer module is concatenated with the output of the self-attention module as the input of the second layer normalization module, the input of the second layer normalization module is concatenated with the output of the cross-attention module as the input of the third layer normalization module, and the input of the third layer normalization module is concatenated with the output of the feedforward network layer as the output of the BasicTransformer module.
[0092] The structure of the feedforward network layer is as follows Fig.11 As shown in Figure 1, from the input side to the output side, it includes a linear layer, a GELU activation layer, a Dropout layer, a linear layer, and a Dropout layer connected in sequence. The structure of the upsampling layer-1 and the 2D upsampling layer in the three cross-attention upsampling layers is shown in Figure 1. Fig.12 As shown, the upsampling operation is completed by including a nearest neighbor interpolation layer and a 3*3 convolution layer.
[0093] like Fig.13 As shown, the text embedding vector c is calculated and Fig.10 The cross attention between image features obtained after the self-attention module and layer normalization. The image feature hidden_states is linearly projected to obtain the query Q, and the text embedding vector c is linearly projected to obtain the key K and value V. The formula for calculating the cross attention is as follows:
[0094]
[0095] In this process, a normalized dot product attention matrix is calculated Its dimension is 8×4096×77, recorded as This matrix represents the attention output of the γ1th layer at time step t and is used to calculate the subsequent loss function. Fig.13 Some criss-cross attention maps are visualized in Figure 1. The output of the criss-cross attention layer of the first criss-cross attention downsampling layer has a size of 1×4096×320 and is added to the result of the previous layer through a residual connection. Then, layer normalization is performed on the combined result and fed into the feedforward network to obtain the output of the BasicTransformer module, which remains at 1×4096×320. Subsequently, the output is reshaped and permuted, and after a convolution layer with a convolution kernel of 1×1, the final output of the 2DTransformer module is 1×320×64×64.
[0096] Step S3: Use the output cross attention matrix to calculate the internal reinforcement loss L of the object layout boxs , External weakening loss L w and the centroid loss L cen , get the overall loss, and update the latent vector.
[0097] Step S31: Obtain the cross attention matrix of the above output and After that, keep the result after softmax of each cross attention matrix and perform reshape operation, recorded as A t .
[0098] Step S32: Calculate the loss function L c and L res For each object that the user expects to be placed in a specified position, in this embodiment, "dog" and "ball", there is already p representing their positions in the conditional text, and p is [2,7]. t Extract the attention map corresponding to p γ represents the selected preset layer. Usually, deep cross attention is richer in position information. For example, this embodiment selects the γ3, γ4 and γ5 layers, that is, γ∈(γ3, γ4, γ5), then the internal reinforcement loss function L s is defined as follows:
[0099]
[0100] L s =L c +BL res
[0101] Among them, L c represents the layout loss corresponding to each phrase object, A (γ) represents the cross attention matrix of the γth layer of the denoising network output, γ represents the selected preset layer, B represents the layout box, i represents the position of the phrase object in the conditional text, u represents the pixel position, represents the attention value at pixel position u in the attention map corresponding to position i in the cross attention matrix of the γth layer, B global represents the union of all layout boxes, n represents the number of layout boxes, j represents the subscript of the layout box, B j represents the jth layout box, L res represents the residual attention loss, represents the attention value at pixel position u in the residual attention map in the cross-attention matrix of the γth layer, and β represents the residual attention loss L res The weight, L s Represents internal reinforcement loss.
[0102] In this embodiment, res is a set of indices that does not correspond to the cross attention map of any conditional text character and is used to extract the residual attention map. In this embodiment, res is preset to {8,9,10,11,12,13,14,15,16,17,18,19,20,21,22}, 15 channels are selected, and A t The attention maps corresponding to 15 channels are extracted and averaged to obtain the residual attention map And in calculating L c When the layout loss is calculated, it is calculated for each layout box. That is, if there are multiple layout boxes, multiple layout losses L are calculated. c , then in calculating the internal reinforcement loss function L s When multiple layout losses L c Add.
[0103] Step S33: Calculate the external weakening loss L w , defined as follows:
[0104]
[0105] Where B′ represents the layout box B j And calculate the external weakening loss L w , the calculation is performed for each layout frame, that is, if there are multiple layout frames, multiple external weakening losses L are calculated. w , when calculating the overall loss, multiple external weakening losses L w Add.
[0106] Step S34, calculate the centroid loss L cen , first calculate the center of mass position of the "dog" and "ball" objects (represented by the center of mass position of their corresponding attention) and the center position of the layout box:
[0107]
[0108] The centroid loss function is defined as follows to move the “dog” and “ball” objects as close as possible to the center of the bounding box:
[0109]
[0110] Where centroid(obj,i) represents the centroid position of the object calculated by the cross attention corresponding to the character i of the object. Here h represents the height of the attention matrix, w represents the width, and i refers to the index of the object character in the input text condition. h,w,i It represents the attention value of the cross attention matrix corresponding to the character i of the object at the position (h,w). centroid(B,i) represents the centroid of the layout box corresponding to the character i of the object. Indicates the horizontal left endpoint of the layout box, that is, the upper left corner. Indicates the horizontal right endpoint of the layout box, that is, the upper right corner. Indicates the vertical right endpoint, that is, the lower right corner, Indicates the vertical left endpoint, that is, the lower left corner. j represents the object centroid represented by the attention matrix, b j Represents the centroid of a given layout box. It should be noted that the centroid loss L is calculated cen When , the calculation is performed for each layout box, that is, if there are multiple layout boxes, multiple centroid losses L are calculated cen , when calculating the overall loss, multiple centroid losses L cen Add.
[0111] Step S35: Calculate the overall loss using the following formula:
[0112] L=L s +η1L w +η2L cen
[0113] Step S36: Use the overall loss to correct the potential vector z t Perform gradient update. In the first cycle, z is updated. T .
[0114]
[0115] Among them, z t ′ is the updated potential vector at the current moment, z t is the potential vector at the current moment, α represents the loss guidance weight, Represents the overall loss function L with respect to parameter z t The gradient of the overall loss function L at z t is the direction of maximum growth, and L is the overall loss.
[0116] Step S4: within a sampling period, iteratively update the latent vector until the overall loss drops below a preset threshold, and in the early stage of the sampling process, update and denoise according to the loss function, while in the subsequent stage, stop further updating of the latent vector and only perform denoising.
[0117] Iterate and update within a cycle. If the total loss is greater than the preset threshold and the current time is less than the set time threshold t end (In this embodiment, t is set end =13), then the overall loss needs to be used to correct the latent vector z t Perform gradient updates to update the denoising network and the updated potential vector zt ′ Denoising; otherwise, directly perform denoising on the potential vector z at the current moment t Use the output noise of the Unet architecture to denoise z t ′ or t Denoising is performed to obtain the potential vector z at time t-1 t-1 When denoising, the output noise is first processed through a convolutional layer to obtain the output latent_out, which is resized to 1×4×64×64. This output latent_out is then used to denoise z. t ′ or t Perform denoising.
[0118] Step S5: When the sampling time becomes 0, the latent vector z0 is output, and the decoder converts the latent vector back to an image to obtain an image that conforms to the given layout.
[0119] In this embodiment, T=50 is set, and sampling and denoising are performed continuously until the sampling time t changes from 50 to 0, and the latent vector z0 is output. The output latent vector z0 is converted back to an image by the decoder, such as Fig.14 As shown here; the VAE decoder is used to convert the denoised potential representation generated by the back diffusion process into an image, and the objects in the image meet the given layout requirements, such as Figure 1 As shown, an image of a "dog playing with a ball" is generated, and "dog" and "ball" conform to the given layout.
[0120] The technical features of the above-described embodiments may be arbitrarily combined. To make the description concise, not all possible combinations of the technical features in the above-described 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.
[0121] The above-mentioned embodiments only express several implementation modes of the present invention, and the description thereof is relatively specific and detailed, but it cannot be understood as limiting the scope of the invention. It should be pointed out that, for a person of ordinary skill in the art, several modifications and improvements can be made without departing from the concept of the present invention, and these all belong to the protection scope of the present invention. Therefore, the protection scope of the present invention shall be subject to the attached claims.
Claims
1. A method for generating images based on cross-attention layout conditions, characterized in that: The method for generating an image based on a cross-attention mechanism layout condition comprises: Step S1, obtaining input conditional text, a layout box set and phrase objects corresponding to the layout boxes, extracting the positions of the phrase objects in the conditional text, and encoding the conditional text and the current time to obtain a text embedding vector and a time embedding vector respectively; Step S2, obtaining the potential vector at the current moment, inputting the potential vector, the text embedding vector and the time embedding vector into the denoising network, obtaining the noise at the current moment output by the denoising network and the cross attention matrix in the denoising process; Step S3, using the output cross attention matrix to respectively calculate the internal reinforcement loss, external weakening loss and centroid loss of the phrase object layout box to obtain the overall loss; Step S4: if the overall loss is greater than a preset threshold and the current time is less than a set time threshold, the denoising network is updated according to the overall loss, and the potential vector at the current time is updated, and the noise at the current time is used to denoise the updated potential vector at the current time to obtain the potential vector at the next time; otherwise, the noise at the current time is used to denoise the potential vector at the current time to obtain the potential vector at the next time; Step S5: If the next moment is not the final moment, the latent vector of the next moment is used as the latent vector of the current moment, and the time embedding vector is updated and then the process returns to step S2; otherwise, the latent vector of the next moment is converted back into an image to obtain an image that conforms to the given layout.
2. The method for generating images based on the layout conditions of the cross-attention mechanism according to claim 1, characterized in that: The denoising network includes a first cross-attention downsampling layer, a second cross-attention downsampling layer, a third cross-attention downsampling layer, a downsampling layer, a cross-attention intermediate layer, an upsampling layer, a first cross-attention upsampling layer, a second cross-attention upsampling layer, and a third cross-attention downsampling layer; The input of the first cross-attention downsampling layer includes the potential vector, text embedding vector and time embedding vector at the current moment, and the output is feature D1; The input of the second cross-attention downsampling layer includes feature D1, text embedding vector and time embedding vector, and the output is feature D2; The input of the third cross-attention downsampling layer includes feature D2, text embedding vector and time embedding vector, and the output is feature D3; The input of the downsampling layer includes feature D3 and a time embedding vector, and the output is feature D4; The input of the cross attention intermediate layer includes feature D4, text embedding vector and time embedding vector, and the output is feature M1; The input of the upsampling layer includes feature M1 and a time embedding vector, and the output is feature U1; The input of the first cross-attention upsampling layer includes feature U1, text embedding vector, time embedding vector and feature D3, and the output is feature U2; The input of the second cross-attention upsampling layer includes feature U2, text embedding vector, time embedding vector and feature D2, and the output is feature U3; The input of the third cross-attention upsampling layer includes feature U3, text embedding vector, time embedding vector and feature D1, and the output is the noise at the current moment.
3. The method for generating images based on the layout conditions of the cross-attention mechanism according to claim 2, characterized in that: The structures of the first cross-attention downsampling layer, the second cross-attention downsampling layer, and the third cross-attention downsampling layer include a 2D residual module, a 2D transformer module, a 2D residual module, a 2D transformer module, and a 2D downsampling layer connected in sequence, the latent vector or feature is used as the input of the first 2D residual module, the text embedding vector is respectively input to the two 2D transformer modules, and the time embedding vector is respectively input to the two 2D residual modules; The structure of the downsampling layer includes two 2D residual modules connected in sequence, the feature D3 is used as the input of the first 2D residual module, and the time embedding vector is input into the two 2D residual modules respectively; The structure of the cross-attention intermediate layer includes a 2D residual module, a 2D Transformer module and a 2D residual module connected in sequence, the feature D4 is used as the input of the first 2D residual module, the text embedding vector is input to the 2D Transformer module, and the time embedding vector is input to the two 2D residual modules respectively; The upsampling layer includes a 2D residual module, a 2D residual module, a 2D residual module and a 2D upsampling layer connected in sequence, the feature M1 is used as the input of the first 2D residual module, and the time embedding vector is spliced with the feature D3 output by the third cross-attention downsampling layer and input into the first 2D residual module, the time embedding vector is spliced with the output of the first 2D residual module in the downsampling layer and input into the second 2D residual module, and the time embedding vector is spliced with the feature D4 output by the downsampling layer and input into the third 2D residual module; The structures of the first cross-attention upsampling layer, the second cross-attention upsampling layer and the third cross-attention downsampling layer include a 2D residual module, a 2D Transformoer module, a 2D residual module, a 2D Transformoer module, a 2D residual module, a 2D Transformoer module and a 2D upsampling layer. The concatenation of feature U1 and feature D3 or the concatenation of feature U2 and feature D2 or the concatenation of feature U3 and feature D1 serves as the input of the first 2D residual module of the corresponding cross-attention upsampling layer, the text embedding vector is input into the three 2D Transformoer modules respectively, and the time embedding vector is input into the three 2D residual modules respectively.
4. The method for generating images based on the layout conditions of the cross-attention mechanism according to claim 2, characterized in that: The cross-attention matrix in the denoising process is the normalized dot product attention matrix obtained by the first cross-attention downsampling layer, the second cross-attention downsampling layer, the third cross-attention downsampling layer, the cross-attention intermediate layer, the first cross-attention upsampling layer, the second cross-attention upsampling layer and the third cross-attention downsampling layer in the denoising process.
5. The method for generating images based on the layout conditions of the cross-attention mechanism according to claim 1, characterized in that: The output cross attention matrix is used to calculate the internal reinforcement loss, external weakening loss and centroid loss of the phrase object layout box respectively, and the overall loss is obtained, including: The internal strengthening loss L s The calculation formula is as follows: L s =L c +βL res Among them, L c represents the layout loss corresponding to each phrase object, A (γ) represents the cross attention matrix of the γth layer of the denoising network output, γ represents the selected preset layer, B represents the layout box, i represents the position of the phrase object in the conditional text, u represents the pixel position, represents the attention value at pixel position u in the attention map corresponding to position i in the cross attention matrix of the γth layer, B global represents the union of all layout boxes, n represents the number of layout boxes, j represents the subscript of the layout box, B j represents the jth layout box, L res represents the residual attention loss, represents the attention value at pixel position u in the residual attention map in the cross-attention matrix of the γth layer, and β represents the residual attention loss L res The weight, L s represents internal reinforcement loss; The external weakening loss Lw is calculated as follows: Where B′ represents the layout box B j The complement of ; The centroid loss L cen The calculation formula is as follows: Where centroid(obj, i) represents the object centroid position calculated by the cross attention matrix corresponding to the position i of the phrase object, h represents the height of the cross attention matrix, w represents the width of the cross attention matrix, and A h,w,i represents the attention value of the cross attention matrix corresponding to the position i of the phrase object at position (h, w), centroid(B, i) represents the centroid of the layout box corresponding to the position i of the phrase object, Indicates the horizontal left endpoint of the layout box, that is, the upper left corner. Indicates the horizontal right endpoint of the layout box, that is, the upper right corner. Indicates the vertical right endpoint of the layout box, that is, the lower right corner. Indicates the vertical left endpoint of the layout box, that is, the lower left corner, c j represents the object centroid position calculated by the phrase object corresponding to the j-th layout box, b i represents the centroid of the layout box calculated by the phrase object corresponding to the j-th layout box; The overall loss calculation formula is as follows: L=L s +η1L w +η2L cen Among them, L is the overall loss, η1 is the external weakening loss L w The weight of η2 is the centroid loss L cen The weight of .
6. The method for generating images based on the layout conditions of the cross-attention mechanism according to claim 1, characterized in that: The updating of the potential vector at the current moment includes: Among them, z′ t is the updated potential vector at the current moment, z t is the potential vector at the current moment, α represents the loss guidance weight, Represents the overall loss function L with respect to parameter z t The gradient of the overall loss function L at z t is the direction of maximum growth, and L is the overall loss.
7. The method for generating images based on layout conditions of a cross-attention mechanism according to claim 1, characterized in that: The latent vector at the initial moment is obtained by randomly sampling from a normal distribution N(0, 1) to obtain the latent vector at the initial moment.
Citation Information
Cited By
Layered guidance method for 2D image generation of target fine control based on trajectory
CN119648568A
Method and device for generating video from text and storage medium
CN119697456A
