File size: 1,216 Bytes
eb39a69
ede35d1
 
 
 
 
 
83a539d
ede35d1
 
 
 
 
 
7f96e03
ede35d1
 
 
 
 
 
 
2e576c3
bb65d6b
a7166c0
99c4e70
ede35d1
 
 
 
 
2f50f13
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
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/healthy_test.15.jpg", "examples/angular_leaf_spot_test.0.jpg", "examples/bean_rust_test.32.jpg"],
    cache_examples=False,
    fn=classify,
    inputs=gr.Image(shape=(224, 224)),
    outputs=gr.Label(num_top_classes=3),
)

interface.launch(debug=True)