# Cross Domain Adaptation of Crowd Counting with Model-Agnostic Meta-Learning
## 3 Methods
### Adaptive Domain-Invariant Feature-Extracting Module
 \tag{5}\]  
where, \(\epsilon_{S}(h)\) and \(\epsilon_{T}(h)\) denote the prediction errors of \(h(\cdot)\), predicting the domain origin, i.e., whether source or target domain. If the prediction error of the domain classifier is high, the two domains become closer and are harder to distinguish, so the distance between the two domains \(d_{\mathcal{H}}(S,T)\) is inversely proportional to the error rate of the domain classifier \(h(\cdot)\). During the training phase, we integrated a gradient reversal layer (GRL) [67] into the feature extracting module. The GRL minimizes the objective function, and adjustment in the negative gradient direction maximizes the objective function. If the feature is adaptive to two different domains, the GRL will make the two domains as indistinguishable as possible. The feature itself gradually inclines towards domain adaptation, and will become a domain invariant feature [10; 67]. Furthermore, to reduce the domain shift between different samples in the source and target domains, as per previous studies, we divided the output features into blocks. This is helpful in alleviating the effects of domain shifts such as lighting, exposure, position, scale, image style, and so on. Crowd counting is a compromise of feature extraction and density estimation, which are considered pixel-wise regression problems, and the domain discriminator is designed to distinguish each pixel of the extracted feature maps. We used four convolution layers for the domain discriminator to generate two-dimensional scores to indicate the confidence with which we can distinguish the source and target domain. Thus, the loss function can be formulated as below:  
\[\mathcal{L}(x^{S},y^{S},x^{T})=\mathcal{L}_{cnt}(x^{S},y^{S})+\lambda\mathcal{ L}_{adv}(f(x^{T})) \tag{6}\]  
where \(\mathcal{L}_{cnt}\) is the standard MSE loss, and \(\mathcal{L}_{adv}\) is the adversarial loss. \(\lambda\) is the weight to balance the losses. For the feature maps \(f(x^{S}),f(x^{T})\), we trained one image-level discriminator \(h(\cdot)\). Through \(h(\cdot)\), we can obtain the pixel-wise domain labels for the source and target domains, denoted as \(O^{S}\) and \(O^{T}\). We utilized binary cross-entropy loss to optimize the discriminator \(h(\cdot)\), which is formulated as:  
\[\mathcal{L}_{f(\cdot)}(x^{S},x^{T})=-\sum_{x^{S}\in D^{S}}\sum_{w\in W}\sum_{h \in H}\log(p(f(x^{S})))-\sum_{x^{T}\in D^{T}}\sum_{w\in W}\sum_{h\in H}\log(1- p(f(x^{T}))) \tag{7}\]  
where \(f(\cdot)\) is the feature extracting component, \(f(x^{S})\) and \(f(x^{T})\) are two-dimensional feature maps of size H \(\times\) W. \(f(x^{S})\) is the source input, and \(f(x^{T})\) is the target input. At the pixel level, we utilized \(p(\cdot)\) as a soft-max function. To confuse \(h(\cdot)\), we also added the inverse adversarial loss into the training phase. The formulation is shown as below:  
\[\mathcal{L}_{adv}(x^{T})=-\sum_{x^{T}\in D^{T}}\sum_{w\in W}\sum_{h\in H}\log( p(f(x^{T}))) \tag{8}\]  
We used the adversarial loss \(\mathcal{L}_{adv}\) to guide \(f(\cdot)\) to fool the discriminator \(h(\cdot)\), by which we effectively alleviated the domain gaps in cross-domain scenarios. This section propose using the adversarial method to train domain-invariant feature-extracting modules for two different domains. With the help of feature visualization tools, the effect can be shown as Figure 4.