A method for achieving real image style transfer based on global information-guided network
By using the global information guidance network in image style transfer, combined with partition adaptive instance normalization and cross-channel partition attention module, and using the color gamut mean loss function and gradient loss function, the insufficient utilization of global color information in the existing technology is solved. solve the problem and achieve high-quality real image style transfer, avoiding image distortion and loss of details.
Patent Information
- Application Number
- CN202110891687.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-08-04
- Publication Date
- 2024-09-06
- Estimated Expiration
- 2041-08-04
AI Technical Summary
The existing technology cannot fully utilize the global color information in image style transfer, resulting in distortion and loss of details in the generated images, and requires pre- and post-processing to suppress deformation and style overflow.
Using a network structure based on global information guidance, through partition adaptive instance normalization and cross-channel partition attention module, combined with the pre-trained VGG-19 network, content and style features are extracted, and the color gamut mean loss function and gradient loss are used Functions are optimized and trained to avoid deformation and improve transfer effects.
Effective use of global color information avoids image deformation and detail loss, improves style transfer effects, and does not require additional pre- or post-processing operations. The generated images maintain realism and content consistency.
Smart Images

Figure CN113570500B
Abstract
Description
Technical Field
[0001] The present invention belongs to the technical field of computer vision style transfer, and in particular relates to a method for achieving real image style transfer based on a global information guided network. Background Art
[0002] Image style transfer refers to the technology of using algorithms to learn the style features of an image and then applying this style feature to another image. Image style transfer plays an important role in the field of computer vision. According to the desired target style, the existing image is converted into the target style image. Style transfer has many applications in animation production, camera photography and simulation systems. The current image style transfer method is basically based on artistic style transfer. By inputting content images and artistic style images into the artistic style transfer network, artistic images can be generated. However, this method usually produces the following problems: 1) The results generated by the artistic style transfer network are often accompanied by distortion and deformation, and cannot maintain realism. The generated image is difficult to be consistent with the content image in terms of content, because style is an extremely abstract feature. Style transfer is regarded as a texture transfer, and texture transfer will inevitably lead to image distortion and deformation, resulting in loss of details; 2) Preprocessing or post-processing is required to suppress problems such as image deformation and style overflow; 3) The existing artistic style transfer method cannot make full use of global color information, resulting in poor image generation effect. Unlike artistic style transfer, real image style transfer requires that the generated image maintains authenticity while transferring the style, that is, the content should be consistent with the content image. Therefore, real image style transfer can be regarded as a kind of color tone transfer. Through real image style transfer, we can obtain generated images that maintain realism. Summary of the invention
[0003] The purpose of the present invention is to overcome the defects in the prior art that global color information cannot be fully utilized, the generated images are distorted and lost in details, and pre-processing and post-processing are required, and to provide a method for achieving real image style transfer based on a global information-guided network that can utilize global color information and global context information, effectively avoid the generation of deformation, and have good transfer effect.
[0004] The technical solution adopted by the present invention to solve its technical problem is:
[0005] A method for achieving real image style transfer based on a global information guided network, characterized in that it comprises the following steps:
[0006] S1. Obtain data set;
[0007] Obtaining a data set including content images and style images, preprocessing the content images and style images in the data set, and randomly extracting content images and style images from the preprocessed data set to establish a test set and a training set respectively;
[0008] S2, build a global information guidance network;
[0009] S21, the global information guidance network is an encoder-decoder structure, firstly uses the pre-trained VGG-19 as the encoder to encode the content image and the style image respectively, and extracts the features of the content image and the style image at different layers respectively, and outputs the content features and the style features;
[0010] S22, inputting the extracted content features and style features of the corresponding level into the partition adaptive instance normalization module for instance normalization, and the obtained shallow features are passed through the downsampling module and then added element by element with the deep features of the next layer for feature transfer;
[0011] S23, performing a convolution operation on the features outputted from step S22, and then sending them to the cross-channel partition attention module after a bilinear interpolation upsampling operation, and then performing channel splicing on the features outputted from the cross-channel partition attention module and the same-scale features obtained by the instance normalization operation through a skip connection;
[0012] S24, finally, the features obtained in step S23 are convolved and sent to the decoder to obtain a generated image after style transfer;
[0013] S3, training and optimizing the global information guidance network;
[0014] S31, input the training set into the constructed global information guided network, optimize the network and use the encoder in step S21 as the loss network to calculate the content loss and style loss. The specific expression is: the content loss function of the content loss is: Where F[O] is the conv4-1 layer feature extracted by sending the generated image into VGG-19, and F[C] is the conv4-1 layer feature extracted by sending the content image into VGG-19; the style loss function of the style loss is: Where F[O] is the feature extracted by sending the generated image into VGG-19, and F[S] is the feature extracted by sending the style image into VGG-19;
[0015] S32, flattening the two-dimensional matrix representing each color channel of the generated image and the style image into a one-dimensional vector, and sorting the vector in ascending order according to the numerical value, and then dividing the vector into color regions with the same number of pixels, taking the mean of each color region to obtain a mean vector, calculating the mean square error of the mean vectors of the generated image and the style image, and obtaining a color gamut mean loss function;
[0016] S33. Using the gradient loss function Calculate the gradient loss, where is the Sobel operator;
[0017] S34, linearly combining the content loss function and style loss function in step S31, the color gamut mean loss function in step S32, and the gradient loss function in step S33 into a new loss function as a total loss function;
[0018] S35, using the Adam optimizer, through the back propagation algorithm, minimize the total loss function, iteratively update the parameters according to the set learning rate until the loss tends to balance, stop iteration, and save the trained network;
[0019] S4, test the trained global information guided network;
[0020] The test set is input into the trained global information guided network, and the generated image after style transfer is output. The SSIM and PSNR indicators are used to determine whether the generated image is distorted compared with the original content image and the style image respectively. If not, the network is retained; if so, step S3 is repeated to adjust the weight of the loss function and the number of training rounds;
[0021] S5. Perform style transfer on content images and style images;
[0022] Get the content image and style image, input the content image and style image into the trained global information guidance network for style transfer, and output the generated image after style transfer.
[0023] Furthermore, the partition adaptive instance normalization includes first sorting the pixels in each channel of the content features and style features according to the numerical value and dividing them into partitions of equal number, calculating the variance and mean of the content features and style features in each partition, and then realizing adaptive instance normalization of the content features and style features of each partition according to the corresponding positions after sorting, and restoring the pixel order after normalization is completed.
[0024] Furthermore, the cross-channel partitioned attention module includes performing a convolution operation on the features obtained by applying cross attention, sorting the features on each channel in ascending order of numerical values and dividing them into intervals containing the same number of pixels, calculating the average value of each interval and expressing it as a vector to obtain a mean vector, performing a squeeze-excitation operation to obtain a weight vector for each channel, multiplying the weight vector by the corresponding interval, and restoring the position of each pixel.
[0025] Furthermore, preprocessing the content image and the style image in step S1 includes: cropping the content image and the style image into a uniform size of 256×256, and normalizing the pixel values of the content image and the style image to a range of [-1, 1].
[0026] Furthermore, the VGG-19 network extracts features of the content image and the style image at conv1_1, conv2_1, conv3_1, and conv4_1 layers, respectively, and outputs content features and style features.
[0027] Furthermore, the encoder has a total of four convolutional layers, the convolution kernel size of each convolutional layer is 3×3, the number of convolution kernels in the next layer is twice that of the previous layer, and the convolutional layers all use the ReLU function as the activation function.
[0028] Furthermore, the calculation formula of the color gamut mean loss function is: Where c is the number of color channels, p is the number of channel color gamuts, Ov ij To generate the mean vector of the jth color domain of the i-th color channel of the image, Sv ij is the mean vector of the jth color domain of the i-th color channel of the style image.
[0029] Furthermore, the calculation formula of the total loss function is: Where λ1, λ2, λ3 and λ4 are weight hyperparameters.
[0030] Furthermore, in the Adam optimizer, the exponential decay rate β1=0.9, the exponential decay rate β2=0.999, ∈=10 -8 .
[0031] Furthermore, the network training uses a dynamic learning rate decay strategy, that is, the current learning rate Where epoch is the current training round and the initial learning rate of the network is lr origin Set to 10 -3 , learning rate decay value lr decay Set to 0.05.
[0032] The beneficial effects of the method for achieving real image style transfer based on a global information guided network of the present invention are:
[0033] 1. The present invention adopts partition adaptive instance normalization to enable the network to obtain more channel information and improve the network's ability to transfer tonal style; the cross-channel partition attention module is adopted to enable the network to better utilize global context information and guide the network to achieve better overall transfer effect.
[0034] 2. The present invention adopts the color gamut mean loss function and makes full use of the global color distribution information, so that the network can achieve better generation effect and can effectively avoid the generation of deformation without the need for additional preprocessing or post-processing operations. BRIEF DESCRIPTION OF THE DRAWINGS
[0035] The present invention will be further described in detail below in conjunction with the accompanying drawings and specific embodiments.
[0036] Figure 1 is an overall flow chart of an embodiment of the present invention;
[0037] Figure 2 is a flow chart of step S2 in an embodiment of the present invention;
[0038] Figure 3 is a flow chart of step S3 in an embodiment of the present invention;
[0039] Figure 4 is a schematic diagram of the structure of a global information guidance network according to an embodiment of the present invention;
[0040] Figure 5 is a diagram of the composition of a portion of a data set in an embodiment of the present invention;
[0041] Figure 6 is a schematic diagram of color gamut mean loss according to an embodiment of the present invention;
[0042] Figure 7 is a schematic diagram of a cross-channel partitioned attention module according to an embodiment of the present invention;
[0043] Figure 8 Schematic diagram of real image style transfer according to an embodiment of the present invention. DETAILED DESCRIPTION
[0044] The present invention will now be described in further detail with reference to the accompanying drawings. These drawings are simplified schematic diagrams, which only illustrate the basic structure of the present invention in a schematic manner, and therefore only show the components related to the present invention.
[0045] like Figure 1-Figure 8 A specific embodiment of a method for implementing real image style transfer based on a global information guided network of the present invention shown in the figure comprises the following steps:
[0046] S1. Obtain data set;
[0047] Obtain a data set containing content pictures and style pictures, preprocess the content pictures and style pictures in the data set, and randomly extract content pictures and style pictures from the preprocessed data set to establish a test set and a training set respectively; preprocessing the content pictures and style pictures includes: uniformly cropping the content pictures and style pictures into 256×256 pixels, and standardizing them, and normalizing the pixel values of the content pictures and style pictures to the range [-1, 1], so as to facilitate the training of the network. In this embodiment, on the basis of 4319 pictures in the Landscape Pictures data set, some pictures crawled from the Internet and taken by themselves are added to obtain a training data set containing 6130 pictures. The picture content in the data set is mainly natural scenery and urban scenery. The pictures are adjusted and cropped on the basis of the original pictures, and the size is unified to 256×256 pixels. During training, the content pictures and style pictures will be randomly paired, and the pictures will be normalized to the interval of [-1, 1].
[0048] S2, build a global information guidance network;
[0049] S21. The global information guided network includes modules such as partition adaptive instance normalization and cross-channel partition attention, and has an encoder-decoder structure. First, the pre-trained VGG-19 is used as the encoder to encode the content image and style image respectively, and the features of the content image and style image at four different layers of conv1-1, conv2-1, conv3-1 and conv4-1 are extracted respectively, and the content features and style features are output; the encoder has a total of four convolutional layers, and the convolution kernel size of each convolutional layer is 3×3, which can reduce the number of parameters, while deepening the network without making the network too complex and difficult to train. The number of convolution kernels in the next layer is twice that of the previous layer, and the convolutional layers all use ReLU function as the activation function.
[0050] S22, input the extracted content features and style features of the corresponding level into the partition adaptive instance normalization module for instance normalization, and the shallow features obtained are added element by element with the deep features of the next level after passing through the downsampling module for feature transfer; in this embodiment, the partition adaptive instance normalization proposes adaptive instance normalization, which only takes a mean and variance for each feature channel, and cannot fully obtain channel information, and the improved partition adaptive instance normalization includes first sorting the pixels in each channel of the content features and style features according to the numerical value and evenly dividing them into partitions with equal number of pixels, calculating the variance and mean of the content features and style features in each partition, and then realizing the adaptive instance normalization of the content features and style features of each partition according to the corresponding positions after sorting, and restoring the order of each pixel position after normalization. The content features and style features of the corresponding level are subjected to the partition adaptive instance normalization operation, and the shallow features obtained are downsampled to half of the original size, and are added element by element with the deep features for feature transfer. The present invention adopts partition adaptive instance normalization to enable the network to obtain more channel information and improve the network's ability to transfer tonal style.
[0051] S23, convolve the features output from step S22, and then send them to the cross-channel partition attention module after bilinear interpolation upsampling. Next, the features output from the cross-channel partition attention module are channel-joined with the same-scale features obtained by instance normalization operation through skip connection; the cross-channel partition attention module first convolutes the features obtained by applying cross attention, sorts the features on each channel in ascending order of value and divides them into intervals containing the same number of pixels, calculates the average value of each interval and connects them to obtain a mean vector. The length of the mean vector is the product of the number of channels and the number of partitions. Referring to the method in SENet, a squeeze excitation operation is performed to obtain the weight vector of each channel partition, the weight vector is multiplied by the corresponding partition, and the position of each pixel is restored to the state before sorting. The present invention adopts a cross-channel partition attention module, which enables the network to better utilize global context information and guide the network to achieve better overall migration effect.
[0052] S24. Finally, the features obtained in step S23 are convolved and sent to the decoder to obtain the generated image after style transfer. In the decoding part, considering the problem of deep feature information loss, the encoded features are sent to the cross-channel partition attention module after convolution and bilinear interpolation upsampling operations with reference to the U-shaped network, and the same-scale features obtained by skip connection and partition adaptive instance normalization are channel-joined. The decoder also has 4 layers, and the size of the feature map of each layer and the number of convolution kernels are consistent with the encoder.
[0053] S3, training and optimizing the global information guidance network;
[0054] S31. Input the training set into the constructed global information guided network, optimize the network training, calculate the color domain mean loss, content loss, style loss and gradient loss respectively, optimize the network parameters based on the back propagation algorithm and Adam optimizer, and iterate the parameters according to the set learning rate. Specifically, in the Adam optimizer, set the exponential decay rate β1 = 0.9, the exponential decay rate β2 = 0.999, ∈ = 10 -8 The network training uses a dynamic learning rate decay strategy.
[0055] That is, the current learning rate
[0056] Where epoch is the current training round and the initial learning rate of the network is lr origin Set to 10 -3 , learning rate decay value lr decay Set to 0.05. When the migration effect of the real style transfer network guided by global information is good, the network training is completed.
[0057] The encoder in step S21 is used as a loss network to calculate the content loss and style loss. The specific expression is: The content loss function of the content loss is: Where F[O] is the conv4-1 layer feature extracted by sending the generated image into VGG-19, and F[C] is the conv4-1 layer feature extracted by sending the content image into VGG-19; the style loss function of the style loss is: Where F[O] is the feature extracted by sending the generated image into VGG-19, and F[S] is the feature extracted by sending the style image into VGG-19;
[0058] S32, flatten the two-dimensional matrix representing each color channel of the generated image and the style image into a one-dimensional vector, and sort the vector in ascending order according to the numerical value, then divide the vector into color regions with the same number of pixels, take the mean of each color region, obtain the mean vector, calculate the mean square error of the mean vector of the generated image and the style image, and obtain the color gamut mean loss; the calculation formula of the color gamut mean loss function is: Where c is the number of color channels, p is the number of channel color gamuts, Ov ij To generate the mean vector of the jth color domain of the i-th color channel of the image, Sv ij is the mean vector of the jth color domain of the ith color channel of the style image. The present invention adopts the color domain mean loss function to make full use of the global color distribution information, so that the network can achieve better generation effect and effectively avoid deformation without additional pre-processing or post-processing operations.
[0059] S33. Using the gradient loss function Calculate the gradient loss, where is the Sobel operator;
[0060] S34, linearly combining the content loss function and style loss function in step S31, the color gamut mean loss function in step S32, and the gradient loss function in step S33 into a new loss function as the total loss function; the calculation formula of the total loss function is: Where λ1, λ2, λ3 and λ4 are weight hyperparameters. The color gamut mean loss controls the migration of hues, the content loss ensures the consistency of the generated image with the content image, the style loss controls the migration of styles, and the gradient loss maintains the edge consistency between the generated image and the content image.
[0061] S35. Use the Adam optimizer to minimize the total loss function through the back-propagation algorithm, iteratively update the parameters according to the set learning rate until the loss tends to balance, stop iteration, and save the trained network; use the Adam optimizer to optimize the network, continuously update the parameters through training, so that the total loss value is minimized and tends to be stable.
[0062] S4, test the trained global information guided network;
[0063] The test set is input into the trained global information guided network, and the generated image after style transfer is output. The SSIM and PSNR indicators are used to determine whether the generated image is distorted compared with the original content image and the style image respectively. If not, the network is retained; if so, step S3 is repeated to adjust the weight of the loss function and the number of training rounds; in this embodiment, preferably, epoch = 50, λ1 = 10000, λ2 = 10, λ3 = 10 and λ4 = 100.
[0064] S5. Perform style transfer on content images and style images;
[0065] Get the content image and style image, input the content image and style image into the trained global information guidance network for style transfer, and output the generated image after style transfer.
[0066] The global information of real image style transfer guides the network to perform real style transfer on the input content image and style image. While keeping the content of the content image basically unchanged, the tonal style of the style image is transferred to the content image, and finally a generated image is obtained that is consistent with the content image in content and consistent with the style image in color style. The method proposed in the present invention can realize the transfer of real image style while maintaining the content details and color hierarchy of the content image.
[0067] It should be understood that the specific embodiments described above are only used to explain the present invention, and are not used to limit the present invention. Obvious changes or modifications derived from the spirit of the present invention are still within the protection scope of the present invention.
Claims
1. A method for achieving real image style transfer based on a global information-guided network, characterized in that: The following steps are involved: S1. Obtain data set; Obtaining a data set including content images and style images, preprocessing the content images and style images in the data set, and randomly extracting content images and style images from the preprocessed data set to establish a test set and a training set respectively; S2, build a global information guidance network; S21, the global information guidance network is an encoder-decoder structure, firstly uses the pre-trained VGG-19 as the encoder to encode the content image and the style image respectively, and extracts the features of the content image and the style image at different layers respectively, and outputs the content features and the style features; S22, inputting the extracted content features and style features of the corresponding level into the partition adaptive instance normalization module for instance normalization, and the obtained shallow features are passed through the downsampling module and then added element by element with the deep features of the next layer for feature transfer; S23, performing a convolution operation on the features outputted from step S22, and then sending them to the cross-channel partition attention module after a bilinear interpolation upsampling operation, and then performing channel splicing on the features outputted from the cross-channel partition attention module and the same-scale features obtained by the instance normalization operation through a skip connection; S24, finally, the features obtained in step S23 are convolved and sent to the decoder to obtain a generated image after style transfer; S3, training and optimizing the global information guidance network; S31, input the training set into the constructed global information guided network, optimize the network and use the encoder in step S21 as the loss network to calculate the content loss and style loss. The specific expression is: the content loss function of the content loss is: Where F[O] is the conv4-1 layer feature extracted by sending the generated image into VGG-19, and F[C] is the conv4-1 layer feature extracted by sending the content image into VGG-19; the style loss function of the style loss is: Where F[O] is the feature extracted by sending the generated image into VGG-19, and F[S] is the feature extracted by sending the style image into VGG-19; S32, flattening the two-dimensional matrix representing each color channel of the generated image and the style image into a one-dimensional vector, and sorting the vector in ascending order according to the numerical value, and then dividing the vector into color regions with the same number of pixels, taking the mean of each color region to obtain a mean vector, calculating the mean square error of the mean vectors of the generated image and the style image, and obtaining a color gamut mean loss function; S33. Using the gradient loss function Calculate the gradient loss, where is the Sobel operator; S34, linearly combining the content loss function, the style loss function, the color gamut mean loss function, and the gradient loss function into a new loss function as the total loss function; S35, using the Adam optimizer, through the back propagation algorithm, minimize the total loss function, iteratively update the parameters according to the set learning rate until the loss tends to balance, stop iteration, and save the trained network; S4, test the trained global information guided network; The test set is input into the trained global information guided network, and the generated image after style transfer is output. The SSIM and PSNR indicators are used to determine whether the generated image is distorted compared with the original content image and the style image respectively. If not, the network is retained; if so, step S3 is repeated to adjust the weight of the loss function and the number of training rounds; S5. Perform style transfer on content images and style images; Obtain content images and style images, input the content images and style images into the trained global information guidance network for style transfer, and output the generated images after style transfer; The partition adaptive instance normalization includes first sorting the pixels in each channel of the content feature and the style feature according to the numerical value and evenly dividing them into partitions with equal number of pixels, calculating the variance and mean of the content feature and the style feature in each partition, and then implementing the adaptive instance normalization of the content feature and the style feature of each partition according to the sorted corresponding position, and restoring the order of each pixel position after normalization; The cross-channel partitioned attention module includes performing a convolution operation on the features obtained by applying cross attention, sorting the features on each channel in ascending order of value and dividing them into intervals containing the same number of pixels, calculating the average value of each interval and expressing it as a vector to obtain a mean vector, performing a squeeze-excitation operation to obtain a weight vector of each channel, multiplying the weight vector by the corresponding interval, and restoring the position of each pixel; The calculation formula of the color gamut mean loss function is: Where c is the number of color channels, p is the number of channel color gamuts, Ov ij To generate the mean vector of the jth color domain of the i-th color channel of the image, Sv ij is the mean vector of the jth color domain of the i-th color channel of the style image.
2. According to claim 1, a method for achieving real image style transfer based on a global information guided network is characterized in that: The preprocessing of the content image and the style image in step S1 includes: cropping the content image and the style image into a uniform size of 256×256, and normalizing the pixel values of the content image and the style image to a range of [-1, 1].
3. The method for achieving real image style transfer based on a global information guided network according to claim 1, characterized in that: The VGG-19 network extracts features of the content image and the style image at the conv1_1, conv2_1, conv3_1, and conv4_1 layers, respectively, and outputs content features and style features.
4. The method for achieving real image style transfer based on a global information guided network according to claim 1, characterized in that: The encoder has a total of four convolutional layers, the convolution kernel size of each convolutional layer is 3×3, the number of convolution kernels in the next layer is twice that of the previous layer, and the convolutional layers all use the ReLU function as the activation function.
5. The method for achieving real image style transfer based on global information guided network according to claim 1, characterized in that: The calculation formula of the total loss function is: Where λ1, λ2, λ3 and λ4 are weight hyperparameters.
6. The method for achieving real image style transfer based on global information guided network according to claim 1, characterized in that: In the Adam optimizer, the exponential decay rate β1 is set to 0.9, the exponential decay rate β2 is set to 0.999, ∈=10 -8 .
7. The method for achieving real image style transfer based on a global information guided network according to claim 1, characterized in that: Network training uses a dynamic learning rate decay strategy. The current learning rate Where epoch is the current training round and the initial learning rate of the network is lr origin Set to 10 -3 , learning rate decay value lr decay Set to 0.05.
