Create a prediction model that is based on the sounds of cats and dogs. During training, the model must be able to compare the audio files that form the training data to determine how to identify a dog barking and a cat purring.
Learn how to best gather and prepare data, create and deploy models, deploy and test a signal processing application, create models with binary classifications, and display the predictions on a web page created using Node-RED.
This repo is based on the IBM Early Program - Animal Sound project. Here.
- Getting Started
- Gathering and Preparing Data
- Build a Machine Learning Model
- Create predictions in a Node-RED applicaiton
- Conclusion
These instructions will get you a copy of the project up and running on your local machine for development and testing purposes.
-
Sign up for an IBM Cloud account.
-
Install and set up Python 3.
-
Install and set up Git command line.
-
Install and set up Pip.
-
Install and set up a suitable IDE to modify Python code, for example:
-
Create an account in Kaggle
-
Clone the repository:
git clone https://github.com/bedangSen/Animal-Sound-Classifier-using-Watson-Studio.git
-
Change to the new animal sounds directory:
cd Animal-Sound-Classifier-using-Watson-Studio
-
Optional: If you have virtualenv, create a new environment for the application and then activate the environment.
-
Find the path by running which or where commands:
- Windows
where python
- Mac or Linux
which python
- Windows
-
You can then use the path to create your virtual environment.
virtualenv animals -p <path to your installed version of python>
-
Activate the environment:
-
Windows
animals/Script/activate.bat
-
Mac or Linux
source animals/bin/activate
-
-
-
If you don’t already have an audio directory, create one for the audio files that you will download from Kaggle:
mkdir audio
-
Sign in to Kaggle here. and download the cats_dogs.zip file.
-
Extract the files and copy them to the audio directory.
-
Change to the src directory inside the animal-sounds directory:
cd src
-
Run the pip install command to install the application requirements, which are the converter and the service application that you'll use in a subsequent lab:
pip install -r requirements.txt
-
Run the converter application that finds the audio files and generates a sound.csv file in the outputs folder:
python ospconverter.py
-
Review the generated file.
-
Log in to your IBM Cloud account and create an instance of the Watson Studio service.
- Name your service instance.
- Choose a region.
- Select the
lite
plan. - Click
Create
.
-
Launch Watson Studio by clicking on
Get Started
. -
From the home page and click
Create a New Project
.- Select
Data Science
- Give the project a name and description.
- Make sure that the Storage field is populated with cloud-object-storage credentials.
- If your Storage field is showing as unpopulated, click
Cloud Object Storage
. - Click
Add
to add Cloud Object Storage for Watson Studio. - You might need to go back to the previous step to associate the cloud object storage with your data science project.
- When you're done creating the project, click
Create
.
- If your Storage field is showing as unpopulated, click
- Select
-
Create a Watson Machine Learning Model
- Click on
Assets
- Click Add to project and select the Watson Machine Learning asset type.
- Name the model and optionally provide a description.
- Select the project that you created and click
Associate a Machine Learning service instance
. - If you do not have a service, select the appropriate plan and click
Create
. If you already have a service, clickExisting
and select it from the list. - Select
Model Builder
because you want the system to do this for you. - Associate an Apache Spark instance. Similar to connecting the Machine Learning service, click
Associate an IBM Analytics
for Apache Spark instance. - If you have an existing service, go to the
Existing
tab and select the service. If you need to create a service, follow the instructions in the New tab. - Click
Reload
. - Select
Manual
so that you can prepare the data and select the models yourself. - Click on
Create
.
- Click on
-
In the Select Data Asset page, click
Add Data Assets
.- Click
Load
and browse for the cats and dogs CSV file. On a Mac, it's the soundmac.csv file. - Select the file that you just uploaded in the main page. You might need to refresh the page to see the file. Then, click
Next
.
- Click
-
Train the model.
-
Select
COLUMN1
from the Column value to predict menu. For Feature columns, you can decide which columns to use. For this project leave it asAll (default)
because all the remaining columns are significant. -
Select
Binary Classification
because you are building a model that is for cats and dogs only. -
Leave the validation split at 60, 20, 20. This will use 60% of the data to train the models, 20% to test the models, and 20% as a check of the better performing models to detect overfitting.
-
On the right side of the page, click Add
Estimators
. -
Select all available estimators. By selecting all of them, you can see which model gives the best results for the data set. Click
Add
. Then, clickNext
.
-
-
Analyze the Performance, Area under roc curve, and Area under pr curve columns. Select the model with the best performance and highest scoring curve columns. Then, click
Save
.
- Click
Deployments
. - On the right side of the page, click
Add Deployment
. - Give the deployment a name (and optional description), select
Web service
, and then clickSave
. - Wait for the model to deploy. The status will show DEPLOY_SUCCESS when it is complete. If it fails to deploy, try refreshing the browser after a few minutes. Alternatively, delete that deployment and follow these steps again.
- Click the name of the deployment.
- note the
deployment ID
.
- note the
-
Log in to IBM Cloud.
-
If you haven’t created an instance of Node-RED, create one:
-
In IBM Cloud, click Catalog > Starter Kits > Node-RED Starter.
-
Enter an application name and domain.
The application name must be unique because it will be publicly addressable. Most likely the name catsdogs is already taken. The domain for this application does not need to match the domain that you used for Watson Studio.
-
Click
Create
. It might take some time for your instance to be created and started. -
After your instance of Node-RED is running, click
Visit App URL
. -
Optional: Secure your Node-RED instance with a user name and password.
-
Click
Next
andNext
again to complete the setup. Then, clickGo to your Node-RED flow editor
.
-
-
Log in in to your Node-RED instance by using the credentials that you previously specified.
Next, you will be importing a prebuilt flow into your Node-RED instance. This flow will allow you to upload a file and run it through the machine learning model that you created earlier.
-
Open a new tab and go to the GitHub repositoryand select the
noderedflows
folder.- Select the
predictionflow.json
file. - Click
Raw
and copy the file contents to your clipboard.By clicking
Raw
, you can copy the raw data without having any unnecessary data on the clipboard that will result in errors when pasting.
- Select the
-
Back in your Node-RED instance, click the menu icon and click
Import > Clipboard
.- Paste your clipboard contents into the field and click
Import
.You might see a number of unknown nodes that appear in the imported flow. This is because the Node-RED starter kit comes preinstalled only with certain nodes. You will install the additional nodes in the next steps.
- Paste your clipboard contents into the field and click
-
Click the menu and click
Manage palette
. Then, select theInstall
tab.- Search for
node-red-contrib-watson-machine-learning
. - Click
Install
. Then, clickInstall
again to confirm the installation. - Install two more nodes:
node-red-contrib-browser-utils
, which contains a microphone, andnode-red-node-base64
. The flow should now show all nodes.
- Search for
-
Click Deploy. Ignore any warnings about the new nodes. You will configure those nodes in the next few steps.
Your application is now deployed and is ready for you to test. Before experimenting with an audio file, you will start with a hardcoded test to check that the prediction is working.
-
Initiate the flow by using the blue tab on the left of the
Hard Coded Test node
.- Click the
Debug
tab. Depending on your version of Node-RED, you might need to click the Debug icon to show the debug messages pane.You should see a "No Configuration Found" error message. This occurs because the Watson Machine Learning node (Run Prediction) has not been configured.
- Click the
-
Double-click the machine learning node (Run Prediction).
- Click the Pencil icon to edit the WML Connection configuration. Then, complete the configuration by using your machine learning credentials. The Access Key can be any non-empty value.
- To find your credentials, select the service from IBM Cloud and click
Credentials
. If your machine learning credentials don’t include an access key, then enter dummy text into that field. Then, clickUpdate
when you're done. - Refetch the Model List.
This will temporarily hide the mode and deployments fields because the node invokes Watson Machine Learning APIs to retrieve a revised list. After the new lists are retrieved, the fields will reappear. Depending on network latency, this might a while to complete. If the fields are not displayed, click Done, click Deploy, and open the node again.
- Set the mode to
Run Prediction
. Then, select your model and deployment. - Click
Done
to save the configuration and deploy the updated flow.
-
Initiate the flow by clicking the left tab.
-
Open a terminal window and create or navigate to a directory for the application.
-
Navigate to animals-sounds/src directory.
-
Edit the
manifest.yaml
- Change the host name so that it is unique in the
manifest.yaml
file. - Save your changes.
- Change the host name so that it is unique in the
-
Log in to the IBM Cloud CLI if you aren't already:
ibmcloud login
If you are using a federated account, login using
ibmcloud login --sso
-
From the command line, push your application to IBM Cloud:
``` ibmcloud app push ``` > If the ibmcloud command doesn’t work, try cf push.After your application is running, you can access the test web page from a web browser:
https://<your-host-name>.mybluemix.net/audio
-
Click either
Upload
orPerform OSP
to select an audio file from the sounds directory in your project on your local machine.- If you see a series of numbers, your application is running, and the API is ready to use in your Node-RED flow.
- Open your Node-RED flow
- Double-click the Perform OSP HTTP Request node.
- Change the URL to https://.mybluemix.net/audio/nodered. Then, deploy your changes.
- Use either the microphone node or the file inject node to initiate the flow. Unless you have a cat or dog around, it’s probably better to use the file inject node.
- Check the output to verify the results.
You now have an application that takes audio tracks as input and processes a signal from the audio file to obtain a series of numbers that it uses to make a machine learning-based prediction as to whether the sound is from a cat or a dog.
You can expand the components that you developed in this repository to other sound-based machine learning models.