Spaces:
Sleeping
Sleeping
Commit ·
1890393
1
Parent(s): d150902
Change model
Browse files
app.py
CHANGED
|
@@ -7,7 +7,7 @@ from PIL import Image
|
|
| 7 |
from datasets import load_dataset
|
| 8 |
from huggingface_hub import hf_hub_download
|
| 9 |
import tensorflow as tf
|
| 10 |
-
|
| 11 |
|
| 12 |
# --- LOAD CLASS LABELS FROM DATASET ---
|
| 13 |
ds = load_dataset("dvk65/TrashTypes")
|
|
@@ -19,24 +19,18 @@ class_names = ds["train"].features["label"].names
|
|
| 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))}
|
|
|
|
| 7 |
from datasets import load_dataset
|
| 8 |
from huggingface_hub import hf_hub_download
|
| 9 |
import tensorflow as tf
|
| 10 |
+
import tensorflow as tf
|
| 11 |
|
| 12 |
# --- LOAD CLASS LABELS FROM DATASET ---
|
| 13 |
ds = load_dataset("dvk65/TrashTypes")
|
|
|
|
| 19 |
# --- LOAD MODEL ---
|
| 20 |
REPO_ID = "dvk65/trash-classifier-resnet50"
|
| 21 |
FILENAME = "trashclassify_13.keras"
|
|
|
|
| 22 |
model_path = hf_hub_download(repo_id=REPO_ID, filename=FILENAME)
|
| 23 |
|
| 24 |
+
model = tf.keras.models.load_model(model_path)
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
|
| 27 |
def preprocess(image):
|
| 28 |
image = image.resize((224, 224))
|
| 29 |
+
image = np.array(image).astype("float32")
|
|
|
|
| 30 |
image = np.expand_dims(image, axis=0)
|
| 31 |
return image
|
| 32 |
|
| 33 |
def predict(img):
|
|
|
|
| 34 |
img = preprocess(img)
|
| 35 |
preds = model.predict(img)[0]
|
| 36 |
return {class_names[i]: float(preds[i]) for i in range(len(preds))}
|