Spaces:
Runtime error
Runtime error
changed app.py
Browse files
app.py
CHANGED
@@ -1,35 +1,27 @@
|
|
1 |
-
import
|
2 |
import gradio as gr
|
3 |
-
import datasets
|
4 |
import torch
|
5 |
from transformers import AutoFeatureExtractor, AutoModelForImageClassification
|
6 |
-
from transformers import ViTFeatureExtractor, ViTForImageClassification
|
7 |
-
from datasets import load_dataset
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
dataset = load_dataset('beans', 'full_size')
|
12 |
|
|
|
|
|
13 |
extractor = AutoFeatureExtractor.from_pretrained("saved_model_files")
|
14 |
model = AutoModelForImageClassification.from_pretrained("saved_model_files")
|
15 |
|
16 |
-
labels = dataset[
|
17 |
-
|
18 |
-
def classify(im):
|
19 |
-
features = feature_extractor(im, return_tensors='pt')
|
20 |
-
logits = model(features["pixel_values"])[-1]
|
21 |
-
probability = torch.nn.functional.softmax(logits, dim=-1)
|
22 |
-
probs = probability[0].detach().numpy()
|
23 |
-
confidences = {label: float(probs[i]) for i, label in enumerate(labels)}
|
24 |
-
return confidences
|
25 |
|
26 |
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
|
|
|
|
|
|
31 |
|
32 |
-
gr_interface = gr.Interface(classify, inputs='image', outputs='label', title='Bean Classification', description='Monitor your crops health in easier way')
|
33 |
|
|
|
|
|
|
|
34 |
|
35 |
-
|
|
|
1 |
+
from datasets import load_dataset
|
2 |
import gradio as gr
|
|
|
3 |
import torch
|
4 |
from transformers import AutoFeatureExtractor, AutoModelForImageClassification
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
+
# Define dataset, feature extractor, and model
|
7 |
+
dataset = load_dataset("beans")
|
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):
|
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(
|
24 |
+
fn=classify, inputs="image", outputs="label", title="Bean leaf classification"
|
25 |
+
)
|
26 |
|
27 |
+
interface.launch(debug=True)
|