<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Data Augmentation | Haobin Tan</title><link>https://haobin-tan.netlify.app/tags/data-augmentation/</link><atom:link href="https://haobin-tan.netlify.app/tags/data-augmentation/index.xml" rel="self" type="application/rss+xml"/><description>Data Augmentation</description><generator>Hugo Blox Builder (https://hugoblox.com)</generator><language>en-us</language><lastBuildDate>Sun, 17 Jan 2021 00:00:00 +0000</lastBuildDate><image><url>https://haobin-tan.netlify.app/media/icon_hu7d15bc7db65c8eaf7a4f66f5447d0b42_15095_512x512_fill_lanczos_center_3.png</url><title>Data Augmentation</title><link>https://haobin-tan.netlify.app/tags/data-augmentation/</link></image><item><title>Data Augmentation</title><link>https://haobin-tan.netlify.app/docs/ai/pytorch/pytorch-recipes/data_augmentation/</link><pubDate>Sun, 17 Jan 2021 00:00:00 +0000</pubDate><guid>https://haobin-tan.netlify.app/docs/ai/pytorch/pytorch-recipes/data_augmentation/</guid><description>&lt;h2 id="what-is-data-augmentation">What is data augmentation?&lt;/h2>
&lt;p>To solve the problem that it&amp;rsquo;s hard to get enough data for training neural networks, &lt;strong>image augmentation is a process of creating new training examples from the existing ones. To make a new sample, you slightly change the original image.&lt;/strong>&lt;/p>
&lt;p>For instance, you could make a new image a little brighter; you could cut a piece from the original image; you could make a new image by mirroring the original one, etc. Here are some examples of transformations of the original image that will create a new training sample:&lt;/p>
&lt;p>&lt;img src="https://raw.githubusercontent.com/EckoTan0804/upic-repo/master/uPic/augmentation.jpg" alt="augmentation">&lt;/p>
&lt;p>By applying those transformations to the original training dataset, you could create an almost infinite amount of new training samples.&lt;/p>
&lt;h2 id="premise-of-data-augmentation">Premise of data augmentation&lt;/h2>
&lt;p>A &lt;a href="https://nanonets.com/blog/human-pose-estimation-2d-guide/">convolutional neural network&lt;/a> that can robustly classify objects even if its placed in different orientations is said to have the property called &lt;strong>invariance&lt;/strong>. More specifically, a CNN can be invariant to &lt;strong>translation, viewpoint, size&lt;/strong> or &lt;strong>illumination&lt;/strong> (Or a combination of the above).&lt;/p>
&lt;h2 id="when-to-apply-augmentation">When to apply augmentation?&lt;/h2>
&lt;p>The answer may seem quite obvious; we do augmentation &lt;strong>before&lt;/strong> we feed the data to the model.&lt;/p>
&lt;p>However, we have two options here:&lt;/p>
&lt;ul>
&lt;li>&lt;strong>Offline augmentation&lt;/strong>
&lt;ul>
&lt;li>Preferred for relatively &lt;strong>smaller datasets&lt;/strong>&lt;/li>
&lt;li>Increasing the size of the dataset by a factor equal to the number of transformations we perform
&lt;ul>
&lt;li>For example, by &lt;strong>flipping&lt;/strong> all my images, I would &lt;strong>increase the size&lt;/strong> of my odataset by a &lt;strong>factor of 2&lt;/strong>&lt;/li>
&lt;/ul>
&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>&lt;strong>Online augmentation / Augmentation on the fly&lt;/strong>
&lt;ul>
&lt;li>Preferred for &lt;strong>larger datasets&lt;/strong>, as we can’t afford the explosive increase in size.&lt;/li>
&lt;li>Perform transformations &lt;strong>on the mini-batches&lt;/strong> that we would feed to our model.&lt;/li>
&lt;/ul>
&lt;/li>
&lt;/ul>
&lt;h2 id="use-data-augmentation-in-the-right-way">Use data augmentation in the right way&lt;/h2>
&lt;p>‼️ &lt;strong>Do NOT increase irrelevant data!!!&lt;/strong>&lt;/p>
&lt;p>Sometimes not all augmentation techniques make sense for a dataset. Consider the following car example:&lt;/p>
&lt;figure>&lt;img src="https://raw.githubusercontent.com/EckoTan0804/upic-repo/master/uPic/1*vW3KGPp_w0wN6k3gYVlVHA.jpeg"
alt="The first image (from the left) is the original, the second one is flipped horizontally, the third one is rotated by 180 degrees, and the last one is rotated by 90 degrees (clockwise).">&lt;figcaption>
&lt;p>The first image (from the left) is the original, the second one is flipped horizontally, the third one is rotated by 180 degrees, and the last one is rotated by 90 degrees (clockwise).&lt;/p>
&lt;/figcaption>
&lt;/figure>
&lt;p>They are pictures of the same car, but our target application may NEVER see cars presented in these orientations. For example, if we&amp;rsquo;re gonna classify random cars on the road, only the second image would make sense to be in the dataset.&lt;/p>
&lt;h2 id="how-to-conduct-data-augmentation-in-pytorch">How to conduct data augmentation in PyTorch?&lt;/h2>
&lt;h3 id="use-torchvisiontransforms">Use &lt;code>torchvision.transforms&lt;/code>&lt;/h3>
&lt;ul>
&lt;li>Provides common image transformations&lt;/li>
&lt;li>Can be chained together using &lt;code>transforms.Compose&lt;/code>&lt;/li>
&lt;/ul>
&lt;h3 id="-use-albumentationshttpsgithubcomalbumentations-teamalbumentations">🔥 Use &lt;a href="https://github.com/albumentations-team/albumentations">&lt;code>albumentations&lt;/code>&lt;/a>&lt;/h3>
&lt;h4 id="demo">Demo&lt;/h4>
&lt;p>&lt;a href="https://albumentations-demo.herokuapp.com/">Demo&lt;/a> for viewing different augmentation transformations&lt;/p>
&lt;h2 id="when-will-data-augmentation-be-applied-in-pytorch">When will data augmentation be applied in PyTorch?&lt;/h2>
&lt;p>In any epoch the dataloader will apply a fresh set of random operations &lt;strong>“on the fly”.&lt;/strong> I.e. the augmentation happens inside of this line:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-python" data-lang="python">&lt;span class="line">&lt;span class="cl">&lt;span class="k">for&lt;/span> &lt;span class="p">(&lt;/span>&lt;span class="n">data&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">target&lt;/span>&lt;span class="p">)&lt;/span> &lt;span class="ow">in&lt;/span> &lt;span class="n">dataloader&lt;/span>&lt;span class="p">:&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Instead of showing the exact same items at every epoch, you are showing a variant that has been changed in a different way. So after three epochs, you would have seen three random variants of each item in a dataset.&lt;/p>
&lt;p>Note that each image will be transformed randomly on-the-fly, thus NO images will be generated and the length of &lt;code>Dataset&lt;/code> stays the SAME.&lt;/p>
&lt;p>If you want to perferm more augmentation and bring more varaibility for the dataset, just increase the number of epochs.&lt;/p>
&lt;blockquote>
&lt;p>Reference:&lt;/p>
&lt;ul>
&lt;li>&lt;a href="https://discuss.pytorch.org/t/data-augmentation-in-pytorch/7925">Data augmentation in PyTorch&lt;/a>&lt;/li>
&lt;li>&lt;a href="https://discuss.pytorch.org/t/transform-and-image-data-augmentation/71942">Transform and Image Data Augmentation&lt;/a>&lt;/li>
&lt;li>&lt;a href="https://discuss.pytorch.org/t/basic-question-about-torchvision-transforms/40213">Basic question about torchvision.transforms&lt;/a>&lt;/li>
&lt;/ul>
&lt;/blockquote>
&lt;h2 id="reference">Reference&lt;/h2>
&lt;ul>
&lt;li>&lt;a href="https://nanonets.com/blog/data-augmentation-how-to-use-deep-learning-when-you-have-limited-data-part-2/">Data Augmentation | How to use Deep Learning when you have Limited Data — Part 2&lt;/a>&lt;/li>
&lt;/ul></description></item></channel></rss>