This is a machine learning model that detects hot or cold water from the sound of pouring (in practical terms the model was fed data where >= 175 f was hot and <= 75 f was cold).
This project is an effort to use skills from Fast.ai's excellent MOOC, and was inspired by an NPR piece on the sound water makes when poured at different temperatures.
Additionally, this project can act as a liberally licensed example of sound classification using fastai as a wrapper around torch. I did some digging while working on this project, and ended up relying very little on other people's work due to licensing concerns! Fear not friend, this project has you covered with an E2E log of my workflow.
Build log 1 starts with a more detailed project description.
Submit an audio clip of pouring water here: https://ml-hot-or-cold.projects.chrisjeakle.com/
Alternatively: clone this repo, and follow the steps used in build log 3 to test data of your own.
Take a look at the build logs to find out!
Build log 1 outlines a series of experiments done to choose a project architecture.
Build log 2 fine-tunes the chosen experimental design into a working model.
Finally, build log 3 tries out the model on some data the model has never seen before (not even for validation).
There is a simple infrence pipeline defined in server.py under the evaluate_audio_sample route.
I've started an addendum with notes and corrections I've learned as I work further in the fast.ai course.
- Install dependencies:
- Install miniconda:
cd ~ wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh bash Miniconda3-latest-Linux-x86_64.sh # Proceed through the install process source ~/.bashrc rm Miniconda3-latest-Linux-x86_64.sh- Run
conda config --set auto_activate_base falseto start your shell sessions outside conda
- Run
conda create -n ml-hot-or-cold python=3.8conda activate ml-hot-or-cold- Install the Jupyter Notebook server
conda install jupyter notebook
- Install project dependencies
pip3 install -r requirements.txt- On certain platforms (such as an Anaconda prompt on Windows) torch and fastai may not install correctly through pip. Use conda to install them in such cases.
- e.g.:
conda install -c pytorch -c fastai fastai
- e.g.:
- Install miniconda:
- Start the notebooks using:
conda activate ml-hot-or-cold && jupyter notebook- Navigate to the url and port printed in the terminal window
- Start the server:
conda activate ml-hot-or-cold && python3 server.py- Navigate to the url and port printed in the terminal window
- When done, exit conda via
conda deactivate
A guide from absolutely nothing to a working site hosted on an VM: I pulled my hair out so you don't have to!
- Provision an Ubuntu 18 LTS VM with at least 2GiB of ram
- Note: for some improved security, be sure to harden your ssh setup
- SSH in
- Install pre-requisite software
sudo apt install git virtualenv libsndfile1 ffmpeg nginx python3-dev
- Create a new non-root user to run the web service
sudo useradd --system ml-hot-or-cold- Create a
systemuser, we have no need for interactive shell sessions or a home dir
- Create a
- Set up the server files
- Note: many of the following steps use
sudo- This is necessary because
/srv/is owned by the root user - This is good -- we want the web server files to be user readable, but root owned
- Why: the server is exposed to the internet and should have the fewest possible permissions, it has no need to be able to modify its own code
- This is necessary because
- Clone this repo into the
/srv/directory- e.g.:
sudo git clone https://github.com/cjjeakle/ml-hot-or-cold.git /srv/ml-hot-or-cold/
- e.g.:
- Navigate to the root of this repo
- e.g.:
cd /srv/ml-hot-or-cold/
- e.g.:
mkdir ./venv && sudo chown ml-hot-or-cold ./venv- Unfortunately, because venv doesn't work well with sudo, we will need the service user to own the virtualenv we create
sudo -u ml-hot-or-cold virtualenv -p /usr/bin/python3 ./venv- This ensures the server is run using
python3
- This ensures the server is run using
- Install pip dependencies
sudo -u ml-hot-or-cold /bin/bash -c "source venv/bin/activate && pip3 install -r requirements.txt"- If you hit memory errors installing torch, try these steps
- Note: allocate 2097152 of swap, e.g.: 2GiB
- If you hit memory errors installing torch, try these steps
- Test that you can start the server as the service user
sudo -u ml-hot-or-cold /bin/bash -c 'cd /srv/ml-hot-or-cold/ && source venv/bin/activate && gunicorn --name ml-hot-or-cold --bind=unix:///tmp/ml-hot-or-cold.sock -w 3 -k uvicorn.workers.UvicornWorker --preload --timeout=120 --log-level warning server:app' # Become the service user and start the server- Test connectivity in another terminal session (or by running the command above in a
screenand detaching to run the following)curl --unix-socket ///tmp/ml-hot-or-cold.sock http://localhost
- Note: many of the following steps use
- Configure the web server using systemd
- Create a service:
/etc/systemd/system/ml-hot-or-cold.service[Unit] Description=ml-hot-or-cold web server [Service] Type=simple Restart=always User=ml-hot-or-cold Group=ml-hot-or-cold WorkingDirectory=/srv/ml-hot-or-cold/ ExecStart=/bin/bash -c 'cd /srv/ml-hot-or-cold/ && source venv/bin/activate && gunicorn --name ml-hot-or-cold --bind=unix:///tmp/ml-hot-or-cold.sock -w 3 -k uvicorn.workers.UvicornWorker --preload --timeout=120 --log-level warning server:app' [Install] WantedBy=multi-user.target- See the uvicorn deployment docs for all available options
- It appears the best practice is to set
-wto 2 * num_cores + 1
- Configure the service to start with the system, and manually start it up immediately:
sudo systemctl enable ml-hot-or-cold && sudo systemctl start ml-hot-or-cold
- You can see high-level service logs using:
systemctl status ml-hot-or-coldjournalctl -u ml-hot-or-cold
- Create a service:
- Verify your systemd config using
curl --unix-socket ///tmp/ml-hot-or-cold.sock http://localhost - Set up nginx
- I ended up using this configuration in
/etc/nginx/conf.d/ml-hot-or-cold.conf:server { listen 80; listen [::]:80; client_max_body_size 10m; server_name ml-hot-or-cold.projects.chrisjeakle.com; proxy_redirect off; proxy_buffering off; proxy_force_ranges on; location / { # Limit available HTTP methods to minimize attack surface limit_except GET HEAD POST { deny all; } proxy_pass http://ml_hot_or_cold_upstream; # Add security headers, allows style-src (CSS) from stackpath.bootstrapcdn.com add_header X-Content-Type-Options nosniff; add_header X-Frame-Options SAMEORIGIN; add_header X-XSS-Protection "1; mode=block"; add_header Referrer-Policy "strict-origin-when-cross-origin"; add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always; add_header Content-Security-Policy "default-src 'self'; script-src 'self'; img-src 'self' data:; style-src 'self' 'unsafe-inline' https://stackpath.bootstrapcdn.com; font-src 'self'; connect-src 'self'; object-src 'none'; frame-ancestors 'none';"; # Enable clickjacking protection for modern browsers add_header X-Content-Security-Policy "frame-ancestors 'self'"; add_header X-WebKit-CSP "frame-ancestors 'self'"; } } upstream ml_hot_or_cold_upstream { server unix:/tmp/ml-hot-or-cold.sock; }- Run
sudo systemctl enable nginx && sudo systemctl start nginx
- Run
- You can see nginx logs under
/var/log/nginx/- You can
grep -v "ml-hot-or-cold" /var/log/nginx/access.log | less +Gto see all accesses to the site, for example
- You can
- I ended up using this configuration in
- Set up letsencrypt certbot (for TLS)
The data is annotated with temperatures, but those annotations are not based on precise measurements. The 40 degree water was kept in the refrigerator before pouring, the 4x degree water was poured a couple times so I raised the annotated temperature a bit, the 7x degree water was at room temperature (the value is based on my thermostat at the time), and all temperatures >= 175 were annotated with the set temperature on my electric kettle. All temperatures are in Fahrenheit.
The MIT License (MIT)
Copyright (c) 2020 Chris Jeakle
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.