<?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 Akash Goswami on Medium]]></title>
        <description><![CDATA[Stories by Akash Goswami on Medium]]></description>
        <link>https://medium.com/@yearsofnolight?source=rss-d4cafba45f27------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*SKlK1qBbQl0oNprZxz2NNA.jpeg</url>
            <title>Stories by Akash Goswami on Medium</title>
            <link>https://medium.com/@yearsofnolight?source=rss-d4cafba45f27------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 01:18:14 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@yearsofnolight/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[Intro to image classification with KNN]]></title>
            <link>https://yearsofnolight.medium.com/intro-to-image-classification-with-knn-987bc112f0c2?source=rss-d4cafba45f27------2</link>
            <guid isPermaLink="false">https://medium.com/p/987bc112f0c2</guid>
            <category><![CDATA[classification]]></category>
            <category><![CDATA[image-recognition]]></category>
            <category><![CDATA[knn]]></category>
            <category><![CDATA[image-classification]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Akash Goswami]]></dc:creator>
            <pubDate>Thu, 08 Feb 2018 11:30:43 GMT</pubDate>
            <atom:updated>2018-08-09T08:05:10.559Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hIBFrnsRnLx-6ik4aRjy_A.png" /></figure><p>The first time I came across a camera that could detect faces, placing a square around and suggesting your age, my jaw simply dropped.</p><p>When I first used text-based image search on my smartphone, or checked out <a href="https://cloud.google.com/vision/">Google Cloud Vision</a>, I was further intrigued. I wondered about the powerful algorithms behind those systems (who wouldn’t, right?) so I started looking into the concepts around them.</p><p>I was overwhelmed by the sheer volume of prerequisite knowledge needed to understand them!</p><p>These algorithms tap into concepts from several branches of mathematics to make sense of what’s going on under the hood. Of course, you can directly start working with <a href="http://scikit-learn.org/stable/">libraries</a> which abstract most of these concepts for you, but without a basic grasp over the terrain, chances are that you will hit a dead end.</p><p>That’s why I want to address few essentials on k-nearest neighbors (KNN) algorithms for image classification — tiny baby steps which helped me understand more complex ideas.</p><p>Before we begin, let’s get the expectation right.</p><p>This write-up is mostly about me taking you on my learning journey. At the end of this, you won’t know how to make vision systems which can be used in production. You won’t be learning about SVM, softmax, gradient descent, back propagation, neural nets or anything on those lines.</p><p>But you will grow intuition on how classification problems are approached and how a machine thinks of an entity (the definition of ‘entity’ is entirely up to you; an image, a word, or some text in paragraph).</p><h3>What is image classification?</h3><p>We can assume a blackbox function which takes an animal as image and returns the text label of the animal.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/564/1*D1-uXpUbajKEL7_kmh6p0g.png" /></figure><p><em>(You might say, that’s absurd! How can I possibly take a cat as input? Hold on to your horses, I’ll literally show you how to take a cat in a function. You might also ask how a function can associate a label with a shape. You will have to wait to get the answer, because if I explain now you won’t get the full picture.)</em></p><p>This, in a nutshell, is image classification. The mind-blowing system which detects faces or suggests age or powers Google Cloud Vision API, implements computer vision tasks (such as object detection or segmentation) which basically boils down to image classification.</p><p>I’m explaining image classification with KNN because this is one algorithm that needs almost no prerequisites. So, the cognitive part of your brain does not have to worry about too many unknowns.</p><h3>What is KNN algorithm?</h3><p>Let’s start with a new problem here:</p><p>If two sets of points are given on an XY plane, with one set colored in green and other set colored in red, what would be the color of the any new point added on the same XY plane?</p><p>Here is the setup:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jIUD8_1tZ1bQP8LsZgElKg.png" /></figure><p>Assume that the colored points belong to two separate distinct groups. Now intuitively, we know if the new point leans towards the bottom-left side of the canvas, it probably belongs to the green (or shades of green) group, otherwise it would be in the red (or shades of red) group.</p><p>In a way you are searching for nearest neighbors. Simple, isn’t it? And that is what KNN is, from a human intuition perspective.</p><p>However, the rub is in the following line:</p><blockquote><em>leans towards the bottom-left side of the canvas</em></blockquote><p>Computers don’t understand contextual information of this kind. We have to explicitly quantify the meaning of ‘bottom-left’ and pass it on to the program. To quantify this, we use Euclidean distance between two points, described as the following:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*l3ANdhVKO81V4TILjE0aQg.png" /></figure><p>This equation is generalized to cover any dimension. For 2D space, the equation reduces to the following:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7IJ7X3kECLGpP1FD0CD5gA.png" /></figure><p>This is the same as:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2_UHWSD4gNMoRK827l8f9g.png" /></figure><p>We are all familiar with the last equation — it’s the distance between two points on an XY plane. Understanding the output of these equations is a cakewalk for our computer program, so once we have this tool, the rest of the algorithm is pretty intuitive.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NaoZSp_8G8CvKnStRY-N0A.gif" /></figure><ul><li>Calculate the distance between the new point and every other point</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sTOtQUDps9Yx0dElg8da8g.gif" /></figure><ul><li>Sort the distances</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4wWC77iV3yXHxw9fdOlMdg.gif" /></figure><ul><li>Pick K minimum distances from the list of sorted distances (thats why it’s called <strong>K</strong>-NN)</li><li>Use majority voting to get the color of the point</li></ul><p><a href="https://adotg.github.io/knn-what-how-why/">Here is the link of the full interactive demo of the algorithm.</a></p><h3>Image classification intuition with KNN</h3><p>Each point in the KNN 2D space example can be represented as a vector (for now, a list of two numbers). All those vectors stacked vertically will form a matrix representing all the points in the 2D plane.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*G6YSFc1_MhNonrF43hLA5A.png" /></figure><p>On a 2D plane, if every point is a vector, then the Euclidean distance (scalar) can be derived from the following diagram:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pukgSbCkSYTR0bDbp0GnGQ.png" /></figure><p>This, essentially, is geometrical representation of the equation we discussed in the last section. Here’s a quick recap:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2_UHWSD4gNMoRK827l8f9g.png" /></figure><p>and</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7IJ7X3kECLGpP1FD0CD5gA.png" /></figure><p>Now think of a <em>32 x 32</em> cat image.</p><p>We have a total 32 x 32 = 1024 pixels. So, if we have to represent an image using a structure which the computer can understand, we will have a large vector that’s 1024 element long.</p><p>But what will be the value of the numbers? Of course, the color which the pixel holds.</p><p>A pixel’s color can be represented using 3 color values (assume RGB color channel) so a pixel holds a list of 3 values (1 value for r, 1 value for g and 1 value for b of RGB channel). We code this in a similar structure, but we create 1024 values of r, followed by 1024 values of g, followed by 1024 values of b. That’s a total of 3072 values in a vector.</p><p>If we have no more than two animals then we have vector length of 3072 stacked vertically. The matrix would look like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*M4IPEaCMMOVLeHQuIMk4MQ.png" /></figure><p>Now this is the answer to the question asked earlier</p><blockquote>How can I possibly take a cat as input?</blockquote><p>This is how we can pass a cat into the function. And we are doing all this because obviously, a program does not know how to handle a cat. That’s why we’ve encoded the cat in a way that our program can understand.</p><p>A point represented in 2D plane has 2 components in its vector. Similarly, the image that we see in 2D is a point in higher dimensions. The beauty of linear algebra is that it scales the idea which we apply in 2D to higher dimensions (hence procedure for calculating Euclidean distance for higher dimensions is still valid).</p><p>Just like we grouped (clustered) our points in 2D space, grouping (clustering) can be done in higher dimensions also where image is represented as a point. So the cat image should cluster around some specific point also (it’s hard to visualize, but you can think the idea in a lower dimension and scale it up to a higher dimension).</p><p>Once we are comfortable with this idea, we can comfortably apply KNN for the image, just like we had done it for our 2D points.</p><p>As you may have noticed, the setup for our 2D problem statement already had some points in place before a new point added for classification. The same concept scales for classifying images too. There are image points in higher dimensional space which are labelled as cat, dog etc. similar to categorization of green and red.</p><p>And this is the answer of other question we asked earlier:</p><blockquote>How can a function associate a label with a shape</blockquote><p>We pass an encoded shape (cat, dog, etc.) and tell the program this is ‘cat’, this is ‘dog’. The program represents images as points on the higher dimensional space and assigns them a label. This is called <em>training the model</em>.</p><p>Once training is complete, we pass test data to the model to predict the animal, just like we gave a new point in the 2D space to determine its color.</p><h3>Food for thought</h3><p>If high dimensionality intrigues you, t<a href="https://mathoverflow.net/questions/25983/intuitive-crutches-for-higher-dimensional-thinking">his is what you might want to read</a>. Here is my one of many shower thoughts</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bfJ0IxCxWxe4EIKXmph5uw.jpeg" /></figure><h3>Practical</h3><p>Enough with theory, it’s now time to get hands on.</p><p><a href="https://github.com/akash-goswami/knn-what-how-why">You can find all the code here</a>. I used snippets from <a href="https://github.com/akash-goswami/knn-what-how-why/blob/master/knn.ipynb">this notebook</a> to explain the practical workflow. The notebook explain most of the process, so I won’t repeat it here.</p><p>I will, however, add the missing links which are not mentioned or concepts that are not explained.</p><h4>Preparing the data</h4><p>When I write a classifier/regression function, I prepare my own data in 2D or 3D space. Yes, it takes a bit of time but it helps you debug really fast.</p><p>Debugging a model with real training and test data is a nightmare. Of course creating my own data won’t make this program bug free, but this approach ensures the rudimentary bugs could be found fast and fixed timely.</p><p><a href="https://github.com/adotg/knn-what-how-why/blob/develop/gen_points.py">The code for preparing the data is here</a>.</p><p>The idea is simple: create cluster centers based on the number of classes you want to create. Randomly generate more points around the cluster center, scaled by a predefined standard deviation.</p><p>Return value of this function is (X, y). X contains vectors stacked vertically and y is the color of the point. Every item in the stack is a point on XY plane.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*G6YSFc1_MhNonrF43hLA5A.png" /></figure><h4>Visualize</h4><p>Visualize the points on the plane to check if the clusters are created correctly.</p><h4>Algorithm</h4><p><a href="https://github.com/adotg/knn-what-how-why/blob/master/knn_algo.py">The actual KNN algorithm is here</a>.</p><p>I used numpy arrays to perform the distance calculation. If you don’t understand in one shot, that’s okay. What I do when I don’t understand a program, is to execute each line in Python REPL to see what the input is and what’s the output. This helps understand why it was done this particular method.</p><p>It takes some time, but it really helps you build a solid concept. And every so often, I find useful functions / transformations / operations which reduces 5–10 lines of code!</p><h4>Test</h4><p>Test the algorithm with our custom generated data.</p><h4>Training set, Validation set, Test set</h4><p>I used data from <a href="http://www.cs.toronto.edu/~kriz/cifar.html">CIFAR-10</a> which is a small dataset of 10 classes. This dataset is already segregated into training set and test set. However, we should create a validation set from training set using the slicing ratio 1:9 (if training set had 1k rows post slicing 100 rows goes in validation set 900 rows goes in training set).</p><p>And we need this validation set to tweak our hyperparameter (parameter which are not derived from learning process but is a part of learning process) — K.</p><p>Note that I’ve used training set and test set only for illustration purpose in the codebase.</p><h4>Outro</h4><p>The rest of the process is very simple and sufficiently explained in <a href="https://github.com/akash-goswami/knn-what-how-why/blob/develop/knn.ipynb">the notebook</a>.</p><p>But as you can see, the accuracy of the algorithm is not something we can be too proud of. What’s interesting is that even the <a href="http://scikit-learn.org/stable/modules/generated/sklearn.neighbors.KNeighborsClassifier.html">state of the art implementation</a> of the algorithm produces similar results — clearly, we still have a long way to go, once our bases are covered.</p><h3>Further Reading &amp; Ref</h3><p><a href="http://scikit-learn.org/stable/modules/neighbors.html">1.6. Nearest Neighbors</a></p><p><a href="http://vision.stanford.edu/teaching/cs231n-demos/knn/">http://vision.stanford.edu/teaching/cs231n-demos/knn/</a></p><h3>Codebase</h3><p><a href="https://github.com/adotg/knn-what-how-why/">adotg/knn-what-how-why</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=987bc112f0c2" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Visualize git diff]]></title>
            <link>https://yearsofnolight.medium.com/visualize-git-diff-6247c458c0be?source=rss-d4cafba45f27------2</link>
            <guid isPermaLink="false">https://medium.com/p/6247c458c0be</guid>
            <category><![CDATA[git]]></category>
            <category><![CDATA[dataviz]]></category>
            <category><![CDATA[github]]></category>
            <category><![CDATA[data-visualization]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Akash Goswami]]></dc:creator>
            <pubDate>Sun, 28 May 2017 15:26:54 GMT</pubDate>
            <atom:updated>2018-08-09T05:41:01.965Z</atom:updated>
            <content:encoded><![CDATA[<h4>With <a href="https://www.research.ibm.com/visual/projects/history_flow/">History flow</a></h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*O2MEYx5WY0ubLRPTmaMSZw.png" /></figure><h4><strong>TL;DR</strong></h4><p>This <em>viz</em> (visualization) is output of performing git diff across all the commits of a file since the beginning of the time. The viz technique used here is <a href="https://www.research.ibm.com/visual/projects/history_flow/">History flow</a> by IBM Collaborative User Experience Research Group.</p><blockquote><strong><em>history flow</em></strong> is a tool for visualizing dynamic, evolving documents and the interactions of multiple collaborating authors.</blockquote><p>You can find the examples <a href="https://adotg.github.io/git-history-flow/"><strong><em>here</em></strong></a><strong><em>, </em></strong>concept of History flow <a href="https://www.research.ibm.com/visual/projects/history_flow/explanation.htm"><strong><em>here</em></strong></a>, and code <a href="https://github.com/adotg/git-history-flow"><strong><em>here</em></strong></a>.</p><h4>TS;WM</h4><p><a href="https://en.wikipedia.org/wiki/Git">Git</a> is used as version control system across all the major projects around the world. It has been there all the time witnessing the project evolved. It tracks every single file of the project , remembers all lines of code removed, updated and added. It knows all the things that one could possibly do with a project.</p><p>Git has plenty of powerful commands which lets you do all the right things you ever wanted to do. For example git status is probably the most used command in any circumstances. What follows git status is the monster git diff. Often times use of git diff remains at the surface level where the current change is compared against the previous change. But like I said, its a monster, this little command can be used to retrieve your entire file history with ownership information. When all these versions are placed side by side, it’s more like a tale that narrates what happened to a file. And if there exists a file(s), which represent the whole project, then this command narrates story for the whole project.</p><p>For example package.json in any node / javascript project, can be deemed as representative file of the project. It has field for version, author information, dependencies, scripts; almost everything which defines a project at global level. On the other hand a project takes iterations and time to evolve or become stable. If package.json is the representative file of the project then this should also reflect the process of evolution. Hence this viz retrieves all the version of the file and charts in front of us.</p><p>Essentially git diff is flow of changes over time when its performed over the lifetime of a file. While I was thinking what kind of viz could be meaningful to address this use case, I came across <a href="https://www.research.ibm.com/visual/projects/history_flow/">History flow</a> by IBM Collaborative User Experience Research Group.</p><blockquote><strong><em>history flow</em></strong> is a tool for visualizing dynamic, evolving documents and the interactions of multiple collaborating authors.</blockquote><p>Its a perfect match. So in a nutshell git diff is applied on a particular file since the beginning of the time and the result is dumped as an input to render the viz. No rocket science there. Please read the <a href="https://www.research.ibm.com/visual/projects/history_flow/explanation.htm">concept of History flow</a> before proceeding. Its super easy, intuitive and important.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*krXg2_lVFMgt13m838ooPQ.png" /></figure><p>This how package.json of <a href="https://github.com/facebook/react">React</a> looks like when the commits spaced by timestamp.</p><p>Each commit is represented by thin colored lines. Two consecutive commits are connected to represent the changes from one commit to another commit. Now a particular commit consists of many contribution hunks. You can think hunk as chunk of code added or updated in a commit. A color is assigned to a contributor. Hence a hunk in one commit line gets color property from the contributor who is responsible for creating the hunk. Hence a commit line becomes multi-colored.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*clEDJROHaJp6mIVDfz0bDg.png" /></figure><p>This ugly example has only 5 commits. The first commit is obviously the creation of the file by a single contributor. In the second commit another guy came and made some changes. Its not the same guy (contributor) because a new color has appeared from the second commit. Now there is also flow between commits. You can think of flow as position mapping of chunk of content from previous version to current version. The transparent area between flows the gap. A convergent gap indicates deletion of content. A divergent gap indicates addition of content.</p><p>The below picture shows the commits in <a href="https://github.com/d3/d3">D3</a> package.json file spaced by time.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dzGaqRtsJt7J-C4omV4sMw.png" /></figure><p>The thin faded grey vertical lines are extension of commit lines and are for reference. Every commit gets a line like this. The commit lines are of various length based on the size of the file, but the length of extension line along with a commit line is always same across all the commit instance. Because of this, extension lines together shows the distribution of the commits over time in a better way. This distribution is meaningful only when the commits are spaced by timestamp. It can be inferred from the picture that the maintenance of the project(commit distribution) was decently distributed apart form few exception (towards the right less clustering of the reference lines).</p><p>The current implementation supports spacing of commits in equal distance (ordinal axis) or by time. <strong>Community View </strong>and <strong>Latest Commit</strong> are the two modes supported in each choice of spacing. Community view mode shows all the users contributions as an overview by applying same level of color encoding. But in Latest commit mode the latest hunks in the commits are highlighted by fading the rest of the hunks.</p><p>Below space describes all these combinations to package.json for <a href="https://github.com/npm/npm">NPM</a> repo.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NQuqNv1YOhvIUEcbaApgSQ.png" /></figure><p><strong>Equally spaced commit with community view. </strong>All the commits are spaced at equal distance ignoring the timestamp. This particular instance conveys some commit pattern for the file. Like, upto some extent only one committer (blue color) was predominantly contributing. Post almost half of the total commits other contributors started to make active contribution. Another observation, there are only few colors which are visible conveying that only handful of people are allowed to maintain the project. See the interactive version <a href="https://akash-goswami.github.io/git-history-flow/ghf-npm/">here</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0cTj67miFNgZDbiiiUD4NQ.png" /></figure><p><strong>Equally spaced commit with latest commit view. </strong>Only the latest hunks in a commit are vibrantly colored while the other older hunks fades away. This kind of gives the overview of how big the change was. This instance of the example conveys major rewrite of dependency happened thrice (3 large blue lines) at the initial stage of the project.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NxAIYaT6-mH-O0Rhx5KzbA.png" /></figure><p><strong>Time spaced commit with community view. </strong>As mentioned previously, the commits are placed based on timestamp. This view gives an overview on how frequently the file / repo is maintained or updated. In this particular example the commits are made at almost regular interval. The high clustering of grey the lines denotes the significant numbers of commits for a unit time.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Fy-ig-u2_Ed_tku7Wd8PkA.png" /></figure><p><strong>Time spaced commit with latest commit view. </strong>The latest commits are spaced by the time the commits are done in this view. One interesting observations from this view when package.json is the subject file is the release cycle. Look the top of the image, the small dots of blue and red forming a line actually tells us the version update of the project. For NPM the release is very consistent.</p><p>This viz does not tells anything about a file / repo for certain. For example if a file gets less / no change over time, one might infer that the it has not got much contributions and at the same time other might infer that the module is stable and requires no changes. Its all contextual. Again since the beginning I have assumed that package.json is the representative file for a node project, which does not have to be true all the time. This viz itself has few assumptions, which if not met, might convey wrong information. Like it viz uses pallet of 24 colors. If the number of contributors exceeds more than 24, already assigned colors are allotted for a new user. Hence from a viz perspective you wont be able to distinguish more than 24 contributors.</p><p>You can browse all of the examples <a href="https://adotg.github.io/git-history-flow/"><strong><em>here</em></strong></a>. You can browse the code <a href="https://github.com/adotg/git-history-flow"><strong><em>here</em></strong></a>. Let me know if you want to know about the code that powers the viz.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6247c458c0be" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>