# Improved Video Anomaly Detection with Dual Generators and Channel Attention
## 3 Method
### Constraint Function  
In order for the network to converge better during training, we use a constraint function to constrain the network. We constrain the appearance and motion levels to make the pseudo-anomaly frames generated by the noise generator far away from the real frames. Meanwhile, the distance between the reconstructed pseudo-abnormal frame generated by the reconstruction generator is far from the real frame, and the distance between the reconstructed real frame and the real frame is close. The apparent constraints \(L_{app}\) are divided into gradient constraints \(L_{gc}\) and strength constraints \(L_{sc}\). Here, \(L_{gc}\) and \(L_{sc}\) can be obtained by Formulas (7) and (8), respectively:  
\[ \begin{split} L_{gc}(\hat{x},x)=\sum_{a,b}(&\|\hat{ x}_{a,b}-\hat{x}_{a-1,b}|-|x_{a,b}-x_{a-1,b}|\|_{1}\\ &+\||\hat{x}_{a,b}-\hat{x}_{a,b-1}|-|x_{a,b}-x_{a,b-1}|\|_{1})\\ & L_{sc}(\hat{x},x)=\|\hat{x}-x\|_{2}^{2}\end{split}  \tag{7}\]  
where \(x\) is the input original frame, \(\hat{x}\) is the reconstruction frame, \(a\) and \(b\) denote the horizontal and vertical coordinates of the frame pixels. Then the apparent constraints \(L_{app}\) can be calculated from the gradient constraints \(L_{gc}\) and strength constraints \(L_{sc}\). \(L_{app}\) can be calculated by Formula (9):  
\[L_{app}=mL_{gc}+nL_{sc} \tag{9}\]  
where \(m:n=1:1\). The motion constraints \(L_{opt}\) are optical flow constraints [25], and the calculation process is shown in Formula (10):  
\[L_{opt}=\|f(\hat{x}_{t+1},x_{t})-f(x_{t+1},x_{t})\|_{1} \tag{10}\]  
where \(t\) represents the \(t\)-th frame. In the training noise generator phase, the generator of the noiseless module is first trained. To shorten the distance between the reconstructed frame and the real frame, the discriminator is fixed, and the generator is constrained at the apparent level. The objective function \(G_{o}\) is shown in Formula (11):  
\[G_{o}=\underset{\theta}{min}\|\hat{x}-x\|_{2}^{2} \tag{11}\]  
Then we train the corresponding discriminator and fix the generator. The discriminator can classify the reconstructed frames as abnormal frames and the real frames as normal frames, to accurately classify the reconstructed frames and the real frames. After label smoothing [26], label outlier 0 represents normal and label outlier 1 represents abnormal; they are replaced by 0.05 and 0.95, respectively. The loss function \(L_{adv}^{D}\) is shown in Formula (12):  
\[L_{adv}^{D}(\hat{x},x)=\frac{1}{2}\sum_{i,j}[L(D(x)_{i,j},0.95)+L(D(\hat{x})_{ i,j},0.05)] \tag{12}\]  
where \(i,j\) are the indices of the frame, \(D(\,\cdot\,)\in[0,1]\), \(L(\,\cdot\,\cdot\,)\) denotes the absolute value of the difference between the two. After obtaining the trained generator and discriminator, we add a noise module to the generator, then fix the discriminator, and add a constraint function at the apparent level to widen the distance between the generated frame and the real frame. We update the parameters of the noise module, and make the noise generator generate pseudo-anomaly frames, and its objective function \(L_{n}\) is shown in Formula (13):  
\[L_{n}=\underset{\theta_{n}}{max}\|G_{o}(x;\theta_{n})-x\|_{2}^{2} \tag{13}\]  
where \(\theta_{n}\) is the parameter of the noise module. In the stage of training the reconstruction generator, the maximum and minimum constraints are mainly applied to the reconstruction generator in terms of appearance and motion. For the pseudo-abnormal frame, the maximum constraint \(G_{r}\) is used to extend the distance between the pseudo-abnormal frame and the real frame. The inter-frame distance of the pixels between the reconstructed pseudo-abnormal frame and the real frame is increased by the strength constraints \(L_{sc}\), and the difference between the distance between the adjacent pixels in the reconstructed pseudo-abnormal frame and the real frame is increased by the gradient constraints \(L_{gc}\). At the same time, optical flow constraints (\(L_{opt}\)) are added to constrain it at the motion level.