<?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 Parasrawatjr on Medium]]></title>
        <description><![CDATA[Stories by Parasrawatjr on Medium]]></description>
        <link>https://medium.com/@parasrawatjr?source=rss-ee9554dace8f------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*uubx10ibkL5Sqr2k.jpg</url>
            <title>Stories by Parasrawatjr on Medium</title>
            <link>https://medium.com/@parasrawatjr?source=rss-ee9554dace8f------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 12:33:32 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@parasrawatjr/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[Image Classification using FastAI and Transfer Learning]]></title>
            <link>https://medium.com/data-science-community-srm/image-classification-using-fastai-and-transfer-learning-463b47285a4f?source=rss-ee9554dace8f------2</link>
            <guid isPermaLink="false">https://medium.com/p/463b47285a4f</guid>
            <category><![CDATA[fastai]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[transfer-learning]]></category>
            <category><![CDATA[deep-learning]]></category>
            <dc:creator><![CDATA[Parasrawatjr]]></dc:creator>
            <pubDate>Mon, 27 Jul 2020 09:24:25 GMT</pubDate>
            <atom:updated>2020-09-21T13:00:24.161Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="CNN classifying the input into Cats or Dogs" src="https://cdn-images-1.medium.com/max/1024/1*rAbCk0T4rksShBcPQjWC0A.gif" /><figcaption>CNN classifying Cats and Dogs</figcaption></figure><p>Image classification is a well known computer vision task. There are multiple approaches for this task but rather than dwelling too deep into the other, more lengthier methods, we will have a go at it using <strong><em>FastAI </em></strong>and<strong><em> Transfer Learning.</em></strong></p><p>The FastAi library is a high-level library built on PyTorch, which allows us to build models using only a few lines of code. Furthermore it implements some of the newest state-of-the-art techniques taken from research papers that allow you to get state-of-the-art results on almost any type of problem.</p><p>Lets get started!</p><h3><strong>Installing the FastAi Library</strong></h3><p>Before we begin with our main code we first have to install the FastAi library!</p><pre>conda install -c pytorch pytorch-cpu torchvision<br>conda install -c fastai fastai</pre><p>After the installation is complete, we have to run the imports:</p><pre>from fastai.vision import *</pre><h3>Loading your data:</h3><p>FastAi provides specific data objects called <em>databunch</em>es which are needed to train a model. The <em>databunches</em> can be created using two methods.</p><p>The first way is to use problem-specific methods like the <em>ImageDataBunch.from_folder </em>which can be used to load data that has the following structure.</p><pre><strong>data-dir</strong><br>  -<strong><em>Train</em></strong><br>     -Class1<br>     -Class2<br>     -...<br>   -<strong><em>Valid</em></strong><br>     -Class1<br>     -Class2<br>     -...</pre><p>This of course is a specific work case where our data is stored in this exact manner. If that is not the case, FastAi has another great method called <a href="https://docs.fast.ai/data_block.html">data block api</a> which gives the user more options to load the data. Once our data is in the given format we can load it in!</p><p>Code for Loading the data:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4295f3755a5dd3878bd5a5abc62eee8c/href">https://medium.com/media/4295f3755a5dd3878bd5a5abc62eee8c/href</a></iframe><p>We can use this code to get information about our classes.</p><pre>data.classes</pre><pre>Out: [&#39;Cats&#39;, &#39;Cows&#39;, &#39;Dogs&#39;, &#39;Horses&#39;]</pre><p>We can show also random batch of images using the <em>show_batch</em> method.</p><pre>data.show_batch(rows=3, figsize=(7, 8))</pre><h3>Creating your model:</h3><p>Now this is where FastAi shines. The FastAi library lets you create models and use Transfer Learning in just a few lines of code! They provide a method called <a href="https://docs.fast.ai/vision.learner.html#create_cnn"><em>create_cnn,</em></a><em> </em>which can be used to create a convolutional neural network<em>.</em></p><p>The main parameters required for this method are the data and the architecture. More parameters can be used to customize the model as you wish!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/be72d30f33dcaa132afe1ce6e79c3550/href">https://medium.com/media/be72d30f33dcaa132afe1ce6e79c3550/href</a></iframe><p>As you can see, our model is using <a href="https://arxiv.org/pdf/1512.03385.pdf">ResNet34</a> which comes pretrained on the <a href="http://www.image-net.org/">imagenet</a> dataset. You can check out other pretrained <a href="https://docs.fast.ai/vision.models.html">models</a> which are available for use!</p><p>Since we are using a pretrained model, we need to unfreeze only the last Fully Connected Layers for training. Lucky for us the FastAi Library does this by default.</p><p>Another trick for quick training is the <em>fit_one_cycle</em> method. FastAi offers the <em>fit</em> and <em>fit_one_cycle</em> method for training. The base difference is that the <em>fit_one_cycle </em>method changes the learning rate over time to get better results whilst the <em>fit </em>method keeps it static.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4fed7160c9553dd01958f0f226c09fae/href">https://medium.com/media/4fed7160c9553dd01958f0f226c09fae/href</a></iframe><p>The output will look like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/352/1*_fdM0xH9CP-owhhsFA_RBg.png" /><figcaption>Training output</figcaption></figure><p>As you can see our loss is reducing but its not quite there yet. Here is how we can improve on this.</p><p>The FastAi library offers something called <strong>differential learning rates </strong>which basically means that we can use different learning rates for different parts of the network.</p><p>We can use the <em>lr_find </em>and <em>recorder.plot </em>methods which tell us how the loss reacts to different learning rates.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7dad4519220ecd4c46b1f69812ccbbd1/href">https://medium.com/media/7dad4519220ecd4c46b1f69812ccbbd1/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/407/1*eaTFFUilBH-Zx-3G0ED7zw.png" /><figcaption>lr_find plot</figcaption></figure><p>To interpret this, we simple need to find the learning rates between which our loss has the steepest gradient or the steepest slope.</p><p>To now train the model using differential learning rates we need to pass the <em>max_lr</em> argument to the <em>fit_one_cycle</em> method.</p><pre>learn.fit_one_cycle(4, max_lr=slice(1e-3, 1e-2))</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/328/1*eLCrdiaEduLEbIaryS6ZvA.png" /><figcaption>Final Output</figcaption></figure><h3>Inference</h3><p>You can run inference on an image using the <em>cnn_learner().predict</em> method</p><pre>learn.predict(img)</pre><p>More information on running inference is provided here: <a href="https://docs.fast.ai/tutorial.inference.html">Link</a></p><h3>Saving the Model</h3><p>Finally after having trained your model successfully you can save it.</p><pre>learn.save(&#39;image_classification&#39;)</pre><h3>Conclusion</h3><p>The FastAi library offers some really cool methods to create and train models in just a few lines of code. They also offer a lot of hands on courses on their website. You can check them out <a href="https://course.fast.ai/">here</a>.</p><p>All the above code is available on this <a href="https://github.com/TannerGilbert/Tutorials/blob/master/FastAI/Animal%20detector%20from%20Google%20images.ipynb">GitHub</a> repo.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=463b47285a4f" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science-community-srm/image-classification-using-fastai-and-transfer-learning-463b47285a4f">Image Classification using FastAI and Transfer Learning</a> was originally published in <a href="https://medium.com/data-science-community-srm">DataX Journal</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>