base.py 964 字节
Newer Older
G
gx_wind 已提交
1 2 3
"""
The base model of the model.
"""
G
gx_wind 已提交
4
from abc import ABCMeta, abstractmethod
G
gx_wind 已提交
5

G
gx_wind 已提交
6

G
gx_wind 已提交
7 8 9 10 11 12 13 14 15 16 17 18 19 20
class Attack(object):
    """
    Abstract base class for adversarial attacks. `Attack` represent an adversarial attack
    which search an adversarial example. subclass should implement the _apply() method.

    Args:
        model(Model): an instance of the class advbox.base.Model.

    """
    __metaclass__ = ABCMeta

    def __init__(self, model):
        self.model = model

G
gx_wind 已提交
21
    def __call__(self, image_label):
G
gx_wind 已提交
22 23 24 25
        """
        Generate the adversarial sample.

        Args:
G
gx_wind 已提交
26
        image_label(list): The image and label tuple list with one element.
G
gx_wind 已提交
27
        """
G
gx_wind 已提交
28
        adv_img = self._apply(image_label)
G
gx_wind 已提交
29 30 31
        return adv_img

    @abstractmethod
G
gx_wind 已提交
32
    def _apply(self, image_label):
G
gx_wind 已提交
33 34 35 36
        """
        Search an adversarial example.

        Args:
G
gx_wind 已提交
37
        image_batch(list): The image and label tuple list with one element.
G
gx_wind 已提交
38 39
        """
        raise NotImplementedError