diff options
author | navanchauhan <navanchauhan@gmail.com> | 2022-11-07 23:36:11 -0700 |
---|---|---|
committer | navanchauhan <navanchauhan@gmail.com> | 2022-11-07 23:36:11 -0700 |
commit | d75527f7eecc4e2fcdd18ab157412506717c8adb (patch) | |
tree | 8a96e3036d59030f5654725edb1ca5ad6db4cb4e /docs/posts/2019-12-10-TensorFlow-Model-Prediction.html | |
parent | 8ca94ab784138ef673bc7c1691b99e2d4d69e015 (diff) |
add blog post
Diffstat (limited to 'docs/posts/2019-12-10-TensorFlow-Model-Prediction.html')
-rw-r--r-- | docs/posts/2019-12-10-TensorFlow-Model-Prediction.html | 42 |
1 files changed, 28 insertions, 14 deletions
diff --git a/docs/posts/2019-12-10-TensorFlow-Model-Prediction.html b/docs/posts/2019-12-10-TensorFlow-Model-Prediction.html index 7187fe8..97ad373 100644 --- a/docs/posts/2019-12-10-TensorFlow-Model-Prediction.html +++ b/docs/posts/2019-12-10-TensorFlow-Model-Prediction.html @@ -51,39 +51,53 @@ <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> +<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> +</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> +<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">'RGB'</span><span class="p">)</span> -</code></pre></div> +<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">'RGB'</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> +<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> +<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> +<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">'Infected'</span><span class="p">,</span><span class="s1">'Uninfected'</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> +<div class="codehilite"> +<pre><span></span><code><span class="nb">print</span><span class="p">([</span><span class="s1">'Infected'</span><span class="p">,</span><span class="s1">'Uninfected'</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> |