|
|
|
"""app.ipynb |
|
|
|
Automatically generated by Colaboratory. |
|
|
|
Original file is located at |
|
https://colab.research.google.com/drive/1F6tnDcfVlChPXwu4UwSHq-bggJlcmyj- |
|
|
|
# This notebook makes predictions based on saved model. |
|
|
|
#Import Libraries |
|
""" |
|
|
|
|
|
import sys |
|
import subprocess |
|
|
|
|
|
subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'fastai']) |
|
subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'fastbook']) |
|
subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'gradio']) |
|
subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'nbdev']) |
|
|
|
reqs = subprocess.check_output([sys.executable, '-m', 'pip', 'freeze']) |
|
installed_packages = [r.decode().split('==')[0] for r in reqs.split()] |
|
print(installed_packages) |
|
|
|
''' |
|
#|export |
|
!pip install -Uqq fastai |
|
!pip install -Uqq fastbook |
|
!pip install -Uqq gradio |
|
!pip install -Uqq nbdev |
|
''' |
|
"""#Dogs Vs Cats""" |
|
|
|
|
|
from fastai.vision.all import * |
|
import gradio as gr |
|
|
|
|
|
def is_cat(x): return x[0].isupper() |
|
|
|
''' |
|
im = PILImage.create('/content/dog.JPG') |
|
im.thumbnail((192,192)) |
|
im |
|
''' |
|
|
|
|
|
learn = load_learner('/content/model.pkl') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"""#Now create a GRADIO Interface that has this information |
|
|
|
Gradio requires us to give it a function that it is going to call. Our's function is `classify_image(img)` |
|
|
|
learn.predict(img) returns 3 things : |
|
pred --> Prediction as a String |
|
idx --> Its index |
|
probs --> Probability that the image is a Cat ? |
|
|
|
Gradio wants to get back a dictionary containing each of the posible categories -- which is this case is a DOg or a Cat -- and the probability of each one.. |
|
|
|
""" |
|
|
|
|
|
categories = ('Dog','Cat') |
|
|
|
def classify_image(img): |
|
pred,idx,probs = learn.predict(img) |
|
return dict(zip(categories,map(float,probs))) |
|
|
|
|
|
|
|
"""#Now creating our GRADIO Interface""" |
|
|
|
|
|
image = gr.Image(height=192,width=192) |
|
label = "label" |
|
examples = ['/content/dog.JPG','/content/cat.JPG','/content/dunno.JPG'] |
|
intf = gr.Interface(fn = classify_image,inputs=image,outputs=label,examples=examples) |
|
intf.launch(inline=False) |
|
|
|
"""#Export the notebook as Python script""" |
|
''' |
|
!pip install -Uqq nbdev |
|
|
|
#from nbdev.export import notebook2script |
|
|
|
from nbdev import nbdev_export |
|
|
|
!pip install -Uqq nbconvert |
|
|
|
!jupyter nbconvert app.ipynb --to python |
|
''' |