<?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 Meetu Patel on Medium]]></title>
        <description><![CDATA[Stories by Meetu Patel on Medium]]></description>
        <link>https://medium.com/@meetuspatel?source=rss-6e08d096aec0------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*y6QPpC7HOF69etCfwVByyg.jpeg</url>
            <title>Stories by Meetu Patel on Medium</title>
            <link>https://medium.com/@meetuspatel?source=rss-6e08d096aec0------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 02:34:34 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@meetuspatel/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[Beginner’s Guide towards Data Pre-Processing for any Computer Vision Project]]></title>
            <link>https://medium.com/@meetuspatel/beginners-guide-towards-data-pre-processing-for-any-computer-vision-project-98df8902e980?source=rss-6e08d096aec0------2</link>
            <guid isPermaLink="false">https://medium.com/p/98df8902e980</guid>
            <category><![CDATA[computer-vision]]></category>
            <category><![CDATA[image-augmentat]]></category>
            <category><![CDATA[data-pre-processing]]></category>
            <dc:creator><![CDATA[Meetu Patel]]></dc:creator>
            <pubDate>Mon, 21 Sep 2020 18:39:28 GMT</pubDate>
            <atom:updated>2020-09-21T18:39:28.765Z</atom:updated>
            <content:encoded><![CDATA[<h3><strong>What is Computer Vision?</strong></h3><p>Computer Vision has become an extremely popular domain in the world of Artificial Intelligence and Data Science. Computer Vision is basically about training computers to perceive and understand things that we, humans, see. Let’s take an example of a simple classification problem. Suppose we want to classify whether the image is of a car or not. For this, the computer is fed with lots of car images and is trained using specialised algorithms to detect the car. Then it learns how a car looks by learning about the edges, corners and the shapes. Of course, for a computer, it’s all about numbers and pixel values.</p><p>Since last decade, Computer Vision has made so many solutions possible. Self-driving cars are no longer just a fantasy. It has made object recognition and classification seem a simple task. Furthermore, it also has a lot of applications in healthcare such as recognising cancer tumours in an MRI scan.</p><p>I remember when I worked on my first real-world Computer Vision project during my internship, it seemed daunting to me to deal with a big real-world dataset. Till then, whatever dataset I had worked on was from the Deep Learning courses that I did. I realised the stark difference between the two types of datasets. The datasets from the courses did not require much pre-processing and they easily gave good accuracy from the first try.</p><p>In this blog post, you will learn about the simple ways to pre-process data for a deep learning project.</p><h3><strong>Analysing the data</strong></h3><p>Start by analysing your data. <strong>Check the number of images in the dataset</strong>. A good rule of thumb is that a higher number of images gives better results. But it also depends on the complexity of the algorithm. A simple convolutional neural network will give good results even with few hundreds of images if they are of good quality and diverse. Don’t worry if you don’t have thousands of images. You can always augment them which we will look at in the coming part.</p><h3><strong>Resizing the data</strong></h3><p>Often times, the images in the dataset have varying sizes. The most important thing is to have all images of the same size. Again, the size of the image depends on the depth of the neural network and your resources. Larger dimensional images require more computing power and GPU space. Similarly, if the number of layers in your network is high, you can afford to have sizes as large as (512,512). But generally, you can experiment with sizes (256,256), (96,96) and (64,64).</p><p>Now, the most essential part while resizing your image is maintaining the aspect ratio. Below is a snippet of code which resizes image while keeping the aspect ratio intact. You can import <em>Image</em> from <em>PIL</em> module. The function resize_image takes parameters such as the image, the target size and the target mode such as RGB, HSV, etc. First, I found the minimum ratio from the (target_width/original_width) and (target_length/orginal_length). Next, I multiplied the original length and width of the image by the minimum ratio. This new_width and new_length will not exactly be our targeted width and length. To do that, I created a black canvas of size (target_width, target_length) with the function <em>Image.new()</em> and then pasted the original image onto it. To paste, we need to find the coordinates of the upper left-hand corner. To find the x- coordinate, I subtracted the canvas width by new_width and divided by 2. Similarly for y-coordinate.</p><pre>from PIL import Image</pre><pre>def resize_image(image,target_size,target_mode):<br>    new_width = target_size[0]<br>    new_height = target_size[1]<br>    <br>    ratio = min(new_width/image.size[0],new_height/image.size[1])    <br>    new_width = int(image.size[0]*ratio)<br>    new_height = int(image.size[1]*ratio)<br>    <br>    resize_image=image.resize((new_width,new_height),Image.BICUBIC)<br>    canvas = Image.new(mode=target_mode,size     (target_size[0],target_size[1]),color=&#39;black&#39;)<br>    offset = ((canvas.size[0]-new_width)//2,(canvas.size[1]-new_height)//2)<br>    <br>    canvas.paste(resize_image,offset)<br>    return canvas</pre><h3><strong>Image Augmentation</strong></h3><p>Next step is to do image augmentation. It is a method to expand the number of images by performing variations such as rotation, shear, horizontal or vertical flip, etc. This is an optional step but a very recommended step for two reasons. First, <strong>it increases the size of the data</strong>. And as the data size increases, the performances improves. Second, <strong>it introduces variety in our data set</strong> which reduces the chances of over-fitting. For example, let’s say our dataset contains only clearly visible images of cats. What will happen is that the computer will only be able to recognise cats if the images are completely cleared and lead to over-fitting. But we want it to identify the image even if the image is blurred or rotated or flipped. That’s where augmentation comes in.</p><p>Below you will find a code snippet for augmenting images. There is a function in Keras called <em>keras.preprocessing.image.ImageDataGenerator</em> which generates batches of images. It takes many parameters. You can find it’s full list and documentation here on this page <a href="https://keras.io/api/preprocessing/image/">https://keras.io/api/preprocessing/image/</a>. Next, the function <em>flow_from_directory()</em> takes the path to the directory and generates batches of augmented data. It returns a directory iterator which contains a <br>tuple (x,y). x is a NumPy array containing a batch of images and y is a NumPy array of corresponding labels. Set the same target_size so the size doesn’t change. Finally, to save the images I am using cv2 module.</p><pre>import tensorflow as tf<br>from tensorflow import keras<br>import cv2</pre><pre>generator = keras.preprocessing.image.ImageDataGenerator(rotation_range=10, shear_range=0.2, zoom_range=0.1, <br>                                                      fill_mode=&#39;constant&#39;, cval=0,<br>                                                      horizontal_flip=True)<br>                                        <br>directoryiter = generator.flow_from_directory(directory=input_path, target_size=target_size,<br>                                            class_mode=&#39;binary&#39;, batch_size=1, shuffle=True, seed=50,<br>                                            interpolation=&#39;bicubic&#39;)</pre><pre>for X, y in directoryiter:<br>   if count &gt; 0:<br>        if y[0]&gt;0.5:<br>            cv2.imwrite(output_path1 + &quot;image{0}&quot;.format(count),X[0])  #for positive class<br>        elif y[0]&lt;0.5:<br>            cv2.imwrite(output_path2 + &quot;image{0}&quot;.format(count),X[0])  #for second class<br>    else:<br>        break<br>    count-=1</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=98df8902e980" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>