                                     Few-Shot Learner Generalizes Across AI-Generated Image Detection

                  Class 1                                   Class 2
                                                                                Algorithm 1 Prototypical Loss Computation Algorithm
                                                                                  Input: Training set D = D1 ∪ D2 ∪ · · · DN , where Dk
                            C1                         C2
                                                                                  denotes the subset of class k; number of training class
                                                                                  Nc ; number of support examples per class Ns ; number
                                                    Query                         of query examples per class Nq .
                                                                                  Output: training loss J.
                     Class 3
                                       C3                                         V ← randomly select Nc classes from {1, 2, . . . , N }
                                                                                  for k in V do
                                                                                     Sk ← randomly select Ns samples from Dk
                                 (a) training                                        Qk ← randomly   select Nq samples from Dk /Sk
                                                                                     ck ← N1s xi ∈Sk fϕ (xi )
                                                                                              P

                   Class Fake                         Class Real                  end for
                                                                                  J ←0
                         CF                                                       for k in V do
                                                                                     J ← J− Nc1Nq xj ∈Qk log Softmax(−d(fϕ (xj ), ck ))
                                                                                                   P

                                                                                  end for
                            Test
                                                                                  Return J
                                               CR




                                                                                compute the distances between the test vector and all the
                                 (b) testing                                    representations d(fϕ (xq ), ci ), using a distance function d :
                                                                                R M × R M → R+   0 . The probability that the query sample
Figure 3. Prototypical calculation. During training, the metric
                                                                                xq belongs to class i is determined by:
space is learned by minimizing the distances between query sam-
ples and their corresponding centers. During testing, the test image
                                                                                   p(y = i|xq ) = Softmax1≤i≤N (−d(fϕ (xq ), ci )).        (2)
is only classified between real and fake.
                                                                                The image xq is assigned to the class with the maximum
ples in each class. xij ∈ RD is an image of D dimensions
                                                                                probability, or the nearest prototypical representation to it.
and yij = i indicates its corresponding label. Each unla-
beled image in the query set will be assigned to one of the                     As shown in Figure 3(a), during each training step, Nc
N classes. This is the so-called N -way K-shot task.                            classes are randomly selected from the training set. For
                                                                                each selected class i, we randomly choose Ns samples from
3.2. Few-Shot Detector                                                          it as the support set Si and another Nq samples as the query
                                                                                set Qi . We compute the Nc prototypical representations ac-
Our FSD is based on the Prototypical Network (Snell et al.,                     cording to Equation (1). The Prototypical Network outputs a
2017), an effective method for addressing the few-shot ques-                    probability distribution over the Nc classes for each sample
tion above. The Prototypical Network aims to learn a metric                     in the query set. The optimization target is to minimize the
space in which samples from the same class are close in                         negative log-probability J(ϕ). Pseudo code to compute the
distance, while samples from different classes are relatively                   loss J(ϕ) is provided in Algorithm 1.
distant from each other. Thus, we can represent each class
by a single vector, known as the prototypical representation.                   3.3. Zero-shot Classification
For each sample in the query set, classification is conducted
by simply finding the nearest representation to it.                             We propose a zero-shot detection approach to verify the
                                                                                applicability of FSD in real-world settings. In the zero-shot
In detail, as depicted in Figure 2, FSD adopts a neural
                                                                                task, there are no additional samples from the test classes.
network fϕ : RD → RM to compute the M -dimensional
                                                                                Instead, we are provided with a metadata vector for each
vector in the metric space of an image, where ϕ is the learn-
                                                                                class in the training set to categorize the test images. We put
able parameters. The prototypical representation ci for class
                                                                                considerable emphasis on this task, since acquiring samples
i is defined as the average of the embedded vectors derived
                                                                                from all potential generative models can be impractical. The
from its corresponding support set Si :
                                                                                zero-shot results can represent the performance of our model
                               1 X                                              in general scenarios.
                    ci =            fϕ (xj ).                         (1)
                              |Si |                                             In this work, we simply define the metadata vector as the
                                     xj ∈Si
                                                                                prototypical representation derived from an extensive collec-
After gaining all the N prototypical representations and                        tion of samples. That is, we randomly select a large number
the vector fϕ (xq ) of a test image xq in the query set, we                     of images from each class in the training set and then com-

                                                                            4
