diff --git a/.gitattributes b/.gitattributes index c3be72b4844d782ec3d1a1706bb55da37a8bf54b..9375c545992d0f3f94760abf036a66848f14475a 100644 --- a/.gitattributes +++ b/.gitattributes @@ -76,3 +76,30 @@ Seq2Seq/Neural_MT_without_Teacher_Forcing/home/jovyan/work/W3A1/images/attn_mech Seq2Seq/Neural_MT_without_Teacher_Forcing/home/jovyan/work/W3A1/images/attn_model.png filter=lfs diff=lfs merge=lfs -text Seq2Seq/Neural_MT_without_Teacher_Forcing/home/jovyan/work/W3A1/images/date_attention.png filter=lfs diff=lfs merge=lfs -text Seq2Seq/Neural_MT_without_Teacher_Forcing/home/jovyan/work/W3A1/images/date_attention2.png filter=lfs diff=lfs merge=lfs -text +Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/audio_examples/chime.wav filter=lfs diff=lfs merge=lfs -text +Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/audio_examples/example_train.wav filter=lfs diff=lfs merge=lfs -text +Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/audio_examples/insert_reference.wav filter=lfs diff=lfs merge=lfs -text +Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/audio_examples/my_audio.wav filter=lfs diff=lfs merge=lfs -text +Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/audio_examples/train_reference.wav filter=lfs diff=lfs merge=lfs -text +Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/chime_output.wav filter=lfs diff=lfs merge=lfs -text +Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/images/model.png filter=lfs diff=lfs merge=lfs -text +Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/images/sound.png filter=lfs diff=lfs merge=lfs -text +Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/images/train_reference.png filter=lfs diff=lfs merge=lfs -text +Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/insert_test.wav filter=lfs diff=lfs merge=lfs -text +Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/raw_data/activates/1_act3.wav filter=lfs diff=lfs merge=lfs -text +Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/raw_data/activates/1.wav filter=lfs diff=lfs merge=lfs -text +Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/raw_data/activates/2_act3.wav filter=lfs diff=lfs merge=lfs -text +Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/raw_data/activates/2.wav filter=lfs diff=lfs merge=lfs -text +Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/raw_data/activates/3_act3.wav filter=lfs diff=lfs merge=lfs -text +Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/raw_data/activates/3.wav filter=lfs diff=lfs merge=lfs -text +Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/raw_data/backgrounds/1.wav filter=lfs diff=lfs merge=lfs -text +Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/raw_data/backgrounds/2.wav filter=lfs diff=lfs merge=lfs -text +Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/raw_data/dev/1.wav filter=lfs diff=lfs merge=lfs -text +Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/raw_data/dev/2.wav filter=lfs diff=lfs merge=lfs -text +Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/raw_data/negatives/1_0.wav filter=lfs diff=lfs merge=lfs -text +Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/raw_data/negatives/3.wav filter=lfs diff=lfs merge=lfs -text +Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/raw_data/negatives/4_0.wav filter=lfs diff=lfs merge=lfs -text +Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/raw_data/negatives/5_1.wav filter=lfs diff=lfs merge=lfs -text +Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/tmp.wav filter=lfs diff=lfs merge=lfs -text +Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/train.wav filter=lfs diff=lfs merge=lfs -text +Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/Trigger_word_detection_v2a.ipynb filter=lfs diff=lfs merge=lfs -text diff --git a/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/.ipynb_checkpoints/Trigger_word_detection_v2a-checkpoint.ipynb b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/.ipynb_checkpoints/Trigger_word_detection_v2a-checkpoint.ipynb new file mode 100644 index 0000000000000000000000000000000000000000..d763817226f2e52db04986540c527f73c8f6f1bf --- /dev/null +++ b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/.ipynb_checkpoints/Trigger_word_detection_v2a-checkpoint.ipynb @@ -0,0 +1,1836 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Trigger Word Detection\n", + "\n", + "Welcome to the second and last programming assignment of Week 3! \n", + "\n", + "In this week's videos, you learned about applying deep learning to speech recognition. In this assignment, you will construct a speech dataset and implement an algorithm for trigger word detection (sometimes also called keyword detection, or wake word detection). \n", + "\n", + "* Trigger word detection is the technology that allows devices like Amazon Alexa, Google Home, Apple Siri, and Baidu DuerOS to wake up upon hearing a certain word. \n", + "* For this exercise, our trigger word will be \"activate\". Every time it hears you say \"activate\", it will make a \"chiming\" sound. \n", + "* By the end of this assignment, you will be able to record a clip of yourself talking, and have the algorithm trigger a chime when it detects you saying \"activate\". \n", + "* After completing this assignment, perhaps you can also extend it to run on your laptop so that every time you say \"activate\" it starts up your favorite app, or turns on a network connected lamp in your house, or triggers some other event? \n", + "\n", + "\n", + "\n", + "In this assignment you will learn to: \n", + "- Structure a speech recognition project\n", + "- Synthesize and process audio recordings to create train/dev datasets\n", + "- Train a trigger word detection model and make predictions\n", + "\n", + "Let's get started!\n", + "\n", + "## Important Note on Submission to the AutoGrader\n", + "\n", + "Before submitting your assignment to the AutoGrader, please make sure you are not doing the following:\n", + "\n", + "1. You have not added any _extra_ `print` statement(s) in the assignment.\n", + "2. You have not added any _extra_ code cell(s) in the assignment.\n", + "3. You have not changed any of the function parameters.\n", + "4. You are not using any global variables inside your graded exercises. Unless specifically instructed to do so, please refrain from it and use the local variables instead.\n", + "5. You are not changing the assignment code where it is not required, like creating _extra_ variables.\n", + "\n", + "If you do any of the following, you will get something like, `Grader Error: Grader feedback not found` (or similarly unexpected) error upon submitting your assignment. Before asking for help/debugging the errors in your assignment, check for these first. If this is the case, and you don't remember the changes you have made, you can get a fresh copy of the assignment by following these [instructions](https://www.coursera.org/learn/nlp-sequence-models/supplement/qHIve/h-ow-to-refresh-your-workspace)." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Table of Contents\n", + "\n", + "- [Packages](#0)\n", + "- [1 - Data synthesis: Creating a Speech Dataset](#1)\n", + " - [1.1 - Listening to the Data](#1-1)\n", + " - [1.2 - From Audio Recordings to Spectrograms](#1-2)\n", + " - [1.3 - Generating a Single Training Example](#1-3)\n", + " - [Exercise 1 - is_overlapping](#ex-1)\n", + " - [Exercise 2 - insert_audio_clip](#ex-2)\n", + " - [Exercise 3 - insert_ones](#ex-3)\n", + " - [Exercise 4 - create_training_example](#ex-4)\n", + " - [1.4 - Full Training Set](#1-4)\n", + " - [1.5 - Development Set](#1-5)\n", + "- [2 - The Model](#2)\n", + " - [2.1 - Build the Model](#2-1)\n", + " - [Exercise 5 - modelf](#ex-5)\n", + " - [2.2 - Fit the Model](#2-2)\n", + " - [2.3 - Test the Model](#2-3)\n", + "- [3 - Making Predictions](#3)\n", + " - [3.1 - Test on Dev Examples](#3-1)\n", + "- [4 - Try Your Own Example! (OPTIONAL/UNGRADED)](#4)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\n", + "## Packages" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "deletable": false, + "editable": false + }, + "outputs": [], + "source": [ + "### v2.2" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import numpy as np\n", + "from pydub import AudioSegment\n", + "import random\n", + "import sys\n", + "import io\n", + "import os\n", + "import glob\n", + "import IPython\n", + "from td_utils import *\n", + "%matplotlib inline" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\n", + "## 1 - Data synthesis: Creating a Speech Dataset \n", + "\n", + "Let's start by building a dataset for your trigger word detection algorithm. \n", + "* A speech dataset should ideally be as close as possible to the application you will want to run it on. \n", + "* In this case, you'd like to detect the word \"activate\" in working environments (library, home, offices, open-spaces ...). \n", + "* Therefore, you need to create recordings with a mix of positive words (\"activate\") and negative words (random words other than activate) on different background sounds. Let's see how you can create such a dataset. \n", + "\n", + "\n", + "### 1.1 - Listening to the Data \n", + "\n", + "* One of your friends is helping you out on this project, and they've gone to libraries, cafes, restaurants, homes and offices all around the region to record background noises, as well as snippets of audio of people saying positive/negative words. This dataset includes people speaking in a variety of accents. \n", + "* In the raw_data directory, you can find a subset of the raw audio files of the positive words, negative words, and background noise. You will use these audio files to synthesize a dataset to train the model. \n", + " * The \"activate\" directory contains positive examples of people saying the word \"activate\". \n", + " * The \"negatives\" directory contains negative examples of people saying random words other than \"activate\". \n", + " * There is one word per audio recording. \n", + " * The \"backgrounds\" directory contains 10 second clips of background noise in different environments.\n", + "\n", + "Run the cells below to listen to some examples." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "IPython.display.Audio(\"./raw_data/activates/1.wav\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "IPython.display.Audio(\"./raw_data/negatives/4.wav\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "scrolled": false + }, + "outputs": [], + "source": [ + "IPython.display.Audio(\"./raw_data/backgrounds/1.wav\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "You will use these three types of recordings (positives/negatives/backgrounds) to create a labeled dataset." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\n", + "### 1.2 - From Audio Recordings to Spectrograms\n", + "\n", + "What really is an audio recording? \n", + "* A microphone records little variations in air pressure over time, and it is these little variations in air pressure that your ear also perceives as sound. \n", + "* You can think of an audio recording as a long list of numbers measuring the little air pressure changes detected by the microphone. \n", + "* We will use audio sampled at 44100 Hz (or 44100 Hertz). \n", + " * This means the microphone gives us 44,100 numbers per second. \n", + " * Thus, a 10 second audio clip is represented by 441,000 numbers (= $10 \\times 44,100$). \n", + "\n", + "#### Spectrogram\n", + "* It is quite difficult to figure out from this \"raw\" representation of audio whether the word \"activate\" was said. \n", + "* In order to help your sequence model more easily learn to detect trigger words, we will compute a *spectrogram* of the audio. \n", + "* The spectrogram tells us how much different frequencies are present in an audio clip at any moment in time. \n", + "* If you've ever taken an advanced class on signal processing or on Fourier transforms:\n", + " * A spectrogram is computed by sliding a window over the raw audio signal, and calculating the most active frequencies in each window using a Fourier transform. \n", + " * If you don't understand the previous sentence, don't worry about it.\n", + "\n", + "Let's look at an example. " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "IPython.display.Audio(\"audio_examples/example_train.wav\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "x = graph_spectrogram(\"audio_examples/example_train.wav\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The graph above represents how active each frequency is (y axis) over a number of time-steps (x axis). \n", + "\n", + "\n", + "
**Figure 1**: Spectrogram of an audio recording
\n", + "\n", + "\n", + "* The color in the spectrogram shows the degree to which different frequencies are present (loud) in the audio at different points in time. \n", + "* Green means a certain frequency is more active or more present in the audio clip (louder).\n", + "* Blue squares denote less active frequencies.\n", + "* The dimension of the output spectrogram depends upon the hyperparameters of the spectrogram software and the length of the input. \n", + "* In this notebook, we will be working with 10 second audio clips as the \"standard length\" for our training examples. \n", + " * The number of timesteps of the spectrogram will be 5511. \n", + " * You'll see later that the spectrogram will be the input $x$ into the network, and so $T_x = 5511$." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "_, data = wavfile.read(\"audio_examples/example_train.wav\")\n", + "print(\"Time steps in audio recording before spectrogram\", data[:,0].shape)\n", + "print(\"Time steps in input after spectrogram\", x.shape)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now, you can define:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "Tx = 5511 # The number of time steps input to the model from the spectrogram\n", + "n_freq = 101 # Number of frequencies input to the model at each time step of the spectrogram" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Dividing into time-intervals\n", + "Note that we may divide a 10 second interval of time with different units (steps).\n", + "* Raw audio divides 10 seconds into 441,000 units.\n", + "* A spectrogram divides 10 seconds into 5,511 units.\n", + " * $T_x = 5511$\n", + "* You will use a Python module `pydub` to synthesize audio, and it divides 10 seconds into 10,000 units.\n", + "* The output of our model will divide 10 seconds into 1,375 units.\n", + " * $T_y = 1375$\n", + " * For each of the 1375 time steps, the model predicts whether someone recently finished saying the trigger word \"activate\". \n", + "* All of these are hyperparameters and can be changed (except the 441000, which is a function of the microphone). \n", + "* We have chosen values that are within the standard range used for speech systems." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "Ty = 1375 # The number of time steps in the output of our model" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\n", + "### 1.3 - Generating a Single Training Example\n", + "\n", + "#### Benefits of synthesizing data\n", + "Because speech data is hard to acquire and label, you will synthesize your training data using the audio clips of activates, negatives, and backgrounds. \n", + "* It is quite slow to record lots of 10 second audio clips with random \"activates\" in it. \n", + "* Instead, it is easier to record lots of positives and negative words, and record background noise separately (or download background noise from free online sources). \n", + "\n", + "#### Process for Synthesizing an audio clip\n", + "* To synthesize a single training example, you will:\n", + " - Pick a random 10 second background audio clip\n", + " - Randomly insert 0-4 audio clips of \"activate\" into this 10 sec. clip\n", + " - Randomly insert 0-2 audio clips of negative words into this 10 sec. clip\n", + "* Because you had synthesized the word \"activate\" into the background clip, you know exactly when in the 10 second clip the \"activate\" makes its appearance. \n", + " * You'll see later that this makes it easier to generate the labels $y^{\\langle t \\rangle}$ as well. \n", + "\n", + "#### Pydub\n", + "* You will use the pydub package to manipulate audio. \n", + "* Pydub converts raw audio files into lists of Pydub data structures.\n", + " * Don't worry about the details of the data structures.\n", + "* Pydub uses 1ms as the discretization interval (1 ms is 1 millisecond = 1/1000 seconds).\n", + " * This is why a 10 second clip is always represented using 10,000 steps. " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Load audio segments using pydub \n", + "activates, negatives, backgrounds = load_raw_audio('./raw_data/')\n", + "\n", + "print(\"background len should be 10,000, since it is a 10 sec clip\\n\" + str(len(backgrounds[0])),\"\\n\")\n", + "print(\"activate[0] len may be around 1000, since an `activate` audio clip is usually around 1 second (but varies a lot) \\n\" + str(len(activates[0])),\"\\n\")\n", + "print(\"activate[1] len: different `activate` clips can have different lengths\\n\" + str(len(activates[1])),\"\\n\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Overlaying positive/negative 'word' audio clips on top of the background audio\n", + "\n", + "* Given a 10 second background clip and a short audio clip containing a positive or negative word, you need to be able to \"add\" the word audio clip on top of the background audio.\n", + "* You will be inserting multiple clips of positive/negative words into the background, and you don't want to insert an \"activate\" or a random word somewhere that overlaps with another clip you had previously added. \n", + " * To ensure that the 'word' audio segments do not overlap when inserted, you will keep track of the times of previously inserted audio clips. \n", + "* To be clear, when you insert a 1 second \"activate\" onto a 10 second clip of cafe noise, **you do not end up with an 11 sec clip.** \n", + " * The resulting audio clip is still 10 seconds long.\n", + " * You'll see later how pydub allows you to do this. " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Label the positive/negative words\n", + "* Recall that the labels $y^{\\langle t \\rangle}$ represent whether or not someone has just finished saying \"activate\". \n", + " * $y^{\\langle t \\rangle} = 1$ when that clip has finished saying \"activate\".\n", + " * Given a background clip, we can initialize $y^{\\langle t \\rangle}=0$ for all $t$, since the clip doesn't contain any \"activate\". \n", + "* When you insert or overlay an \"activate\" clip, you will also update labels for $y^{\\langle t \\rangle}$.\n", + " * Rather than updating the label of a single time step, we will update 50 steps of the output to have target label 1. \n", + " * Recall from the lecture on trigger word detection that updating several consecutive time steps can make the training data more balanced.\n", + "* You will train a GRU (Gated Recurrent Unit) to detect when someone has **finished** saying \"activate\". \n", + "\n", + "##### Example\n", + "* Suppose the synthesized \"activate\" clip ends at the 5 second mark in the 10 second audio - exactly halfway into the clip. \n", + "* Recall that $T_y = 1375$, so timestep $687 = $ `int(1375*0.5)` corresponds to the moment 5 seconds into the audio clip. \n", + "* Set $y^{\\langle 688 \\rangle} = 1$. \n", + "* We will allow the GRU to detect \"activate\" anywhere within a short time-internal **after** this moment, so we actually **set 50 consecutive values** of the label $y^{\\langle t \\rangle}$ to 1. \n", + " * Specifically, we have $y^{\\langle 688 \\rangle} = y^{\\langle 689 \\rangle} = \\cdots = y^{\\langle 737 \\rangle} = 1$. \n", + "\n", + "##### Synthesized data is easier to label\n", + "* This is another reason for synthesizing the training data: It's relatively straightforward to generate these labels $y^{\\langle t \\rangle}$ as described above. \n", + "* In contrast, if you have 10sec of audio recorded on a microphone, it's quite time consuming for a person to listen to it and mark manually exactly when \"activate\" finished. " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Visualizing the labels\n", + "* Here's a figure illustrating the labels $y^{\\langle t \\rangle}$ in a clip.\n", + " * We have inserted \"activate\", \"innocent\", \"activate\", \"baby.\" \n", + " * Note that the positive labels \"1\" are associated only with the positive words. \n", + "\n", + "\n", + "
**Figure 2**
\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Helper functions\n", + "\n", + "To implement the training set synthesis process, you will use the following helper functions. \n", + "* All of these functions will use a 1ms discretization interval\n", + "* The 10 seconds of audio is always discretized into 10,000 steps. \n", + "\n", + "\n", + "1. `get_random_time_segment(segment_ms)`\n", + " * Retrieves a random time segment from the background audio.\n", + "2. `is_overlapping(segment_time, existing_segments)`\n", + " * Checks if a time segment overlaps with existing segments\n", + "3. `insert_audio_clip(background, audio_clip, existing_times)`\n", + " * Inserts an audio segment at a random time in the background audio\n", + " * Uses the functions `get_random_time_segment` and `is_overlapping`\n", + "4. `insert_ones(y, segment_end_ms)`\n", + " * Inserts additional 1's into the label vector y after the word \"activate\"" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Get a random time segment\n", + "\n", + "* The function `get_random_time_segment(segment_ms)` returns a random time segment onto which we can insert an audio clip of duration `segment_ms`. \n", + "* Please read through the code to make sure you understand what it is doing. " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def get_random_time_segment(segment_ms):\n", + " \"\"\"\n", + " Gets a random time segment of duration segment_ms in a 10,000 ms audio clip.\n", + " \n", + " Arguments:\n", + " segment_ms -- the duration of the audio clip in ms (\"ms\" stands for \"milliseconds\")\n", + " \n", + " Returns:\n", + " segment_time -- a tuple of (segment_start, segment_end) in ms\n", + " \"\"\"\n", + " \n", + " segment_start = np.random.randint(low=0, high=10000-segment_ms) # Make sure segment doesn't run past the 10sec background \n", + " segment_end = segment_start + segment_ms - 1\n", + " \n", + " return (segment_start, segment_end)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Check if audio clips are overlapping\n", + "\n", + "* Suppose you have inserted audio clips at segments (1000,1800) and (3400,4500).\n", + " * The first segment starts at step 1000 and ends at step 1800. \n", + " * The second segment starts at 3400 and ends at 4500.\n", + "* If we are considering whether to insert a new audio clip at (3000,3600) does this overlap with one of the previously inserted segments? \n", + " * In this case, (3000,3600) and (3400,4500) overlap, so we should decide against inserting a clip here.\n", + "* For the purpose of this function, define (100,200) and (200,250) to be overlapping, since they overlap at timestep 200. \n", + "* (100,199) and (200,250) are non-overlapping. \n", + "\n", + "\n", + "### Exercise 1 - is_overlapping\n", + "\n", + "* Implement `is_overlapping(segment_time, existing_segments)` to check if a new time segment overlaps with any of the previous segments. \n", + "* You will need to carry out 2 steps:\n", + "\n", + "1. Create a \"False\" flag, that you will later set to \"True\" if you find that there is an overlap.\n", + "2. Loop over the previous_segments' start and end times. Compare these times to the segment's start and end times. If there is an overlap, set the flag defined in (1) as True. \n", + "\n", + "You can use:\n", + "```python\n", + "for ....:\n", + " if ... <= ... and ... >= ...:\n", + " ...\n", + "```\n", + "Hint: There is overlap if:\n", + "* The new segment starts before the previous segment ends **and**\n", + "* The new segment ends after the previous segment starts." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# UNQ_C1\n", + "# GRADED FUNCTION: is_overlapping\n", + "\n", + "def is_overlapping(segment_time, previous_segments):\n", + " \"\"\"\n", + " Checks if the time of a segment overlaps with the times of existing segments.\n", + " \n", + " Arguments:\n", + " segment_time -- a tuple of (segment_start, segment_end) for the new segment\n", + " previous_segments -- a list of tuples of (segment_start, segment_end) for the existing segments\n", + " \n", + " Returns:\n", + " True if the time segment overlaps with any of the existing segments, False otherwise\n", + " \"\"\"\n", + " \n", + " segment_start, segment_end = segment_time\n", + " \n", + " ### START CODE HERE ### (≈ 4 lines)\n", + " # Step 1: Initialize overlap as a \"False\" flag. (≈ 1 line)\n", + " overlap = None\n", + " \n", + " # Step 2: loop over the previous_segments start and end times.\n", + " # Compare start/end times and set the flag to True if there is an overlap (≈ 3 lines)\n", + " for previous_start, previous_end in previous_segments:\n", + " if None:\n", + " overlap = None\n", + " break\n", + " ### END CODE HERE ###\n", + "\n", + " return overlap" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "deletable": false, + "editable": false + }, + "outputs": [], + "source": [ + "### THIS CELL IS NOT EDITABLE\n", + "\n", + "# UNIT TEST\n", + "def is_overlapping_test(target):\n", + " assert target((670, 1430), []) == False, \"Overlap with an empty list must be False\"\n", + " assert target((500, 1000), [(100, 499), (1001, 1100)]) == False, \"Almost overlap, but still False\"\n", + " assert target((750, 900), [(100, 750), (1001, 1100)]) == True, \"Must overlap with the end of first segment\"\n", + " assert target((750, 1250), [(300, 600), (1250, 1500)]) == True, \"Must overlap with the beginning of second segment\"\n", + " assert target((750, 1250), [(300, 600), (600, 1500), (1600, 1800)]) == True, \"Is contained in second segment\"\n", + " assert target((800, 1100), [(300, 600), (900, 1000), (1600, 1800)]) == True, \"New segment contains the second segment\"\n", + "\n", + " print(\"\\033[92m All tests passed!\")\n", + " \n", + "is_overlapping_test(is_overlapping)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "scrolled": true + }, + "outputs": [], + "source": [ + "overlap1 = is_overlapping((950, 1430), [(2000, 2550), (260, 949)])\n", + "overlap2 = is_overlapping((2305, 2950), [(824, 1532), (1900, 2305), (3424, 3656)])\n", + "print(\"Overlap 1 = \", overlap1)\n", + "print(\"Overlap 2 = \", overlap2)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Expected Output**:\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
\n", + " **Overlap 1**\n", + " \n", + " False\n", + "
\n", + " **Overlap 2**\n", + " \n", + " True\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Insert audio clip\n", + "\n", + "* Let's use the previous helper functions to insert a new audio clip onto the 10 second background at a random time.\n", + "* We will ensure that any newly inserted segment doesn't overlap with previously inserted segments. \n", + "\n", + "\n", + "### Exercise 2 - insert_audio_clip\n", + "* Implement `insert_audio_clip()` to overlay an audio clip onto the background 10sec clip. \n", + "* You implement 4 steps:\n", + "\n", + "1. Get the length of the audio clip that is to be inserted.\n", + " * Get a random time segment whose duration equals the duration of the audio clip that is to be inserted.\n", + "2. Make sure that the time segment does not overlap with any of the previous time segments. \n", + " * If it is overlapping, then go back to step 1 and pick a new time segment.\n", + "3. Append the new time segment to the list of existing time segments\n", + " * This keeps track of all the segments you've inserted. \n", + "4. Overlay the audio clip over the background using pydub. We have implemented this for you." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# UNQ_C2\n", + "# GRADED FUNCTION: insert_audio_clip\n", + "\n", + "def insert_audio_clip(background, audio_clip, previous_segments):\n", + " \"\"\"\n", + " Insert a new audio segment over the background noise at a random time step, ensuring that the \n", + " audio segment does not overlap with existing segments.\n", + " \n", + " Arguments:\n", + " background -- a 10 second background audio recording. \n", + " audio_clip -- the audio clip to be inserted/overlaid. \n", + " previous_segments -- times where audio segments have already been placed\n", + " \n", + " Returns:\n", + " new_background -- the updated background audio\n", + " \"\"\"\n", + " \n", + " # Get the duration of the audio clip in ms\n", + " segment_ms = len(audio_clip)\n", + " \n", + " ### START CODE HERE ### \n", + " # Step 1: Use one of the helper functions to pick a random time segment onto which to insert \n", + " # the new audio clip. (≈ 1 line)\n", + " segment_time = None\n", + " \n", + " # Step 2: Check if the new segment_time overlaps with one of the previous_segments. If so, keep \n", + " # picking new segment_time at random until it doesn't overlap. To avoid an endless loop\n", + " # we retry 5 times(≈ 2 lines)\n", + " retry = 5 \n", + " while None and retry >= 0:\n", + " segment_time = None\n", + " retry = retry - 1\n", + " ### END CODE HERE ###\n", + " #print(segment_time)\n", + " # if last try is not overlaping, insert it to the background\n", + " if not is_overlapping(segment_time, previous_segments):\n", + " ### START CODE HERE ### \n", + " # Step 3: Append the new segment_time to the list of previous_segments (≈ 1 line)\n", + " None\n", + " ### END CODE HERE ###\n", + " # Step 4: Superpose audio segment and background\n", + " new_background = background.overlay(audio_clip, position = segment_time[0])\n", + " else:\n", + " #print(\"Timeouted\")\n", + " new_background = background\n", + " segment_time = (10000, 10000)\n", + " \n", + " return new_background, segment_time" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "deletable": false, + "editable": false + }, + "outputs": [], + "source": [ + "### THIS CELL IS NOT EDITABLE\n", + "\n", + "# UNIT TEST\n", + "def insert_audio_clip_test(target):\n", + " np.random.seed(5)\n", + " audio_clip, segment_time = target(backgrounds[0], activates[0], [(0, 4400)])\n", + " duration = segment_time[1] - segment_time[0]\n", + " #print(f\"xx: {segment_time}\")\n", + " assert segment_time[0] > 4400, \"Error: The audio clip is overlapping with the first segment\"\n", + " assert duration + 1 == len(activates[0]) , \"The segment length must match the audio clip length\"\n", + " assert audio_clip != backgrounds[0] , \"The audio clip must be different than the pure background\"\n", + " assert segment_time == (7286, 8201), f\"Wrong segment. Expected: (7286, 8201) got:{segment_time}\"\n", + " \n", + " # Not possible to insert clip into background\n", + " audio_clip, segment_time = target(backgrounds[0], activates[0], [(0, 9999)])\n", + " assert segment_time == (10000, 10000), \"Segment must match the out by max-retry mark\"\n", + " assert audio_clip == backgrounds[0], \"output audio clip must be exactly the same input background\"\n", + " \n", + " print(\"\\033[92m All tests passed!\")\n", + "\n", + "insert_audio_clip_test(insert_audio_clip)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "np.random.seed(5)\n", + "audio_clip, segment_time = insert_audio_clip(backgrounds[0], activates[0], [(3790, 4400)])\n", + "audio_clip.export(\"insert_test.wav\", format=\"wav\")\n", + "print(\"Segment Time: \", segment_time)\n", + "IPython.display.Audio(\"insert_test.wav\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Expected Output**\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + "
\n", + " **Segment Time**\n", + " \n", + " (2254, 3169)\n", + "
" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Expected audio\n", + "IPython.display.Audio(\"audio_examples/insert_reference.wav\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Insert ones for the labels of the positive target\n", + "\n", + "* Implement code to update the labels $y^{\\langle t \\rangle}$, assuming you just inserted an \"activate\" audio clip.\n", + "* In the code below, `y` is a `(1,1375)` dimensional vector, since $T_y = 1375$. \n", + "* If the \"activate\" audio clip ends at time step $t$, then set $y^{\\langle t+1 \\rangle} = 1$ and also set the next 49 additional consecutive values to 1.\n", + " * Notice that if the target word appears near the end of the entire audio clip, there may not be 50 additional time steps to set to 1.\n", + " * Make sure you don't run off the end of the array and try to update `y[0][1375]`, since the valid indices are `y[0][0]` through `y[0][1374]` because $T_y = 1375$. \n", + " * So if \"activate\" ends at step 1370, you would get only set `y[0][1371] = y[0][1372] = y[0][1373] = y[0][1374] = 1`\n", + "\n", + "\n", + "### Exercise 3 - insert_ones \n", + "Implement `insert_ones()`. \n", + "* You can use a for loop. \n", + "* If you want to use Python's array slicing operations, you can do so as well.\n", + "* If a segment ends at `segment_end_ms` (using a 10000 step discretization),\n", + " * To convert it to the indexing for the outputs $y$ (using a $1375$ step discretization), we will use this formula: \n", + "```\n", + " segment_end_y = int(segment_end_ms * Ty / 10000.0)\n", + "```" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# UNQ_C3\n", + "# GRADED FUNCTION: insert_ones\n", + "\n", + "def insert_ones(y, segment_end_ms):\n", + " \"\"\"\n", + " Update the label vector y. The labels of the 50 output steps strictly after the end of the segment \n", + " should be set to 1. By strictly we mean that the label of segment_end_y should be 0 while, the\n", + " 50 following labels should be ones.\n", + " \n", + " \n", + " Arguments:\n", + " y -- numpy array of shape (1, Ty), the labels of the training example\n", + " segment_end_ms -- the end time of the segment in ms\n", + " \n", + " Returns:\n", + " y -- updated labels\n", + " \"\"\"\n", + " _, Ty = y.shape\n", + " \n", + " # duration of the background (in terms of spectrogram time-steps)\n", + " segment_end_y = int(segment_end_ms * Ty / 10000.0)\n", + " \n", + " if segment_end_y < Ty:\n", + " # Add 1 to the correct index in the background label (y)\n", + " ### START CODE HERE ### (≈ 3 lines)\n", + " for i in range(None, None):\n", + " if None < None:\n", + " y[0, i] = None\n", + " ### END CODE HERE ###\n", + " \n", + " return y" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "deletable": false, + "editable": false + }, + "outputs": [], + "source": [ + "### THIS CELL IS NOT EDITABLE\n", + "\n", + "# UNIT TEST\n", + "import random\n", + "def insert_ones_test(target):\n", + " segment_end_y = random.randrange(0, Ty - 50) \n", + " segment_end_ms = int(segment_end_y * 10000.4) / Ty; \n", + " arr1 = target(np.zeros((1, Ty)), segment_end_ms)\n", + "\n", + " assert type(arr1) == np.ndarray, \"Wrong type. Output must be a numpy array\"\n", + " assert arr1.shape == (1, Ty), \"Wrong shape. It must match the input shape\"\n", + " assert np.sum(arr1) == 50, \"It must insert exactly 50 ones\"\n", + " assert arr1[0][segment_end_y - 1] == 0, f\"Array at {segment_end_y - 1} must be 0\"\n", + " assert arr1[0][segment_end_y] == 0, f\"Array at {segment_end_y} must be 0\"\n", + " assert arr1[0][segment_end_y + 1] == 1, f\"Array at {segment_end_y + 1} must be 1\"\n", + " assert arr1[0][segment_end_y + 50] == 1, f\"Array at {segment_end_y + 50} must be 1\"\n", + " assert arr1[0][segment_end_y + 51] == 0, f\"Array at {segment_end_y + 51} must be 0\"\n", + " \n", + " arr1 = target(np.zeros((1, Ty)), 9632)\n", + " assert np.sum(arr1) == 50, f\"Expected sum of 50, but got {np.sum(arr1)}\"\n", + " arr1 = target(np.zeros((1, Ty)), 9637)\n", + " assert np.sum(arr1) == 49, f\"Expected sum of 49, but got {np.sum(arr1)}\"\n", + " arr1 = target(np.zeros((1, Ty)), 10008)\n", + " assert np.sum(arr1) == 0, f\"Expected sum of 0, but got {np.sum(arr1)}\"\n", + " arr1 = target(np.zeros((1, Ty)), 10000)\n", + " assert np.sum(arr1) == 0, f\"Expected sum of 0, but got {np.sum(arr1)}\"\n", + " arr1 = target(np.zeros((1, Ty)), 9996)\n", + " assert np.sum(arr1) == 0, f\"Expected sum of 0, but got {np.sum(arr1)}\"\n", + " arr1 = target(np.zeros((1, Ty)), 9990)\n", + " assert np.sum(arr1) == 1, f\"Expected sum of 1, but got {np.sum(arr1)}\"\n", + " arr1 = target(np.zeros((1, Ty)), 9980)\n", + " assert np.sum(arr1) == 2, f\"Expected sum of 2, but got {np.sum(arr1)}\"\n", + "\n", + " print(\"\\033[92m All tests passed!\")\n", + " \n", + "insert_ones_test(insert_ones)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "arr1 = insert_ones(np.zeros((1, Ty)), 9700)\n", + "plt.plot(insert_ones(arr1, 4251)[0,:])\n", + "print(\"sanity checks:\", arr1[0][1333], arr1[0][634], arr1[0][635])" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Expected Output**\n", + "\n", + " \n", + " \n", + " \n", + " \n", + "
\n", + " **sanity checks**:\n", + " \n", + " 0.0 1.0 0.0\n", + "
\n", + "" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Creating a training example\n", + "Finally, you can use `insert_audio_clip` and `insert_ones` to create a new training example.\n", + "\n", + "\n", + "### Exercise 4 - create_training_example\n", + "\n", + "Implement `create_training_example()`. You will need to carry out the following steps:\n", + "\n", + "1. Initialize the label vector $y$ as a numpy array of zeros and shape $(1, T_y)$.\n", + "2. Initialize the set of existing segments to an empty list.\n", + "3. Randomly select 0 to 4 \"activate\" audio clips, and insert them onto the 10 second clip. Also insert labels at the correct position in the label vector $y$.\n", + "4. Randomly select 0 to 2 negative audio clips, and insert them into the 10 second clip. \n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# UNQ_C4\n", + "# GRADED FUNCTION: create_training_example\n", + "\n", + "def create_training_example(background, activates, negatives, Ty):\n", + " \"\"\"\n", + " Creates a training example with a given background, activates, and negatives.\n", + " \n", + " Arguments:\n", + " background -- a 10 second background audio recording\n", + " activates -- a list of audio segments of the word \"activate\"\n", + " negatives -- a list of audio segments of random words that are not \"activate\"\n", + " Ty -- The number of time steps in the output\n", + "\n", + " Returns:\n", + " x -- the spectrogram of the training example\n", + " y -- the label at each time step of the spectrogram\n", + " \"\"\"\n", + " \n", + " # Make background quieter\n", + " background = background - 20\n", + "\n", + " ### START CODE HERE ###\n", + " # Step 1: Initialize y (label vector) of zeros (≈ 1 line)\n", + " y = None\n", + "\n", + " # Step 2: Initialize segment times as empty list (≈ 1 line)\n", + " previous_segments = None\n", + " ### END CODE HERE ###\n", + " \n", + " # Select 0-4 random \"activate\" audio clips from the entire list of \"activates\" recordings\n", + " number_of_activates = np.random.randint(0, 5)\n", + " random_indices = np.random.randint(len(activates), size=number_of_activates)\n", + " random_activates = [activates[i] for i in random_indices]\n", + " \n", + " ### START CODE HERE ### (≈ 3 lines)\n", + " # Step 3: Loop over randomly selected \"activate\" clips and insert in background\n", + " for one_random_activate in random_activates:\n", + " # Insert the audio clip on the background\n", + " background, segment_time = None\n", + " # Retrieve segment_start and segment_end from segment_time\n", + " segment_start, segment_end = None\n", + " # Insert labels in \"y\" at segment_end\n", + " y = None\n", + " ### END CODE HERE ###\n", + "\n", + " # Select 0-2 random negatives audio recordings from the entire list of \"negatives\" recordings\n", + " number_of_negatives = np.random.randint(0, 3)\n", + " random_indices = np.random.randint(len(negatives), size=number_of_negatives)\n", + " random_negatives = [negatives[i] for i in random_indices]\n", + "\n", + " ### START CODE HERE ### (≈ 2 lines)\n", + " # Step 4: Loop over randomly selected negative clips and insert in background\n", + " for random_negative in random_negatives:\n", + " # Insert the audio clip on the background \n", + " background, _ = None\n", + " ### END CODE HERE ###\n", + " \n", + " # Standardize the volume of the audio clip \n", + " background = match_target_amplitude(background, -20.0)\n", + "\n", + " # Export new training example \n", + " file_handle = background.export(\"train\" + \".wav\", format=\"wav\")\n", + " \n", + " # Get and plot spectrogram of the new recording (background with superposition of positive and negatives)\n", + " x = graph_spectrogram(\"train.wav\")\n", + " \n", + " return x, y" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "deletable": false, + "editable": false + }, + "outputs": [], + "source": [ + "### THIS CELL IS NOT EDITABLE\n", + "\n", + "# UNIT TEST\n", + "def create_training_example_test(target):\n", + " np.random.seed(18)\n", + " x, y = target(backgrounds[0], activates, negatives, 1375)\n", + " \n", + " assert type(x) == np.ndarray, \"Wrong type for x\"\n", + " assert type(y) == np.ndarray, \"Wrong type for y\"\n", + " assert tuple(x.shape) == (101, 5511), \"Wrong shape for x\"\n", + " assert tuple(y.shape) == (1, 1375), \"Wrong shape for y\"\n", + " assert np.all(x > 0), \"All x values must be higher than 0\"\n", + " assert np.all(y >= 0), \"All y values must be higher or equal than 0\"\n", + " assert np.all(y <= 1), \"All y values must be smaller or equal than 1\"\n", + " assert np.sum(y) >= 50, \"It must contain at least one activate\"\n", + " assert np.sum(y) % 50 == 0, \"Sum of activate marks must be a multiple of 50\"\n", + " assert np.isclose(np.linalg.norm(x), 39745552.52075), \"Spectrogram is wrong. Check the parameters passed to the insert_audio_clip function\"\n", + "\n", + " print(\"\\033[92m All tests passed!\")\n", + "\n", + "create_training_example_test(create_training_example)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Set the random seed\n", + "np.random.seed(18)\n", + "x, y = create_training_example(backgrounds[0], activates, negatives, Ty)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Expected Output**\n", + "" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now you can listen to the training example you created and compare it to the spectrogram generated above." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "IPython.display.Audio(\"train.wav\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Expected Output**" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "IPython.display.Audio(\"audio_examples/train_reference.wav\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Finally, you can plot the associated labels for the generated training example." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "scrolled": true + }, + "outputs": [], + "source": [ + "plt.plot(y[0])" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Expected Output**\n", + "" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\n", + "### 1.4 - Full Training Set\n", + "\n", + "* You've now implemented the code needed to generate a single training example. \n", + "* We used this process to generate a large training set. \n", + "* To save time, we generate a smaller training set of 32 examples. " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# START SKIP FOR GRADING\n", + "np.random.seed(4543)\n", + "nsamples = 32\n", + "X = []\n", + "Y = []\n", + "for i in range(0, nsamples):\n", + " if i%10 == 0:\n", + " print(i)\n", + " x, y = create_training_example(backgrounds[i % 2], activates, negatives, Ty)\n", + " X.append(x.swapaxes(0,1))\n", + " Y.append(y.swapaxes(0,1))\n", + "X = np.array(X)\n", + "Y = np.array(Y)\n", + "# END SKIP FOR GRADING" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "You would like to save your dataset into a file that you can load later if you work in a more realistic environment. We let you the following code for reference. Don't try to run it into Coursera since the file system is read-only, and you cannot save files." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Save the data for further uses\n", + "# np.save(f'./XY_train/X.npy', X)\n", + "# np.save(f'./XY_train/Y.npy', Y)\n", + "# Load the preprocessed training examples\n", + "# X = np.load(\"./XY_train/X.npy\")\n", + "# Y = np.load(\"./XY_train/Y.npy\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\n", + "### 1.5 - Development Set\n", + "\n", + "* To test our model, we recorded a development set of 25 examples. \n", + "* While our training data is synthesized, we want to create a development set using the same distribution as the real inputs. \n", + "* Thus, we recorded 25 10-second audio clips of people saying \"activate\" and other random words, and labeled them by hand. \n", + "* This follows the principle described in Course 3 \"Structuring Machine Learning Projects\" that we should create the dev set to be as similar as possible to the test set distribution\n", + " * This is why our **dev set uses real audio** rather than synthesized audio. \n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Load preprocessed dev set examples\n", + "X_dev = np.load(\"./XY_dev/X_dev.npy\")\n", + "Y_dev = np.load(\"./XY_dev/Y_dev.npy\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\n", + "## 2 - The Model\n", + "\n", + "* Now that you've built a dataset, let's write and train a trigger word detection model! \n", + "* The model will use 1-D convolutional layers, GRU layers, and dense layers. \n", + "* Let's load the packages that will allow you to use these layers in Keras. This might take a minute to load. " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from tensorflow.keras.callbacks import ModelCheckpoint\n", + "from tensorflow.keras.models import Model, load_model, Sequential\n", + "from tensorflow.keras.layers import Dense, Activation, Dropout, Input, Masking, TimeDistributed, LSTM, Conv1D\n", + "from tensorflow.keras.layers import GRU, Bidirectional, BatchNormalization, Reshape\n", + "from tensorflow.keras.optimizers import Adam" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\n", + "### 2.1 - Build the Model\n", + "\n", + "Our goal is to build a network that will ingest a spectrogram and output a signal when it detects the trigger word. This network will use 4 layers:\n", + " * A convolutional layer\n", + " * Two GRU layers\n", + " * A dense layer. \n", + "\n", + "Here is the architecture we will use.\n", + "\n", + "\n", + "
**Figure 3**
\n", + "\n", + "##### 1D convolutional layer\n", + "One key layer of this model is the 1D convolutional step (near the bottom of Figure 3). \n", + "* It inputs the 5511 step spectrogram. Each step is a vector of 101 units.\n", + "* It outputs a 1375 step output\n", + "* This output is further processed by multiple layers to get the final $T_y = 1375$ step output. \n", + "* This 1D convolutional layer plays a role similar to the 2D convolutions you saw in Course 4, of extracting low-level features and then possibly generating an output of a smaller dimension. \n", + "* Computationally, the 1-D conv layer also helps speed up the model because now the GRU can process only 1375 timesteps rather than 5511 timesteps. \n", + "\n", + "##### GRU, dense and sigmoid\n", + "* The two GRU layers read the sequence of inputs from left to right.\n", + "* A dense plus sigmoid layer makes a prediction for $y^{\\langle t \\rangle}$. \n", + "* Because $y$ is a binary value (0 or 1), we use a sigmoid output at the last layer to estimate the chance of the output being 1, corresponding to the user having just said \"activate\".\n", + "\n", + "#### Unidirectional RNN\n", + "* Note that we use a **unidirectional RNN** rather than a bidirectional RNN. \n", + "* This is because we consider that focusing on past context is more critical than future context for detecting the activation word. This simplification reduces computational overhead, as the model does not process information in both directions. \n", + "* In a real application, trigger word detection could be performed every second using a sliding 10-second window. Therefore, even if the activation word could appear at any point within these 10 seconds, it can be detected immediately upon being spoken. If the algorithm initially fails to detect it, further detections with each subsequent second provide additional opportunities for recognition. This approach efficiently balances performance with timely detection attempts." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Implement the model\n", + "\n", + "In the following model, the input of each layer is the output of the previous one. Implementing the model can be done in four steps. \n", + "\n", + "**Step 1**: CONV layer. Use `Conv1D()` to implement this, with 196 filters, \n", + "a filter size of 15 (`kernel_size=15`), and stride of 4. [conv1d](https://keras.io/layers/convolutional/#conv1d)\n", + "\n", + "```Python\n", + "output_x = Conv1D(filters=...,kernel_size=...,strides=...)(input_x)\n", + "```\n", + "* Follow this with batch normalization. No parameters need to be set.\n", + "\n", + "```Python\n", + "output_x = BatchNormalization()(input_x)\n", + "```\n", + "* Follow this with a ReLu activation. Note that we can pass in the name of the desired activation as a string, all in lowercase letters.\n", + "\n", + "```Python\n", + "output_x = Activation(\"...\")(input_x)\n", + "```\n", + "\n", + "* Follow this with dropout, using a rate of 0.8 \n", + "\n", + "```Python\n", + "output_x = Dropout(rate=...)(input_x)\n", + "```\n", + "\n", + "\n", + "**Step 2**: First GRU layer. To generate the GRU layer, use 128 units.\n", + "```Python\n", + "output_x = GRU(units=..., return_sequences = ...)(input_x)\n", + "```\n", + "* Return sequences instead of just the last time step's prediction to ensure that all the GRU's hidden states are fed to the next layer. \n", + "* Follow this with dropout, using a rate of 0.8.\n", + "* Follow this with batch normalization. No parameters need to be set.\n", + "```Python\n", + "output_x = BatchNormalization()(input_x)\n", + "```\n", + "\n", + "**Step 3**: Second GRU layer. This has the same specifications as the first GRU layer.\n", + "* Follow this with a dropout, batch normalization, and then another dropout.\n", + "\n", + "**Step 4**: Create a time-distributed dense layer as follows: \n", + "```Python\n", + "output_x = TimeDistributed(Dense(1, activation = \"sigmoid\"))(input_x)\n", + "```\n", + "This creates a dense layer followed by a sigmoid, so that the parameters used for the dense layer are the same for every time step. \n", + "Documentation:\n", + "* [Keras documentation on wrappers](https://keras.io/layers/wrappers/). \n", + "* To learn more, you can read this blog post [How to Use the TimeDistributed Layer in Keras](https://machinelearningmastery.com/timedistributed-layer-for-long-short-term-memory-networks-in-python/).\n", + "\n", + "\n", + "### Exercise 5 - modelf\n", + "\n", + "Implement `modelf()`, the architecture is presented in Figure 3." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# UNQ_C5\n", + "# GRADED FUNCTION: modelf\n", + "\n", + "def modelf(input_shape):\n", + " \"\"\"\n", + " Function creating the model's graph in Keras.\n", + " \n", + " Argument:\n", + " input_shape -- shape of the model's input data (using Keras conventions)\n", + "\n", + " Returns:\n", + " model -- Keras model instance\n", + " \"\"\"\n", + " \n", + " X_input = Input(shape = input_shape)\n", + " \n", + " ### START CODE HERE ###\n", + " \n", + " # Step 1: CONV layer (≈4 lines)\n", + " # Add a Conv1D with 196 units, kernel size of 15 and stride of 4\n", + " X = None\n", + " # Batch normalization\n", + " X = None\n", + " # ReLu activation\n", + " X = None\n", + " # dropout (use 0.8)\n", + " X = None \n", + "\n", + " # Step 2: First GRU Layer (≈4 lines)\n", + " # GRU (use 128 units and return the sequences)\n", + " X = None\n", + " # dropout (use 0.8)\n", + " X = None\n", + " # Batch normalization.\n", + " X = None \n", + " \n", + " # Step 3: Second GRU Layer (≈4 lines)\n", + " # GRU (use 128 units and return the sequences)\n", + " X = None\n", + " # dropout (use 0.8)\n", + " X = None \n", + " # Batch normalization\n", + " X = None\n", + " # dropout (use 0.8)\n", + " X = None \n", + " \n", + " # Step 4: Time-distributed dense layer (≈1 line)\n", + " # TimeDistributed with sigmoid activation \n", + " X = None \n", + "\n", + " ### END CODE HERE ###\n", + "\n", + " model = Model(inputs = X_input, outputs = X)\n", + " \n", + " return model " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "deletable": false, + "editable": false + }, + "outputs": [], + "source": [ + "### THIS CELL IS NOT EDITABLE\n", + "\n", + "# UNIT TEST\n", + "from test_utils import *\n", + "\n", + "def modelf_test(target):\n", + " Tx = 5511\n", + " n_freq = 101\n", + " model = target(input_shape = (Tx, n_freq))\n", + " expected_model = [['InputLayer', [(None, 5511, 101)], 0],\n", + " ['Conv1D', (None, 1375, 196), 297136, 'valid', 'linear', (4,), (15,), 'GlorotUniform'],\n", + " ['BatchNormalization', (None, 1375, 196), 784],\n", + " ['Activation', (None, 1375, 196), 0],\n", + " ['Dropout', (None, 1375, 196), 0, 0.8],\n", + " ['GRU', (None, 1375, 128), 125184, True],\n", + " ['Dropout', (None, 1375, 128), 0, 0.8],\n", + " ['BatchNormalization', (None, 1375, 128), 512],\n", + " ['GRU', (None, 1375, 128), 99072, True],\n", + " ['Dropout', (None, 1375, 128), 0, 0.8],\n", + " ['BatchNormalization', (None, 1375, 128), 512],\n", + " ['Dropout', (None, 1375, 128), 0, 0.8],\n", + " ['TimeDistributed', (None, 1375, 1), 129, 'sigmoid']]\n", + " comparator(summary(model), expected_model)\n", + " \n", + " \n", + "modelf_test(modelf)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "model = modelf(input_shape = (Tx, n_freq))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Let's print the model summary to keep track of the shapes." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "model.summary()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Expected Output**:\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
\n", + " **Total params**\n", + " \n", + " 523,329\n", + "
\n", + " **Trainable params**\n", + " \n", + " 522,425\n", + "
\n", + " **Non-trainable params**\n", + " \n", + " 904\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The output of the network is of shape (None, 1375, 1) while the input is (None, 5511, 101). The Conv1D has reduced the number of steps from 5511 to 1375. " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\n", + "### 2.2 - Fit the Model" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "* Trigger word detection takes a long time to train. \n", + "* To save time, we've already trained a model for about 3 hours on a GPU using the architecture you built above, and a large training set of about 4000 examples. \n", + "* Let's load the model. " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from tensorflow.keras.models import model_from_json\n", + "\n", + "json_file = open('./models/model.json', 'r')\n", + "loaded_model_json = json_file.read()\n", + "json_file.close()\n", + "model = model_from_json(loaded_model_json)\n", + "model.load_weights('./models/model.h5')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\n", + "#### 2.2.1 Block Training for BatchNormalization Layers\n", + "\n", + "If you are going to fine-tune a pretrained model, it is important that you block the weights of all your batchnormalization layers. If you are going to train a new model from scratch, skip the next cell. " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "model.layers[2].trainable = False\n", + "model.layers[7].trainable = False\n", + "model.layers[10].trainable = False" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "You can train the model further, using the Adam optimizer and binary cross entropy loss, as follows. This will run quickly because we are training just for two epochs and with a small training set of 32 examples. " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "opt = Adam(lr=1e-6, beta_1=0.9, beta_2=0.999)\n", + "model.compile(loss='binary_crossentropy', optimizer=opt, metrics=[\"accuracy\"])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "model.fit(X, Y, batch_size = 16, epochs=1)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\n", + "### 2.3 - Test the Model\n", + "\n", + "Finally, let's see how your model performs on the dev set." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "loss, acc, = model.evaluate(X_dev, Y_dev)\n", + "print(\"Dev set accuracy = \", acc)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This looks pretty good! \n", + "* However, accuracy isn't a great metric for this task\n", + " * Since the labels are heavily skewed to 0's, a neural network that just outputs 0's would get slightly over 90% accuracy. \n", + "* We could define more useful metrics such as F1 score or Precision/Recall. \n", + " * Let's not bother with that here, and instead just empirically see how the model does with some predictions." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\n", + "## 3 - Making Predictions\n", + "\n", + "Now that you have built a working model for trigger word detection, let's use it to make predictions. This code snippet runs audio (saved in a wav file) through the network. \n", + "\n", + "" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def detect_triggerword(filename):\n", + " plt.subplot(2, 1, 1)\n", + " \n", + " # Correct the amplitude of the input file before prediction \n", + " audio_clip = AudioSegment.from_wav(filename)\n", + " audio_clip = match_target_amplitude(audio_clip, -20.0)\n", + " file_handle = audio_clip.export(\"tmp.wav\", format=\"wav\")\n", + " filename = \"tmp.wav\"\n", + "\n", + " x = graph_spectrogram(filename)\n", + " # the spectrogram outputs (freqs, Tx) and we want (Tx, freqs) to input into the model\n", + " x = x.swapaxes(0,1)\n", + " x = np.expand_dims(x, axis=0)\n", + " predictions = model.predict(x)\n", + " \n", + " plt.subplot(2, 1, 2)\n", + " plt.plot(predictions[0,:,0])\n", + " plt.ylabel('probability')\n", + " plt.show()\n", + " return predictions" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Insert a chime to acknowledge the \"activate\" trigger\n", + "* Once you've estimated the probability of having detected the word \"activate\" at each output step, you can trigger a \"chiming\" sound to play when the probability is above a certain threshold. \n", + "* $y^{\\langle t \\rangle}$ might be near 1 for many values in a row after \"activate\" is said, yet we want to chime only once. \n", + " * So we will insert a chime sound at most once every 75 output steps. \n", + " * This will help prevent us from inserting two chimes for a single instance of \"activate\". \n", + " * This plays a role similar to non-max suppression from computer vision.\n", + "\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "chime_file = \"audio_examples/chime.wav\"\n", + "def chime_on_activate(filename, predictions, threshold):\n", + " audio_clip = AudioSegment.from_wav(filename)\n", + " chime = AudioSegment.from_wav(chime_file)\n", + " Ty = predictions.shape[1]\n", + " # Step 1: Initialize the number of consecutive output steps to 0\n", + " consecutive_timesteps = 0\n", + " i = 0\n", + " # Step 2: Loop over the output steps in the y\n", + " while i < Ty:\n", + " # Step 3: Increment consecutive output steps\n", + " consecutive_timesteps += 1\n", + " # Step 4: If prediction is higher than the threshold for 20 consecutive output steps have passed\n", + " if consecutive_timesteps > 20:\n", + " # Step 5: Superpose audio and background using pydub\n", + " audio_clip = audio_clip.overlay(chime, position = ((i / Ty) * audio_clip.duration_seconds) * 1000)\n", + " # Step 6: Reset consecutive output steps to 0\n", + " consecutive_timesteps = 0\n", + " i = 75 * (i // 75 + 1)\n", + " continue\n", + " # if amplitude is smaller than the threshold reset the consecutive_timesteps counter\n", + " if predictions[0, i, 0] < threshold:\n", + " consecutive_timesteps = 0\n", + " i += 1\n", + " \n", + " audio_clip.export(\"chime_output.wav\", format='wav')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\n", + "### 3.1 - Test on Dev Examples" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Let's explore how our model performs on two unseen audio clips from the development set. Lets first listen to the two dev set clips. " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "IPython.display.Audio(\"./raw_data/dev/1.wav\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "IPython.display.Audio(\"./raw_data/dev/2.wav\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now lets run the model on these audio clips and see if it adds a chime after \"activate\"!" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "filename = \"./raw_data/dev/1.wav\"\n", + "prediction = detect_triggerword(filename)\n", + "chime_on_activate(filename, prediction, 0.5)\n", + "IPython.display.Audio(\"./chime_output.wav\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "filename = \"./raw_data/dev/2.wav\"\n", + "prediction = detect_triggerword(filename)\n", + "chime_on_activate(filename, prediction, 0.5)\n", + "IPython.display.Audio(\"./chime_output.wav\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Congratulations \n", + "\n", + "You've come to the end of this assignment! \n", + "\n", + "#### Here's what you should remember:\n", + "- Data synthesis is an effective way to create a large training set for speech problems, specifically trigger word detection. \n", + "- Using a spectrogram and optionally a 1D conv layer is a common pre-processing step prior to passing audio data to an RNN, GRU or LSTM.\n", + "- An end-to-end deep learning approach can be used to build a very effective trigger word detection system. \n", + "\n", + "*Congratulations* on finishing this assignment!" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\n", + "## 4 - Try Your Own Example! (OPTIONAL/UNGRADED)\n", + "\n", + "In this optional and ungraded portion of this notebook, you can try your model on your own audio clips! \n", + "\n", + "* Record a 10 second audio clip of you saying the word \"activate\" and other random words, and upload it to the Coursera hub as `myaudio.wav`. \n", + "* Be sure to upload the audio as a wav file. \n", + "* If your audio is recorded in a different format (such as mp3) there is free software that you can find online for converting it to wav. \n", + "* If your audio recording is not 10 seconds, the code below will either trim or pad it as needed to make it 10 seconds. " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Preprocess the audio to the correct format\n", + "def preprocess_audio(filename):\n", + " # Trim or pad audio segment to 10000ms\n", + " padding = AudioSegment.silent(duration=10000)\n", + " segment = AudioSegment.from_wav(filename)[:10000]\n", + " segment = padding.overlay(segment)\n", + " # Set frame rate to 44100\n", + " segment = segment.set_frame_rate(44100)\n", + " # Export as wav\n", + " segment.export(filename, format='wav')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Once you've uploaded your audio file to Coursera, put the path to your file in the variable below." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "your_filename = \"audio_examples/my_audio.wav\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "preprocess_audio(your_filename)\n", + "IPython.display.Audio(your_filename) # listen to the audio you uploaded " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Finally, use the model to predict when you say activate in the 10 second audio clip, and trigger a chime. If beeps are not being added appropriately, try to adjust the chime_threshold." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "chime_threshold = 0.5\n", + "prediction = detect_triggerword(your_filename)\n", + "chime_on_activate(your_filename, prediction, chime_threshold)\n", + "IPython.display.Audio(\"./chime_output.wav\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "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.7.6" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/Trigger_word_detection_v2a.ipynb b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/Trigger_word_detection_v2a.ipynb new file mode 100644 index 0000000000000000000000000000000000000000..a49bb61895d0719a612467cdfb7b39030642d39b --- /dev/null +++ b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/Trigger_word_detection_v2a.ipynb @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:0a2ac45381690397424fb0882b83213d6eeb8f5b4811fd29251f1195d0edec58 +size 29212735 diff --git a/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/XY_dev/X_dev.npy b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/XY_dev/X_dev.npy new file mode 100644 index 0000000000000000000000000000000000000000..6241f647f7d67baeefb4ad86780e345d58de7245 --- /dev/null +++ b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/XY_dev/X_dev.npy @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:976486c83889ec182da33f32d7ccc1f928ed893c8a2ee3384c7b2d8c45fe12e7 +size 111322328 diff --git a/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/XY_dev/Y_dev.npy b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/XY_dev/Y_dev.npy new file mode 100644 index 0000000000000000000000000000000000000000..77271cf758e432d4545f33cecc6b93bbba011a72 --- /dev/null +++ b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/XY_dev/Y_dev.npy @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:5055b824872ddf114a2317dc82d0f9a237b1cf9811aa8b0ddcde57a0ed96d814 +size 275128 diff --git a/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/XY_train/.DS_Store b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/XY_train/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..5008ddfcf53c02e82d7eee2e57c38e5672ef89f6 Binary files /dev/null and b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/XY_train/.DS_Store differ diff --git a/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/XY_train/X.npy b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/XY_train/X.npy new file mode 100644 index 0000000000000000000000000000000000000000..bc7eaf237ab4de740e8689d2b09a24fb3a8c78fe --- /dev/null +++ b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/XY_train/X.npy @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:05d21b8db3929bc2cdedec44e4036323b0547d8f1276c7b63a2554b3ed0537fb +size 142492544 diff --git a/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/XY_train/Y.npy b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/XY_train/Y.npy new file mode 100644 index 0000000000000000000000000000000000000000..eb48468993215aa207472c63eb99795a9acd5c84 --- /dev/null +++ b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/XY_train/Y.npy @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:65e87e1ca59628d5a6516eaf4d5d3355be93c0d81745c095600bebb6fed382c8 +size 352128 diff --git a/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/__pycache__/td_utils.cpython-37.pyc b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/__pycache__/td_utils.cpython-37.pyc new file mode 100644 index 0000000000000000000000000000000000000000..ca9e81291c681d36c9adbe3fe0ed8ec23fa39186 Binary files /dev/null and b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/__pycache__/td_utils.cpython-37.pyc differ diff --git a/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/__pycache__/test_utils.cpython-37.pyc b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/__pycache__/test_utils.cpython-37.pyc new file mode 100644 index 0000000000000000000000000000000000000000..8e89714aa91c8a6b9676022e570f5eec4edb5e86 Binary files /dev/null and b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/__pycache__/test_utils.cpython-37.pyc differ diff --git a/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/audio_examples/chime.wav b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/audio_examples/chime.wav new file mode 100644 index 0000000000000000000000000000000000000000..e965eb8730904ed1b0cb0c2a62f35e7e0a8a592c --- /dev/null +++ b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/audio_examples/chime.wav @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:92bd78b7795b26365b0cb1464b511d1abc382d535e58c9df332e866c0f97f87d +size 165476 diff --git a/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/audio_examples/example_train.wav b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/audio_examples/example_train.wav new file mode 100644 index 0000000000000000000000000000000000000000..59e5f4bdc1ca2a5b84092487cd111d7a5ece6201 --- /dev/null +++ b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/audio_examples/example_train.wav @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:1f05679dec5d8bf560a5914627c98c7ab6b59abcd71dda9de1daa7e7b1437afe +size 1764044 diff --git a/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/audio_examples/insert_reference.wav b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/audio_examples/insert_reference.wav new file mode 100644 index 0000000000000000000000000000000000000000..7e64f948a2e96d6c3018fdbd1eea8a8fe97382b1 --- /dev/null +++ b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/audio_examples/insert_reference.wav @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:03719961e6afa8302ca0283291c6881bab975e305ee90a7d33b676719b54a244 +size 1764044 diff --git a/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/audio_examples/my_audio.wav b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/audio_examples/my_audio.wav new file mode 100644 index 0000000000000000000000000000000000000000..09b9ab437bcf52330b6b02791bfd1b5364f82a5d --- /dev/null +++ b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/audio_examples/my_audio.wav @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:ebeca83419e48d23d92e56c014df63c4dc685ab5dc8c005c416672e99ad0b1e9 +size 1764044 diff --git a/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/audio_examples/train_reference.wav b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/audio_examples/train_reference.wav new file mode 100644 index 0000000000000000000000000000000000000000..6403f7e22d09efdefa90f85a2593ba26aa304a6e --- /dev/null +++ b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/audio_examples/train_reference.wav @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:4f5f7404f7ea18d9a89917d8fb6c696546438432cd5054d3cec43f6b2a7cf20f +size 1764044 diff --git a/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/chime_output.wav b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/chime_output.wav new file mode 100644 index 0000000000000000000000000000000000000000..7f319273d7e88e97c681ab56f9970b7f6dde971d --- /dev/null +++ b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/chime_output.wav @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:86ec3d6e23d0b39e59b311dc148d5fc96042699b17d27e8b3b4bba9100526af4 +size 1764044 diff --git a/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/generateTestCases.py b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/generateTestCases.py new file mode 100644 index 0000000000000000000000000000000000000000..e98f9baf3b066c9faa604a113528dec7a8f32d78 --- /dev/null +++ b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/generateTestCases.py @@ -0,0 +1,159 @@ +# New Generate Test Cases +import numpy as np +import math +import os,sys +sys.path.append('../') +os.environ['TF_CPP_MIN_LOG_LEVEL']='2' + +from td_utils import * +import warnings +warnings.filterwarnings("ignore") + +from pydub import AudioSegment + +class suppress_stdout_stderr(object): + ''' + A context manager for doing a "deep suppression" of stdout and stderr in + Python, i.e. will suppress all print, even if the print originates in a + compiled C/Fortran sub-function. + This will not suppress raised exceptions, since exceptions are printed + to stderr just before a script exits, and after the context manager has + exited (at least, I think that is why it lets exceptions through). + + ''' + def __init__(self): + # Open a pair of null files + self.null_fds = [os.open(os.devnull,os.O_RDWR) for x in range(2)] + # Save the actual stdout (1) and stderr (2) file descriptors. + self.save_fds = [os.dup(1), os.dup(2)] + + def __enter__(self): + # Assign the null pointers to stdout and stderr. + os.dup2(self.null_fds[0],1) + os.dup2(self.null_fds[1],2) + + def __exit__(self, *_): + # Re-assign the real stdout/stderr back to (1) and (2) + os.dup2(self.save_fds[0],1) + os.dup2(self.save_fds[1],2) + # Close all file descriptors + for fd in self.null_fds + self.save_fds: + os.close(fd) + +with suppress_stdout_stderr(): + from solutions import * + +# import copy +# from keras.callbacks import History +# import tensorflow as tf +sys.path.append('../../') + +from grader_support import stdout_redirector +from grader_support import util + + + + +# This grader is for the Emojify assignment + +mFiles = [ + "is_overlapping.py", + "insert_audio_clip.py", + "insert_ones.py", + "create_training_example.py", + "model.py" +] + +np.random.seed(3) + +# generating the testCases for is_overlapping +overlap1 = is_overlapping((900, 2500), [(2000, 2550), (260, 949)]) +overlap2 = is_overlapping((2306, 2307), [(824, 1532), (1900, 2305), (3424, 3656)]) + +# generating the test cases for the insert_audio_clip +# (2732, 3452), (4859, 5579) +a = AudioSegment.from_wav("activate.wav") +b = AudioSegment.from_wav("background.wav") +audio_clip, segment_time = insert_audio_clip(b, a, [(3790, 4400)]) +audio_clip.export('test.wav', format = 'wav') + +inserted = graph_spectrogram('test.wav') + +# generate the testCases for insert_ones +arr1 = insert_ones(np.zeros((1, Ty)), 9) + +# generate the test Cases for create_training_example + +n = AudioSegment.from_wav("negative.wav") + +A = [] +N = [] +A.append(a) +N.append(n) + +with stdout_redirector.stdout_redirected(): + a, s = create_training_example(b, A, N) + + +# generating the test cases for the model +with suppress_stdout_stderr(): + model = model(input_shape = (Tx, n_freq)) + ml = len(model.layers) + cp = model.count_params() + mi = len(model.inputs) + mo = len(model.outputs) + +def generateTestCases(): + testCases = { + 'is_overlapping': { + 'partId': 'S8DvY', + 'testCases': [ + { + 'testInput': ((900, 2500), [(2000, 2550), (260, 949)]), + 'testOutput': overlap1 + }, + { + 'testInput': ((2306, 2307), [(824, 1532), (1900, 2305), (3424, 3656)]), + 'testOutput': overlap2 + } + ] + }, + 'insert_audio_clip': { + 'partId': 'BSIWi', + 'testCases': [ + { + 'testInput': ("activate.wav", "background.wav"), + 'testOutput': inserted + } + ] + }, + 'insert_ones': { + 'partId': '2Kdnr', + 'testCases': [ + { + 'testInput': (np.zeros((1, Ty)), 9) , + 'testOutput': arr1 + } + ] + }, + 'create_training_example': { + 'partId': 'v097u', + 'testCases': [ + { + 'testInput': (b, A, N), + 'testOutput': (a,s) + } + ] + }, + 'model': { + 'partId': '0Txcd', + 'testCases': [ + { + 'testInput': (Tx, n_freq), + 'testOutput': np.asarray([cp, ml, mi, mo]) + } + ] + } + } + return testCases + diff --git a/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/images/label_diagram.png b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/images/label_diagram.png new file mode 100644 index 0000000000000000000000000000000000000000..bac602915c236da656ff7da173a5a3c60e161036 Binary files /dev/null and b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/images/label_diagram.png differ diff --git a/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/images/model.png b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/images/model.png new file mode 100644 index 0000000000000000000000000000000000000000..39cca00e71a1997f5d81242e66748a78d5d32278 --- /dev/null +++ b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/images/model.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:a3cdeeb86bddb27b101aedec2e79fdeb3e37a5732962ea6e2d3cce12b37a6946 +size 232146 diff --git a/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/images/ones_reference.png b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/images/ones_reference.png new file mode 100644 index 0000000000000000000000000000000000000000..e78da169aa2e5341b564c81680d3dcc4254580c5 Binary files /dev/null and b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/images/ones_reference.png differ diff --git a/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/images/sound.png b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/images/sound.png new file mode 100644 index 0000000000000000000000000000000000000000..8763a09afac705ae1c19805a4775b835e4f83554 --- /dev/null +++ b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/images/sound.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:49d883090e8db70d6789c0dd6a40fa04ee7144bb6feb2e6c3592ad589d7227d1 +size 201217 diff --git a/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/images/spectrogram.png b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/images/spectrogram.png new file mode 100644 index 0000000000000000000000000000000000000000..9402f5562a568b3515fb02c2a053d5203d543aac Binary files /dev/null and b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/images/spectrogram.png differ diff --git a/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/images/train_label.png b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/images/train_label.png new file mode 100644 index 0000000000000000000000000000000000000000..7210dbd7f420dcde5572db00fad60217dd8d1541 Binary files /dev/null and b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/images/train_label.png differ diff --git a/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/images/train_reference.png b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/images/train_reference.png new file mode 100644 index 0000000000000000000000000000000000000000..352dcf2d63a860e5c46e1342ff471b95938a9df6 --- /dev/null +++ b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/images/train_reference.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:7f1201d5879840450f28e3310102d059139ce17f5e7103a80d3e7f1d38216e7e +size 349804 diff --git a/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/insert_test.wav b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/insert_test.wav new file mode 100644 index 0000000000000000000000000000000000000000..7e64f948a2e96d6c3018fdbd1eea8a8fe97382b1 --- /dev/null +++ b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/insert_test.wav @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:03719961e6afa8302ca0283291c6881bab975e305ee90a7d33b676719b54a244 +size 1764044 diff --git a/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/models/.DS_Store b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/models/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..5008ddfcf53c02e82d7eee2e57c38e5672ef89f6 Binary files /dev/null and b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/models/.DS_Store differ diff --git a/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/models/model.h5 b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/models/model.h5 new file mode 100644 index 0000000000000000000000000000000000000000..037bd74b00c9d8e8d7f6b33c4ad736186f458d45 --- /dev/null +++ b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/models/model.h5 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:22f4a153c9e50f73db60df7fc67bce93698b31594a2383a41011eab4ea9e8ef5 +size 2130896 diff --git a/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/models/model.json b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/models/model.json new file mode 100644 index 0000000000000000000000000000000000000000..5023a3854c30a74fd5b0d16f70f69d134174e416 --- /dev/null +++ b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/models/model.json @@ -0,0 +1 @@ +{"class_name": "Functional", "config": {"name": "model", "layers": [{"class_name": "InputLayer", "config": {"batch_input_shape": [null, 5511, 101], "dtype": "float32", "sparse": false, "ragged": false, "name": "input_1"}, "name": "input_1", "inbound_nodes": []}, {"class_name": "Conv1D", "config": {"name": "conv1d", "trainable": true, "dtype": "float32", "filters": 196, "kernel_size": [15], "strides": [4], "padding": "valid", "data_format": "channels_last", "dilation_rate": [1], "groups": 1, "activation": "linear", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}}, "bias_initializer": {"class_name": "Zeros", "config": {}}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}, "name": "conv1d", "inbound_nodes": [[["input_1", 0, 0, {}]]]}, {"class_name": "BatchNormalization", "config": {"name": "batch_normalization", "trainable": false, "dtype": "float32", "axis": [2], "momentum": 0.99, "epsilon": 0.001, "center": true, "scale": true, "beta_initializer": {"class_name": "Zeros", "config": {}}, "gamma_initializer": {"class_name": "Ones", "config": {}}, "moving_mean_initializer": {"class_name": "Zeros", "config": {}}, "moving_variance_initializer": {"class_name": "Ones", "config": {}}, "beta_regularizer": null, "gamma_regularizer": null, "beta_constraint": null, "gamma_constraint": null}, "name": "batch_normalization", "inbound_nodes": [[["conv1d", 0, 0, {}]]]}, {"class_name": "Activation", "config": {"name": "activation", "trainable": true, "dtype": "float32", "activation": "relu"}, "name": "activation", "inbound_nodes": [[["batch_normalization", 0, 0, {}]]]}, {"class_name": "Dropout", "config": {"name": "dropout", "trainable": true, "dtype": "float32", "rate": 0.8, "noise_shape": null, "seed": null}, "name": "dropout", "inbound_nodes": [[["activation", 0, 0, {}]]]}, {"class_name": "GRU", "config": {"name": "gru", "trainable": true, "dtype": "float32", "return_sequences": true, "return_state": false, "go_backwards": false, "stateful": false, "unroll": false, "time_major": false, "units": 128, "activation": "tanh", "recurrent_activation": "sigmoid", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}}, "recurrent_initializer": {"class_name": "Orthogonal", "config": {"gain": 1.0, "seed": null}}, "bias_initializer": {"class_name": "Zeros", "config": {}}, "kernel_regularizer": null, "recurrent_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "recurrent_constraint": null, "bias_constraint": null, "dropout": 0.0, "recurrent_dropout": 0.0, "implementation": 2, "reset_after": true}, "name": "gru", "inbound_nodes": [[["dropout", 0, 0, {}]]]}, {"class_name": "Dropout", "config": {"name": "dropout_1", "trainable": true, "dtype": "float32", "rate": 0.8, "noise_shape": null, "seed": null}, "name": "dropout_1", "inbound_nodes": [[["gru", 0, 0, {}]]]}, {"class_name": "BatchNormalization", "config": {"name": "batch_normalization_1", "trainable": false, "dtype": "float32", "axis": [2], "momentum": 0.99, "epsilon": 0.001, "center": true, "scale": true, "beta_initializer": {"class_name": "Zeros", "config": {}}, "gamma_initializer": {"class_name": "Ones", "config": {}}, "moving_mean_initializer": {"class_name": "Zeros", "config": {}}, "moving_variance_initializer": {"class_name": "Ones", "config": {}}, "beta_regularizer": null, "gamma_regularizer": null, "beta_constraint": null, "gamma_constraint": null}, "name": "batch_normalization_1", "inbound_nodes": [[["dropout_1", 0, 0, {}]]]}, {"class_name": "GRU", "config": {"name": "gru_1", "trainable": true, "dtype": "float32", "return_sequences": true, "return_state": false, "go_backwards": false, "stateful": false, "unroll": false, "time_major": false, "units": 128, "activation": "tanh", "recurrent_activation": "sigmoid", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}}, "recurrent_initializer": {"class_name": "Orthogonal", "config": {"gain": 1.0, "seed": null}}, "bias_initializer": {"class_name": "Zeros", "config": {}}, "kernel_regularizer": null, "recurrent_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "recurrent_constraint": null, "bias_constraint": null, "dropout": 0.0, "recurrent_dropout": 0.0, "implementation": 2, "reset_after": true}, "name": "gru_1", "inbound_nodes": [[["batch_normalization_1", 0, 0, {}]]]}, {"class_name": "Dropout", "config": {"name": "dropout_2", "trainable": true, "dtype": "float32", "rate": 0.8, "noise_shape": null, "seed": null}, "name": "dropout_2", "inbound_nodes": [[["gru_1", 0, 0, {}]]]}, {"class_name": "BatchNormalization", "config": {"name": "batch_normalization_2", "trainable": false, "dtype": "float32", "axis": [2], "momentum": 0.99, "epsilon": 0.001, "center": true, "scale": true, "beta_initializer": {"class_name": "Zeros", "config": {}}, "gamma_initializer": {"class_name": "Ones", "config": {}}, "moving_mean_initializer": {"class_name": "Zeros", "config": {}}, "moving_variance_initializer": {"class_name": "Ones", "config": {}}, "beta_regularizer": null, "gamma_regularizer": null, "beta_constraint": null, "gamma_constraint": null}, "name": "batch_normalization_2", "inbound_nodes": [[["dropout_2", 0, 0, {}]]]}, {"class_name": "Dropout", "config": {"name": "dropout_3", "trainable": true, "dtype": "float32", "rate": 0.8, "noise_shape": null, "seed": null}, "name": "dropout_3", "inbound_nodes": [[["batch_normalization_2", 0, 0, {}]]]}, {"class_name": "TimeDistributed", "config": {"name": "time_distributed", "trainable": true, "dtype": "float32", "layer": {"class_name": "Dense", "config": {"name": "dense", "trainable": true, "dtype": "float32", "units": 1, "activation": "sigmoid", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}}, "bias_initializer": {"class_name": "Zeros", "config": {}}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}}}, "name": "time_distributed", "inbound_nodes": [[["dropout_3", 0, 0, {}]]]}], "input_layers": [["input_1", 0, 0]], "output_layers": [["time_distributed", 0, 0]]}, "keras_version": "2.4.0", "backend": "tensorflow"} \ No newline at end of file diff --git a/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/models/model_new8.h5 b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/models/model_new8.h5 new file mode 100644 index 0000000000000000000000000000000000000000..a3ba9a7e1f921cc508c0b267ed292bc1a6bcfb7b --- /dev/null +++ b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/models/model_new8.h5 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:92dc387f9230849d6707c73ab9a6a4dd060c8d79fea07f5b7a853ebfca1d798b +size 2130888 diff --git a/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/models/model_new8.json b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/models/model_new8.json new file mode 100644 index 0000000000000000000000000000000000000000..05f9354f691bd805cfeef439ab4ce9828e77878b --- /dev/null +++ b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/models/model_new8.json @@ -0,0 +1 @@ +{"class_name": "Functional", "config": {"name": "model", "layers": [{"class_name": "InputLayer", "config": {"batch_input_shape": [null, 5511, 101], "dtype": "float32", "sparse": false, "ragged": false, "name": "input_1"}, "name": "input_1", "inbound_nodes": []}, {"class_name": "Conv1D", "config": {"name": "conv1d", "trainable": true, "dtype": "float32", "filters": 196, "kernel_size": [15], "strides": [4], "padding": "valid", "data_format": "channels_last", "dilation_rate": [1], "groups": 1, "activation": "linear", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}}, "bias_initializer": {"class_name": "Zeros", "config": {}}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}, "name": "conv1d", "inbound_nodes": [[["input_1", 0, 0, {}]]]}, {"class_name": "BatchNormalization", "config": {"name": "batch_normalization", "trainable": true, "dtype": "float32", "axis": [2], "momentum": 0.99, "epsilon": 0.001, "center": true, "scale": true, "beta_initializer": {"class_name": "Zeros", "config": {}}, "gamma_initializer": {"class_name": "Ones", "config": {}}, "moving_mean_initializer": {"class_name": "Zeros", "config": {}}, "moving_variance_initializer": {"class_name": "Ones", "config": {}}, "beta_regularizer": null, "gamma_regularizer": null, "beta_constraint": null, "gamma_constraint": null}, "name": "batch_normalization", "inbound_nodes": [[["conv1d", 0, 0, {}]]]}, {"class_name": "Activation", "config": {"name": "activation", "trainable": true, "dtype": "float32", "activation": "relu"}, "name": "activation", "inbound_nodes": [[["batch_normalization", 0, 0, {}]]]}, {"class_name": "Dropout", "config": {"name": "dropout", "trainable": true, "dtype": "float32", "rate": 0.8, "noise_shape": null, "seed": null}, "name": "dropout", "inbound_nodes": [[["activation", 0, 0, {}]]]}, {"class_name": "GRU", "config": {"name": "gru", "trainable": true, "dtype": "float32", "return_sequences": true, "return_state": false, "go_backwards": false, "stateful": false, "unroll": false, "time_major": false, "units": 128, "activation": "tanh", "recurrent_activation": "sigmoid", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}}, "recurrent_initializer": {"class_name": "Orthogonal", "config": {"gain": 1.0, "seed": null}}, "bias_initializer": {"class_name": "Zeros", "config": {}}, "kernel_regularizer": null, "recurrent_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "recurrent_constraint": null, "bias_constraint": null, "dropout": 0.0, "recurrent_dropout": 0.0, "implementation": 2, "reset_after": true}, "name": "gru", "inbound_nodes": [[["dropout", 0, 0, {}]]]}, {"class_name": "Dropout", "config": {"name": "dropout_1", "trainable": true, "dtype": "float32", "rate": 0.8, "noise_shape": null, "seed": null}, "name": "dropout_1", "inbound_nodes": [[["gru", 0, 0, {}]]]}, {"class_name": "BatchNormalization", "config": {"name": "batch_normalization_1", "trainable": true, "dtype": "float32", "axis": [2], "momentum": 0.99, "epsilon": 0.001, "center": true, "scale": true, "beta_initializer": {"class_name": "Zeros", "config": {}}, "gamma_initializer": {"class_name": "Ones", "config": {}}, "moving_mean_initializer": {"class_name": "Zeros", "config": {}}, "moving_variance_initializer": {"class_name": "Ones", "config": {}}, "beta_regularizer": null, "gamma_regularizer": null, "beta_constraint": null, "gamma_constraint": null}, "name": "batch_normalization_1", "inbound_nodes": [[["dropout_1", 0, 0, {}]]]}, {"class_name": "GRU", "config": {"name": "gru_1", "trainable": true, "dtype": "float32", "return_sequences": true, "return_state": false, "go_backwards": false, "stateful": false, "unroll": false, "time_major": false, "units": 128, "activation": "tanh", "recurrent_activation": "sigmoid", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}}, "recurrent_initializer": {"class_name": "Orthogonal", "config": {"gain": 1.0, "seed": null}}, "bias_initializer": {"class_name": "Zeros", "config": {}}, "kernel_regularizer": null, "recurrent_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "recurrent_constraint": null, "bias_constraint": null, "dropout": 0.0, "recurrent_dropout": 0.0, "implementation": 2, "reset_after": true}, "name": "gru_1", "inbound_nodes": [[["batch_normalization_1", 0, 0, {}]]]}, {"class_name": "Dropout", "config": {"name": "dropout_2", "trainable": true, "dtype": "float32", "rate": 0.8, "noise_shape": null, "seed": null}, "name": "dropout_2", "inbound_nodes": [[["gru_1", 0, 0, {}]]]}, {"class_name": "BatchNormalization", "config": {"name": "batch_normalization_2", "trainable": true, "dtype": "float32", "axis": [2], "momentum": 0.99, "epsilon": 0.001, "center": true, "scale": true, "beta_initializer": {"class_name": "Zeros", "config": {}}, "gamma_initializer": {"class_name": "Ones", "config": {}}, "moving_mean_initializer": {"class_name": "Zeros", "config": {}}, "moving_variance_initializer": {"class_name": "Ones", "config": {}}, "beta_regularizer": null, "gamma_regularizer": null, "beta_constraint": null, "gamma_constraint": null}, "name": "batch_normalization_2", "inbound_nodes": [[["dropout_2", 0, 0, {}]]]}, {"class_name": "Dropout", "config": {"name": "dropout_3", "trainable": true, "dtype": "float32", "rate": 0.8, "noise_shape": null, "seed": null}, "name": "dropout_3", "inbound_nodes": [[["batch_normalization_2", 0, 0, {}]]]}, {"class_name": "TimeDistributed", "config": {"name": "time_distributed", "trainable": true, "dtype": "float32", "layer": {"class_name": "Dense", "config": {"name": "dense", "trainable": true, "dtype": "float32", "units": 1, "activation": "sigmoid", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}}, "bias_initializer": {"class_name": "Zeros", "config": {}}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}}}, "name": "time_distributed", "inbound_nodes": [[["dropout_3", 0, 0, {}]]]}], "input_layers": [["input_1", 0, 0]], "output_layers": [["time_distributed", 0, 0]]}, "keras_version": "2.4.0", "backend": "tensorflow"} \ No newline at end of file diff --git a/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/models/tr_model.h5 b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/models/tr_model.h5 new file mode 100644 index 0000000000000000000000000000000000000000..03d92b2146d9310f46f07a440034aa0c5a592118 --- /dev/null +++ b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/models/tr_model.h5 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:e1fc69bf375bbe0a3ed64f8df6766d4aff61aa2fc9a6b48959f88a7b5b6fd792 +size 7687264 diff --git a/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/raw_data/.DS_Store b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/raw_data/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..a1be1e8fe65c6d30a25e3a9419bbba6b7f659b6c Binary files /dev/null and b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/raw_data/.DS_Store differ diff --git a/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/raw_data/activates/1.wav b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/raw_data/activates/1.wav new file mode 100644 index 0000000000000000000000000000000000000000..4ad2f54d9fce3503d43924bca47162c19bb68bb0 --- /dev/null +++ b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/raw_data/activates/1.wav @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:0837e165424b2ee6868aff35019c266fcfb73066a55a132f09f20a0baa130445 +size 127148 diff --git a/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/raw_data/activates/1_act2.wav b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/raw_data/activates/1_act2.wav new file mode 100644 index 0000000000000000000000000000000000000000..b52b630e5a91c5b910221c373fd9526c04caab16 Binary files /dev/null and b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/raw_data/activates/1_act2.wav differ diff --git a/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/raw_data/activates/1_act3.wav b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/raw_data/activates/1_act3.wav new file mode 100644 index 0000000000000000000000000000000000000000..574bbff7dd38e0a8cea646c73fbff5004e98829d --- /dev/null +++ b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/raw_data/activates/1_act3.wav @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:4087f9e8d33157ec3d260bbe36b6f5cede6ed4db851f46a97398cad2f6e25bcc +size 307244 diff --git a/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/raw_data/activates/2.wav b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/raw_data/activates/2.wav new file mode 100644 index 0000000000000000000000000000000000000000..ed4fb834f55159560f3813def8423028bdd52bcb --- /dev/null +++ b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/raw_data/activates/2.wav @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:28e524a403d45080ca88ad3b31eb8729b916d9e56f7b95df751eb34778b30abb +size 160336 diff --git a/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/raw_data/activates/2_act2.wav b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/raw_data/activates/2_act2.wav new file mode 100644 index 0000000000000000000000000000000000000000..16987329221a56906ccecafe4ac560b84386b384 Binary files /dev/null and b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/raw_data/activates/2_act2.wav differ diff --git a/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/raw_data/activates/2_act3.wav b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/raw_data/activates/2_act3.wav new file mode 100644 index 0000000000000000000000000000000000000000..1f74bad12bd32b0ea1cc2a63df1bdac4a6be6775 --- /dev/null +++ b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/raw_data/activates/2_act3.wav @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:7539f5966d7a4db2be94752600c2cfc6dc3c97d4a7d8c298714d4cd759ce60f8 +size 278572 diff --git a/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/raw_data/activates/3.wav b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/raw_data/activates/3.wav new file mode 100644 index 0000000000000000000000000000000000000000..6b7af44e099c4d4933df9175d13b28c2f70355b6 --- /dev/null +++ b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/raw_data/activates/3.wav @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:a17469a412d5d8b113cc9c2442fc336da4fc8d2bd1a19e8bc7325d16b9099e56 +size 117856 diff --git a/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/raw_data/activates/3_act2.wav b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/raw_data/activates/3_act2.wav new file mode 100644 index 0000000000000000000000000000000000000000..e8e7ff4134b4dd84fa2695276e8f999e013b30e0 Binary files /dev/null and b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/raw_data/activates/3_act2.wav differ diff --git a/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/raw_data/activates/3_act3.wav b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/raw_data/activates/3_act3.wav new file mode 100644 index 0000000000000000000000000000000000000000..694e26ebe8764b2a15228e2d5dc797db571ab89c --- /dev/null +++ b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/raw_data/activates/3_act3.wav @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:33a7db1f092ff91824bf0884ce8c868539574ec8187cde5c0f8064f9fefb6b2b +size 421932 diff --git a/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/raw_data/activates/4_act2.wav b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/raw_data/activates/4_act2.wav new file mode 100644 index 0000000000000000000000000000000000000000..adfd11ae77c397cc7da317a730ea9c294aa2e950 Binary files /dev/null and b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/raw_data/activates/4_act2.wav differ diff --git a/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/raw_data/backgrounds/1.wav b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/raw_data/backgrounds/1.wav new file mode 100644 index 0000000000000000000000000000000000000000..61dd12a741a32531030fcf6786d59a6a5380db1a --- /dev/null +++ b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/raw_data/backgrounds/1.wav @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:300a3d49a912c53828163f8040b137bbbcd12e8d43c47faca27d048d239a3dfe +size 1764044 diff --git a/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/raw_data/backgrounds/2.wav b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/raw_data/backgrounds/2.wav new file mode 100644 index 0000000000000000000000000000000000000000..df9b9031d07e5a3f33d99953fb36c77c683fa5ad --- /dev/null +++ b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/raw_data/backgrounds/2.wav @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:69a9f50b4d3f47bc10952049eb2f9730b8bdfd2376e44d6266b2b80ea57ed8ea +size 1764044 diff --git a/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/raw_data/dev/1.wav b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/raw_data/dev/1.wav new file mode 100644 index 0000000000000000000000000000000000000000..d03289f4c87552d3a7f6710a69f5f9d9fe55f6fa --- /dev/null +++ b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/raw_data/dev/1.wav @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:55eb57c10eb0179e3cc08dc52bc9181c0908b659161aa310be74e5b94c894fda +size 1764044 diff --git a/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/raw_data/dev/2.wav b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/raw_data/dev/2.wav new file mode 100644 index 0000000000000000000000000000000000000000..11a62acf59e52c40cf1374530f6eebd5fbdbd3df --- /dev/null +++ b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/raw_data/dev/2.wav @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:3947adc899c86618e9d6f22010c2c7891ca93ed775a29a7b2d237d00a1701cba +size 1764044 diff --git a/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/raw_data/negatives/1.wav b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/raw_data/negatives/1.wav new file mode 100644 index 0000000000000000000000000000000000000000..75349883aca37be71195db4c62f5ba4897915ed4 Binary files /dev/null and b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/raw_data/negatives/1.wav differ diff --git a/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/raw_data/negatives/1_0.wav b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/raw_data/negatives/1_0.wav new file mode 100644 index 0000000000000000000000000000000000000000..3824cccad9bc6e730a925ad1a1fa9210dffb4b23 --- /dev/null +++ b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/raw_data/negatives/1_0.wav @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:df7e9713ce93df12570bc87b75a6a92c0f63426a6cc4d96ef6e2e7fe0e5aeb69 +size 102176 diff --git a/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/raw_data/negatives/2.wav b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/raw_data/negatives/2.wav new file mode 100644 index 0000000000000000000000000000000000000000..3762c19171c9a710bb121df978e08e23a3fdb30e Binary files /dev/null and b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/raw_data/negatives/2.wav differ diff --git a/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/raw_data/negatives/2_1.wav b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/raw_data/negatives/2_1.wav new file mode 100644 index 0000000000000000000000000000000000000000..544d88f92e46519d7bf89119f7271d875fbaf3aa Binary files /dev/null and b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/raw_data/negatives/2_1.wav differ diff --git a/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/raw_data/negatives/3.wav b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/raw_data/negatives/3.wav new file mode 100644 index 0000000000000000000000000000000000000000..662d9dae9c744c82155a1cb92f65b5344c85895e --- /dev/null +++ b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/raw_data/negatives/3.wav @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:2474e6e0ddf194c668c24ecad310e3f599fde144a2193bd7adc6157d255e1f40 +size 115588 diff --git a/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/raw_data/negatives/3_2.wav b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/raw_data/negatives/3_2.wav new file mode 100644 index 0000000000000000000000000000000000000000..a1aa045dbd432c47e9abcbd38709f46681bd6eec Binary files /dev/null and b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/raw_data/negatives/3_2.wav differ diff --git a/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/raw_data/negatives/4.wav b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/raw_data/negatives/4.wav new file mode 100644 index 0000000000000000000000000000000000000000..7a3b4268c7bbce4bba921df9b4926977a72b64cf Binary files /dev/null and b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/raw_data/negatives/4.wav differ diff --git a/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/raw_data/negatives/4_0.wav b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/raw_data/negatives/4_0.wav new file mode 100644 index 0000000000000000000000000000000000000000..20deb2bf2e90dbeee3cbdc731f3aaebe1d6fd9bf --- /dev/null +++ b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/raw_data/negatives/4_0.wav @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:869b76c9d55455c5b98cb4262d535493f00c72c6b8b74ec7b85051be1231df29 +size 235892 diff --git a/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/raw_data/negatives/5.wav b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/raw_data/negatives/5.wav new file mode 100644 index 0000000000000000000000000000000000000000..c1a888a3b25ab9a27d10330c06514429eb206c71 Binary files /dev/null and b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/raw_data/negatives/5.wav differ diff --git a/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/raw_data/negatives/5_1.wav b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/raw_data/negatives/5_1.wav new file mode 100644 index 0000000000000000000000000000000000000000..c28fc812fc4ea36d22fba12fa0cef0e0db54b1dc --- /dev/null +++ b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/raw_data/negatives/5_1.wav @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:b742d61948769ae5cc5ec2382f0533ea0579858fadc45ee3b435b23a8a96fb54 +size 105884 diff --git a/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/td_utils.py b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/td_utils.py new file mode 100644 index 0000000000000000000000000000000000000000..7754ae111effd5cd5e555808d456fb949df2240e --- /dev/null +++ b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/td_utils.py @@ -0,0 +1,46 @@ +import matplotlib.pyplot as plt +from scipy.io import wavfile +import os +from pydub import AudioSegment + +# Calculate and plot spectrogram for a wav audio file +def graph_spectrogram(wav_file): + rate, data = get_wav_info(wav_file) + nfft = 200 # Length of each window segment + fs = 8000 # Sampling frequencies + noverlap = 120 # Overlap between windows + nchannels = data.ndim + if nchannels == 1: + pxx, freqs, bins, im = plt.specgram(data, nfft, fs, noverlap = noverlap) + elif nchannels == 2: + pxx, freqs, bins, im = plt.specgram(data[:,0], nfft, fs, noverlap = noverlap) + return pxx + +# Load a wav file +def get_wav_info(wav_file): + rate, data = wavfile.read(wav_file) + return rate, data + +# Used to standardize volume of audio clip +def match_target_amplitude(sound, target_dBFS): + change_in_dBFS = target_dBFS - sound.dBFS + return sound.apply_gain(change_in_dBFS) + +# Load raw audio files for speech synthesis +def load_raw_audio(path): + activates = [] + backgrounds = [] + negatives = [] + for filename in os.listdir(path + "activates"): + if filename.endswith("wav"): + activate = AudioSegment.from_wav(path + "activates/" + filename) + activates.append(activate) + for filename in os.listdir(path + "backgrounds"): + if filename.endswith("wav"): + background = AudioSegment.from_wav(path + "backgrounds/" + filename) + backgrounds.append(background) + for filename in os.listdir(path + "negatives"): + if filename.endswith("wav"): + negative = AudioSegment.from_wav(path + "negatives/" + filename) + negatives.append(negative) + return activates, negatives, backgrounds \ No newline at end of file diff --git a/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/test_utils.py b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/test_utils.py new file mode 100644 index 0000000000000000000000000000000000000000..d1fba735acef900327e7fc4e4631ad6139295f2c --- /dev/null +++ b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/test_utils.py @@ -0,0 +1,82 @@ +from termcolor import colored + +from tensorflow.keras.layers import Input +from tensorflow.keras.layers import Conv2D +from tensorflow.keras.layers import MaxPooling2D +from tensorflow.keras.layers import Dropout +from tensorflow.keras.layers import Conv2DTranspose +from tensorflow.keras.layers import concatenate +from tensorflow.keras.layers import ZeroPadding2D +from tensorflow.keras.layers import Dense +from tensorflow.keras.layers import LSTM +from tensorflow.keras.layers import RepeatVector +from tensorflow.keras.layers import TimeDistributed +from tensorflow.keras.layers import GRU +from tensorflow.keras.layers import Conv1D + +# Compare the two inputs +def comparator(learner, instructor): + layer = 0 + if len(learner) != len(instructor): + raise AssertionError(f"The number of layers in the model is incorrect. Expected: {len(instructor)} Found: {len(learner)}") + for a, b in zip(learner, instructor): + if tuple(a) != tuple(b): + print(colored("Test failed", attrs=['bold']), + f"at layer: {layer}", + "\n Expected value \n\n", colored(f"{b}", "green"), + "\n\n does not match the input value: \n\n", + colored(f"{a}", "red")) + raise AssertionError("Error in test") + layer += 1 + print(colored("All tests passed!", "green")) + return True + +# extracts the description of a given model +def summary(model): + model.compile(optimizer='adam', + loss='categorical_crossentropy', + metrics=['accuracy']) + result = [] + for layer in model.layers: + descriptors = [layer.__class__.__name__, layer.output_shape, layer.count_params()] + if (type(layer) == Conv1D): + descriptors.append(layer.padding) + descriptors.append(layer.activation.__name__) + descriptors.append(layer.strides) + descriptors.append(layer.kernel_size) + descriptors.append(layer.kernel_initializer.__class__.__name__) + + if (type(layer) == Conv2D): + descriptors.append(layer.padding) + descriptors.append(layer.activation.__name__) + descriptors.append(layer.kernel_initializer.__class__.__name__) + + if (type(layer) == MaxPooling2D): + descriptors.append(layer.pool_size) + descriptors.append(layer.strides) + descriptors.append(layer.padding) + + if (type(layer) == Dropout): + descriptors.append(layer.rate) + + if (type(layer) == ZeroPadding2D): + descriptors.append(layer.padding) + + if (type(layer) == Dense): + descriptors.append(layer.activation.__name__) + + if (type(layer) == LSTM): + descriptors.append(layer.input_shape) + descriptors.append(layer.activation.__name__) + + if (type(layer) == RepeatVector): + descriptors.append(layer.n) + + if (type(layer) == TimeDistributed): + descriptors.append(layer.layer.activation.__name__) + + if (type(layer) == GRU): + descriptors.append(layer.return_sequences) + + result.append(descriptors) + return result \ No newline at end of file diff --git a/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/tmp.wav b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/tmp.wav new file mode 100644 index 0000000000000000000000000000000000000000..16504cd81bf75597a3997792d5d0d27e2acc7a5c --- /dev/null +++ b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/tmp.wav @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:56e76a653b23ce5e51a158008e9d44142a99a25afe017c51b195678e31e739b1 +size 1764044 diff --git a/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/train.py b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/train.py new file mode 100644 index 0000000000000000000000000000000000000000..b70b7e3ec23c6100b7d22c2fc18a52d85615b5ef --- /dev/null +++ b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/train.py @@ -0,0 +1,95 @@ +import numpy as np +from pydub import AudioSegment +import random +import sys +import io +import os +import glob +import IPython +from td_utils import * + +from tensorflow.keras.callbacks import ModelCheckpoint +from tensorflow.keras.models import Model, load_model, Sequential +from tensorflow.keras.layers import Dense, Activation, Dropout, Input, Masking, TimeDistributed, LSTM, Conv1D +from tensorflow.keras.layers import GRU, Bidirectional, BatchNormalization, Reshape +from tensorflow.keras.optimizers import Adam + +Tx = 5511 # The number of time steps input to the model from the spectrogram +n_freq = 101 # Number of frequencies input to the model at each time step of the spectrogram + +Ty = 1375 # The number of time steps in the output of our model + +X = np.load("./XY_train/X0.npy") +Y = np.load("./XY_train/Y0.npy") + +X = np.concatenate((X, np.load("./XY_train/X1.npy")), axis=0) +Y = np.concatenate((Y, np.load("./XY_train/Y1.npy")), axis=0) + +Y = np.swapaxes(Y, 1, 2) + +# Load preprocessed dev set examples +X_dev = np.load("./XY_dev/X_dev.npy") +Y_dev = np.load("./XY_dev/Y_dev.npy") + +# GRADED FUNCTION: model + +def modelf(input_shape): + """ + Function creating the model's graph in Keras. + + Argument: + input_shape -- shape of the model's input data (using Keras conventions) + + Returns: + model -- Keras model instance + """ + + X_input = Input(shape = input_shape) + + ### START CODE HERE ### + + # Step 1: CONV layer (≈4 lines) + X = Conv1D(196, kernel_size = 15, strides = 4)(X_input) # CONV1D + X = BatchNormalization()(X) # Batch normalization + X = Activation("relu")(X) # ReLu activation + X = Dropout(0.8)(X) # dropout (use 0.8) + + # Step 2: First GRU Layer (≈4 lines) + X = GRU(units = 128, return_sequences = True)(X) # GRU (use 128 units and return the sequences) + X = Dropout(0.8)(X) # dropout (use 0.8) + X = BatchNormalization()(X) # Batch normalization + + # Step 3: Second GRU Layer (≈4 lines) + X = GRU(units = 128, return_sequences = True)(X) # GRU (use 128 units and return the sequences) + X = Dropout(0.8)(X) # dropout (use 0.8) + X = BatchNormalization()(X) # Batch normalization + X = Dropout(0.8)(X) # dropout (use 0.8) + + # Step 4: Time-distributed dense layer (≈1 line) + X = TimeDistributed(Dense(1, activation = "sigmoid"))(X) # time distributed (sigmoid) + + ### END CODE HERE ### + + model = Model(inputs = X_input, outputs = X) + + return model + +model = modelf(input_shape = (Tx, n_freq)) +model.summary() + +opt = Adam(lr=0.001, beta_1=0.9, beta_2=0.999, decay=0.01) +model.compile(loss='binary_crossentropy', optimizer=opt, metrics=["accuracy"]) + + +model.fit(X, Y, batch_size=20, epochs=100) + +loss, acc = model.evaluate(X_dev, Y_dev) +print("Dev set accuracy = ", acc) + +from tensorflow.keras.models import model_from_json + +json_file = open('./models/model_new3.json', 'r') +loaded_model_json = json_file.read() +json_file.close() +model = model_from_json(loaded_model_json) +model.load_weights('./models/model_new3.h5') \ No newline at end of file diff --git a/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/train.wav b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/train.wav new file mode 100644 index 0000000000000000000000000000000000000000..aaf94004f0d1d1c52669aef1ea03a7c01818b6fb --- /dev/null +++ b/Seq2Seq/Trigger_detection/home/jovyan/work/W3A2/train.wav @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:eb78ac3ca6a2ea9432e443bec5beb0830db070dd57c16de79028f88a5e607dfe +size 1764044