Returns function that gives a numpy view of the current tensor buffer. This allows reading and writing to this tensors w/o copies. This more closely mirrors the C++ Interpreter class interface's tensor() member, hence the name. Be careful to not hold these output references through call
(self, tensor_index)
| 388 | return self._interpreter.GetTensor(tensor_index) |
| 389 | |
| 390 | def tensor(self, tensor_index): |
| 391 | """Returns function that gives a numpy view of the current tensor buffer. |
| 392 | |
| 393 | This allows reading and writing to this tensors w/o copies. This more |
| 394 | closely mirrors the C++ Interpreter class interface's tensor() member, hence |
| 395 | the name. Be careful to not hold these output references through calls |
| 396 | to `allocate_tensors()` and `invoke()`. This function cannot be used to read |
| 397 | intermediate results. |
| 398 | |
| 399 | Usage: |
| 400 | |
| 401 | ``` |
| 402 | interpreter.allocate_tensors() |
| 403 | input = interpreter.tensor(interpreter.get_input_details()[0]["index"]) |
| 404 | output = interpreter.tensor(interpreter.get_output_details()[0]["index"]) |
| 405 | for i in range(10): |
| 406 | input().fill(3.) |
| 407 | interpreter.invoke() |
| 408 | print("inference %s" % output()) |
| 409 | ``` |
| 410 | |
| 411 | Notice how this function avoids making a numpy array directly. This is |
| 412 | because it is important to not hold actual numpy views to the data longer |
| 413 | than necessary. If you do, then the interpreter can no longer be invoked, |
| 414 | because it is possible the interpreter would resize and invalidate the |
| 415 | referenced tensors. The NumPy API doesn't allow any mutability of the |
| 416 | the underlying buffers. |
| 417 | |
| 418 | WRONG: |
| 419 | |
| 420 | ``` |
| 421 | input = interpreter.tensor(interpreter.get_input_details()[0]["index"])() |
| 422 | output = interpreter.tensor(interpreter.get_output_details()[0]["index"])() |
| 423 | interpreter.allocate_tensors() # This will throw RuntimeError |
| 424 | for i in range(10): |
| 425 | input.fill(3.) |
| 426 | interpreter.invoke() # this will throw RuntimeError since input,output |
| 427 | ``` |
| 428 | |
| 429 | Args: |
| 430 | tensor_index: Tensor index of tensor to get. This value can be gotten from |
| 431 | the 'index' field in get_output_details. |
| 432 | |
| 433 | Returns: |
| 434 | A function that can return a new numpy array pointing to the internal |
| 435 | TFLite tensor state at any point. It is safe to hold the function forever, |
| 436 | but it is not safe to hold the numpy array forever. |
| 437 | """ |
| 438 | return lambda: self._interpreter.tensor(self._interpreter, tensor_index) |
| 439 | |
| 440 | def invoke(self): |
| 441 | """Invoke the interpreter. |
no outgoing calls