<?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 Nev Acar on Medium]]></title>
        <description><![CDATA[Stories by Nev Acar on Medium]]></description>
        <link>https://medium.com/@nevacar?source=rss-6b32a92a518a------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*5BYTs9gR4qrJgoulSzctlg.jpeg</url>
            <title>Stories by Nev Acar on Medium</title>
            <link>https://medium.com/@nevacar?source=rss-6b32a92a518a------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 01:28:27 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@nevacar/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[Playlist Classification on Spotify using KNN and Naive Bayes Classification]]></title>
            <link>https://medium.com/data-science/playlist-classification-on-spotify-using-knn-and-naive-bayes-classification-35a279b7e255?source=rss-6b32a92a518a------2</link>
            <guid isPermaLink="false">https://medium.com/p/35a279b7e255</guid>
            <category><![CDATA[music]]></category>
            <category><![CDATA[classification]]></category>
            <category><![CDATA[spotify]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Nev Acar]]></dc:creator>
            <pubDate>Wed, 23 Jan 2019 01:18:25 GMT</pubDate>
            <atom:updated>2021-08-28T10:40:29.226Z</atom:updated>
            <content:encoded><![CDATA[<h3>Playlist Classification on Spotify using KNN and Naive Bayes Classifier</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*87n3r105VP1eo3n_yHLCpA.jpeg" /><figcaption>by Spencer Imbrock on Unsplash.com</figcaption></figure><p>One day, I thought it would be cool if Spotify helped me pick a playlist when I like a song. The idea is to touch on the plus button when my phone is locked and Spotify add it into one of my playlists rather than library so that I don’t go into the app and try to pick a playlist that it suits well. This way, I wouldn’t have to choose a playlist among all of my playlists and I would just leave it to the algorithms. Then, I realized it makes a good side project for a machine learning enthusiast. After all, I started this project to avoid unlocking my phone and think for three seconds which is not the most optimized solution for me as an individual.</p><p>You can find the Jupyter Notebook on <a href="https://github.com/n0acar/spotify-playlist-selection">https://github.com/n0acar/spotify-playlist-selection</a></p><h3>1- Scraping Data from Spotify Web API</h3><p>This is actually where everything starts. I found Spotify Web API and Spotipy framework. By the combination of both, we will be able to extract official Spotify data with useful features.</p><ul><li><a href="https://spotipy.readthedocs.io/en/latest/">Welcome to Spotipy! - spotipy 2.0 documentation</a></li><li><a href="https://developer.spotify.com/documentation/web-api/">Web API | Spotify for Developers</a></li></ul><p>Before sending a request to Spotify Web API, you need to install and import dependencies below.</p><pre>import spotipy<br>import spotipy.util as util<br>from spotipy.oauth2 import SpotifyClientCredentials</pre><p>In order to get access to API, you will need special codes created only for your application. Go to <a href="https://developer.spotify.com/dashboard/">https://developer.spotify.com/dashboard/</a> and click “Create a Client ID” or “Create an App” to get your “Client ID” and “Client Secret”. After that, Redirect URI must be changed to any page you decide on in the settings of your Spotify application.</p><pre>client_id= &quot;YOUR_CLIENT_ID&quot;<br>client_secret= &quot;YOUR_CLIENT_SECRET&quot;<br>redirect_uri=&#39;<a href="http://google.com/&#39;">http://google.com/&#39;</a></pre><p>Next, state your scope from Spotipy documentation and “sp” will be your access key to Spotify data, from now on.</p><pre>username=&#39;n.acar&#39;<br>client_credentials_manager = SpotifyClientCredentials(client_id=client_id, client_secret=client_secret) <br>scope = &#39;user-library-read playlist-read-private&#39;<br>try:<br>    token = util.prompt_for_user_token(username, scope,client_id=client_id, client_secret=client_secret, redirect_uri=redirect_uri)  <br>    sp=spotipy.Spotify(auth= token)<br>except:<br>    print(&#39;Token is not accesible for &#39; + username)</pre><p>Then, you can do a lot with that sp variable. It is your Spotify object. For example, you can extract your song library, take the data of a playlist or better, all playlists of a user.</p><pre>songLibrary = sp.current_user_saved_tracks()</pre><pre>playlist = sp.user_playlist(username, playlist_id=&#39;6TXpoloL4A7u7kgqqZk6Lb&#39;)</pre><pre>playlists = sp.user_playlists(username)</pre><p>To find ID numbers of playlists or tracks, basically use sharing link and get the code out of it. Everything the API provides come in JSON format and you can basically extract the information you need out of that.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1Ytoh0zMh5oI_CMU3U5ooQ.png" /></figure><p>For various usage of Spotify data, you can go through the documentations of Spotipy and Spotify Web API. There are plenty of methods and data types that might be good use for a project.</p><h3>2- Visualization of Features and Insight</h3><p>When human beings listen to a song, they perceive several aspects of it. A person probably does not have trouble determining whether a track is acoustic or electronic. Most of the people having some kind of music sense can say a lot about music that can be hard to get for a computer. However, Spotify API provides some useful <a href="https://developer.spotify.com/documentation/web-api/reference/tracks/get-audio-features/">features</a> about any track on the platform. Most of them are normalized between 0 and 1 so they are pretty easy to tackle.</p><p>Below is a comparison of two Spotify playlists called “Classical Essentials” represented by blue and “Get Turnt” (a rap playlist by Spotify) represented by red.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0plpEaSGp-Cl1s9yWLxM2A.png" /><figcaption>Get Turnt (RED) — Classical Essentials (BLUE)</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Npb9kgQ_AGh5HgqA2QpHEQ.png" /></figure><p>From these plots, you can see that these two genres show distinct patterns by the nature of the music. While the rap playlist has higher values in terms of danceability, energy, loudness, speechiness, valence and tempo, the classical music playlist have higher acousticness and instrumentalness values as expected.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*T4oXwjY8BNtQsXifDfni0w.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/918/1*MTqx3RKCgYoMcgxzFAMBkw.png" /></figure><h3>3- Applying Machine Learning Algorithms</h3><p>I used two algorithms which are K-Nearest Neighbors and Naive Bayes Classification. Actually, I only wanted to implement a couple of basic algorithm from scratch and I realized those are the most convenient ones to start with.</p><p><strong>K-Nearest Neighbors Classification: </strong>K -Nearest Neighbor (KNN) algorithm is a classification technique that utilizes the feature similarity between existing and new data based on the notion of distance.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f37842373df11f1f057a3ec8607460e3/href">https://medium.com/media/f37842373df11f1f057a3ec8607460e3/href</a></iframe><p><strong>Naive Bayes Classification: </strong>Naive Bayes Classification is a method which calculates the probability of every feature as if they are independent and determines the outcome with the highest probability based on Baye’s Theorem.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dff7575e11515034580fd62b70384125/href">https://medium.com/media/dff7575e11515034580fd62b70384125/href</a></iframe><p>The statistical distinction between the two is pretty obvious. I excluded tempo and loudness from my feature list since they didn’t contain much information about the genre and very scattered. I used first 35 songs of the playlists for training and rest of the songs for testing. The testing set might not be the most objective one out of the subjective nature of music genres. Since both playlists are created by Spotify, I thought it’s the safest way to test the algorithms. Luckily, our target is to classify rap and classical music, the statistics don’t make any mistakes and we get 100% accuracy rate.</p><p>Even if all songs turned out to be in their right category, one classical song was almost going to the wrong bucket upon using KNN. It is called <a href="https://open.spotify.com/track/3nxaPqAOxUxkLhIhbvmv4t?si=s9sYUAZlRq6dQNl-KmY5zw">Violin Concerto BWV 1042 in E Major: I. Allegro</a>. It wasn’t even close to a rap song when using Naive Bayes Classification, yet it is still the closest thing to a rap in classical music playlist. At first, I tried to find the closest rap songs in terms of distance which is Crushed Up by Future so it was a bit surprising. Then, I checked the features of this classical song which actually looks a bit faulty to me. (Give it a listen! At least the “instrumentalness” feature should be greater than zero.)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ibn-YuKlJfpc5dHLaEshcg.png" /></figure><p>Nevertheless, this wasn’t a hard task. To make things more complicated and evaluate the algorithms better, I decided to add one more playlist which is “Rock Save the Queen”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vuYQXRVc5VKeUlXfkd0qJw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/536/1*McX9_dx9usLaAUSNqOWrNQ.png" /><figcaption>Accuracy Rates (%)</figcaption></figure><p>NBC looks more consistent than KNN. That classical song mentioned above is not in classical music playlist anymore because of newcoming rock songs. Still, NBC doesn’t make any mistake. However, in Get Turnt playlist, things are a little bit different. Deciding the genre based on only closest neighbors is not a good idea for rap songs this time. The most confused rap song is “EA” by “Wifisfuneral” and “Robb Bank$” and it ended up in rock bucket.</p><p>To make things more challenging I added a new playlist, “Coffee Table Jazz”, created by again Spotify.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xXOczAad3wLAeR5cUDIecg.png" /></figure><p>Before showing you the results, I’d like to add updated visualization of the four playlists the algorithms use in the final model. I will refer to these at the final step when I investigate the results. (Rock Save the Queen and Coffee Table Jazz are represented by yellow and orange, respectively.)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2I1cUv1nhTWDpckVlwtSGw.png" /><figcaption>Get Turnt (RED) — Classical Essentials (BLUE) — Rock Save the Queen (YELLOW) — Coffee Table Jazz (ORANGE)</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0bx__cy8A6HhQMzyzqIW9g.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/522/1*Qzs1ZjAREV6W1KhjaOUUhA.png" /><figcaption>Accuracy Rates (%)</figcaption></figure><p>From these, we clearly see that classical music and jazz music genres are statistically close to each other while the same is true for rock and rap genres. This is why when rock music is introduced, the newcoming songs didn’t cause any confusion of classical songs but damaged the accuracy rate of Get Turnt. The same case occurred when Coffee Table Jazz was introduced to the system. This time, classical music classification by KNN is severely hurt. Knowing that classical music is only confused with jazz, 51% accuracy rate is almost random. This shows that considering only the closest neighbors don’t work to classify the genre. At least, it is safe to say NBC is much better.</p><p>NBC couldn’t work for classical and jazz music as well as it works for rap and rock. I think this is the case because the way people distinguish classical songs from jazz is classifying the different instruments used traditionally. Even if there is a bit of difference between them in terms of danceability, that is not enough for some of the songs. The other features are almost the same which is expected. Instrumentalness; however, is almost exactly same as it can be seen on the spider graph. Yes, they are obviously both instrumental, yet this is where this feature comes short of human sense. Maybe, Spotify providing another data type of which instruments or types of instruments are specifically used in a song might resolve this problem as well. I think 98.44% accuracy rate is great for the other two playlist. NBC only missed one of the songs from each playlist. They are <a href="https://open.spotify.com/track/7wBJfHzpfI3032CSD7CE2m?si=mb5ERmxYSYaBeY9XYvadCQ">STARGAZING</a> by Travis Scott which is identified as rock and <a href="https://open.spotify.com/track/4L2F0LcDVtNrFc1K4lmx3m?si=8V-sV5tQQkm70zJ-R2tWnQ">Distant Past</a> by Everything Everything as rap. (If you listen to them, please share your comments.)</p><h3>4- Conclusion</h3><p>The aim of this project is to create undetermined genres of given playlists and classify the new songs accordingly. It is supposed to work for any playlist that has the same type of songs in it. I opt to use four clearly genre-labeled playlists created by Spotify. I used two different algorithms which are KNN and NBC as mentioned in the post. NBC outperformed KNN overall. The challenges this system might encounter is to distinguish two different playlist with very close genres. I also realized that features that Spotify provides might not be enough for some cases.</p><p>Don’t hesitate to ask questions and give feedback. Music, genres, and overall this work are subjective. I appreciate any comment on the results since the topic is open for discussion.</p><p>Cheers,</p><p>Nev</p><p>Give me a follow for more content and support!</p><p>Connect with me on <a href="https://twitter.com/nevacar_">Twitter</a> and <a href="https://www.linkedin.com/in/nevzat-acar/">LinkedIn</a>…</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=35a279b7e255" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science/playlist-classification-on-spotify-using-knn-and-naive-bayes-classification-35a279b7e255">Playlist Classification on Spotify using KNN and Naive Bayes Classification</a> was originally published in <a href="https://medium.com/data-science">TDS Archive</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Eigenfaces: Recovering Humans from Ghosts]]></title>
            <link>https://medium.com/data-science/eigenfaces-recovering-humans-from-ghosts-17606c328184?source=rss-6b32a92a518a------2</link>
            <guid isPermaLink="false">https://medium.com/p/17606c328184</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[data-visualization]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[data-science]]></category>
            <dc:creator><![CDATA[Nev Acar]]></dc:creator>
            <pubDate>Tue, 21 Aug 2018 21:48:55 GMT</pubDate>
            <atom:updated>2021-08-28T10:41:12.457Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/638/1*9TOko4WOa0vLBdgQ4_p5bg.jpeg" /><figcaption>Figure #1: Casper the Friendly Ghost (source: <a href="https://vignette.wikia.nocookie.net/christmasspecials/images/e/e8/Casper.jpg/revision/latest?cb=20121014033319">https://vignette.wikia.nocookie.net/christmasspecials/images/e/e8/Casper.jpg/revision/latest?cb=20121014033319</a>)</figcaption></figure><p>Having more by having less… Easy! This machine learning method will help you store more information in smaller sizes with only some linear algebra fundamentals. By that, we will recover human faces from some ghost shaped messes.</p><p>You must have been told, independent vectors create spaces in abstract ways. Now, imagine for real. All the spaces are formed by the vectors and so are the things inside those spaces. Some of these vectors contribute to some of the things more than the others. The others may be negligible besides them and maybe we don’t need to know about those because we basically don’t care and don’t want to carry their problems with us. This is exactly what I will show you to do about eigenfaces in this post.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/660/1*LCyLvtf1vkRxe6mED2JLVQ.jpeg" /><figcaption>Figure #2: not this space but also this space (source: <a href="https://img.purch.com/w/660/aHR0cDovL3d3dy5saXZlc2NpZW5jZS5jb20vaW1hZ2VzL2kvMDAwLzA5Ny85NTcvb3JpZ2luYWwvc3BhY2UuanBn">https://img.purch.com/w/660/aHR0cDovL3d3dy5saXZlc2NpZW5jZS5jb20vaW1hZ2VzL2kvMDAwLzA5Ny85NTcvb3JpZ2luYWwvc3BhY2UuanBn</a>)</figcaption></figure><p>Our principle topic is PCA. It is a widely used algorithm in variety of subjects. Using it on faces makes it more human interpretable so it is one of the most popular applications. Eigenfaces is a method that is useful for face recognition and detection by determining the variance of faces in a collection of face images and use those variances to encode and decode a face in a machine learning way without the full information reducing computation and space complexity. Even though it was firstly used in 1991 by Turk and Pentland and has limitations compared to today’s tech, its basics are still highly beneficial for new practices.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*9Xzx2kmsW3zHPEGW8XrkZA.jpeg" /><figcaption>Figure #3: LFW (source: <a href="http://vis-www.cs.umass.edu/lfw/Six_Face_Panels_sm.jpg">http://vis-www.cs.umass.edu/lfw/Six_Face_Panels_sm.jpg</a>)</figcaption></figure><p>Before moving onto implementation, I’d like to give you a little introduction about basic concepts mentioned. Also, you can find full script and dataset (a small fraction of <a href="http://conradsanderson.id.au/lfwcrop/">LFW_cropped</a>) I used <a href="https://github.com/n0acar/eigenfaces">here</a>.</p><h4>Eigenvectors and Eigenvalues</h4><p>We all know how eigenvectors and eigenvalues exactly work. Maybe not exactly but we’ve heard of them, like, many times. Here is an explanation for the minority like me who keep forgetting.</p><blockquote>Let’s have a square <strong>A</strong> matrix. Almost all vectors change direction when they are multiplied by <strong>A</strong> as we expect. However, there are certain vectors <strong>x</strong> which have the exact same direction as <strong>Ax</strong>. Soo… Guess what? Those are our eigenvectors. Special ones… Even if they are in the same direction, <strong>Ax</strong> and <strong>x </strong>still<strong> </strong>might be two different vectors by their magnitudes. Here is a familiar equality. <strong>Ax</strong>=λ<strong>x</strong>. What makes those two equal is λ, our magnitude equalizer scalar, eigenvalue. This will tell us how our eigenvector changed after the multiplication</blockquote><p>From above, you can tell why they are useful in practice. For a deeper understanding and probably appreciation go to <a href="http://math.mit.edu/~gs/linearalgebra/ila0601.pdf">MIT’s Eigenvalues and Eigenvectors Chapter</a>.</p><h4>Principal Component Analysis (PCA)</h4><p>The main goal of PCA is dimensionality reduction. It has many applications in visualisation, feature extraction, data compression, etc. The idea behind it is to linearly project original data onto a lower dimensional subspace offering the principal components (eigenvectors) maximum variance of the projected data and/or minimum distortion error from the projection. Eventually, both lead to the same result which is the best reconstruction formula. As a side note, this subspace is called principal subspace.</p><p>For calculations of maximizing variance and minimizing distortion error, you can go to <a href="https://machlearn.gitlab.io/isml2018/lectures/11a_Principal_Component_Analysis.pdf">ANU’s lecture notes about PCA</a>. Also, another video on youtube called <a href="https://www.youtube.com/watch?v=kw9R0nD69OU">Principal Components Analysis -Georgia Tech-Machine Learning</a> helped me understand the concepts much better.</p><h4>Implementation</h4><p>Ok! Everyone ready for implementation. First, we’ll import the required libraries. I assume you all know what they are for. I’ll focus on the explanation part of algorithms. If you have any other questions about the code, leave a response below.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b0177d5bba6a10e9345cf6562c9f2dc4/href">https://medium.com/media/b0177d5bba6a10e9345cf6562c9f2dc4/href</a></iframe><p>Then, we need a function to visualise the images from our dataset in a good-looking way. Here it is.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6d9172a4841fa0a0d644f6ea4e38d426/href">https://medium.com/media/6d9172a4841fa0a0d644f6ea4e38d426/href</a></iframe><p>Let’s put it on our dataset <a href="http://conradsanderson.id.au/lfwcrop/lfwcrop_grey.zip">lfwcrop_grey.zip</a>. Here, I only take first 1000 of the faces because of the computational complexity but it really doesn’t matter. If you want to go full house then go for it. I also transformed images into numpy array (shaped 1000, 64, 64) for processing.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8d27c236fc036979cbd41dea5019f57b/href">https://medium.com/media/8d27c236fc036979cbd41dea5019f57b/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_H68GrB_bR27Ewq7BYpaHg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*m0Nzj5CKETxZN53eLGf1Tg.png" /><figcaption>Figure #4: Left: The people that I thought it would be nice to viualise / Right: 16 Random Famous People that I don’t actually care from the dataset of 1000</figcaption></figure><p>The table on the right is the one I will actually use. I basically visualised first 16 images from the dataset of 1000. The one on the left was a dataset that I collected from the original one when I had no idea what I was doing. Yet, I noticed it is better to have more data to explain the concepts more clearly.</p><p>Going on, the next step is to define PCA. Its arguments are X which is vectors of images and n_pc which is the number of principal components. As a little reminder, principal components define an orthonormal basis that can extract the maximum variance in the original data. In our case, its shape is (1000, 4096) since we needed to transform the images into vectors for PCA. Then, we find the mean and subtract it from our data to center it around the origin. After that, we need to perform Singular Value Decomposition on the centered data to find those principle components called eigenfaces.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/172/1*03nm4U0T2eFANZsqcYP15Q.png" /><figcaption>S matrix has the singular values along the diagonal while U and V are singular vectors</figcaption></figure><p>(SVD is difficult to get and much more complex than that. For a helpful illustration, go to that <a href="https://www.youtube.com/watch?v=cOUTpqlX-Xs">MIT OpenCourseWare video</a>.)</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/559057d75bd217fe0c2189b1e6ea7039/href">https://medium.com/media/559057d75bd217fe0c2189b1e6ea7039/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/432/1*PXMHYZGRAv8qgjfOdxqSwQ.png" /><figcaption>Figure #5: mean face</figcaption></figure><p>By the way, here is our mean face of all 1000 faces in the dataset. We are trying to find the features that makes individuals different than the others. I hope subtracting the mean face from everyone and maximising variance make more sense now. Look at that. Carefully! It’s a bit scary, isn’t it?</p><p>The next steps are to manipulate our images to become vectors, call PCA function and manipulate our eigenfaces to become images again.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6f7284254eb78b8578ae66a3d0160bb5/href">https://medium.com/media/6f7284254eb78b8578ae66a3d0160bb5/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*U14Aw-_f3esZh0tMpz3p5A.png" /><figcaption>Figure #6: Bunch of ghost shaped images. Look at them in the eyes.</figcaption></figure><p>First parameter named n_components determines how many eigenfaces we want to create. Those are our principal components from which we will recover our data that is human face. Changing it won’t change this table unless it is less than 16. However, I can guarantee there are only 50 images that we can trust rather than 1000 as it was the case at the beginning. (eigenfaces.shape = (50, 64, 64))</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a6bce84d2b00d0c9ee7b83e04d3347a3/href">https://medium.com/media/a6bce84d2b00d0c9ee7b83e04d3347a3/href</a></iframe><p>For reconstruction, we will use those eigenfaces. Each face is a weighted combination of those eigenfaces as you might say. To find weights, we should dot multiply our centered data and eigenfaces. Then, we should weight eigenfaces which gives us the centered face and add it up to the mean face again. After all, we are ready to take a look at the recovered faces compared to original ones.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*40HQ3jxyNVZ6DjYTDE2LIg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5U8XiTejA_4zJWbGQ-8Aqg.png" /><figcaption>Figure #7: n_components=50</figcaption></figure><p>Wow! Not looking good! But, don’t forget we came from 1000 x 64 x 64 to 50 x 64 x 64. In this way, we get to keep 20 times less data than before. Still, I won’t fool anybody. Those are not good enough. It is clear we still have some important principal components we took out. Let’s see what we can do. I’ll try to use the first 100 components.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*40HQ3jxyNVZ6DjYTDE2LIg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JunNa99ws7XizKbs23DC8A.png" /><figcaption>Figure #8: n_components=100</figcaption></figure><p>Getting better but still, not good enough. 250…</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*40HQ3jxyNVZ6DjYTDE2LIg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*859Kjp9q2f7GrV1SSB01bw.png" /><figcaption>Figure #9: n_components=250</figcaption></figure><p>This one is totally satisfying for me. With only quarter of what we have we recovered totally distinguishable faces. It’s totally pointless but you can actually use 1000 eigenface components and get the exact original results. Just to show you that, I’ll run some computation and space complexity.</p><p>Still running… Hold on… It’s coming.</p><p>The results have come. Pretty sweet!!!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*40HQ3jxyNVZ6DjYTDE2LIg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pT4lD2Xb5E5SwSRqNsWvDg.png" /><figcaption>Figure #10: n_components=1000</figcaption></figure><p>This post actually showed you having good enough information with very small size of data and this is one of our most important expectations from machine learning. I hope you see something beyond this application and start thinking about new innovations. I stated the references as I used or above.</p><p>Once again, hope you enjoyed my post. Don’t hesitate to ask your questions about it or give any type of feedback.</p><p>Cheers,</p><p>Nev</p><p>Give me a follow for more content and support!</p><p>Connect with me on <a href="https://twitter.com/nevacar_">Twitter</a> and <a href="https://www.linkedin.com/in/nevzat-acar/">LinkedIn</a>…</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=17606c328184" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science/eigenfaces-recovering-humans-from-ghosts-17606c328184">Eigenfaces: Recovering Humans from Ghosts</a> was originally published in <a href="https://medium.com/data-science">TDS Archive</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Training a Tensorflow Image Classification Model and Integrating It Into ios Apps Using Core ML]]></title>
            <link>https://medium.com/@nevacar/training-a-tensorflow-image-classification-model-and-integrating-it-into-ios-apps-148fe513f6e?source=rss-6b32a92a518a------2</link>
            <guid isPermaLink="false">https://medium.com/p/148fe513f6e</guid>
            <category><![CDATA[coreml]]></category>
            <category><![CDATA[tutorial]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[tensorflow]]></category>
            <category><![CDATA[image-classification]]></category>
            <dc:creator><![CDATA[Nev Acar]]></dc:creator>
            <pubDate>Fri, 27 Jul 2018 08:52:25 GMT</pubDate>
            <atom:updated>2021-08-28T10:42:05.301Z</atom:updated>
            <content:encoded><![CDATA[<p>If you are reading this article, you are probably trying to build an iOS app that can recognize what it sees. By the time this article is released, there are different ways you can make your app classify images. One of them is using Create ML that is announced at WWDC18 by Apple. This is the easiest way for iOS developers although it is only compatible with iOS 12 devices. For the older versions of iOS, one way I’ve recently used and strongly suggest is retraining a TensorFlow model called MobileNet and converting it into Core ML. You can also use the retrained model within your Android Apps.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*ptF50Eji2i2yXLLc7aVAAA.png" /><figcaption>Image from <a href="https://www.techleer.com/articles/433-now-announcing-core-ml-support-in-tensorflow-lite/">https://www.techleer.com/articles/433-now-announcing-core-ml-support-in-tensorflow-lite/</a></figcaption></figure><p>Before I begin, I must say I used <a href="https://hackernoon.com/integrating-tensorflow-model-in-an-ios-app-cecf30b9068d">another Medium article</a> written by <a href="https://medium.com/u/2300266301b3">Mohammad Azam</a>, Google Codelabs tutorial called <a href="https://codelabs.developers.google.com/codelabs/tensorflow-for-poets/#0">TensorFlow for Poets</a> and <a href="https://www.tensorflow.org/hub/tutorials/image_retraining">TensorFlow Image Retraining</a> page mainly and publications of other great developers out there. After working on plenty of resources for a while, I feel an urge to post this comprehensive tutorial for newbie developers like me.</p><h3><strong>1- Retraining the Tensorflow Model</strong></h3><p>Open the terminal. We are going to use it mainly throughout this tutorial. First of all, you need to install Tensorflow. It is tested to be working on Tensorflow 1.7. Yet, you can use the later versions at your own risk. Assuming you have pip installed:</p><pre>pip install --upgrade &quot;tensorflow==1.7.*&quot;</pre><p>I suggest you to open a desktop folder to follow the steps easily. You should download the retrain.py and the image dataset here.</p><pre>cd desktop</pre><pre>mkdir ImageClassification</pre><pre>cd ImageClassification</pre><pre>curl -LO https://raw.githubusercontent.com/googlecodelabs/tensorflow-for-poets-2/master/scripts/retrain.py</pre><pre>curl -LO http://download.tensorflow.org/example_images/flower_photos.tgz</pre><pre>tar xzf flower_photos.tgz</pre><p>We are using flower_photos from TensorFlow example images as our dataset but you can use your own data by labeling the folders in your dataset regardless of names of image files. You can see the naming format by below command or just checking your dataset.</p><pre>ls flower_photos</pre><p>As I mentioned in the introduction, we are going to retrain on top of MobileNet. For this, you should state below variables in the terminal. 224px is the largest image size we can use and 1.0 is the fraction of the largest MobileNet we will be using.</p><pre>IMAGE_SIZE=224</pre><pre>ARCHITECTURE=&quot;mobilenet_1.0_${IMAGE_SIZE}&quot;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BgKUQXcJAMLlSPV-x64loQ.png" /><figcaption>You should have something similar in your shell before training.</figcaption></figure><p>Finally, you can start retraining MobileNet with your dataset. There are heaps worth taking longer time along the way but they are not in the span of this tutorial. I suggest you to take a look at <a href="https://www.tensorflow.org/hub/tutorials/image_retraining">TensorFlow image retraining page</a> for deeper understanding. You can start the training by running the code below. This should take a couple of minutes on a regular laptop. (It took about 5 minutes on my 2015 Macbook Pro.) how_many_training_steps=500 determines the training amount. You can change it or simply remove it to set it to default 4000. More steps means more training time and more accuracy.</p><pre>python retrain.py \</pre><pre>--bottleneck_dir=bottlenecks \</pre><pre>--how_many_training_steps=500 \</pre><pre>--model_dir=models \</pre><pre>--summaries_dir=training_summaries/&quot;${ARCHITECTURE}&quot; \</pre><pre>--output_graph=retrained_graph.pb \</pre><pre>--output_labels=retrained_labels.txt \</pre><pre>--architecture=&quot;${ARCHITECTURE}&quot; \</pre><pre>--image_dir=flower_photos</pre><p>Now, you should have outcomes named “retrained_graph.pb” and “retrained_labels.txt”. Keep an eye on them. Those are important. To test your network you can use the lines below.</p><pre>curl -LO https://raw.githubusercontent.com/googlecodelabs/tensorflow-for-poets-2/master/scripts/label_image.py</pre><pre>python label_image.py \</pre><pre>--graph=retrained_graph.pb \</pre><pre>--labels=retrained_labels.txt \</pre><pre>--image=flower_photos/tulips/116343334_9cb4acdc57_n.jpg</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/320/1*wDX81z1NUJFjIhd6R251XQ.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/488/1*3Mau-M8-eWwhzgfnGIeG-A.png" /></figure><p>That concludes the training part based on MobileNet for an arbitray mobile device. Now, we can go on with converting this model into Core ML.</p><h3>2- Converting Tensorflow into Core ML</h3><p>Congratulations! It’s not easy to come this far. Next job is to convert the model you built into Core ML. In order to do so, you should install <a href="https://github.com/tf-coreml/tf-coreml">tfcoreml</a> first. The easiest way is to use pip as below:</p><pre>pip install -U tfcoreml</pre><p>However, since it is being upgraded periodically and it might not have the latest changes, it is best to clone it from github and install as below.</p><pre>git clone https://github.com/tf-coreml/tf-coreml.git</pre><pre>cd tf-coreml</pre><pre>python setup.py bdist_wheel</pre><pre>pip install -e .</pre><pre>cd ..</pre><p>At this stage, you have a working model and tfcoreml tool. Now, you need to create tf_to_coreml.py file.</p><pre>touch tf_to_coreml.py<br>open tf_to_coreml.py</pre><p>For the rest of the tutorial, we are going to use this python file. As mentioned above, we got two important outputs from retraining which are retrained_graph.pb, our network, and retrained_labels.txt, labels of that network. It’s time to use them. For starters, you should run the code below within tf_to_coreml.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9ae466c128febad2b28f0cb5fa419216/href">https://medium.com/media/9ae466c128febad2b28f0cb5fa419216/href</a></iframe><pre>python tf_to_coreml.py</pre><p>tf_model_path requires the graph model, mlmodel_path takes where the .mlmodel file is saved, output_feature_names takes the name of the last tensor which is Softmax. To see the details of your network I suggest you to <a href="https://github.com/lutzroeder/Netron/blob/master/README.md">download Netron</a>. If you follow the steps above, you don’t have to download it nor check the parameters. Still, you can have deeper understanding on your network.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vSq4ZBe6jxGTSNxbKZZCSA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*u-19NTW60__X7X6Fuo4TUQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yMOf9u_6do5MFa1ABkzBvQ.png" /></figure><p>Next, you should run the code below to arrange input and output as image and result.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bee72ac03b86aeac3b994147cb77c229/href">https://medium.com/media/bee72ac03b86aeac3b994147cb77c229/href</a></iframe><p>Finally, fully adjust the parameters by the code below.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c028acf4897ae3a73e16f917abd22a77/href">https://medium.com/media/c028acf4897ae3a73e16f917abd22a77/href</a></iframe><p>You are probably seeing an mlmodel file named flowers.mlmodel in ImageClassification folder. Open it and check if it’s the same as below screenshot. (Inputs and Outputs should be the same.)</p><pre>open flowers.mlmodel</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uvS5mOmhUOsSLmMkfnZA5g.png" /></figure><p>Everything is right. We have our inputs as images and outputs as dictionary of final results and class label.</p><h3>3- Test What You Have</h3><p>It’s finally time to import the mlmodel and see if your app can really see around. For that, I’ll use a sample app called “<a href="https://developer.apple.com/documentation/vision/classifying_images_with_vision_and_core_ml">Classifying Images with Vision and Core ML</a>“ provided by Apple. Simply, drag the flowers.mlmodel to the left just next to MobileNet.mlmodel and change the model from where you see the warning on the screenshot below. After those changes , just change the bundles and run it on a real iOS device with a camera (shouldn’t be too hard).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*V_EQ36EYqxF7CXSmTHc2fQ.png" /></figure><p>Here are the screenshots of results I obtained using my model. If you followed all the steps above, you should also see something similar. As you might have noticed, I took the photos from my computer by searching on the web. I hope you are luckier than me and have the chance to test your application in the nature.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ncIhUskupQK-ceCd3FMQfA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iISCwiqrm2bcaldKJozNow.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*K7hoTj0Dm8X62zMaZoRKgA.png" /></figure><p>Good job everyone! At the end, you have an image classification model that you trained and converted into Core ML and you can see it is actually working. I hope you enjoyed my very first post on Medium. Don’t hesitate to ask your questions about the tutorial or give any type of feedback.</p><p>Cheers,</p><p>Nev</p><h4>More where this came from</h4><p>This story is published in <a href="http://blog.usejournal.com/">Noteworthy</a>, where thousands come every day to learn about the people &amp; ideas shaping the products we love.</p><p>Follow our publication to see more product &amp; design stories featured by the <a href="https://usejournal.com/?utm_source=usejournal.com&amp;utm_medium=blog&amp;utm_campaign=guest_post">Journal</a> team.</p><p>Give me a follow for more content and support!</p><p>Connect with me on <a href="https://twitter.com/nevacar_">Twitter</a> and <a href="https://www.linkedin.com/in/nevzat-acar/">LinkedIn</a>…</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=148fe513f6e" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>