<?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[Tensorpad - Medium]]></title>
        <description><![CDATA[Data Intelligence - Medium]]></description>
        <link>https://medium.com/tensorpad?source=rss----78c19b7d9ce5---4</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>Tensorpad - Medium</title>
            <link>https://medium.com/tensorpad?source=rss----78c19b7d9ce5---4</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 09:25:20 GMT</lastBuildDate>
        <atom:link href="https://medium.com/feed/tensorpad" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Fastai Training metrics as Notification on mobile]]></title>
            <link>https://medium.com/tensorpad/fastai-training-metrics-as-notification-on-mobile-1b2d419535e7?source=rss----78c19b7d9ce5---4</link>
            <guid isPermaLink="false">https://medium.com/p/1b2d419535e7</guid>
            <category><![CDATA[transfer-learning]]></category>
            <category><![CDATA[fastai]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[training-notifications]]></category>
            <dc:creator><![CDATA[Sai Nath]]></dc:creator>
            <pubDate>Mon, 05 Aug 2019 11:56:06 GMT</pubDate>
            <atom:updated>2019-09-17T18:49:32.271Z</atom:updated>
            <content:encoded><![CDATA[<h3>FastAI Training metrics as Notification on mobile</h3><h4>Get the model updates on mobile as notifications — this would work on any of the Cloud providers or even Google Colab</h4><p>Ever had to train a machine learning model for so long that you had to sit and stare at the screen every time an epoch got over to have a look at the model metrics?</p><p>Forget it! using the combined power of Fastai callbacks and pushover.net you can get training notifications right on your mobile phone</p><p>This is possible because of the rich callbacks system in FastAI library</p><p>Lets split our tutorial into three sections</p><ul><li>pushover.net configuration on mobile</li><li>Pneumonia detection model training with Fastai</li></ul><p>Github repo link → <a href="https://github.com/zszazi/pneumonia-detection-fastai">pneumonia-detection-fastai</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/354/1*0VsGHw2goOFpeis6bqZJhA.png" /><figcaption><strong>Training notifs on mobile</strong></figcaption></figure><h4>STEP 1 -&gt; Setting up a workspace on your favourite cloud provider</h4><p>Open the terminal from Jupyter lab and clone the repo git clone https://github.com/zszazi/pneumonia-detection-fastai.git to your workspace</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PF9iXCpzzqBfFq5fxwZqiQ.png" /></figure><ul><li>now open the Jupyter notebook by name pneumonia_prediction_using_fastau_with_mobile_notification.ipynb</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iYKdmB_Ciy_DbDcc7olq7w.png" /></figure><h4>STEP 2 -&gt; Pushover.net configuration on mobile</h4><p>Head over to <a href="https://pushover.net/">https://pushover.net</a> and set up an account and download the mobile app too, You will only be able to receive notifications on mobile if you have their app installed. They have extensive <a href="https://pushover.net/api">docs</a> and <a href="https://pushover.net/faq">faq</a> which cover on setting up, to receive notifications on your pushover mobile app.</p><blockquote>pushover currently offers a <a href="https://pushover.net/faq#overview-fees">7 day trial</a> on signup accross all platforms</blockquote><p>Copy the API keys; there are two of these found in your account section</p><pre>&quot;user&quot;  : &quot;your key&quot;,<br>&quot;token&quot; : &quot;Your key&quot;,</pre><h4>STEP 3 -&gt; Training the model and getting notifications on mobile</h4><p>To run the cell press <em>Shift+Enter</em></p><ul><li>Make sure to upload the <em>kaggle.json</em> (watch this <a href="https://www.youtube.com/watch?v=eEgZtNOCJhk">Youtube video</a>)</li><li>We can use the upload button in Jupyter workspace to upload the kaggle.jsonfile</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/510/1*xtiTBx1fKlGxiql9BjDy6g.png" /></figure><ul><li>On running the cell containing learn.fit_one_cycle() you should have got the push notifications on your mobile, and this would be sent for each epoch</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/928/1*KjOdvgBoDiMi9yHphSYo3A.png" /></figure><h4>🔔 Training Notifications on mobile 🔔</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/450/1*w375vvWj50ZL2-AK-KLq6w.png" /></figure><p><strong>What’s more?</strong> You can even send your <strong>loss plots</strong> as 🔔 to mobile.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/300/1*SMk0mS63xNRrRYNoVpbjzQ.jpeg" /><figcaption><strong>LossPlot as notification!</strong></figcaption></figure><p>Here’s a Github gist showing you the central part of mobile callback notification</p><p><em>code</em></p><p>GitHub gist</p><p>FastAI provides you with a rich collection of callbacks (<em>see pic</em>), but we mainly have used the following ones: whose name are self-explanatory :)</p><ul><li>on_train_begin</li><li>on_epoch_begin</li><li>on_train_end</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/672/1*aVBO-anWoBYA0DFW4zZd2Q.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/663/1*qSUoXOYkyHcyWZztxUdzRA.png" /><figcaption>Screenshot from Fastai <a href="https://docs.fast.ai/callbacks.html">doc</a></figcaption></figure><p><strong>Resources</strong></p><ol><li>FastAI <a href="https://forums.fast.ai/">forums</a></li><li>FastAI <a href="https://docs.fast.ai/callbacks.html">Callbacks</a></li><li>Kaggle<a href="https://www.kaggle.com/paultimothymooney/chest-xray-pneumonia/"> dataset</a></li><li>Pushover <a href="https://pushover.net/api">docs</a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1b2d419535e7" width="1" height="1" alt=""><hr><p><a href="https://medium.com/tensorpad/fastai-training-metrics-as-notification-on-mobile-1b2d419535e7">Fastai Training metrics as Notification on mobile</a> was originally published in <a href="https://medium.com/tensorpad">Tensorpad</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Pythia (Facebook)— Greek god doing Deep learning]]></title>
            <link>https://medium.com/tensorpad/pythia-facebook-greek-god-doing-deep-learning-ff1d2a76c6d9?source=rss----78c19b7d9ce5---4</link>
            <guid isPermaLink="false">https://medium.com/p/ff1d2a76c6d9</guid>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[facebook]]></category>
            <dc:creator><![CDATA[Sai Nath]]></dc:creator>
            <pubDate>Tue, 11 Jun 2019 08:19:53 GMT</pubDate>
            <atom:updated>2019-09-17T18:50:22.670Z</atom:updated>
            <content:encoded><![CDATA[<h3>Pythia (Facebook) — Greek god doing Deep learning</h3><h4>“Artificial Intelligence” in 2019 has been exciting. Can it be more exciting than this? Guess what I found an answer for it and the answer is from Facebook Research</h4><p>Recently Facebook open sourced their new framework called <strong>Pythia</strong></p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgfycat.com%2Fifr%2Fjealousilliterategrassspider&amp;url=https%3A%2F%2Fgfycat.com%2Fjealousilliterategrassspider&amp;image=https%3A%2F%2Fthumbs.gfycat.com%2FJealousIlliterateGrassspider-size_restricted.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=gfycat" width="1920" height="912" frameborder="0" scrolling="no"><a href="https://medium.com/media/8d7cad46b0f83298ffe20320faaa2b1e/href">https://medium.com/media/8d7cad46b0f83298ffe20320faaa2b1e/href</a></iframe><p><strong>ELI5:</strong> Give Pythia an image and shoot questions to it, get your questions answered; sounds interesting isn’t it, but Pythia can do more than this….</p><p>Pythia is the new multimodal research framework for supercharging vision and language tasks built on top of Pytorch</p><p>Is there a reason behind naming it Pythia?</p><blockquote>‘<em>The name ‘</em><a href="https://en.wikipedia.org/wiki/Pythia"><em>Pythia</em></a><em>’ is an homage to the Oracle of Apollo at Delphi, who answered questions in Ancient Greece.’</em></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/450/1*BvN1Crkd4CcsFOakuAYQvA.jpeg" /><figcaption><em>Lycurgus Consulting the Pythia</em></figcaption></figure><p>Ever wondered how great it would be if we had a single framework that could incorporate both NLP and vision tasks easily, this is what Pythia does, it is designed for answering questions related to visual data and automatically generating image captions</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Iz2fIHr5UqyqZ009mFvZFg.png" /><figcaption><a href="https://visualqa.org/">https://visualqa.org</a></figcaption></figure><p>CloudCV has a <a href="https://vqa.cloudcv.org/">Demo of Pythi</a>a up and running which you can try it out</p><p>Pythia incorporates elements of Facebook research’s winning entries in recent AI competitions (the <a href="https://arxiv.org/abs/1807.09956">VQA Challenge</a> 2018 and <a href="http://vizwiz.org/data/#challenge">Vizwiz Challenge 2018</a>) done by the Facebook AI Research (FAIR)’s A-STAR (Agents that See, Talk, Act, and Reason) team</p><p><a href="https://visualqa.org/">Visual Question Answering (VQA)</a> Challenge- Given an image and a natural language question about the image, the task is to provide an accurate natural language answer.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*M8cdF8zbQA42VzZHSgfZSw.jpeg" /><figcaption>Pythia got an accuracy of 70.24% on VQA v2.0 dataset</figcaption></figure><p>In addition to multitasking, Pythia also supports distributed training and a variety of datasets, as well as custom losses, metrics, scheduling, and optimizers.</p><p>Now let’s get into <strong>Technical Details</strong></p><ol><li><strong>Bottom-up and Top-down attention</strong></li></ol><p>The critical idea in up-down is the use of an object detector — <a href="https://arxiv.org/abs/1506.01497">Faster RCNN</a> pre-trained on the <a href="https://visualgenome.org/">Visual Genome dataset</a> — to extract image features with bottom-up attention, i.e., visual feed-forward attention. A ResNet-101 was chosen as the backbone network. Visual Genome is a knowledge base to connect structured image concept to language</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eHGTu5K0KRj3h1r0lkGzQg.png" /><figcaption>Visual Genome Dataset</figcaption></figure><p>The question’s text is then used to compute the top-down attention, i.e., task-specific attention, for each object in the image. Multi-modal fusion is done through a simple <a href="https://www.google.co.in/url?sa=t&amp;rct=j&amp;q=&amp;esrc=s&amp;source=web&amp;cd=1&amp;cad=rja&amp;uact=8&amp;ved=2ahUKEwj9vJ6hnNriAhUu63MBHYJnBjUQFjAAegQIBRAB&amp;url=https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FHadamard_transform&amp;usg=AOvVaw0mGIa2CrO6YzEcIjh22pYy">Hadamard product</a> (also called Schur product) followed by a multi-label classifier using a sigmoid activation function to predict the answer scores. The performance reached 70.34% on VQA 2.0 test-std split with an ensemble of 30 models.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/834/1*mpP_nu7Bui7UdTIBDe74Xw.png" /><figcaption><a href="https://www.google.co.in/url?sa=t&amp;rct=j&amp;q=&amp;esrc=s&amp;source=web&amp;cd=1&amp;cad=rja&amp;uact=8&amp;ved=2ahUKEwj9vJ6hnNriAhUu63MBHYJnBjUQFjAAegQIBRAB&amp;url=https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FHadamard_transform&amp;usg=AOvVaw0mGIa2CrO6YzEcIjh22pYy">Hadamard product</a></figcaption></figure><p>2. <strong>Model Architecture</strong></p><p>A weight normalization followed by ReLU was used instead of gated hyperbolic tangent activation to reduce computations involved. As evident from the application of Pythia, it needed some way of connecting the Text and Image, and this was done with the help of element-wise multiplication to combine the features from text and visual modalities when computing the top-down attention(task-specific) instead of just feature concatenation. How do we represent the Task Question? Use <a href="https://nlp.stanford.edu/pubs/glove.pdf">GloVe</a> (Global Vectors) then pass the word embeddings onto a <a href="https://en.wikipedia.org/wiki/Gated_recurrent_unit">GRU</a> network(Gated Recurrent Unit-a LSTM with a forget gate) and a question attention module to extract attentive text features and discard unnecessary ones.</p><p>3.<strong> Optimizer</strong></p><p>The optimizer used is <a href="http://ruder.io/optimizing-gradient-descent/index.html#adamax">ADAMAX</a> (a version of ADAM). This fantastic post on <a href="http://ruder.io/optimizing-gradient-descent">ruder.io</a> gives an overview of most of the optimizers.</p><h4>Why Pythia matters?</h4><blockquote>Pythia smooths the process of entering the growing subfield of vision and language and frees researchers to focus on faster prototyping and experimentation. Our goal is to accelerate progress by increasing the reproducibility of these models and results. This will make it easier for the community to build on, and benchmark against, successful systems. We hope that removing some of the obstacles will allow researchers to more quickly develop new ways for people and intelligent machines to communicate. This work should also help researchers develop adaptive AI that synthesizes multiple kinds of understanding into a more context-based, multimodal understanding. In addition to this open source release, we plan to continue adding tools, tasks, data sets, and reference models.(source- <a href="https://code.fb.com/ai-research/pythia/">https://code.fb.com/ai-research/pythia/</a>)</blockquote><h3>Features of Pythia</h3><ul><li><strong>Model Zoo: </strong>Reference implementations for state-of-the-art vision and language model</li><li><strong>Multi-Tasking: </strong>Support for multi-tasking which allows training on multiple datasets together</li><li><strong>Datasets: </strong>Includes support for various datasets built-in including VQA, VizWiz, TextVQA, VisualDialog and COCO Captioning.</li><li><strong>Modules: </strong>Provides implementations for many commonly used layers in vision and language domain</li><li><strong>Distributed:</strong> Support for distributed training based on DataParallel as well as DistributedDataParallel.</li><li><strong>Unopinionated:</strong> Unopinionated about the dataset and model implementations built on top of it.</li><li><strong>Customization:</strong> Custom losses, metrics, scheduling, optimizers, TensorBoard; suits all your custom needs.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uFeEK23hL2moKGlIGrfx0Q.jpeg" /><figcaption>Source :Github: <a href="https://github.com/facebookresearch/pythia">Pythia</a></figcaption></figure><p>With Open Source driving the AI community Pythia can be seen as a step getting closer to democratizing AI which earlier was confined to the walls of Research labs and large companies. This easy to use framework can give birth to exciting projects from independent groups and researchers thus accelerating the progress in this field</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*q0LmeeORCp6GCdL6xE2Vgw.png" /><figcaption><a href="https://www.tensorpad.com/?utm_source=medium&amp;utm_campaign=pythia-p1"><strong>Tensorpad</strong></a></figcaption></figure><h4>[Resources]</h4><ol><li>Github: <a href="https://github.com/facebookresearch/pythia">https://github.com/facebookresearch/pythia</a></li><li>FB research: <a href="https://code.fb.com/ai-research/pythia/">https://code.fb.com/ai-research/pythia/</a></li><li>arXiv paper: <a href="https://arxiv.org/abs/1807.09956">https://arxiv.org/abs/1807.09956</a></li><li>Doc: <a href="https://learnpythia.readthedocs.io/en/latest/">https://learnpythia.readthedocs.io/en/latest/</a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ff1d2a76c6d9" width="1" height="1" alt=""><hr><p><a href="https://medium.com/tensorpad/pythia-facebook-greek-god-doing-deep-learning-ff1d2a76c6d9">Pythia (Facebook)— Greek god doing Deep learning</a> was originally published in <a href="https://medium.com/tensorpad">Tensorpad</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Building an Animal Classifier with Tensorflow]]></title>
            <link>https://medium.com/tensorpad/building-an-animal-classifier-with-tensorflow-ad5931b04946?source=rss----78c19b7d9ce5---4</link>
            <guid isPermaLink="false">https://medium.com/p/ad5931b04946</guid>
            <category><![CDATA[tensorboard]]></category>
            <category><![CDATA[tensorflow]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Peter Chau]]></dc:creator>
            <pubDate>Thu, 10 Jan 2019 09:57:31 GMT</pubDate>
            <atom:updated>2019-09-17T18:52:18.292Z</atom:updated>
            <content:encoded><![CDATA[<h3>Is Your Dog an Elephant? Building an Animal Classifier with TensorFlow</h3><h3>Overview</h3><p>Have you ever wanted to try machine learning? If so, this is the tutorial for you. You’ll learn to build an image classifier and train it using Cloud GPUs. What is an image classifier? It is a machine learning model which can classify an image. You’ll show it an image, and it’ll tell you what it thinks it is. Although our model won’t be as accurate as state-of-the-art models, it is a good starting point.</p><h3>Objective</h3><p>Our objective is to build an image classifier then use it to make predictions. This process includes the following phases:</p><ol><li>Building an image dataset (we’ve done this for you)</li><li>Loading the image dataset in a way that is for efficient training</li><li>Building the image classifier model</li><li>Training the model using Cloud GPUs</li><li>Validating the trained model</li></ol><p>It is important to remember that the process above is not linear. Generally, each phase will need to be revisited multiple times to optimize the model’s accuracy.</p><p>Additionally, we will also make predictions using the trained model, and export it for use elsewhere.</p><h3>Getting started</h3><p>Follow these steps to try this code:</p><ol><li>Open JupyterLabwith pre-installed TensorFlow 1.11.</li><li>Open a Terminal in the opened lab.</li><li>Clone this project’s <a href="https://github.com/PeterChauYEG/animal_classifier">Github repository</a> with: $ git clone <a href="https://github.com/PeterChauYEG/animal_classifier.git">https://github.com/PeterChauYEG/animal_classifier.git</a></li><li>Open Animal Classifier.pynb inside the animal_classifier directory. This will open the Jupyter notebook.</li><li>Run the whole notebook by using the Run menu and selecting Run All Cells</li><li>Scroll down to see training in action. The first part of training occurs at cell 20.</li></ol><h3>Loading Dependencies</h3><p>We need to import a number of Python dependencies.</p><pre># Allows division to return a float<br>from __future__ import division</pre><pre># Allows access to the file system<br>import os</pre><pre># Provides an API for scientific computing<br>import numpy as np</pre><pre># Allows use to timestamp the training run<br>from datetime import datetime</pre><pre># Allows us to render images and plot data<br>from keras.preprocessing.image import ImageDataGenerator, img_to_array, load_img<br>import math<br>import matplotlib.pyplot as plt</pre><pre># Machine learning framework that provides an abstract API on top of Tensorflow<br>import keras<br>from keras.callbacks import TensorBoard<br>from keras.layers import Conv2D, Dense, Flatten, MaxPooling2D<br>from keras.models import Sequential<br>from keras import optimizers</pre><h3>Configurations</h3><p>Because we are plotting in a Jupyter Lab, we need to configure matplotlib to render plots inline.</p><pre># configure the matplotlib for Jupyter Lab used for rendering the images<br>%matplotlib inline</pre><h4>Dataset Directories</h4><p>The dataset holds all the images. A separate directory should be created for each of the following:</p><ol><li>Training Dataset: Images which are used during model training</li><li>Validation Dataset: Images which are used during model validation</li></ol><p>Each image dataset should be organized as directories of images. They should be named by the class (eg. cat) of images it holds. It is important that none of the images in the training dataset is in the validation dataset.</p><pre># Paths to datasets to be used<br>train_dir = &#39;dataset/train&#39;<br>validate_dir = &#39;dataset/validate&#39;</pre><h4>Hyperparameters</h4><p>Hyperparameters are used to tune the model and model training. They greatly influence the resulting metrics. Let’s examine them:</p><ol><li>Images will be resized to 200x200x3. This means each image will be 200 pixels by 200 pixels with 3 color channels (red, green, blue). More pixels tends to help the model as it can increase details of the image.</li><li>The learning rate is the rate which the model will update the gradients which it is trying to optimize.</li><li>The batch size is the number of images that will be feed into the model in one iteration.</li><li>The epoch is the number of times the model should iterate over the entire dataset and update the weights of the model. At some number of epochs, the gains of training approach 0. It is possible to overtrain a model.</li><li>It is often recommended to split the dataset in an 80:20 ratio. This is a general rule that works reasonably well.</li></ol><pre># number of images in the training dataset<br>n_train = 8000</pre><pre># number of images in the validation dataset<br>n_validation = 2000</pre><pre># the number of pixels for the width and height of the image<br>image_dim = 200</pre><pre># the size of the image (h,w,c)<br>input_shape = (image_dim, image_dim, 3)</pre><pre># the rate which the model learns<br>learning_rate = 0.001</pre><pre># size of each mini-batch<br>batch_size = 32</pre><pre># nunmber of training episodes<br>epochs = 10</pre><h4>Outputs</h4><p>We will output 2 items:</p><ol><li>Training logs: These can be feed into Tensorboard for analysis</li><li>Trained model: So it can be used elsewhere</li></ol><p>We want to save the training logs to a directory with a timestamp of when training started, and some data about the hyperparameters used. We also want to give the trained model a name when we save it.</p><pre># directory which we will save training outputs to<br># add a timestamp so that tensorboard show each training session as a different run<br>timestamp = datetime.now().strftime(&#39;%Y-%m-%d %H:%M:%S&#39;)<br>output_logs_dir = &#39;logs/&#39; + timestamp + &#39;-&#39; + str(batch_size) + &#39;-&#39; + str(epochs)</pre><pre># directory to save the model<br>model_name = &#39;trained_model&#39;</pre><h3>Loading the image dataset in a way that is for efficient training</h3><h4>Image Data Generators</h4><p>A naive approach to data loading is to load all the images and transform them up front. This would result in a huge amount of used RAM before training starts. Your machine might not be able to handle this, which would result in crashing kernels. It can also take a very long time depending on the dataset.</p><p>Instead, we can load and transform images required exactly when we need it. This would be when feeding a batch of images to the model during training.</p><p>Keras provides an optimized method of doing this with the Image Data Generator class. It allows us to load images from a directory efficiently. These generators can also transform the dataset in many other ways to augment it. Explore these optional transformations to help make your model more general, and improve accuracy.</p><pre># define data generators<br>train_data_generator = ImageDataGenerator(rescale=1./255,<br>                                          fill_mode=&#39;nearest&#39;)<br>validation_data_generator = ImageDataGenerator(rescale=1./255,<br>                                               fill_mode=&#39;nearest&#39;)</pre><pre># tell the data generators to use data from the train and validation directories<br>train_generator = train_data_generator.flow_from_directory(train_dir,<br>                                                          target_size=(image_dim, image_dim),<br>                                                          batch_size=batch_size,<br>                                                          class_mode=&#39;categorical&#39;)</pre><pre>validation_generator = validation_data_generator.flow_from_directory(validate_dir,<br>                                                          target_size=(image_dim, image_dim),<br>                                                          batch_size=batch_size,<br>                                                          class_mode=&#39;categorical&#39;)</pre><h4>Get Class Names</h4><p>It is useful to have a dictionary of image classes. We can use this dictionary to make our predictions more human-readable.</p><pre># get a dictionary of class names<br>classes_dictionary = train_generator.class_indices</pre><pre># turn classes dictionary into a list<br>class_keys = list(classes_dictionary.keys())</pre><pre># get the number of classes<br>n_classes = len(class_keys)</pre><h4>Load Image Paths of the Validation Dataset</h4><p>Load the paths for all of the images in the validation dataset. These will be used later when we make predictions.</p><pre># Get the name of each directory in the root directory and store them as an array.<br>classes = get_class_labels(validate_dir)</pre><pre># Get the paths of all the images in the first class directory and store them as a 2d array.<br>image_paths = get_class_images(classes, validate_dir)</pre><h4>Building the image classifier model</h4><p>Our model consists of many layers. Images are passed through the model and a set of numbers are outputted. This set of numbers describe the probability of class the image is. We take the largest of these numbers as the most likely class.</p><p>We will use several types of layers and activations:</p><ol><li>Conv2D is a 2-dimensional convolutional layer. It applies filters over the inputted image. This helps the model learn about spatial relationships in the image.</li><li>ReLu is a type of non-linear activation function. It helps the model understand which neurons are activating.</li><li>MaxPooling2D downsamples its input. We use It to reduce the dimensionality of input. This creates a more abstract form of the input.</li><li>Flatten will turn a matrix into a row. Like flattening a muffin into a pancake. We use it so that we can feed the output into dense layers.</li><li>Dense is a densely-connected neural network layer.</li><li>Softmax is an activation function. We use it to turn the output numbers into a range of 0 and 1. It will also cause all the outputted numbers to add up to 1. This can be interpreted as the decimal probability of a class.</li></ol><p>Note that the last layer has the same number of neurons as classes. This means that this layer will output 10 numbers, mapping to a class.</p><pre># define the model <br># takes in images, convoles them, flattens them, classifies them<br>model = Sequential([<br>    Conv2D(16, (3, 3), activation=&#39;relu&#39;, padding=&#39;same&#39;, input_shape=input_shape),<br>    Conv2D(16, (3, 3), activation=&#39;relu&#39;, padding=&#39;same&#39;),<br>    MaxPooling2D(pool_size=(2,2), strides=None, padding=&#39;valid&#39;),<br>    Conv2D(32, (3, 3), activation=&#39;relu&#39;, padding=&#39;same&#39;),<br>    Conv2D(32, (3, 3), activation=&#39;relu&#39;, padding=&#39;same&#39;),<br>    MaxPooling2D(pool_size=(2,2), strides=None, padding=&#39;valid&#39;),<br>    Conv2D(64, (3, 3), activation=&#39;relu&#39;, padding=&#39;same&#39;),<br>    Conv2D(64, (3, 3), activation=&#39;relu&#39;, padding=&#39;same&#39;),<br>    MaxPooling2D(pool_size=(2,2), strides=None, padding=&#39;valid&#39;),<br>    Conv2D(128, (3, 3), activation=&#39;relu&#39;, padding=&#39;same&#39;),<br>    Conv2D(128, (3, 3), activation=&#39;relu&#39;, padding=&#39;same&#39;),<br>    MaxPooling2D(pool_size=(2,2), strides=None, padding=&#39;valid&#39;),<br>    Flatten(),<br>    Dense(256, activation=&#39;relu&#39;),<br>    Dense(n_classes, activation=&#39;softmax&#39;)<br>])</pre><pre># define the optimizer and loss to use<br>model.compile(optimizer=optimizers.SGD(lr=learning_rate, momentum=0.9),<br>              loss=&#39;categorical_crossentropy&#39;,<br>              metrics=[&#39;accuracy&#39;])</pre><h4>Examine the Model</h4><p>We can generate a high-level overview of the model structure. Each row is a layer of the model.</p><pre># look at the defined model<br>model.summary()</pre><h4>Model Structure</h4><pre>_________________________________________________________________ Layer (type)                 Output Shape              Param #    ================================================================= conv2d_1 (Conv2D)            (None, 200, 200, 16)      448        _________________________________________________________________ conv2d_2 (Conv2D)            (None, 200, 200, 16)      2320       _________________________________________________________________ max_pooling2d_1 (MaxPooling2 (None, 100, 100, 16)      0          _________________________________________________________________ conv2d_3 (Conv2D)            (None, 100, 100, 32)      4640       _________________________________________________________________ conv2d_4 (Conv2D)            (None, 100, 100, 32)      9248       _________________________________________________________________ max_pooling2d_2 (MaxPooling2 (None, 50, 50, 32)        0          _________________________________________________________________ conv2d_5 (Conv2D)            (None, 50, 50, 64)        18496      _________________________________________________________________ conv2d_6 (Conv2D)            (None, 50, 50, 64)        36928      _________________________________________________________________ max_pooling2d_3 (MaxPooling2 (None, 25, 25, 64)        0          _________________________________________________________________ conv2d_7 (Conv2D)            (None, 25, 25, 128)       73856      _________________________________________________________________ conv2d_8 (Conv2D)            (None, 25, 25, 128)       147584     _________________________________________________________________ max_pooling2d_4 (MaxPooling2 (None, 12, 12, 128)       0          _________________________________________________________________ flatten_1 (Flatten)          (None, 18432)             0          _________________________________________________________________ dense_1 (Dense)              (None, 256)               4718848    _________________________________________________________________ dense_2 (Dense)              (None, 10)                2570       ================================================================= Total params: 5,014,938 Trainable params: 5,014,938 Non-trainable params: 0 _________________________________________________________________</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/287/1*0t0yuZiqgvd8tBpVaT4l3A.png" /></figure><h4>Examine Model Accuracy Before Training</h4><p>Let’s examine how well the model performs before we train it. We will determine the model’s accuracy on 1 class. This will be done by making predictions with all the images of 1 class. Remember that this isn’t representative of the whole model as it is only 1 class of 10.</p><pre># label of the class we are making predictions on<br>single_class = class_keys[0]</pre><pre># first class image paths <br>single_class_image_paths = image_paths[0]</pre><pre># make predictions on the first class<br>single_class_predictions = predict(int(n_validation / n_classes), single_class_image_paths, model)</pre><pre># get the accuracy of predictions on the first class<br>single_class_accuracy = predictions_accuracy(class_keys, single_class, single_class_predictions)</pre><pre>print(&quot;Current accuracy of model for class &quot; + single_class + &quot;: &quot; + str(single_class_accuracy))</pre><h4>Training the model using Cloud GPUs</h4><p>This model has over 5000000 trainable parameter — far too many to set manually. We need to train the model with the training dataset so that the model can to learn the optimal weights that should be used. These weights are the parameter values of the model.</p><pre># log information for use with tensorboard<br>tensorboard = TensorBoard(log_dir=output_logs_dir)</pre><pre># train the model using the training data generator<br>model.fit_generator(train_generator,<br>                    steps_per_epoch=math.floor(n_train/batch_size),<br>                    validation_data=validation_generator,<br>                    validation_steps=n_validation,<br>                    epochs=epochs,<br>                    callbacks=[tensorboard])</pre><h4>Examine Model Accuracy After Some Training</h4><p>Let’s examine how well the model performs now that we’ve trained it a bit. Again, we will determine the model’s accuracy on 1 class.</p><pre># make predictions on the first class<br>single_class_predictions = predict(int(n_train / n_classes), single_class_image_paths, model)</pre><pre># get the accuracy of predictions on the first class<br>single_class_accuracy = predictions_accuracy(class_keys, single_class, single_class_predictions)</pre><pre>print(&quot;Current accuracy of model for class &quot; + single_class + &quot;: &quot; + str(single_class_accuracy))</pre><h4>Continue Training the Model</h4><p>Let’s continue training the model.</p><pre># train the model using the training data generator<br>model.fit_generator(train_generator,<br>                    steps_per_epoch=math.floor(n_train/batch_size),<br>                    validation_data=validation_generator,<br>                    validation_steps=n_validation,<br>                    epochs=epochs,<br>                    callbacks=[tensorboard])</pre><h4>Examine Model Accuracy After Training</h4><p>Now that we’ve completed training the model, let’s examine its accuracy on 1 class.</p><pre># make predictions on the first class<br>single_class_predictions = predict(int(n_train / n_classes), single_class_image_paths, model)</pre><pre># get the accuracy of predictions on the first class<br>single_class_accuracy = predictions_accuracy(class_keys, single_class, single_class_predictions)</pre><pre>print(&quot;Current accuracy of model for class &quot; + single_class + &quot;: &quot; + str(single_class_accuracy))</pre><h4>Understanding training metrics</h4><p>Our goal is to maximize validation accuracy while minimizing validation loss. The validation dataset is never used for training. This allows us to measure how well the model performs on images it’s never seen before.</p><p>The training and validation accuracies should be similar at the end of training. If these values aren’t, this could be a sign of overfitting.</p><p>You should see training loss (loss) decrease, training accuracy (acc) increase for the training data.</p><p>You should see validation loss (val_loss) decrease, validation accuracy (val_acc) increase for the validation data.</p><h3>Tensorboard</h3><p><a href="https://www.tensorflow.org/guide/summaries_and_tensorboard">Tensorboard</a> is “a suite of visualization tools called TensorBoard. You can use TensorBoard to visualize your TensorFlow graph, plot quantitative metrics about the execution of your graph, and show additional data like images that pass through it”. This is useful for understanding how models/hyperparameters compare.</p><p>In JupyterLab, you can use the commands tab to create a new Tensorboard.</p><ol><li>Open the commands panel with CTRL + SHIFT + C</li><li>Search for Create a new tensorboard</li><li>Select this option and point it to animal_classifier/logs.</li></ol><p>You’ll be able to visualize the accuracy of your model over epochs. Each training run creates a new set of logs. This appears in Tensorboard as a separate plotted line.</p><p>The following are screenshots of my training results plotted on Tensorboard. Your results should look similar.</p><h4>Training Loss</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pgt17VfJB-nQGXnOeVaAAg.png" /></figure><h4>Training Accuracy</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OShqi_wCKTJVx9kBi33jBg.png" /></figure><h4>Validation Loss</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cNmH1gFGlGZWAIGqDV1_9A.png" /></figure><h4>Validation Accuracy</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AIrBRjUx0dH3gUbf1rHpgQ.png" /></figure><h3>Predict</h3><p>It is useful to know which image predictions were correct and which were wrong. Let’s examine 10 predictions, 1 prediction per class.</p><pre># get 1 image path per class<br>predict_image_paths = [image_path[0] for image_path in image_paths]</pre><pre># Make 1 prediction per class<br>predictions = predict(10, predict_image_paths, model)</pre><pre># plot the image that was predicted<br>plot_prediction(class_keys, predict_image_paths, predictions)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*uYB51mw1h8JoYpSfLySQoA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*IY1AiitQqV3LYaJXl-ub6g.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*c3rqSXK23gVS6jgCxHvAqw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*hJBn-UcTZfj3hMLnj-QwWA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/900/1*mc4ocajY3JdS1N8gAVkvFw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*-mSkKHUnOjgbWJdtkIEA-g.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/929/1*DY97W_86qdjCMQOMf9ZWoQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*pL2YVsyLNn4XSL0WVc0Zdw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*wtKXLISzG7mmqehSb3jPkw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/981/1*tWLSS-zknxbql6hq8qya1A.png" /></figure><h3>Exporting the Trained Model</h3><p>What do we do with a trained model? Export for your applications!</p><p>We can use the train model instead of training it every time we want to use. There are many formats you can export it in. Here, we will export it so that it can be loaded up by this notebook</p><pre># export the model for later<br>model.save(model_name)</pre><h3>Next Steps</h3><p>In my next blog post, we will explore hyperparameter sweeping to the model’s accuracy. We will also explore how we can improve the model’s architecture.</p><h3>Learning Resources</h3><ul><li><a href="https://www.coursera.org/learn/machine-learning">Andrew Ng’s course on Machine Learning</a> is considered a standard. It is often recommended.</li><li><a href="https://www.fast.ai/">Fast.ai</a> provide many excellent courses to learn Machine Learning</li><li><a href="https://keras.io/getting-started/sequential-model-guide/">Keras — Guide to the Sequential Model</a> is a great starter to building models with Keras.</li></ul><h3>References</h3><p><a href="https://keras.io/">Keras</a><br><a href="https://www.kaggle.com/alessiocorrado99/animals10">Animals 10 dataset</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ad5931b04946" width="1" height="1" alt=""><hr><p><a href="https://medium.com/tensorpad/building-an-animal-classifier-with-tensorflow-ad5931b04946">Building an Animal Classifier with Tensorflow</a> was originally published in <a href="https://medium.com/tensorpad">Tensorpad</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>