Spaces:
Runtime error
Runtime error
Initial Commit
Browse filescnn_from_scratch_sigmoid
app.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import pickle
|
| 3 |
+
import tensorflow as tf
|
| 4 |
+
import gradio as gr
|
| 5 |
+
import numpy as np
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
#Transforming or Reading an input image and making it prediction ready as per our model
|
| 9 |
+
#filename :- input image path as str
|
| 10 |
+
#img_shape :- input image shape except color channel as List i.e here [128,128]
|
| 11 |
+
def transform_image_for_prediction(filename,img_shape=[256,256]):
|
| 12 |
+
"""
|
| 13 |
+
Reads an image from filename, turns it into a tensor
|
| 14 |
+
and reshapes it to (img_shape, img_shape, colour_channel).
|
| 15 |
+
"""
|
| 16 |
+
# Read in target file (an image)
|
| 17 |
+
img = tf.io.read_file(filename)
|
| 18 |
+
|
| 19 |
+
# Decode the read file (array) into a tensor & ensure 3 colour channels
|
| 20 |
+
# (our model is trained on images with 3 colour channels and sometimes images have 4 colour channels)
|
| 21 |
+
img = tf.image.decode_image(img, channels=3)
|
| 22 |
+
|
| 23 |
+
# Resize the image (to the same size our model was trained on)
|
| 24 |
+
img = tf.image.resize(img,size=img_shape)
|
| 25 |
+
|
| 26 |
+
#Applying Feature scaling as we done it /255. in our training and test data
|
| 27 |
+
img = img/255. #dot at end of 255 so to convert integer into float value
|
| 28 |
+
|
| 29 |
+
return img
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
def make_prediction(img):
|
| 33 |
+
cnn_model = pickle.load(open("E:\\Machine Learning\\Projects\\meWhoObserves\\model.pkl","rb"))
|
| 34 |
+
|
| 35 |
+
img_tf = transform_image_for_prediction(img)
|
| 36 |
+
pred_prob = cnn_model.predict(tf.expand_dims(img_tf, axis=0)) #adding fake dimension which represnts batchno
|
| 37 |
+
class_names = ['crocodile', 'logs'] #as define in .ipynb file
|
| 38 |
+
if len(pred_prob[0]) > 1: # prediction for multi-class
|
| 39 |
+
pred_class = class_names[pred_prob.argmax()] # if more than one output, take the max
|
| 40 |
+
else: #prediction for binary class
|
| 41 |
+
if (pred_prob[0][0] >=0.60):
|
| 42 |
+
i=1
|
| 43 |
+
else:
|
| 44 |
+
i=0
|
| 45 |
+
pred_class = class_names[i] # if only one output, round
|
| 46 |
+
|
| 47 |
+
result = f"Prediction of being crocodile: {np.round(((1-pred_prob[0][0])*100),2)}%" + "\n" + f"Prediction of being driftwood: {np.round(((pred_prob[0][0])*100),2)}%"
|
| 48 |
+
prediction = f"Image belongs to the {pred_class} class"
|
| 49 |
+
result_for_slider = {"Crocodile": float(1-pred_prob[0][0]), "driftwood": float(pred_prob[0][0])}
|
| 50 |
+
|
| 51 |
+
return prediction,result,result_for_slider
|
| 52 |
+
|
| 53 |
+
inputs = gr.Image(type='filepath',label="Feed me some image!") #interactive=True,
|
| 54 |
+
outputs=[
|
| 55 |
+
gr.Textbox(label="Croc-O-Net Classified Class:"),
|
| 56 |
+
gr.Textbox(label="Croc-O-Net Predicting Probabilities"),
|
| 57 |
+
gr.Label("Slider prob")
|
| 58 |
+
]
|
| 59 |
+
|
| 60 |
+
app = gr.Interface(fn=make_prediction,inputs=inputs,outputs=outputs,title="Welcome to the Croc-O-Net Classifier ๐๐",
|
| 61 |
+
article="<p style='text-align: center'><a href='https://medium.com/@p3pioneer22/croc-o-net-the-only-cnn-project-you-need-to-get-a-job-d57b86ac8fac' target='_blank'>Blog post</a></p>")
|
| 62 |
+
app.launch()
|