mocleiri / tensorflow-micropython-examples

A custom micropython firmware integrating tensorflow lite for microcontrollers and ulab to implement the tensorflow micro examples.
MIT License
170 stars 79 forks source link

Using without callbacks for input and output? #69

Closed cgreening closed 2 years ago

cgreening commented 2 years ago

I'm fairly new to Micropython, so this might not be the way things work.

Is it possible to use the interpreter without input and output callbacks?

e.g.

model = bytearray(59760)
model_file = io.open('test.tflite', 'rb')
model_file.readinto(model)
model_file.close()

interp = microlite.interpreter(model, 4096, None, None)
inputTensor = microlite_interpreter.getInputTensor(0)
# fill the input tensor with data
outputTensor = microlite_interpreter.getOutputTensor(0)
interp.invoke()
result = outputTensor.getValue(0)
mocleiri commented 2 years ago

The callback approach is not the best and dates from the very beginning (January 2021) when I didn't know how anything worked.

I plan to change how the interpreter works in #10 to try and line up as a subset of the tensorflow lite python Interpreter API.

I just took a look at your question on my m5 camera board and its possible to not use the callbacks but at the moment you have to stub a specific kind of no-op function.

def noop(interp):
    pass

interp = microlite.interpreter(model, 4096, noop, noop)
inputTensor = microlite_interpreter.getInputTensor(0)
# fill the input tensor with data
outputTensor = microlite_interpreter.getOutputTensor(0)
interp.invoke()
result = outputTensor.getValue(0)

I'll make an API change on this issue so that if you don't set callbacks don't use them.

i.e.

interp = microlite.interpreter(model, 4096)
inputTensor = microlite_interpreter.getInputTensor(0)
# fill the input tensor with data
outputTensor = microlite_interpreter.getOutputTensor(0)
interp.invoke()
result = outputTensor.getValue(0)

I think it should be a minor adjustment to the interpreter constructor

and the invoke method.

It seemed like it was a little bit faster without the function calls.

cgreening commented 2 years ago

That works nicely!