Blog post

Sleeping with One AI App Open

Projects
By Nick Bild
Sleeping with One AI App Open

There is nothing quite like a good night’s sleep. A nice long snooze helps us to think clearly, reduces stress, and even lowers our risk for serious diseases like diabetes and heart disease. The benefits of sleep are especially apparent to those that are not getting enough of it. Sleep deprivation can be caused by many factors, such as snoring  — 40% of adult men and 24% of adult women snore regularly, and this commonly impacts both sleep quality and quantity. And that is not the end of it, either. The person who sleeps with the snorer can also be disturbed and suffer from sleep deprivation (not to mention irritation with the snorer).

For most people, snoring is at its worst when sleeping on their backs. So, one simple, natural solution to snoring that often works is shifting to sleep on one’s side. Taking these facts under consideration, machine learning enthusiast Wamiq Raza decided to develop an algorithm to detect snoring that can run locally on a smartphone or other edge computing device. With such an algorithm available, one would be able to unobtrusively recognize patterns of snoring, perhaps on hardware they already own. This would make it possible to give a snorer a gentle nighttime nudge to remind them to roll on their side, or to provide their doctor with longitudinal data about their sleep issues.

Exploring the training data in Edge Impulse Studio

Raza knew from previous experience that a neural network classifier should be able to do the job of recognizing the characteristic sound of snoring, so he set about building this type of model in Edge Impulse Studio. Before a neural network can be put to work on a new task, it needs to be trained, however. A dataset of 1000 audio samples was pieced together from multiple sources for this purpose. 500 of the examples contained snoring sounds (some also with various background noises present), and another 500 did not contain any snoring, but rather consisted of a variety of common sounds — sirens of emergency vehicles, rain and thunderstorms, streetcar sounds, and people talking, for example.

The dataset was uploaded to Edge Impulse using the data acquisition tool, then an impulse was built to process and classify it. An Audio MFE preprocessing block was first added to extract time and frequency features from a signal. By extracting these features, it reduces the computational complexity of the rest of the pipeline — this is critically important in working with resource-constrained edge computing devices. Those features were then routed into a neural network with two outputs, snoring and not snoring.

The completed audio classification impulse

The model was trained for 100 epochs after some minor tweaking of the hyperparameters. The data acquisition tool had automatically set aside 20% of the data to be excluded from training so that it could be used to validate the model’s performance later. Using this test data, it was found that the model had achieved a very impressive 97.42% accuracy rate in distinguishing between audio samples containing snoring, and those that do not. A live classification tool is also available that makes it possible to test out an algorithm in real-world scenarios before deploying it to a physical device. Since Raza was wide awake, however, he moved on to the deployment process.

Results of model testing

As previously mentioned, Raza wanted to make sure that this algorithm was available in the most unobtrusive manner possible, so he deployed it to the smartphone that he already had using Edge Impulse Studio’s deployment tools. With just a few clicks, his smartphone became an expert snoring detector. He points out that the exact same machine learning classification pipeline can just as easily be deployed to alternate targets. This could come in handy if someone was interested in developing a dedicated snore detection device, for example.

Smartphone running snore detector

Detailed project documentation is available, so be sure to take a look for some helpful tips. You can also clone Raza’s Edge Impulse project to get a head start in building your own snore detector before you slip away to dreamland.


Want to see Edge Impulse in action? Schedule a demo today.

Comments

Subscribe

Are you interested in bringing machine learning intelligence to your devices? We're happy to help.

Subscribe to our newsletter