{ "cells": [ { "cell_type": "markdown", "id": "357397da-2398-4735-a4b3-9333a48e3161", "metadata": {}, "source": [ "# Customize CNN training\n", "\n", "This notebook demonstrates how to use classes from `opensoundscape.ml.cnn` and architectures created using `opensoundscape.ml.cnn_architectures` to\n", "\n", "- choose between single-target and multi-target model behavior\n", "- modify learning rates, learning rate decay schedule, and regularization\n", "- choose from various CNN architectures\n", "- train a multi-target model with a special loss function\n", "- use strategic sampling for imbalanced training data\n", "- customize preprocessing: train on spectrograms with a bandpassed frequency range\n", "\n", "Rather than demonstrating their effects on training (model training is slow!), most examples in this notebook either don't train the model or \"train\" it for 0 epochs for the purpose of demonstration.\n", "\n", "For an introductory demonstration of model training, please see the [\"Train a CNN\" tutorial](https://opensoundscape.org/en/latest/tutorials/train_cnn.html). For a demo of how to apply a trained model to a dataset, see the [\"Predict with pretrained CNNs\" tutorial](https://opensoundscape.org/en/latest/tutorials/predict_with_cnn.html).\n", "\n", "For a hands-on walkthrough of machine learning for bioacoustics, use the [\"Classifiers 101 Guide\"](https://opensoundscape.org/en/latest/classifier_guide/guide.html)\n", "\n", "## Run this tutorial\n", "\n", "This tutorial is more than a reference! It's a Jupyter Notebook which you can run and modify on Google Colab or your own computer.\n", "\n", "|Link to tutorial|How to run tutorial|\n", "| :- | :- |\n", "| [](https://colab.research.google.com/github/kitzeslab/opensoundscape/blob/master/docs/tutorials/customize_cnn_training.ipynb) | The link opens the tutorial in Google Colab. Uncomment the \"installation\" line in the first cell to install OpenSoundscape. |\n", "| [](https://minhaskamal.github.io/DownGit/#/home?url=https://github.com/kitzeslab/opensoundscape/blob/master/docs/tutorials/customize_cnn_training.ipynb) | The link downloads the tutorial file to your computer. Follow the [Jupyter installation instructions](https://opensoundscape.org/en/latest/installation/jupyter.html), then open the tutorial file in Jupyter. |" ] }, { "cell_type": "code", "execution_count": null, "id": "b7bb1cf8-823c-49ae-8266-77cd8150f28e", "metadata": {}, "outputs": [], "source": [ "# if this is a Google Colab notebook, install opensoundscape in the runtime environment\n", "if 'google.colab' in str(get_ipython()):\n", " %pip install \"opensoundscape==0.12.1\" \"jupyter-client<8,>=5.3.4\" \"ipykernel==6.17.1\"" ] }, { "cell_type": "markdown", "id": "25011ade-9721-424d-a554-d3212dfc7571", "metadata": {}, "source": [ "## Setup\n", "\n", "### Import needed packages" ] }, { "cell_type": "code", "execution_count": 1, "id": "eb11d417-c381-4950-a78c-65e8a512a786", "metadata": {}, "outputs": [], "source": [ "from opensoundscape.preprocess import preprocessors\n", "from opensoundscape.ml import cnn, cnn_architectures\n", "\n", "import torch\n", "import pandas as pd\n", "from pathlib import Path\n", "import numpy as np\n", "import random \n", "import subprocess\n", "\n", "from matplotlib import pyplot as plt\n", "plt.rcParams['figure.figsize']=[15,5] #for big visuals\n", "%config InlineBackend.figure_format = 'retina'" ] }, { "cell_type": "markdown", "id": "5eb3e483-1160-4abb-a774-7ca5f612dd52", "metadata": {}, "source": [ "### Download labeled audio files\n", "\n", "The Kitzes Lab has created a small labeled dataset of short clips of American Woodcock vocalizations. You have two options for obtaining the folder of data, called `woodcock_labeled_data`:\n", "\n", "1. Run the following cell to download this small dataset. These commands require you to have `tar` installed on your computer, as they will download and unzip a compressed file in `.tar.gz` format. \n", "\n", "2. Download a `.zip` version of the files by clicking [here](https://pitt.box.com/shared/static/m0cmzebkr5qc49q9egxnrwwp50wi8zu5.zip). You will have to unzip this folder and place the unzipped folder in the same folder that this notebook is in." ] }, { "cell_type": "code", "execution_count": null, "id": "811c6470-044b-4acd-a3dc-1492c7a1e39f", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ " % Total % Received % Xferd Average Speed Time Time Time Current\n", " Dload Upload Total Spent Left Speed\n", " 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0\n", "100 9499k 100 9499k 0 0 1316k 0 0:00:07 0:00:07 --:--:-- 2435k\n" ] }, { "data": { "text/plain": [ "CompletedProcess(args=['rm', 'woodcock_labeled_data.tar.gz'], returncode=0)" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "subprocess.run(\n", " [\n", " \"curl\",\n", " \"https://drive.google.com/uc?export=download&id=1Ly2M--dKzpx331cfUFdVuiP96QKGJz_P\",\n", " \"-L\",\n", " \"-o\",\n", " \"woodcock_labeled_data.tar.gz\",\n", " ]\n", ") # Download the data\n", "subprocess.run(\n", " [\"tar\", \"-xzf\", \"woodcock_labeled_data.tar.gz\"]\n", ") # Unzip the downloaded tar.gz file\n", "subprocess.run(\n", " [\"rm\", \"woodcock_labeled_data.tar.gz\"]\n", ") # Remove the file after its contents are unzipped" ] }, { "cell_type": "markdown", "id": "fe28c0b3-f481-4d90-b3be-647c3d68fed5", "metadata": {}, "source": [ "### Prepare audio data\n", "\n", "To create a machine learning model, we need two dataframes of labeled clips, one for training and one for testing. \n", "\n", "The steps to create these dataframes are described in more detail in other tutorials (e.g. the [\"Audio annotations\" tutorial](tutorials/annotations.html)).\n", "\n", "First, we need a dataframe with file paths in the index, so we manipulate the included `one_hot_labels.csv` slightly." ] }, { "cell_type": "code", "execution_count": null, "id": "83f851bc-acad-47f5-a595-3f6ce6765dd7", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
| \n", " | present | \n", "
|---|---|
| file | \n", "\n", " |
| ./woodcock_labeled_data/d4c40b6066b489518f8da83af1ee4984.wav | \n", "1 | \n", "
| ./woodcock_labeled_data/e84a4b60a4f2d049d73162ee99a7ead8.wav | \n", "0 | \n", "
| ./woodcock_labeled_data/79678c979ebb880d5ed6d56f26ba69ff.wav | \n", "1 | \n", "
| ./woodcock_labeled_data/49890077267b569e142440fa39b3041c.wav | \n", "1 | \n", "
| ./woodcock_labeled_data/0c453a87185d8c7ce05c5c5ac5d525dc.wav | \n", "1 | \n", "