A predictor which directly use an existing session and given tensors. Attributes: sess: The tf.Session object associated with this predictor.
| 80 | |
| 81 | |
| 82 | class OnlinePredictor(PredictorBase): |
| 83 | """ |
| 84 | A predictor which directly use an existing session and given tensors. |
| 85 | |
| 86 | Attributes: |
| 87 | sess: The tf.Session object associated with this predictor. |
| 88 | """ |
| 89 | |
| 90 | ACCEPT_OPTIONS = False |
| 91 | """ See Session.make_callable """ |
| 92 | |
| 93 | def __init__(self, input_tensors, output_tensors, |
| 94 | return_input=False, sess=None): |
| 95 | """ |
| 96 | Args: |
| 97 | input_tensors (list): list of names. |
| 98 | output_tensors (list): list of names. |
| 99 | return_input (bool): same as :attr:`PredictorBase.return_input`. |
| 100 | sess (tf.Session): the session this predictor runs in. If None, |
| 101 | will use the default session at the first call. |
| 102 | Note that in TensorFlow, default session is thread-local. |
| 103 | """ |
| 104 | def normalize_name(t): |
| 105 | if isinstance(t, six.string_types): |
| 106 | return get_op_tensor_name(t)[1] |
| 107 | return t |
| 108 | |
| 109 | self.return_input = return_input |
| 110 | self.input_tensors = [normalize_name(x) for x in input_tensors] |
| 111 | self.output_tensors = [normalize_name(x) for x in output_tensors] |
| 112 | self.sess = sess |
| 113 | |
| 114 | if sess is not None: |
| 115 | self._callable = sess.make_callable( |
| 116 | fetches=output_tensors, |
| 117 | feed_list=input_tensors, |
| 118 | accept_options=self.ACCEPT_OPTIONS) |
| 119 | else: |
| 120 | self._callable = None |
| 121 | |
| 122 | def _do_call(self, dp): |
| 123 | assert len(dp) == len(self.input_tensors), \ |
| 124 | "{} != {}".format(len(dp), len(self.input_tensors)) |
| 125 | if self.sess is None: |
| 126 | self.sess = tf.get_default_session() |
| 127 | assert self.sess is not None, "Predictor isn't called under a default session!" |
| 128 | |
| 129 | if self._callable is None: |
| 130 | self._callable = self.sess.make_callable( |
| 131 | fetches=self.output_tensors, |
| 132 | feed_list=self.input_tensors, |
| 133 | accept_options=self.ACCEPT_OPTIONS) |
| 134 | # run_metadata = tf.RunMetadata() |
| 135 | # options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE) |
| 136 | return self._callable(*dp) |
| 137 | |
| 138 | |
| 139 | class OfflinePredictor(OnlinePredictor): |
no outgoing calls
no test coverage detected