base.py 993 字节
Newer Older
G
gx_wind 已提交
1 2 3 4 5 6 7 8 9
"""
The base model of the model.
"""
from abc import ABCMeta
#from advbox.base import Model
import abc

abstractmethod = abc.abstractmethod

G
gx_wind 已提交
10

G
gx_wind 已提交
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 40 41 42 43
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