<?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 Viraf on Medium]]></title>
        <description><![CDATA[Stories by Viraf on Medium]]></description>
        <link>https://medium.com/@virafpatrawala?source=rss-31356c339457------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*R04h_eboCIBsrrwArUc5aA.jpeg</url>
            <title>Stories by Viraf on Medium</title>
            <link>https://medium.com/@virafpatrawala?source=rss-31356c339457------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 15:04:33 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@virafpatrawala/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[A Monkey Picks My Stock Portfolio]]></title>
            <link>https://medium.com/geekculture/a-monkey-picks-my-stock-portfolio-dc10fd665368?source=rss-31356c339457------2</link>
            <guid isPermaLink="false">https://medium.com/p/dc10fd665368</guid>
            <category><![CDATA[investment]]></category>
            <category><![CDATA[portfolio-management]]></category>
            <category><![CDATA[stocks]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Viraf]]></dc:creator>
            <pubDate>Mon, 12 Jul 2021 20:57:37 GMT</pubDate>
            <atom:updated>2021-07-12T20:57:37.862Z</atom:updated>
            <content:encoded><![CDATA[<h4>And outperforms the index for 10 straight years! Why even bother predicting/optimizing when you can just randomly pick stocks?</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rK4BReVRtUrEuvYbgthclw.jpeg" /><figcaption>Some serious thought before making any investment decisions is a must. (Original pictures by Photo by <a href="https://www.pexels.com/@weekendplayer?utm_content=attributionCopyText&amp;utm_medium=referral&amp;utm_source=pexels">Burak Kebapci</a> and <a href="https://www.pexels.com/@pixabay">Pixabay</a> from <a href="https://www.pexels.com/photo/white-android-tablet-turned-on-displaying-a-graph-186464/?utm_content=attributionCopyText&amp;utm_medium=referral&amp;utm_source=pexels">Pexels</a>)</figcaption></figure><p>An interesting quote by Burton Malkiel, a Princeton University professor, in his book titled ‘A Random Walk Down Wall Street’ went like —</p><blockquote><strong>“A blindfolded monkey throwing darts at a newspaper’s financial pages could select a portfolio that would do just as well as one carefully selected by experts.”</strong></blockquote><p>Wait. Where do I find this monkey? Will normal darts work? And how much does this monkey charge as commissions?</p><p>Jokes apart, the point this quote is trying to make, is that randomly selected stocks will perform just as well or better than the fund managers who try to ‘beat’ the market.</p><p>I was very curious about the truth behind the statement — was it just a fancy click-baity (<em>unlike this post’s title, lol</em>) sentence that looks good in a book? Also, <strong>will it hold true in the stock market of a developing country, where there are many market inefficiencies that can still be exploited?</strong></p><p>Which is why the following analysis will focus on the Indian stock market (can be replicated for any other market as well), and take the <a href="https://www1.nseindia.com/live_market/dynaContent/live_watch/equities_stock_watch.htm">NIFTY50 index</a> as the baseline to compare against the Monkey Portfolio. Later on, we shall also <strong>compare the returns with some of the large Indian funds as well</strong>.</p><p>I’ll try to keep the analysis as simple as possible. Here’s what we will do —</p><ul><li>From a total <strong>sample space of 501 stocks</strong>, we shall <strong>choose 30 stocks randomly</strong> at the start of every year.</li><li>We shall <strong>divide the portfolio amount equally</strong> between these 30 stocks.</li><li>We shall <strong>square-off our positions at the end of the year</strong>, and repeat the process all over again for the next year. This means our portfolio will be shuffled every year.</li><li>The <strong>returns at the end of 10 years</strong> will be compared to the index returns and a couple of mutual funds.</li><li>And yes, we will repeat this process <strong>100 times</strong> to ensure no flukes.</li></ul><p>Okay then, let’s get right to it!</p><h3>1. Fetch Data</h3><p>First, the required imports:</p><pre>import pandas as pd<br>import numpy as np<br>import yfinance as yf<br>import matplotlib.pyplot as plt<br>from datetime import datetime</pre><p>Before we can fetch the data, we need to know the ticker names for our 501-stock-sample-space. We can download the current Nifty500 stock list (<em>yes, it has 50</em><strong><em>1</em></strong><em> stocks — I was surprised too</em>) from the official website <a href="https://www1.nseindia.com/products/content/equities/indices/nifty_500.htm">here</a>.</p><p><em>(Do note that for an ideal analysis, we would need to consider the Nifty500 stocks from every year, and not only the current Nifty500. However, I could not find such a historical database, so we’ll have to manage with what we have for now.)</em></p><pre># Read the csv file<br>tickers_df = pd.read_csv(&quot;ind_nifty500list.csv&quot;)</pre><pre># Yfinance tickers have &#39;.NS&#39; as a suffix for indian stock tickers<br>tickers_df[&#39;Yfinance_tickers&#39;] = tickers_df[&#39;Symbol&#39;] + &#39;.NS&#39;<br>tickers_df.head()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1021/1*gG1rVsFiUBYi5rPCI2x7rQ.png" /><figcaption>A summary of the Dataframe consisting of Nifty500 stocks</figcaption></figure><p>Next, we can fetch the data for the past 10 years from yahoo finance using the <a href="https://pypi.org/project/yfinance/">yfinance python api</a> by making simple manipulations to the ticker-names.</p><p>I found that <strong>fetching the data on a daily timescale</strong> and then <strong>resampling</strong> it to get the yearly dataframe was the best way to do it.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8255b0232d66c89bb7066b1c4c78f910/href">https://medium.com/media/8255b0232d66c89bb7066b1c4c78f910/href</a></iframe><p>And here is how the final dataframe looks — <strong>close</strong> values for the <strong>501 tickers</strong> from <strong>2009 to 2019 </strong>(with many NaNs since those stocks didn’t exist at that time).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*X_FK4SpKHvzwBQrWxP9_MQ.png" /><figcaption>The final resampled dataframe with the yearly close prices for the 501 tickers.</figcaption></figure><p>We shall repeat the above process to fetch the Nifty50 index data as well.</p><h3>2. The code</h3><p>Now that we have the data, let’s make the function which shall create the random portfolios, square-off at the end of the year, and calculate returns.</p><p>To summarize the code below:</p><ul><li>Loop for 10 years from 2010 to the end of 2019.</li><li>Every year, filter out the stocks which weren’t existing at that point of time.</li><li>From the remaining stock list, choose 30 stocks randomly, and invest equal amount of the portfolio value in each of the 30 stocks.</li><li>At the end of the year, the returns for each stock is multiplied with the amount invested, and the portfolio value is updated.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0b040c84260f6c342985e01b4764ce78/href">https://medium.com/media/0b040c84260f6c342985e01b4764ce78/href</a></iframe><p>Please read the code comments to understand the logic flow better. The final output is a dataframe which contains the stocks in the portfolio and portfolio value for each year, and also the corresponding Nifty50 index returns.</p><h3>3. Comparison</h3><p>Finally, let’s see how our monkey-portfolios have done against the index.</p><pre>portfolio_size = 30<br>starting_amount = 100</pre><pre>monkey_portfolio = monkey_portfolio_function(data, nifty50_data, starting_amount, portfolio_size)<br>monkey_portfolio.plot(y=[&quot;MonkeyPortfolio Value&quot;, &quot;Nifty50&quot;])</pre><p>Plotting the output for a single execution of the above code —</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/475/1*SRA484HlHs8vzyFaXPGWvg.png" /><figcaption>Comparison of the Monkey Portfolio v/s the Nifty50 index for a period of 10 years</figcaption></figure><p>Our Monkey Portfolio clearly beats the index, that too every single year! The Nifty50 portfolio returns are around 144% whereas the Monkey Portfolio has more than 550% returns!</p><p>Now, repeating this process for a 100 times, and plotting a box plot for the Monkey Portfolio returns —</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/472/1*pa17JIwTyxUJR934MsP9kA.png" /><figcaption>Monkey Portfolio returns (100 runs) box plot v/s Nifty50 returns.</figcaption></figure><p>The following is a summary of the 100 runs:</p><pre>Maximum returns (%) = 1019.9819061774933<br>Minimum returns (%) = 282.90673885903345<br>Average returns (%) = 584.2476825941768</pre><p>The returns’ spread is quite wide, but even the lowest returns value of the Monkey portfolio is almost twice that of the Nifty50 index returns…</p><p><strong>Now let’s compare it with some of the big Indian fund houses —</strong></p><p>The HDFC Large and Midcap fund barely manages to double the initial amount over a period of 10 years. This is lesser than even the index returns.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*B8azbUqmMibqMESKsrYawQ.png" /><figcaption><a href="https://www.moneycontrol.com/mutual-funds/nav/hdfc-growth-opportunities-fund/MMS001">HDFC Large and Midcap Fund</a></figcaption></figure><p>Let’s try one more. But this time, we shall look at a <strong>small-cap fund</strong>. The SBI small cap fund has a dismal run in the first four years as compared to the Monkey Portfolio, but it does quite well in the next six years and has returns of 400%!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SPZBr93mhyMWMJrdNWzeBQ.png" /><figcaption><a href="https://www.moneycontrol.com/mutual-funds/nav/sbi-small-cap-fund/MSA012">SBI Small Cap Fund</a></figcaption></figure><p><em>You can find the entire code for this tutorial in my </em><a href="https://github.com/virafpatrawala/Monkey-Portfolio-Selector"><em>GitHub repository</em></a><em>.</em></p><h3>Concluding Words…</h3><p>Although the SBI small cap fund does not beat the highest (or even average) Monkey Portfolio returns, it reveals an important thing — we had been initially comparing the Monkey Portfolio against the wrong baseline! Since the <strong>501 stocks in our sample space include a majority of small-cap companies</strong> (as compared to Nifty50 which only includes large-cap companies), the returns are bound to be larger. But at the same time, the <strong>volatility and risk of the Monkey Portfolio will be higher as well</strong>.</p><p>The above analysis wasn’t the most ideal, but it gives us a good idea behind the thought process of Burton Malkiel, and goes to show that his statement wasn’t entirely a gimmick.</p><p>So the question remains — do we need portfolio managers? Or fund houses? If random selection more often than not performs better than several highly educated humans sitting in fancy offices, why even bother entrusting your hard-earned money to them? And pay them a commission on top of that!</p><p><em>Finally, if you have any ideas/feedback that can help improve the above analysis, do let me know!</em></p><p><em>Do you want to be rich overnight using ML in stocks? This article is (NOT) for you!</em></p><p><a href="https://towardsdatascience.com/how-not-to-predict-stock-prices-with-lstms-a51f564ccbca">How (NOT) To Predict Stock Prices With LSTMs</a></p><p><em>Can an ML model literally read visual stock price charts? Check out my article below to see the results…</em></p><p><a href="https://towardsdatascience.com/can-an-ml-model-read-stock-charts-and-predict-prices-fb73c551c7a4">Can a Machine Learning Model Read Stock Charts and Predict Prices?</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=dc10fd665368" width="1" height="1" alt=""><hr><p><a href="https://medium.com/geekculture/a-monkey-picks-my-stock-portfolio-dc10fd665368">A Monkey Picks My Stock Portfolio</a> was originally published in <a href="https://medium.com/geekculture">Geek Culture</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Can a Machine Learning Model Read Stock Charts and Predict Prices?]]></title>
            <link>https://medium.com/data-science/can-an-ml-model-read-stock-charts-and-predict-prices-fb73c551c7a4?source=rss-31356c339457------2</link>
            <guid isPermaLink="false">https://medium.com/p/fb73c551c7a4</guid>
            <category><![CDATA[stocks]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[money]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Viraf]]></dc:creator>
            <pubDate>Tue, 01 Sep 2020 18:18:51 GMT</pubDate>
            <atom:updated>2020-09-01T19:37:47.055Z</atom:updated>
            <content:encoded><![CDATA[<h4>What if your Machine Learning model could literally read the stock price charts like a human?</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Bz_7BFKWGRx2ByXjpy86sg.jpeg" /><figcaption>Original photos by <a href="https://www.pexels.com/@agk42?utm_content=attributionCopyText&amp;utm_medium=referral&amp;utm_source=pexels">Alex Knight</a> from <a href="https://www.pexels.com/photo/high-angle-photo-of-robot-2599244/?utm_content=attributionCopyText&amp;utm_medium=referral&amp;utm_source=pexels">Pexels</a> and <a href="https://unsplash.com/@adamaszczos?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Adam Nowakowski</a> on <a href="https://unsplash.com/s/photos/stock-market?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p><strong><em>Note from Towards Data Science’s editors:</em></strong><em> While we allow independent authors to publish articles in accordance with our </em><a href="https://towardsdatascience.com/questions-96667b06af5"><em>rules and guidelines</em></a><em>, we do not endorse each author’s contribution. You should not rely on an author’s works without seeking professional advice. See our </em><a href="https://towardsdatascience.com/readers-terms-b5d780a700a4"><em>Reader Terms</em></a><em> for details.</em></p><p>Most of the ML models out there are trying to predict stock prices (or the changes in stock prices) using historical price data and other technical indicators — i.e., NUMERIC INPUTS. But, I asked myself, why can’t an <strong>ML model replicate precisely how a human trades</strong> <strong>in the stock markets?</strong></p><p>Any average person would open up the stock price charts (<a href="https://www.investopedia.com/trading/candlestick-charting-what-is-it/">candlestick charts</a>), and try to find some patterns in the price data. Maybe next, apply some technical indicators, peek a look at the company fundamentals, and finally make a decision on the next movement of the stock price as a combined output of all the charted and numeric inputs. <strong><em>What if an ML model could also make these conclusions from visual information?</em></strong></p><p>So let’s pass the price history time series as visual information to a CNN model as input. Will the model recognize the patterns in the visual data like a human does? Will this model perform better than a model that takes numeric data as input? Well, let’s find out!</p><p>Before we start, in my last article, we explore why using LSTMs blindly to predict stock prices is going to land you in trouble. You can read that <a href="https://towardsdatascience.com/how-not-to-predict-stock-prices-with-lstms-a51f564ccbca">here</a>. Already read that? Good, now we can proceed.</p><p><strong>And next, a disclaimer.</strong> <em>None of this is financial advice, nor should you implement it directly in practice. Consider this as an experiment and nothing more. Cool, now this aside, we can get on with our experimenting!</em></p><p>Instead of predicting stock prices, we shall predict the <strong>direction of price movement </strong>for the next day, i.e., whether the stock price will go up or down or sideways. Let’s get started!</p><p>The flow of this article is as follows:</p><ul><li>Get historical stock data in python.</li><li>Convert the price data to a visual representation</li><li>Build and train a model with Tensorflow Keras.</li><li>Predict and interpret the results.</li></ul><h3>Step 1: Get Stock Data</h3><p>There are multiple options to get access to historical stock prices in python, but one of the most straightforward libraries is <a href="https://pypi.org/project/yfinance/"><strong>yfinance</strong></a>. Quite convenient and free, it gets the job done by scraping data from <a href="https://sg.finance.yahoo.com/">yahoo finance</a>.</p><pre>!pip install yfinance</pre><pre>## Import the required libraries<br>import yfinance as yf<br>import pandas as pd<br>import numpy as np<br>import matplotlib.pyplot as plt</pre><pre>from pandas.plotting import register_matplotlib_converters<br>register_matplotlib_converters()</pre><p><em>For this article, I will take up the stock prices of ‘Reliance,’ the largest corporation in the Indian stock markets, but feel free to try this out for any other instrument as well.</em></p><p>The following piece of code downloads stock price data for Reliance a period of around 18 years (I am intentionally avoiding the COVID period) with a resolution of 1 day and stores it in a pandas dataframe. You can vary these parameters as you deem fit for your experiments.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/24d030aaa10f0f014a5e6f5600f74a5f/href">https://medium.com/media/24d030aaa10f0f014a5e6f5600f74a5f/href</a></iframe><p>Printing out the head of the pandas dataframe, you can see the various parameters like ‘Open’, ‘Close’, ‘High’, ‘Low’, ‘Volume’ available for the stock data.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/512/1*ZVroOvJk8LqBoLjsAAbRmg.png" /><figcaption>Pandas dataframe top for Reliance stock data</figcaption></figure><p>Plotting out the ‘Close’ price to visualize the data, see how nicely the stock has risen in the last few years.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4mFHrrag1fM5JIsg8hsKDg.png" /><figcaption>Close price for Reliance stock data</figcaption></figure><p>Now we shall add some additional features to our data. First, calculate the percentage change from the previous day close. Next based this change, generate a signal which is:</p><ul><li>0 (if the change is lesser than -2.5%)</li><li>1 (if the change is between -2.5% and -1%)</li><li>2 (if the change is between -1% and 1%)</li><li>3 (if the change is between 1% and 2.5%)</li><li>4 (if the change is greater than 2.5%)</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e5a17246ce212b492852f6bbd17073f1/href">https://medium.com/media/e5a17246ce212b492852f6bbd17073f1/href</a></iframe><p>The dataframe now looks like:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/767/1*YeGCciW3aNlxgbi_V2YrUA.png" /><figcaption>Dataframe with added features</figcaption></figure><p><em>Now, this is completely arbitrary, you can vary it as you please. You can break the change into an even more/lesser number of parts (adjust the last layer of the model accordingly).</em></p><h3>Step 2: Visual Representation of Stock Prices</h3><p>Now, this step is very important, since it will decide what the model sees. For this article, I want to keep the images as close as possible to the candlestick charts a human trader sees. A constraint is that every input image has to be of the same dimensions and must contain sufficient information for the model to make conclusions from.</p><p>We shall have a <strong>lookback period</strong>, for example, the <strong>past 50 days</strong> (10 trading weeks), which shall be represented. For simplicity, for each of these 50 days, the <strong>open and close</strong>, and the <strong>direction of the price movement</strong> will be encoded.</p><p>Since images of the same dimension will be fed to the neural network, it might not understand where this image stands in the entirety of the 15 years. For example, in 2002, the prices are at ~80–90 whereas they slowly rise with time to ~1800. Although price patterns may (or may not) be independent of the absolute stock prices, I wanted to encode some of this information as well in the images. So, to give the image some context to the past prices and their absolute price levels, the 50-day price representations will be scaled in a bigger time window which considers additional previous price values.</p><p>The code for generating this visual representation is:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b90c70c0ca2b3bda8ad0f4f181171cb4/href">https://medium.com/media/b90c70c0ca2b3bda8ad0f4f181171cb4/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/475/1*lY6c0NxIHEzxi0pOPWlZRQ.png" /><figcaption>Visual representation of Open and Close for a lookback period of 50 days</figcaption></figure><p><em>Note: This is a visual representation very close to what we see on the candlestick charts (that was the whole point). However, you can use your imagination (and some discretion) to create a completely different visual representation with other encoded parameters as well. Because the way a CNN model sees and learns from an image might (or might not) be very different from how we do!</em></p><p>Next, let’s create a <strong>data generator</strong> that will iterate through the 15 years of data and create pairs of images and the corresponding next-day-predictions.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/aa72ac90f8a4569cfd5d373f2fb18d14/href">https://medium.com/media/aa72ac90f8a4569cfd5d373f2fb18d14/href</a></iframe><p>This data generates image-prediction batches like</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/484/1*urJoVYMo9FYIhWv1f8yNeA.png" /><figcaption>A batch of 4 generated images with their predictions</figcaption></figure><h3>Step 3: Building and Training an ML Model</h3><p>Since we have inputs as images and require outputs as one of three classes (up, down, no movement), our model will have a few convolutional layers, followed by a few dense layers and finally a softmax function.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6ae7d74e2a907e82caeb8fb22b2d1974/href">https://medium.com/media/6ae7d74e2a907e82caeb8fb22b2d1974/href</a></iframe><p>Let’s initiate the generator objects and start the training.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9a31afb0f33d1a8512ecdfeb8e307f5f/href">https://medium.com/media/9a31afb0f33d1a8512ecdfeb8e307f5f/href</a></iframe><h3>Step 4: Visualizing the Results</h3><p>Post-training, the losses can be plotted as:</p><pre>plt.plot(history.history[&#39;loss&#39;], label=&#39;train&#39;)<br>plt.plot(history.history[&#39;val_loss&#39;], label=&#39;val&#39;)<br>plt.legend()<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/373/1*M_yqeZc1o1NZCnXFX4E0NQ.png" /><figcaption>Plotting the losses</figcaption></figure><p>Let’s now see the accuracy on the test data.</p><pre>## Evaluating the performance of the model<br>print(model.evaluate(test_gen,steps = (len(data)-split_test)//batch_size))</pre><pre>&gt;&gt;&gt; loss: 1.3830 - accuracy: 0.4375</pre><p>Well, given 5 possible outcomes, the accuracy of randomly guessing one would be 0.2. So this model seems to have performed quite well comparatively. Or has it?</p><p>Plotting out the predicted outputs, it’s clear what has happened. The model has not learned anything useful yet, so it ends up predicting “2” as every output resulting in relatively high accuracy.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2F8av8uq9TDIFcwNqGus0g.png" /><figcaption>Predicted vs True Outputs</figcaption></figure><p>Let’s train the model some more then. But now, the validation loss increases greatly. Plus, testing gives an accuracy of less than 0.3. Not very encouraging, the model has probably overfitted on the training data. A very clear reason for this is the <strong>small size of training data compared to the model size</strong>.</p><h3>Concluding Words…</h3><p>Although the performance was not extremely encouraging, this was quite an innovative approach of looking at the age-old problem of predicting stock prices. A few more ideas on these lines are giving multi-timeframe inputs (say, weekly data as inputs as well), or additionally providing technical indicators as numeric data (in parallel with visual charts), or transform the price data into other visual domains (doesn’t have to be the charts as we see them), etc. What else do you think we can do to improve this model? Do you have any other interesting ideas?</p><p><strong>After trying out all these ideas, the question remains — is feeding visual data providing any edge that numeric data cannot?<em> </em></strong>What do you think?</p><p>You can find the entire code in my <a href="https://github.com/virafpatrawala/StocksPredict-ImageCNN">GitHub repository</a>.</p><p><em>Finally, I am not saying I am an expert in these fields — I am just putting forward my explorations on this topic, so feel free to point out my errors or add anything I missed. I would love to hear your feedback.</em></p><p><em>Do you want to be rich overnight using ML in stocks? This article is (NOT) for you!</em></p><p><a href="https://towardsdatascience.com/how-not-to-predict-stock-prices-with-lstms-a51f564ccbca">How (NOT) To Predict Stock Prices With LSTMs</a></p><p><em>Or check out some of my other machine learning articles. I’m sure you’ll find them useful…</em></p><ul><li><a href="https://towardsdatascience.com/master-the-coco-dataset-for-semantic-image-segmentation-part-1-of-2-732712631047">Master the COCO Dataset for Semantic Image Segmentation</a></li><li><a href="https://towardsdatascience.com/create-a-synthetic-image-dataset-the-what-the-why-and-the-how-f820e6b6f718">Create A Synthetic Image Dataset — The “What”, The “Why” and The “How”</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fb73c551c7a4" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science/can-an-ml-model-read-stock-charts-and-predict-prices-fb73c551c7a4">Can a Machine Learning Model Read Stock Charts and Predict Prices?</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[How (NOT) To Predict Stock Prices With LSTMs]]></title>
            <link>https://medium.com/data-science/how-not-to-predict-stock-prices-with-lstms-a51f564ccbca?source=rss-31356c339457------2</link>
            <guid isPermaLink="false">https://medium.com/p/a51f564ccbca</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[lstm]]></category>
            <category><![CDATA[stocks]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Viraf]]></dc:creator>
            <pubDate>Tue, 30 Jun 2020 00:12:51 GMT</pubDate>
            <atom:updated>2021-07-19T05:48:59.543Z</atom:updated>
            <content:encoded><![CDATA[<h4>Stocks and Machine Learning — a combination made in heaven. But are things as easy as they appear to be?</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xq-r8wnLXE9jFQ4uuLAeRw.jpeg" /><figcaption>Want to be an overnight millionaire? This article is (NOT) for you! [Original photos by <a href="https://www.pexels.com/@karolina-grabowska?utm_content=attributionCopyText&amp;utm_medium=referral&amp;utm_source=pexels">Karolina Grabowska</a> from <a href="https://www.pexels.com/photo/crop-farmer-showing-money-in-green-summer-field-in-countryside-4497591/?utm_content=attributionCopyText&amp;utm_medium=referral&amp;utm_source=pexels">Pexels</a>, <a href="https://www.pexels.com/@energepic-com-27411?utm_content=attributionCopyText&amp;utm_medium=referral&amp;utm_source=pexels">energepic.com</a> from <a href="https://www.pexels.com/photo/blue-and-yellow-graph-on-stock-market-monitor-159888/?utm_content=attributionCopyText&amp;utm_medium=referral&amp;utm_source=pexels">Pexels</a>]</figcaption></figure><p>Not so recently, a brilliant and <em>‘original’ </em>idea suddenly struck me — what if I could predict stock prices using Machine Learning. After all, a time series can be easily modeled with an LSTM. I could see myself getting rich overnight! If this is so easy, why hasn’t anyone done it yet?</p><p>Very excited at my bright prospects, I powered up my laptop, opened Google, and keyed in “predict stock prices LSTM python.” The results poured in — and very quickly, I realized that my idea was not very original. Well, so much for that. Anyway, I went through many of these articles, and each one of them seemed to have gotten surprisingly good results. But the tutorial ends there — no one put it to test with real money. Why aren’t these people millionaires yet? Something was fishy.</p><p><strong>As a disclaimer, note that none of the contents of this article is financial advice and is purely educational</strong>. Quite obvious, I know, but needs to be said. On the contrary, I am trying to educate you on how to not be fooled by this, and start using your head.</p><blockquote>Nowadays, with Stocks and Machine Learning both becoming so hands-on, easy-to-use, and accessible, it has become really hard to not fall into this trap, where an incomplete, half-baked knowledge of both fields can lead you into serious trouble.</blockquote><p>Let’s get started on how to NOT use an LSTM for predicting stock prices. The flow of this article is as follows:</p><ul><li>A simple introduction to LSTMs.</li><li>Get historical stock data in python.</li><li>Create a dataset in a format suitable for the LSTM model.</li><li>Build and train the LSTM model with TensorFlow Keras.</li><li>Predict and interpret the results.</li></ul><h3>An Intro to LSTMs</h3><p>Long-Short-Term-Memory (LSTM) networks are a type of neural network commonly used to predict time series data. In simple words, they have a memory/cache functionality which helps them learn the long term dependencies and relations in the data. So, looking at the previous <em>N </em>data points, they can predict the next (or next few) points by learning the patterns.</p><p><em>I am going to keep this part quite simple. If you want more clarity on how an LSTM functions, I recommend going through </em><a href="https://towardsdatascience.com/illustrated-guide-to-lstms-and-gru-s-a-step-by-step-explanation-44e9eb85bf21"><em>this</em></a><em>, but you should pretty much understand the main gist and purpose of this article even without an in-depth understanding.</em></p><h3>Step 1: Get Stock Data</h3><p>There are multiple options to get access to historical stock prices in python, but one of the simplest libraries is <a href="https://pypi.org/project/yfinance/"><strong>yfinance</strong></a>. Quite convenient and free, it gets the job done by scraping data from <a href="https://sg.finance.yahoo.com/">yahoo finance</a>.</p><pre>!pip install yfinance</pre><pre># Import the required libraries<br>import yfinance as yf<br>import pandas as pd<br>import matplotlib.pyplot as plt</pre><pre>from pandas.plotting import register_matplotlib_converters<br>register_matplotlib_converters()</pre><p><em>For this article, I will take up the stock prices of ‘Reliance,’ but this is valid for all other instruments as well.</em></p><p>The following piece of code downloads stock price data for Reliance over 15 years with a resolution of 1 day and stores it in a pandas dataframe. You can vary these parameters as you deem fit for your experiments.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/786114fd499a9f9ff7766c7887797499/href">https://medium.com/media/786114fd499a9f9ff7766c7887797499/href</a></iframe><p>Printing out the head of the pandas dataframe, you can see the various parameters available for the stock data.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/574/1*IiN7W5eZnsBvZLPbKaD1Rw.png" /><figcaption>Pandas dataframe top for Reliance stock data</figcaption></figure><p>Plotting out the ‘close’ price to visualize the data, see how nicely the stock has risen in the last few years. The steep drop in March 2020 (due to COVID) is also visible. But it has seemingly beaten this downfall and risen to a new high yet again.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FOtwImlllMQkXZa262fIcg.png" /><figcaption>Close price for Reliance stock data</figcaption></figure><p><em>A question to ask yourself here — </em><strong><em>do you think an ML model will be able to capture all this randomness?</em></strong></p><h3>Step 2: Create the Dataset</h3><p>Let’s fix our problem statement now — <strong>the LSTM model shall see the close prices for the last 10 days (called the time_step) and predict the close price for the next day.</strong></p><p>For simplicity, let’s test on only the final 200 days. We can train on the rest of the data. We have 3486 data points to train on (will increase depending on the day you execute the code).</p><p><em>(Note that you can vary this according to your requirements, but I feel this should be sufficient to put forth my point).</em></p><pre># Get Close data<br>df = data[[&#39;Close&#39;]].copy()</pre><pre># Split data into train and test<br>train, test = df.iloc[0:-200], df.iloc[-200:len(df)]</pre><pre>print(len(train), len(test))<br>&gt;&gt;&gt; 3486 200</pre><p>Now, we need to normalize our data. <strong><em>One common mistake people make at this stage is that they normalize the test data separately as well</em></strong>. But in a practical scenario, the test data will be in real-time, so you won’t know the minimum or maximum or average values beforehand! <em>If you provide the model with this information from the future</em>, <em>you basically end up providing a trend or guideline</em> <em>that the predicted prices need to follow.</em> I have seen so many authors make this error, and I don’t blame them since it took me a while to realize this too.</p><p>To tackle this issue, we will use the minimum and maximum of the training data to normalize the test data as well. This should work as a decent approximation (of course, there are better ways to do it, for example, take min and max values of only last N prices).</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ed5b8324545e2b7166aee686060fd6a1/href">https://medium.com/media/ed5b8324545e2b7166aee686060fd6a1/href</a></iframe><p>Finally, let’s structure the data so that our LSTM model can easily read them. The LSTM requires input in the form of <strong>[samples, time_steps, features]</strong>. The following code creates the dataset in this needed format.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/169d9060804a38671c8dd50a3bdd8b03/href">https://medium.com/media/169d9060804a38671c8dd50a3bdd8b03/href</a></iframe><h3>Step 3: Create the Model and Train</h3><p>For our model, we will be using the TensorFlow Keras library in python. The simple sequential model has an LSTM layer followed by a dropout (to reduce over-fitting) and a final dense layer (our output prediction). We shall use the ever-dependable ‘Adam’ optimizer with Mean Absolute Error loss. Train it for about 50 epochs. Remember to keep shuffle = False (since the sequence of samples is important — kind of the whole point here :P).</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/109a7d210afc1961fccd11e17277a638/href">https://medium.com/media/109a7d210afc1961fccd11e17277a638/href</a></iframe><p>The summary of the model is as shown in the picture below. The model is very minimal, but you can try and play around with a few more layers and whatnot. Be careful not to overfit the data though (even with this small model the data overfits).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/518/1*LzYVeZYjBRpSo7IiKTSbdw.png" /><figcaption>Summary of our LSTM model</figcaption></figure><p>Post-training, plot the loss as:</p><pre>plt.plot(history.history[&#39;loss&#39;], label=&#39;train&#39;)<br>plt.legend();<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/402/1*TNWXhnrqV_PwjxCB0z183Q.png" /><figcaption>The training loss</figcaption></figure><p>The training loss has clearly reduced over the epochs (with some ripples). The model seems to have trained well. Or has it? Let’s evaluate it.</p><h4>Step 4: Predict!</h4><p>Pass the test set through the model. Rescale the results (remember, we had normalized it earlier) and finally plot them out to see how it performed visually.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4915be7b80fe4830b9a736e2339d8c4e/href">https://medium.com/media/4915be7b80fe4830b9a736e2339d8c4e/href</a></iframe><p>Look at the predictions in the image below! They are shockingly good! The model seems to have predicted the COVID fall as well! This is brilliant! We can make millions in the stock market now!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aIeuZpeHa69iQ2NwgwzXvA.png" /><figcaption>Predictions of the model</figcaption></figure><p>Most tutorials end here. The authors’ final words being ‘The results look promising, tweak the parameters, alter the model, etc.’ <strong>But wait, before you go quit your job to become a full-time trader,</strong> <strong>let’s zoom into the results once!</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tKXGNLkbZVjJdvGx5TSloA.png" /><figcaption>Zoom in to the predictions</figcaption></figure><p>Can you see what is happening now? The predicted price is trailing the true price by 1 time-step. <strong>The model is just predicting a value close to the previous price it sees, as this is the best prediction for the next price.</strong></p><p>Let me make it easier for you. Let’s plot the true price value with a lag of 1 day in the image below. See how the shapes of the plots match — now it is clearer to see that our model is just trying to mimic the last price it sees.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gz6kaehkvNjznlB06biFlQ.png" /><figcaption>The model just mimics the previous price value</figcaption></figure><p><em>What seems to be the problem?</em> The LSTM is not doing anything wrong! As you know, stocks are quite dynamic and random (for all practical purposes). The model is giving the best possible guess for a <a href="https://en.wikipedia.org/wiki/Random_walk"><strong>random walk problem</strong></a>, which turns out to be the previous day’s price. But this means that the model is not predicting anything useful for trading/investing. And that is why blindly using this in practice is very dangerous.</p><h3>Concluding Words</h3><p><em>I am not saying that the task of predicting stock prices is impossible.</em> It definitely is possible — there are several algorithms and trading bots out there, which employ some amount of machine learning — but it is not going to be so damn straightforward.</p><p>A little more effort will be required to formulate the problem statement, inputs, and model. Some good direction to head in would be <strong>predicting changes in value (a derivative or second derivative) instead of the value itself</strong>. Predicting only the <strong>stock price movements (binary classification problem) </strong>could be another option. Or to use an <strong>ensemble of models</strong> to achieve combined/different goals. I’d recommend that you try to gain stronger domain knowledge in these fields first, followed by explorations — don’t restrict yourself to these ideas or other ideas you see on the internet.</p><p>I am not saying I am an expert in these fields — I am just putting forward the conclusions I got to after my explorations with this topic, so feel free to point out my errors or add anything I missed. Thank you for your time.</p><p><em>Can an ML model literally read visual stock price charts? Check out my article below to see the results!</em></p><p><a href="https://towardsdatascience.com/can-an-ml-model-read-stock-charts-and-predict-prices-fb73c551c7a4">Can a Machine Learning Model Read Stock Charts and Predict Prices?</a></p><p><em>Why waste time and effort in trying to predict the stock market when you can do this…</em></p><p><a href="https://medium.com/geekculture/a-monkey-picks-my-stock-portfolio-dc10fd665368">A Monkey Picks My Stock Portfolio</a></p><p><em>Or, check out some of my other machine learning articles. I’m sure you’ll find them useful…</em></p><ul><li><a href="https://towardsdatascience.com/master-the-coco-dataset-for-semantic-image-segmentation-part-1-of-2-732712631047">Master the COCO Dataset for Semantic Image Segmentation</a></li><li><a href="https://towardsdatascience.com/create-a-synthetic-image-dataset-the-what-the-why-and-the-how-f820e6b6f718">Create A Synthetic Image Dataset — The “What”, The “Why” and The “How”</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a51f564ccbca" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science/how-not-to-predict-stock-prices-with-lstms-a51f564ccbca">How (NOT) To Predict Stock Prices With LSTMs</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[Fail Harder]]></title>
            <link>https://medium.com/the-junction/fail-harder-fae0ac71dbe9?source=rss-31356c339457------2</link>
            <guid isPermaLink="false">https://medium.com/p/fae0ac71dbe9</guid>
            <category><![CDATA[poetry]]></category>
            <category><![CDATA[failure]]></category>
            <category><![CDATA[never-give-up]]></category>
            <category><![CDATA[real-life-experiences]]></category>
            <category><![CDATA[life]]></category>
            <dc:creator><![CDATA[Viraf]]></dc:creator>
            <pubDate>Sat, 13 Jun 2020 19:09:25 GMT</pubDate>
            <atom:updated>2020-06-13T19:09:25.505Z</atom:updated>
            <content:encoded><![CDATA[<h4>Reflections of Life</h4><h4>Life will put you down. Again and again. Will you fight back?</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*DjR97JGyhV9bTvsF" /><figcaption>Photo by <a href="https://unsplash.com/@rade_nugroho?utm_source=medium&amp;utm_medium=referral">Rade Nugroho</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Shattered, broken, like bits of glass<br>Lay my heart in solace,<br>Nothing left to look up to,<br>Nothing more I could do,<br>I had failed — of what use was life?</p><p>Thoughts, memories flew by<br>Tears burnt my eyes,<br>I tried to scream away the frustration,<br>Destroy those vicious thoughts, but<br>I had failed — did I have any right?</p><p>Who were they to judge me?<br>Comparison, criticism,<br>Tore at me, their sharp claws.<br>I looked up, a shimmer of hope, but<br>I had failed — did I get a second chance?</p><p>Reviving, rebuilding myself,<br>I strived, I sweated, I fought,<br>I swore to do whatever it took.<br>Suddenly my hopes soared high, but<br>I had failed — did I have the strength?</p><p>The time has come, and now I am<br>Stronger, wiser, more weathered, <br>Confidence, courage leap at my feet,<br>And give my best I will, but<br>I had failed — could I now succeed?</p><p>The fated day came, and I had to show them<br>I was no less. I was not weak.<br>I had come a long way since last time,<br>I couldn’t afford to fail, that I knew, but<br>I had failed — and this time, I failed again.</p><p>The end — or is it?<br>This time, at least I am not afraid<br>To stand up, to take one more shot — <br>To fail harder.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fae0ac71dbe9" width="1" height="1" alt=""><hr><p><a href="https://medium.com/the-junction/fail-harder-fae0ac71dbe9">Fail Harder</a> was originally published in <a href="https://medium.com/the-junction">The Junction</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Am I Racist?]]></title>
            <link>https://medium.com/the-friday-fix/am-i-racist-71b4113da975?source=rss-31356c339457------2</link>
            <guid isPermaLink="false">https://medium.com/p/71b4113da975</guid>
            <category><![CDATA[microfiction]]></category>
            <category><![CDATA[short-story]]></category>
            <category><![CDATA[racism]]></category>
            <category><![CDATA[flash-fiction]]></category>
            <category><![CDATA[japan]]></category>
            <dc:creator><![CDATA[Viraf]]></dc:creator>
            <pubDate>Mon, 08 Jun 2020 11:01:01 GMT</pubDate>
            <atom:updated>2020-06-09T08:30:28.778Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*3qnnl5PsstzEBAQr" /><figcaption>Photo by <a href="https://unsplash.com/@jiangxulei1990?utm_source=medium&amp;utm_medium=referral">青 晨</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>She boarded the bus and sat right beside me. I turned, hoping to greet her with a smile. She kept looking straight ahead. Weird. We played badminton together just last week. Had she already forgotten me? Wait. This was <em>her</em>, right?</p><p>Life’s tough for a foreigner in an Asian country.</p><p>Check out some of my other work…</p><p><a href="https://curiosityneverkilledthewriter.com/tokyo-dreams-460264ed09c0">Tokyo Dreams</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=71b4113da975" width="1" height="1" alt=""><hr><p><a href="https://medium.com/the-friday-fix/am-i-racist-71b4113da975">Am I Racist?</a> was originally published in <a href="https://medium.com/the-friday-fix">The Friday Fix</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Tokyo Dreams]]></title>
            <link>https://curiosityneverkilledthewriter.com/tokyo-dreams-460264ed09c0?source=rss-31356c339457------2</link>
            <guid isPermaLink="false">https://medium.com/p/460264ed09c0</guid>
            <category><![CDATA[thriller]]></category>
            <category><![CDATA[fiction]]></category>
            <category><![CDATA[japan]]></category>
            <category><![CDATA[short-story]]></category>
            <category><![CDATA[life]]></category>
            <dc:creator><![CDATA[Viraf]]></dc:creator>
            <pubDate>Fri, 05 Jun 2020 17:01:01 GMT</pubDate>
            <atom:updated>2020-06-19T00:59:31.545Z</atom:updated>
            <content:encoded><![CDATA[<h4>Fiction, but real</h4><h4>Dreams can be turned upside down in a second.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*-VH92S3ziwh9gKuv" /><figcaption>Photo by <a href="https://unsplash.com/@studio_k_pdx?utm_source=medium&amp;utm_medium=referral">Ken Wang</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p><em>Nakano, Tokyo, Japan. 11:54 PM Friday:</em></p><p>The phone vibrated twice in Akhil’s pocket as the day’s penultimate train slowly pulled into Nakano metro station. One of the busiest train lines in all of Japan during the morning peak hours, the Tōzai line was relatively empty at this time of the night, carrying mostly worn-out salarymen and drunk college kids. The train slowly ground to a halt, and a few passengers trickled out. Akhil wearily boarded the train and collapsed into a vacant seat in the corner. He took out his modest Nokia phone from his pocket and squinted into the slightly cracked screen. “37<em> dead &amp; 50+ injured. Suicide bombing blast in Shinjuku elementary school…” </em>said the notification headline from the news application. <em>Not now.</em> He slid his finger across the screen, dismissing it, and stuffed the phone back into his pocket.</p><p>Akhil closed his eyes, wrapped his arms around his bag, and leaned deep back into his seat, resting his head on the glass window behind him. It had been a long, hard twelve-hour work-day — not that today was any different from the others. He had always known that the life of a migrant in Japan was not going to be easy, but well, at least he was able to send over some money back home in Mumbai, and that was what mattered at the moment. Ever since his father had passed away, and his mother had suffered a stroke, times had been hard. The money Akhil sent over at the end of every month was the family’s only source of income. He was determined to fight it out so that his family could have a decent, comfortable life. Most importantly, he wanted to give it his all for his fifteen-year-old sister’s dream to study more and eventually become an engineer. He didn’t want her to end up being married off at a young age, just because the family couldn’t financially support her goals.<em> </em>He clutched his bag tighter and gritted his teeth.<em> I can do this — only a few more years to go</em>.</p><p><em>“The next station is Nishi-Kasai. Nishi-Kasai. Doors will open on the left.”</em></p><p>Nishi-Kasai — also known as Tokyo’s Little India — had been where Akhil had lived for almost three years now, but not once had he felt home. He started towards his building, which was a ten-minute walk away from the station. Akhil didn’t mind the long walk. Generally, in Japan, the further away a building is from a railway station, the cheaper the rent. And less expensive rent meant more money saved, which was good.</p><p>Finally, arriving at the lane leading up to his building, Akhil turned towards the entrance and was about to go up when something lying on the ground caught his eye. He bent down to have a closer look at it — it was a mobile phone and seemed to be in a decent condition. Akhil looked around, but as expected at this time of the night, no one seemed to be there. He could take the phone to the nearest police station, but that was about a seven-minute walk in the other direction, and he was in no mood for that now. Another possible option was to call up the owner and figure things out — if only he could get the owner’s details somehow. The easiest option was to act as if he had never seen it and go about his life.</p><p>Akhil hesitated for a bit, but finally picked up the phone. He pressed the unlock button and waited, but nothing happened. <em>The phone seems to be in the switch-off mode</em>, he thought, <em>which is why the owner probably wouldn’t have been able to trace its whereabouts</em>. He long-pressed the ON button, and the screen lit up. Thankfully, the device wasn’t password locked. Akhil opened the contacts and there! Phil J. Adamson, read the contact card, with an alternate mobile number — that was convenient! He tapped on the dial icon as he climbed up the building to his house. A few rings later, someone answered —</p><p>“Hello, is this Mr. — um — Adamson? I just found a mobile phone that possibly belongs to you.”</p><p>“Thank God!” exclaimed the voice at the other end. “You have no idea how relieved I am right now. Thank you so much!”</p><p>“Not a problem,” replied Akhil as he struggled to hold his bag and fit the key into the keyhole at the same time. “Will you be able to collect it from me sometime? I live in Nishi-Kasai.”</p><p>“Amm… I’m sorry to trouble you so much, but I am guessing you live close to the Tōzai metro line — is there any chance I could pick it up at any station?” He said hopefully, adding, “If possible, that is.”</p><p>“Well, I will be going to work tomorrow,” said Akhil, who finally managed to get the door open and stepped inside his one-room apartment. “I could carry it up to Nakano station at the max. Could you meet me there at around 10:15 AM outside the West exit?</p><p>“Perfect!” he said. “I’ll give you a call on this number tomorrow to coordinate. Good night. Thank you once again!” Click.</p><p>From what Akhil could conclude, Adamson seemed quite pleased. Maybe — just maybe — he would be kind enough to give him some reward, hopefully monetary. <em>Diwali</em> was coming up next month, and his mother could do with some new clothes.</p><p>Finally able to relax, Akhil heaved a sigh of relief, lay down on the <em>futon</em>, and turned on the television. He pressed a random channel number on the remote and turned up the volume. This had become a daily habit now — the channel or show didn’t really matter, the noise of the television was the only way he was able to drown out his thoughts and get some much-needed sleep.</p><p>Akhil was about to lock the door and walk down the stairs when he remembered that he’d left Adamson’s phone inside. He groaned as he opened the door once again and grabbed the phone. He looked at his watch — 9:40 AM, it said. Given the lesser frequency of trains on the weekend, he’d have to rush to make it in time. A quick-paced walk later, he reached the station just as his train pulled in. A seat was out of the question at this time, but luckily, he had sufficient place to stand comfortably. To help pass the time, he plugged in his earphones into the Nokia and started listening to his sister’s Spotify playlist.</p><p>About 25 minutes later, with more than half the journey done, the phone in his shirt pocket started ringing. Akhil quickly took it out and silenced the ringer first, contemplating whether to answer it or not, since talking on the phone on trains or buses is often frowned upon in Japan. He decided to text Adamson instead. The phone started ringing again. <em>It looks like this guy won’t stop calling unless I answer, </em>thought Akhil, shaking his head as he pressed the answer button, and…</p><p><em>Umeda, Osaka, Japan. 10:25 AM Saturday:</em></p><p>Wiping the wine glasses one last time before the bar opened up for the day, the bartender looked up at the television screen. The news was on, and something terrible seemed to have gone down. The on-site reporter spoke in a very urgent tone, and there appeared to be fire and destruction all around him — everything was in a mess. He squinted his eyes, trying to read the bold, red headline below, which said — ‘Train blast shakes Tokyo.’</p><p>Check out this 50-word story (micro-fiction) I wrote recently…</p><p><a href="https://medium.com/the-friday-fix/am-i-racist-71b4113da975">Am I Racist?</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=460264ed09c0" width="1" height="1" alt=""><hr><p><a href="https://curiosityneverkilledthewriter.com/tokyo-dreams-460264ed09c0">Tokyo Dreams</a> was originally published in <a href="https://curiosityneverkilledthewriter.com/">Curiosity Never Killed the Writer</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Create A Synthetic Image Dataset — The “What”, The “Why” and The “How”]]></title>
            <link>https://medium.com/data-science/create-a-synthetic-image-dataset-the-what-the-why-and-the-how-f820e6b6f718?source=rss-31356c339457------2</link>
            <guid isPermaLink="false">https://medium.com/p/f820e6b6f718</guid>
            <category><![CDATA[image-dataset]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[data-creation]]></category>
            <category><![CDATA[synthetic-datasets]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Viraf]]></dc:creator>
            <pubDate>Mon, 11 May 2020 17:50:18 GMT</pubDate>
            <atom:updated>2020-09-07T10:38:27.325Z</atom:updated>
            <content:encoded><![CDATA[<h3>Create A Synthetic Image Dataset — The “What”, The “Why” and The “How”</h3><h4>Running short on images to train your model? Here’s how to increase your dataset size multi-fold with synthetic images using Image Composition techniques.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8whwkDAHXUVV9ueRScBYkA.jpeg" /><figcaption>Send your dog to the moon! An example of Image Composition techniques (Original photos by <a href="https://www.pexels.com/@wildlittlethingsphoto?utm_content=attributionCopyText&amp;utm_medium=referral&amp;utm_source=pexels">Helena Lopes</a> and <a href="https://www.pexels.com/@pixabay?utm_content=attributionCopyText&amp;utm_medium=referral&amp;utm_source=pexels">Pixabay</a> from <a href="https://www.pexels.com/photo/astronaut-standing-beside-american-flag-on-the-moon-39896/?utm_content=attributionCopyText&amp;utm_medium=referral&amp;utm_source=pexels">Pexels</a>)</figcaption></figure><p>It is quite a known fact that the quality and quantity of dataset used to train your model will have a direct effect on the model’s performance. Availability of an extensive, versatile dataset could seal the deal, and you could jump to the next step of your Machine Learning / Deep Learning pipeline. But often you might end up in a situation where the dataset you need is just not readily available. Or the data you have is not enough to train a heavy duty model. Now what do you do?</p><blockquote>There are so many resources out there that can help you create a new custom dataset of any object class you need. But if I had to describe the process of building a dataset in one word, I’d call it “<strong>cumbersome</strong>” and “time-consuming” and “frustrating” and — I know I used more than one word, but you get the point!</blockquote><p>Especially when it comes to drawing segmentation masks, I just can’t be bothered to annotate any more than a few images! Don’t get me wrong, I know dataset creation is a very very very important step, but isn’t there a way out?</p><p><em>Data augmentation</em> is a great way to increase the variety of the dataset that your model sees, but again, there is a limit to how much it can help. How about we take it a step further?</p><h4>What is a Synthetic Dataset?</h4><p>Creating any kind of data (image, audio, text, etc.) ‘artificially’ or ‘programatically’ results in what we call a synthetic dataset. This data is not collected naturally or normally and labelled manually, so is it safe to use it to train your model? Will it give good results?</p><p>While there are many <a href="http://news.mit.edu/2017/artificial-data-give-same-results-as-real-data-0303">papers</a> claiming that carefully created synthetic data can give a performance at par with natural data, I recommend having a healthy mixture of the two. A well designed synthetic dataset can take the concept of data augmentations to the next level, and gives the model an even larger variety of training data. The more the better right?</p><p>There are several ways out there to create synthetic image datasets, and in this tutorial, let’s look at one of the most basic method — Image Composition. We will generate new images by composing the object image as a foreground on multiple background images.</p><p>In this tutorial, I am going to create synthetic images for the output class “dog”. In reality, there are many existing datasets online that include the dog class (for example, <a href="https://towardsdatascience.com/master-the-coco-dataset-for-semantic-image-segmentation-part-1-of-2-732712631047">COCO</a>), but what’s the harm in creating more pictures of these cute gifts of nature. :P</p><h4>1. Imports</h4><p>Have a look at the import statements below. Install any module you might not already have.</p><pre>import os<br>import numpy as np<br>import skimage.io as io<br>import skimage.transform as transform<br>import matplotlib.pyplot as plt<br>from matplotlib.patches import Rectangle<br>%matplotlib inline</pre><h4>2. The object — foreground</h4><p>There are 2 ways to go about this —</p><p><strong>(A) Create your own dataset</strong></p><p>First, let’s take an image of a dog available on the internet. There are several websites out there which allow you to use the images for free like — <a href="https://www.pexels.com/">pexels</a>, <a href="http://flickr.com/">flickr</a>, <a href="http://unsplash.com/">unsplash </a>— take your pick. I’m going to use the one below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*b9pny7Mp3aTkNKrqI9X62Q.jpeg" /><figcaption>Photo by <a href="https://www.pexels.com/@bertellifotografia?utm_content=attributionCopyText&amp;utm_medium=referral&amp;utm_source=pexels">Matheus Bertelli</a> from <a href="https://www.pexels.com/photo/brown-dog-running-on-grassy-field-1906153/?utm_content=attributionCopyText&amp;utm_medium=referral&amp;utm_source=pexels">Pexels</a></figcaption></figure><p>Next, you will have to put in a little manual effort to separate the dog from the background. Yeah, I’m sorry — there are no free meals. But eventually this is definitely lesser effort than creating an entire dataset, I promise.</p><p>A few tools that I know of to get this job done are <a href="https://www.adobe.com/in/products/photoshop.html?promoid=PC1PQQ5T&amp;mv=other">Photoshop </a>(paid) or <a href="https://www.gimp.org/">GIMP </a>(free). Or you could even use the ever-dependable Microsoft Paint! The final output looks like the one below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*BudWTv3NIgIMhB2pKO0cwQ.jpeg" /><figcaption>The segmented image of the dog</figcaption></figure><p>Read the image. Next, cut out the foreground layer from this image and set the remaining pixels to zero. An alternative to coding this step is to add a black background with the tool you used (GIMP/Paint/Photoshop) while segmenting the image itself. But nonetheless, here’s how to extract the foreground object in python — simple thresholding of pixels.</p><pre># Read the image<br>I = io.imread(&#39;./dogSeg.jpg&#39;)/255.0</pre><pre># Cut out the foreground layer<br>foreground = I.copy()<br>foreground[foreground&gt;=0.9]=0 # Setting surrounding pixels to zero</pre><pre>plt.axis(&#39;off&#39;)<br>plt.imshow(foreground)<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/345/1*x6HHturKJ0hxorHaGBHjFA.png" /><figcaption>Surrounding pixels set to zero, foreground layer remains</figcaption></figure><p><strong>(B) Use an existing dataset</strong></p><p>If you don’t want to start from scratch, another option is to make use of the images from an already available dataset.</p><p>I am going to use the <a href="http://cocodataset.org/">COCO</a> dataset here to demonstrate. Since COCO already has the object category “dog”, it will be easy to retrieve the image and its mask. The COCO dataset has 4385 (train) and 177 (val) images for the “dog” category.</p><p>You can refer to the entire code for this process in my <a href="https://github.com/virafpatrawala/Synthetic-Image-Datasets">GitHub repository</a> for this tutorial. I will not be going over the code for this step in this post.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/286/1*9683lbgdcaidc2TGKiLdaQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/286/1*Jyvp7VbIZeqNt27hmqBP5w.png" /><figcaption>The COCO image (left) with it’s annotations marked (right)</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/286/1*DgK8ZCSG4_HB6pxgZDaoGg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/286/1*f71suP3M0174j5mPJoy_oA.png" /><figcaption>The segmentation mask (left) and the cut out foreground layer (right)</figcaption></figure><p><em>If you are interested in manipulating the COCO image dataset and creating a data generator to train your model for Image Segmentation, I have an extensive tutorial </em><a href="https://towardsdatascience.com/master-the-coco-dataset-for-semantic-image-segmentation-part-1-of-2-732712631047"><em>here</em></a><em>.</em></p><h4><strong>3. Augment the foreground</strong></h4><p>Let’s play a bit with the dog (pun intended). I have used random rotations, random zoom, random translations and random horizontal flips in my code, but you can use something else too.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/43a922514a0175b04d8a9d1635ebb60b/href">https://medium.com/media/43a922514a0175b04d8a9d1635ebb60b/href</a></iframe><p>The output of the code is shown below. See how different it looks from the original image!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/345/1*A4Xf8zG1DrfhlHg9_Pn34g.png" /><figcaption>Augmented foreground image</figcaption></figure><p>After applying all these changes to the foreground, let’s extract the segmentation mask from the image.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c6b2d262b70b777ec6a280906eb4c21a/href">https://medium.com/media/c6b2d262b70b777ec6a280906eb4c21a/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/345/1*r3guH3h1l_jVniL3-M0kPw.png" /><figcaption>Segmentation mask of the augmented foreground</figcaption></figure><h4>4. The background</h4><p>Now you need a new background image. Use the free websites I mentioned earlier to get a wide variety of backgrounds. You can choose any image in the world (see how I sent my dog to the moon in the display picture of this article), but I recommend keeping it as realistic as possible.</p><p>I am going to use these 4 images as my backgrounds for this tutorial.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*mpMLx15hPqAHDU1bkuvJyQ.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*TiQ1FB0vptOquWzpC7KF3w.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*mST_Z7tuAw3CMuNY_zs3DA.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*3nSep1xlNcrnaEARSP2iwA.jpeg" /><figcaption>Photos by <a href="https://www.pexels.com/@creative-vix?utm_content=attributionCopyText&amp;utm_medium=referral&amp;utm_source=pexels">Creative Vix</a>, <a href="https://www.pexels.com/@pixabay?utm_content=attributionCopyText&amp;utm_medium=referral&amp;utm_source=pexels">Pixabay</a>, and <a href="https://www.pexels.com/@mastercowley?utm_content=attributionCopyText&amp;utm_medium=referral&amp;utm_source=pexels">Nathan Cowley</a> from <a href="https://www.pexels.com/photo/garden-7283/?utm_content=attributionCopyText&amp;utm_medium=referral&amp;utm_source=pexels">Pexels</a></figcaption></figure><p>Let’s select a background at random.</p><pre># Random selection of background from the backgrounds folder</pre><pre>background_fileName = np.random.choice(os.listdir(&quot;./backgrounds/&quot;))<br>background = io.imread(&#39;./backgrounds/&#39;+background_fileName)/255.0</pre><h4>5. Finally: Compose the foreground and background</h4><p>It’s time to add your dog image to the background!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/52def9a84eb7897f5f2741dcef4df976/href">https://medium.com/media/52def9a84eb7897f5f2741dcef4df976/href</a></iframe><p>This function gives the following output. Now, I know it’s not perfect, but it’s quite good!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/345/1*GrGuLacfgeztDPo_3jBzGA.png" /><figcaption>The final composed image</figcaption></figure><p>For object localization tasks, you can also get the box coordinates easily from the new segmentation mask we developed.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a314483551fa2fb9398f35984c51220f/href">https://medium.com/media/a314483551fa2fb9398f35984c51220f/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/345/1*sg3_y4S7iybcD70F49DWLg.png" /><figcaption>Object box marked on the image</figcaption></figure><p>And on performing these functions in a loop with all the backgrounds, we get a nice set of images!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vRh7tzLSWC4LIEQG8bTS1Q.png" /><figcaption>Synthetic images of a dog on multiple backgrounds</figcaption></figure><p>In this tutorial, I used 1 foreground with 4 backgrounds. If you use multiple foregrounds in random with multiple backgrounds, you can create many, many images! Maybe you could merge two or more foregrounds on a background as well. <em>Get creative — play with sizes, angles, foregrounds, and backgrounds to build an awesome synthetic dataset according to your needs. There are a lot of design considerations here as well, but with a little effort you can do much more and create more accurate datasets.</em></p><blockquote><strong>Finally, you can find the entire code for this tutorial in my </strong><a href="https://github.com/virafpatrawala/Synthetic-Image-Datasets"><strong>GitHub repository</strong></a><strong>.</strong></blockquote><p>As a next step on this topic, check out this nice <a href="https://blog.stratospark.com/generating-synthetic-data-image-segmentation-unity-pytorch-fastai.html">tutorial</a> on creating synthetic image datasets with Unity 3D.</p><p><em>Well, that’s it for now — thank you for reading this far into the article. I hope something good came out of it. Any thoughts, questions, comments? Let me know in the responses below!</em></p><p><em>If you are interested in image datasets for semantic segmentation, check out my post below:</em></p><p><a href="https://towardsdatascience.com/master-the-coco-dataset-for-semantic-image-segmentation-part-1-of-2-732712631047">Master the COCO Dataset for Semantic Image Segmentation</a></p><p><em>Or do you want to be rich overnight using ML in stocks? This article is (NOT) for you!</em></p><p><a href="https://towardsdatascience.com/how-not-to-predict-stock-prices-with-lstms-a51f564ccbca">How (NOT) To Predict Stock Prices With LSTMs</a></p><p><em>Can an ML model literally read the stock price charts?</em></p><p><a href="https://towardsdatascience.com/can-an-ml-model-read-stock-charts-and-predict-prices-fb73c551c7a4">Can a Machine Learning Model Read Stock Charts and Predict Prices?</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f820e6b6f718" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science/create-a-synthetic-image-dataset-the-what-the-why-and-the-how-f820e6b6f718">Create A Synthetic Image Dataset — The “What”, The “Why” and The “How”</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[Master the COCO Dataset for Semantic Image Segmentation]]></title>
            <link>https://medium.com/data-science/master-the-coco-dataset-for-semantic-image-segmentation-part-2-of-2-c0d1f593096a?source=rss-31356c339457------2</link>
            <guid isPermaLink="false">https://medium.com/p/c0d1f593096a</guid>
            <category><![CDATA[coco-dataset]]></category>
            <category><![CDATA[image-segmentation]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[tensorflow]]></category>
            <category><![CDATA[semantic-segmentation]]></category>
            <dc:creator><![CDATA[Viraf]]></dc:creator>
            <pubDate>Wed, 06 May 2020 02:54:35 GMT</pubDate>
            <atom:updated>2020-09-07T10:40:06.374Z</atom:updated>
            <content:encoded><![CDATA[<h3>Master the COCO Dataset for Semantic Image Segmentation — Part 2 of 2</h3><h4>In the final part of this 2 part walk-through, we will create a Data Generator with Image Augmentations using the COCO (Common Objects in Context) image dataset for Semantic Image Segmentation in Python with libraries including PyCoco, and Tensorflow Keras.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hki1s4QtFbvYmQV_TrvWAQ.jpeg" /><figcaption><strong>Have you gone COCOnuts?!</strong> (Original photo by <a href="https://www.pexels.com/@thatguycraig000?utm_content=attributionCopyText&amp;utm_medium=referral&amp;utm_source=pexels">Craig Adderley</a> from <a href="https://www.pexels.com/photo/brown-coconut-on-sand-1917838/?utm_content=attributionCopyText&amp;utm_medium=referral&amp;utm_source=pexels">Pexels</a>)</figcaption></figure><p><a href="https://towardsdatascience.com/master-the-coco-dataset-for-semantic-image-segmentation-part-1-of-2-732712631047">Master the COCO Dataset for Semantic Image Segmentation</a></p><p><em>For new readers, you can find Part 1 of this series </em><a href="https://medium.com/@virafpatrawala/master-the-coco-dataset-for-semantic-image-segmentation-part-1-of-2-732712631047"><em>here</em></a><em>. I strongly recommend going through it to better understand the following article.</em></p><p><a href="http://cocodataset.org/#home">COCO (Common Objects in Context)</a>, being one of the most popular image datasets out there, with applications like object detection, segmentation, and captioning — it is quite surprising how few comprehensive but simple, end-to-end tutorials exist. When I first started out with this dataset, I was quite lost and intimidated. I had to plough my way through so many scattered, inadequate resources on the web, multiple vague tutorials, and some experimentation to finally see light at the end of this tunnel. When I was done, I knew I had to document this journey, from start to finish. And so I did. With the hope that someday, someone out there would find these of value and not have to go through all the trouble I faced.</p><p>Here’s presenting you a two part series comprising of a start-to-finish tutorial to aid you in exploring, using, and mastering the COCO Image dataset for Image Segmentation. I’ll try to keep it as simple as possible, provide explanations for every step, and use only free, easy libraries. <em>My job here is to get you acquainted and comfortable with this topic to a level where </em><strong><em>you can take center stage and manipulate it to your needs!</em></strong></p><p>In <a href="https://medium.com/@virafpatrawala/master-the-coco-dataset-for-semantic-image-segmentation-part-1-of-2-732712631047">Part 1</a>, we explored the COCO dataset for Image Segmentation with a python library called <a href="https://pypi.org/project/pycoco/#description">pycoco</a>.</p><p>In Part 2, we will use the T<a href="https://www.tensorflow.org/guide/keras">ensorflow Keras</a> library to ease training models on this dataset and add image augmentations as well. Let’s get going!</p><p>You can find the entire code for this tutorial in my <a href="https://github.com/virafpatrawala/COCO-Semantic-Segmentation">GitHub repository</a>. However, continue reading this post for a much more detailed explanation.</p><h3>1. Create a Data Generator</h3><blockquote>Now, we will combine all the concepts discussed in <a href="https://medium.com/@virafpatrawala/master-the-coco-dataset-for-semantic-image-segmentation-part-1-of-2-732712631047">part 1</a> to create a data generator which you can use to train any segmentation model. The functions below are independent functions and do not require any of the variables from part 1. The folder structure of the project is as in part 1. To understand the concepts behind the functioning of these functions more clearly, I recommend a quick read through <a href="https://medium.com/@virafpatrawala/master-the-coco-dataset-for-semantic-image-segmentation-part-1-of-2-732712631047">part 1</a>.</blockquote><p>Imports mentioned below are the same as before in part 1.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/87f14b89a51d70d86f40d4260f61618d/href">https://medium.com/media/87f14b89a51d70d86f40d4260f61618d/href</a></iframe><h4>(a) Image Filtering</h4><p>The function takes 3 inputs — <br>(a) <strong>folder</strong>: the folder containing the COCO dataset<br>(b) <strong>classes</strong>: None OR a list object containing desired output classes <br>(c) <strong>mode</strong>: ‘train’ OR ‘val’</p><p>This function first loads and initiates the pycoco object [lines 3–4]. Then all possible combinations of “classes” are taken [lines 6–13]. If no filter classes are given, it loads the entire dataset [lines 15–18]. Since some images may contain two or more of the output classes, there might be repeat images in our <em>images</em> variable. So [lines 19–23] we iterate through it and filter out the unique images. Finally, we shuffle the unique image list and return it along with some other useful data.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3965a725b57fdd7b741abb94c4e057d8/href">https://medium.com/media/3965a725b57fdd7b741abb94c4e057d8/href</a></iframe><p>Now that our function is defined, let’s call and initialize it.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9aeea9278ba4994ace196b05f92ce449/href">https://medium.com/media/9aeea9278ba4994ace196b05f92ce449/href</a></iframe><p><strong><em>[Note</em></strong><em>: For this tutorial, I will be using </em><strong><em>mode = ‘val’</em></strong><em>, however when using it for training a model, call these functions separately for both ‘val’ and ‘train’ to create 2 generator objects.</em><strong><em>]</em></strong></p><p>I have mentioned 3 desired output classes (laptop, tv, cell phone). The function filters the COCO dataset to return images containing one or more of only these output classes.</p><p>The function returns — <br>(a) <strong>images</strong>: a list containing all the filtered image objects (unique)<br>(b) <strong>dataset_size</strong>: The size of the generated filtered dataset <br>(c) <strong>coco</strong>: The initialized coco object</p><p>You can print out each of these outputs to understand them better.</p><h4>(b) Generate the images and masks</h4><p>We have our filtered dataset ready, let’s make a generator object to yield image and masks in batches.</p><p><strong><em>[Note:</em></strong><em> I have not included the code for the sub-functions here, so as to make it a little easier to view. You can view my </em><a href="https://github.com/virafpatrawala/COCO-Semantic-Segmentation"><em>GitHub repository</em></a><em> for the entire code.</em><strong><em>]</em></strong></p><p>The function <em>dataGeneratorCoco </em>takes 8 inputs — <br>(a) <strong>images</strong>: the image list returned by the <em>filterDataset </em>function <br>(b) <strong>classes</strong>: the filter classes list same as input to the <em>filterDataset </em>function<br>(c) <strong>coco</strong>: the coco object returned by the <em>filterDataset </em>function <br>(d) <strong>folder</strong>: the folder containing the dataset <br>(e) <strong>input_image_size</strong>: size of input image to your model as (width, height) <br>(f) <strong>batch_size</strong>: batch size <br>(g) <strong>mode</strong>: ‘train’ or ‘val’ same as input to the <em>filterDataset </em>function<br>(h) <strong>mask_type</strong>: ‘binary’ or ‘normal’(classes can’t be None)</p><p>The function performs batch-sized loops [lines 20~] and retrieves the image from the folder, loads the mask with the filtered outputs (binary or normal segmentation masks) and finally lumps them to form an image batch and mask batch. The yield statement [line 41] is responsible for the creation of a generator-type object.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f9e0d2c2562822cb529b72829d0aff02/href">https://medium.com/media/f9e0d2c2562822cb529b72829d0aff02/href</a></iframe><p>Now that our function is defined, let’s call and initialize it.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a87358698480baa72c3e265ee1e8425c/href">https://medium.com/media/a87358698480baa72c3e265ee1e8425c/href</a></iframe><p>The function creates a data generator object yielding batches of images and binary masks. Since our generator is finally ready, let’s define a function to visualize it.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a376d1231463bfad7c4ada2c0fbb835e/href">https://medium.com/media/a376d1231463bfad7c4ada2c0fbb835e/href</a></iframe><pre>visualizeGenerator(val_gen)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lmHKDnlqSML3mrNo6WVKHg.png" /><figcaption>Visualization of the images generated by the generator for mask_type=’binary’.</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FX-sdv4ZJiYbty_xThD8qw.png" /><figcaption>Visualization of the images generated by the generator for mask_type=’normal’.</figcaption></figure><h3>2. Add Augmentations</h3><p>The COCO data generator is already ready if you have followed the previous steps! You can use it for training your model as it is, but if you wish to add data augmentations to your images to increase potential dataset size, follow this final step!</p><p>Here we will add image augmentations using Image Generator from Tensorflow Keras.</p><p>The <em>augmentationsGenerator </em>function takes 3 inputs- <br>(a) <strong>gen</strong>: the generator object to which you wish to add augmentations <br>(b) <strong>augGeneratorArgs</strong>: the augmentation arguments (see the code example ahead) <br>(c) <strong>seed</strong>: seed to generate the random augmentation parameters or None</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c846cfbb2ed064317402d20f88ac0bdf/href">https://medium.com/media/c846cfbb2ed064317402d20f88ac0bdf/href</a></iframe><p>To define the arguments for augmentation, follow the following format. You can experiment with the values for your use case. For further details on the arguments, you can check out the <a href="https://keras.io/preprocessing/image/#imagedatagenerator-class">keras documentation</a> on this topic.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3d6266e6b82d9f173e192c12b84a64e4/href">https://medium.com/media/3d6266e6b82d9f173e192c12b84a64e4/href</a></iframe><p>The output of the function is a data generator object yielding batches of augmented images and correspondingly augmented binary masks. Visualizing the augmented data generator, note how you can now see rotations, brightness variations, reflections, etc.</p><pre>visualizeGenerator(aug_gen)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kxJ9729dpVC-4RNuWjrD7A.png" /><figcaption>Visualization of the images generated by the augmentations generator for mask_type=’binary’.</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lrjlS18mS2qALnfI-d_Uow.png" /><figcaption>Visualization of the images generated by the augmentations generator for mask_type=’normal’.</figcaption></figure><h3>3. Finally… Train A Model</h3><p>Now that the generator is ready, what can we do with it? This following code is a quick template on how to make use of these COCO dataset generators in your training.</p><p><strong>TIP</strong>: <em>You can create a separate .py file (say cocoFunctions.py) and copy the code for the imports, filterDataset function, and dataGeneratorCoco + supporting functions. This will help make the code more systematic.</em></p><p><em>You can then simply import the functions into any code by using: <br>from cocoFunctions import filterDataset, dataGeneratorCoco</em></p><pre>-&gt; Create filtered train dataset (using filterDataset()) <br>-&gt; Create filtered val dataset (using filterDataset())</pre><pre>-&gt; Create train generator (using dataGeneratorCoco()) <br>-&gt; Create train generator (using dataGeneratorCoco())</pre><pre># Set your parameters<br>n_epochs = &lt;number of epochs of training&gt;</pre><pre>steps_per_epoch = dataset_size_train // batch_size<br>validation_steps = dataset_size_val // batch_size</pre><pre>m = &lt;your model&gt;<br>opt = &lt;your optimizer&gt;<br>lossFn = &lt;your loss function&gt;</pre><pre># Compile your model first<br>m.compile(loss = lossFn, optimizer = opt, metrics=[&#39;accuracy&#39;])</pre><pre># Start the training process<br>history = m.fit(x = train_gen_aug,<br>                validation_data = val_gen_aug,<br>                steps_per_epoch = steps_per_epoch,<br>                validation_steps = validation_steps,<br>                epochs = n_epochs,<br>                verbose = True)</pre><p>This was one way to go about it. <strong>What do <em>you </em>plan to do with this newfound mastery?</strong></p><p>Once again, the entire code for this tutorial can be found in my <a href="https://github.com/virafpatrawala/COCO-Semantic-Segmentation">GitHub Repository</a>.</p><p>And there, we’re done! I hope you have attained some kind of mastery and confidence over the COCO dataset by now. <strong><em>But don’t stop here — get out there, experiment the hell out of this, and rock the world of image segmentation with your new ideas!</em></strong></p><p>My next post teaches you how to easily multiply your image dataset. Do give it a read!</p><p><a href="https://towardsdatascience.com/create-a-synthetic-image-dataset-the-what-the-why-and-the-how-f820e6b6f718">Create A Synthetic Image Dataset — The “What”, The “Why” and The “How”</a></p><p>Or want to be rich overnight using ML in stocks? This article is (NOT) for you!</p><p><a href="https://towardsdatascience.com/how-not-to-predict-stock-prices-with-lstms-a51f564ccbca">How (NOT) To Predict Stock Prices With LSTMs</a></p><p>Can an ML model literally read the stock price charts?</p><p><a href="https://towardsdatascience.com/can-an-ml-model-read-stock-charts-and-predict-prices-fb73c551c7a4">Can a Machine Learning Model Read Stock Charts and Predict Prices?</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c0d1f593096a" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science/master-the-coco-dataset-for-semantic-image-segmentation-part-2-of-2-c0d1f593096a">Master the COCO Dataset for Semantic Image Segmentation</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[Master the COCO Dataset for Semantic Image Segmentation]]></title>
            <link>https://medium.com/data-science/master-the-coco-dataset-for-semantic-image-segmentation-part-1-of-2-732712631047?source=rss-31356c339457------2</link>
            <guid isPermaLink="false">https://medium.com/p/732712631047</guid>
            <category><![CDATA[semantic-segmentation]]></category>
            <category><![CDATA[coco-dataset]]></category>
            <category><![CDATA[tensorflow]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Viraf]]></dc:creator>
            <pubDate>Sun, 03 May 2020 14:34:44 GMT</pubDate>
            <atom:updated>2020-09-07T10:40:03.366Z</atom:updated>
            <content:encoded><![CDATA[<h3>Master the COCO Dataset for Semantic Image Segmentation — Part 1 of 2</h3><h4>In this 2 part walk-through, we will explore and manipulate the COCO (Common Objects in Context) image dataset for Semantic Image Segmentation in Python with libraries including PyCoco, and Tensorflow Keras.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*taqoyoo-5DlTvktWYM21tQ.jpeg" /><figcaption>Did you mean COCOA? (Original photo by <a href="https://www.pexels.com/@nietjuh?utm_content=attributionCopyText&amp;utm_medium=referral&amp;utm_source=pexels">Ylanite Koppens</a> from <a href="https://www.pexels.com/photo/white-ceramic-mug-1693652/?utm_content=attributionCopyText&amp;utm_medium=referral&amp;utm_source=pexels">Pexels</a>)</figcaption></figure><p><a href="http://cocodataset.org/#home">COCO (Common Objects in Context)</a>, being one of the most popular image datasets out there, with applications like object detection, segmentation, and captioning - it is quite surprising how few comprehensive but simple, end-to-end tutorials exist. When I first started out with this dataset, I was quite lost and intimidated. I had to plough my way through so many scattered, inadequate resources on the web, multiple vague tutorials, and some experimentation to finally see light at the end of this tunnel. When I was done, I knew I had to document this journey, from start to finish. And so I did. With the hope that someday, someone out there would find these of value and not have to go through all the trouble I faced.</p><p>Here’s presenting you a two part series comprising of a start-to-finish tutorial to aid you in exploring, using, and mastering the COCO Image dataset for Image Segmentation. I’ll try to keep it as simple as possible, provide explanations for every step, and use only free, easy libraries. <em>My job here is to get you acquainted and comfortable with this topic to a level where </em><strong><em>you can take center stage and manipulate it to your needs!</em></strong></p><p>In Part 1, we will first explore and manipulate the COCO dataset for Image Segmentation with a python library called <a href="https://pypi.org/project/pycoco/#description">pycoco</a>. This library eases the handling of the COCO dataset, which otherwise would have been very difficult to code yourself.</p><p>In <a href="https://towardsdatascience.com/master-the-coco-dataset-for-semantic-image-segmentation-part-2-of-2-c0d1f593096a">Part 2</a>, we will use the T<a href="https://www.tensorflow.org/guide/keras">ensorflow Keras</a> library to ease training models on this dataset and add image augmentations as well.</p><p>You can find the entire code for this tutorial in my <a href="https://github.com/virafpatrawala/COCO-Semantic-Segmentation">GitHub repository</a>. However, continue reading this post for a much more detailed explanation.</p><h3>1. The COCO Dataset</h3><blockquote>“COCO is a large-scale object detection, segmentation, and captioning dataset.”</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*vP5WkxR8oY2Iz0Ox0tXW1w.jpeg" /><figcaption>The <a href="http://cocodataset.org/#home">COCO Dataset</a></figcaption></figure><p>Common Objects in Context (COCO) literally implies that the images in the dataset are everyday objects captured from everyday scenes. This adds some “context” to the objects captured in the scenes. <a href="http://cocodataset.org/#explore">Explore this dataset here</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*ShJDpj-tEmuxJ0BdvsvIOw.png" /><figcaption>An example image from the dataset.</figcaption></figure><p>COCO provides multi-object labeling, segmentation mask annotations, image captioning, key-point detection and panoptic segmentation annotations with a total of 81 categories, making it a very versatile and multi-purpose dataset.</p><p>In this walk-through, we shall be focusing on the Semantic Segmentation applications of the dataset.</p><h3>2. Downloads and Installations</h3><h4>COCO</h4><p>You’ll need to download the COCO dataset on to your device (quite obviously). You can download the 2017 dataset files using the links below. The files are quite large, so be patient as it may take some time. The files you need are:</p><p>(a) <a href="http://images.cocodataset.org/zips/train2017.zip">2017 train images</a></p><p>(b) <a href="http://images.cocodataset.org/zips/val2017.zip">2017 val images</a></p><p>(c) <a href="http://images.cocodataset.org/annotations/stuff_annotations_trainval2017.zip">2017 Stuff Train/Val annotations</a></p><p>Extract the zipped files. From (c) only the <em>instances </em>files for train and val are needed i.e. <em>instances_train2017.json </em>and <em>instances_val2017.json.</em></p><p>Arrange these files as the file-structure given below. Some simple re-arrangement and re-naming of folders and files is required. I have done this just for ease of use and visualization, if you don’t wish to do it, all you need to do is change the code (mainly the file paths) accordingly.</p><pre>Project Folder<br>└───the code notebook (.py / .ipynb)<br>│<br>└───COCOdataset2017   <br>    └───images<br>    │   └───train<br>    │   │    │   000000000009.jpg<br>    │   │    │   000000000025.jpg<br>    │   │    │   ...<br>    │   └───val   <br>    │        │   000000000139.jpg<br>    │        │   000000000285.jpg<br>    │        │   ...<br>    └───annotations<br>        │   instances_train.json<br>        │   instances_val.json</pre><p><em>#Note that there is a way to access images with their URLs (from the annotations file), which would require you to download only (c). However, during a lengthy training process, it’s better if you do not depend on the internet and hence I recommend downloading (a) and (b) as well.</em></p><h4>PyCoco</h4><p>Next, let’s install our major library, <strong>pycoco</strong>. <em>I am particularly stating the procedure for this because you’ll face many errors in the installation and import process if you follow your standard procedure.</em></p><p>In <a href="https://docs.conda.io/en/latest/">Conda</a>, if you follow the procedure below, you should be able to install, import and use this library quite smoothly.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ba4ebc048a34b8894d3e6c4a63e496cc/href">https://medium.com/media/ba4ebc048a34b8894d3e6c4a63e496cc/href</a></iframe><h4>Others</h4><p>Let’s import all the libraries we’ll be using for this tutorial. The installation for the other libraries is quite straightforward, so I won’t be mentioning the details here. Install all the libraries in your python environment.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/87f14b89a51d70d86f40d4260f61618d/href">https://medium.com/media/87f14b89a51d70d86f40d4260f61618d/href</a></iframe><h3>3. Dataset Exploration with PyCoco</h3><p>First, let’s initiate the PyCoco library. This library takes the COCO annotations (.json) file (the ones we downloaded in step 2) as an input. Either train or val instances annotations should work, but for this tutorial, I am using “instances_val.json” since it’s faster to load (reason: val dataset is smaller than train dataset).</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e7eb65462632662ebba1e094fc7b37bd/href">https://medium.com/media/e7eb65462632662ebba1e094fc7b37bd/href</a></iframe><p>The output, i.e. the categories are printed as:</p><pre>[{&#39;supercategory&#39;: &#39;person&#39;, &#39;id&#39;: 1, &#39;name&#39;: &#39;person&#39;},<br> {&#39;supercategory&#39;: &#39;vehicle&#39;, &#39;id&#39;: 2, &#39;name&#39;: &#39;bicycle&#39;},<br> {&#39;supercategory&#39;: &#39;vehicle&#39;, &#39;id&#39;: 3, &#39;name&#39;: &#39;car&#39;},<br> {&#39;supercategory&#39;: &#39;vehicle&#39;, &#39;id&#39;: 4, &#39;name&#39;: &#39;motorcycle&#39;},<br> {&#39;supercategory&#39;: &#39;vehicle&#39;, &#39;id&#39;: 5, &#39;name&#39;: &#39;airplane&#39;},<br> {&#39;supercategory&#39;: &#39;vehicle&#39;, &#39;id&#39;: 6, &#39;name&#39;: &#39;bus&#39;},<br> {&#39;supercategory&#39;: &#39;vehicle&#39;, &#39;id&#39;: 7, &#39;name&#39;: &#39;train&#39;},<br> {&#39;supercategory&#39;: &#39;vehicle&#39;, &#39;id&#39;: 8, &#39;name&#39;: &#39;truck&#39;},<br> {&#39;supercategory&#39;: &#39;vehicle&#39;, &#39;id&#39;: 9, &#39;name&#39;: &#39;boat&#39;},<br> {&#39;supercategory&#39;: &#39;outdoor&#39;, &#39;id&#39;: 10, &#39;name&#39;: &#39;traffic light&#39;},<br> {&#39;supercategory&#39;: &#39;outdoor&#39;, &#39;id&#39;: 11, &#39;name&#39;: &#39;fire hydrant&#39;},<br> {&#39;supercategory&#39;: &#39;outdoor&#39;, &#39;id&#39;: 13, &#39;name&#39;: &#39;stop sign&#39;},<br> {&#39;supercategory&#39;: &#39;outdoor&#39;, &#39;id&#39;: 14, &#39;name&#39;: &#39;parking meter&#39;},<br> {&#39;supercategory&#39;: &#39;outdoor&#39;, &#39;id&#39;: 15, &#39;name&#39;: &#39;bench&#39;},<br> {&#39;supercategory&#39;: &#39;animal&#39;, &#39;id&#39;: 16, &#39;name&#39;: &#39;bird&#39;},<br> {&#39;supercategory&#39;: &#39;animal&#39;, &#39;id&#39;: 17, &#39;name&#39;: &#39;cat&#39;},<br> {&#39;supercategory&#39;: &#39;animal&#39;, &#39;id&#39;: 18, &#39;name&#39;: &#39;dog&#39;},<br> {&#39;supercategory&#39;: &#39;animal&#39;, &#39;id&#39;: 19, &#39;name&#39;: &#39;horse&#39;},<br> {&#39;supercategory&#39;: &#39;animal&#39;, &#39;id&#39;: 20, &#39;name&#39;: &#39;sheep&#39;},<br> {&#39;supercategory&#39;: &#39;animal&#39;, &#39;id&#39;: 21, &#39;name&#39;: &#39;cow&#39;},<br> {&#39;supercategory&#39;: &#39;animal&#39;, &#39;id&#39;: 22, &#39;name&#39;: &#39;elephant&#39;},<br> {&#39;supercategory&#39;: &#39;animal&#39;, &#39;id&#39;: 23, &#39;name&#39;: &#39;bear&#39;},<br> {&#39;supercategory&#39;: &#39;animal&#39;, &#39;id&#39;: 24, &#39;name&#39;: &#39;zebra&#39;},<br> {&#39;supercategory&#39;: &#39;animal&#39;, &#39;id&#39;: 25, &#39;name&#39;: &#39;giraffe&#39;},<br> {&#39;supercategory&#39;: &#39;accessory&#39;, &#39;id&#39;: 27, &#39;name&#39;: &#39;backpack&#39;},<br> {&#39;supercategory&#39;: &#39;accessory&#39;, &#39;id&#39;: 28, &#39;name&#39;: &#39;umbrella&#39;},<br> {&#39;supercategory&#39;: &#39;accessory&#39;, &#39;id&#39;: 31, &#39;name&#39;: &#39;handbag&#39;},<br> {&#39;supercategory&#39;: &#39;accessory&#39;, &#39;id&#39;: 32, &#39;name&#39;: &#39;tie&#39;},<br> {&#39;supercategory&#39;: &#39;accessory&#39;, &#39;id&#39;: 33, &#39;name&#39;: &#39;suitcase&#39;},<br> {&#39;supercategory&#39;: &#39;sports&#39;, &#39;id&#39;: 34, &#39;name&#39;: &#39;frisbee&#39;},<br> {&#39;supercategory&#39;: &#39;sports&#39;, &#39;id&#39;: 35, &#39;name&#39;: &#39;skis&#39;},<br> {&#39;supercategory&#39;: &#39;sports&#39;, &#39;id&#39;: 36, &#39;name&#39;: &#39;snowboard&#39;},<br> {&#39;supercategory&#39;: &#39;sports&#39;, &#39;id&#39;: 37, &#39;name&#39;: &#39;sports ball&#39;},<br> {&#39;supercategory&#39;: &#39;sports&#39;, &#39;id&#39;: 38, &#39;name&#39;: &#39;kite&#39;},<br> {&#39;supercategory&#39;: &#39;sports&#39;, &#39;id&#39;: 39, &#39;name&#39;: &#39;baseball bat&#39;},<br> {&#39;supercategory&#39;: &#39;sports&#39;, &#39;id&#39;: 40, &#39;name&#39;: &#39;baseball glove&#39;},<br> {&#39;supercategory&#39;: &#39;sports&#39;, &#39;id&#39;: 41, &#39;name&#39;: &#39;skateboard&#39;},<br> {&#39;supercategory&#39;: &#39;sports&#39;, &#39;id&#39;: 42, &#39;name&#39;: &#39;surfboard&#39;},<br> {&#39;supercategory&#39;: &#39;sports&#39;, &#39;id&#39;: 43, &#39;name&#39;: &#39;tennis racket&#39;},<br> {&#39;supercategory&#39;: &#39;kitchen&#39;, &#39;id&#39;: 44, &#39;name&#39;: &#39;bottle&#39;},<br> {&#39;supercategory&#39;: &#39;kitchen&#39;, &#39;id&#39;: 46, &#39;name&#39;: &#39;wine glass&#39;},<br> {&#39;supercategory&#39;: &#39;kitchen&#39;, &#39;id&#39;: 47, &#39;name&#39;: &#39;cup&#39;},<br> {&#39;supercategory&#39;: &#39;kitchen&#39;, &#39;id&#39;: 48, &#39;name&#39;: &#39;fork&#39;},<br> {&#39;supercategory&#39;: &#39;kitchen&#39;, &#39;id&#39;: 49, &#39;name&#39;: &#39;knife&#39;},<br> {&#39;supercategory&#39;: &#39;kitchen&#39;, &#39;id&#39;: 50, &#39;name&#39;: &#39;spoon&#39;},<br> {&#39;supercategory&#39;: &#39;kitchen&#39;, &#39;id&#39;: 51, &#39;name&#39;: &#39;bowl&#39;},<br> {&#39;supercategory&#39;: &#39;food&#39;, &#39;id&#39;: 52, &#39;name&#39;: &#39;banana&#39;},<br> {&#39;supercategory&#39;: &#39;food&#39;, &#39;id&#39;: 53, &#39;name&#39;: &#39;apple&#39;},<br> {&#39;supercategory&#39;: &#39;food&#39;, &#39;id&#39;: 54, &#39;name&#39;: &#39;sandwich&#39;},<br> {&#39;supercategory&#39;: &#39;food&#39;, &#39;id&#39;: 55, &#39;name&#39;: &#39;orange&#39;},<br> {&#39;supercategory&#39;: &#39;food&#39;, &#39;id&#39;: 56, &#39;name&#39;: &#39;broccoli&#39;},<br> {&#39;supercategory&#39;: &#39;food&#39;, &#39;id&#39;: 57, &#39;name&#39;: &#39;carrot&#39;},<br> {&#39;supercategory&#39;: &#39;food&#39;, &#39;id&#39;: 58, &#39;name&#39;: &#39;hot dog&#39;},<br> {&#39;supercategory&#39;: &#39;food&#39;, &#39;id&#39;: 59, &#39;name&#39;: &#39;pizza&#39;},<br> {&#39;supercategory&#39;: &#39;food&#39;, &#39;id&#39;: 60, &#39;name&#39;: &#39;donut&#39;},<br> {&#39;supercategory&#39;: &#39;food&#39;, &#39;id&#39;: 61, &#39;name&#39;: &#39;cake&#39;},<br> {&#39;supercategory&#39;: &#39;furniture&#39;, &#39;id&#39;: 62, &#39;name&#39;: &#39;chair&#39;},<br> {&#39;supercategory&#39;: &#39;furniture&#39;, &#39;id&#39;: 63, &#39;name&#39;: &#39;couch&#39;},<br> {&#39;supercategory&#39;: &#39;furniture&#39;, &#39;id&#39;: 64, &#39;name&#39;: &#39;potted plant&#39;},<br> {&#39;supercategory&#39;: &#39;furniture&#39;, &#39;id&#39;: 65, &#39;name&#39;: &#39;bed&#39;},<br> {&#39;supercategory&#39;: &#39;furniture&#39;, &#39;id&#39;: 67, &#39;name&#39;: &#39;dining table&#39;},<br> {&#39;supercategory&#39;: &#39;furniture&#39;, &#39;id&#39;: 70, &#39;name&#39;: &#39;toilet&#39;},<br> {&#39;supercategory&#39;: &#39;electronic&#39;, &#39;id&#39;: 72, &#39;name&#39;: &#39;tv&#39;},<br> {&#39;supercategory&#39;: &#39;electronic&#39;, &#39;id&#39;: 73, &#39;name&#39;: &#39;laptop&#39;},<br> {&#39;supercategory&#39;: &#39;electronic&#39;, &#39;id&#39;: 74, &#39;name&#39;: &#39;mouse&#39;},<br> {&#39;supercategory&#39;: &#39;electronic&#39;, &#39;id&#39;: 75, &#39;name&#39;: &#39;remote&#39;},<br> {&#39;supercategory&#39;: &#39;electronic&#39;, &#39;id&#39;: 76, &#39;name&#39;: &#39;keyboard&#39;},<br> {&#39;supercategory&#39;: &#39;electronic&#39;, &#39;id&#39;: 77, &#39;name&#39;: &#39;cell phone&#39;},<br> {&#39;supercategory&#39;: &#39;appliance&#39;, &#39;id&#39;: 78, &#39;name&#39;: &#39;microwave&#39;},<br> {&#39;supercategory&#39;: &#39;appliance&#39;, &#39;id&#39;: 79, &#39;name&#39;: &#39;oven&#39;},<br> {&#39;supercategory&#39;: &#39;appliance&#39;, &#39;id&#39;: 80, &#39;name&#39;: &#39;toaster&#39;},<br> {&#39;supercategory&#39;: &#39;appliance&#39;, &#39;id&#39;: 81, &#39;name&#39;: &#39;sink&#39;},<br> {&#39;supercategory&#39;: &#39;appliance&#39;, &#39;id&#39;: 82, &#39;name&#39;: &#39;refrigerator&#39;},<br> {&#39;supercategory&#39;: &#39;indoor&#39;, &#39;id&#39;: 84, &#39;name&#39;: &#39;book&#39;},<br> {&#39;supercategory&#39;: &#39;indoor&#39;, &#39;id&#39;: 85, &#39;name&#39;: &#39;clock&#39;},<br> {&#39;supercategory&#39;: &#39;indoor&#39;, &#39;id&#39;: 86, &#39;name&#39;: &#39;vase&#39;},<br> {&#39;supercategory&#39;: &#39;indoor&#39;, &#39;id&#39;: 87, &#39;name&#39;: &#39;scissors&#39;},<br> {&#39;supercategory&#39;: &#39;indoor&#39;, &#39;id&#39;: 88, &#39;name&#39;: &#39;teddy bear&#39;},<br> {&#39;supercategory&#39;: &#39;indoor&#39;, &#39;id&#39;: 89, &#39;name&#39;: &#39;hair drier&#39;},<br> {&#39;supercategory&#39;: &#39;indoor&#39;, &#39;id&#39;: 90, &#39;name&#39;: &#39;toothbrush&#39;}]</pre><p>The COCO dataset has 81 object categories (<em>note that ‘id’:0 is background</em>), as we printed out above (also listed <a href="https://gist.github.com/AruniRC/7b3dadd004da04c80198557db5da4bda">here</a>). However as you can observe, the label ids for these 81 classes range from 0 to 90 (some empty id numbers in between). Here is a convenient function which can fetch a class name for a given id number.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b57575e7b8bbc4a9c5163ab0ed7b6488/href">https://medium.com/media/b57575e7b8bbc4a9c5163ab0ed7b6488/href</a></iframe><p>This will give an output:</p><pre>The class name is cell phone</pre><h4>Class Filtering</h4><p>Let’s say I want images containing only the classes “laptop”, “tv”, and “cell phone” and I don’t require any other object class. To get this subset of the dataset, follow the steps below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ea42fd0546d6aaaaafd50b91b5935a8d/href">https://medium.com/media/ea42fd0546d6aaaaafd50b91b5935a8d/href</a></iframe><p>Now, the <em>imgIDs </em>variable contains all the images which contain all the <em>filterClasses</em>. The output of the print statement is:</p><pre>Number of images containing all the classes: 11</pre><p>This implies, out of the entire validation dataset, there are 11 images which contain <em>ALL</em> the 3 classes which I wanted. And a sample image displayed is:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/304/1*QZd5XQXNiy9EnL1BXzSXXw.png" /><figcaption>A sample image containing the filtered output classes.</figcaption></figure><p>To display the annotations we shall follow the code as below. Note that we use pycoco functionalities <strong>“</strong><a href="https://github.com/cocodataset/cocoapi/blob/master/PythonAPI/pycocotools/coco.py#L200"><strong>loadAnns</strong></a><strong>”</strong> to load the annotations concerning the object in COCO format and <strong>“</strong><a href="https://github.com/cocodataset/cocoapi/blob/master/PythonAPI/pycocotools/coco.py#L233"><strong>showAnns</strong></a><strong>”</strong> to sketch these out on the image. These functions largely ease the drawing of the annotation masks. You can have a look at the linked function definitions to see how they work internally.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4e808488a997e5c36fefe6f2b283aced/href">https://medium.com/media/4e808488a997e5c36fefe6f2b283aced/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/304/1*C0BP5dLinScCUjH3DrNhNA.png" /><figcaption>Annotations for filtered classes neatly drawn out.</figcaption></figure><h4>All Possible Combinations</h4><p>When we filter the dataset with classes, the pycoco tool returns images which consist of only ALL your required classes, not one or two or any other combinations. Thus, this piece of code will ensure that all possible combinations for the given <em>filterClass</em> lists are obtained in the resultant dataset.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/91d975d29df377b777eca29bfa8f330c/href">https://medium.com/media/91d975d29df377b777eca29bfa8f330c/href</a></iframe><p>The output of the print statement is:</p><pre>Number of images containing the filter classes: 503</pre><p>See how above, we had received only 11 images, but now there are 503 images! We have avoided any repetition of images as well.</p><h3>4. Image Segmentation Mask Generation</h3><p>For any Semantic Segmentation training task, you’ll require a folder full of the images (train and val) and the corresponding output ground-truth masks. This section will help create the corresponding image masks.</p><h4>Mask Type 1: Normal Semantic Segmentation Mask</h4><p>Each pixel has a label according to the class it falls into. I am not using the official COCO ids, but instead allotting pixel values as per the order of the class name in the array ‘<em>filterClasses</em>’, i.e.:<br>0: background<br>1: laptop<br>2: tv<br>3: cell phone</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c0635641df5b8e6079292ab72f6de5dd/href">https://medium.com/media/c0635641df5b8e6079292ab72f6de5dd/href</a></iframe><p>The output is a 2-channel semantic segmentation mask with dimensions equal to the original image, as displayed below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/330/1*VIBddw3Pj29rATNbc4rk9g.png" /><figcaption>A normal 2-channel mask for semantic segmentation.</figcaption></figure><h4>Mask Type 2: Binary Semantic Segmentation Mask</h4><p>In general, your output mask will have <em>N</em> possible pixel values for N output classes. However, binary masking implies that the output mask will have <strong>only 2 pixel values</strong>, i.e., <strong>1 </strong>(object: could be any of the N classes) and <strong>0 </strong>(the background).</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fe61d3f91e246b25d2f3296d0044696f/href">https://medium.com/media/fe61d3f91e246b25d2f3296d0044696f/href</a></iframe><p>The output is a 2-channel binary semantic segmentation mask with dimensions equal to the original image, as displayed below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/330/1*DlLjP-00rEHjVMNhvE1wPQ.png" /><figcaption>A binary semantic segmentation mask. Yellow represents pixel value 1, violet represents pixel value 0.</figcaption></figure><p><strong>Note</strong>: the format of how your desired masks can be different from the ones mentioned above. For example, you might want to keep the label id numbers the same as in the original COCO dataset (0–90). Or you might want an output format for an <em>instance segmentation </em>use case. Another example is, you might want your masks to be one-hot-encoded, i.e., number of channels = number of output object classes, and each channel having only 0s (background) and 1s (that object).</p><p>For any case, modify the above code accordingly and you can get the mask as you desire. If I get some time in the future, I’ll try to add the codes for these additional types as well.</p><p><strong><em>Next, let’s move on to </em></strong><a href="https://towardsdatascience.com/master-the-coco-dataset-for-semantic-image-segmentation-part-2-of-2-c0d1f593096a"><strong><em>Part 2</em></strong></a><strong><em>.</em></strong></p><p><a href="https://towardsdatascience.com/master-the-coco-dataset-for-semantic-image-segmentation-part-2-of-2-c0d1f593096a">Master the COCO Dataset for Semantic Image Segmentation</a></p><p>Again, the code for this tutorial in my <a href="https://github.com/virafpatrawala/COCO-Semantic-Segmentation">GitHub repository</a>.</p><p>And my friends, that’s it for the day! If you have come so far, I hope you have attained some kind of confidence with the COCO dataset. <strong><em>But don’t stop here — get out there, experiment the hell out of this, and rock the world of image segmentation with your new ideas!</em></strong></p><p>If you liked this article, this next one shows you how to easily multiply your image dataset with minimal effort. Do give it a read!</p><p><a href="https://towardsdatascience.com/create-a-synthetic-image-dataset-the-what-the-why-and-the-how-f820e6b6f718">Create A Synthetic Image Dataset — The “What”, The “Why” and The “How”</a></p><p>Or want to be rich overnight using ML in stocks? This article is (NOT) for you!</p><p><a href="https://towardsdatascience.com/how-not-to-predict-stock-prices-with-lstms-a51f564ccbca">How (NOT) To Predict Stock Prices With LSTMs</a></p><p>Can an ML model literally read the stock price charts?</p><p><a href="https://towardsdatascience.com/can-an-ml-model-read-stock-charts-and-predict-prices-fb73c551c7a4">Can a Machine Learning Model Read Stock Charts and Predict Prices?</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=732712631047" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science/master-the-coco-dataset-for-semantic-image-segmentation-part-1-of-2-732712631047">Master the COCO Dataset for Semantic Image Segmentation</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>