<?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 Agnihotri on Medium]]></title>
        <description><![CDATA[Stories by Akash Agnihotri on Medium]]></description>
        <link>https://medium.com/@agniakash25?source=rss-7b80f2303030------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*6v0ovqg5TAb10jrFATvn_w.jpeg</url>
            <title>Stories by Akash Agnihotri on Medium</title>
            <link>https://medium.com/@agniakash25?source=rss-7b80f2303030------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 17:52:39 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@agniakash25/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[Chaos Driven Development]]></title>
            <link>https://agniakash25.medium.com/chaos-driven-development-652734c221e1?source=rss-7b80f2303030------2</link>
            <guid isPermaLink="false">https://medium.com/p/652734c221e1</guid>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[technology]]></category>
            <category><![CDATA[india]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[policy]]></category>
            <dc:creator><![CDATA[Akash Agnihotri]]></dc:creator>
            <pubDate>Wed, 06 Aug 2025 10:31:10 GMT</pubDate>
            <atom:updated>2025-08-06T10:31:10.315Z</atom:updated>
            <content:encoded><![CDATA[<h3>How Chaos Shaped Indian Tech — and the Next Leapfrog</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*4hyXrNV8X6sVfyIb" /></figure><h3>An Industry Born from Chaos</h3><p>As the twentieth century drew its curtains, many waited anxiously for a disaster they were told was imminent. They pictured modern civilisation collapsing: planes dropping from the sky, stock markets freezing, satellites falling back through the atmosphere. Y2K was not foretold by any Mayan calendar, yet to many it felt like an end‑of‑civilisation omen, born of a trivial software bug.</p><p>For decades, dates were stored as six miserly digits — YYMMDD on screens and tapes. When the clock rolled over00, the software read <strong>1900</strong> instead of <strong>2000</strong>. Finance systems packed those digits in COMP-3 to save bytes; PCs held the year in BCD and relied on the BIOS to bump a separate century byte from 19 to 20. Miss any step and you time‑travel to 1900.</p><p>Then came the century traps: code that only checked “divisible by 4” mislabelled <strong>1900</strong> as a leap year, while code that banned all century years forgot the “/400” exception and rejected <strong>2000‑02‑29</strong>. Put together, an ordinary midnight threatened to scramble interest accruals, expiries, batch jobs, and settlements — unless a vast, coordinated patch landed first.</p><p>When the world realised it was sitting on a ticking “time” bomb, it needed an army of engineers to comb through millions of lines of COBOL and mainframe code. That army was found in Bengaluru: newly minted, English‑proficient graduates working around the clock. While people in the West slept, these teams ran 24×7 shifts, delivering at scale and a fraction of on‑shore cost.</p><p>Their effort worked. Y2K never became the catastrophe many feared. As the century turned with calm and celebration, an industry stepped out of the chaos, an industry known for reliability at scale, sharp cost discipline, and an army of battle‑tested engineers.</p><h3>The Mobile Leap</h3><p>Progress isn’t always linear. Much of the West moved from cash to cards and is only now rolling out mobile wallets at scale, while India vaulted straight from cash to phone‑first payments. QR codes are ubiquitous — from vegetable vendors to street‑side stalls, routing payments over UPI. In 2023, the world logged <strong>266.2 billion</strong> real‑time transactions; in 2024, <strong>UPI alone carried ~172 billion</strong>, underscoring India’s outsized share and mobile‑first lead. (<a href="https://investor.aciworldwide.com/news-releases/news-release-details/global-real-time-payments-growth-sustainable-new-use-cases-push">source</a>).</p><p>While much of the world moved from rotary‑dial phones to landlines and only then to mobile, many Indians skipped straight to smartphones — often with a SIM that offered 1–2 GB of daily data and unlimited calls. Overnight, hundreds of millions came online, fuelling demand for local language content, fintech and microfinance, and rural agritech, and seeding a startup ecosystem solving local problems with global‑grade technology.</p><h3>The Art of Jugaad</h3><p>Growing up in Kolkata, my undergraduate college was about 3–4 km from the nearest bus stop, with almost no public transport. What we did have was a cobbled‑together carriage: a flat wooden plank bolted to what looked like a motorcycle frame, with two rear wheels to create the illusion of stability. It carried six to eight students at a time, perched around the plank, legs dangling like loosely strapped cargo.</p><p>“<strong>Jugaad</strong>” (frugal improvisation) has long powered Indian innovation. In a country that has often grappled with patchy power supply, scarce high‑quality parts, and tight budgets, jugaad becomes the silent co-founder response to modern homegrown innovations, including, on occasion, transporting engineering students.</p><h3>Deep Tech Has No Shortcut</h3><p>In January 2025, DeepSeek R1 landed and sent the Western AI community into a loop of self‑reflection, denial, and grudging acceptance: China isn’t just keeping pace — it may yet outpace the incumbents. As the world argues over superintelligence and shifting power dynamics, one Indian startup quietly dominated its category with <strong>80%+</strong> market share and over <strong>four million</strong> paying users. Is it an AI lab or a social‑media juggernaut? Neither. It’s an <strong>astrology app</strong>, letting people read their future, commission rituals in their name, and receive blessings digitally, all frictionlessly powered by online payments.</p><p>The trouble with deep tech is that you can’t <strong>jugaad</strong> or leapfrog your way to it. It takes years of disciplined research, patient capital, and institutions staffed by people dedicated to discovery. Moving from a working prototype to the extra 5–10% performance that makes a product commercially viable demands sustained investment and talent. You can’t replicate <strong>NVIDIA</strong> or <strong>OpenAI</strong> the way Indian startups replicated “the Indian Uber” or “the Indian Netflix.” <strong>Intel</strong>, after decades as the undisputed leader in chips, lost its crown, failing to anticipate the AI wave, among other missteps, and now struggles for relevance in a world that has moved on.</p><h3>Building for a Billion</h3><p>Humans aren’t good at visualising very large numbers. We may grasp a <strong>billion</strong> in the abstract, but few of us can feel its scale. A billion people stretch governance, urban planning, and healthcare models until many simply <strong>break down</strong>, prompting talent to seek calmer waters. Silicon Valley’s rise was partly powered by Indian brains abroad. Yet with a base that large, even <strong>0.1%</strong> choosing innovation and nation‑building yields <strong>1,000,000</strong> committed people — bigger than many cities. With that depth of talent, it’s hard for India to stay behind the curve for long.</p><p>How India — and Indians — choose to tackle uniquely Indian problems will define the next era of Indian tech. Will the next leapfrog be <strong>AI‑driven, low‑cost healthcare at scale</strong> for a billion people? <strong>Autonomy for chaotic roads</strong> that makes self‑driving work in Indian conditions? Or <strong>digital co‑pilots for governance</strong> that ease the burden on an overworked bureaucracy, judiciary, and public services? The choices we prioritise now will set the trajectory for decades.</p><p><strong><em>“In India, the choice could never be between chaos and stability, but between manageable and unmanageable chaos… between tolerable and intolerable disorder.”</em></strong><em> — Ashis Nandy</em></p><blockquote>This was originally posted on <a href="https://akashagnihotri.substack.com/p/chaos-driven-development"><strong>SubStack</strong></a></blockquote><blockquote><em>You can follow me on </em><a href="http://www.linkedin.com/in/agni25"><strong>Linkedin</strong></a></blockquote><blockquote><em>You can read my other articles on </em><a href="https://agniakash25.medium.com/"><strong>Medium</strong></a></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=652734c221e1" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Demystifying Machine Learning Engineering: Bridging the Gap Between Data and Solutions]]></title>
            <link>https://agniakash25.medium.com/what-is-a-machine-learning-engineer-and-how-to-become-one-1b3be3958ddc?source=rss-7b80f2303030------2</link>
            <guid isPermaLink="false">https://medium.com/p/1b3be3958ddc</guid>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[guides-and-tutorials]]></category>
            <category><![CDATA[engineering]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[software-development]]></category>
            <dc:creator><![CDATA[Akash Agnihotri]]></dc:creator>
            <pubDate>Mon, 27 Feb 2023 16:36:15 GMT</pubDate>
            <atom:updated>2023-02-27T16:36:15.872Z</atom:updated>
            <content:encoded><![CDATA[<h4>What is a Machine Learning Engineer and how do you become one?</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1023/1*OZmxUimlHlBtFaEpypvuTw.png" /><figcaption>A Machine Learning Engineer Digital Art (Generated using DALL-E)</figcaption></figure><blockquote>“The rise of powerful AI will be either the best or the worst thing ever to happen to humanity. We do not yet know which.” — Stephen Hawking</blockquote><p>As long as I have been an MLE people have always asked me what it means, in one form or another, everyone has their own interpretation as the field is still evolving with new ideas like MLOps coming to light. After being one for the last 5 years I think I have some qualifications to try and answer that question.</p><blockquote>“Are you like a data scientists who can code?”</blockquote><blockquote>“How are you different than a software engineer?”</blockquote><blockquote>“What does your day at work look like?”</blockquote><blockquote>“What skills do I need to learn to become one?”</blockquote><p>So through this article, I would like to answer some of these questions and provide a basic overview of what is a MLE and what skills you need to become one. Let&#39;s go...</p><h3>What is a Machine Learning Engineer?</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/501/1*cs0CT2wM4DLovHcqbBsJkw.png" /><figcaption>MLEs reside at the center of multiple skill sets. (Image by Author)</figcaption></figure><p>A Machine Learning Engineer sits at the center of 3 key domains; Software Engineering, Data Science, and System Engineering. Now, this does not mean we are experts in all these trades but that we can combine them to build products that are reliable, consistent, and resilient.</p><h4>From Ideation to Production</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yD0jC7sHDDV3zSgxXVycig.png" /><figcaption>Image shows various stages of ML Product Development and roles associated with them (Image by Author)</figcaption></figure><p>As shown in the above image, ML product development needs multiple roles to work together to deliver a great product, let us take a closer look at each of these stages and the roles associated with them.</p><ul><li><strong>Ideation</strong>: Define the problem you want to solve and how it adds value.<br><strong><em>- Data Scientists</em></strong> work with businesses to define what&#39;s possible and how it can be used to solve the problem.</li><li><strong>Data Collection:</strong> The team collects &amp; identifies relevant data sources keeping data privacy in mind.<br><strong><em>- Data Scientists</em></strong> define what kind of data is needed and its attributes.<br><strong><em>- Data Engineer</em></strong>s identify the relevant data sources and bring them to the staging layer.</li><li><strong>Pre-processing</strong> the data, for example joining multiple data sources into semantic tables for easier consumption.<br>- <strong><em>Data Engineer</em></strong> plays the key role here by making sure data is ready to use and pipelines are compliant.<br>- <strong><em>Data Scientists</em></strong> make sure the transformations applied do not create biased data sources and meet the requirements.</li><li><strong>Feature Engineering</strong>: The team uses the raw data to build features that are informative and useful for the model.<br>- <strong><em>Data Scientists</em></strong> build features that could feed useful information to the model, they also make sure these features are nonbiased.<br>- <strong><em>Machine Learning Engineers</em></strong> here are to make sure that feature transformations are reproducible, modular, and scalable in nature.</li><li><strong>Model Selection &amp; Tuning:</strong> this is when the team starts building the model and tuning it to produce high-value results.<br>- <strong><em>Data Scientists</em></strong> experiment with an array of models to find the one that fits their needs and use hyperparameters to tune the results.<br>- <strong><em>Machine Learning Engineers</em></strong> are the ones building these models sometimes from scratch or by implementing existing algorithms.</li><li><strong>Model Integration:</strong> the team builds APIs that can deliver model results seamlessly across the organization&#39;s infra.<br>- <strong><em>Machine Learning Engineers</em></strong> play the main role here by optimizing and building training, and inference pipelines that can be consumed by these APIs<br>- <strong><em>Software Engineers</em></strong> implement these APIs to make sure they adhere to industry standards.</li><li><strong>Model Deployment: </strong>At this stage, the team deploys its product across multiple stages like DEV, UAT, and PROD.<br>- <strong><em>Machine Learning Engineers</em></strong> implement a reliable MLOps framework which would allow the team to monitor training and results over time.<br>- <strong><em>DevOps Engineers</em></strong> are useful in this stage as they can help implement a string CI/CD system to automate these processes.</li><li><strong>Model Monitoring:</strong> The MLOps framework built allows the team to monitor the results and make any necessary changes.<br>- <strong><em>Machine Learning Engineers</em></strong> push new features to make the system more stable and keep the results consistently over time.<br>- <strong><em>DevOps Engineers</em></strong> can help build more automation at this stage.</li></ul><h3>Machine Learning Engineering Skill Set</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/819/1*HsyBWw65KDGzxoFvj0G-cQ.png" /><figcaption>The skill set of a Machine Learning Engineer. (Image by Author)</figcaption></figure><p>As a Machine Learning Engineer, you will need to have a good understanding of various technologies to build and deploy machine learning applications.</p><ol><li><strong>Programming Languages</strong>: You need to have a good understanding of programming languages such as Python, R, and SQL to build machine learning models and data pipelines.</li><li><strong>Machine Learning Libraries</strong>: You would need to know machine learning libraries such as scikit-learn, TensorFlow, and PyTorch to build, train, evaluate, and deploy machine learning models.</li><li><strong>Data Science Libraries</strong>: Although exploratory analysis won’t be a big part of you should also be familiar with data science libraries such as NumPy, Pandas, and Matplotlib to preprocess, analyze, and visualize data, this would allow you to play a bigger role in smaller firms.</li><li><strong>Big Data Technologies</strong>: You need to have experience with big data technologies such as Hadoop and PySpark to handle large volumes of data needed to train these models.</li><li><strong>Platform &amp; System Engineering</strong>: You should be comfortable with cloud computing platforms such as AWS, Google Cloud, and Azure to deploy models on the cloud.</li><li><strong>Containerization Technologies</strong>: Such as Docker and Kubernetes to package and deploy machine learning applications would give you an edge, as many companies today face challenges with deploying models on existing infra.</li><li><strong>DevOps Tools</strong>: You should be familiar with DevOps tools such as Git, CircleCI, and Azure DevOps to automate the build, test, and deployment pipelines.</li><li><strong>Web Frameworks</strong>: This one is optional but in my experience knowing how to build APIs to deliver results of your model gives you an edge over others, learning FastAPI, Flask or Django would go a long way for your career.</li></ol><p>Keep in mind these are skills people develop over many years and no one expects you to know everything at the start of your career.</p><h3>Accidental Engineers</h3><p>Most machine learning engineers I have met including myself were once aspiring researchers or data scientists who over time realized that they have the soul of an engineer and enjoy building and deploying models more than experimenting on notebooks. But the role itself has existed since the later 50s with an evolving title.</p><ul><li>Statistics Engineer</li><li>Knowledge Engineers</li><li>AI Programmers</li><li>Quantitative Engineers</li></ul><p>All these have shared some common attributes with Modern MLEs, as they all sit closer to researchers and help engineer their ideas into reality. This is the key to being a successful MLE a strong understanding of Data Science should be the foundation of any Machine Learning Engineer as they should be able to develop new algorithms or customize existing ones to solve unique problems.</p><h3>Conclusion</h3><p>Names and labels might keep changing over time but the fact is mad scientists would always have minions by their side turning blueprints into reality.</p><h4>If you enjoyed the article.</h4><blockquote><em>Leave a comment to let me know what you think.</em></blockquote><blockquote><em>Connect with me over </em><a href="https://www.linkedin.com/in/agni25/"><em>Linkedin</em></a><em>, </em><a href="https://agniakash25.medium.com/"><em>Medium </em></a><em>or my </em><a href="http://www.akashagnihotri.com/"><em>Website</em></a></blockquote><blockquote><em>Share the article with your network.</em></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1b3be3958ddc" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[ChatGPT — A New Era]]></title>
            <link>https://agniakash25.medium.com/chatgpt-a-new-era-4b8f6533b866?source=rss-7b80f2303030------2</link>
            <guid isPermaLink="false">https://medium.com/p/4b8f6533b866</guid>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[chatgpt]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Akash Agnihotri]]></dc:creator>
            <pubDate>Tue, 07 Feb 2023 17:02:19 GMT</pubDate>
            <atom:updated>2023-02-07T17:02:19.495Z</atom:updated>
            <content:encoded><![CDATA[<h3>ChatGPT — A New Era</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UpPHjpPf05eP4PURvY9gYw.png" /><figcaption>A lonely robot miner in space in Van Gogh Style (Generated by DALL-E 2)</figcaption></figure><blockquote>“Open the pod bay door HAL” — Dave</blockquote><blockquote>“I am sorry Dave. I am afraid I can’t do that” — HAL</blockquote><p>With the hype that ChatGPT has generated, one could be forgiven to fear an AI apocalypse on the horizon. But let us keep aside that discussion for movies and History Channel let us look objectively at the impact of ChatGPT on our tech and society. We must look at history to find examples of similar breakthroughs and their impact, we must evaluate objectively what it means for job markets and society.</p><h3>From the Horse’s Mouth.</h3><p>It’s the late 19th century and automobiles are a rising trend all around the globe, but still, most of the world depends on horses for transportation, let’s hear what is mood among the horses association about this new magic machine.</p><p>One group of horses thinks this is just another fancy tool that will be forgotten as it can never serve millions of people all over the world, and the fuel it uses is rare to find.</p><p>Another group of horses thinks if anything it means more jobs for horses and better jobs for them, the machines would take over more boring jobs like plowing the field or running water pumps.</p><p>The third group is more worried than anyone else and is not taken seriously by most. They worry that this machine would take over their jobs and they would be limited to a ceremonial attraction in parks.</p><h3>Don’t be a Luddite.</h3><p>The Luddites were a secret oath-based organization of English textile workers in the 19th century who formed a radical faction that destroyed textile machinery. They feared that the machines would eliminate the job markets and leave them and their families starving.</p><p>When cranes were adopted across ports the workers went on strike to stop them from taking over their jobs. Similar strikes happened across India when computers were introduced into government services.</p><p>“The real problem of humanity is the following: We have paleolithic emotions, medieval institutions, and godlike technology. And it is terrifically dangerous, and it is now approaching a point of crisis overall.” — Edward O. Wilson</p><h3>What is ChatGPT</h3><p>It is a very powerful and well-trained language model. It is trained on a lot of data and can combine that learning to provide custom answers to our questions. As it has been trained on multiple forms of text data like songs, stories, code, wiki, and virtually all forms of text available on the internet it can combine this information to generate custom solutions to our questions.</p><p>As powerful and all-knowing as it might feel, it has its limitations. For one, it has no or very limited knowledge of the world after its training and therefore it is not something you want to use for your morning news. Everything it produces is plagiarized because it’s all derived from data it was trained on, it only combines it in a useful way to maximize the reward function.</p><h3>The Possibilities</h3><p>Never in human history has a new tool caused the total number of jobs to go down. Yes, when the telephone came the telegram industry became extinct when the automobile came horse carriages became a decoration on the wall, but it did not reduce the total number of jobs in the world, it only accelerated the pace of human expansion. Turning itself into the factory of the world allowed China to raise billions of people out of poverty and when manufacturing moved out of the USA it turned itself into the hub of innovation and entrepreneurship creating some of the biggest industries in the world.</p><h3>The Game of Scale.</h3><p>Google became the leader in search engines not because it has the most sophisticated search algorithm, but because it has the most indexed pages in the world. The same would be true for upcoming language models, it’s not about who produces the clearest and natural output, it is about who has the most information they can generate from.</p><p>As we see more such models become reality it would lead to a new kind of tech race one where players would try to compete for users by offering them better tools near real-time information, summarization for easier consumption of information, and tools to get content out faster.</p><h3>API’s</h3><p>Just as the Internet led to the rise of software jobs across the world, APIs built on top of these models would provide millions of new jobs for people who would try to build tools using them. An industry would rise which would use these models to generate value.</p><p>We would need people to build these models, which would consume a gargantuan amount of data, results that would need to be served across multiple fronts, web, mobile, IoT, and robotics. All this would need an army of engineers, designers, testers, API developers, and above all users to consume these results to produce value for the people.</p><h3>Space: The Final Frontier</h3><p>Imagine being a miner on an asteroid, with a drill trying to extract Deuterium for fusion reactors on mars, how horrible that experience would be. If you think oil drillers in the middle of the ocean, feel alone and deal with danger, boy this would be on a whole new level (literally). These are the kind of jobs AI would take over one day, did you want that job in the first place, I hope not.</p><p>Space is vast and dangerous and filled with uncertain environments; the current generation of robots is not good enough to explore them. We need a smarter machine that is better at taking decisions and this could be a step in that direction, imagine a ChatGPT-like system onboard an unmanned probe evaluating sensor data on a planet before deciding on whether to explore it or move on. It would require combining a lot of existing knowledge like geography, in a sensible way to make a decision. If you are not already aware of The Von Neumann Probe it might be a good time to jump into that rabbit hole.</p><h3>Conclusion</h3><p>I must say this again, never in human history has a new technology caused a reduction in the total number of jobs in the world.</p><blockquote>“Give me a firm place to stand and a lever and I can move the Earth.” — Archimedes</blockquote><p>Maybe this breakthrough in AI/ML is the lever that would finally allow us to make humanity an interplanetary species and secure our future.</p><p>Hope you enjoyed reading it!!</p><p>Feel free to share your thoughts, I would love to hear your perspective on the topic.</p><blockquote>You can read my articles on a varity of topics on my <a href="https://www.akashagnihotri.com/">Website</a></blockquote><blockquote>You can find my code on my <a href="https://github.com/akash-agni"><em>GitHub</em></a></blockquote><blockquote>You can follow me on <a href="http://www.linkedin.com/in/agni25"><em>Linkedin</em></a></blockquote><blockquote>You can read my other articles on <a href="https://agniakash25.medium.com/"><em>Medium</em></a></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4b8f6533b866" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Implementing Deep Convolutional GAN]]></title>
            <link>https://medium.com/data-science/implementing-deep-convolutional-gan-f76e9b9af270?source=rss-7b80f2303030------2</link>
            <guid isPermaLink="false">https://medium.com/p/f76e9b9af270</guid>
            <category><![CDATA[unsupervised-learning]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[gans]]></category>
            <category><![CDATA[pytorch]]></category>
            <dc:creator><![CDATA[Akash Agnihotri]]></dc:creator>
            <pubDate>Fri, 26 Aug 2022 17:59:01 GMT</pubDate>
            <atom:updated>2022-08-26T17:59:01.318Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*M1iU8iKDFc8LlRTDdxpJ9Q.jpeg" /><figcaption>The Red Vineyard by Vincent van Gogh (<a href="https://en.wikipedia.org/wiki/The_Red_Vineyard#/media/File:Red_vineyards.jpg">Source</a>)</figcaption></figure><p>According to the <a href="https://www.nytimes.com/2012/09/23/technology/data-centers-waste-vast-amounts-of-energy-belying-industry-image.html">New York Times</a>, 90% of the energy used by data centers is wasted, this is because most of the data collected by companies is never analyzed or used in any form whatsoever, this is more specifically called <strong>Dark Data.</strong></p><blockquote>Dark data is data which is acquired through various computer network operations but not used in any manner to derive insights or for decision making. The ability of an organisation to collect data can exceed the throughput at which it can analyse the data. In some cases the organisation may not even be aware that the data is being collected. IBM estimate that roughly 90 percent of data generated by sensors and analog-to-digital conversions never get used. — <strong>Dark data definition on Wikipedia</strong></blockquote><p>From a machine learning perspective, one of the key reasons why this data is not useful for deriving any insights is the lack of labels. This makes unsupervised learning algorithms very attractive to unlock the potential of this data.</p><h3>Generative Adversarial Networks</h3><p>In 2014 Ian Goodfellow et al. proposed a new approach to the estimation of generative models through an adversarial process. It involved training two separate models at the same time, a Generator model which attempts to model the data distribution, and a Discriminator which attempts to classify the input as either training data or fake data by generator.</p><p>The paper sets a very important milestone in the modern machine learning landscape, opening new avenues for unsupervised learning. Deep Convolutional GAN <a href="https://arxiv.org/abs/1511.06434">paper</a>(Radford et al. 2015) continued building on this idea by applying the principles of the convolutional networks to produce 2D images successfully.</p><p>Through this article, I attempt to explain key components in the paper and implement them using PyTorch.</p><h4>What is so remarkable about GAN?</h4><p>To understand the importance of GAN or DCGAN let’s look at what makes them so popular.</p><ol><li>As a large percentage of real-world data is unlabeled the unsupervised learning nature of GANs makes them ideal for such use cases.</li><li>Generator and Discriminator act as very good feature extractors for use cases with limited labeled data or generate additional data to improve secondary model training, as they can generate fake samples instead of using augmentations.</li><li>GANs provide an alternative to maximum likelihood techniques. Their adversarial learning process and non-heuristic cost function make them very attractive to reinforcement learning.</li><li>The research around GAN has been very attractive and the results have been a source of widespread debate on the impact of ML/DL. For example, Deepfake, one of the applications of GAN which can overlay people’s faces on a target person, has been very controversial in nature as it has the potential to be used for nefarious purposes.</li><li>The last but most important point is that it’s just so cool to work with, and all the new research in the field has been mesmerizing.</li></ol><h3>Architecture</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1017/1*nP-etcj_wYCsjDexaFBaQw.png" /><figcaption>The architecture of Deep Convolutional GAN (Image by Author)</figcaption></figure><p>As we discussed earlier, we will be working through DCGAN which attempts to implement the core ideas of GAN for a convolutional network that can generate realistic-looking images.</p><p>DCGAN is made up of two separate models, a Generator (G) which attempts to model random noise vector as input and attempts to learn data distribution to generate fake samples, and a Discriminator (D) which takes training data (real samples) and generated data (fake samples) and tries to classify them, this struggle between the two models is what we call adversarial training process where one’s loss is other’s benefit.</p><h4>Generator</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/272/1*RX_YoiZCYm_mloDGhMEwGQ.png" /><figcaption>Generator Diagram (Image by Author)</figcaption></figure><p>The generator is the one we are most interested in as it is the one generating fake images to try and fool the discriminator.</p><p>Now let’s look at the generator architecture in more detail.</p><ol><li><strong>Linear layer:</strong> The noise vector is fed into a fully connected layer the output of which is then reshaped into a 4D tensor.</li><li><strong>Batch Normalization Layer:</strong> Stabilizes learning by normalizing inputs to zero mean and unit variance, this avoids training issues like vanishing or exploding gradient and allows the gradient to flow through the network.</li><li><strong>Up sample Layer:</strong> As per my interpretation of the paper, it mentions using upsampling and then applying a simple convolutional layer on it rather than using a convolutional transpose layer to up sample. But I have seen some people use convolutional transpose, so make your own decision.</li><li><strong>2D Convolutional Layer:</strong> As we up sample the matrix, we pass it through a convolutional layer with a stride of 1 and the same padding to allow it to learn from its up-sampled data</li><li><strong>ReLU Layer:</strong> Paper mentions using RelU instead of LeakyReLU for the generator as it allowed the model to saturate quickly and cover the color space of training distribution.</li><li><strong>TanH Activation:</strong> Paper suggests we use the TanH activation function for generator output but does not go into detail as to why, if we had to guess it would be because the nature of TanH allows the model to converge faster.</li></ol><p>Layers 2 to 5 make up the core generator block which can be repeated N times to get the desired output image shape.</p><p><a href="https://github.com/akash-agni/ReadThePaper/blob/main/DCGAN/dcgan.py">Here</a> is how we can implement it in PyTorch.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*dKe3XXxcYDu1IcV4HyEu-A.png" /><figcaption>Generator implemented with PyTorch. (Code by Author)</figcaption></figure><h4>Discriminator</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/437/1*8cVr2suFmO4sHiszctBJLA.png" /><figcaption>Discriminator Diagram (Image by Author)</figcaption></figure><p>Now the discriminator is rather like an image classification network with a few minor tweaks, for example, it does not use any pooling layer to downsample but a stride convolutional layer allowing it to learn its own downsampling.</p><p>Let’s look at the discriminator architecture in more detail.</p><ol><li><strong>Concat Layer:</strong> The layer combines fake images and real images in a single batch to feed the discriminator, but this can also be done separately for getting the generator loss only.</li><li><strong>Convolutional Layer:</strong> We use a stride convolution here which allows us to downsample the image and learn filters in a single pass.</li><li><strong>LeakyReLU:</strong> As the paper mentions it found LeakyReLU useful for discriminators compared to the max-out function of the original GAN paper, as it allows for easier training.</li><li><strong>Dropout:</strong> Used only for training, helps avoid overfitting. The model has a tendency to memorize real image data and training could collapse at that point as the discriminator cannot be fooled by the generator anymore</li><li><strong>Batch Normalization:</strong> The paper mentions that it applies batch normalization at the end of every discriminator block except the first one. The reason mentioned by the paper is that applying batch normalization over every layer cause sample oscillation and model instability.</li><li><strong>Linear:</strong> A fully connected layer that takes a reshaped vector from the 2D batch normalization layer applied through.</li><li><strong>Sigmoid Activation:</strong> As we are dealing with binary classification for discriminator output making a sigmoid layer logical choice.</li></ol><p>Layers 2 to 5 make up the core discriminator block which can be repeated N times to make the model more complex per training data.</p><p><a href="https://github.com/akash-agni/ReadThePaper/blob/main/DCGAN/dcgan.py">Here</a> is how we can implement it in PyTorch.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/702/1*tToFmOaLHLSnBXSSDcDVsA.png" /><figcaption>Discriminator implemented with PyTorch (Code by Author)</figcaption></figure><h3>Adversarial Training</h3><p>We train Discriminator (D) to maximize the probability of assigning the correct label to both training examples and samples from Generator (G) which can be done by minimizing log(D(x)). We simultaneously train G to minimize log(1 − D(G(z))) where z is the noise vector. In other words, D and G play the following two-player minimax game with value function V (G, D):</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/626/1*JYcjHBTw2nwyyYKCVc8p8g.png" /><figcaption>Adversarial Cost Function (<a href="https://arxiv.org/pdf/1406.2661.pdf">Source</a>)</figcaption></figure><blockquote>In practice, above equation may not provide sufficient gradient for G to learn well. Early in learning, when G is poor, D can reject samples with high confidence because they are clearly different from the training data. In this case, log(1 − D(G(z))) saturates. Rather than training G to minimize log(1 − D(G(z))) we can train G to maximize logD(G(z)). This objective function results in the same fixed point of the dynamics of G and D but provides much stronger gradients early in learning. — <a href="https://arxiv.org/pdf/1406.2661.pdf">Source</a></blockquote><p>As we are training two models simultaneously it could be tricky and GANs are notoriously difficult to train, one of the known problems we will discuss later is called mode collapse.</p><p>Paper suggests using an Adam optimizer with a learning rate of 0.0002, such a low learning rate suggests that GANs tend to diverge very quickly. It also uses the momentum of the first order and second order with values of 0.5 and 0.999 to further accelerate training. The model is initialized with normal weight distribution with zero mean and 0.02 standard deviation.</p><p><a href="https://github.com/akash-agni/ReadThePaper/blob/main/DCGAN/dcgan.py">Here</a> is how we can implement a training loop for this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/435/1*pcSnCUAbNK7pxozQ090kGQ.png" /><figcaption>Training loop for DCGAN (Code by Author)</figcaption></figure><h4>Mode Collapse</h4><p>Ideally, we would like our generator to produce a wide variety of outputs, for example, if it generates a face, it should generate a new face for every random input. But if the generator produces a plausible output good enough to fool the discriminator it might keep producing the same output again and again.</p><p>Eventually generator over-optimizes for a single discriminator and rotates between a small set of outputs, such a condition is called mode collapse.</p><p>The following approaches can be used to remedy the condition.</p><ol><li><strong>Wasserstein loss:</strong> The Wasserstein loss alleviates mode collapse by letting you train the discriminator to optimality without worrying about vanishing gradients. If the discriminator doesn’t get stuck in local minima, it learns to reject the outputs that the generator stabilizes on. So, the generator has to try something new.</li><li><strong>Unrolled GANs:</strong> Unrolled GANs use a generator loss function that incorporates not only the current discriminator’s classifications but also the outputs of future discriminator versions. So, the generator can’t over-optimize for a single discriminator.</li></ol><h3>Application</h3><ol><li><strong>Style Transfer: </strong>Face modification apps are all the hype nowadays, face aging, crying face, and celebrity face overlay are just some applications already widely popular on social media.</li><li><strong>Video games: </strong>Texture generation for 3D objects, and scene generation using images are just some applications helping the video games industry develop bigger games faster.</li><li><strong>Movie Industry: </strong>CGI has been a big part of model cinema, with the potential GAN brings, movie makers can now dream bigger than ever.</li><li><strong>Speech Generation: </strong>Some companies are using GANs to improve text-to-speech applications by using them to generate more realistic voices.</li><li><strong>Image Restoration: </strong>Using GANs to denoise and restore corrupted images, coloring historical images, and improving old videos by producing missing frames to improve their frame rate.</li></ol><h3>Conclusion</h3><p>GAN along with DCGAN is a milestone paper that has opened new avenues when it comes to unsupervised learning. The adversarial training approach provides a new way of training models that closely mimic real-world learning processes. It would be very interesting to see how this area evolves.</p><p>Hope you enjoyed the article.</p><blockquote><em>You can find the complete implementation on my </em><a href="https://github.com/akash-agni/ReadThePaper/tree/main/DCGAN">GitHub</a></blockquote><blockquote><em>You can follow me on </em><a href="http://www.linkedin.com/in/agni25">Linkedin</a></blockquote><blockquote><em>You can read my other articles on </em><a href="https://agniakash25.medium.com/">Medium</a></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f76e9b9af270" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science/implementing-deep-convolutional-gan-f76e9b9af270">Implementing Deep Convolutional GAN</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[Reproducibility in Machine Learning]]></title>
            <link>https://agniakash25.medium.com/reproducibility-in-machine-learning-24aaedecb4a1?source=rss-7b80f2303030------2</link>
            <guid isPermaLink="false">https://medium.com/p/24aaedecb4a1</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[mlops]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[software-engineering]]></category>
            <dc:creator><![CDATA[Akash Agnihotri]]></dc:creator>
            <pubDate>Sun, 10 Jul 2022 06:00:31 GMT</pubDate>
            <atom:updated>2022-07-10T06:00:31.158Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HWqnEm2wl4syxPbxAh6eQQ.jpeg" /><figcaption><a href="https://en.wikipedia.org/wiki/en:Wheat_Field_with_Cypresses">Wheat Field with Cypresses</a> by Vincent van Gogh (1889)</figcaption></figure><p>“Any device which takes an input and produces an (exact) output is a machine” — this has been the rough definition of any machine for a long time, but machine learning solutions have put it into question inside the industry when it comes to implementation. This arises from the fact that a lot of companies are still facing challenges in deploying end-to-end solutions involving machine learning and deep learning.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QzMBnQVEJp17IRm0omzl7A.png" /><figcaption>A Machine with predictable and constant behavior (<a href="https://en.wikipedia.org/wiki/Machine#/media/File:Bonsack_machine.png">Source</a>)</figcaption></figure><p>As per a report by <a href="https://venturebeat.com/2019/07/19/why-do-87-of-data-science-projects-never-make-it-into-production/">Venturebeat</a>, nearly 87% of machine learning projects never reach the production stage, this is due to a whole host of issues including lack of MLOps framework, team structure, and overfitting ML to solve every problem, but one of the key issues that projects face when they move from a jupyter notebook on someone’s local system to a central environment is the reproducibility of results. This leads to results that drastically vary and the project losses business trust and eventually funding.</p><p>In my years as Machine Learning Engineer, I have identified 3 key variables that need to be tracked and versioned in sync to make machine learning results consistent and reproducible over time, these combine ideas from MLOps, DataOps, and DevOps to build a framework that allows projects to produce outputs that are consistent and reproducible.</p><h3>The Model</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/330/1*ACSe9SJHl7Gj31cVbPY5Sw.png" /><figcaption>An artificial neural network (<a href="https://en.wikipedia.org/wiki/Machine_learning#/media/File:Colored_neural_network.svg">Source</a>)</figcaption></figure><p>The centerpiece of this puzzle but also the simplest part to build and implement. Yes, I believe most industry solutions implementing ML does not require a complex model and therefore are often the simplest thing to build from an engineering perspective.</p><p>The challenge of reproducibility in a model arises from 2 factors, incorrect model training pipeline which does not consider the pseudo-random algorithms which are part of ML, for example, model weight initialization, training data batch sequence, etc. these algorithms generate a non-deterministic output if the SEED variable is not fixed across multiple runs. SEED is a hyperparameter used during model training that allows pseudo-random algorithms to generate the same result on every run given that everything else remains the same.</p><p>Almost all major libraries provide some function to implement a global SEED that applies across the entire training instance. SEED should be treated like any other hyperparameter and should be registered along with the model inside experiment tracking systems like MLFlow.</p><h3>The Data</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/439/1*QDFvF0_Tig_IitAojoOaCg.png" /><figcaption>Types of Data (<a href="https://upload.wikimedia.org/wikipedia/commons/6/6d/Data_types_-_en.svg">Source</a>)</figcaption></figure><p>Any machine learning system is as good as the data it is trained on, and the same is true when it comes to the reproducibility of the quality of data versioning and how it is implemented. In real-life data is never as clean as we see in Kaggle and building a robust feature engineering pipeline that produces consistent results is a big challenge for engineering teams.</p><p>As we have already seen how treating SEED as a crucial hyperparameter can solve the curse of pseudo-random algorithms we will not visit that again, lets&#39;s talk about how we can maintain data versions.</p><p>Mention data versioning to your platform or data engineering team and look at their reaction, that would tell you a lot about what are some of the biggest challenges with data versioning but for time’s sake let me list down some below.</p><ul><li>Real data versioning means redundancy of data, and means exploding storage costs.</li><li>For index-based versioning what happens when the underlying data changes.</li><li>How do we deal with data retention or governance, what about access management?</li></ul><p>There are tools available to assist you in this effort, for example, DVC, and Azure ML Studio but all of them have their limitation the most common being what happens when actual data being tracked is changed in the background or worse deleted.</p><p>Time travel, no not the HG Wells time travel, but data time travel is what I have found to be a cheap and reliable solution. So, as I mentioned earlier, most ML projects are not very complex that might need model training every few hours on a new dataset. It might be daily at best, for such use cases data versioning can be something as simple as the ASOF date for when the model was trained.</p><p>Most experiment tracking systems provide the details of the date and time when the model was trained, which provides the information one needs to find the right data version for reproducibility of results.</p><h3>The Code</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1O0RTWlZGD1VxKwmD81Ulw.jpeg" /><figcaption>Code is the engine mixing data and model. (<a href="https://www.pexels.com/photo/close-up-photo-of-programming-of-codes-546819/">Source</a>)</figcaption></figure><p>Now, this part might be the largest and most crucial part of product development, coding the actual solution, but the good news is it is also the most matured component. Code versioning is a well-established and well-understood topic. But the key here is to track the version of code used for model training, and most experiment tracking systems can register the git metadata used to create the pipeline, but the same is not true for the code running inside the pipeline itself.</p><p>To solve this problem, we can develop our solution as a python package that can be built into a wheel and maintained easily across versions. Training or inference pipelines can then track them as any other dependency for model training or inference. For more on building python packages for machine learning products, you can review my <a href="https://agniakash25.medium.com/design-ideas-for-frustrated-ml-engineers-6b3ca9e581f0">article</a> on the topic.</p><h3>The Experiment Tracking System</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/0*5kH59xDyaSTO-7pR.png" /><figcaption>We must follow the scientific method of reproducible results when building great products. (<a href="https://www.goodfreephotos.com/vector-images/chemistry-experiment-vector-clipart.png.php">Source</a>)</figcaption></figure><p>“It was showing good results in my system” is the ML version of “It was working on my machine”. In the age of the cloud, please don’t develop an actual ML solution on your local systems and expect the engineering team to just “pick it up and deploy it”. It does not work like that.</p><p>Building products that can be deployed into production in a large organization means collaboration across teams and that is only possible if you publish your experiments on a central repo for all to see just like the code. MLFlow, Neptune, Weights &amp; Biases, H2O, etc. are some tools available to track all your experiments under one roof.</p><p>An experiment tracking system allows you to bring all your key variables into one place and track them in sync, such that teams can review the results and reproduce them if needed. They track the model versions trained and deployed allowing you to roll back when something goes wrong, they also track the data and code/dependencies used to train the model.</p><p>A good experiment tracking system will have at least the following capabilities.</p><ul><li>Model training and inference pipeline tracking.</li><li>Model versioning along with the associated artifacts.</li><li>Tracking the repo metadata building and publishing training or inference pipeline.</li><li>Tracking the data and environment used for pipeline execution.</li><li>Hierarchical experiment tracking for hyperparameter tuning.</li></ul><h3>Conclusion</h3><p>Machine Learning is one of the most revolutionary technologies of our generation, but if it must find industry-wide acceptance it has to work under the same principles as all other technology and one of which is reproducible results that can generate trust among its users. There are many tools out there making brave promises on how they take care of all this and allow you to “Have fun with it”, beware of such promises. Building great products is not always fun and requires hard work and no tool in the world can compensate for human errors or lack of structure and discipline in development.</p><p>Hope you enjoyed the article.</p><blockquote><em>You can find useful code on my </em><a href="https://github.com/akash-agni">GitHub</a></blockquote><blockquote><em>You can follow me on </em><a href="http://www.linkedin.com/in/agni25">Linkedin</a></blockquote><blockquote><em>You can read my other articles on </em><a href="https://agniakash25.medium.com/">Medium</a></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=24aaedecb4a1" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Design Ideas for Frustrated ML Engineers]]></title>
            <link>https://agniakash25.medium.com/design-ideas-for-frustrated-ml-engineers-6b3ca9e581f0?source=rss-7b80f2303030------2</link>
            <guid isPermaLink="false">https://medium.com/p/6b3ca9e581f0</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[mlops]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Akash Agnihotri]]></dc:creator>
            <pubDate>Wed, 18 May 2022 11:51:09 GMT</pubDate>
            <atom:updated>2022-05-18T11:51:09.205Z</atom:updated>
            <content:encoded><![CDATA[<h4>Simple ideas to solve complex problems.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gQptSwA4HefeZ0b4GiynWg.jpeg" /><figcaption>The Night Café, 1888 by Vincent Van Gogh (<a href="https://en.wikipedia.org/wiki/The_Night_Caf%C3%A9#/media/File:Vincent_Willem_van_Gogh_076.jpg">Source</a>)</figcaption></figure><p>“I like these calm little moments before the storm. It reminds me of Beethoven.” — Gary Oldman in Léon: The Professional.</p><p>This is how I feel every time one of the data science teams comes up with another brilliant idea to solve a problem with ML, cause it means long hours solving linting issues, writing test cases, and resolving platform issues.</p><figure><img alt="ML System Diagram" src="https://cdn-images-1.medium.com/max/652/1*ZNuyIKUdwEJUUXuah9alrw.png" /><figcaption>A generic diagram of the production ML System. (Author)</figcaption></figure><p>In the above diagram, you can see how small is the actual model training component. In reality, we spend even less time focusing on model training, most of the time spent by an ML Engineer is building the package which contains the code to build data pipelines, training pipelines, prediction pipelines, writing test cases, and solving linting issues.</p><p>“The reality is often disappointing”, I know, but to see a final product is worth it. To see data seamlessly flowing through your model and being presented to users, who solve real problems, is all worth it.</p><p>So here I am going to suggest 3 approaches I take to solve some of the problems I have faced.</p><h3>Keep it simple</h3><p>The simplest solution is often the best, or at least it gets the job done. As ML went mainstream in the software industry, everyone expected software engineers to just pick up ML and start building applications, turns out, that doesn’t work.</p><p>The problem is not that they can’t pick up ML, the problem is they can’t drop their years of software training, they come with years of building applications where every line of code is tested and linted.</p><p>That doesn&#39;t always work in ML applications when that one line of code can run for 3 hours using 16 GB of GPU Memory. When a slight change in random state can change the results. How do you test that (you can!!). Not every ML library comes with linting standards.</p><p>Not every ML application needs that level of scrutiny. A simple document tagging system quietly working in the background tagging new documents using ML does not need to pass all of mypy or flake8’s standards, or need a 100% test coverage.</p><figure><img alt="Simple python project repo for ML" src="https://cdn-images-1.medium.com/max/296/1*n2ixuju9cqQJIFmKGWVZKw.png" /><figcaption>Start with the simplest solution. (Author)</figcaption></figure><p>You don’t always need to start by defining abstract classes and datasets or implement 15 different linting hooks, a simple package that can be containerized along with simple pipelines to train and deploy models could solve half of your ML problems. Also, it can be developed very quickly, it&#39;s a great starting point to show your idea in action.</p><p>This design works very well in the below cases.</p><ol><li>Single use-case problem, there is 1 dataset, 1 model, and 1 prediction.</li><li>Using classic models like logistic regression or random forest.</li><li>Want to reach the minimum viable product stage quickly.</li></ol><p>As you build, new components can be easily added like hyperparameter tuning or model monitoring.</p><h3>Stand on the shoulders of greats</h3><p>The modern machine learning landscape is built by selfless people in our open source community, if not for NumPy or Scikit-Learn, we all will be hitting our heads on keyboards typing MATLAB code starting loops from 1.</p><p>Whenever possible use the tools available in open source, you do not have to develop your own BERT model and train it. PyTorch and HuggingFace provide a whole host of models that are more than enough powerful to solve your problem, ranging from computer vision to language models.</p><p>There is an existing tool for almost everything an ML application needs, below are some suggestions. No need to implement all components through tools, but also no need to develop every tool in-house.</p><ul><li>Model Inventory: PyTorch, HuggingFace, TensorFlow.</li><li>Model Training and Evaluation: PyTorch Lightning, FastAI</li><li>Data Versioning: DVC</li><li>Dependency Management: Conda, pip, poem</li><li>Experiment Tracking: MLFlow, KubeFlow</li><li>Model Registry: MLFlow</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*LACUjVKPH-udCtVp.png" /><figcaption>A lot of toys out there to play with. (<a href="https://fall2019.fullstackdeeplearning.com/course-content/infrastructure-and-tooling">Source</a>)</figcaption></figure><p>The pro is that you get production-grade well maintained code, available for plug and play. The con is maintenance can become very difficult with so many dependencies.</p><h3><strong>Do not go gentle into the good night</strong></h3><p>Now if none of the above 2 ideas work for you, because your use-case is really unique or really big then first, I feel sorry for you, next I have a diagram for you.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/491/1*Tt42VuLVd76fR3PW7sg8lw.png" /><figcaption>A diagram to help build the large applications. (Author)</figcaption></figure><p>The above diagram has helped me when no one could. Sometimes we come across products that are not just a simple use case, but a platform that would include numerous and often undefined use cases, in such case you need to bring the big guns. This is the kind of stuff that requires the baggage we discussed earlier carried by software engineers.</p><p>This would allow you to add new models and new use cases as you scale, there is no hiding from linting or testing now. This level of solution requires a high level of linting and testing standards using mypy, flake8, pydocstyle, isort, pytest and etc.</p><p>In the above diagram, every component is a module of code and has a complimentary test module. Some guidelines to go along with the diagram are:</p><ol><li>Do not hard code any configuration, pass it as an argument or environment variable.</li><li>Define your own training and prediction loops to keep control.</li><li>Define a callback mechanism that allows you to control training and logging.</li><li>Log everything, and build an extensive logging system with proper formatting. Debugging can become hell otherwise.</li><li>Build extensive exception handling and implement tests for them. You can define very very small datasets to only be used to test code modules related to training or tuning to avoid long test times.</li><li>Build pipelines to bring models, data, and training together for each use case.</li></ol><h3>Conclusion</h3><p>The ideas here are from my practical experience and do not cover all the possible situations, these ideas have helped me build solutions that I could be proud of, and I hope you can use them to solve some of your problems.</p><p>The great thing about ML is that the landscape is still very unexplored and there are endless possibilities. Find your own way of doing things and make it work.</p><p>If you enjoyed the article</p><blockquote>Leave a comment</blockquote><blockquote>Connect with me on <a href="https://twitter.com/agni_akash">Twitter</a>, <a href="https://www.linkedin.com/in/agni25/">LinkedIn</a>, <a href="https://github.com/akash-agni">Github, </a>or <a href="https://agniakash25.medium.com/">Medium</a>.</blockquote><blockquote>Share the article with your network.</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6b3ca9e581f0" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Attending to Attention]]></title>
            <link>https://medium.com/data-science/attending-to-attention-eba798f0e940?source=rss-7b80f2303030------2</link>
            <guid isPermaLink="false">https://medium.com/p/eba798f0e940</guid>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[nlp]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[attention]]></category>
            <category><![CDATA[pytorch]]></category>
            <dc:creator><![CDATA[Akash Agnihotri]]></dc:creator>
            <pubDate>Fri, 12 Nov 2021 18:35:29 GMT</pubDate>
            <atom:updated>2021-11-13T16:51:44.034Z</atom:updated>
            <content:encoded><![CDATA[<h4>A summary of a revolutionary paper “Attention is All You Need” and Implementing the Transformer using PyTorch</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Z3218uUbG2W6qOhN1Ell3g.png" /><figcaption>Vincent van Gogh<strong> </strong><em>Self-Portrait with Grey Felt Hat</em>, Winter 1887/88. (<a href="https://en.wikipedia.org/wiki/Portraits_of_Vincent_van_Gogh">Source</a>)</figcaption></figure><p>I have been a Machine Learning Engineer for almost 4 years now, I started with what is now called the “Classical Models”, Logistic, Tree-based, Baysian, etc, and since last year has moved into Neural Networks and Deep Learning. I would say I did pretty well, that was until my attention lay on “Attention” (pun intended). I tried reading through tutorials, lectures, guides but nothing ever fully helped me grasp the core idea.</p><p>So I decided it is time to face bull head-on, and sat down and read the paper from arxiv, and wrote my <a href="https://github.com/akash-agni/ReadThePaper"><em>implementation </em></a>through Python and PyTorch. This helped me get a full understanding of the core concepts. Now, as they say, the best way to check your learning is to try and explain it to someone else, so here I am trying to break down what I learned.</p><h3>History</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/996/1*RFDkdPasxr82bCtRAVFHCw.jpeg" /><figcaption>A comparison between NLP designs <a href="https://deepmind.com/learning-resources/deep-learning-lecture-series-2020">Source</a></figcaption></figure><p>To understand why Transformers are all the hype today, we must understand what happened before it came into the picture. The dominant approach was using Recurrent Neural Networks like LSTM or GRU to implement a sequence-to-sequence model, where an encoder was used to create a context vector that would contain all the information of the source sequence, and then the decoder will use this context vector to generate new tokens sequence by sequence.</p><p>The problem, as the above table shows, with this method was that it was unable to encode pairwise encodings into the context vector. What it means is that when we need to look at multiple tokens across the sequence to figure the prediction, the RNN based models do not do so well, as they cannot encode pairwise relationships.</p><h3>Architecture</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/423/1*1h07W0O6bIOo267XA05Xcg.jpeg" /><figcaption>The architecture of the Transformer (<a href="https://arxiv.org/abs/1706.03762">Source</a>)</figcaption></figure><p>In December of 2016, the Google Brains team came up with a new way to model sequences called Transformers presented in their paper <a href="https://arxiv.org/abs/1706.03762">Attention is all you need</a>. The impact of this paper continues as most Language models use this approach including some of the industry favorites like BERT and GPT-2</p><p>The transformer design consists of an Encoder and a Decoder, both comprise of Multi-Head Attention modules and FeedForward modules, model uses residual connections to concatenate with results and normalized to improve training. The sublayer of the attention module and feedforward module is then repeated N times for both Encoder and Decoder.</p><h4>Encoder</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/212/1*XIBrmaCYMNTLBDxGANFQRw.jpeg" /><figcaption>Encoder Architecture (<a href="https://arxiv.org/pdf/1706.03762.pdf">Source</a>)</figcaption></figure><blockquote>The encoder is composed of a stack of N = 6 identical layers. Each layer has two sub-layers. The first is a <strong>multi-head self-attention</strong> mechanism, and the second is a simple, <strong>position-wise fully connected feed-forward network</strong>. We employ a residual connection around each of the two sub-layers, followed by layer normalization. That is, the output of each sub-layer is LayerNorm(x + Sublayer(x)), where Sublayer(x) is the function implemented by the sub-layer itself. To facilitate these residual connections, all sub-layers in the model, as well as the embedding layers, produce outputs of dimension 512. (<a href="https://arxiv.org/pdf/1706.03762.pdf">Source</a>)</blockquote><p>The input tokens pass through the token embedding layer and as the model does not use a recurrent layer it uses the positional embeddings to get the positional information on the tokens, these two are added together. The positional embedding layer does not concern with the tokens but the position of the token. The results from token embeddings and positional embeddings are summed elementwise and the resulting vector is then scaled by the square root of the hidden dimension of the model.</p><p>The scaling is done to reduce the variance in the embedding vector as it makes it difficult for the model to train. The dropout is applied to the final embedding vector. The embedding vector is then applied through N encoding layers to get the final context vector which is used by the decoder. The source mask is similar to the source vector, it contains 1 when the token is not &lt;pad&gt; and 0 otherwise, this is done to avoid the attention layer from focusing on the padding token.</p><p>Initially, the embedding vector is passed through the Multi-Head Attention layer, the result along with residual connection are elementwise summed and passed through layer normalization. The Multi-Head-Attention is passed the source sentence as the <strong>key</strong>, <strong>value,</strong> and <strong>query</strong> (more on that later), this is done so that the attention network will attend to the source sentence itself, therefore the name <strong>Self-Attention</strong>.</p><p>The resulting vector is then passed through a position-wise feedforward network and along with the normalization layer along with its residual connection. The results are then passed on to the next layer.</p><h3>Attention</h3><p>Attention has been in use before this paper came along, but the way it was implemented here has been crucial in its success and vast adoption. Instead of serving as a support module to enhance the context vector, this architecture uses it at its core as an expert system.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/761/1*5UnOZ_2AjIEMcWppY7uiug.png" /><figcaption>Attention Mechanism (<a href="https://arxiv.org/pdf/1706.03762.pdf">Source</a>)</figcaption></figure><h4>Single Dot Product Attention</h4><p>A single attention head takes 3 values as input namely Query(Q), Key(K), and Value(V). One can think of attention as a function which maps given Query to Key-Value pairs, and the corresponding result can be thought of as weighted values that describe which key-value pair is more important to query. This value is then passed through the softmax function to get normalized weights and then the dot product is taken with values.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/311/1*K8_-WakUXRGU_EMOcSu23A.jpeg" /></figure><h4>Multi-Head Attention</h4><p>Instead of applying Query, Key, and values through a single dot product attention, we divide the dimension up into <em>h</em> components. The computation happens in parallel and the results are then concatenated to get the final result. This allows the model to learn multiple concepts together rather than focus on a single concept.</p><p>One analogy that helps me understand this goes like this. Imagine a room consisting of experts in it, we don’t know currently who is an expert in which topic, so we pass a question into the room and get the result from each expert with the probability of confidence for each expert. Now initially each expert has an equal confidence score assigned for any and every topic. But as we backpropagate and learn over examples, we figure out, which experts answer is more useful for which topic.</p><p>For example, when I pass a question on cars, each expert provides their advice, but over time, we know whose answer is more useful for this topic, similarly some other experts can be useful for other topics. In this example, a single dot product attention is an expert, our multi-head attention layer is the room full of experts, and our Query, Key, and Values are questions we ask.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/490/1*m36QZIX-KKExZHa32rSG5A.jpeg" /></figure><h3>Decoder</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/376/1*qXaC0-eLtwkt-nAHOT-jUg.jpeg" /><figcaption>Decoder Architecture (<a href="https://arxiv.org/pdf/1706.03762.pdf">Source</a>)</figcaption></figure><p>The decoder works similarly to the encoder and uses an attention mechanism on target tokens instead of source tokens. Except for the part where it has two Multi-Head Attention layers. The first uses targets embeddings and the next use encoder output as Key-Value pair and previous layers output as Query.</p><p>Before the decoder module, we pass the targets through a standard embedding and perform elementwise sum with positional encoding embeddings, these perform a similar job as in encoder. The result is then passed through N Decoder layers, point to be noted here is that paper never specified that the number of layers in Encoder and Decoder has to be the same.</p><p>The decoder layer consists of two <em>Multi-Head Attention</em> layers, one <em>self-attention,</em> and another <em>encoder attention</em>. The first takes target tokens as Query and Key-Value pairs and performs self-attention, while the other takes the output of <em>self-attention</em> layer as Query and <em>Encoder Output</em> as Key-Value pair.</p><p>The first attention module uses a target sequence mask, this is done to prevent the model from being able to see the next token in sequence as we process all the tokens in parallel. The second attention layer uses the output of the self-attention layer as Query and Encoder Output as Key-Value pair, the module is also provided with a source mask, this is done to prevent the model from paying attention to &lt;pad&gt; tokens.</p><p>Both Multi-Head Attention layers are followed by residual connection and Dropout layer which feeds into a layer normalization module. The result is then passed through as a position-wise feedforward network and another set of residual connection and layer normalization modules the result is then passed to the next layer.</p><h3>Conclusion</h3><p>Understanding this paper has allowed me to understand the new transformers that are being published lately like BERT, Roberta, GPT-3, etc. It has also given me the confidence to read and implement more papers on my own. I hope this has been a useful full read for you. Please let me know if there are any mistakes on my part, as that would help me improve my understanding of the concept.</p><p>Hope you enjoyed the article.</p><blockquote>You can find the implementation on my <a href="https://github.com/akash-agni/ReadThePaper/blob/main/Attention%20Is%20All%20You%20Need.ipynb"><em>GitHub</em></a></blockquote><blockquote>You can follow me on <a href="http://www.linkedin.com/in/agni25"><em>Linkedin</em></a></blockquote><blockquote>You can read my other articles on <a href="https://agniakash25.medium.com/"><em>Medium</em></a></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=eba798f0e940" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science/attending-to-attention-eba798f0e940">Attending to Attention</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[Presenting Machine Learning Model Results as Business Insights]]></title>
            <link>https://medium.com/data-science/presenting-machine-learning-model-results-as-business-insights-b42309c5e974?source=rss-7b80f2303030------2</link>
            <guid isPermaLink="false">https://medium.com/p/b42309c5e974</guid>
            <category><![CDATA[presentations]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[data-visualization]]></category>
            <category><![CDATA[business]]></category>
            <category><![CDATA[notes-from-industry]]></category>
            <dc:creator><![CDATA[Akash Agnihotri]]></dc:creator>
            <pubDate>Fri, 25 Jun 2021 19:25:03 GMT</pubDate>
            <atom:updated>2021-06-28T20:43:33.028Z</atom:updated>
            <content:encoded><![CDATA[<h4><a href="https://towardsdatascience.com/tagged/notes-from-industry">Notes from Industry</a></h4><h4>How to present machine learning model performance as actionable insights to Business.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lBiSvgOHc_QwaUoWZ_g7NA.jpeg" /><figcaption><em>Wheatfield Under Thunderclouds</em>, 1890, Amsterdam, Netherlands (<a href="https://en.wikipedia.org/wiki/Vincent_van_Gogh#/media/File:Vincent_van_Gogh_-_Wheatfield_under_thunderclouds_-_Google_Art_Project.jpg">Source</a>)</figcaption></figure><p>Machine Learning and Deep Learning have been some of the most revolutionary technologies of our generation and have the potential to radically redefine our way of life. With this much hype surrounding a technical stack, it can often take a life of its own and it is very easy to forget that at the end of the day it is just another tool to add value to our businesses that serve the customer at its center.</p><p>I have been working at one of the largest banks in the world for over 2 years now as a Machine Learning Engineer, and if I could summarize my experience in a few words it would be.</p><blockquote>“If you can’t write your message in a sentence, you can’t say it in an hour.” -Dianna Booher</blockquote><p>And it is true for any engineer working with their business or clients, they neither care what great technology you have used, nor how complex your solution is but only how much value can this solution add to their business.</p><p>To that extent, through this guide, I will make an effort to present to you some ways that I have often used to some degree of success while presenting my work to business.</p><h3>Part 1: Show it, don’t say it.</h3><p>There is a reason why <strong>Salesforce </strong>spent over $15 billion dollars to acquire a Data Visualization/Reporting tool called <strong>Tableau.</strong> The value of a graph rising up over time is much more than a thousand words could convey.</p><p>Let us look at few ways to present your results.</p><h4>Gains Chart</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/590/1*MtskQ5N7om0r8UOfhK5p2g.png" /><figcaption>Gains Chart represents the cumulative bad capture rate of your model. (Image by Author)</figcaption></figure><p>Gains chart is a very useful tool when you are working with models which produce continues probability score, as these values can be sorted and accumulated to show how much of <strong>event </strong>you are able to capture using the model, compared to random case.</p><p>For example in this case we can see our model is able to capture <strong>80% of events in the top 20% scored data, </strong>which means if a business focuses on just the top 20% of the total observations scored, it can capture up to 80% of events using your model.</p><p>This helps businesses understand the value your model brings to the table, and how it can help them make better decisions.</p><h4>True vs Predicted Value Distribution.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/741/1*ZXwsc2P-B_dC7wPvlDocBw.png" /><figcaption>Plotting the distribution of predicted values over the distribution of true value. (Image by Author)</figcaption></figure><p>When working with regression models it can often be tricky to present your model results to business teams, as you don’t want them to go through all the residual analysis and multivariant analysis.</p><p>An overlapped distribution histogram of True vs Predicted values is a great way to present how well your model results compare with reality. This will help the business understand if your model is good enough to replace the existing system in place.</p><h3>Part 2: Measuring ROI in Machine Learning Models</h3><p>Just because you have a great model with an amazing tech stack and a full-proof product design, does not mean the business owes it to you to approve the funds to build it, I learned that the hard way, you shouldn’t.</p><p>Have a cost-benefit analysis when you present to the business any product design or model designs. No matter how cool or inspired your model is, no one would approve it if it did not justify the cost.</p><p>In 2006 Netflix spent $1 million to fund a competition to find an improved solution to their movie recommendation system but did not end up using even the state of art solutions presented, as “The additional cost of engineering and maintenance does not justify the improvement in accuracy”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/687/1*pZGYXiG9wc4LmsH--RnhSg.png" /><figcaption>Netflix competition. (<a href="https://netflixtechblog.com/netflix-recommendations-beyond-the-5-stars-part-1-55838468f429">Source</a>)</figcaption></figure><h4>Understand the Cost of Confusion</h4><p>To calculate the cost vs benefit of a simple binary classifier you can look at the confusion matrix and assign value to each cell.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/407/1*ErYr_XfLqZ5cE6Jo4I_W3g.png" /><figcaption>Confusion matrix for a customer churn prediction model (Image by Author)</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/205/1*0sgyH4ydvqFm5jZlD_PUWw.png" /><figcaption>Classification Report (Image By Author)</figcaption></figure><p>Consider the example, you are tasked with building a model to predict customer churn for a credit card company. You are excited and inspired and go ahead and build a piece of beauty for them, with results similar to this.</p><p>Now some business analysts do the cost-benefit analysis for you, Lets us see what they find. As per their analysis company loses about $20,000 per customer (Cost of False Negative) in lost revenue and other costs for the period of prediction, once a model is put in place it would cost the company around $1,000 (Cost of both False Positive and True Positive) to try and retain the customer. Let us see how your model does.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/512/1*N0S6lYTIn3xegREEAFy_Qg.png" /><figcaption>Your company will end up making a loss of over a million dollars. (Image by Author)</figcaption></figure><p>Now of course I manipulated the number a little to get the results, but the point remains, also I still ignored the engineering and maintenance cost of the entire thing. The point is, sometimes machine learning is not the solution or at least not the most viable solution.</p><p>As a Machine Learning Engineer, it would help you a long way if you learn how to figure out early in your project minimum viable performance.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/566/1*8qbv59c3SrLwRS-AnzfQDA.png" /><figcaption>The state of AI in Enterprise (<a href="https://www2.deloitte.com/content/dam/insights/us/articles/4780_State-of-AI-in-the-enterprise/DI_State-of-AI-in-the-enterprise-2nd-ed.pdf">source</a>)</figcaption></figure><h3>Part 3: Be Part of the Solution</h3><p>No matter what your final results are, always provide a list of recommendations on the next steps. You are paid to solve problems not just find and evaluate them. It also shows your ability to look above and beyond your own space of work.</p><p>If your model works and is financially viable.</p><ol><li>Suggest a product development roadmap and timeline.</li><li>How existing teams/Infra can be involved to reduce the cost.</li><li>What improvements will future versions possibly bring?</li></ol><p>If your model is not feasible.</p><ol><li>Suggest a few off-the-shelf products available in the market.</li><li>How a rule-based solution will perform compared to a model.</li></ol><h3>Conclusion</h3><p>Unless and until you are ML/DL Researcher, presenting your solutions will be a big part of your job, and just like any other job, this is something you will learn with practice. The early you can horn your presentation skills the smoother the ride will be for you.</p><p>If you enjoyed the article.</p><blockquote>Leave a comment to let me know your thoughts.</blockquote><blockquote>Connect with me on <a href="https://twitter.com/agni_akash">Twitter</a>, <a href="https://www.linkedin.com/in/agni25/">Linkedin</a>, <a href="https://github.com/akash-agni">Github</a>, or <a href="https://agniakash25.medium.com/">Medium</a>.</blockquote><blockquote>Share the article with your network.</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b42309c5e974" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science/presenting-machine-learning-model-results-as-business-insights-b42309c5e974">Presenting Machine Learning Model Results as Business Insights</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[Implementing Real-time Object Detection System using PyTorch and OpenCV]]></title>
            <link>https://medium.com/data-science/implementing-real-time-object-detection-system-using-pytorch-and-opencv-70bac41148f7?source=rss-7b80f2303030------2</link>
            <guid isPermaLink="false">https://medium.com/p/70bac41148f7</guid>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Akash Agnihotri]]></dc:creator>
            <pubDate>Fri, 18 Jun 2021 14:27:24 GMT</pubDate>
            <atom:updated>2021-06-18T14:27:24.300Z</atom:updated>
            <content:encoded><![CDATA[<h4>Hands-On Guide to implement real-time object detection system using python</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*ADo7r1hofD1RWB7Wo_k79A.jpeg" /><figcaption>Vincent van Gogh (1853–1890), Paris, May-July 1887 (<a href="https://www.vangoghmuseum.nl/en/collection/s0086V1962#details">source</a>)</figcaption></figure><p>The Self-Driving car might still be having difficulties understanding the difference between humans and garbage can, but that does not take anything away from the amazing progress state-of-the-art object detection models have made in the last decade.</p><p>Combine that with the image processing abilities of libraries like OpenCV, it is much easier today to build a real-time object detection system prototype in hours. In this guide, I will try to show you how to develop sub-systems that go into a simple object detection application and how to put all of that together.</p><h3>Python vs C++</h3><p>I know some of you might be thinking why I am using Python, isn&#39;t it too slow for a real-time application, and you are right; to some extent.</p><p>The most compute-heavy operations, like predictions or image processing, are being performed by PyTorch and OpenCV both of which use c++ behind the scene to implement these operations, therefore it won&#39;t make much difference if we use c++ or python for our use case here.</p><p>But again, it is just a prototype that has very little infrastructure code and overhead attached to it. If you wish to learn production-grade real-time implementation I would suggest python is not the way to go, at least not yet.</p><h3>Reading The Video Stream</h3><p>Your input video stream source can be anything, you might want to read from your webcam, or parse an already existing video, or from an external camera connected to the network. No matter what the problem is OpenCV is the solution. In this example, I will show how to read a video stream from youtube or a webcam.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RYOxzW7nU4sxmNplxjgxGA.png" /><figcaption>You can create a Video Stream to feed your application using OpenCV. (<a href="https://www.youtube.com/watch?v=wqctLW0Hb_0">source</a>)</figcaption></figure><h4>Reading from YourTube</h4><p>For your prototype, you might not want to go out and create a new video, but use one of many available online. In such a case, you can read the video stream from youtube.</p><pre>import cv2 # opencv2 package for python.<br>import pafy # pafy allows us to read videos from youtube.</pre><pre>URL = &quot;https://www.youtube.com/watch?v=dQw4w9WgXcQ&quot; #URL to parse<br>play = pafy.new(self._URL).streams[-1] #&#39;-1&#39; means read the lowest quality of video.<br>assert play is not None # we want to make sure their is a input to read.<br>stream = cv2.VideoCapture(play.url) #create a opencv video stream.</pre><h4>Reading from WebCam</h4><p>Sometimes you just want to look at your own face. for such cases feel free to use the internal WebCam.</p><pre>import cv2</pre><pre>stream = cv2.VideoCapture(0) # 0 means read from local camera.</pre><h4>Reading IP Camera</h4><p>If you are building an application that will be deployed on a server your camera will have an IP address from which you can access the Video Stream.</p><pre>import cv2</pre><pre>camera_ip = &quot;rtsp://username:password@IP/port&quot;<br>stream = cv2.VideoCapture(camera_ip)</pre><h3>Load the Model</h3><p>Machine learning engineers today are spoiled for choice or should I say confused by choice. There are many great object detection models out there each with its pros and cons. To keep things simple we will go with <strong>YoloV5</strong> as it provides us with fast inferences which are critical for our real-time application. You can also look into other models like FasterRCNN.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*hwwso_UOhbAqBru7.png" /><figcaption>As per Yolov5 paper, it is the fastest model in the market right now. (<a href="https://github.com/ultralytics/yolov5">source</a>)</figcaption></figure><p>We can directly load the model from the PyTorch hub, the first time you run the code it might take few minutes as it will download the model from the internet, but next time onwards it will be loaded directly from the disk.</p><pre>from torch import hub # Hub contains other models like FasterRCNN</pre><pre>model = torch.hub.load( \<br>                      &#39;ultralytics/yolov5&#39;, \<br>                      &#39;yolov5s&#39;, \<br>                      pretrained=True)</pre><h3>Scoring a Single Frame</h3><p>As the saying goes, “journey of thousand miles, start with a single step”, so we can say “Parsing of a video stream, starts by a single frame”. So let us see how we can score and parse a single frame.</p><p>The device we use to perform the inference makes a huge difference in our inference speed, modern deep learning models work best when working with GPU’s so if you have a GPU with a CUDA kernel it will improve your performance by a huge margin. In my experience system with even a single GPU can achieve 45–60 frames per second, where a CPU might give you 25–30 frames at best.</p><pre>&quot;&quot;&quot;<br>The function below identifies the device which is availabe to make the prediction and uses it to load and infer the frame. Once it has results it will extract the labels and cordinates(Along with scores) for each object detected in the frame.<br>&quot;&quot;&quot;</pre><pre>def score_frame(frame, model):<br>    device = &#39;cuda&#39; if torch.cuda.is_available() else &#39;cpu&#39;<br>    model.to(device)<br>    frame = [torch.tensor(frame)]<br>    results = self.model(frame)<br>    labels = results.xyxyn[0][:, -1].numpy()<br>    cord = results.xyxyn[0][:, :-1].numpy()<br>    return labels, cord</pre><h3>Plotting Boxes over the Frame</h3><p>Once we have scored the frame, we would need to plot the identified objects along with their boxes over the frame before writing the frame into the output stream. To do this we can use OpenCV&#39;s image processing toolkit.</p><pre>&quot;&quot;&quot;<br>The function below takes the results and the frame as input and plots boxes over all the objects which have a score higer than our threshold.<br>&quot;&quot;&quot;<br>def plot_boxes(self, results, frame):<br>    labels, cord = results<br>    n = len(labels)<br>    x_shape, y_shape = frame.shape[1], frame.shape[0]<br>    for i in range(n):<br>        row = cord[i]<br>        # If score is less than 0.2 we avoid making a prediction.<br>        if row[4] &lt; 0.2: <br>            continue<br>        x1 = int(row[0]*x_shape)<br>        y1 = int(row[1]*y_shape)<br>        x2 = int(row[2]*x_shape)<br>        y2 = int(row[3]*y_shape)<br>        bgr = (0, 255, 0) # color of the box<br>        classes = self.model.names # Get the name of label index<br>        label_font = cv2.FONT_HERSHEY_SIMPLEX #Font for the label.<br>        cv2.rectangle(frame, \<br>                      (x1, y1), (x2, y2), \<br>                       bgr, 2) #Plot the boxes<br>        cv2.putText(frame,\<br>                    classes[labels[i]], \<br>                    (x1, y1), \<br>                    label_font, 0.9, bgr, 2) #Put a label over box.<br>        return frame</pre><p>Once done this function would produce output something like this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1towgGw7VpoYrfypxgd0ng.jpeg" /><figcaption>You can try to have different colors for different objects. (Image by Author)</figcaption></figure><h3>One Function to Unite Them All</h3><p>Sorry for the Lord of the rings reference, but yes, now we bring it all together into a single call function that performs the entire operation in a loop.</p><p>So let&#39;s go over the steps that our main function will have to perform to successfully run the application.</p><ol><li>Create a Video Streaming Input.</li><li>Load the model.</li><li>While Input is available, read the next frame.</li><li>Score the frame to get labels and coordinates.</li><li>Plot the boxes over the objects detected.</li><li>Write the processed frame onto the output video stream.</li></ol><p>Six simple steps to operate, although we will add some infrastructure code to help us make the application more robust, the basics are the same. So let&#39;s do it.</p><pre>&quot;&quot;&quot;<br>The Function below oracestrates the entire operation and performs the real-time parsing for video stream.<br>&quot;&quot;&quot;<br>def __call__(self):<br>    player = self.get_video_stream() #Get your video stream.<br>    assert player.isOpened() # Make sure that their is a stream. <br>    #Below code creates a new video writer object to write our<br>    #output stream.<br>    x_shape = int(player.get(cv2.CAP_PROP_FRAME_WIDTH))<br>    y_shape = int(player.get(cv2.CAP_PROP_FRAME_HEIGHT))<br>    four_cc = cv2.VideoWriter_fourcc(*&quot;MJPG&quot;) #Using MJPEG codex<br>    out = cv2.VideoWriter(out_file, four_cc, 20, \<br>                          (x_shape, y_shape)) <br>    ret, frame = player.read() # Read the first frame.<br>    while rect: # Run until stream is out of frames<br>        start_time = time() # We would like to measure the FPS.<br>        results = self.score_frame(frame) # Score the Frame<br>        frame = self.plot_boxes(results, frame) # Plot the boxes.<br>        end_time = time()<br>        fps = 1/np.round(end_time - start_time, 3) #Measure the FPS.<br>        print(f&quot;Frames Per Second : {fps}&quot;)<br>        out.write(frame) # Write the frame onto the output.<br>        ret, frame = player.read() # Read next frame.</pre><p>You should take all these components and pack them into a nice class which can be called along with the URL and output file you wish to write the output stream onto. Your final product will look something like this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/0*e9eu2a2tZyI2qCfN.gif" /><figcaption>A 60 FPS output stream of processed video in real-time. (Image by Author)</figcaption></figure><h3>Conclusion</h3><p>Of course production level real-time applications are way more complicated than this, but this guide does not intend to teach that. It is to show you the amazing power of Python which allows us to build such a complicated application prototype in hours. The possibilities from here are only limited by your imagination.</p><p>You can check the full code and more such awesome applications on my <a href="https://github.com/akash-agni">Github </a>profile.</p><h4>If you enjoyed the guide.</h4><blockquote>Leave a comment to let me know what you think.</blockquote><blockquote>Connect with me over <a href="https://twitter.com/agni_akash">Twitter</a>, <a href="https://www.linkedin.com/in/agni25/">Linkedin</a>, or <a href="https://www.linkedin.com/in/agni25/">Medium</a>.</blockquote><blockquote>Share the article with your network.</blockquote><p>Happy Coding!!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=70bac41148f7" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science/implementing-real-time-object-detection-system-using-pytorch-and-opencv-70bac41148f7">Implementing Real-time Object Detection System using PyTorch and OpenCV</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[A Practical Guide To MLOps Using AWS Sagemaker — Part II]]></title>
            <link>https://medium.com/data-science/a-practical-guide-to-mlops-using-aws-sagemaker-part-ii-c5159b4b51aa?source=rss-7b80f2303030------2</link>
            <guid isPermaLink="false">https://medium.com/p/c5159b4b51aa</guid>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[api]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <dc:creator><![CDATA[Akash Agnihotri]]></dc:creator>
            <pubDate>Sun, 13 Jun 2021 16:28:58 GMT</pubDate>
            <atom:updated>2021-06-13T16:28:58.186Z</atom:updated>
            <content:encoded><![CDATA[<h3>A Practical Guide To MLOps Using AWS Sagemaker — Part II</h3><h4>How to deploy your model using AWS lambda and REST API for the world to use.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*x5CS1MnMfcWTT0vP" /><figcaption>Starry Night Over the Rhône by Vincent van Gogh (<a href="https://commons.wikimedia.org/wiki/Category:Starry_Night_Over_the_Rhone_by_Vincent_van_Gogh">source</a>)</figcaption></figure><p>Before we begin, I would strongly suggest you read <a href="https://towardsdatascience.com/a-practical-guide-to-mlops-in-aws-sagemaker-part-i-1d28003f565">part-1</a> if you have not yet done so. In this guide, we will go through how to deploy your model such that front-end developers can use it in their applications using a REST API without having to worry too much about the underlying details.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/681/1*T_72LWHsI31hWYIG_fL51Q.png" /><figcaption>We need to build a pipeline connecting the user&#39;s Web Browser (left) to our model endpoint (right). (Image by author)</figcaption></figure><p>As we have already developed and deployed our model endpoint in the previous part, we will start by developing our lambda function.</p><h3>AWS Lambda Functions</h3><blockquote>AWS Lambda is a serverless compute service that lets you run code without provisioning or managing servers, creating workload-aware cluster scaling logic, maintaining event integrations, or managing runtimes.</blockquote><p>By using the AWS Lambda function we can avoid setting up dedicated servers to monitor incoming requests and execute the code. There are many benefits of this like we only pay for the compute every time the lambda function is triggered by incoming requests, instead of a dedicated server.</p><p>Follow the steps to create a lambda function that will process the incoming requests.</p><ol><li>Login to your AWS console and select lambda from the list of services.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/981/1*G0Iy3klwUmEyRwU_qoZJ6Q.png" /><figcaption>Lambda function dashboard shows how much storage you are using and other Stats. (Image by Author)</figcaption></figure><p>2. Create a new lambda function to begin coding.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/738/1*XrpYsG9o2Ohrre46EU6XCg.png" /><figcaption>Lambda function allows you to deploy a simple code package to a serverless environment. (Image by Author)</figcaption></figure><p>3. Add the name of your model endpoint as an environment variable, by clicking on the configuration tab and add a new variable, with key ‘ENDPOINT_NAME’ and value as the name of your endpoint developed.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/838/1*ygs6WXNYN8CqdkJOSEs-Yg.png" /><figcaption>Adding endpoint names as environment variables will help us in the future to automate the process when a better model is developed. (Image by Author)</figcaption></figure><p>4. Put the below code into the code editor, make sure you replace the value for the variable “<strong>bucket” </strong>with your own variable so it points to the location where you saved the transformations during model development.</p><p><strong>Note: You can find the s3 location in the notebook you created at the beginning of part-1.</strong></p><pre>import os<br>import json<br>import boto3<br>import pickle<br>import sklearn<br>import warnings<br>warnings.simplefilter(&quot;ignore&quot;)</pre><pre># grab environment variables<br><strong>ENDPOINT_NAME </strong>= os.environ[&#39;ENDPOINT_NAME&#39;]<br><strong>runtime</strong>= boto3.client(&#39;runtime.sagemaker&#39;)<br><strong>bucket </strong>= &quot;sagemaker-ap-south-1-573002217864&quot;<br><strong>key </strong>= &quot;cust-churn-model/transformation/transformation.sav&quot;<br>s3 = boto3.resource(&#39;s3&#39;)</pre><pre>def lambda_handler(event, context):<br>    <br>    payload = process_data(event)<br>    response = runtime.invoke_endpoint(EndpointName=ENDPOINT_NAME,<br>                                       ContentType=&#39;text/csv&#39;,<br>                                       Body=payload)<br>    result = json.loads(response[&#39;Body&#39;].read().decode())<br>    predicted_label = &#39;True&#39; if result &gt; 0.39 else &#39;False&#39;<br>    <br>    return predicted_label</pre><pre>def process_data(event):<br>    trans = pickle.loads(s3.Object(bucket, key).get()[&#39;Body&#39;].read())<br>    event.pop(&#39;Phone&#39;)<br>    event[&#39;Area Code&#39;] = int(event[&#39;Area Code&#39;])<br>    obj_data = [[value for key,value in event.items() if key in trans[&#39;obj_cols&#39;]]]<br>    num_data = [[value for key,value in event.items() if key in trans[&#39;num_cols&#39;]]]<br>    <br>    obj_data = trans[&#39;One_Hot&#39;].transform(obj_data).toarray()<br>    num_data = trans[&#39;scaler&#39;].transform(num_data)<br>    <br>    obj_data = [str(i) for i in obj_data[0]]<br>    num_data = [str(i) for i in num_data[0]]<br>    <br>    data = obj_data + num_data<br>    return &quot;,&quot;.join(data)</pre><p>5. One last step before we can execute our lambda function, lambda function uses vanilla python3 for execution and does not have any of the libraries like Pandas, NumPy, or sklearn installed by default. So we need to add a sklearn layer so that our transformations can be loaded.</p><p>Now I will not be going into the details of how to create and to add a layer, as it is a separate topic, you can find more details about that <a href="https://towardsdatascience.com/python-packages-in-aws-lambda-made-easy-8fbc78520e30">here</a>.</p><p>Once done, we are ready to test our lambda function, select the <strong>test</strong> drop down to configure a test case, and paste the below input data into it to run the test.</p><pre>{<br>  &quot;State&quot;: &quot;SC&quot;,<br>  &quot;Account Length&quot;: &quot;15&quot;,<br>  &quot;Area Code&quot;: &quot;836&quot;,<br>  &quot;Phone&quot;: &quot;158-8416&quot;,<br>  &quot;Int&#39;l Plan&quot;: &quot;yes&quot;,<br>  &quot;VMail Plan&quot;: &quot;no&quot;,<br>  &quot;VMail Message&quot;: &quot;0&quot;,<br>  &quot;Day Mins&quot;: &quot;10.018992664834252&quot;,<br>  &quot;Day Calls&quot;: &quot;4&quot;,<br>  &quot;Day Charge&quot;: &quot;4.226288822198435&quot;,<br>  &quot;Eve Mins&quot;: &quot;2.3250045529370977&quot;,<br>  &quot;Eve Calls&quot;: &quot;0&quot;,<br>  &quot;Eve Charge&quot;: &quot;9.97259241534841&quot;,<br>  &quot;Night Mins&quot;: &quot;7.141039871521733&quot;,<br>  &quot;Night Calls&quot;: &quot;200&quot;,<br>  &quot;Night Charge&quot;: &quot;6.436187619334115&quot;,<br>  &quot;Intl Mins&quot;: &quot;3.2217476231887012&quot;,<br>  &quot;Intl Calls&quot;: &quot;6&quot;,<br>  &quot;Intl Charge&quot;: &quot;2.559749162329034&quot;,<br>  &quot;CustServ Calls&quot;: &quot;8&quot;<br>}</pre><p>6. Once you run the test, the lambda function will run the input data through the transformations into the endpoint we have deployed to get a response on our data.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*_lZTjZqfRES422oKXmPJgQ.png" /><figcaption>You will see a response from your model if everything goes well. (Image by Author)</figcaption></figure><h3>Building REST API</h3><blockquote>An API is a set of definitions and protocols for building and integrating application software. It’s sometimes referred to as a contract between an information provider and an information user — establishing the content required from the consumer (the call) and the content required by the producer (the response).</blockquote><p>In our case, the lambda function is the <em>producer </em>which uses the model endpoint to predict a score for input <em>(the call) </em>provided by the consumers, which can be any web application developed by front-end developers.</p><ol><li>Select “<strong><em>API Gateway” </em></strong>from the list of AWS services and select<strong><em> </em></strong>the<strong><em> “Create API” </em></strong>option to create a new REST API.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eXqU6ZqgxDV9kpFnZDQvuQ.png" /><figcaption>Using a REST API we can serve our model to front-end apps. (Image by Author)</figcaption></figure><p>2. From the list of API’s select REST API and click on build.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/518/1*XgbJX6FwrZVWvspOZhNH1Q.png" /><figcaption>Build a new REST API. (Image by Author)</figcaption></figure><p>3. Select New API, give it a nice name, and create. Make sure you leave the Endpoint Type as Regional.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/601/1*J4fT1OxflF1A92qWJk6yig.png" /><figcaption>Leave Endpoint Type as Regional. (Image by Author)</figcaption></figure><p>4. Click on the <strong>Actions </strong>dropdown and select <strong><em>“Create Resource”. </em></strong>Create a new resource. Next, click on Actions again and create a new Post Method.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/430/1*_aUCtdWkILkYpk6cGeoEuA.png" /></figure><p>5. Once you create a <strong><em>Post Method</em></strong> you will get an option to integrate the method with the lambda function you created, enter the name of your lambda function to continue.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/627/1*lARTKt9v1wmpaGcczrYHLA.png" /><figcaption>Make sure all your items (lambda, API, Sagemaker studio, s3) are in the same region. (Image by Author)</figcaption></figure><p>Once you have created the API you will need to deploy it. As you can see the dashboard shows the architecture of your API and how it is integrated into your lambda function.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/887/1*ob_B2mW7lHOxLSjusoeQYQ.png" /><figcaption>API structure deployed along with your lambda functions. (Image by Author)</figcaption></figure><p>6. You can deploy the API by clicking on the <strong>Actions</strong> tab and select the <strong>Deploy API </strong>option. This will provide you with a link that you can use to sent <strong>Post </strong>requests to your Model endpoint.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/947/1*sCF-45h7j1GuSDRQ-zkamQ.png" /><figcaption>Make sure you select the post method before copying the invoke URL. (Image by Author)</figcaption></figure><h3>Bringing it All Together</h3><p>Now the moment to test if our model results are available to the world through our deployment. We can use Postman to test our API.</p><p>Create a new test in Postman and paste the link you have created from your REST API and select <strong>Body </strong>as input type and <strong>POST </strong>as the request type and provide the input data.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/945/1*uwLfPxipUdNH2sJkbBD9Ug.png" /><figcaption>Postman can be used to test your API before deployment into production. (Image by Author)</figcaption></figure><p>Once you click on send, it will send a request to your API which will pass it to the lambda function to get a response.</p><p>Hurray!! you have built a complete model deployment pipeline end-to-end.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/931/1*nndxpH5y_fIEIhoKfp_D8A.png" /><figcaption>The response will be presented by Postman along with some stats. (Image by Author)</figcaption></figure><h3>Conclusion</h3><p>In summary, we have.</p><ol><li>Built a complete CI/CD compliant model development pipeline on AWS.</li><li>Access model endpoint using AWS lambda function to perform preprocessing.</li><li>Built a REST API to exposure our model to front-end applications.</li><li>Tested our pipeline using Postman.</li></ol><p>At this stage, you should take a moment to look back at all the things you have done and try to think of new cool experiments to make this even better.</p><h4>If you found my work helpful, here is what you can do.</h4><blockquote>Post a comment to let me know your thoughts, or if you found any issues.</blockquote><blockquote>Share the article with your friends.</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c5159b4b51aa" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science/a-practical-guide-to-mlops-using-aws-sagemaker-part-ii-c5159b4b51aa">A Practical Guide To MLOps Using AWS Sagemaker — Part II</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>
    </channel>
</rss>