This method will build the trainer's tower function under ``TowerContext(is_training=False)``, and returns a callable predictor with input placeholders & output tensors in this tower. This method handles the common case where you inference with the same tower function
(self, input_names, output_names, device=0)
| 81 | return self.tower_func.towers |
| 82 | |
| 83 | def get_predictor(self, input_names, output_names, device=0): |
| 84 | """ |
| 85 | This method will build the trainer's tower function under ``TowerContext(is_training=False)``, |
| 86 | and returns a callable predictor with input placeholders & output tensors in this tower. |
| 87 | |
| 88 | This method handles the common case where you inference with the same tower function |
| 89 | you provide to the trainer. |
| 90 | If you want to do inference with a different tower function, you can always build the tower by yourself, |
| 91 | under a "reuse" variable scope and a `TowerContext(is_training=False)`. |
| 92 | |
| 93 | Args: |
| 94 | input_names (list): list of input names, matching the inputs declared for the trainer. |
| 95 | output_names(list): list of tensor names without the tower prefix. |
| 96 | device (int): build the predictor on device '/gpu:{device}' or use -1 for '/cpu:0'. |
| 97 | |
| 98 | Returns: |
| 99 | an :class:`OnlinePredictor`. |
| 100 | |
| 101 | Example: |
| 102 | |
| 103 | .. code-block:: none |
| 104 | |
| 105 | # in the graph: |
| 106 | interesting_tensor = tf.identity(x, name='fun') |
| 107 | # in _setup_graph callback method: |
| 108 | self._predictor = self.trainer.get_predictor(['input1', 'input2'], ['fun']) |
| 109 | # After session is initialized (see Tutorials - Write a Callback), can use it by: |
| 110 | outputs = self._predictor(input1, input2) |
| 111 | |
| 112 | The CycleGAN example and DQN example have more concrete use of this method. |
| 113 | """ |
| 114 | assert self.tower_func is not None, "Must set tower_func on the trainer to use get_predictor()!" |
| 115 | tower_name = 'tower-pred-{}'.format(device) if device >= 0 else 'tower-pred-cpu' |
| 116 | device_id = device |
| 117 | device = '/gpu:{}'.format(device_id) if device_id >= 0 else '/cpu:0' |
| 118 | |
| 119 | try: |
| 120 | tower = self.tower_func.towers[tower_name] |
| 121 | assert tower is not None, "This is a bug!" |
| 122 | except KeyError: |
| 123 | tower = None |
| 124 | |
| 125 | if tower is None: |
| 126 | input = PlaceholderInput() |
| 127 | input.setup(self.input_signature) |
| 128 | |
| 129 | vs_name = self._vs_name_for_predictor(device_id) |
| 130 | with tfv1.variable_scope(tfv1.get_variable_scope(), reuse=True), \ |
| 131 | tf.device(device), PredictTowerContext( |
| 132 | tower_name, vs_name=vs_name): |
| 133 | logger.info("Building graph for predict tower '{}' on device {} {}...".format( |
| 134 | tower_name, device, |
| 135 | "with variable scope '{}'".format(vs_name) if vs_name else '')) |
| 136 | self.tower_func(*input.get_input_tensors()) |
| 137 | tower = self.tower_func.towers[tower_name] |
| 138 | input_tensors = tower.get_tensors(input_names) |
| 139 | output_tensors = tower.get_tensors(output_names) |
| 140 | predictor = OnlinePredictor(input_tensors, output_tensors) |
no test coverage detected