<jupyter_start><jupyter_text>"Engine knock detection AI part 1/5"
> "This is the first notebook in a series where I'm following along with the fast.ai course Practical Deep Learning for Coders by training an AI to identify knocking in 4-stroke car petrol engines"
- image: images/motor-768750_1920.jpg > Important: This notebook is not guaranteed to work without modification. It's a while since I wrote this and haven't tested it since. For a summary of this project see [Engine knock detection AI part 5/5](http://niklasekman.com/2021/02/10/Train-model-updated.html)  Background
The inspiration for this project comes from the presentation of [fast.ai](https://www.fast.ai/) course alumni projects by <NAME> in some of the first lessons of the third installment of [Practical Deep Learning for Coders](https://course.fast.ai/) where training an image classifier using spectrograms to identify sounds is mentioned.

What this means in simpler terms is that an image classifying algorithm is presented with a series of pictures labeled with which class or case they represent. Each picture represents what that sound looks like if one were to plot the sound intensities at different frequencies as a function of time.   Method

A set of recordings of both knocking and healthy sounding engines has first to be aquired in order to create a training set of spectrograms. I've chosen to create two playlists on youtube of videos specifically depicting car engines, one for each case. I made sure to just include petrol engines since my suspicion was that the sound of a diesel engine might throw things off.

As a non car mechanic I made that assumption based on the idea that if I could distinguish a well running engine from a knocking one then the resulting model could do it too. I found that I had some trouble making the distinction with some of the cold started diesels I listened to.  Downloading audio files
After creating the playlists Google Drive is mounted to store the intermediary sound files.<jupyter_code>#collapse-output
from google.colab import drive
drive.mount('/content/drive')<jupyter_output>Go to this URL in a browser: https://accounts.google.com/o/oauth2/auth?client_id=947318989803-6bn6qk8qdgf4n4g3pfee6491hc0brc4i.apps.googleusercontent.com&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&scope=email%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdocs.test%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive.photos.readonly%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fpeopleapi.readonly&response_type=code

Enter your authorization code:
··········
Mounted at /content/drive<jupyter_text>The youtube-dl python package is installed and the soundtrack from each of the videos is downloaded.<jupyter_code>#collapse-output
!pip install --upgrade youtube-dl
import youtube_dl
#collapse-output
ydl = youtube_dl.YoutubeDL({'outtmpl': '%(id)s%(ext)s'})

ydl_opts = {
    'format': 'bestaudio/best',
    'postprocessors': [{
        'key': 'FFmpegExtractAudio',
        'preferredcodec': 'mp3',
        'preferredquality': '192',
    }],
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
    ydl.download(['https://www.youtube.com/playlist?list=PL9R1Zswn-XPCh3A3bV9Vnf7ui9YCZWo9w'])
!rm -rf ./sample_data<jupyter_output><empty_output><jupyter_text>Checking to see that all the expected files are present.<jupyter_code>!ls *.mp3<jupyter_output>'1996 Subaru Outback, Normal Engine Sound-8BOtWY3RobA.mp3'
'2003 toyota corolla engine sound-ho38ZYkQJxs.mp3'
'2016-2018 Toyota Tacoma Loud Engine sound is Normal-nSB65kClSXM.mp3'
'2016 Kia Rio 1.6L V4 Engine - Normal Running Noises _ Sounds-nGQcC7giMkU.mp3'
'2017 Hyundai Elantra SE 2.0L 4 Cylinder - Normal Engine Running Noises-Eky3PMh76gY.mp3'
'2017 Kia Rio 1.6L 4 Cylinder GDI Engine - Normal Engine Sounds-2EQcfJAU7IM.mp3'
'2017 Nissan Versa Note - Normal Engine Running Noises - 1.6L Engine-PRO0HgD9qx4.mp3'
'2018 Hyundai Tucson 2.0 GDI Nu Engine Sound Normal-pLmX2ws7znE.mp3'
'3.2 fsi engine sound cold (normal or not)-mOFcfwLTNkY.mp3'
'Bmw f30 Noisy Engine Sound-bkPtY1pW82Y.mp3'
'Listen Toyota 2.4 VVT-i engine sound, when engine is very OK. Years 2002 to 2015-6GPgodkLSkQ.mp3'
'Mini R56S normal engine sound-8cXmy_U0_28.mp3'
'Nissan Frontier 4L v6 engine sound idle-YiwsC_UlDFY.mp3'
'Normal Engine Idle Sound Cold & Warm - 2006 Nissan Sentra-5q1uATQu8zg.mp3'
'Volvo S60 2.0T - Normal engin[...]<jupyter_text>Moving the sound files from the Colab instance to a folder on Google Drive.<jupyter_code>!mv *.mp3 /content/drive/My\ Drive/Colab\ Notebooks/fast.ai/KnockKnock/data/normal/<jupyter_output><empty_output><jupyter_text>Rinse and repeat for the other playlist.<jupyter_code>#collapse-output
ydl_opts = {
    'format': 'bestaudio/best',
    'postprocessors': [{
        'key': 'FFmpegExtractAudio',
        'preferredcodec': 'mp3',
        'preferredquality': '192',
    }],
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
    ydl.download(['https://www.youtube.com/playlist?list=PL9R1Zswn-XPCnpyXQLRPYVJr4BXJUYie8'])
!mv *.mp3 /content/drive/My\ Drive/Colab\ Notebooks/fast.ai/KnockKnock/data/knocking/<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08933773548261799, 'fraction_numerical': 0.043309380462225676, 'mean_word_length': 5.116389548693586, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 22, 'https://': 5, 'lorem ipsum': 0, 'www.': 7, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 23, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '233973', 'license': 'Apache-2.0', 'n_tokens_mistral': 1883, 'n_tokens_neox': 1699, 'n_words': 549, 'path': '_notebooks/2019-09-13-youtube-dl_commented.ipynb', 'repo_name': 'niklasekman/my_fastpages', 'size': '5156'}