inference.py 4.9 KB
Newer Older
Y
Yu Yang 已提交
1
import numpy
Y
Yu Yang 已提交
2
import py_paddle.swig_paddle as api
Y
Yu Yang 已提交
3
import collections
Y
Yu Yang 已提交
4
import topology
Y
Yu Yang 已提交
5
import minibatch
Y
Yu Yang 已提交
6 7
from data_feeder import DataFeeder

Y
Yu Yang 已提交
8
__all__ = ['infer', 'Inference']
Y
Yu Yang 已提交
9 10


Y
Yu Yang 已提交
11
class Inference(object):
Q
qijun 已提交
12 13 14
    """
    Inference combines neural network output and parameters together
    to do inference.
X
xuwei06 已提交
15

Y
Yu Yang 已提交
16
    ..  code-block:: python
X
xuwei06 已提交
17

Y
Yu Yang 已提交
18 19 20 21
        inferer = Inference(output_layer=prediction, parameters=parameters)
        for data_batch in batches:
            print inferer.infer(data_batch)

Q
qijun 已提交
22

Y
Yu Yang 已提交
23
    :param output_layer: The neural network that should be inferenced.
Q
qijun 已提交
24 25 26 27 28
    :type output_layer: paddle.v2.config_base.Layer or the sequence
                        of paddle.v2.config_base.Layer
    :param parameters: The parameters dictionary.
    :type parameters: paddle.v2.parameters.Parameters
    """
Q
qijun 已提交
29

30 31
    def __init__(self, output_layer, parameters):
        topo = topology.Topology(output_layer)
Y
Yu Yang 已提交
32 33 34 35 36
        gm = api.GradientMachine.createFromConfigProto(
            topo.proto(), api.CREATE_MODE_TESTING, [api.PARAMETER_VALUE])
        for param in gm.getParameters():
            val = param.getBuf(api.PARAMETER_VALUE)
            name = param.getName()
Y
Yu Yang 已提交
37 38
            assert isinstance(val, api.Vector)
            val.copyFromNumpyArray(parameters.get(name).flatten())
Y
Yu Yang 已提交
39 40 41
        self.__gradient_machine__ = gm
        self.__data_types__ = topo.data_type()

Y
Yu Yang 已提交
42
    def iter_infer(self, input, feeding=None):
Y
Yu Yang 已提交
43
        feeder = DataFeeder(self.__data_types__, feeding)
Y
Yu Yang 已提交
44
        batch_size = len(input)
Y
Yu Yang 已提交
45

Y
Yu Yang 已提交
46 47 48
        def __reader_impl__():
            for each_sample in input:
                yield each_sample
Y
Yu Yang 已提交
49

Y
Yu Yang 已提交
50
        reader = minibatch.batch(__reader_impl__, batch_size=batch_size)
Y
Yu Yang 已提交
51

Y
Yu Yang 已提交
52 53
        self.__gradient_machine__.start()
        for data_batch in reader():
Y
Yu Yang 已提交
54
            yield self.__gradient_machine__.forwardTest(feeder(data_batch))
Y
Yu Yang 已提交
55 56 57
        self.__gradient_machine__.finish()

    def iter_infer_field(self, field, **kwargs):
T
Tao Luo 已提交
58 59 60
        if not isinstance(field, list) and not isinstance(field, tuple):
            field = [field]

T
Tao Luo 已提交
61 62 63 64 65
        for result in self.iter_infer(**kwargs):
            for each_result in result:
                item = [each_result[each_field] for each_field in field]
                yield item

Y
Yu Yang 已提交
66 67 68 69 70 71
    def infer(self, input, field='value', **kwargs):
        """
        Infer a data by model.
        :param input: input data batch. Should be python iterable object.
        :param field: output field.
        """
T
Tao Luo 已提交
72
        retv = None
Y
Yu Yang 已提交
73
        kwargs['input'] = input
T
Tao Luo 已提交
74 75
        for result in self.iter_infer_field(field=field, **kwargs):
            if retv is None:
L
Luo Tao 已提交
76
                retv = [[] for i in xrange(len(result))]
T
Tao Luo 已提交
77 78 79 80 81 82 83
            for i, item in enumerate(result):
                retv[i].append(item)
        retv = [numpy.concatenate(out) for out in retv]
        if len(retv) == 1:
            return retv[0]
        else:
            return retv
Y
Yu Yang 已提交
84 85


86
def infer(output_layer, parameters, input, feeding=None, field='value'):
Y
Yu Yang 已提交
87 88 89 90
    """
    Infer a neural network by given neural network output and parameters.  The
    user should pass either a batch of input data or reader method.

L
Luo Tao 已提交
91
    Example usage for sinlge output_layer:
Y
Yu Yang 已提交
92 93 94

    ..  code-block:: python

X
xuwei06 已提交
95 96
        result = paddle.infer(output_layer=prediction,
                              parameters=parameters,
97
                              input=SomeData)
Y
Yu Yang 已提交
98 99
        print result

L
Luo Tao 已提交
100
    Example usage for multiple outout_layers and fields:
101 102 103

    ..  code-block:: python

X
xuwei06 已提交
104 105
        result = paddle.infer(output_layer=[prediction1, prediction2],
                              parameters=parameters,
106 107 108 109
                              input=SomeData,
                              field=[id, value]])
        print result

110
    :param output_layer: output of the neural network that would be inferred
X
xuwei06 已提交
111
    :type output_layer: paddle.v2.config_base.Layer or a list of
112
                        paddle.v2.config_base.Layer
Y
Yu Yang 已提交
113 114 115 116 117
    :param parameters: parameters of the neural network.
    :type parameters: paddle.v2.parameters.Parameters
    :param input: input data batch. Should be a python iterable object, and each
                  element is the data batch.
    :type input: collections.Iterable
118
    :param feeding: Reader dictionary. Default could generate from input
Y
Yu Yang 已提交
119
                        value.
X
xuwei06 已提交
120 121
    :param field: The prediction field. It should in [`value`, `id`, `prob`].
                  `value` and `prob` mean return the prediction probabilities,
L
Luo Tao 已提交
122
                  `id` means return the prediction labels. Default is `value`.
X
xuwei06 已提交
123
                  Note that `prob` only used when output_layer is beam_search
L
Luo Tao 已提交
124
                  or max_id.
Y
Yu Yang 已提交
125
    :type field: str
X
xuwei06 已提交
126 127
    :return: The prediction result. If there are multiple outout_layers and fields,
             the return order is outout_layer1.field1, outout_layer2.field1, ...,
L
Luo Tao 已提交
128
             outout_layer1.field2, outout_layer2.field2 ...
Y
Yu Yang 已提交
129 130 131
    :rtype: numpy.ndarray
    """

132 133
    inferer = Inference(output_layer=output_layer, parameters=parameters)
    return inferer.infer(field=field, input=input, feeding=feeding)