Spaces:
Sleeping
Sleeping
Commit ·
f4b8744
1
Parent(s): b1cdb15
Classifier model change
Browse files- app.py +46 -15
- requirements.txt +1 -2
app.py
CHANGED
|
@@ -1,24 +1,41 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import random
|
| 3 |
import torch
|
| 4 |
-
from transformers import
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
)
|
| 10 |
# ------------------ LOAD CLASSIFIER ------------------
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
def classify_image(image):
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
results = {}
|
| 18 |
-
for item in preds[:3]:
|
| 19 |
-
label = item["label"]
|
| 20 |
-
score = float(item["score"])
|
| 21 |
-
results[label] = score
|
| 22 |
return results
|
| 23 |
|
| 24 |
|
|
@@ -204,6 +221,20 @@ waste_analyzation_v3 = {
|
|
| 204 |
}
|
| 205 |
analysis = [waste_analyzation_v3, waste_analyzation_v2, waste_analyzation]
|
| 206 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
def classify_pipeline(image):
|
| 208 |
global top_label
|
| 209 |
|
|
@@ -217,7 +248,7 @@ def analyze_pipeline():
|
|
| 217 |
if top_label is None:
|
| 218 |
return "Please classify an image first."
|
| 219 |
|
| 220 |
-
explanation = explain_recycling(top_label)
|
| 221 |
|
| 222 |
choice = random.randint(0, 2)
|
| 223 |
info = analysis[choice][top_label]
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import random
|
| 3 |
import torch
|
| 4 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
| 5 |
+
from transformers import CLIPProcessor, CLIPModel
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
|
|
|
|
| 9 |
# ------------------ LOAD CLASSIFIER ------------------
|
| 10 |
+
clip_model_name = "openai/clip-vit-base-patch32"
|
| 11 |
+
|
| 12 |
+
clip_model = CLIPModel.from_pretrained(clip_model_name)
|
| 13 |
+
clip_processor = CLIPProcessor.from_pretrained(clip_model_name)
|
| 14 |
+
|
| 15 |
+
clip_model.eval()
|
| 16 |
|
| 17 |
def classify_image(image):
|
| 18 |
+
inputs = clip_processor(
|
| 19 |
+
text=WASTE_LABELS,
|
| 20 |
+
images=image,
|
| 21 |
+
return_tensors="pt",
|
| 22 |
+
padding=True
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
with torch.no_grad():
|
| 26 |
+
outputs = clip_model(**inputs)
|
| 27 |
+
|
| 28 |
+
logits = outputs.logits_per_image[0]
|
| 29 |
+
probs = logits.softmax(dim=0)
|
| 30 |
+
|
| 31 |
+
# return top 3
|
| 32 |
+
topk = torch.topk(probs, k=3)
|
| 33 |
+
|
| 34 |
+
results = {
|
| 35 |
+
WASTE_LABELS[i]: float(probs[i])
|
| 36 |
+
for i in topk.indices.tolist()
|
| 37 |
+
}
|
| 38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
return results
|
| 40 |
|
| 41 |
|
|
|
|
| 221 |
}
|
| 222 |
analysis = [waste_analyzation_v3, waste_analyzation_v2, waste_analyzation]
|
| 223 |
|
| 224 |
+
WASTE_LABELS = [
|
| 225 |
+
"cardboard",
|
| 226 |
+
"paper",
|
| 227 |
+
"plastic",
|
| 228 |
+
"glass",
|
| 229 |
+
"metal",
|
| 230 |
+
"food waste",
|
| 231 |
+
"organic waste",
|
| 232 |
+
"battery",
|
| 233 |
+
"electronic waste",
|
| 234 |
+
"textile",
|
| 235 |
+
"trash"
|
| 236 |
+
]
|
| 237 |
+
|
| 238 |
def classify_pipeline(image):
|
| 239 |
global top_label
|
| 240 |
|
|
|
|
| 248 |
if top_label is None:
|
| 249 |
return "Please classify an image first."
|
| 250 |
|
| 251 |
+
explanation = explain_recycling(top_label) #EXCEEDS CPU, WILL GIVE INACCURATE ANSWER. DO NOT RETURN
|
| 252 |
|
| 253 |
choice = random.randint(0, 2)
|
| 254 |
info = analysis[choice][top_label]
|
requirements.txt
CHANGED
|
@@ -4,5 +4,4 @@ Pillow
|
|
| 4 |
torch
|
| 5 |
transformers
|
| 6 |
sentencepiece
|
| 7 |
-
accelerate
|
| 8 |
-
|
|
|
|
| 4 |
torch
|
| 5 |
transformers
|
| 6 |
sentencepiece
|
| 7 |
+
accelerate
|
|
|