chuckfinca's picture
Gets notebook code exports working. Prepares app.py for Spaces
f72940a
# AUTOGENERATED! DO NOT EDIT! File to edit: lesson_2.ipynb.
# %% auto 0
__all__ = ['dls', 'labels', 'interface', 'predict']
# %% lesson_2.ipynb 4
from fastai.vision.all import *
from fastai.collab import *
if os.path.isfile('export.pkl'):
learn: Learner = load_learner('export.pkl')
else:
path = untar_data(URLs.PETS)
dls = ImageDataLoaders.from_name_re(path, get_image_files(path/'images'), pat='(.+)_\d+.jpg', item_tfms=Resize(460), batch_tfms=aug_transforms(size=224, min_scale=0.75))
learn = vision_learner(dls, models.resnet50, metrics=accuracy)
learn.fine_tune(1)
learn.path = Path('.')
learn.export()
# %% lesson_2.ipynb 5
dls: DataLoaders = learn.dls
labels = dls.vocab
def predict(image):
image = PILImage.create(image)
prediction, prediction_index, probabilities = learn.predict(image)
return { labels[i]: float(probabilities[i]) for i in range(len(labels))}
# %% lesson_2.ipynb 7
import gradio
interface = gradio.Interface(
fn=predict,
inputs=gradio.Image(height=512, width=512),
outputs=gradio.Label(num_top_classes=3),
title="Dog Breed Classifier",
description="Upload a photo of a dog and we'll tell you the likely breeds",
article="This is Roman, he's a Red Nosed Pitbull (aka. American Pit-Bull Terrier) from New Orleans.\n\nTo try out the classifier, tap on his image to load him into the input box above and then press submit.",
examples=['roman.png']
)
interface.launch(share=True)