Can a Tensorflow Numpy function be converted to a tensorflow lite Model?

0

I have a python function that accept a Numpy array and return another Numpy array let's call it getFeatures, the function output are features that will be passed as input to Tensorflow lite model for classification, since Tensorflow support numpy_function that wraps a python function and uses it as a TensorFlow op i want to transfrom getFeatures using numpy_function as a tflite model, so i don't have to develop getFeatures for android in order to extract the features.

The problem is i'm getting some error using this code :

from __future__ import absolute_import, division, print_function, unicode_literals
import traceback
import textwrap
import tensorflow as tf

def getFeatures(dataArray): 
    x = features(dataArray, 1600)
    return x 

@tf.function
def tf_function(dataArray): 
    y = tf.numpy_function(getFeatures, [dataArray], tf.double)
    return y

root = tf.train.Checkpoint()
root.f = tf_function
input_data = np.array([1,3])
concrete_func = root.f.get_concrete_function(input_data)
converterC = tf.lite.TFLiteConverter.from_concrete_functions([concrete_func])
tfliteC = converterC.convert()
open("FeaturesModel/tfliteFunc", "wb").write(tfliteC)

and here is the error:


2020-01-15 16:29:09.823958: I tensorflow/core/grappler/devices.cc:60] Number of eligible GPUs (core count >= 8, compute capability >= 0.0): 0 (Note: TensorFlow was not compiled with CUDA support)
2020-01-15 16:29:09.879081: I tensorflow/core/grappler/clusters/single_machine.cc:356] Starting new session
2020-01-15 16:29:09.882306: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
2020-01-15 16:29:09.889356: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:716] Optimization results for grappler item: graph_to_optimize
2020-01-15 16:29:09.891628: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:718]   function_optimizer: function_optimizer did nothing. time = 0.003ms.
2020-01-15 16:29:09.901985: I tensorflow/core/grappler/optimizers/meta_optimizer.cc:718]   function_optimizer: function_optimizer did nothing. time = 0ms.
2020-01-15 16:29:09.916149: I tensorflow/core/grappler/devices.cc:60] Number of eligible GPUs (core count >= 8, compute capability >= 0.0): 0 (Note: TensorFlow was not compiled with CUDA support)
2020-01-15 16:29:09.920390: I tensorflow/core/grappler/clusters/single_machine.cc:356] Starting new session
Traceback (most recent call last):
  File "mlFeaturesToTFlite.py", line 31, in <module>
    tfliteC = converterC.convert()
  File "C:\Users\Lafi\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow_core\lite\python\lite.py", line 446, in convert
    **converter_kwargs)
  File "C:\Users\Lafi\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow_core\lite\python\convert.py", line 449, in toco_convert_impl
    enable_mlir_converter=enable_mlir_converter)
  File "C:\Users\Lafi\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow_core\lite\python\convert.py", line 200, in toco_convert_protos
    raise ConverterError("See console for info.\n%s\n%s\n" % (stdout, stderr))
tensorflow.lite.python.convert.ConverterError: See console for info.
2020-01-15 16:29:11.883141: I tensorflow/lite/toco/import_tensorflow.cc:659] Converting unsupported operation: PyFunc
2020-01-15 16:29:11.883329: F tensorflow/lite/toco/import_tensorflow.cc:114] Check failed: attr.value_case() == AttrValue::kType (1 vs. 6)
Fatal Python error: Aborted

Current thread 0x00002180 (most recent call first):
  File "c:\users\lafi\appdata\local\programs\python\python36\lib\site-packages\tensorflow_core\lite\toco\python\toco_from_protos.py", line 52 in execute
  File "c:\users\lafi\appdata\local\programs\python\python36\lib\site-packages\absl\app.py", line 250 in _run_main
  File "c:\users\lafi\appdata\local\programs\python\python36\lib\site-packages\absl\app.py", line 299 in run
  File "c:\users\lafi\appdata\local\programs\python\python36\lib\site-packages\tensorflow_core\python\platform\app.py", line 40 in run
  File "c:\users\lafi\appdata\local\programs\python\python36\lib\site-packages\tensorflow_core\lite\toco\python\toco_from_protos.py", line 89 in main
  File "C:\Users\Lafi\AppData\Local\Programs\Python\Python36\Scripts\toco_from_protos.exe\__main__.py", line 7 in <module>
  File "c:\users\lafi\appdata\local\programs\python\python36\lib\runpy.py", line 85 in _run_code
  File "c:\users\lafi\appdata\local\programs\python\python36\lib\runpy.py", line 193 in _run_module_as_main
python-3.x
tensorflow
tensorflow-lite
asked on Stack Overflow Jan 15, 2020 by Raid Lafi

1 Answer

0

The short answer is no. There is no Numpy array in TFLite.

answered on Stack Overflow Jun 19, 2020 by Alex Cohn

User contributions licensed under CC BY-SA 3.0