File size: 1,478 Bytes
f72940a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# 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)