Hi, I need to implement a custom convolution layer which is not supported by Tensorflow and TF Lite, so I tried to define it by using the tutorial to have a TF operator for a custom op and the guide to have a custom op supported by TF Lite. However, when I try to convert the operator with TF Lite converter, I get this error:
Traceback (most recent call last):
File "es.py", line 39, in <module>
converter =
tf.lite.TFLiteConverter.from_concrete_functions([tf.function(convol1d).get_concrete_function(inp)])
File "/home/em/venv/lib/python3.8/site-packages/tensorflow/python/eager/def_function.py",
line 1299, in get_concrete_function
concrete = self._get_concrete_function_garbage_collected(*args, **kwargs)
File "/home/em/venv/lib/python3.8/site-packages/tensorflow/python/eager/def_function.py",
line 1205, in _get_concrete_function_garbage_collected
self._initialize(args, kwargs, add_initializers_to=initializers)
File "/home/em/venv/lib/python3.8/site-packages/tensorflow/python/eager/def_function.py",
line 725, in _initialize
self._stateful_fn._get_concrete_function_internal_garbage_collected( # pylint: disable=protected-
access
File "/home/em/venv/lib/python3.8/site-packages/tensorflow/python/eager/function.py", line
2969, in _get_concrete_function_internal_garbage_collected
graph_function, _ = self._maybe_define_function(args, kwargs)
File "/home/em/venv/lib/python3.8/site-packages/tensorflow/python/eager/function.py", line
3314, in _maybe_define_function
self._function_spec.canonicalize_function_inputs(*args, **kwargs)
File "/home/em/venv/lib/python3.8/site-packages/tensorflow/python/eager/function.py", line
2697, in canonicalize_function_inputs
inputs, flat_inputs, filtered_flat_inputs = _convert_numpy_inputs(inputs)
File "/home/em/venv/lib/python3.8/site-packages/tensorflow/python/eager/function.py", line
2753, in _convert_numpy_inputs
a = _as_ndarray(value)
File "/home/em/venv/lib/python3.8/site-packages/tensorflow/python/eager/function.py", line
2711, in _as_ndarray
return value.__array__()
File "/home/em/venv/lib/python3.8/site-packages/tensorflow/python/keras/engine
/keras_tensor.py", line 273, in __array__
raise TypeError(
TypeError: Cannot convert a symbolic Keras input/output to a numpy array. This error may
indicate that you're trying to pass a symbolic value to a NumPy call, which is not supported. Or,
you may be trying to pass Keras symbolic inputs/outputs to a TF API that does not register
dispatching, preventing Keras from automatically converting the API call to a lambda layer in the
Functional Model.
The code is like this:
import tensorflow as tf
tf.config.run_functions_eagerly(True)
from keras.datasets import mnist
from keras.models import Model
from keras.layers import add,Input,Activation,Flatten,Dense
def convol(inp):
conv_module = tf.load_op_library('./conv.so')
x = conv_module.conv(inp, name="Conv")
return x
def read_mnist(path):
(train_x,train_y), (test_x,test_y)=mnist.load_data()
return train_x,train_y,test_x,test_y
def tcn(train_x,train_y,test_x,test_y):
inp=Input(shape=(28,28))
x = convol(inp)
x=Flatten()(x)
x=Dense(10,activation='softmax')(x)
model=Model(inputs=inp,outputs=x)
model.summary()
model.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=
['accuracy'])
model.fit(train_x,train_y,batch_size=100,epochs=10,validation_data=(test_x,test_y))
pred=model.evaluate(test_x,test_y,batch_size=100)
print('test_loss:',pred[0],'- test_acc:',pred[1])
train_x,train_y,test_x,test_y=read_mnist('MNIST_data')
tcn(train_x,train_y,test_x,test_y)
tflite_model_name = 'net'
inp=Input(shape=(28,28))
converter =
tf.lite.TFLiteConverter.from_concrete_functions([tf.function(convol).get_concrete_function(inp)])
converter.allow_custom_ops = True
tflite_model = converter.convert()
open(tflite_model_name + '.tflite', 'wb').write(tflite_model)