# Micro speech example from TensorFlow on an ESP32

**URL:** <https://discuss.tinyml.seas.harvard.edu/t/micro-speech-example-from-tensorflow-on-an-esp32/376>\
**Category:** Programming Q&A\
**Created:** [February 19, 2021, 9:57am UTC](https://discuss.tinyml.seas.harvard.edu/t/micro-speech-example-from-tensorflow-on-an-esp32/376 "2021-02-19T09:57:12Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![gameldar](https://avatars.discourse-cdn.com/v4/letter/g/ecae2f/32.png) [@gameldar](https://discuss.tinyml.seas.harvard.edu/u/gameldar)\
**Post date:** [February 19, 2021, 9:57am UTC](https://discuss.tinyml.seas.harvard.edu/t/micro-speech-example-from-tensorflow-on-an-esp32/376/1 "2021-02-19T09:57:12Z")

</div>

Is there a good place to ask technical/implementation questions related to tiny ml - e.g. something like the programming questions forum of Arduino ([https://forum.arduino.cc/index.php?board=4.0](https://forum.arduino.cc/index.php?board=4.0)) to get assistance with troubles implementing specifically TinyML issues?

Would it make sense for this discourse to have a similar type of category?

For example of the sort of question I was looking at ask:  
I’ve been working on a modified version of the micro speech example from TensorFlow on an ESP32 and seem to be hitting an issue where the input tensor is not being initialized correctly. I’m essentially hitting a null pointer issue when trying to copy captured audio to the input-\>data.int8. I don’t fully understand the internals, but I suspect that the initialization is failing because of how the model is loaded, but I’m not sure (e.g. it could be because of space or other issues as I’ve extended my setup to allow OTA updates etc)

---

<div class="post-metadata">

**Author:** ![vjreddi](https://yyz1.discourse-cdn.com/flex027/user_avatar/discuss.tinyml.seas.harvard.edu/vjreddi/32/35_2.png) [@vjreddi](https://discuss.tinyml.seas.harvard.edu/u/vjreddi)\
**Post date:** [February 19, 2021, 2:18pm UTC](https://discuss.tinyml.seas.harvard.edu/t/micro-speech-example-from-tensorflow-on-an-esp32/376/2 "2021-02-19T14:18:44Z")

</div>

Hi @gameldar,

An awesome idea! Thanks for the suggestion. Someone else also asked this exact same question.

If you all think it makes sense, we should have it. So I created a new forum location:

> **[Programming Q&A](https://discuss.tinyml.seas.harvard.edu/c/programming-q-a/23)**
>
> Stuck on something? Ask for the community’s help. A place to ask your questions about programming tiny ML models and understanding how to deploy them on various devices.

And fyi, I relocated your post to the new category. Cheers!

---

<div class="post-metadata">

**Author:** ![vjreddi](https://yyz1.discourse-cdn.com/flex027/user_avatar/discuss.tinyml.seas.harvard.edu/vjreddi/32/35_2.png) [@vjreddi](https://discuss.tinyml.seas.harvard.edu/u/vjreddi)\
**Post date:** [February 19, 2021, 3:49pm UTC](https://discuss.tinyml.seas.harvard.edu/t/micro-speech-example-from-tensorflow-on-an-esp32/376/3 "2021-02-19T15:49:11Z")

</div>

@gameldar do you have a GitHub repo where we can take a look at the code?

---

<div class="post-metadata">

**Author:** ![gameldar](https://avatars.discourse-cdn.com/v4/letter/g/ecae2f/32.png) [@gameldar](https://discuss.tinyml.seas.harvard.edu/u/gameldar)\
**Post date:** [February 19, 2021, 4:22pm UTC](https://discuss.tinyml.seas.harvard.edu/t/micro-speech-example-from-tensorflow-on-an-esp32/376/4 "2021-02-19T16:22:31Z")

</div>

I’ve worked out what the issue was! To understand the code better I typed it out, rather than copy/paste based upon the micro\_speech example… unfortunately that meant I somehow missed the step of allocating the tensor after initializing the interpreter, hence the data buffer wasn’t allocated.

So I’m making progress now - except now I’ve hit an issue with my version of the model as I’ve somehow ended up with a CONV\_2D instead of DEPTHWISE\_CONV\_2D operator - so I think I need to look at how I trained the model (I thought I had done it the same way as the micro\_speech example, but it might be I’ve mixed up a parameter to the train.py script)

Just to explain what I’m trying to do (apart from learning how this all works!) - I’m working off the micro\_speech example as a basis, but I’ve created a new model using recordings I made (from the ESP32) of my pool pump running at different speeds with the intention of being able to detect what speed the pump is running at remotely. My long term aim is actually to use Autoencoding to do anomaly detection so I can tell if my pump is having issues as well as report the running level so I can integrate it with Home Assistant to let me know if I’ve left the pump on, but started with just the level detection as the keyword spotting example matches the data format more closely than the heart rate anomaly detection.

---

<div class="post-metadata">

**Author:** ![gameldar](https://avatars.discourse-cdn.com/v4/letter/g/ecae2f/32.png) [@gameldar](https://discuss.tinyml.seas.harvard.edu/u/gameldar)\
**Post date:** [February 22, 2021, 6:01am UTC](https://discuss.tinyml.seas.harvard.edu/t/micro-speech-example-from-tensorflow-on-an-esp32/376/5 "2021-02-22T06:01:17Z")

</div>

> [@gameldar](#):
>
> So I’m making progress now - except now I’ve hit an issue with my version of the model as I’ve somehow ended up with a CONV\_2D instead of DEPTHWISE\_CONV\_2D operator - so I think I need to look at how I trained the model (I thought I had done it the same way as the micro\_speech example, but it might be I’ve mixed up a parameter to the train.py script)

So I’ve managed to fix this issue… but only by doing the training in a Google Colab instead of running it locally. As far as I can see the only difference I have locally is that I’m running with Tensorflow 2 (2.4.1) locally versus Tensforflow 1 on the Colab. Does this sound like a plausible reason for the difference?

---

<div class="post-metadata">

**Author:** ![vjreddi](https://yyz1.discourse-cdn.com/flex027/user_avatar/discuss.tinyml.seas.harvard.edu/vjreddi/32/35_2.png) [@vjreddi](https://discuss.tinyml.seas.harvard.edu/u/vjreddi)\
**Post date:** [February 22, 2021, 2:14pm UTC](https://discuss.tinyml.seas.harvard.edu/t/micro-speech-example-from-tensorflow-on-an-esp32/376/6 "2021-02-22T14:14:17Z")

</div>

Cool. Thanks for sharing. So are you able to run the model on device now?

So just to confirm, you still have a DEPTHWISE\_CONV\_2D? And you are still using the TinyCov model, right? The one we discuss in Course 2 (not sure if you are taking it or not, but just using that as a reference).

---

<div class="post-metadata">

**Author:** ![gameldar](https://avatars.discourse-cdn.com/v4/letter/g/ecae2f/32.png) [@gameldar](https://discuss.tinyml.seas.harvard.edu/u/gameldar)\
**Post date:** [February 22, 2021, 4:09pm UTC](https://discuss.tinyml.seas.harvard.edu/t/micro-speech-example-from-tensorflow-on-an-esp32/376/7 "2021-02-22T16:09:40Z")

</div>

> [@vjreddi](#):
>
> So just to confirm, you still have a DEPTHWISE\_CONV\_2D? And you are still using the TinyCov model, right? The one we discuss in Course 2 (not sure if you are taking it or not, but just using that as a reference).

Yes - the training I was running locally is the version I had running from Course 2 ([https://github.com/tinyMLx/colabs/blob/master/3-5-18-TrainingKeywordSpotting.ipynb](https://github.com/tinyMLx/colabs/blob/master/3-5-18-TrainingKeywordSpotting.ipynb)) - the working version is actually the version from the tensorflow micro\_speech example ([https://github.com/tensorflow/tensorflow/tree/master/tensorflow/lite/micro/examples/micro\_speech/train](https://github.com/tensorflow/tensorflow/tree/master/tensorflow/lite/micro/examples/micro_speech/train)), but as far as I could see they are identical in practice.

So in both cases I have:

> PREPROCESS = ‘micro’  
> WINDOW\_STRIDE = 20  
> MODEL\_ARCHITECTURE = ‘tiny\_conv’  
> QUANT\_INPUT\_MIN = 0.0  
> QUANT\_INPUT\_MAX = 26.0  
> QUANT\_INPUT\_RANGE = QUANT\_INPUT\_MAX - QUANT\_INPUT\_MIN
> 
> SAMPLE\_RATE = 16000  
> CLIP\_DURATION\_MS = 1000  
> WINDOW\_SIZE\_MS = 30.0  
> FEATURE\_BIN\_COUNT = 40  
> BACKGROUND\_FREQUENCY = 0.8  
> BACKGROUND\_VOLUME\_RANGE = 0.1  
> TIME\_SHIFT\_MS = 100.0

The version built with tensorflow 2.4.1 results in the following error when trying to allocate the tensors:

> Didn’t find op for builtin opcode ‘CONV\_2D’ version ‘3’. An older version of this builtin might be supported. Are you using an old TFLite binary with a newer model?

When I switched to using the version from the micro\_speech source it got further. At the time I was printing out the layers and could see that I had DEPTHWISE\_CONV\_2D when that version of the model was loaded, compared to the straight CONV\_2D with my locally trained version.  
So I uploaded my sound samples to a Colab and reran the training with my audio samples and the resulting model successfully worked.

I’ve now got the model running on my ESP32 - but I haven’t managed to confirm it works yet (as the pump is outside my kids’ bedrooms and it is late at night!) - all I get responding is the ‘None’ response (which I should probably roll into the Silence examples from the speech commands set)

I’ve uploaded the working case to my github - but it is ugly and isn’t a good example to follow at this point:

> **[GitHub - gameldar/pump-power-levels-tinyml: Experimenting with Tensorflow...](https://github.com/gameldar/pump-power-levels-tinyml)**
>
> Experimenting with Tensorflow Lite Micro to detect pool pump levels - GitHub - gameldar/pump-power-levels-tinyml: Experimenting with Tensorflow Lite Micro to detect pool pump levels

NOTE: you won’t be able to build this without having setup tensorflow to work with platformio
