<?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 Paritosh Malhotra on Medium]]></title>
        <description><![CDATA[Stories by Paritosh Malhotra on Medium]]></description>
        <link>https://medium.com/@paritoshmalhotra?source=rss-c135b4c07c67------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*14q9Ej0O6A799Qu9rf9TYA.jpeg</url>
            <title>Stories by Paritosh Malhotra on Medium</title>
            <link>https://medium.com/@paritoshmalhotra?source=rss-c135b4c07c67------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 27 Nov 2026 18:21:25 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@paritoshmalhotra/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[Using Keras ImageDataGenerator with Transfer Learning]]></title>
            <link>https://becominghuman.ai/using-keras-imagedatagenerator-with-transfer-learning-c4787d430060?source=rss-c135b4c07c67------2</link>
            <guid isPermaLink="false">https://medium.com/p/c4787d430060</guid>
            <category><![CDATA[keras]]></category>
            <category><![CDATA[tensorflow]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[image-classification]]></category>
            <category><![CDATA[deep-learning]]></category>
            <dc:creator><![CDATA[Paritosh Malhotra]]></dc:creator>
            <pubDate>Mon, 07 Sep 2020 04:47:34 GMT</pubDate>
            <atom:updated>2020-09-25T15:57:34.380Z</atom:updated>
            <content:encoded><![CDATA[<h3>Using Keras Image Data Generator with Transfer Learning</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*7MMu2PqRJMr4-tGA" /></figure><h3>Introduction</h3><p><a href="https://becominghuman.ai/keras-cheat-sheet-neural-networks-in-python-738c0d170c2e">Keras</a> is a high-level Python API to build <a href="https://becominghuman.ai/">Neural networks</a>, which has made life easier for people who want to start building <a href="https://becominghuman.ai/basics-of-neural-network-bef2ba97d2cf">Neural networks</a> all the way to researchers.</p><blockquote><strong>Use Case</strong></blockquote><p>With this project, I want to address a problem that all of us have: <strong><em>too many Whatsapp images and no way to sort them. </em></strong>As an initial experiment, I made a model that differentiates pictures of people from memes, so that they can be labelled or moved to be stored separately (currently working, hopefully there will be a part 2).</p><p>Keras is a BIG library, and thus many of it’s useful functions fly under the radar. In this post, I explore two of such functions:</p><blockquote><strong>ImageDataGenerator</strong></blockquote><p>ImageDataGenerator is a powerful tool that can be used for <strong>image augmentation</strong> and feeding these images into our model. The augmentation takes place in memory, and the generators make it very easy to setup training and testing data, <strong>without the need of manual labeling of the images</strong></p><blockquote><strong>Transfer Learning</strong></blockquote><p>Transfer learning is a popular technique, especially while using <a href="https://becominghuman.ai/making-a-simple-neural-network-2ea1de81ec20">CNN</a>s for computer vision tasks. In transfer learning, we take a big model that has already been trained for days (even weeks) on a huge dataset, use the low-level features it has learned and fine-tune it to out dataset to obtain a high level of accuracy.</p><figure><a href="https://aijobsboard.com/?fbclid=IwAR0GcsLs8A6CFM-fynrZMM3sTgo_Zpbto2CjglJm0Dmi6otC6YWc7CDFpQk"><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dAlwXfVrtYspdCzmC8iADw.png" /></a><figcaption>ML Jobs</figcaption></figure><p><strong>Combined, ImageDataGenerators and Transfer Learning drastically reduce both:</strong></p><ul><li><em>The amount of data required to train the model</em></li><li><em>The amount of time required to setup our own data</em></li></ul><h3>Setting up our Data</h3><p>To use the ImageDataGenerator, we set up our data on our machine in a specific directory structure. We then point the generators to these directories in the code, to tell them where they can fetch the images from.</p><p>The directory structure for our task looks like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/659/1*B7okHasd_M51F79tQr5qqg.jpeg" /><figcaption>Directory Structure for our data</figcaption></figure><p>As you can observe, I use only 110 images of each class. I can get away with using so little data and still getting results due to the power of transfer learning and using already learned features.</p><h3>Trending AI Articles:</h3><blockquote><a href="https://becominghuman.ai/microsoft-azure-machine-learning-x-udacity-lesson-4-notes-ab5444ed9227">1. Microsoft Azure Machine Learning x Udacity — Lesson 4 Notes</a></blockquote><blockquote><a href="https://becominghuman.ai/fundamentals-of-ai-ml-for-product-managers-64279e9d3681">2. Fundamentals of AI, ML and Deep Learning for Product Managers</a></blockquote><blockquote><a href="https://becominghuman.ai/roadmap-to-data-scientist-b1dcb17896e7">3. Roadmap to Data Science</a></blockquote><blockquote><a href="https://becominghuman.ai/work-on-artificial-intelligence-projects-65ea39a42106">4. Work on Artificial Intelligence Projects</a></blockquote><h4>A primer on Image Augmentation</h4><p>Image augmentation is a technique of preprocessing image data. It involves applying transformations (rotation, cropping, shearing, zooming etc.) on our existing images and adding these images to our database.</p><p>These transformed images are completely new to our model and help us in 2 two ways:</p><ul><li>Increases the size of our dataset</li><li>Makes our model more robust and capable of handling ‘true-to-life’ images, which are not ‘perfect’</li></ul><p>More on Image Augmentation <a href="https://www.youtube.com/watch?v=QWdYWwW6OAE">here</a></p><h3>Enough with theory, It’s Code Time!</h3><p>Firstly, we import required libraries for image preprocessing</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f71c71e9e47bd2f94c86fc966b71c8fc/href">https://medium.com/media/f71c71e9e47bd2f94c86fc966b71c8fc/href</a></iframe><p>Next, we setup our Training and Validation DataGenerators and point them to our image directories</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/18d8c925423075204017746e29026f27/href">https://medium.com/media/18d8c925423075204017746e29026f27/href</a></iframe><p>This line of code is used to define the transformations that the training DataGenerator will apply on all the images to augment the size of the dataset. The names are pretty self-explanatory, more information can be found in the <a href="https://www.tensorflow.org/api_docs/python/tf/keras/preprocessing/image/ImageDataGenerator">docs</a></p><p>train_datagen = ImageDataGenerator(rescale = 1./255, rotation_range=40, width_shift_range=0.2, height_shift_range=0.2, shear_range=0.2, zoom_range=0.2, horizontal_flip=True, fill_mode=’nearest’)</p><p>For the validation DataGenerator, we only specify the scaling factor. The other transformations are not required because we are not training the model on this data.</p><p>valid_datagen = ImageDataGenerator(rescale = 1./255)</p><p>Next, we define the Model. I am using a VGG-16 as my base model, and add custom output layers to it for Binary Classification</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/031e1a83df8d95af31b2ea6ae3982d3b/href">https://medium.com/media/031e1a83df8d95af31b2ea6ae3982d3b/href</a></iframe><p>We set layer.trainable=False for each layer of the VGG model, as we are using the pre-trained weights of the model.</p><p>Next, we flatten the outputs, add a custom Dense layer with ReLU activation and an Output layer with a Sigmoid activation.</p><p>Now we compile our model and train it on the dataset, that is augmented using ImageDataGenerators</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e19eabef3df2873905452849d12d46ca/href">https://medium.com/media/e19eabef3df2873905452849d12d46ca/href</a></iframe><p>After the model is done training, it is very important to save the model, so that it can be reused</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/20622a20e3c4d916a2b0a19599f6eb2d/href">https://medium.com/media/20622a20e3c4d916a2b0a19599f6eb2d/href</a></iframe><p>To view the performance of the Model graphically, we use Matplotlib</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f6918c6c0c35b71f4f0a7a00e4d32783/href">https://medium.com/media/f6918c6c0c35b71f4f0a7a00e4d32783/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/378/1*f1eqF3jTfcgeum0e192rbA.png" /></figure><p>To feed a test image into the model, we need to preprocess it first</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/efcd6e17990e3e58fcac941aa407b1e3/href">https://medium.com/media/efcd6e17990e3e58fcac941aa407b1e3/href</a></iframe><p>The result of the prediction is inferred as follows:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/82590b8205f0b2584d5781dc54e773cd/href">https://medium.com/media/82590b8205f0b2584d5781dc54e773cd/href</a></iframe><h4>But how do we know the classes?</h4><p>I spent a considerable time to understand what the numbers in the results mean, as it is not explicitly apparent.</p><p>ImageDataGenerator assigns numbers to classes based on the <strong>Alphabetic order of the class names.</strong> Since ‘memes’ comes before ‘people’ lexicographically, it is assigned the class number 0 and ‘people’ is assigned the class number 1.</p><h3>Conclusion</h3><p>This concludes our discussion for Part 1 of the series. The next article will focus on expanding the Model to cove more classes (Notes/Posters etc.), followed by minifying the model and serving it as a mobile application.</p><p><strong>The Code for this article can be found at:</strong></p><p><a href="https://github.com/malhotra-paritosh/Whatsapp_Sorter_DL">malhotra-paritosh/Whatsapp_Sorter_DL</a></p><h3>Footnote</h3><p>If you have read all this way, how about some claps👏🏻👏🏻 and comments💭💭</p><p>Au Revoir!</p><h3>Don’t forget to give us your 👏 !</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/780/0*2lvCls4yjxVMfZSR" /></figure><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fupscri.be%2F8f5f8b%3Fas_embed%3Dtrue&amp;dntp=1&amp;url=https%3A%2F%2Fupscri.be%2F8f5f8b&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=upscri" width="800" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/c43026df6fee7cdb1aab8aaf916125ea/href">https://medium.com/media/c43026df6fee7cdb1aab8aaf916125ea/href</a></iframe><figure><a href="https://becominghuman.ai/artificial-intelligence-communities-c305f28e674c"><img alt="" src="https://cdn-images-1.medium.com/max/255/1*2f7OqE2AJK1KSrhkmD9ZMw.png" /></a></figure><figure><a href="https://upscri.be/8f5f8b"><img alt="" src="https://cdn-images-1.medium.com/max/255/1*v-PpfkSWHbvlWWamSVHHWg.png" /></a></figure><figure><a href="https://becominghuman.ai/write-for-us-48270209de63"><img alt="" src="https://cdn-images-1.medium.com/max/255/1*Wt2auqISiEAOZxJ-I7brDQ.png" /></a></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c4787d430060" width="1" height="1" alt=""><hr><p><a href="https://becominghuman.ai/using-keras-imagedatagenerator-with-transfer-learning-c4787d430060">Using Keras ImageDataGenerator with Transfer Learning</a> was originally published in <a href="https://becominghuman.ai/">Becoming Human: Artificial Intelligence Magazine</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>