A method for makeup transfer based on Transformer-based generative adversarial network
Through the Generative Adversarial Network based on Transformer, using Swin-Transformer block and attention mechanism, the problem of spatial misalignment in makeup migration is solved, and a more natural and controllable makeup migration effect is achieved.
Patent Information
- Application Number
- CN202111116720.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-09-23
- Publication Date
- 2024-10-18
- Estimated Expiration
- 2041-09-23
AI Technical Summary
Existing makeup transfer methods are difficult to effectively handle the spatial misalignment problem between the source image and the reference image, resulting in insufficient local understanding and controllability, and unable to achieve natural facial makeup transfer.
A generative adversarial network built based on Transformer is used. By introducing the Swin-Transformer block as a feature extractor, and combining the attention mechanism and facial marker points, a pixel-by-pixel correspondence is established to achieve effective makeup transfer.
It improves the local detail consistency and style feature extraction capabilities of makeup transfer, solves the problem of spatial misalignment, and achieves a more natural and controllable makeup transfer effect.
Smart Images

Figure CN113808008B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of computer vision and pattern recognition, and in particular to a method for realizing makeup transfer by constructing a generative adversarial network based on Transformer. Background Art
[0002] Makeup transfer refers to the technology of directly transferring the makeup style on the target image to the original image. Virtual makeup software attempts to transfer different makeup, such as lipstick, eye shadow, blush, etc., between faces in photos or short videos, and is gradually becoming more and more popular. The natural transfer of the makeup style of a suitable reference image to an unmade-up face image can be widely used in photography, video, entertainment, fashion and other fields. The current methods for facial makeup transfer can be roughly classified into traditional digital image processing-based methods and deep learning-based methods. With the rapid development of deep learning technology, especially the introduction and continuous innovation of GANs, the makeup transfer method based on GANs has increasingly advantages that traditional digital image processing methods cannot match.
[0003] Image style transfer: Image style transfer has been a hot topic in recent years. Researchers have tried to convert source images into images of various styles, such as Van Gogh, art, cartoons, etc. Makeup transfer is also one of them. Traditional style transfer can be divided into the following three categories: (1) stroke-based rendering; (2) region-based technology; (3) example-based rendering. With the development of deep learning, many CNN-based models have emerged. However, these image style transfer methods usually transfer the image style from one image domain to another. Therefore, they lack local understanding and controllability and cannot be applied to the migration of specific facial makeup.
[0004] Makeup transfer: CycleGAN is one of the widely-regarded image transfer methods, which can achieve style transfer between two unpaired images, but it cannot specify a reference image. PairedCycleGAN further introduces an asymmetric function to complete the makeup transfer / removal task and a variant of the cycle consistency loss to support makeup transfer of specified makeup images. BeautyGlow uses the Glow framework to deconstruct latent features into makeup features and non-makeup features, which are then recombined and inverted to the image domain. BeautyGAN introduces a dual-input / dual-output GAN to simultaneously complete makeup transfer and removal, and refines local details through makeup loss. Local Adversarial Structure Network (LADN) uses multiple overlapping local discriminators and asymmetric loss functions to ensure the consistency of local details. Although the above methods can perform makeup transfer in a sense, they do not specifically deal with the spatial misalignment problem between the source image and the reference image. Summary of the invention
[0005] The purpose of the present invention is to overcome the shortcomings of the prior art and provide a method for realizing makeup transfer by constructing a generative adversarial network based on Transformer.
[0006] To achieve the above purpose, the technical solution provided by the present invention is:
[0007] The method of constructing a generative adversarial network based on Transformer to realize makeup transfer includes the following steps:
[0008] S1. Collect photos on the Internet to form a picture dataset.
[0009] S2, classify the images in the image dataset into makeup images and non-makeup images;
[0010] S3, preprocessing the classified images;
[0011] S4. Build a Transformer-based makeup transfer network;
[0012] S5. In the preprocessed image dataset, the images without makeup are used as source images, and the images with makeup are used as reference images, which are used to train the Transformer-based makeup transfer network built in step S4;
[0013] S6. Apply the trained Transformer-based makeup transfer network to makeup transfer.
[0014] Furthermore, the specific process of preprocessing the classified images in step S3 includes:
[0015] S3-1, cropping the face area;
[0016] S3-2, scaling the cropped facial makeup image;
[0017] S3-3. Use the dlib library to obtain 68 facial key points of the face as facial markers;
[0018] S3-4. Use the BiSeNet semantic segmentation network to segment the face into three regions: eyes, lips, and face.
[0019] Furthermore, the Transformer-based makeup transfer network includes two input branches, which have the same structure and function but do not share weights;
[0020] Each branch includes the first feature extraction stage and the second feature extraction stage, and each stage uses two consecutive Swin-Transformer blocks as feature extractors.
[0021] Furthermore, the calculation process of the two consecutive Swin-Transformer blocks is as follows:
[0022]
[0023]
[0024]
[0025]
[0026] Among them, WMSA is a window-based multi-head self-attention mechanism, SWMSA is a sliding window multi-head self-attention mechanism, MLP is a multi-layer perceptron, LN is a layer normalization layer, With Z l They represent the feature outputs of WMSA or SWMSA and MLP in the lth Swin-Transformer block, respectively, and the residual structure is used.
[0027] Furthermore, in the Transformer-based makeup transfer network, the loss function is designed as follows:
[0028] 1) Fighting against losses:
[0029] Since there are two inputs, the source image and the reference image, two discriminators are needed to determine whether the image is from the source image domain X, the reference image domain Y, or the generated image, so the adversarial loss is:
[0030]
[0031] Where D X and D Y Respectively represent the discriminator of image domain X and image domain Y, G() is the generator, and G(x,y) represents the generation of images in domain Y from images in domain X;
[0032] 2) Cycle consistency loss:
[0033] Since the training data is unpaired, an unsupervised training method is used, so the cycle consistency loss is introduced to constrain the reconstructed image:
[0034]
[0035] 3) Perceptual loss:
[0036] Using the VGG-16 pre-trained model, in the hidden layer F l The two-norm loss of the source image and the generated image is calculated in , where l represents the lth layer of VGG-16:
[0037]
[0038] 4) Total loss:
[0039]
[0040] Among them, λ adv ,λ cyc ,λ per is a hyperparameter to balance the various losses.
[0041] Furthermore, the specific process of applying the trained Transformer-based makeup transfer network to makeup transfer in step S6 is as follows:
[0042] In each branch,
[0043] The corresponding source image or reference image is used as input, and the input image is first divided into non-overlapping small blocks, each of which is regarded as a token, and its feature is set as the concatenation of the original pixel RBG values;
[0044] Then use a linear embedding layer to map the original feature values to an arbitrary dimension C;
[0045] The data is then passed to two Swin-Transformer Blocks for self-attention calculation; the linear embedding layer and the following two Swin-Transformer Blocks constitute the first stage of feature extraction;
[0046] Next, in order to achieve hierarchical representation of features, the small matrices in each patch are grouped and then connected, and then a linear connection layer is used to reduce the dimension. At this time, the size of the feature space is reduced by half, which is the same as the size of the feature space after convolution-based downsampling. The downsampling layer and the following two Swin-Transformer Blocks constitute the second stage of feature extraction;
[0047] Then the feature space is transformed to convert the one-dimensional feature sequence into a two-dimensional feature matrix;
[0048] Then perform feature fusion of the two branches;
[0049] Finally, after two layers of deconvolution upsampling layers, the feature map after feature fusion is inverted and output as a picture of the source image with the makeup of the reference image transferred.
[0050] Furthermore, the specific process of feature fusion of the two branches includes:
[0051] Calculate a spatial attention matrix A∈R through the AMM module HW×HW To specify how the pixels of the source image x are deformed from the pixels of the reference image y; Ai,j Represents the attention value corresponding to the i-th pixel of the source image x and the j-th pixel of the reference image y;
[0052] Use 68 facial landmarks as anchor points to describe pixel x i The position-dependent feature P i , which is calculated by computing the pixel x i The coordinate difference with 68 facial landmarks is:
[0053] P i =[f(x i )-f(l 1 ), f(x i )-f(l 2 ), ..., f(x i )-f(l 68 ),
[0054] g(x i )-g(l 1 ), g(x i )-g(l 2 ), ..., g(x i )-g(l 68 )]
[0055] Among them, f() and g() represent the horizontal and vertical coordinates of the pixel points respectively, l i represents the i-th facial landmark point, which is obtained by the facial landmark detector; considering that the image size may be different, these position-related features P are normalized to
[0056] In order to achieve better makeup transfer effect, the BiSeNet semantic segmentation network has been used to segment the face into three regions: eyes, lips and face. i and v j Respectively represent the eigenvalues corresponding to the pixels of the original image and the reference image after two-order feature extraction and conversion, then the spatial attention A i,j It is expressed as:
[0057]
[0058] Among them, ω is the weight of the relevant feature, and Respectively represent x i and i The areas where the eyes, lips and face are located are represented by 0, 1 and 2 respectively, and θ() is the impulse function. 1 when it is, otherwise 0;
[0059] Let γ∈R 1×H×W and β∈R1×H×W Represents the feature map V from the reference image y ∈R C×H×W The makeup matrices obtained by each 1×1 convolution layer are multiplied with the A matrix to obtain two spatially deformed makeup matrices:
[0060]
[0061]
[0062] Then expand γ′ and β′ along the channel dimension to obtain Γ′∈R C×H×W and B′∈R C×H×W , and then applied to the feature map V of the source image x ∈R C×H×W Then we get the feature map V′ after feature fusion x :
[0063] V′ x =Γ′V x +B′.
[0064] Compared with the existing technology, the principle and advantages of this solution are as follows:
[0065] This solution uses Swin-Transformer block instead of ResNet block as the feature extractor of the generator, and adopts a hierarchical feature representation similar to the CNN network, so it has a stronger style feature extraction ability than the traditional ResNet-based network. At the same time, in order to solve the spatial dislocation problem in the makeup transfer process, this solution introduces the attention mechanism, establishes a pixel-by-pixel correspondence, and realizes the effective fusion of the corresponding facial area features through facial analysis masks and facial landmarks, thereby achieving effective makeup transfer. BRIEF DESCRIPTION OF THE DRAWINGS
[0066] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the services required for use in the embodiments or the prior art descriptions are briefly introduced below. Obviously, the drawings described below are only 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.
[0067] Figure 1 This is a schematic diagram of the structure of the Transformer-based makeup transfer network;
[0068] Figure 2 Schematic diagram of the network structure of two consecutive swin-Transformer blocks;
[0069] Figure 3 It is a structural diagram of the AMM module. DETAILED DESCRIPTION
[0070] The present invention will be further described below in conjunction with specific embodiments:
[0071] The method for implementing makeup transfer based on Transformer-based generative adversarial network described in this embodiment includes the following steps:
[0072] S1. Collect photos on the Internet to form a picture dataset.
[0073] S2, classify the images in the image dataset into makeup images and non-makeup images;
[0074] S3, preprocess the classified images, the process is as follows:
[0075] S3-1, cropping the face area;
[0076] S3-2, scaling the cropped facial makeup image;
[0077] S3-3. Use the dlib library to obtain 68 facial key points of the face as facial markers;
[0078] S3-4. Use the BiSeNet semantic segmentation network to segment the face into three regions: eyes, lips, and face.
[0079] S4. Build a Transformer-based makeup transfer network;
[0080] Structure Figure 1 As shown, the Transformer-based makeup transfer network includes two input branches, which have the same structure and function but do not share weights;
[0081] Each branch includes the first feature extraction stage and the second feature extraction stage, and each stage uses two consecutive Swin-Transformer blocks as feature extractors, such as Figure 2 shown.
[0082] The calculation process of two consecutive Swin-Transformer blocks is as follows:
[0083]
[0084]
[0085]
[0086]
[0087] Among them, WMSA is a window-based multi-head self-attention mechanism, SWMSA is a sliding window multi-head self-attention mechanism, MLP is a multi-layer perceptron, LN is a layer normalization layer, With Z l They represent the feature outputs of WMSA or SWMSA and MLP in the lth Swin-Transformer block, respectively, and the residual structure is used.
[0088] In the Transformer-based makeup transfer network, the loss function is designed as follows:
[0089] 1) Fighting against losses:
[0090] Since there are two inputs, the source image and the reference image, two discriminators are needed to determine whether the image is from the source image domain X, the reference image domain Y, or the generated image, so the adversarial loss is:
[0091]
[0092] Where D X and D Y Respectively represent the discriminator of image domain X and image domain Y, G() is the generator, and G(x,y) represents the generation of images in domain Y from images in domain X;
[0093] 2) Cycle consistency loss:
[0094] Since the training data is unpaired, an unsupervised training method is used, so the cycle consistency loss is introduced to constrain the reconstructed image:
[0095]
[0096] 3) Perceptual loss:
[0097] Using the VGG-16 pre-trained model, in the hidden layer F l The two-norm loss of the source image and the generated image is calculated in , where l represents the lth layer of VGG-16:
[0098]
[0099] 4) Total loss:
[0100]
[0101] Among them, λ adv ,λ cyc ,λ per is a hyperparameter to balance the various losses.
[0102] S5. In the preprocessed image dataset, the images without makeup are used as source images, and the images with makeup are used as reference images, which are used to train the Transformer-based makeup transfer network built in step S4;
[0103] S6. Apply the trained Transformer-based makeup transfer network to makeup transfer.
[0104] The specific process of makeup migration is as follows:
[0105] In each branch,
[0106] Because Transformer originates from the field of natural language processing, its input data is a one-dimensional serial token and needs to be embedded, so it cannot directly use two-dimensional image data as input. To this end, the input image of this embodiment is first divided into non-overlapping small blocks, each of which is regarded as a token, and its feature is set as the concatenation of the original pixel RBG value; in the implementation, the size of the small block is set to 4×4, so the feature dimension of each patch is 4×4×3=48;
[0107] Then a linear embedding layer is used to map the original feature values to an arbitrary dimension C. In this embodiment, C is set to 96.
[0108] Then the data is passed to two Swin-Transformer Blocks for self-attention calculation; the linear embedding layer and the following two Swin-Transformer Blocks constitute the first stage of feature extraction; at this time, the feature space is
[0109] In order to achieve hierarchical representation of features, that is, as the number of network layers increases, the number of tokens decreases and the dimension increases, in each patch, small 2×2 matrices are grouped and then connected, so the dimension becomes 4C and the number of tokens becomes After another linear connection layer, the dimension is reduced to 2C. At this time, the size of the feature space is reduced by half, which is The size of the feature space after convolution-based downsampling is the same, and the downsampling layer and the two subsequent Swin-TransformerBlocks constitute the second stage of feature extraction;
[0110] Then the feature space is transformed, the one-dimensional feature sequence is converted into a two-dimensional feature matrix, and then the features of the two branches are fused;
[0111] The specific process of feature fusion of the two branches includes:
[0112] Since the poses and expressions of the source and reference images may be quite different, the features of the two input branches obtained in the previous steps will be spatially misaligned.
[0113] To this end, this embodiment adopts Figure 3 The AMM module shown computes a spatial attention matrix A∈R HW×HW To specify how the pixels of the source image x are deformed from the pixels of the reference image y; A i,j Represents the attention value corresponding to the i-th pixel of the source image x and the j-th pixel of the reference image y;
[0114] Use 68 facial landmarks as anchor points to describe pixel x i The position-dependent feature P i , which is calculated by computing the pixel x i The coordinate difference with 68 facial landmarks is:
[0115] P i =[f(x i )-f(l 1 ), f(x i )-f(l 2 ), ..., f(x i )-f(l 68 ),
[0116] g(x i )-g(l 1 ), g(x i )-g(l 2 ), ..., g(x i )-g(l 68 )]
[0117] Among them, f() and g() represent the horizontal and vertical coordinates of the pixel points respectively, l i represents the i-th facial landmark point, which is obtained by the facial landmark detector; considering that the image size may be different, these position-related features P are normalized to
[0118] In order to achieve better makeup transfer effect, the BiSeNet semantic segmentation network has been used to segment the face into three regions: eyes, lips and face. i and v j Respectively represent the eigenvalues corresponding to the pixels of the original image and the reference image after two-order feature extraction and conversion, then the spatial attention A i,j It is expressed as:
[0119]
[0120] Among them, ω is the weight of the relevant feature, and Respectively represent x i and i The areas where the eyes, lips and face are located are represented by 0, 1 and 2 respectively, and θ() is the impulse function. 1 when it is, otherwise 0;
[0121] Let γ∈R 1×H×W and β∈R 1×H×W Represents the feature map V from the reference image y ∈R C×H×W The makeup matrices obtained by each 1×1 convolution layer are multiplied with the A matrix to obtain two spatially deformed makeup matrices:
[0122]
[0123]
[0124] Then expand γ′ and β′ along the channel dimension to obtain Γ′∈R C×H×W and B′∈R C×H×W , and then applied to the feature map V of the source image x ∈R C×H×W Then we get the feature map V′ after feature fusion x :
[0125] V′ x =Γ′V x +B′.
[0126] The embodiments described above are only preferred embodiments of the present invention and are not intended to limit the scope of implementation of the present invention. Therefore, all changes made according to the shape and principle of the present invention should be included in the protection scope of the present invention.
Claims
1. A method for realizing makeup transfer based on Transformer-based generative adversarial network, characterized in that: The following steps are involved: S1. Collect photos on the Internet to form a picture dataset. S2, classify the images in the image dataset into makeup images and non-makeup images; S3, preprocessing the classified images; S4. Build a Transformer-based makeup transfer network; S5. In the preprocessed image dataset, the images without makeup are used as source images, and the images with makeup are used as reference images, which are used to train the Transformer-based makeup transfer network built in step S4; S6. Apply the trained Transformer-based makeup transfer network to makeup transfer; The Transformer-based makeup transfer network has two input branches, which have the same structure and function but do not share weights. Each branch includes the first stage of feature extraction and the second stage of feature extraction, and each stage uses two consecutive Swin-Transformer blocks as feature extractors; The specific process of applying the trained Transformer-based makeup transfer network to makeup transfer in step S6 is as follows: In each branch, The corresponding source image or reference image is used as input, and the input image is first divided into non-overlapping small blocks, each of which is regarded as a token, and the feature of each token is set as the concatenation of the original pixel RBG value; Then use a linear embedding layer to map the original feature values to an arbitrary dimension C; The data is then passed to two Swin-Transformer Blocks for self-attention calculation; the linear embedding layer and the following two Swin-Transformer Blocks constitute the first stage of feature extraction; Next, in order to achieve hierarchical representation of features, the small matrices in each patch are grouped and then connected, and then a linear connection layer is used to reduce the dimension. At this time, the size of the feature space is reduced by half, which is the same as the size of the feature space after convolution-based downsampling. The downsampling layer and the following two Swin-Transformer Blocks constitute the second stage of feature extraction; Then the feature space is transformed to convert the one-dimensional feature sequence into a two-dimensional feature matrix; Then perform feature fusion of the two branches; Finally, after two layers of deconvolution upsampling layers, the feature map after feature fusion is inverted and output as a picture of the source image with the makeup of the reference image transferred; The specific process of feature fusion of the two branches includes: Calculate a spatial attention matrix A∈R through the AMM module HW×HW To specify how the pixels of the source image x are deformed from the pixels of the reference image y; A i,j Represents the attention value corresponding to the i-th pixel of the source image x and the j-th pixel of the reference image y; Use 68 facial landmarks as anchor points to describe pixel x i The position-dependent feature P i , which is calculated by computing the pixel x i The coordinate difference with 68 facial landmarks is: P i =[f(x i )-f(l1),f(x i )-f(l2),...,f(x i )-f(l 68 ),g(x i )-g(l1),g(x i )-g(l2),…,g(x i )-g(l 68 )] Among them, f() and g() represent the horizontal and vertical coordinates of the pixel points respectively, l1 to l 68 represents 68 facial landmarks, which are obtained by facial landmark detector; considering the different sizes of pictures, these position-related features P are normalized to In order to achieve better makeup transfer effect, the BiSeNet semantic segmentation network has been used to segment the face into three regions: eyes, lips and face. i and v j Respectively represent the eigenvalues corresponding to the pixels of the original image and the reference image after two-order feature extraction and conversion, then the spatial attention A i,j It is expressed as: Among them, ω is the weight of the relevant feature, and Respectively represent x i and j The areas where the eyes, lips and face are located are represented by 0, 1 and 2 respectively, and θ() is the impulse function. 1 when it is, otherwise 0; Let γ∈R 1×H×W and β∈R 1×H×W Represents the feature map V from the reference image y ∈R C×H×W The makeup matrices obtained by each 1×1 convolution layer are multiplied with the A matrix to obtain two spatially deformed makeup matrices: Then expand γ′ and β′ along the channel dimension to obtain Γ′∈R C×H×W and B′∈R C×H×W , and then applied to the feature map V of the source image x ∈R C×H×W Then we get the feature map V′ after feature fusion x : V′ x =Γ′V x +B′。 2. The method for realizing makeup transfer by constructing a generative adversarial network based on Transformer according to claim 1, characterized in that: The specific process of preprocessing the classified images in step S3 includes: S3-1, cropping the face area; S3-2, scaling the cropped facial makeup image; S3-3. Use the dlib library to obtain 68 facial key points of the face as facial markers; S3-4. Use the BiSeNet semantic segmentation network to segment the face into three regions: eyes, lips, and face.
3. The method for realizing makeup transfer by constructing a generative adversarial network based on Transformer according to claim 1, characterized in that: The calculation process of the two consecutive Swin-Transformer blocks is as follows: Among them, WMSA is a window-based multi-head self-attention mechanism, SWMSA is a sliding window multi-head self-attention mechanism, MLP is a multi-layer perceptron, LN is a layer normalization layer, With Z l They represent the feature outputs of WMSA or SWMSA and MLP in the lth Swin-Transformer block, respectively, and the residual structure is used.
4. The method for realizing makeup transfer by constructing a generative adversarial network based on Transformer according to claim 3, characterized in that: In the Transformer-based makeup transfer network, the loss function is designed as follows: 1) Fighting against losses: Since there are two inputs, the source image and the reference image, two discriminators are needed to determine whether the image is from the source image domain X, the reference image domain Y, or the generated image, so the adversarial loss is: Where D X and D Y Respectively represent the discriminator of image domain X and image domain Y, G() is the generator, and G(x,y) represents the generation of images in domain Y from images in domain X; 2) Cycle consistency loss: Since the training data is unpaired, an unsupervised training method is used, so the cycle consistency loss is introduced to constrain the reconstructed image: 3) Perceptual loss: Using the VGG-16 pre-trained model, in the hidden layer F l The two-norm loss of the source image and the generated image is calculated in , where 1 represents the first layer of VGG-16: 4) Total loss: Among them, λ adv ,λ cyc ,λ per is a hyperparameter to balance the various losses.
Citation Information
Patent Citations
Human face makeup method and system based on semantic segmentation
CN112949605A
