File size: 1,388 Bytes
0a075d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f5f8fa9
0a075d8
 
9b1e81f
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
34
35
36
37
38
39
import torch
from transformers import ViTImageProcessor, AutoFeatureExtractor, AutoModelForImageClassification
import gradio as gr

image_processor = ViTImageProcessor.from_pretrained("google/vit-base-patch16-224")

extractor = AutoFeatureExtractor.from_pretrained("saved_model_files")
model = AutoModelForImageClassification.from_pretrained("saved_model_files")

labels = ['angular_leaf_spot', 'bean_rust', 'healthy'] 

def classify(image):
  features = image_processor(image, 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)} 
  print(confidences)
  return confidences


theme = gr.themes.Soft(
    primary_hue="green",
    secondary_hue="green",
    neutral_hue="green",
).set(
    block_background_fill_dark='*body_background_fill',
    button_border_width='*block_label_border_width',
    button_border_width_dark='*checkbox_label_border_width'
)

with gr.Blocks(theme=theme) as demo:

    inference = gr.Interface(fn=classify, inputs="image", outputs="label",
                        title="Plant leaves Classification",
                    description="Classify the leaves by uploading image",
                    examples=["images/1.png","images/2.png", "images/3.png"])

demo.launch()