Editing the Anomaly Detection (K-Means) block locally

Question/Issue:
Hi, so I started tinkering with Edge Impulse about two weeks ago. My use case is basically gathering data from multiple Seeed Xiao Nrf Sense Sensors, and using the readings to train multiple classification models that label the data as good, bad, or idle. I’ll be deploying these models on a Flutter application that connects to the sensors and performs live classification. I tested a simple version of this in Edge Impulse Studio, and it works well. The Impulse I designed included a Spectral Features DSP block, a Classification block, and an Anomaly Detection (K-Means) block.

However, after a lot of research, I realised that the best deployment option for me would be the .tflite file. In this thread, I saw that the tflite model only contains the classification block, and the DSP and anomaly detection would have to be included separately.

So I did find the code for the DSP block on edge impulse’s github, but I’ve been unable to find anything for the anomaly detection block. There isn’t any option to edit it locally in the Studio either.

I did find this thread, which explains what exactly the Anomaly Detection block does, but I would love to be able to mimic Edge Impulse’s approach exactly.

  1. So my first question is, am I missing this the anomaly detection block’s implementation in the docs somewhere? or is it not available at the moment?

  2. This feels a bit naive, but I’m currently thinking of uploading both the DSP block and anomaly detection block (thinking of coding it myself if I don’t find anything online) as separate tflite files to Edge Impulse. This will help me maintain consistency between the Impulse I train and the Impulse I finally deploy. But does that make sense? As this will lead to me having to combine 3 separate tflite models in a flutter environment and building a pipeline out of that? But I don’t even know if that’s possible at the moment, would love if anyone has any input on that!

  3. Also, if I upload a tflite model for anomaly detection block to edge impulse, and use that same tflite file on my Flutter app, will I be missing something? Like is the anomaly detection block also trained on edge impulse? And if so, how would I get access to that ‘trained’ block?

I’m sorry if my questions don’t make sense, I’m still learning a lot. But if anyone has any suggestions, I would love to hear them out! Thanks so much!

Hi @namayjindal,

  1. Some of the blocks, including the anomaly detection block, are not open source.
  2. You would need to write K-means in terms of supported TFLite Micro ops (e.g. tflite-micro/tensorflow/lite/micro/micro_mutable_op_resolver.h at main · tensorflow/tflite-micro · GitHub). That’s probably a lot of work for what it’s worth.
  3. If you upload a tflite model to Edge Impulse, you’d use the BYOM system (Bring your own model (BYOM) | Edge Impulse Documentation). When you use that, it overrides the blocks in your project, so you can no longer train. It assumes that you already have a pre-trained model that you are uploading.

If none of the Edge Impulse deployment options (e.g. C++ SDK, Python library) work for you, you might consider implementing your own K-means clustering, which isn’t nearly as bad as full NN models: Implementing k-means clustering from scratch in C++ · Reasonable Deviations

Hi Shawn,

Thanks so much for getting back to me! So I’m thinking of changing my approach -
My main purpose is to have multiple classification models for various exercises, and keep track of how many reps of each exercise were performed.

So I’m thinking of deploying a common DSP block (which will include peak detection and segment extraction) directly to the sensors. I’m hoping a common block should work for all exercises (the number of sensors do vary between exercises and therefore the models). Post this, we extract the raw features and send it across to my app, which will then have a self-implemented K-Means Anomaly detection block (currently not sure if this can be common, or if it’ll be exercise specific, would love any help/input if possible here!), as well as a classification model in the tflite format.

Does this seem like a more viable option? Thank you so much for your help in the previous message!

Hi @namayjindal,

Is there a reason for multiple classification models? Most modern ML models (including neural networks) can do multi-class predictions.

Hi Shawn,

My use case is to identify a ‘good’ rep of an exercise from either an anomaly, the idle state, or a ‘bad’ rep. The purpose is not to distinguish different exercises from each other, but rather identify the same exercise’s nature. This is why I’m looking into building multiple classification models. If you think there’s another way I could go about it, I’d be happy to learn more!

Thanks

Hi @namayjindal,

You can probably do a single K-means anomaly detection model for detecting “idle,” “good,” or “bad,” as K-means creates clusters around the different groupings of data. For example, if you had “good” data for “idle,” “squat,” and “pushup,” the K-means clustering algorithm would create groups for all of those. Now, when someone did one of those exercises, it could tell you “good” vs. “bad.”

If you need to identify the type of exercise performed, then you probably want a NN (or some other ML model) to do classification.

Does that help?