File size: 2,015 Bytes
20b9fa6
f777747
20b9fa6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b5c9402
20b9fa6
 
 
 
 
 
 
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60

from  datasets import load_dataset
import gradio as gr
import torch
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 = image_processor(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


title = """<h1 id="title">Bean plant health predictor through images of leaves using ViT image classifier</h1>"""

description = """
Use Case: A farming company that is having issues with diseases affecting their bean plants. The farmers have to constantly monitor the leaves of the plants so that they can immediately treat the leaves if they show any signs of disease. 
We are asked to build a machine learning-based app they can deploy on a drone to quickly identify diseased plants.


Solution: Building a Leaf Classification App that focuses on image classification to quickly identify diseased plants.

- The Dataset used for finetuning the model [Beans](https://huggingface.co/datasets/beans). 
- The model used for classifying the images [Vision Transformer (base-sized model)](https://huggingface.co/google/vit-base-patch16-224).
"""

css = '''
h1#title {
  text-align: center;
}
'''
theme = gr.themes.Soft()
demo = gr.Blocks(css=css, theme=theme)

with demo:
  gr.Markdown(title)
  gr.Markdown(description)


  interface =  gr.Interface(fn=classify, inputs="image", outputs="label")

  gr.Markdown("## Example Images")
  gr.Examples(["images/1.jpeg","images/2.jpeg", "images/3.jpg"],
        inputs="image", 
        outputs="label",
        fn=classify,
        cache_examples=True
  )

demo.launch()