base.py 1.5 KB
Newer Older
D
dzhwinter 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
#  Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserve.
#
#Licensed under the Apache License, Version 2.0 (the "License");
#you may not use this file except in compliance with the License.
#You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
#Unless required by applicable law or agreed to in writing, software
#distributed under the License is distributed on an "AS IS" BASIS,
#WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#See the License for the specific language governing permissions and
#limitations under the License.
G
gx_wind 已提交
14 15 16
"""
The base model of the model.
"""
G
gx_wind 已提交
17
from abc import ABCMeta, abstractmethod
G
gx_wind 已提交
18

G
gx_wind 已提交
19

G
gx_wind 已提交
20 21 22 23 24 25 26 27 28 29 30 31 32 33
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 已提交
34
    def __call__(self, image_label):
G
gx_wind 已提交
35 36 37 38
        """
        Generate the adversarial sample.

        Args:
G
gx_wind 已提交
39
        image_label(list): The image and label tuple list with one element.
G
gx_wind 已提交
40
        """
G
gx_wind 已提交
41
        adv_img = self._apply(image_label)
G
gx_wind 已提交
42 43 44
        return adv_img

    @abstractmethod
G
gx_wind 已提交
45
    def _apply(self, image_label):
G
gx_wind 已提交
46 47 48 49
        """
        Search an adversarial example.

        Args:
G
gx_wind 已提交
50
        image_batch(list): The image and label tuple list with one element.
G
gx_wind 已提交
51 52
        """
        raise NotImplementedError