Spaces:
Sleeping
Sleeping
Commit ·
d150902
1
Parent(s): 33f731b
Change model
Browse files- app.py +29 -30
- requirements.txt +5 -4
app.py
CHANGED
|
@@ -1,46 +1,45 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import pipeline
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
|
| 12 |
-
#
|
| 13 |
-
|
| 14 |
-
|
| 15 |
|
| 16 |
-
classifier = pipeline("image-classification", model="yangy50/garbage-classification")
|
| 17 |
|
| 18 |
|
| 19 |
-
#
|
| 20 |
-
|
| 21 |
-
|
| 22 |
|
| 23 |
-
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
|
| 38 |
def predict(img):
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
# return {class_names[i]: float(preds[i]) for i in range(len(preds))}
|
| 44 |
|
| 45 |
|
| 46 |
demo = gr.Interface(
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
# from transformers import pipeline
|
| 3 |
|
| 4 |
+
import numpy as np
|
| 5 |
+
from PIL import Image
|
| 6 |
|
| 7 |
+
from datasets import load_dataset
|
| 8 |
+
from huggingface_hub import hf_hub_download
|
| 9 |
+
import tensorflow as tf
|
| 10 |
+
from tensorflow.keras.applications.resnet50 import preprocess_input
|
| 11 |
|
| 12 |
+
# --- LOAD CLASS LABELS FROM DATASET ---
|
| 13 |
+
ds = load_dataset("dvk65/TrashTypes")
|
| 14 |
+
class_names = ds["train"].features["label"].names
|
| 15 |
|
| 16 |
+
# classifier = pipeline("image-classification", model="yangy50/garbage-classification")
|
| 17 |
|
| 18 |
|
| 19 |
+
# --- LOAD MODEL ---
|
| 20 |
+
REPO_ID = "dvk65/trash-classifier-resnet50"
|
| 21 |
+
FILENAME = "trashclassify_13.keras"
|
| 22 |
|
| 23 |
+
model_path = hf_hub_download(repo_id=REPO_ID, filename=FILENAME)
|
| 24 |
|
| 25 |
+
model = tf.keras.models.load_model(
|
| 26 |
+
model_path,
|
| 27 |
+
custom_objects={"preprocess_input": preprocess_input}
|
| 28 |
+
)
|
| 29 |
|
| 30 |
|
| 31 |
+
def preprocess(image):
|
| 32 |
+
image = image.resize((224, 224))
|
| 33 |
+
image = np.array(image)
|
| 34 |
+
image = preprocess_input(image)
|
| 35 |
+
image = np.expand_dims(image, axis=0)
|
| 36 |
+
return image
|
| 37 |
|
| 38 |
def predict(img):
|
| 39 |
+
|
| 40 |
+
img = preprocess(img)
|
| 41 |
+
preds = model.predict(img)[0]
|
| 42 |
+
return {class_names[i]: float(preds[i]) for i in range(len(preds))}
|
|
|
|
| 43 |
|
| 44 |
|
| 45 |
demo = gr.Interface(
|
requirements.txt
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
Pillow
|
| 3 |
-
transformers
|
| 4 |
-
torch
|
| 5 |
-
timm
|
|
|
|
| 1 |
+
tensorflow-cpu==2.16.1
|
| 2 |
+
huggingface_hub
|
| 3 |
+
datasets
|
| 4 |
+
gradio
|
| 5 |
+
numpy
|
| 6 |
Pillow
|
|
|
|
|
|
|
|
|