{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Predict\n", "Similar to training, prediction can be done via three interfaces:\n", "- via python, `das.predict.predict`\n", "- via the command line, `das predict`, with audio data from a wav file.\n", "- the GUI - see the [GUI tutorial](/tutorials_gui/predict)\n", "\n", "Prediction will:\n", "\n", "- load the audio data and the network\n", "- run inference to produce confidence scores (`class_probabilties`)\n", "- post-process the confidence score to extract the times of events and label segments.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Prediction using python" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "from pprint import pprint\n", "import scipy.io.wavfile\n", "import das.predict\n", "help(das.predict.predict)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "%%time\n", "samplerate, x = scipy.io.wavfile.read('dat/dmel_song_rt.wav')\n", "print(f\"DAS requires [T, channels], but single-channel wave files are loaded with shape [T,] (data shape is {x.shape}).\")\n", "x = np.atleast_2d(x).T\n", "events, segments, class_probabilities, class_names = das.predict.predict(x, \n", " model_save_name='models/dmel_single_rt/20200430_201821',\n", " verbose=2,\n", " segment_minlen=0.02,\n", " segment_fillgap=0.02)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Outputs of `predict`\n", "- `class_probabilties`: `[T, nb_classes]` including noise.\n", "- `segments`: Labelled segments\n", " - `samplerate_Hz`: \n", " - `names`: names of all segment types\n", " - `index`: indices of all segments types into class_probabiltiies\n", " - `probabilities = class_probabilites[:, index]`\n", " - `sequence`: sequence of segment names (one entry per detected segment). Excludes noise\n", " - `samples`: labelled sample trace (label of the sequence occupying each sample)\n", " - `onsets_seconds`, `offsets_seconds`, `durations_seconds`: Onsets, offsets, and duration of individual segmeents\n", "- `events`: Detected events\n", " - `samplerate_Hz`: \n", " - `index`: indices of all events types into class_probabiltiies\n", " - `names`: names of all event types\n", " - `probabilities`: probabilities (confidence scores) for detected events. Value of `class_probabilities` for the detected event index at each event time.\n", " - `seconds`: times (seconds) of detected events\n", " - `sequence`: sequence of event names (one per detected event)." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "plt.style.use('ncb.mplstyle')\n", "\n", "t0 = 0\n", "t1 = 30_000 \n", "fs =segments['samplerate_Hz']\n", "time = np.arange(t0, t1) / fs\n", "nb_classes = class_probabilities.shape[1]\n", "\n", "plt.figure(figsize=(30, 10))\n", "plt.subplot(411)\n", "plt.plot(time, x[t0:t1], 'k', linewidth=0.5)\n", "plt.title('Song')\n", "plt.xticks([])\n", "plt.ylim(-0.25, 0.25)\n", "\n", "plt.subplot(412)\n", "plt.imshow(class_probabilities[t0:t1].T, cmap='Greys')\n", "plt.yticks(np.arange(nb_classes), labels=class_names)\n", "plt.title('Raw confidence scores')\n", "plt.xticks([])\n", "\n", "ax = plt.subplot(413)\n", "plt.plot(time, x[t0:t1],'k', linewidth=0.5)\n", "plt.ylim(-0.25, 0.25)\n", "plt.title('Annotations')\n", "plt.xlabel('Time [seconds]')\n", "for onset, offset, segment_name in zip(segments['onsets_seconds'], segments['offsets_seconds'], segments['sequence']):\n", " if onset >= t0 /fs and offset <= t1 / fs:\n", " plt.plot([onset, offset], [0.1, 0.1], c='b')\n", " ax.annotate(segment_name, xy=(onset, 0.11), c='b')\n", "\n", "for pulse_time, pulse_name in zip(events['seconds'], events['sequence']):\n", " if pulse_time >= t0 /fs and pulse_time <= t1 / fs:\n", " plt.axvline(pulse_time, c='r')\n", " ax.annotate(pulse_name, xy=(pulse_time, 0.1), c='r', rotation=-90)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Prediction using command-line scripts\n", "Will save the output of `das.predict.predict` to a h5 file ending in `_das.h5` or specified via the `--save-filename` argument.\n", "\n", "See [cli](/technical/cli) for a full list of arguments." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "!das predict dat/dmel_song_rt.wav models/dmel_single_rt/20200430_201821" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import h5py\n", "with h5py.File('dat/dmel_song_rt_das.h5', mode='r') as f:\n", " print(list(f.keys()))" ] } ], "metadata": { "file_extension": ".py", "kernelspec": { "display_name": "Python 3.9.9 ('base')", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.9" }, "mimetype": "text/x-python", "name": "python", "npconvert_exporter": "python", "pygments_lexer": "ipython3", "version": 3, "vscode": { "interpreter": { "hash": "7ea0ec616133ead53c1908c8f6539f5c0cb9b2f78368e2bb6ab3f847e89ca400" } } }, "nbformat": 4, "nbformat_minor": 4 }