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.