Spaces:
Sleeping
Sleeping
fix 2files
Browse files
app.py
CHANGED
|
@@ -26,8 +26,7 @@ class LargeNet(nn.Module):
|
|
| 26 |
x = x.squeeze(1) # Flatten to [batch_size]
|
| 27 |
return x
|
| 28 |
|
| 29 |
-
|
| 30 |
-
NEW_LABELS = ["paper", "cardboard", "other"]
|
| 31 |
|
| 32 |
# Load best model
|
| 33 |
net = LargeNet()
|
|
@@ -49,25 +48,15 @@ def predict(img):
|
|
| 49 |
output = net(img_t)
|
| 50 |
probs = F.softmax(output[0], dim=0)
|
| 51 |
|
| 52 |
-
|
| 53 |
-
for original_label, prob in zip(ORIGINAL_LABELS, probs):
|
| 54 |
-
if original_label == "paper":
|
| 55 |
-
new_probs[0] = prob
|
| 56 |
-
elif original_label == "cardboard":
|
| 57 |
-
new_probs[1] = prob
|
| 58 |
-
else:
|
| 59 |
-
# Accumulate probabilities if the model do not predict "paper" or "cardboard"
|
| 60 |
-
new_probs[2] += prob
|
| 61 |
-
|
| 62 |
-
return {label: float(prob) for label, prob in zip(NEW_LABELS, new_probs)}
|
| 63 |
|
| 64 |
# Create interface for uploading image and showing predictions
|
| 65 |
iface = gr.Interface(
|
| 66 |
fn=predict,
|
| 67 |
inputs=gr.Image(type="pil"),
|
| 68 |
-
outputs=gr.Label(num_top_classes=
|
| 69 |
title="Garbage Classifier",
|
| 70 |
-
description="Classify images into
|
| 71 |
)
|
| 72 |
|
| 73 |
if __name__ == "__main__":
|
|
|
|
| 26 |
x = x.squeeze(1) # Flatten to [batch_size]
|
| 27 |
return x
|
| 28 |
|
| 29 |
+
LABELS = ['battery', 'biological', 'cardboard', 'glass', 'metal', 'paper', 'plastic']
|
|
|
|
| 30 |
|
| 31 |
# Load best model
|
| 32 |
net = LargeNet()
|
|
|
|
| 48 |
output = net(img_t)
|
| 49 |
probs = F.softmax(output[0], dim=0)
|
| 50 |
|
| 51 |
+
return {label: float(prob) for label, prob in zip(LABELS, probs)}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
# Create interface for uploading image and showing predictions
|
| 54 |
iface = gr.Interface(
|
| 55 |
fn=predict,
|
| 56 |
inputs=gr.Image(type="pil"),
|
| 57 |
+
outputs=gr.Label(num_top_classes=7),
|
| 58 |
title="Garbage Classifier",
|
| 59 |
+
description="Classify garbage images into Battery, Biological, Cardboard, Glass', Metal, Paper, or Plastic"
|
| 60 |
)
|
| 61 |
|
| 62 |
if __name__ == "__main__":
|