neerajprad's picture
Colab update - fix classify
99c4e70
raw
history blame
1.12 kB
import torch
from datasets import load_dataset
import gradio as gr
from transformers import AutoFeatureExtractor, AutoModelForImageClassification
# This should be the same as the first line of Python code in this Colab notebook
dataset = load_dataset('beans')
feature_extractor = AutoFeatureExtractor.from_pretrained("saved_model_files")
model = AutoModelForImageClassification.from_pretrained("saved_model_files")
labels = dataset['train'].features['labels'].names
def classify(im):
features = feature_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(
title="Leaf Spot Classifier",
description="Classify the leaf into one of: angular_leaf_spot, bean_rust, healthy",
examples="examples",
cache_examples=False,
fn=classify,
inputs=gr.Image(shape=(224, 224)),
outputs=gr.Label(num_top_classes=3),
)
interface.launch(debug=True)