Cannot convert a symbolic Keras input/output to a numpy array with TF Lite converter

51 views
Skip to first unread message

James B.

unread,
Jul 17, 2021, 6:14:31 AMJul 17
to SIG Micro
I need to implement a custom convolution 2D layer which is not supported by Tensorflow and TF Lite, so I tried to define it by using this link:
https://www.tensorflow.org/guide/create_op (in order to have a TF operator for the op) and this link: https://www.tensorflow.org/lite/guide/ops_custom (to have a TF Lite operator for this op). I can train the model; however, when I try to convert the model with the operator with TF Lite converter, I get this error:

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 example 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)

I think the problem is due to the fact that TF Lite converter receives a Keras Tensor (a placeholder) rather than an array numpy, but I'm not sure how I could convert the model if I have to use a TF Lite custom op.

Thanks in advance.

Advait Jain

unread,
Jul 19, 2021, 4:54:34 PMJul 19
to SIG Micro, James B.
Hi James,

This appears to be the same question as https://github.com/tensorflow/tflite-micro/issues/288.

For general converter issues, we would refer you to the TfLite mailing list or other links listed in https://github.com/tensorflow/tflite-micro#getting-help

Regards,
Advait
Reply all
Reply to author
Forward
0 new messages