|
|
import gradio as gr |
|
|
from fastai.vision.all import PILImage, load_learner |
|
|
categories = ('Cat', 'Dog') |
|
|
|
|
|
interface_title = "FastAI Cat vs. Dog Classifier 🐶" |
|
|
|
|
|
interface_description = """ |
|
|
The model was built on resnet18 and trained on Kaggle's Dogs vs Cats competition. |
|
|
The full code is available here: https://www.kaggle.com/code/sagsan/dogs-vs-cats-fastai |
|
|
|
|
|
To use it, upload an image or select one of the examples below. |
|
|
The output shows model's confidence scores for each category. |
|
|
""" |
|
|
|
|
|
def get_labels(fn): |
|
|
fn_str = str(fn).lower() |
|
|
|
|
|
if 'dog' in fn_str: |
|
|
return 'dog' |
|
|
elif 'cat' in fn_str: |
|
|
return 'cat' |
|
|
else: |
|
|
|
|
|
|
|
|
raise ValueError(f"File must be labeled 'dog' or 'cat', but is not: {fn}") |
|
|
|
|
|
|
|
|
def classify_image(img): |
|
|
pred, idx, probs = learn.predict(img) |
|
|
return dict(zip(categories, map(float, probs))) |
|
|
|
|
|
learn = load_learner("./model.pkl") |
|
|
|
|
|
image = gr.Image(width=192, height=192) |
|
|
label = gr.Label() |
|
|
examples = ["3.jpg", "5.jpg", "6.jpg", "7.jpg", "8.jpg", "10.jpg", "14.jpg", "17.jpg", "23.jpg", "44.jpg"] |
|
|
|
|
|
intf = gr.Interface(fn=classify_image, examples=examples, inputs=image, outputs=label, title=interface_title, description=interface_description) |
|
|
intf.launch(inline=False) |
|
|
|