<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Rahul Saxena on Medium]]></title>
        <description><![CDATA[Stories by Rahul Saxena on Medium]]></description>
        <link>https://medium.com/@rahul9iitk?source=rss-9608357e2846------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*9UABXCQEiEhUYQGdJfPDeQ.jpeg</url>
            <title>Stories by Rahul Saxena on Medium</title>
            <link>https://medium.com/@rahul9iitk?source=rss-9608357e2846------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 18:02:10 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@rahul9iitk/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Who spoke when? Build your own Speaker Diarization module from scratch!]]></title>
            <link>https://medium.com/saarthi-ai/who-spoke-when-build-your-own-speaker-diarization-module-from-scratch-e7d725ee279?source=rss-9608357e2846------2</link>
            <guid isPermaLink="false">https://medium.com/p/e7d725ee279</guid>
            <category><![CDATA[speech-recognition]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[developer]]></category>
            <category><![CDATA[deep-learning]]></category>
            <dc:creator><![CDATA[Rahul Saxena]]></dc:creator>
            <pubDate>Wed, 24 Jun 2020 08:40:46 GMT</pubDate>
            <atom:updated>2020-06-24T08:47:13.912Z</atom:updated>
            <content:encoded><![CDATA[<h3>Who spoke when! How to Build your own Speaker Diarization Module</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GfupRiGBVnMtOqMj0vCINw.png" /></figure><p>In this blog, we implement our own Speaker Diarization module by using the pre-trained model provided by the Resemblyzer repository for creating speaker embeddings.</p><p>But before we get to implement it, let’s get a comprehensive understanding of how Speaker Diarization works.</p><h3>A Look at Speaker Diarization</h3><p><strong>Speaker diarization</strong> is the process of partitioning an audio stream with multiple people into homogeneous segments associated with each individual. It is an important part of speech recognition systems and a well-known open problem.</p><p>Speaker Diarization has applications in many important scenarios, such as understanding medical conversations, video captioning and many more areas.</p><p>To illustrate with an example, suppose you and your friend are on a call, discussing about the assignment which is due next day ( don’t tell me you start your assignments anytime before deadline 😜). Now, what speaker diarization does is that it takes in your audio call, segments it out speaker wise, and creates a transcript of the call. In this case your transcript might look like -</p><p><strong><em>You:</em></strong><em> Hey, Mike! Started the assignment already?</em></p><p><strong><em>Mike:</em></strong><em> No way man, I even forgot we had an assignment due next day!</em></p><p><strong><em>You:</em></strong><em> Ya bro, we better start working on it right now. It has a weightage of 20 percent!</em></p><p><strong><em>Mike:</em></strong><em> Right, and I have already flunked my mid-semester. Guess this is the only way to avoid an F in the course!</em></p><p><strong><em>You:</em></strong><em> Ya! Let’s begin. I am coming to your place in 20 minutes.</em></p><p><strong><em>Mike:</em></strong><em> Ya sure! See you then.</em></p><p>So, Speaker Diarization is essentially the task of processing an audio file and creating a transcript of the entire conversation. Although this task might seem easy, it is actually an involved problem, and currently a hot topic of research with giants like Google Brain and IBM Research working to build robust and accurate speaker diarization models.</p><p><strong>In this post, I will try to give a roadmap for you to build your own Speaker Diarization model. </strong>Feel free to experiment further. Who knows, maybe you are the one to initiate the next big advancement in this field! 😎</p><h4>Anatomy of a Speaker Diarization System</h4><p>A Speaker Diarization system consists of the following subsystems :</p><ul><li><strong>Speech Detection: </strong>In this step, we use a Voice Activity Detector (VAD) module to separate out speech from non-speech. This is required to trim out silences and non-speech part from your audio recording.</li><li><strong>Speech Segmentation:</strong> In this step, we extract out small segments of your audio call (typically about 1 second long) in a continuous manner. Speech segments typically contain just one speaker.</li><li><strong>Embedding Extraction:</strong> This system creates a neural-network based embedding of your speech segments extracted in the previous step. An embedding is a vector representation of data which could be used by the deep learning framework. We can create embeddings for audio, text, images, documents etc.</li><li><strong>Clustering:</strong> After creating embeddings of the segments, we next need to cluster these embeddings. After clustering, the embeddings of the segments belonging to same speakers are part of one cluster, and assigned the label of the speaker. This step is crucial as it assigns the labels to our embeddings, as well as the number of clusters, which indicates us of the number of speakers in the audio file.</li><li><strong>Transcription:</strong> After clustering, we now have the labels for all the embeddings. Using this, we can now segment out the audio into clips belonging to each speaker, and pass it to a Speech-to-Text model (or <strong>ASR</strong>) which produces the transcript of the speech segment.</li></ul><h4><strong>Relevant Research Works</strong></h4><p>Speaker Diarization is an active area of research, and new methodologies are introduced frequently. In this post, we will implement a research paper published by Google Brain — “ <a href="https://arxiv.org/abs/1710.10468"><em>Speaker Diarization with LSTM</em></a>” . Although it’s not required for the purpose of this tutorial, you can go through the paper once. A lot of terms might not make much sense to you. Don’t worry, I will have it all covered for you 👍</p><h4>Brief Explanation of paper</h4><p>I will try to give a brief explanation of the methodology being used in the paper for speaker diarization. Feel free to skip to next section if you can’t wait to get your hands dirty!</p><p>In this paper the authors have implemented all the sub-parts of a speaker diarization system as we discussed before —</p><ol><li><strong>Speech Detection :</strong> The authors have used the VAD module from <a href="https://github.com/pyannote/pyannote-audio">pyannote.metrics</a> library. A <strong>VAD</strong> is basically a neural network trained to distinguish speech signals from non-speech signals. It is used to trim out silences from the audio file.</li><li><strong>Speech Segmentation:</strong> This is achieved by segmenting the audio into windows with overlap. The size of the window determines the size of your segment. Think of it as a magnifier which works on a specific segment of your audio file. So, if your window size is <strong>2 seconds</strong>, and you set an overlap of <strong>0.5 seconds</strong>, your first window would be : (<strong>start</strong> = 0.0s , <strong>stop</strong> = 2.0s), next window will be: (<strong>start</strong> = 0.5s, <strong>stop</strong> = 2.5s) … and so on until your full audio is covered.</li><li><strong>Embedding Extraction:</strong> The authors extracted embeddings of each of the audio segments which we found before. First, we find the <strong>MFCC (Mel Frequency Cepstral Coefficient)</strong> of the audio segment. These are basically feature coefficients which capture the variations in the speech like pitch, quality, intone etc of the voice in a much better way. They are obtained by doing a specialized Fourier Transform of the speech signal. Don’t worry, the SciPy library of python has a separate module for finding MFCCs for us. In the next step, the authors use an <strong>LSTM based network</strong> which takes in the MFCCs and outputs a vector representation (embedding) which they call a <strong>d-vector</strong>.</li><li><strong>Clustering:</strong> Clustering is an <strong>Unsupervised machine learning</strong> method which tries to create clusters (or groups) of your data in an n-dimensional space. There are many clustering algorithms being used by machine learning researchers, most famous among them being <strong>K-means</strong>.</li></ol><p>If you are unfamiliar with clustering, there are tons of great articles on net explaining clustering algorithms.</p><p>In their work, the authors found that <strong>Spectral Clustering</strong> works best for their data.</p><p>Spectral clustering works by first creating an <strong>affinity matrix</strong> of the data ( a matrix whose <strong>(i,j)th</strong> element is obtained by performing some mathematical operation on <strong>ith</strong> and <strong>jth</strong> embedding), and then after performing some matrix operations on this affinity matrix, we use K-means to get the final labels.</p><p>The math is beyond the scope of this article, and I will write a separate blog post on spectral clustering soon 😅</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/918/1*ryP8OUNqGTg_JLsCyOOxKA.png" /></figure><p>And that’s it. This paper has been a big step in the field of Speaker Diarization, and set new benchmarks by achieving a lower <strong>Diarization Error Rate</strong> than the previous state-of-the-art methods (Diarization Error Rate or <strong>DER</strong> is the metric used to evaluate Speaker Diarization models. Huh..lots of new terms eh ?😒)</p><p><strong>Congrats for coming this far!</strong> 🙌</p><p>I am sure you enjoyed reading about the research work which we are about to implement.</p><p>I mean, why not?</p><p>Who doesn’t like reading intricate research works with tons of unheard terms and complex engineering mathematics 😛</p><h3><strong>Let’s begin the implementation </strong>🔥</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/498/1*pBT5DbG6ylfGJWPsPmpfsA.gif" /></figure><p>In our implementation, we will be using this awesome open-source repository for our work — <a href="https://github.com/resemble-ai/Resemblyzer"><strong>Resemblyzer</strong></a> .</p><p>Resemblyzer will be doing three of our major tasks, namely, Speech detection, Speech Segmentation and Embedding Extraction.</p><p>For our purpose you can work without a GPU since we will be using mostly pre-trained models. However, for doing your own experiments or training your own model, you would need a decent GPU.</p><p><strong>Prerequisites:</strong></p><ol><li>Get a linux machine. If you are a windows user, try Google Colab. It is a very popular online notebook which gives the best environment you can ask for doing Deep Learning experiments. It also provides you with free GPU facility.</li><li>Get the latest version of anaconda installed on your system.</li><li>Although not mandatory, it would be very nice to have a Jupyter notebook installed on your system. It gives a very interactive working environment and makes debugging a lot easier.</li></ol><p><strong>Now we are good to go! 😃</strong></p><p>First, clone the resemblyzer on your system and make it your working directory</p><pre>git clone <a href="https://github.com/resemble-ai/Resemblyzer.git">https://github.com/resemble-ai/Resemblyzer.git</a><br>cd Resemblyzer</pre><p>Now you need an audio file to diarize. You can use use any audio file you want. For starting, you can record a small conversation with your friend on your phone ( about 2–3 minutes long) and save it on your system. Now write the below lines of code —</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3c553ac86f21e48bf3da57e4948979bd/href">https://medium.com/media/3c553ac86f21e48bf3da57e4948979bd/href</a></iframe><h4><strong>Let&#39;s go over step-by-step whats going on here</strong></h4><ul><li>You provide the pathname to your file by passing it into the Path module, in my case its ‘my_recording.wav’. Then you pass the file path to the <strong>preprocess_wav</strong><em> </em>function, which internally uses a <strong>VAD </strong>to<strong> </strong>trim out the silences in the audio file and also normalizes the decibel level of audio.</li><li>Then you create an instance of the <strong>VoiceEncoder</strong> class name encoder, passing cpu as your default device. The <strong>embed_utterance</strong> function of this instance takes in the processed wav file, segments it out into windows , makes MFCCs of these segments and finally creates <strong>d-vectors</strong> of these audio segments.</li><li>The <strong>cont_embeds</strong> is a <strong>N by D</strong> matrix, where <strong>N is the number of segments</strong> created (which is equal to the number of d-vectors) and <strong>D is the dimension of each d-vector</strong>, which by default is 256. <strong>wav_splits</strong> is a list with the start and end time of each window for which a d-vector has been created.</li></ul><h4>Note:</h4><p>Sometimes the module might throw an error when you try to use an audio file having file extension <strong>different from wav</strong>, like <strong>mp3</strong>. In this case, you might need to convert your mp3 file to wav before using. You can use the below script (you will need to install pydub first)</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a1b980b0eb56c36d5f299edefcb3ab5f/href">https://medium.com/media/a1b980b0eb56c36d5f299edefcb3ab5f/href</a></iframe><p>Next step is the clustering of our d-vectors. For this, we will use an open source implementation of Spectral Clustering by Quan Wang, one of the original authors of the paper we are implementing, who has been generous enough to provide us with the code. 🙌 🙌</p><p><em>Install the package on your system:</em></p><pre>pip3 install spectralcluster</pre><p><em>Now continue your code by writing the following lines —</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/aa4159eaf12817bd020a233728e7c7ed/href">https://medium.com/media/aa4159eaf12817bd020a233728e7c7ed/href</a></iframe><p>That’s it. We now have got labels for our d-vectors. We are almost done! 😌</p><h4>Creating continuous segments</h4><p>We have got labels for our d-vectors in the previous step. However, each d-vector corresponds to a small window frame, and using them directly for any further task is not feasible. We need to join continuous windows which have a common speaker together.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/200091e6d2697a9f0c8df8dd2fc96192/href">https://medium.com/media/200091e6d2697a9f0c8df8dd2fc96192/href</a></iframe><p>We pass the previously created <strong>labels</strong> and <strong>wav_splits</strong> to the <strong>create_labelling</strong> function, which finds a <strong>continuous segment of windows having a common label</strong> (or a common speaker) and merges them together. <strong>labelling</strong> is a list of tuples with values in order <strong>(speaker_label, start_time, end_time)</strong></p><h4>Using our diarization labels</h4><p>After you have got the labelling file, which is basically a dictionary of <strong>which person spoke when</strong> your diarization task is in theory complete. You can now use these labels to create a text transcription of your audio call as we had seen earlier, or create a cool animation plot of your audio file as I have done here —</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FSY6rHaD-eEc%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DSY6rHaD-eEc&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FSY6rHaD-eEc%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/e3bee05eebdb466c9dcded538899bcc4/href">https://medium.com/media/e3bee05eebdb466c9dcded538899bcc4/href</a></iframe><p>You can use Google’s <a href="https://cloud.google.com/speech-to-text/">speech-to-text</a> API for creating a transcript of your audio file. You can find the step-by-step process for getting your own API account <a href="https://cloud.google.com/speech-to-text/docs/quickstart-client-libraries">here</a>. I am providing below the pseudocode which you can follow to create your transcript —</p><ol><li>For each tuple element ‘ele’ in your labelling file, extract ele[0] as the speaker label, ele[1] as the start time and ele[2] as the end time.</li><li>Trim your original audio file from start time to end time. You can use <strong>ffmpeg</strong> for this task.</li><li>Pass the trimmed audio file obtained in the previous step to Google’s API which will return you the text transcript of this audio segment.</li><li>Write the transcript along with the speaker label to a text file and save it.</li></ol><p>And that’s it!</p><p>After going through each element in your labelling file and following the above procedure you will get your transcript file created, something like this-</p><pre>Speaker 0 : well that was Jason and Yuki we asked you who&#39;s Yuki meeting on Saturday night <br>Speaker 1 : probably going to meet <br>Speaker 0 : but instead of saying going to Yuki said going to she&#39;s probably going to meet Lucy <br>Speaker 1 : salt with looking up and mint program English because often say the words going to very far out of it. Like going to going to say usually do this if Informer situation when the choking <br>Speaker 0 : does that mean that you shouldn&#39;t say going to when you need to speak carefully and politely like in a job interview <br>Speaker 1 : really it&#39;s going to come out naturally probably okay but a job interview so it&#39;s probably a bit more polite and you don&#39;t usually use going to NY pic unless you actually writing that. In science <br>Speaker 0 : some people never use it even informally more common in American English <br>Speaker 1 : one other thing about is that we only use it to replace into when going to explode <br>Speaker 0 : rude if you say I&#39;m going to Parrot you can&#39;t use going to to say I&#39;m going to Paris you have to add a verb base and say I&#39;m going to go to Paris <br>Speaker 1 : I go to Paris let&#39;s have another clip with the mold short spoken both during this weekend&#39;s UK <br>Speaker 0 : so Jason said whatcha whatcha that&#39;s a short form of what are you but sometimes what do you</pre><p>Woah.. !! That was a lot of code 😆. But you finally built your own speaker diarization model by implementing an actual research paper !! You have got every right to brag to your classmates and friends 😎</p><h4>What can I do next?</h4><p>In this blog, we implemented our own speaker diarization module by using the pre-trained model provided by Resemblyzer repository for creating speaker embeddings.</p><p>You can train your model using your own data and try experimenting with a different clustering algorithm.</p><p>Also, I would recommend going through <a href="https://github.com/wq2012/awesome-diarization">this</a> awesome repo for a curated list of datasets and research papers on speaker diarization.</p><h4>End of Session 😆</h4><p>Speaker Diarization is a vast field and new researches and advancements are being made in this field regularly. Here I have tried to give a small peek into this vast topic. I hope you enjoyed this blog and got to learn something interesting. Feel free to ask me any doubts you have!</p><p>End of the article. Take a break 😄 you deserve it!</p><p>I would like to thank <a href="https://medium.com/@008karan">Karan Purohit</a> sir and <a href="https://medium.com/@vishwanath.jha91">Vishwa Nath Jha</a> sir for constantly guiding me and clearing all my doubts from time to time.</p><p>If you enjoyed this article, and found it helpful, help it reach more deep learning enthusiasts around the world by giving it a clap and sharing it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/345/1*UcrcRXbJ0Oday4QyX9AzYQ.gif" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e7d725ee279" width="1" height="1" alt=""><hr><p><a href="https://medium.com/saarthi-ai/who-spoke-when-build-your-own-speaker-diarization-module-from-scratch-e7d725ee279">Who spoke when? Build your own Speaker Diarization module from scratch!</a> was originally published in <a href="https://medium.com/saarthi-ai">Saarthi.ai</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>