Create a predictor that takes inputs from an :class:`InputSource`, instead of from feeds. An instance `pred` of :class:`FeedfreePredictor` can be called only by `pred()`, which returns a list of output values as defined in config.output_names.
| 10 | |
| 11 | |
| 12 | class FeedfreePredictor(PredictorBase): |
| 13 | """ |
| 14 | Create a predictor that takes inputs from an :class:`InputSource`, instead of from feeds. |
| 15 | An instance `pred` of :class:`FeedfreePredictor` can be called only by `pred()`, which returns |
| 16 | a list of output values as defined in config.output_names. |
| 17 | """ |
| 18 | |
| 19 | def __init__(self, config, input_source): |
| 20 | """ |
| 21 | Args: |
| 22 | config (PredictConfig): the config to use. |
| 23 | input_source (InputSource): the feedfree InputSource to use. |
| 24 | Must match the signature of the tower function in config. |
| 25 | """ |
| 26 | self._config = config |
| 27 | self._input_source = input_source |
| 28 | assert config.return_input is False, \ |
| 29 | "return_input is not supported in FeedfreePredictor! " \ |
| 30 | "If you need to fetch inputs, add the names to the output_names!" |
| 31 | |
| 32 | self._hooks = [] |
| 33 | self.graph = config._maybe_create_graph() |
| 34 | with self.graph.as_default(): |
| 35 | self._input_callbacks = Callbacks( |
| 36 | self._input_source.setup(config.input_signature)) |
| 37 | with PredictTowerContext(''): |
| 38 | self._input_tensors = self._input_source.get_input_tensors() |
| 39 | config.tower_func(*self._input_tensors) |
| 40 | self._tower_handle = config.tower_func.towers[-1] |
| 41 | |
| 42 | self._output_tensors = self._tower_handle.get_tensors(config.output_names) |
| 43 | |
| 44 | self._input_callbacks.setup_graph(None) |
| 45 | |
| 46 | for h in self._input_callbacks.get_hooks(): |
| 47 | self._register_hook(h) |
| 48 | self._initialize_session() |
| 49 | |
| 50 | def _register_hook(self, hook): |
| 51 | """ |
| 52 | Args: |
| 53 | hook (tf.train.SessionRunHook): |
| 54 | """ |
| 55 | self._hooks.append(hook) |
| 56 | |
| 57 | def _initialize_session(self): |
| 58 | # init the session |
| 59 | self._config.session_init._setup_graph() |
| 60 | self._sess = self._config.session_creator.create_session() |
| 61 | self._config.session_init._run_init(self._sess) |
| 62 | |
| 63 | with self._sess.as_default(): |
| 64 | self._input_callbacks.before_train() |
| 65 | self._hooked_sess = HookedSession(self._sess, self._hooks) |
| 66 | |
| 67 | def __call__(self): |
| 68 | return self._hooked_sess.run(self._output_tensors) |
| 69 |
no outgoing calls
no test coverage detected
searching dependent graphs…