Spaces:
Sleeping
Sleeping
Commit ·
bf5e7b1
1
Parent(s): f4b8744
Classifier model change
Browse files
app.py
CHANGED
|
@@ -14,9 +14,52 @@ 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=
|
| 20 |
images=image,
|
| 21 |
return_tensors="pt",
|
| 22 |
padding=True
|
|
@@ -28,15 +71,23 @@ def classify_image(image):
|
|
| 28 |
logits = outputs.logits_per_image[0]
|
| 29 |
probs = logits.softmax(dim=0)
|
| 30 |
|
| 31 |
-
#
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
return
|
| 40 |
|
| 41 |
|
| 42 |
# ------------------ LOAD CHAT MODEL ------------------
|
|
@@ -104,7 +155,6 @@ def explain_recycling(class_label):
|
|
| 104 |
|
| 105 |
|
| 106 |
# ------------------ PIPELINE ------------------
|
| 107 |
-
top_label = None
|
| 108 |
waste_analyzation = {
|
| 109 |
"cardboard": {
|
| 110 |
"Recycling type": "Paper/Cardboard recycling",
|
|
@@ -221,19 +271,8 @@ waste_analyzation_v3 = {
|
|
| 221 |
}
|
| 222 |
analysis = [waste_analyzation_v3, waste_analyzation_v2, waste_analyzation]
|
| 223 |
|
| 224 |
-
|
| 225 |
-
|
| 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
|
|
|
|
| 14 |
|
| 15 |
clip_model.eval()
|
| 16 |
|
| 17 |
+
CLIP_LABELS = {
|
| 18 |
+
"paper/cardboard": [
|
| 19 |
+
"a photo of cardboard packaging waste",
|
| 20 |
+
"a photo of paper waste or newspaper"
|
| 21 |
+
],
|
| 22 |
+
"plastic": [
|
| 23 |
+
"a photo of plastic bottle or plastic packaging waste"
|
| 24 |
+
],
|
| 25 |
+
"glass": [
|
| 26 |
+
"a photo of glass bottle or glass container waste"
|
| 27 |
+
],
|
| 28 |
+
"metal": [
|
| 29 |
+
"a photo of metal can or metal packaging waste"
|
| 30 |
+
],
|
| 31 |
+
"organic waste": [
|
| 32 |
+
"a photo of food waste or leftovers",
|
| 33 |
+
"a photo of organic waste such as fruit peels or leaves"
|
| 34 |
+
],
|
| 35 |
+
"hazardous waste": [
|
| 36 |
+
"a photo of a used battery waste item",
|
| 37 |
+
"a photo of hazardous household waste"
|
| 38 |
+
],
|
| 39 |
+
"electronic waste": [
|
| 40 |
+
"a photo of electronic waste such as cables, chargers, or devices"
|
| 41 |
+
],
|
| 42 |
+
"textile": [
|
| 43 |
+
"a photo of textile waste or old clothing"
|
| 44 |
+
],
|
| 45 |
+
"general trash": [
|
| 46 |
+
"a photo of mixed general trash or landfill waste"
|
| 47 |
+
]
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
|
| 51 |
def classify_image(image):
|
| 52 |
+
all_prompts = []
|
| 53 |
+
prompt_to_category = {}
|
| 54 |
+
|
| 55 |
+
# Build prompt list
|
| 56 |
+
for category, prompts in CLIP_LABELS.items():
|
| 57 |
+
for p in prompts:
|
| 58 |
+
all_prompts.append(p)
|
| 59 |
+
prompt_to_category[p] = category
|
| 60 |
+
|
| 61 |
inputs = clip_processor(
|
| 62 |
+
text=all_prompts,
|
| 63 |
images=image,
|
| 64 |
return_tensors="pt",
|
| 65 |
padding=True
|
|
|
|
| 71 |
logits = outputs.logits_per_image[0]
|
| 72 |
probs = logits.softmax(dim=0)
|
| 73 |
|
| 74 |
+
# merge probabilities by category
|
| 75 |
+
category_scores = {}
|
| 76 |
+
for i, p in enumerate(all_prompts):
|
| 77 |
+
cat = prompt_to_category[p]
|
| 78 |
+
category_scores[cat] = category_scores.get(cat, 0) + float(probs[i])
|
| 79 |
+
total = sum(category_scores.values())
|
| 80 |
+
for k in category_scores:
|
| 81 |
+
category_scores[k] /= total
|
| 82 |
|
| 83 |
+
# Return top 3
|
| 84 |
+
top3 = sorted(
|
| 85 |
+
category_scores.items(),
|
| 86 |
+
key=lambda x: x[1],
|
| 87 |
+
reverse=True
|
| 88 |
+
)[:3]
|
| 89 |
|
| 90 |
+
return dict(top3)
|
| 91 |
|
| 92 |
|
| 93 |
# ------------------ LOAD CHAT MODEL ------------------
|
|
|
|
| 155 |
|
| 156 |
|
| 157 |
# ------------------ PIPELINE ------------------
|
|
|
|
| 158 |
waste_analyzation = {
|
| 159 |
"cardboard": {
|
| 160 |
"Recycling type": "Paper/Cardboard recycling",
|
|
|
|
| 271 |
}
|
| 272 |
analysis = [waste_analyzation_v3, waste_analyzation_v2, waste_analyzation]
|
| 273 |
|
| 274 |
+
top_label = None
|
| 275 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 276 |
|
| 277 |
def classify_pipeline(image):
|
| 278 |
global top_label
|