File size: 1,045 Bytes
3cecdc7
 
 
1872f6e
 
 
3cecdc7
 
 
 
 
 
 
 
fc5989d
3b7b8ef
 
3cecdc7
 
 
 
 
 
 
 
5bf5cf7
3cecdc7
55bead6
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
import datasets
from transformers import AutoFeatureExtractor, AutoModelForImageClassification
import gradio as gr
import torch
import transformers


dataset = datasets.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')
  with torch.no_grad():
    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(classify, inputs='image', outputs='label', title='Leaf classification on beans dataset',
                         description='Sample fine-tuning a ViT for leaf classification. Upload a picture of a leaf to see if it is healthy, has angular leaf spots or bean rust.')

interface.launch()