eloquentarduino / EloquentTinyML

Eloquent interface to Tensorflow Lite for Microcontrollers
288 stars 57 forks source link

Allow using multiple networks by supporting multiple instances of TfLite #32

Closed smuellener closed 2 years ago

smuellener commented 2 years ago

With the current implementation it is not possible to use EloquentTinyML with multiple different networks due to static properties within the class. This pull request allows using multiple instances and hence multiple networks with EloquentTinyML.

eloquentarduino commented 2 years ago

Thank you for your contribution. I'll bump the library version for the Arduino Library Manager. If I may ask, did you found yourself needing to run multiple networks at once?

smuellener commented 2 years ago

Yes, in approximately the following way:

// Network 1
auto net_1 = new Eloquent::TinyML::TfLite<NUMBER_OF_INPUTS, NUMBER_OF_OUTPUTS, TENSOR_ARENA_SIZE>();
if (!net_1->begin(net_1_tflite))
{
    Serial.print("Cannot inialize model for network 1: ");
    Serial.println(net_1->errorMessage());
    return;
}
float out_1 = net_1->predict(input_1);
if (net_1->getError() != OK) Serial.println(net_1->errorMessage());
delete net_1;

// Network 2
auto net_2 = new Eloquent::TinyML::TfLite<NUMBER_OF_INPUTS, NUMBER_OF_OUTPUTS, TENSOR_ARENA_SIZE>();
if (!net_2->begin(net_2_tflite))
{
    Serial.print("Cannot inialize model for network 2: ");
    Serial.println(net_2->errorMessage());
    return;
}
float out_2 = net_2->predict(input_2);
if (net_2->getError() != OK) Serial.println(net_2->errorMessage());
delete net_2;
whubaichuan commented 1 year ago

@smuellener @eloquentarduino Thanks for your contribution.

I have tested the code in the original EloquentTinyMl:

tf.begin(model1);

# some test code about model1

tf.begin(model2);
# some test code about model2

It works. And maybe it's another method to run multiple networks in one project.