<?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 Andy Wang on Medium]]></title>
        <description><![CDATA[Stories by Andy Wang on Medium]]></description>
        <link>https://medium.com/@andywanghvac?source=rss-d7d3c4867d3------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*IXseIRr8w1OWKnUl</url>
            <title>Stories by Andy Wang on Medium</title>
            <link>https://medium.com/@andywanghvac?source=rss-d7d3c4867d3------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 10:29:01 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@andywanghvac/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 Gentle Introduction to Batch Processing in Keras]]></title>
            <link>https://medium.com/@andywanghvac/a-gentle-introduction-to-batch-processing-in-keras-ec9290febb73?source=rss-d7d3c4867d3------2</link>
            <guid isPermaLink="false">https://medium.com/p/ec9290febb73</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[batch-processing]]></category>
            <category><![CDATA[ndsc]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[generator]]></category>
            <dc:creator><![CDATA[Andy Wang]]></dc:creator>
            <pubDate>Sat, 30 Mar 2019 10:50:37 GMT</pubDate>
            <atom:updated>2019-03-30T14:41:35.529Z</atom:updated>
            <content:encoded><![CDATA[<p>As many of you may have already heard of, Keras is high-level neural networks API capable of running on top of deep learning frameworks such as TensorFlow, CNTK or Theano. When it comes to deep learning, the first thing that pops up in your mind would be tons of data entries or hundreds of thousands of images. How to deal with the gigantic dataset which could easily eat up your memory? This is the first step before diving into deep learning with Keras.</p><p>Batch processing is widely used in Keras to process dataset in batch instead of loading all the data in one shot. By doing this, the computer memory can be used in a more efficient manner and the memory bottleneck will no longer be an issue for deep learning even on a normal office laptop. Isn’t it fantastic to do deep learning on your 3-year old laptop with 16GB RAM and integrated graphics? This is how I did deep learning recently in the biggest national data science competition in Singapore to classify e-commerce products using text and images (approximately 70GB of 800,000 images). Machine learning is for everybody! The source code of my solution can be found <a href="https://github.com/andywang13/NDSC"><strong>here</strong></a>. Feel free to drop any suggestions and recommendations. One of the key takeaways from this Kaggle competition is a closer insight on different ways of batch processing in Keras, and this is what I will be sharing below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*AxrmemgHeZtYlWpUQS88Kg.png" /><figcaption>The largest Data Science Competition in Singapore</figcaption></figure><p>If you go to <a href="https://keras.io/"><strong>Keras documentation</strong></a><strong> </strong>and click on <strong>Sequential</strong> on the left panel, you’ll see a list of sequential model methods which are basically the ways to feed data into models for training, evaluation and prediction. The detailed explanation to the arguments in different methods is well documented in Keras documentation. Feel free to go through them if you want to have a better understanding of different settings. Here, I’ve summarized the key arguments which are related to batch processing.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/918/1*OmDd3aTeyea7OoAfb8xdzA.png" /><figcaption>Summary of Sequential Model Methods</figcaption></figure><p><strong>x, y</strong>: Numpy array of data (if the model has a single input), or list of Numpy arrays (if the model has multiple inputs)</p><p><strong>batch_size</strong>: Number of samples per gradient update</p><p><strong>generator</strong>: A generator or an instance of Sequence (keras.utils.Sequence) object</p><p><strong>steps_per_epoch</strong>: Total number of steps (batches of samples) to yield from generator before declaring one epoch finished and starting the next epoch</p><p><strong>steps</strong>: Total number of steps (batches of samples) to yield from generator before stopping</p><p>From the table above, it’s clear that Keras has the option to either feed data (x,y) into the model in one shot as numpy arrays or feed data batch by batch through generator/Sequence object. Take the above Kaggle competition as an example. Because of the memory limitation, it’s impossible to first pre-process 70GB of image and then save all the processed results to numpy arrays for the model to use. Hence, fit, evaluate and predict methods are not feasible. The go-to method should be generator-related methods (fit_generator, evaluate_generator, predict_generator) which feed the image data into the model batch by batch (data is loaded into and released from memory batch by batch). So how to create a generator for image pre-processing? Keras provides a few built-in methods to create a generator. Here, I will focus on the two most commonly used ones: <strong>flow_from_directory</strong> and <strong>flow_from_dataframe</strong>.</p><h3><strong><em>flow_from_directory</em></strong></h3><p>This method requires all images to be placed under respective subfolders for training and validation data, with one subfolder per class. The subfolder names are class names. For test data, images to be placed under a single subfolder. The directory tree should follow the following structure.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/328/1*Y78rHRaLewKREyHuF0WtDg.png" /><figcaption>Sample Folder Structure</figcaption></figure><p>Once all images are placed under respective folders, you are ready to create generators. It’s important to note that generators should be created separately for training, validation and test data. Sample codes like below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/490/1*iVKXrrwx-NotOX8z70FfeA.png" /><figcaption>Sample Code for flow_from_directory</figcaption></figure><p>As you may have noticed, this method is most suitable when images are already in respective folders. When all images are in one folder (quite normal in real world), re-allocating images to different folders is necessary, which includes creating subfolders, moving images to correct subfolders, etc. For big dataset like the competition above, it could take hours to finish image re-allocation.</p><p>For more information on other settings in this method, you can refer to a very comprehensive tutorial <a href="https://medium.com/@vijayabhaskar96/tutorial-image-classification-with-keras-flow-from-directory-and-generators-95f75ebe5720">here</a>.</p><h3><strong>flow_from_dataframe</strong></h3><p>Another method of creating generators is to create directly from dataframe where the image paths and categories are available. This method doesn’t require subfolders for different classes, which saves the efforts to re-allocate images. Similar to flow_from_directory method, generators need to be created separately for training, validation and test data. In each generator, it is assumed all images are in the same folder by default, and a directory pointing to the folder needs to be specified. If images are not all in the same folder, the directory can be set as None but the image path in the dataframe should be absolute path. For the ease of mind, it is recommended to pre-process the image paths in the dataframe and make sure they are absolute paths before creating generators using this method. Below is an example dataframe with absolute paths and the sample code.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/921/1*R2gKfUpuuMikBk-ai8LNmA.png" /><figcaption>Sample Data Frame for flow_from_dataframe</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/505/1*-otg_n0qTuuLSDiwPOVFdw.png" /><figcaption>Sample Code for flow_from_dataframe</figcaption></figure><p>It is to be noted that y_col needs to be set as None in test generator. For more information on other settings in this method, you can refer to a very comprehensive tutorial <a href="https://medium.com/@vijayabhaskar96/tutorial-on-keras-flow-from-dataframe-1fd4493d237c">here</a>.</p><h3><strong>Sequence Object</strong></h3><p>Instead of using Keras built-in methods to create a generator, Keras Sequence object is another way of dealing with batch processing. It is a base object for fitting to a sequence of data. Sequence guarantees that the model will only train once on each sample per epoch which is not the case with generators. In generators, if the dataset size is not divisible by the batch size, the last batch of the epoch will be smaller than other batches and the generator is expected to loop over to the data indefinitely until the model finishes training the number of steps_per_epoch batches per epoch. Sequence object is fully customizable, you can embed any pre-processing procedures into it (e.g. image resizing, image normalization, etc). Different from the above two methods, training and validation data can share the same Sequence object, while Sequence object for test data is different. Below is the sample code to create Sequence object using the same data frame in flow_from_dataframe section.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/708/1*Ow1u4OAyxAbkOP18yg5Sdg.png" /><figcaption>Sample Code for Sequence Object</figcaption></figure><p>The Sequence object training_set, validation_set and test_set can be fed into the model just like generators.</p><p>The above are the three methods I’ve tried to deal with batch processing in the Kaggle competition. flow_from_directory is the most time-consuming one due to image relocation, followed by flow_from_dataframe and Sequence object. Sequence turns out to be the fastest, probably due to the customization that cuts some built-in features in flow_from_dataframe. I used sequence object for batch processing in the competition. Any shortcomings with Sequence object? I’m looking forward to hearing your opinions.</p><p>Feel free to leave comments if you have any questions.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ec9290febb73" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>