-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Python wrapper for LCE interpreter (#507)
Co-authored-by: Tom Bannink <tombannink@gmail.com> Co-authored-by: Adam Hillier <7688302+AdamHillier@users.noreply.github.com>
- Loading branch information
1 parent
6bbc061
commit d72b679
Showing
18 changed files
with
475 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
from larq_compute_engine.mlir.python.converter import convert_keras_model | ||
from larq_compute_engine.tflite.python import interpreter as testing | ||
|
||
__all__ = ["convert_keras_model"] | ||
__all__ = ["convert_keras_model", "testing"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
load("@org_tensorflow//tensorflow:tensorflow.bzl", "pybind_extension") | ||
load("@org_tensorflow//tensorflow/lite:build_def.bzl", "tflite_linkopts") | ||
|
||
package( | ||
default_visibility = ["//visibility:public"], | ||
licenses = ["notice"], # Apache 2.0 | ||
) | ||
|
||
cc_library( | ||
name = "interpreter_wrapper_utils", | ||
hdrs = ["interpreter_wrapper_utils.h"], | ||
deps = [ | ||
"@org_tensorflow//tensorflow/lite/c:common", | ||
"@pybind11", | ||
], | ||
) | ||
|
||
pybind_extension( | ||
name = "interpreter_wrapper_lite", | ||
srcs = ["interpreter_wrapper_lite.cc"], | ||
linkopts = tflite_linkopts(), | ||
module_name = "interpreter_wrapper_lite", | ||
deps = [ | ||
":interpreter_wrapper_utils", | ||
"//larq_compute_engine/tflite/kernels:lce_op_kernels", | ||
"@org_tensorflow//tensorflow/lite:framework", | ||
"@org_tensorflow//tensorflow/lite/kernels:builtin_ops", | ||
"@pybind11", | ||
], | ||
) | ||
|
||
py_library( | ||
name = "interpreter", | ||
srcs = [ | ||
"__init__.py", | ||
"interpreter.py", | ||
], | ||
deps = [ | ||
":interpreter_wrapper_lite", | ||
], | ||
) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
from typing import List, Tuple, Union | ||
|
||
import numpy as np | ||
from tqdm import tqdm | ||
|
||
from larq_compute_engine.tflite.python import interpreter_wrapper_lite | ||
|
||
__all__ = ["Interpreter"] | ||
|
||
|
||
class Interpreter: | ||
"""Interpreter interface for Larq Compute Engine Models. | ||
!!! example | ||
```python | ||
lce_model = convert_keras_model(model) | ||
interpreter = Interpreter(lce_model) | ||
interpreter.predict(input_data, verbose=1) | ||
``` | ||
# Arguments | ||
flatbuffer_model: A serialized Larq Compute Engine model in the flatbuffer format. | ||
# Attributes | ||
input_types: Returns a list of input types. | ||
input_shapes: Returns a list of input shapes. | ||
output_types: Returns a list of output types. | ||
output_shapes: Returns a list of output shapes. | ||
""" | ||
|
||
def __init__(self, flatbuffer_model: bytes): | ||
self.interpreter = interpreter_wrapper_lite.LiteInterpreter(flatbuffer_model) | ||
|
||
@property | ||
def input_types(self) -> list: | ||
"""Returns a list of input types.""" | ||
return self.interpreter.input_types | ||
|
||
@property | ||
def input_shapes(self) -> List[Tuple[int]]: | ||
"""Returns a list of input shapes.""" | ||
return self.interpreter.input_shapes | ||
|
||
@property | ||
def output_types(self) -> list: | ||
"""Returns a list of output types.""" | ||
return self.interpreter.output_types | ||
|
||
@property | ||
def output_shapes(self) -> List[Tuple[int]]: | ||
"""Returns a list of output shapes.""" | ||
return self.interpreter.output_shapes | ||
|
||
def predict( | ||
self, x: Union[np.ndarray, List[np.ndarray]], verbose: int = 0 | ||
) -> Union[np.ndarray, List[np.ndarray]]: | ||
"""Generates output predictions for the input samples. | ||
# Arguments | ||
x: Input samples. A Numpy array, or a list of arrays in case the model has | ||
multiple inputs. | ||
verbose: Verbosity mode, 0 or 1. | ||
# Returns | ||
Numpy array(s) of output predictions. | ||
""" | ||
|
||
if not isinstance(x, (list, np.ndarray)) or len(x) == 0: | ||
raise ValueError( | ||
"Expected either a non-empty list of inputs or a Numpy array with " | ||
f"implicit initial batch dimension. Received: {x}" | ||
) | ||
|
||
if len(self.input_shapes) == 1: | ||
x = [x] | ||
|
||
batch_iter = tqdm(zip(*x)) if verbose >= 1 else zip(*x) | ||
prediction_batch_iter = ( | ||
self.interpreter.predict([np.expand_dims(inp, axis=0) for inp in inputs]) | ||
for inputs in batch_iter | ||
) | ||
outputs = [np.concatenate(batches) for batches in zip(*prediction_batch_iter)] | ||
|
||
if len(self.output_shapes) == 1: | ||
return outputs[0] | ||
return outputs |
Oops, something went wrong.