eloquentarduino / EloquentTinyML

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

What the meaning of TF_NUM_OPS ? #71

Closed YuriSizuku closed 8 months ago

YuriSizuku commented 8 months ago

I try to read the source code but don't understand this parameter. Could you please explain or add some comments about this parameter ?

jakobkilian commented 8 months ago

I would also be very interested in this. Unfortunately, I can't get my models to run with the update, but I don't know what should be entered there... Thanks for the great work, Simone!

jakobkilian commented 8 months ago

Didn't really solve this but here is my research:

So, if you only add FullyConnected TF_NUM_OPS should be 1? I have the feeling this library here will be dropped anyways in favour of this one, which also uses the number of operations as argument....

YuriSizuku commented 8 months ago

Didn't really solve this but here is my research:

  • TF_NUM_OPS eventually will go here as tOpCount
  • I am not experienced enough to explain it fully but by reading this it seems to be the number of "operations" that you add later by e.g. doing: tf.resolver.AddFullyConnected(); or tf.resolver.AddSoftmax();

So, if you only add FullyConnected TF_NUM_OPS should be 1? I have the feeling this library here will be dropped anyways in favour of this one, which also uses the number of operations as argument....

Thank you for the information. Is this the number of operations ? Is that means we needs to pre allocate memory before using each operation ?

jakobkilian commented 8 months ago

Just a short note as I understand some more things now and may be able to explain this partly:

There is a couple of possible operations in tflite. With TF_NUM_OPS in this library (and also in others) you just reserve memory to do these operations.

Example: When using a quantized model (taking in float, quantizing it to an int, running the inference and dequantize it back to a float) you would need TF_NUM_OPS to be at least 3 because:

#define TF_NUM_OPS 3
#define ARENA_SIZE 2000
Eloquent::TF::Sequential<TF_NUM_OPS, ARENA_SIZE> tf;

void setup() {
    //stuff
    tf.resolver.AddFullyConnected();
    tf.resolver.AddQuantize();
    tf.resolver.AddDequantize();
}

with this you could load a quantized model like described here in the end

YuriSizuku commented 8 months ago

Just a short note as I understand some more things now and may be able to explain this partly:

There is a couple of possible operations in tflite. With TF_NUM_OPS in this library (and also in others) you just reserve memory to do these operations.

Example: When using a quantized model (taking in float, quantizing it to an int, running the inference and dequantize it back to a float) you would need TF_NUM_OPS to be at least 3 because:

#define TF_NUM_OPS 3
#define ARENA_SIZE 2000
Eloquent::TF::Sequential<TF_NUM_OPS, ARENA_SIZE> tf;

void setup() {
    //stuff
    tf.resolver.AddFullyConnected();
    tf.resolver.AddQuantize();
    tf.resolver.AddDequantize();
}

with this you could load a quantized model like described here in the end

Thank you for your explanation. I wonder how do we know what operations are necessory for the model ?

jakobkilian commented 8 months ago

I also wonder why there seems to be no good docu about it. If you train your model yourself, you can decide on its architecture and the operations you build in. When you have a tflite file and no knowledge you can use Netron to analyse the model. This is e.g. a tiny quantized tflite model I trained as a test. It has quantization built in as you can see in the screenshot and there is a correspinding AddFullyConnected in the mentioned list. Still I don't fully understand this: this model runs for example even if you just add a single AddFullyConnected() and AddQuantize() only (not multiple fully connected and dequantize).

Netron App Screenshot

YuriSizuku commented 8 months ago

I also wonder why there seems to be no good docu about it. If you train your model yourself, you can decide on its architecture and the operations you build in. When you have a tflite file and no knowledge you can use Netron to analyse the model. This is e.g. a tiny quantized tflite model I trained as a test. It has quantization built in as you can see in the screenshot and there is a correspinding AddFullyConnected in the mentioned list. Still I don't fully understand this: this model runs for example even if you just add a single AddFullyConnected() and AddQuantize() only (not multiple fully connected and dequantize).

Netron App Screenshot

I found a use way to summary the ops in a tflite model. python -m tensorflow.lite.tools.visualize xxx.tflite

YuriSizuku commented 8 months ago

I also wonder why there seems to be no good docu about it. If you train your model yourself, you can decide on its architecture and the operations you build in. When you have a tflite file and no knowledge you can use Netron to analyse the model. This is e.g. a tiny quantized tflite model I trained as a test. It has quantization built in as you can see in the screenshot and there is a correspinding AddFullyConnected in the mentioned list. Still I don't fully understand this: this model runs for example even if you just add a single AddFullyConnected() and AddQuantize() only (not multiple fully connected and dequantize). Netron App Screenshot

Thank you for providing the information. I found a easy way to summary the ops in a tflite model. python -m tensorflow.lite.tools.visualize xxx.tflite