Closed lisaong closed 5 years ago
@lisaong Please provide details about what platform you are using (operating system, architecture). Also include your TensorFlow version. Also, did you compile from source or install a binary?
Make sure you also include the exact command if possible to produce the output included in your test case. If you are unclear what to include see the issue template displayed in the Github new issue template.
We ask for this in the issue submission template, because it is really difficult to help without that information. Thanks!
Hi @muddham, provided info as requested. Thank you.
@muddham, thanks for the pointers. One part is unclear to me after reading these links: https://www.tensorflow.org/lite/guide/ops_custom
In particular, how do I do this part?
“When initializing the OpResolver, add the custom op into the resolver, this will register the operator with Tensorflow Lite so that TensorFlow Lite can use the new implementation. “
Where does this code run?
tflite::ops::builtin::BuiltinOpResolver builtins; builtins.AddCustom("Sin", Register_SIN());
Is there an end-to-end example of registering a custom TFLite op?
Like @lisaong said is (https://github.com/tensorflow/tensorflow/issues/28791#issuecomment-493622520) there a full sample for creating even the simplest custom operation in TensorFlow Lite?
Just to learn TensorFlow & TensorFlow Lite I created a model in TensorFlow with a simple custom operation (implemented my own addition, even if it exists on both libraries). I then created a model and after training it I converted it to .tflite
But now I am unable to convert the source code of my operation to Lite version so that I can use it
Found the solution, at least for my issue: https://www.tensorflow.org/lite/guide/ops_select
RandomStandardNormal is part of the whitelist: https://github.com/tensorflow/tensorflow/blob/master/tensorflow/lite/toco/tflite/whitelisted_flex_ops.cc
# RandomStandardNormal is not part of TensorFlow lite, so we need to use SELECT_TF_OPS to include it
converter.target_ops = [tf.lite.OpsSet.TFLITE_BUILTINS,
tf.lite.OpsSet.SELECT_TF_OPS]
tflite_model = converter.convert()
This doesn't resolve the issue reported by @hamlatzis
I am closing this issue as it was resolved. Please open a new ticket if this issue persists. For new issues, please open a new ticket so that it will be helpful to the community to follow. Thanks!
Hi @hamlatzis,
Turns out the SELECT_TF_OPS option is very unwieldy for target systems that are not iOS or Android. For my case I'm trying to compile the model for Raspberry Pi. The problem with SELECT_TF_OPS is that you have to compile (big) TensorFlow, and your mileage may vary on other platforms.
Here's an example of how the custom op can be registered. I adapted minimal.cc to register the op. Hopefully this will help.
Step 1: Implement your custom operator.
TfLiteStatus RandomStandardNormal_Prepare(TfLiteContext* context, TfLiteNode* node) {
...
return kTfLiteOk;
}
TfLiteStatus RandomStandardNormal_Eval(TfLiteContext* context, TfLiteNode* node) {
...
return kTfLiteOk;
}
TfLiteRegistration* Register_RandomStandardNormal() {
static TfLiteRegistration r = {nullptr, nullptr,
RandomStandardNormal_Prepare,
RandomStandardNormal_Eval};
return &r;
}
Step 2: Register it with the resolver.
// Build the interpreter
tflite::ops::builtin::BuiltinOpResolver resolver;
// Register custom operators
resolver.AddCustom("RandomStandardNormal", Register_RandomStandardNormal());
InterpreterBuilder builder(*model, resolver);
std::unique_ptr<Interpreter> interpreter;
builder(&interpreter);
TFLITE_MINIMAL_CHECK(interpreter != nullptr);
Source: https://github.com/lisaong/diec/tree/master/day3/inference
Regards, lisa
System information
Provide the text output from tflite_convert
Also, please include a link to a GraphDef or the model if possible.
Basically this boils down to the usage of this on the model:
epsilon = K.random_normal(shape=(batch, dim))
Command used to reproduce:
See Jupyter Notebook: https://github.com/lisaong/diec/blob/tflite-mcu/day3/Anomaly_detection_VAE.ipynb
Any other info / logs
Include any logs or source code that would be helpful to diagnose the problem. If including tracebacks, please include the full traceback. Large logs and files should be attached.