Video Moiré Removal Method Based on Linear Sparse Attention Transformer
Through the video de-moire method based on linear sparse attention Transformer, combined with spatial and temporal attention, the complexity of video moiré removal is solved, an efficient and low-cost video de-moire effect is achieved, and the video de-moire effect is significantly improved. The visual effects and performance of moiré.
Patent Information
- Application Number
- CN202210649880.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-06-10
- Publication Date
- 2024-09-06
- Estimated Expiration
- 2042-06-10
AI Technical Summary
It is difficult to effectively remove moiré in videos with existing technology, and the image de-moiré method cannot be directly applied to videos, resulting in artifacts and flickering when removing moiré from videos, and is time-consuming and costly.
A video de-moiré pattern method based on linear sparse attention Transformer is adopted. Through the feature extraction module, spatial Transformer module and temporal Transformer module, the spatial and temporal attention are combined to remove moiré patterns, and the video frame is restored through the image reconstruction module, using sparse matrix constraints. and loss function to optimize the de-moire effect.
It achieves high-quality removal of video moiré with low computational complexity, avoids artifacts and flickers, significantly improves the visual effects and performance of video de-moire, and reduces labor and time costs.
Smart Images

Figure CN114881888B
Abstract
Description
Technical Field
[0001] The present invention belongs to the field of video processing and computer vision technology, and in particular relates to a video moiré removal method based on a linear sparse attention Transformer. Background Art
[0002] With the rapid development of mobile devices and multimedia technology, smartphones have become an indispensable tool in daily life. With the support of improved shooting quality, the popularity of mobile photography is also increasing. Images and videos are an indispensable part of modern human communication and information transmission, and are of great significance to the development of all aspects of society. Digital screens are ubiquitous in modern daily life, such as TV screens at home, computers, and large LED screens in public places. It is common to use mobile phones to shoot these screens to quickly save information. Sometimes shooting images and videos is the only practical way to save information. However, when shooting digital screens, moiré patterns often appear and contaminate the underlying clean images and videos. Moiré is caused by the mutual interference between the camera's color filter array (CFA) and the sub-pixel layout of the screen, resulting in color-distorted stripes in the captured images and videos, which seriously reduces the visual quality of images and videos. The development of computer vision and the upgrade of hardware have made it possible to achieve this problem, so many researchers have begun to invest in the research of image moiré removal, but the problem of video moiré removal is still rarely studied.
[0003] Removing moiré is a challenging task because the shape and color of moiré are irregular, and it spans low and high frequencies. Unlike other image and video restoration tasks, such as image or video denoising, image demosaicing, and image or video super-resolution, the moiré removal task needs to deal with complex low-frequency and high-frequency moiré fringes, and restore the details in the image and video. At the same time, moiré also affects the color difference of the captured image. Since the formation of moiré is closely related to the camera imaging process, especially the frequency of the color filter array (CFA). Therefore, many people have proposed methods aimed at improving the imaging pipeline to remove moiré. However, these methods have high computational complexity and are not suitable for practical applications. In 2018, Sun et al. created a large-scale de-moiré benchmark dataset containing more than 100,000 image pairs, the TIP2018 dataset, and proposed a novel multi-resolution fully convolutional network to remove moiré, which greatly promoted the development of image de-moiré tasks. Video demoiré is more difficult than image demoiré. It cannot be simply removed frame by frame using the image demoiré method, because this will introduce artifacts and flickering into the video, and the temporal coherence between frames cannot be guaranteed, resulting in unsatisfactory performance. Therefore, a new method specifically designed to solve the video demoiré task is urgently needed.
[0004] Video demoiré has important practical significance. For a huge number of digital videos, manually removing the moiré in the video will cost a lot of manpower and time. The video demoiré algorithm solves this problem. Developers only need to use the trained video demoiré network to automatically remove the moiré in the video, avoiding repetitive work and saving a lot of time. However, since few people have studied the video demoiré task and the image demoiré method cannot be simply used to solve the video demoiré task, this task still needs to be studied. Summary of the invention
[0005] In order to make up for the gaps and shortcomings of the prior art, the present invention proposes a video demoiré method based on a linear sparse attention Transformer, based on a designed video demoiré network based on a linear sparse attention Transformer to achieve high-quality video demoiré.
[0006] The present invention specifically adopts the following technical solutions:
[0007] A video de-moiré method based on a linear sparse attention transformer, characterized in that: a video de-moiré network based on a linear sparse attention transformer is trained to remove moiré from an input video after the training is completed;
[0008] The video de-moiré network based on linear sparse attention Transformer includes:
[0009] A feature extraction module, used to extract features from video frames;
[0010] The spatial Transformer module uses the spatial attention of the spatial Transformer to capture the location of moiré patterns in a single frame image and remove them;
[0011] The temporal Transformer module uses the temporal attention of the temporal Transformer to capture the complementary information between multiple frames and uses the complementary information of adjacent frames to restore the image;
[0012] And, an image reconstruction module is used to decode the video frame features that have passed through the spatial Transformer module and the temporal Transformer module, and restore them into a de-moiréd video frame with the same scale as the input video.
[0013] Furthermore, the input of the feature extraction module is five adjacent video frames in the same moiré video, wherein the input video frames are represented by I tRepresents that its size is 3×H×W, t∈[1,5]; the module consists of four convolution blocks and three pooling layers. The convolution block is responsible for extracting image features, and the pooling layer uses a 2×2 average pooling layer to reduce the feature scale; the video frame I t Input into the first convolution block to get the feature map Its size is C×H×W. Send it to the pooling layer and the second convolution block to get the feature map Its size is The same, will Send it to the pooling layer and the third convolution block to get Will Send it to the pooling layer and the last convolution block to get and The sizes are and
[0014] Each of the convolution blocks is composed of a convolution layer, an activation layer, a convolution layer and an activation layer in sequence; wherein, both activation layers use the ReLu activation function, both convolution layers use convolution with a convolution kernel of 3×3, the first convolution layer realizes the change of the number of channels, and the second convolution layer maintains the number of channels unchanged.
[0015] Furthermore, the spatial Transformer module consists of nine linear sparse attention demoirification layers and an absolute position encoding;
[0016] The input of the first layer is the feature map of the feature extraction module The input of each subsequent layer is the output of the previous layer, and the output feature map F of the last layer is t It is the final output of the spatial Transformer module. Each layer calculates the spatial attention of the feature map in linear time complexity.
[0017] The absolute position code is and The learnable matrix has the same scale, and the parameters of the matrix are initialized using the Xavier initialization method before training;
[0018] The linear sparse attention de-moiré layer is composed of a spatial self-attention layer, a random dropout layer, a normalization layer, a multi-layer perceptron, a random dropout layer and a normalization layer in sequence; the two random dropout layers both set the neuron dropout probability to 0.1, and the two normalization layers use layer normalization; the multi-layer perceptron is composed of a first fully connected layer, an activation layer and a second fully connected layer in sequence, and the activation layer uses a ReLu activation function; before the input feature map is sent to the spatial self-attention layer, the feature map is first element-wise added to the absolute position code, and then sent to the spatial self-attention layer; the input feature map with the absolute position code and the output of the first random dropout layer have a residual connection, and the output of the first normalization layer and the output of the second random dropout layer have a residual connection;
[0019] The spatial self-attention layer consists of four learnable matrices: Query weight matrix W q , Key weight matrix W k , Value weight matrix W v and the bottleneck matrix W p ; The calculation formula of this layer is as follows:
[0020] Q=Dot(W q ,F in )
[0021] K=Dot(W k ,F in )
[0022] V=Dot(W v ,F in )
[0023] H=Dot(Softmax(Q),Dot(Softmax(K T ),V))
[0024] F out =Dot(W p ,H)
[0025] Among them, F in is the input of the spatial self-attention layer, F out is the output of the spatial self-attention layer, Q, K and V are the Query matrix, Key matrix and Value matrix respectively, K T represents the transposed matrix of the K matrix, H is the attention feature map of the spatial self-attention layer, Dot() represents matrix multiplication calculation; Q, K and W v They are all sparse matrices under the L2 loss function constraint.
[0026] Furthermore, the input of the temporal Transformer module is the final output F of the spatial Transformer module. t ; It consists of four temporal attention demoirification layers, an absolute position code, and an absolute time code;
[0027] This absolute position encoding shares parameters with the absolute position encoding of the spatial Transformer module;
[0028] The absolute time code is a learnable matrix with a scale of 5×8C×1×1, and the parameters of the matrix are initialized using the Xavier initialization method before training;
[0029] The input of the first temporal attention de-moiré layer is F corresponding to five video frames. t , the input of each subsequent layer is the output of the previous layer, and the output feature map of the last layer for the tth frame is the final output of the temporal Transformer module for t frames;
[0030] The temporal attention de-moiré layer is composed of a temporal self-attention layer, a random dropout layer, a normalization layer, a spatial self-attention layer, a random dropout layer, a normalization layer, a multi-layer perceptron, a random dropout layer and a normalization layer in sequence;
[0031] The three random dropout layers all set the neuron dropout probability to 0.1, and the three normalization layers all use layer normalization. The multilayer perceptron consists of a fully connected layer, an activation layer, and a fully connected layer in sequence. The activation layer uses the ReLu activation function.
[0032] The structure of the spatial self-attention layer is the same as that of the spatial self-attention layer in the linear sparse attention demoiré layer;
[0033] Before the input feature map is sent to the temporal self-attention layer, the feature maps of the five input video frames are first spliced in the time dimension, and then the spliced feature map is added element by element with the absolute time code, and then sent to the temporal self-attention layer. Before the feature map is input to the spatial self-attention layer, the spliced feature map needs to be split according to the video frame, and then the absolute position code of the spatial Transformer needs to be added; the input feature map with absolute time code has a residual connection with the output of the first random dropout layer, the feature map with absolute position code has a residual connection with the output of the second random dropout layer, and the output of the second normalization layer has a residual connection with the output of the third random dropout layer;
[0034] The temporal self-attention layer consists of four learnable matrices: Query weight matrix W′ q, Key weight matrix W′ k , Value weight matrix W′ v and bottleneck matrix W′ p ; The calculation formula of this layer is as follows:
[0035]
[0036]
[0037]
[0038] K a =[K 1 ,K 2 ,…,K 5 ]
[0039] V a =[V 1 ,V 2 ,…,V 5 ]
[0040] H t (i,j)=Dot(Softmax(Dot(Q t (i,j),(K a (i,j)) T )),V a (i,j))
[0041] F out = Dot(W′ p ,H)
[0042] Where t represents the tth frame, t∈[1,5], is the input feature belonging to the tth frame in the temporal self-attention layer, F out is the output of the temporal self-attention layer, Q t , K t and V t They are the Query matrix, Key matrix, and Value matrix belonging to the tth frame respectively. Softmax() indicates softmax calculation on the last dimension of the matrix. Dot() indicates matrix multiplication calculation. The superscript T indicates matrix transposition. [] indicates matrix composition operation. K a and V a are the Key matrix and Value matrix of the five frames respectively. H is the attention feature map of the complete temporal self-attention layer. (i, j) indicates the location of the feature. That is, the feature map is divided into multiple 2×2 non-overlapping small squares. (i, j) indicates the location of the small square where the feature is located. H t (i, j) represents the local attention feature of the (i, j) position in H of the tth frame, Ka (i,j) indicates that in K a The local Key matrix at position (i, j) in V a (i,j) indicates that V a The local Value matrix at position (i, j) in t (i,j) indicates that t The local query matrix at position (i, j) in v is a sparse matrix under the L2 loss function constraint.
[0043] Furthermore, the input of the image reconstruction module is the final output of the temporal Transformer module for the third frame. Its scale is It consists of three upsampling blocks, three convolution blocks, a 1×1 convolution and a Tanh activation layer; Input to the first upsampling block to get the feature map Its size is Will And the feature map of the feature extraction module After concatenation by channel, it is input into the first convolution block to obtain the feature map Will Input to the second upsampling block to get the feature map Its size is Will And the feature map of the feature extraction module After concatenation by channel, it is input into the second convolution block to obtain the feature map Will Input to the third upsampling block to get the feature map Its size is C×H×W. And the feature map of the feature extraction module After splicing by channel, it is input into the third convolution block to obtain the feature map Will Input to a 1×1 convolution and a Tanh activation layer to get the output frame image That is, the de-moiré frame corresponding to the moiré video frame I3;
[0044] The upsampling block is composed of an upsampling layer, a convolution layer and an activation layer in sequence. The upsampling layer adopts bilinear interpolation upsampling with a magnification of 2. The convolution layer is a convolution with a convolution kernel of 3×3. At the same time, the convolution layer realizes the change of the number of feature map channels, that is, the number of channels is reduced to half of the original number. The activation layer adopts the ReLu activation function; the convolution block is composed of a convolution layer, an activation layer, a convolution layer, and an activation layer in sequence. The convolution layers are all convolutions with a convolution kernel of 3×3, and the activation layers all adopt the ReLu activation function.
[0045] Furthermore, the loss function for training the video de-moiré network is constructed as follows:
[0046] The overall optimization goal of the network is as follows:
[0047] min(L),
[0048]
[0049] Among them, min(L) means minimizing L, L represents the total loss of the network to remove moiré patterns, represents the Charbonnier loss of the de-moiré image and the clean image, represents the edge texture loss of the de-moiré image and the clean image, represents the loss of the sparse matrices in the constrained spatial Transformer module and the temporal Transformer module;
[0050] Charbonnier loss of the de-moiréd image and the clean image The calculation formula is as follows:
[0051]
[0052] in, represents the de-moiré image corresponding to the third frame of the five moiré video frames, O3 represents the clean image paired with it, ∈ represents the constant for controlling the accuracy, and λ C represents the weight of the loss;
[0053] The edge texture loss of the de-moiré image and the clean image The calculation formula is as follows:
[0054]
[0055] Among them, || ||1 is the absolute value operation, Sobel * Represents the improved Sobel filter in different directions, Sobel * () represents the convolution operation, λ ASL represents the weight of the loss;
[0056] The color loss of the de-moiréd image and the clean image The calculation formula is as follows:
[0057]
[0058] Where G represents the Gaussian blur kernel, represents the blurred de-moiré image, G(O3) represents the blurred clean image, is the square operation of the two-norm, λ cr represents the weight of the loss;
[0059] The loss of sparse matrices in the constrained spatial Transformer module and the temporal Transformer module The calculation formula is as follows:
[0060]
[0061] Among them, Q * and K * Represents the query matrix and key matrix calculated in all spatial self-attention layers in the spatial Transformer module. Represents the Value weight matrix calculated in all spatial self-attention layers in the spatial Transformer module and the temporal Transformer module. Represents the Value weight matrix calculated from all the time self-attention layers in the temporal Transformer module, is the square operation of taking the absolute value, λ sparse Represents the weight of the loss.
[0062] Furthermore, the video de-moiré network based on linear sparse attention Transformer is trained by processing the videos in the original data set using the training set to obtain moiré videos and clean video pairs, and each video is decoded into video frames. At the same time, each frame of the video is preprocessed to obtain the image, which specifically includes the following steps:
[0063] Step S11, obtaining an original data set, wherein the moiré video and the corresponding clean video in the original data set have the same size and one-to-one correspondence in content, firstly decoding each video in the original data set at the same frame rate to obtain video frames; videos from the same video content are combined into a moiré video and a clean video pair, and ensuring that the content of each frame in the video pair is one-to-one correspondence;
[0064] Step S12, performing the same random flipping on all video frames of the moiré video and the clean video pair, and then randomly cropping them into moiré video frames and clean video frames of size H×W, and maintaining the relationship between the moiré video and the clean video pair;
[0065] Step S13: normalize all moiré video frames and clean video frames of size H×W; given a frame image I(i, j), calculate the normalized frame image The formula is as follows:
[0066]
[0067] Among them, (i, j) represents the position of the pixel.
[0068] Furthermore, the training process specifically includes the following steps:
[0069] Step S41: randomly select a pair of moiré video and clean video from the training set, then randomly select five adjacent video frames from the moiré video, and simultaneously select five corresponding video frames from the corresponding clean video. The moiré video frame and the clean video frame are respectively denoted as I t and O r , t∈[1,5];
[0070] Step S42: The first stage of training the video de-moiré network based on linear sparse attention Transformer: input five video frames I t , the de-moiré intermediate frame is obtained through network calculation Calculate the total loss L for removing moiré patterns, use the back propagation method to calculate the gradients of each parameter in the network, and use the Adam optimization method to update the parameters, where the learning rate is increased from 10 -4 Reduce to 10 -5 ;
[0071] Step S43: The second stage of training the video demoiré network based on linear sparse attention Transformer: The input and training methods are the same as those in the first stage, except that the color loss weight λ in the total demoiré loss L is replaced by cr Set to 0, and the learning rate is increased by 10 -5 Reduce to 10 -6 , and fine-tune the network.
[0072] Furthermore, after the training is completed, the specific operation of removing moiré from the input video is as follows: for a new moiré video, two blank frames are inserted at the beginning and the end of the video, and the first five frames of the video are first input into the network for calculation to obtain the de-moiré video frame corresponding to the first frame of the original moiré video. Then, the second to sixth frames of the video are input into the network to obtain the de-moiré video frame corresponding to the second frame of the original moiré video. The same operation is subsequently performed until the de-moiré video frames corresponding to all moiré video frames are obtained.
[0073] An electronic device comprises a memory, a processor and a computer program stored in the memory and executable on the processor, wherein when the processor executes the program, the video de-moiré method based on the linear sparse attention Transformer as described above is implemented.
[0074] A computer-readable storage medium having a computer program stored thereon, characterized in that when the program is executed by a processor, the video demoirification method based on the linear sparse attention Transformer as described above is implemented.
[0075] Compared with the prior art, the present invention and its preferred embodiment have the following beneficial effects:
[0076] By using spatial attention and temporal attention, spatial information and temporal information between different frames are effectively used to remove moiré and supplement details, thereby preventing artifacts and flickering in the video. This method uses a linear attention calculation method, which can effectively reduce the square time complexity of the original Transformer attention calculation method to a linear time complexity, greatly reducing the amount of network calculation and improving the actual application effect of the network. At the same time, in the linear attention calculation process, a loss function is used to constrain the calculation matrix to a sparse matrix to achieve a more effective and stable moiré removal effect. The present invention can remove moiré in the video at a low computational complexity and generate a high-quality moiré-free clean video. The generated moiré-free video has improved visual effects and performance indicators. Therefore, the present invention has strong practicality and broad application prospects. BRIEF DESCRIPTION OF THE DRAWINGS
[0077] Figure 1 The figure is a schematic diagram of the implementation process of the embodiment of the present invention.
[0078] Figure 2 Schematic diagram of the structure of a video de-moiré network according to an embodiment of the present invention.
[0079] Figure 3 Schematic diagram of the structure of the spatial Transformer module of an embodiment of the present invention.
[0080] Figure 4 Schematic diagram of the structure of the temporal Transformer module according to an embodiment of the present invention. DETAILED DESCRIPTION
[0081] In order to make the features and advantages of this patent more obvious and easy to understand, the following embodiments are specifically described in detail as follows:
[0082] It should be noted that the following detailed descriptions are illustrative and are intended to provide further explanation of the present application. Unless otherwise specified, all technical and scientific terms used in this specification have the same meanings as those commonly understood by those skilled in the art to which the present application belongs.
[0083] It should be noted that the terms used herein are only for describing specific embodiments and are not intended to limit the exemplary embodiments according to the present application. As used herein, unless the context clearly indicates otherwise, the singular form is also intended to include the plural form. In addition, it should be understood that when the terms "comprise" and / or "include" are used in this specification, it indicates the presence of features, steps, operations, devices, components and / or combinations thereof.
[0084] like Figure 1 As shown, this embodiment further introduces the scheme of the present invention in detail in a step-by-step manner according to a specific operation example of implementing a video demoirification method based on a linear sparse attention Transformer.
[0085] The specific steps can be summarized as follows:
[0086] Step S1, processing the videos in the original data set to obtain moiré videos and clean video pairs, and decoding each video into video frames, and preprocessing each frame of the video to obtain a training set; Step S1 specifically includes the following steps:
[0087] Step S11, obtaining an original data set, wherein the moiré video and the corresponding clean video in the original data set have the same size and one-to-one correspondence in content, firstly decoding each video in the original data set at the same frame rate to obtain video frames; combining videos from the same video content into a moiré video and a clean video pair, and ensuring that the content of each frame in the video pair is one-to-one correspondence.
[0088] Step S12, performing the same random flipping on all video frames of the moiré video and the clean video pair, and then randomly cropping them into moiré video frames and clean video frames of size H×W, and maintaining the relationship between the moiré video and the clean video pair;
[0089] Step S13: normalize all moiré video frames and clean video frames of size H×W; given a frame image I(i, j), calculate the normalized frame image The formula is as follows:
[0090]
[0091] Among them, (i, j) represents the position of the pixel.
[0092] Step S2: Construct a video de-moiré network based on linear sparse attention Transformer. Figure 2 As shown in the figure, the video de-moiré network consists of four parts: feature extraction module, spatial Transformer module, temporal Transformer module and image reconstruction module.
[0093] Specifically, step S2 includes the following steps:
[0094] Step S21, constructing a feature extraction module, and using the feature extraction module to extract features from the video frame to prepare for subsequent moiré removal;
[0095] like Figure 2 As shown, the input of the feature extraction module is five adjacent video frames in the same moiré video, where the input video frame is represented by I t Represents that its size is 3×H×W, t∈[1,5]; the module consists of four convolution blocks and three pooling layers. The convolution block is responsible for extracting image features, and the pooling layer uses a 2×2 average pooling layer to reduce the feature scale; the video frame I t Input into the first convolution block to get the feature map Its size is C×H×W. Send it to the pooling layer and the second convolution block to get the feature map Its size is The same, will Send it to the pooling layer and the third convolution block to get Will Send it to the pooling layer and the last convolution block to get and The sizes are and
[0096] Each convolution block consists of a convolution layer, an activation layer, a convolution layer, and an activation layer in order; both activation layers use the ReLu activation function, and both convolution layers use a 3×3 convolution kernel. The first convolution layer changes the number of channels, such as the number of channels in the first convolution block changes from 3 to C, the number of channels in other convolution blocks doubles, and the second convolution layer maintains the number of channels unchanged. Step S22: Construct a spatial Transformer module to use the spatial attention of the spatial Transformer to capture the location of moiré in a single frame image and remove it in order to achieve a better de-moiré effect.
[0097] like Figure 3 As shown in Figure 2, the spatial Transformer module consists of nine linear sparse attention de-moiré layers and an absolute position encoder, where the input of the first layer is the feature map of the feature extraction module. The input of each subsequent layer is the output of the previous layer, and the output feature map F of the last layer is t is the final output of the spatial Transformer module. Each layer calculates the spatial attention of the feature map in linear time complexity, helping the network to find and remove areas with severe moiré. The absolute position encoding is The learnable matrix has the same scale, and the parameters of the matrix are initialized using the Xavier initialization method before training;
[0098] The linear sparse attention de-moiré layer is composed of a spatial self-attention layer, a random dropout layer, a normalization layer, a multi-layer perceptron, a random dropout layer, and a normalization layer in sequence; the two random dropout layers set the probability of neuron dropout to 0.1, the two normalization layers use layer normalization, the multi-layer perceptron is composed of a fully connected layer, an activation layer, and a fully connected layer in sequence, and the activation layer uses the ReLu activation function; before the input feature map is sent to the spatial self-attention layer, the feature map is first element-wise added to the absolute position code, and then sent to the spatial self-attention layer; there is a residual connection between the input feature map with the absolute position code and the output of the first random dropout layer, and there is a residual connection between the output of the first normalization layer and the output of the second random dropout layer;
[0099] The spatial self-attention layer consists of four learnable matrices: the query weight matrix W q , Key weight matrix W k , Value weight matrix W v and the bottleneck matrix W p ; The calculation formula of this layer is as follows:
[0100] Q=Dot(W q ,F in )
[0101] K=Dot(W k ,F in )
[0102] V=Dot(W v ,F in )
[0103] H=Dot(Softmax(Q),Dot(Softmax(K T ),V))
[0104] F out =Dot(W p ,H)
[0105] Among them, F in is the input of the spatial self-attention layer, F out is the output of this layer, Q, K and V are Query matrix, Key matrix and Value matrix respectively, K T represents the transposed matrix of the K matrix, H is the attention feature map of the spatial self-attention layer, Dot() represents matrix multiplication calculation; Q, K and W v They are all sparse matrices under the L2 loss function constraint.
[0106] Step S23: construct a temporal Transformer module to use the temporal attention of the temporal Transformer to capture the complementary information between multiple frames of images, and use the complementary information of adjacent frames to perform image restoration to further improve the de-moiré effect.
[0107] like Figure 4 As shown, the input of the temporal Transformer module is the final output F of the spatial Transformer module. t ; This module consists of four temporal attention demoiré layers, an absolute position code and an absolute time code. The absolute position code shares parameters with the absolute position code of the spatial Transformer module. The absolute time code is a learnable matrix of scale 5×8C×1×1. The Xavier initialization method is used to initialize the parameters of the matrix before training. The input of the first temporal attention demoiré layer is the F corresponding to five video frames. t , the input of each subsequent layer is the output of the previous layer, and the output feature map of the last layer for the tth frame is the final output of the temporal Transformer module for t frames; the temporal attention de-moiré layer and the linear sparse attention de-moiré layer are very similar in structure. The main difference is that the temporal attention de-moiré layer has an additional temporal self-attention layer, which can be used to capture complementary information between adjacent video frames and help remove moiré through this temporal complementary information;
[0108] The temporal attention de-moiré layer is composed of a temporal self-attention layer, a random dropout layer, a normalization layer, a spatial self-attention layer, a random dropout layer, a normalization layer, a multi-layer perceptron, a random dropout layer, and a normalization layer in sequence; the three random dropout layers all set the neuron dropout probability to 0.1, and the three normalization layers all use layer normalization. The multi-layer perceptron is composed of a fully connected layer, an activation layer, and a fully connected layer in sequence. The activation layer uses the ReLu activation function. The structure of the spatial self-attention layer is the same as that of the spatial self-attention layer in the linear sparse attention de-moiré layer; before sending the input feature map to the temporal self-attention layer, the five input videos are first The feature maps of the frames are concatenated in the time dimension, and then the concatenated feature maps are element-by-element added with the absolute time code, and then sent to the temporal self-attention layer. Before the feature maps are input to the spatial self-attention layer, the concatenated feature maps need to be split according to the video frames, and then the absolute position code of the spatial Transformer needs to be added. There is a residual connection between the input feature map with the absolute time code and the output of the first random dropout layer, a residual connection between the feature map with the absolute position code and the output of the second random dropout layer, and a residual connection between the output of the second normalization layer and the output of the third random dropout layer.
[0109] The temporal self-attention layer consists of four learnable matrices: the query weight matrix W′ q , Key weight matrix W′ k , Value weight matrix W′ v and bottleneck matrix W′ p ; The calculation formula of this layer is as follows:
[0110]
[0111]
[0112]
[0113] K a =[K 1 ,K 2 ,…,K 5 ]
[0114] V a =[V 1 ,V 2 ,…,V 5 ]
[0115] H t (i,j)=Dot(Softmax(Dot(Q t (i,j),(K a (i,j)) T )),V a (i,j))
[0116] F out = Dot(W′ p ,H)
[0117] Where t represents the tth frame, t∈[1,5], is the input feature belonging to the tth frame in the temporal self-attention layer, F out is the output of this layer, Q t , K t and V t They are the Query matrix, Key matrix, and Value matrix belonging to the tth frame respectively. Softmax() indicates softmax calculation on the last dimension of the matrix. Dot() indicates matrix multiplication calculation. The superscript T indicates matrix transposition. [] indicates matrix composition operation. K a and V aare the Key matrix and Value matrix of the five frames respectively. H is the attention feature map of the complete temporal self-attention layer. (i, j) indicates the location of the feature. Specifically, the feature map is divided into multiple 2×2 non-overlapping small squares. (i, j) indicates the location of the small square where the feature is located. H t (i, j) represents the local attention feature of the (i, j) position in H of the tth frame, K a (i,j) indicates that in K a The local Key matrix at position (i, j) in V a (i,j) indicates that V a The local Value matrix at position (i, j) in t (i,j) indicates that t The local query matrix at position (i, j) in v is a sparse matrix under the L2 loss function constraint.
[0118] Step S24: construct an image reconstruction module to use the image reconstruction module to decode the video frame features that have passed through the spatial Transformer module and the temporal Transformer module, and restore them into a de-moiréd video frame with the same scale as the input video.
[0119] like Figure 2 As shown, the input of the image reconstruction module is the final output of the temporal Transformer module for the intermediate frame (the third frame) Its scale is The module consists of three upsampling blocks, three convolution blocks, a 1×1 convolution and a Tanh activation layer; Input to the first upsampling block to get the feature map Its size is Will And the feature map of the feature extraction module After concatenation by channel, it is input into the first convolution block to obtain the feature map Will Input to the second upsampling block to get the feature map Its size is Will And the feature map of the feature extraction module After concatenation by channel, it is input into the second convolution block to obtain the feature map Will Input to the third upsampling block to get the feature map Its size is C×H×W. And the feature map of the feature extraction module After splicing by channel, it is input into the third convolution block to obtain the feature map Will Input to a 1×1 convolution and a Tanh activation layer to get the output frame image That is, the de-moiré frame corresponding to the moiré video frame I3;
[0120] The upsampling block is composed of an upsampling layer, a convolution layer, and an activation layer in sequence. The upsampling layer uses bilinear interpolation upsampling with a magnification of 2. The convolution layer is a convolution with a convolution kernel of 3×3. At the same time, the convolution layer realizes the change of the number of channels of the feature map, that is, the number of channels is reduced to half of the original number. The activation layer uses the ReLu activation function; the convolution block is composed of a convolution layer, an activation layer, a convolution layer, and an activation layer in sequence. The convolution layers are all convolutions with a convolution kernel of 3×3, and the activation layers also use the ReLu activation function;
[0121] Step S3: construct a loss function for training a video de-moiré network.
[0122] Step S3 specifically includes the following steps:
[0123] Step S31, construct the overall optimization target of the entire network; the optimization target is as follows:
[0124] min(L),
[0125]
[0126] Among them, min(L) means minimizing L, L represents the total loss of the network to remove moiré patterns, represents the Charbonnier loss of the de-moiré image and the clean image, represents the edge texture loss of the de-moiré image and the clean image, represents the loss of the sparse matrices in the constrained spatial Transformer module and the temporal Transformer module;
[0127] Step S32, constructing the Charbonnier loss of the de-moiré image and the clean image; The calculation formula is as follows:
[0128]
[0129] in, represents the de-moiré image corresponding to the third frame of the five moiré video frames, O3 represents the clean image paired with it, ∈ represents the constant for controlling the accuracy, and λ C represents the weight of the loss;
[0130] Step S33, constructing edge texture loss of the de-moiré image and the clean image; The calculation formula is as follows:
[0131]
[0132] Among them, || ||1 is the absolute value operation, Sobel * Represents the improved Sobel filter in different directions, Sobel * () represents the convolution operation, λ ASL represents the weight of the loss;
[0133] Step S34, constructing the color loss of the de-moiré image and the clean image; The calculation formula is as follows:
[0134]
[0135] Where G represents the Gaussian blur kernel, represents the blurred de-moiré image, G(O3) represents the blurred clean image, is the square operation of the two-norm, λ cr represents the weight of the loss;
[0136] Step S35, constructing the loss of sparse matrices in the constrained spatial Transformer module and the temporal Transformer module; The calculation formula is as follows:
[0137]
[0138] Among them, Q * and K * Represents the query matrix and key matrix calculated in all spatial self-attention layers in the spatial Transformer module. Represents the Value weight matrix calculated in all spatial self-attention layers in the spatial Transformer module and the temporal Transformer module. Represents the Value weight matrix calculated from all the time self-attention layers in the temporal Transformer module, is the square operation of taking the absolute value, λ sparse represents the weight of the loss;
[0139] Step S4: using the training data set to train the video moiré removal network.
[0140] Step S4 specifically includes the following steps:
[0141] Step S41: randomly select a pair of moiré video and clean video from the training data set, then randomly select five adjacent video frames from the moiré video, and simultaneously select five corresponding video frames from the corresponding clean video. The moiré video frame and the clean video frame are respectively denoted as I t and O t , t∈[1,5];
[0142] Step S42: The first stage of training a video de-moiré network based on a linear sparse attention Transformer; input five video frames I t , the de-moiré intermediate frame is obtained through network calculation Calculate the total loss L for removing moiré patterns, use the back propagation method to calculate the gradients of each parameter in the network, and use the Adam optimization method to update the parameters, where the learning rate is increased from 10 -4 Slowly reduce to 10 -5 ;
[0143] Step S43, the second stage of training the video demoiré network based on linear sparse attention Transformer; the input and training methods are the same as the first stage, except that the color loss weight λ in the total demoiré loss L is replaced by cr Set to 0, and the learning rate is increased by 10 -5 Slowly reduce to 10 -6 , perform fine-tuning training on the network;
[0144] In this embodiment, the entire training process is iterated 4 million times, and in each iteration, a plurality of video pairs are randomly sampled as a batch for training, the first 2.5 million iterations are for the first stage training of step S42, and the remaining 1.5 million iterations are for the second stage training of step S43;
[0145] Step S5, input the new moiré video into the trained video de-moiré network, and output a clean video after de-moiré. Specifically, for a new moiré video, two blank frames are inserted at the beginning and the end of the video, and the first five frames of the video are first input into the network for calculation to obtain the de-moiré video frame corresponding to the first frame of the original moiré video, and then the second to sixth frames of the video are input into the network to obtain the de-moiré video frame corresponding to the second frame of the original moiré video, and the same operation is subsequently performed until the de-moiré video frames corresponding to all the moiré video frames are obtained.
[0146] This embodiment also provides a video demoirification system based on a linear sparse attention Transformer, including a memory, a processor, and computer program instructions stored in the memory and capable of being executed by the processor. When the processor executes the computer program instructions, the above-mentioned method steps can be implemented.
[0147] It will be appreciated by those skilled in the art that embodiments of the present invention may be provided as methods, devices, or computer program products. Therefore, the present invention may take the form of a complete hardware embodiment, a complete software embodiment, or an embodiment combining software and hardware. Moreover, the present invention may take the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.
[0148] The present invention is described with reference to flowcharts of methods, devices (apparatus), and computer program products according to embodiments of the present invention. It should be understood that each process in the flowchart, as well as the combination of processes in the flowchart, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing device to produce a machine, so that the instructions executed by the processor of the computer or other programmable data processing device generate instructions for implementing the process in the flowchart. Figure 1 A device that specifies functions in a process or multiple processes.
[0149] These computer program instructions may also be stored in a computer-readable memory capable of directing a computer or other programmable data processing device to operate in a specific manner, so that the instructions stored in the computer-readable memory produce an article of manufacture comprising an instruction device, which implements the process Figure 1 A process or multiple functions specified in a flowchart.
[0150] These computer program instructions can also be loaded onto a computer or other programmable data processing device so that a series of operating steps are executed on the computer or other programmable device to produce a computer-implemented process, thereby providing instructions for implementing the process. Figure 1 The steps of a specified function in a process or multiple processes.
[0151] The above is only a preferred embodiment of the present invention, and does not limit the present invention in other forms. Any technician familiar with the profession may use the above disclosed technical content to change or modify it into an equivalent embodiment with equivalent changes. However, any simple modification, equivalent change and modification made to the above embodiment according to the technical essence of the present invention without departing from the technical solution of the present invention still belongs to the protection scope of the technical solution of the present invention.
[0152] This patent is not limited to the above-mentioned optimal implementation mode. Anyone can derive other forms of video demoirification methods based on linear sparse attention Transformer under the inspiration of this patent. All equal changes and modifications made according to the scope of the patent application of the present invention should be covered by this patent.
Claims
1. A video de-moiré method based on linear sparse attention Transformer, characterized by: Train a video de-moiré network based on a linear sparse attention transformer to remove moiré from the input video after training. The video de-moiré network based on linear sparse attention Transformer includes: A feature extraction module, used to extract features from video frames; The spatial Transformer module uses the spatial attention of the spatial Transformer to capture the location of moiré patterns in a single frame image and remove them; The temporal Transformer module uses the temporal attention of the temporal Transformer to capture the complementary information between multiple frames and uses the complementary information of adjacent frames to restore the image; and, an image reconstruction module, for decoding the video frame features after the spatial Transformer module and the temporal Transformer module, and restoring the de-moiréd video frame to the same scale as the input video; The input of the feature extraction module is five adjacent video frames in the same moiré video, where the input video frames are represented by I t Represents that its size is 3×H×W, t∈[1,5]; the module consists of four convolution blocks and three pooling layers. The convolution block is responsible for extracting image features, and the pooling layer uses a 2×2 average pooling layer to reduce the feature scale; the video frame I t Input into the first convolution block to get the feature map Its size is C×H×W. Send it to the pooling layer and the second convolution block to get the feature map Its size is The same, will Send it to the pooling layer and the third convolutional block to get Will Send it to the pooling layer and the last convolution block to get and The sizes are and Each of the convolution blocks is composed of a convolution layer, an activation layer, a convolution layer and an activation layer in sequence; wherein both activation layers use the ReLu activation function, both convolution layers use convolution with a convolution kernel of 3×3, the first convolution layer realizes the change of the number of channels, and the second convolution layer maintains the number of channels unchanged; The spatial Transformer module consists of nine linear sparse attention demolarization layers and an absolute position encoder; The input of the first layer is the feature map of the feature extraction module The input of each subsequent layer is the output of the previous layer, and the output feature map F of the last layer is t It is the final output of the spatial Transformer module. Each layer calculates the spatial attention of the feature map in linear time complexity. The absolute position code is and The learnable matrix has the same scale, and the parameters of the matrix are initialized using the Xavier initialization method before training; The linear sparse attention de-moiré layer is composed of a spatial self-attention layer, a random dropout layer, a normalization layer, a multi-layer perceptron, a random dropout layer and a normalization layer in sequence; the two random dropout layers both set the neuron dropout probability to 0.1, and the two normalization layers use layer normalization; the multi-layer perceptron is composed of a first fully connected layer, an activation layer and a second fully connected layer in sequence, and the activation layer uses a ReLu activation function; before the input feature map is sent to the spatial self-attention layer, the feature map is first element-wise added to the absolute position code, and then sent to the spatial self-attention layer; the input feature map with the absolute position code and the output of the first random dropout layer have a residual connection, and the output of the first normalization layer and the output of the second random dropout layer have a residual connection; The spatial self-attention layer consists of four learnable matrices: Query weight matrix W q , Key weight matrix W k , Value weight matrix W v and the bottleneck matrix W p ; The calculation formula of this layer is as follows: Q=Dot(W q ,F in ) K=Dot(W k ,F in ) V=Dot(W v ,F in ) H=Dot(Softmax(Q),Dot(Softmax(K T ),V)) F out =Dot(W p ,H) Among them, F in is the input of the spatial self-attention layer, F out is the output of the spatial self-attention layer, Q, K and V are the Query matrix, Key matrix and Value matrix respectively, K T represents the transposed matrix of the K matrix, H is the attention feature map of the spatial self-attention layer, Dot() represents matrix multiplication calculation; Q, K and W v They are all sparse matrices under the L2 loss function constraint; The input of the temporal Transformer module is the final output F of the spatial Transformer module. t ; It consists of four temporal attention demoirification layers, an absolute position code, and an absolute time code; This absolute position encoding shares parameters with the absolute position encoding of the spatial Transformer module; The absolute time code is a learnable matrix with a scale of 5×8C×1×1, and the parameters of the matrix are initialized using the Xavier initialization method before training; The input of the first temporal attention de-moiré layer is F corresponding to five video frames. t , the input of each subsequent layer is the output of the previous layer, and the last layer outputs the feature map of the tth frame is the final output of the temporal Transformer module for t frames; The temporal attention de-moiré layer is composed of a temporal self-attention layer, a random dropout layer, a normalization layer, a spatial self-attention layer, a random dropout layer, a normalization layer, a multi-layer perceptron, a random dropout layer and a normalization layer in sequence; The three random dropout layers all set the neuron dropout probability to 0.1, and the three normalization layers all use layer normalization. The multilayer perceptron consists of a fully connected layer, an activation layer, and a fully connected layer in sequence. The activation layer uses the ReLu activation function. The structure of the spatial self-attention layer is the same as that of the spatial self-attention layer in the linear sparse attention demoiré layer; Before the input feature map is sent to the temporal self-attention layer, the feature maps of the five input video frames are first spliced in the time dimension, and then the spliced feature map is added element by element with the absolute time code, and then sent to the temporal self-attention layer. Before the feature map is input to the spatial self-attention layer, the spliced feature map needs to be split according to the video frame, and then the absolute position code of the spatial Transformer needs to be added; the input feature map with absolute time code has a residual connection with the output of the first random dropout layer, the feature map with absolute position code has a residual connection with the output of the second random dropout layer, and the output of the second normalization layer has a residual connection with the output of the third random dropout layer; The temporal self-attention layer consists of four learnable matrices: Query weight matrix W′ q , Key weight matrix W′ k , Value weight matrix W′ v and bottleneck matrix W′ p ; The calculation formula of this layer is as follows: K a =[K 1 ,K 2 ,...,K 5 ] V a =[V 1 ,V 2 ,...,V 5 ] H t (i,j)=Dot(Softmax(Dot(Q t (i,j),(K a (i,j)) T )),V a (i,j)) F out =Dot(W′ p ,H) Where t represents the tth frame, t∈[1,5], is the input feature belonging to the tth frame in the temporal self-attention layer, F out is the output of the temporal self-attention layer, Q t , K t and V t They are the Query matrix, Key matrix, and Value matrix belonging to the tth frame respectively. Softmax() indicates softmax calculation on the last dimension of the matrix. Dot() indicates matrix multiplication calculation. The superscript T indicates matrix transposition. [] indicates matrix composition operation. K a and V a are the Key matrix and Value matrix of the five frames respectively. H is the attention feature map of the complete temporal self-attention layer. (i, j) indicates the location of the feature. That is, the feature map is divided into multiple 2×2 non-overlapping small squares. (i, j) indicates the location of the small square where the feature is located. H t (i, j) represents the local attention feature of the (i, j) position in H of the tth frame, K a (i, j) indicates that a The local Key matrix at position (i, j) in V a (i, j) indicates that V a The local Value matrix at position (i, j) in t (i, j) indicates that t The local query matrix at position (i, j) in v It is a sparse matrix under the L2 loss function constraint; The input of the image reconstruction module is the final output of the temporal Transformer module for the third frame. Its scale is It consists of three upsampling blocks, three convolution blocks, a 1×1 convolution and a Tanh activation layer; Input to the first upsampling block to get the feature map Its size is Will And the feature map of the feature extraction module After concatenation by channel, it is input into the first convolution block to obtain the feature map Will Input to the second upsampling block to get the feature map Its size is Will And the feature map of the feature extraction module After concatenation by channel, it is input into the second convolution block to obtain the feature map Will Input to the third upsampling block to get the feature map Its size is C×H×W. And the feature map of the feature extraction module After splicing by channel, it is input into the third convolution block to obtain the feature map Will Input to a 1×1 convolution and a Tanh activation layer to get the output frame image That is, the de-moiré frame corresponding to the moiré video frame I3; The upsampling block is composed of an upsampling layer, a convolution layer and an activation layer in sequence. The upsampling layer adopts bilinear interpolation upsampling with a magnification of 2. The convolution layer is a convolution with a convolution kernel of 3×3. At the same time, the convolution layer realizes the change of the number of feature map channels, that is, the number of channels is reduced to half of the original number. The activation layer adopts the ReLu activation function; the convolution block is composed of a convolution layer, an activation layer, a convolution layer, and an activation layer in sequence. The convolution layers are all convolutions with a convolution kernel of 3×3, and the activation layers all adopt the ReLu activation function.
2. The video demoirification method based on linear sparse attention Transformer according to claim 1, characterized in that: The loss function used to train the video demoiré network is constructed as follows: The overall optimization goal of the network is as follows: min(L), Among them, min(L) means minimizing L, L represents the total loss of the network to remove moiré patterns, represents the Charbonnier loss of the de-moiré image and the clean image, Represents the edge texture loss of the de-moiré image and the clean image, Represents the loss of the sparse matrix in the constrained spatial Transformer module and the temporal Transformer module; Charbonnier loss of the de-moiréd image and the clean image The calculation formula is as follows: in, represents the de-moiré image corresponding to the third frame of the five moiré video frames, O3 represents the clean image paired with it, ∈ represents the constant for controlling the accuracy, and λ c represents the weight of the loss; The edge texture loss of the de-moiré image and the clean image The calculation formula is as follows: Among them, || ||1 is the absolute value operation, Sobel * Represents the improved Sobel filter in different directions, Sobel * () represents the convolution operation, λ ASL represents the weight of the loss; The color loss of the de-moiréd image and the clean image The calculation formula is as follows: Where G represents the Gaussian blur kernel, represents the blurred de-moiré image, G(O3) represents the blurred clean image, is the square operation of the two-norm, λ cr represents the weight of the loss; The loss of sparse matrices in the constrained spatial Transformer module and the temporal Transformer module The calculation formula is as follows: Among them, Q * and K * Represents the query matrix and key matrix calculated in all spatial self-attention layers in the spatial Transformer module. Represents the Value weight matrix calculated in all spatial self-attention layers in the spatial Transformer module and the temporal Transformer module. Represents the Value weight matrix calculated from all the time self-attention layers in the temporal Transformer module, is the square operation of taking the absolute value, λ sparse Represents the weight of the loss.
3. The video demoirification method based on linear sparse attention Transformer according to claim 2, characterized in that: Training the video de-moiré network based on linear sparse attention Transformer uses the training set to process the videos in the original data set to obtain moiré videos and clean video pairs, and decodes each video into video frames. At the same time, each frame of the video is preprocessed to obtain the image, which specifically includes the following steps: Step S11, obtaining an original data set, wherein the moiré video and the corresponding clean video in the original data set have the same size and one-to-one correspondence in content, firstly decoding each video in the original data set at the same frame rate to obtain video frames; videos from the same video content are combined into a moiré video and a clean video pair, and ensuring that the content of each frame in the video pair is one-to-one correspondence; Step S12, performing the same random flipping on all video frames of the moiré video and the clean video pair, and then randomly cropping them into moiré video frames and clean video frames of size H×W, and maintaining the relationship between the moiré video and the clean video pair; Step S13: normalize all moiré video frames and clean video frames of size H×W; given a frame image I(i, j), calculate the normalized frame image The formula is as follows: Where (i, j) represents the position of the pixel.
4. The video de-moiré method based on linear sparse attention Transformer according to claim 3, characterized in that: The training process specifically includes the following steps: Step S41: randomly select a pair of moiré video and clean video from the training set, then randomly select five adjacent video frames from the moiré video, and simultaneously select five corresponding video frames from the corresponding clean video. The moiré video frame and the clean video frame are respectively denoted as I t and O t , t∈[1,5]; Step S42: The first stage of training the video de-moiré network based on linear sparse attention Transformer: input five video frames I t , the de-moiré intermediate frame is obtained through network calculation Calculate the total loss L for removing moiré patterns, use the back propagation method to calculate the gradients of each parameter in the network, and use the Adam optimization method to update the parameters, where the learning rate is increased from 10 -4 Reduce to 10 -5 ; Step S43: The second stage of training the video demoiré network based on linear sparse attention Transformer: The input and training methods are the same as those in the first stage, except that the color loss weight λ in the total demoiré loss L is replaced by cr Set to 0, and the learning rate is increased from 10 -5 Reduce to 10 -6 , and perform fine-tuning training on the network.
5. The video de-moiré method based on linear sparse attention Transformer according to claim 4, characterized in that: After the training is completed, the specific operation of removing moiré from the input video is as follows: for a new moiré video, two blank frames are inserted at the beginning and the end of the video. The first five frames of the video are first input into the network for calculation to obtain the de-moiré video frame corresponding to the first frame of the original moiré video. Then, the second to sixth frames of the video are input into the network to obtain the de-moiré video frame corresponding to the second frame of the original moiré video. The same operation is subsequently performed until the de-moiré video frames corresponding to all moiré video frames are obtained.
6. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that: When the processor executes the program, it implements the video demoirification method based on linear sparse attention Transformer as described in any one of claims 1-5.
