File size: 1,435 Bytes
fcdb587 | 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 | # -*- coding: utf-8 -*-
"""Copy of dogs_cats.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1pu75-TcRCtcDPFHn3zA6IqSsH8BM5yka
## Gradio Pets
# New Section
"""
# !pip install -Uqq fastai
#/ default_exp app
from fastai.vision.all import *
import gradio as gr
def is_cat(x): return x[0].isupper()
# path = untar_data(URLs.PETS)/'images'
# dls = ImageDataLoaders.from_name_func('.',
# get_image_files(path), valid_pct=0.2, seed=42, #giving
# label_func=is_cat,
# item_tfms=Resize(192))
# dls.show_batch()
# learn = vision_learner(dls, resnet18, metrics=error_rate)
# learn.fine_tune(3)
# learn.export('model.pkl')
# cell
learn= load_learner('model.pk1')
categories= ('Dog', 'Cat')
# # function we need to define for gradio
# # predcition is a string and prob ius a
# # grradio wants dict with each category and prob of each
# # zip -- putting together .... dict-- putting into correct format
# # gradio doesnt work with tensors thus need to convert to float
def classify_image(img):
pred,idx,probs= learn.predict(img)
return dict(zip(categories, map(float, probs)))
im=PILImage.create('dog.jpg')
im.thumbnail((192,192))
im
learn.predict(im)
classify_image(im)
examples= ['dog.jpg', 'cat.jpg']
gr.Interface(fn=classify_image, inputs=[gr.Image(type="pil")], outputs=[gr.Label(num_top_classes=2)], examples=examples).launch() |