Execute inference on a batch of images. The images should already be batched and preprocessed, as prepared by the ImageBatcher class. Memory copying to and from the GPU device will be performed here. :param batch: A numpy array holding the image batch. :param top: Th
(self, batch, top=1)
| 97 | return self.outputs[0]["shape"], self.outputs[0]["dtype"] |
| 98 | |
| 99 | def infer(self, batch, top=1): |
| 100 | """ |
| 101 | Execute inference on a batch of images. The images should already be batched and preprocessed, as prepared by |
| 102 | the ImageBatcher class. Memory copying to and from the GPU device will be performed here. |
| 103 | :param batch: A numpy array holding the image batch. |
| 104 | :param top: The number of classes to return as top_predicitons, in descending order by their score. By default, |
| 105 | setting to one will return the same as the maximum score class. Useful for Top-5 accuracy metrics in validation. |
| 106 | :return: Three items, as numpy arrays for each batch image: The maximum score class, the corresponding maximum |
| 107 | score, and a list of the top N classes and scores. |
| 108 | """ |
| 109 | # Prepare the output data |
| 110 | output = np.zeros(*self.output_spec()) |
| 111 | |
| 112 | # Process I/O and execute the network |
| 113 | common.memcpy_host_to_device(self.inputs[0]["allocation"], np.ascontiguousarray(batch)) |
| 114 | self.context.execute_v2(self.allocations) |
| 115 | common.memcpy_device_to_host(output, self.outputs[0]["allocation"]) |
| 116 | |
| 117 | # Process the results |
| 118 | classes = np.argmax(output, axis=1) |
| 119 | scores = np.max(output, axis=1) |
| 120 | top = min(top, output.shape[1]) |
| 121 | top_classes = np.flip(np.argsort(output, axis=1), axis=1)[:, 0:top] |
| 122 | top_scores = np.flip(np.sort(output, axis=1), axis=1)[:, 0:top] |
| 123 | |
| 124 | return classes, scores, [top_classes, top_scores] |
| 125 | |
| 126 | |
| 127 | def main(args): |
no test coverage detected