Spaces:
Runtime error
Runtime error
Daniel Meyer
commited on
Commit
•
11328f2
1
Parent(s):
3973533
launch app
Browse files- app.py +29 -0
- images/img_0.png +0 -0
- requirements.txt +6 -0
- saved_model_files/config.json +34 -0
- saved_model_files/preprocessor_config.json +22 -0
- saved_model_files/pytorch_model.bin +3 -0
app.py
ADDED
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import datasets
|
2 |
+
import torch
|
3 |
+
import gradio as gr
|
4 |
+
from transformers import AutoFeatureExtractor, AutoModelForImageClassification
|
5 |
+
|
6 |
+
dataset = datasets.load_dataset("beans")
|
7 |
+
|
8 |
+
extractor = AutoFeatureExtractor.from_pretrained("saved_model_files")
|
9 |
+
model = AutoModelForImageClassification.from_pretrained("saved_model_files")
|
10 |
+
|
11 |
+
labels = dataset['train'].features['labels'].names
|
12 |
+
|
13 |
+
|
14 |
+
def classify(im: gr.inputs.Image) -> dict:
|
15 |
+
features = extractor(im, return_tensors='pt')
|
16 |
+
logits = model(features["pixel_values"])[-1]
|
17 |
+
probability = torch.nn.functional.softmax(logits, dim=-1)
|
18 |
+
probs = probability[0].detach().numpy()
|
19 |
+
confidences = {label: float(probs[i]) for i, label in enumerate(labels)}
|
20 |
+
return confidences
|
21 |
+
|
22 |
+
|
23 |
+
interface = gr.Interface(fn=classify,
|
24 |
+
inputs="image",
|
25 |
+
outputs="label",
|
26 |
+
title="Bean classifier",
|
27 |
+
description="Web-application that can take in an image of a bean leaf and predict whether it is healthy or diseased",
|
28 |
+
examples = ['./images/img_0.png'])
|
29 |
+
interface.launch(debug=True)
|
images/img_0.png
ADDED
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
datasets==2.12.0
|
2 |
+
gradio==3.28.1
|
3 |
+
gradio_client==0.1.4
|
4 |
+
huggingface-hub==0.14.1
|
5 |
+
torch==2.0.0
|
6 |
+
transformers==4.28.1
|
saved_model_files/config.json
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"_name_or_path": "google/vit-base-patch16-224",
|
3 |
+
"architectures": [
|
4 |
+
"ViTForImageClassification"
|
5 |
+
],
|
6 |
+
"attention_probs_dropout_prob": 0.0,
|
7 |
+
"encoder_stride": 16,
|
8 |
+
"hidden_act": "gelu",
|
9 |
+
"hidden_dropout_prob": 0.0,
|
10 |
+
"hidden_size": 768,
|
11 |
+
"id2label": {
|
12 |
+
"0": "angular_leaf_spot",
|
13 |
+
"1": "bean_rust",
|
14 |
+
"2": "healthy"
|
15 |
+
},
|
16 |
+
"image_size": 224,
|
17 |
+
"initializer_range": 0.02,
|
18 |
+
"intermediate_size": 3072,
|
19 |
+
"label2id": {
|
20 |
+
"angular_leaf_spot": "0",
|
21 |
+
"bean_rust": "1",
|
22 |
+
"healthy": "2"
|
23 |
+
},
|
24 |
+
"layer_norm_eps": 1e-12,
|
25 |
+
"model_type": "vit",
|
26 |
+
"num_attention_heads": 12,
|
27 |
+
"num_channels": 3,
|
28 |
+
"num_hidden_layers": 12,
|
29 |
+
"patch_size": 16,
|
30 |
+
"problem_type": "single_label_classification",
|
31 |
+
"qkv_bias": true,
|
32 |
+
"torch_dtype": "float32",
|
33 |
+
"transformers_version": "4.28.1"
|
34 |
+
}
|
saved_model_files/preprocessor_config.json
ADDED
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"do_normalize": true,
|
3 |
+
"do_rescale": true,
|
4 |
+
"do_resize": true,
|
5 |
+
"image_mean": [
|
6 |
+
0.5,
|
7 |
+
0.5,
|
8 |
+
0.5
|
9 |
+
],
|
10 |
+
"image_processor_type": "ViTImageProcessor",
|
11 |
+
"image_std": [
|
12 |
+
0.5,
|
13 |
+
0.5,
|
14 |
+
0.5
|
15 |
+
],
|
16 |
+
"resample": 2,
|
17 |
+
"rescale_factor": 0.00392156862745098,
|
18 |
+
"size": {
|
19 |
+
"height": 224,
|
20 |
+
"width": 224
|
21 |
+
}
|
22 |
+
}
|
saved_model_files/pytorch_model.bin
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:b9b1703e5b5b9c051f0fa9dae5e5550b9486a69e859faab86177d5b3071aa453
|
3 |
+
size 343271789
|