paddle.py 3.9 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 17 18 19 20 21 22
from __future__ import absolute_import

import numpy as np
import paddle.v2 as paddle
import paddle.v2.fluid as fluid
from paddle.v2.fluid.framework import program_guard

from .base import Model

G
gx_wind 已提交
23

G
gx_wind 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
class PaddleModel(Model):
    """
    Create a PaddleModel instance.
    When you need to generate a adversarial sample, you should construct an instance of PaddleModel.

    Args:
        program(paddle.v2.fluid.framework.Program): The program of the model which generate the adversarial sample.
        input_name(string): The name of the input.
        logits_name(string): The name of the logits.
        predict_name(string): The name of the predict.
        cost_name(string): The name of the loss in the program.
    """

    def __init__(self,
                 program,
                 input_name,
                 logits_name,
                 predict_name,
                 cost_name,
                 bounds,
                 channel_axis=3,
                 preprocess=None):
        super(PaddleModel, self).__init__(
G
gx_wind 已提交
47
            bounds=bounds, channel_axis=channel_axis, preprocess=preprocess)
G
gx_wind 已提交
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62

        if preprocess is None:
            preprocess = (0, 1)

        self._program = program
        self._place = fluid.CPUPlace()
        self._exe = fluid.Executor(self._place)

        self._input_name = input_name
        self._logits_name = logits_name
        self._predict_name = predict_name
        self._cost_name = cost_name

        # gradient
        loss = self._program.block(0).var(self._cost_name)
G
gx_wind 已提交
63 64
        param_grads = fluid.backward.append_backward(
            loss, parameter_list=[self._input_name])
G
gx_wind 已提交
65
        self._gradient = dict(param_grads)[self._input_name]
G
gx_wind 已提交
66 67 68 69 70 71 72 73 74 75 76

    def predict(self, image_batch):
        """
            Predict the label of the image_batch.

            Args:
                image_batch(list): The image and label tuple list.
            Return:
                numpy.ndarray: predictions of the images with shape (batch_size, num_of_classes).
        """
        feeder = fluid.DataFeeder(
G
gx_wind 已提交
77 78 79
            feed_list=[self._input_name, self._logits_name],
            place=self._place,
            program=self._program)
G
gx_wind 已提交
80
        predict_var = self._program.block(0).var(self._predict_name)
G
gx_wind 已提交
81 82 83
        predict = self._exe.run(self._program,
                                feed=feeder.feed(image_batch),
                                fetch_list=[predict_var])
G
gx_wind 已提交
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
        return predict

    def num_classes(self):
        """
            Calculate the number of classes of the output label. 

        Return:
            int: the number of classes
        """
        predict_var = self._program.block(0).var(self._predict_name)
        assert len(predict_var.shape) == 2
        return predict_var.shape[1]

    def gradient(self, image_batch):
        """
        Calculate the gradient of the loss w.r.t the input.

        Args:
            image_batch(list): The image and label tuple list.
        Return:
            list: The list of the gradient of the image.
        """
        feeder = fluid.DataFeeder(
            feed_list=[self._input_name, self._logits_name],
G
gx_wind 已提交
108 109 110 111 112 113
            place=self._place,
            program=self._program)

        grad, = self._exe.run(self._program,
                              feed=feeder.feed(image_batch),
                              fetch_list=[self._gradient])
G
gx_wind 已提交
114
        return grad