base.py 930 字节
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 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
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

    def __call__(self, image_batch):
        """
        Generate the adversarial sample.

        Args:
        image_batch(list): The image and label tuple list.
        """
        adv_img = self._apply(image_batch)
        return adv_img

    @abstractmethod
    def _apply(self, image_batch):
        """
        Search an adversarial example.

        Args:
        image_batch(list): The image and label tuple list.
        """
        raise NotImplementedError