summaryrefslogtreecommitdiff
path: root/Content/posts/2019-12-10-TensorFlow-Model-Prediction.md
diff options
context:
space:
mode:
authorNavan Chauhan <navanchauhan@gmail.com>2021-05-26 23:58:29 +0530
committerNavan Chauhan <navanchauhan@gmail.com>2021-05-26 23:58:29 +0530
commitbfd3a825c2d73bd842769cdfaf11ad0319a3bd6e (patch)
tree7b2c052bdf539f433ed3ab6bd133b6d46c7ff7e5 /Content/posts/2019-12-10-TensorFlow-Model-Prediction.md
parent2cb28c0dd749611e6edd4688955769bda3381453 (diff)
added code and content
Diffstat (limited to 'Content/posts/2019-12-10-TensorFlow-Model-Prediction.md')
-rw-r--r--Content/posts/2019-12-10-TensorFlow-Model-Prediction.md60
1 files changed, 60 insertions, 0 deletions
diff --git a/Content/posts/2019-12-10-TensorFlow-Model-Prediction.md b/Content/posts/2019-12-10-TensorFlow-Model-Prediction.md
new file mode 100644
index 0000000..cafa026
--- /dev/null
+++ b/Content/posts/2019-12-10-TensorFlow-Model-Prediction.md
@@ -0,0 +1,60 @@
+---
+date: 2019-12-10 11:10
+description: Making predictions for image classification models built using TensorFlow
+tags: Tutorial, Tensorflow, Code-Snippet
+---
+
+# Making Predictions using Image Classifier (TensorFlow)
+
+*This was tested on TF 2.x and works as of 2019-12-10*
+
+If you want to understand how to make your own custom image classifier, please refer to my previous post.
+
+If you followed my last post, then you created a model which took an image of dimensions 50x50 as an input.
+
+First we import the following if we have not imported these before
+
+```python
+import cv2
+import os
+```
+
+Then we read the file using OpenCV.
+
+```python
+image=cv2.imread(imagePath)
+```
+
+The cv2. imread() function returns a NumPy array representing the image. Therefore, we need to convert it before we can use it.
+
+```python
+image_from_array = Image.fromarray(image, 'RGB')
+```
+
+Then we resize the image
+
+```python
+size_image = image_from_array.resize((50,50))
+```
+
+After this we create a batch consisting of only one image
+
+```python
+p = np.expand_dims(size_image, 0)
+```
+
+We then convert this uint8 datatype to a float32 datatype
+
+```python
+img = tf.cast(p, tf.float32)
+```
+
+Finally we make the prediction
+
+```python
+print(['Infected','Uninfected'][np.argmax(model.predict(img))])
+```
+
+`Infected`
+
+