|
import datasets |
|
import transformers |
|
from transformers import AutoFeatureExtractor, AutoModelForImageClassification |
|
import torch |
|
|
|
dataset = datasets.load_dataset('beans') |
|
|
|
extractor = AutoFeatureExtractor.from_pretrained("lucasdmpp/BeanLeaf") |
|
model = AutoModelForImageClassification.from_pretrained("lucasdmpp/BeanLeaf") |
|
|
|
labels = dataset['train'].features['labels'].names |
|
example_imgs = ["example_0.jpg", "example_1.jpg"] |
|
|
|
def classify(im): |
|
features = extractor(im, return_tensors='pt') |
|
logits = model(features["pixel_values"])[-1] |
|
probability = torch.nn.functional.softmax(logits, dim=-1) |
|
probs = probability[0].detach().numpy() |
|
confidences = {label: float(probs[i]) for i, label in enumerate(labels)} |
|
return confidences |
|
|
|
import gradio as gr |
|
|
|
interface = interface = gr.Interface(classify, inputs='image', |
|
outputs='label', |
|
title='Bean Leaf Classification', |
|
description='Check the health of your bean leaves', |
|
examples = example_imgs) |
|
|
|
interface.launch(debug=True) |