summaryrefslogtreecommitdiff
path: root/posts/2019-12-10-TensorFlow-Model-Prediction/index.html
blob: a9a28dd7028f14e7390d84dfd023192f9bf9ae1a (plain)
1
2
3
4
5
6
7
8
9
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"/><meta name="og:site_name" content="Navan Chauhan"/><link rel="canonical" href="https://navanchauhan.github.io/posts/2019-12-10-TensorFlow-Model-Prediction"/><meta name="twitter:url" content="https://navanchauhan.github.io/posts/2019-12-10-TensorFlow-Model-Prediction"/><meta name="og:url" content="https://navanchauhan.github.io/posts/2019-12-10-TensorFlow-Model-Prediction"/><title>Making Predictions using Image Classifier (TensorFlow) | Navan Chauhan</title><meta name="twitter:title" content="Making Predictions using Image Classifier (TensorFlow) | Navan Chauhan"/><meta name="og:title" content="Making Predictions using Image Classifier (TensorFlow) | Navan Chauhan"/><meta name="description" content="Making predictions for image classification models built using TensorFlow"/><meta name="twitter:description" content="Making predictions for image classification models built using TensorFlow"/><meta name="og:description" content="Making predictions for image classification models built using TensorFlow"/><meta name="twitter:card" content="summary"/><link rel="stylesheet" href="/styles.css" type="text/css"/><meta name="viewport" content="width=device-width, initial-scale=1.0"/><link rel="shortcut icon" href="/images/favicon.png" type="image/png"/><link rel="alternate" href="/feed.rss" type="application/rss+xml" title="Subscribe to Navan Chauhan"/><meta name="twitter:image" content="https://navanchauhan.github.io/images/logo.png"/><meta name="og:image" content="https://navanchauhan.github.io/images/logo.png"/></head><head><script>var _paq=window._paq=window._paq||[];_paq.push(['trackPageView']),_paq.push(['enableLinkTracking']),function(){var a='https://navanspi.duckdns.org:6969/analytics/';_paq.push(['setTrackerUrl',a+'matomo.php']),_paq.push(['setSiteId','2']);var e=document,t=e.createElement('script'),p=e.getElementsByTagName('script')[0];t.type='text/javascript',t.async=!0,t.src=a+'matomo.js',p.parentNode.insertBefore(t,p)}();</script></head><head><script src="https://www.googletagmanager.com/gtag/js?id=UA-108635191-1v"></script><script>window.dataLayer = window.dataLayer || [];function gtag(){dataLayer.push(arguments);}gtag('js', new Date());gtag('config', 'UA-108635191-1');</script></head><body class="item-page"><header><div class="wrapper"><a class="site-name" href="/">Navan Chauhan</a><nav><ul><li><a href="/about">About Me</a></li><li><a class="selected" href="/posts">Posts</a></li><li><a href="/publications">Publications</a></li><li><a href="/assets/résumé.pdf">Résumé</a></li><li><a href="https://navanchauhan.github.io/repo">Repo</a></li></ul></nav></div></header><div class="wrapper"><article><div class="content"><span class="reading-time">1 minute read</span><span class="reading-time">Created on December 10, 2019</span><span class="reading-time">Last modified on June 1, 2020</span><h1>Making Predictions using Image Classifier (TensorFlow)</h1><p><em>This was tested on TF 2.x and works as of 2019-12-10</em></p><p>If you want to understand how to make your own custom image classifier, please refer to my previous post.</p><p>If you followed my last post, then you created a model which took an image of dimensions 50x50 as an input.</p><p>First we import the following if we have not imported these before</p><pre><code><div class="highlight"><span></span><span class="kn">import</span> <span class="nn">cv2</span>
<span class="kn">import</span> <span class="nn">os</span>
</div></code></pre><p>Then we read the file using OpenCV.</p><pre><code><div class="highlight"><span></span><span class="n">image</span><span class="o">=</span><span class="n">cv2</span><span class="o">.</span><span class="n">imread</span><span class="p">(</span><span class="n">imagePath</span><span class="p">)</span>
</div></code></pre><p>The cv2. imread() function returns a NumPy array representing the image. Therefore, we need to convert it before we can use it.</p><pre><code><div class="highlight"><span></span><span class="n">image_from_array</span> <span class="o">=</span> <span class="n">Image</span><span class="o">.</span><span class="n">fromarray</span><span class="p">(</span><span class="n">image</span><span class="p">,</span> <span class="s1">&#39;RGB&#39;</span><span class="p">)</span>
</div></code></pre><p>Then we resize the image</p><pre><code><div class="highlight"><span></span><span class="n">size_image</span> <span class="o">=</span> <span class="n">image_from_array</span><span class="o">.</span><span class="n">resize</span><span class="p">((</span><span class="mi">50</span><span class="p">,</span><span class="mi">50</span><span class="p">))</span>
</div></code></pre><p>After this we create a batch consisting of only one image</p><pre><code><div class="highlight"><span></span><span class="n">p</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">expand_dims</span><span class="p">(</span><span class="n">size_image</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
</div></code></pre><p>We then convert this uint8 datatype to a float32 datatype</p><pre><code><div class="highlight"><span></span><span class="n">img</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">cast</span><span class="p">(</span><span class="n">p</span><span class="p">,</span> <span class="n">tf</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span>
</div></code></pre><p>Finally we make the prediction</p><pre><code><div class="highlight"><span></span><span class="nb">print</span><span class="p">([</span><span class="s1">&#39;Infected&#39;</span><span class="p">,</span><span class="s1">&#39;Uninfected&#39;</span><span class="p">][</span><span class="n">np</span><span class="o">.</span><span class="n">argmax</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">predict</span><span class="p">(</span><span class="n">img</span><span class="p">))])</span>
</div></code></pre><p><code>Infected</code></p></div><span>Tagged with: </span><ul class="tag-list"><li><a href="/tags/tutorial">Tutorial</a></li><li><a href="/tags/tensorflow">Tensorflow</a></li><li><a href="/tags/codesnippet">Code-Snippet</a></li></ul></article></div><footer><p>Made with ❤️ using <a href="https://github.com/johnsundell/publish">Publish</a></p><p><a href="/feed.rss">RSS feed</a></p></footer></body></html>