|
import datasets |
|
from datasets import load_dataset |
|
|
|
import torch |
|
import gradio as gr |
|
from transformers import AutoFeatureExtractor, AutoModelForImageClassification |
|
|
|
dataset = load_dataset("beans") |
|
|
|
extractor = AutoFeatureExtractor.from_pretrained("saved_model_files") |
|
model = AutoModelForImageClassification.from_pretrained("saved_model_files") |
|
|
|
labels = dataset['train'].features['labels'].names |
|
|
|
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 |
|
|
|
interface = gr.Interface( |
|
fn=classify, inputs="image", |
|
outputs="label", |
|
title="Bean Leaf Classifier", |
|
description="An app to help us test our bean leaf classifier in the real world!", |
|
examples=['bean-plant-example.jpeg', 'non-bean-leaf-example.jpeg']) |
|
interface.launch(debug=False) |