summaryrefslogtreecommitdiff
path: root/docs/posts/2019-12-10-TensorFlow-Model-Prediction.html
blob: 317af8571524e5d06b3677a169da7eff72013c43 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<!DOCTYPE html>
<html lang="en">
<head>
    
    <link rel="stylesheet" href="https://unpkg.com/latex.css/style.min.css" />
    <link rel="stylesheet" href="/assets/main.css" />
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Making Predictions using Image Classifier (TensorFlow)</title>
    <meta name="og:site_name" content="Navan Chauhan" />
    <link rel="canonical" href="https://web.navan.dev/posts/2019-12-10-TensorFlow-Model-Prediction.html" />
    <meta name="twitter:url" content="https://web.navan.dev/posts/2019-12-10-TensorFlow-Model-Prediction.html />
    <meta name="og:url" content="https://web.navan.dev/posts/2019-12-10-TensorFlow-Model-Prediction.html" />
    <meta name="twitter:title" content="Making Predictions using Image Classifier (TensorFlow)" />
    <meta name="og:title" content="Making Predictions using Image Classifier (TensorFlow)" />
    <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_large_image" />
    <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://web.navan.dev/images/opengraph/posts/2019-12-10-TensorFlow-Model-Prediction.png" />
    <meta name="og:image" content="https://web.navan.dev/images/opengraph/posts/2019-12-10-TensorFlow-Model-Prediction.png" />
    <meta name="google-site-verification" content="LVeSZxz-QskhbEjHxOi7-BM5dDxTg53x2TwrjFxfL0k" />
    <script data-goatcounter="https://navanchauhan.goatcounter.com/count"
        async src="//gc.zgo.at/count.js"></script>
    <script defer data-domain="web.navan.dev" src="https://plausible.io/js/plausible.js"></script>
    <link rel="manifest" href="/manifest.json" />
    
</head>
<body>
    <center><nav style="display: block;">
|
<a href="/">home</a> |
<a href="/about/">about/links</a> |
<a href="/posts/">posts</a> |
<a href="/3D-Designs/">3D designs</a> |
<!--<a href="/publications/">publications</a> |-->
<!--<a href="/repo/">iOS repo</a> |-->
<a href="/feed.rss">RSS Feed</a> |
</nav>
</center>
    
<main>

	<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>

<div class="codehilite">
<pre><span></span><code><span class="kn">import</span> <span class="nn">cv2</span>
<span class="kn">import</span> <span class="nn">os</span>
</code></pre>
</div>

<p>Then we read the file using OpenCV.</p>

<div class="codehilite">
<pre><span></span><code><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>
</code></pre>
</div>

<p>The cv2. imread() function returns a NumPy array representing the image. Therefore, we need to convert it before we can use it.</p>

<div class="codehilite">
<pre><span></span><code><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>
</code></pre>
</div>

<p>Then we resize the image</p>

<div class="codehilite">
<pre><span></span><code><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>
</code></pre>
</div>

<p>After this we create a batch consisting of only one image</p>

<div class="codehilite">
<pre><span></span><code><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>
</code></pre>
</div>

<p>We then convert this uint8 datatype to a float32 datatype</p>

<div class="codehilite">
<pre><span></span><code><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>
</code></pre>
</div>

<p>Finally we make the prediction</p>

<div class="codehilite">
<pre><span></span><code><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>
</code></pre>
</div>

<p><code>Infected</code></p>

	<blockquote>If you have scrolled this far, consider subscribing to my mailing list <a href="https://listmonk.navan.dev/subscription/form">here.</a> You can subscribe to either a specific type of post you are interested in, or subscribe to everything with the "Everything" list.</blockquote>
	<script data-isso="https://comments.navan.dev/"
        src="https://comments.navan.dev/js/embed.min.js"></script>
	<section id="isso-thread">
	    <noscript>Javascript needs to be activated to view comments.</noscript>
	</section>
</main>

    <script src="assets/manup.min.js"></script>
    <script src="/pwabuilder-sw-register.js"></script>    
</body>
</html>